| 1 | // LAF OS Library |
| 2 | // Copyright (C) 2019-2022 Igara Studio S.A. |
| 3 | // Copyright (C) 2012-2018 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef OS_EVENT_H_INCLUDED |
| 9 | #define OS_EVENT_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "base/paths.h" |
| 13 | #include "gfx/point.h" |
| 14 | #include "gfx/size.h" |
| 15 | #include "os/keys.h" |
| 16 | #include "os/pointer_type.h" |
| 17 | #include "os/window.h" |
| 18 | |
| 19 | #include <functional> |
| 20 | |
| 21 | #pragma push_macro("None") |
| 22 | #undef None // Undefine the X11 None macro |
| 23 | |
| 24 | namespace os { |
| 25 | |
| 26 | class Event { |
| 27 | public: |
| 28 | enum Type { |
| 29 | None, |
| 30 | |
| 31 | // macOS: When the Quit option is selected from the popup menu |
| 32 | // of the app icon in the dock bar. |
| 33 | CloseApp, |
| 34 | |
| 35 | // When the X is pressed in the current window. |
| 36 | CloseWindow, |
| 37 | |
| 38 | // When the window is resized/maximized/restored (any time the |
| 39 | // client area size of the window changes) |
| 40 | ResizeWindow, |
| 41 | |
| 42 | // Some files are dropped in the window |
| 43 | DropFiles, |
| 44 | |
| 45 | // Common mouse events |
| 46 | MouseEnter, |
| 47 | MouseLeave, |
| 48 | MouseMove, |
| 49 | MouseDown, |
| 50 | MouseUp, |
| 51 | MouseWheel, |
| 52 | MouseDoubleClick, |
| 53 | |
| 54 | // A key is pressed (or is autorepeated if the key is kept pressed) |
| 55 | KeyDown, |
| 56 | |
| 57 | // A key is released |
| 58 | KeyUp, |
| 59 | |
| 60 | // Pinch gesture with fingers to zoom in/out |
| 61 | TouchMagnify, |
| 62 | Callback, |
| 63 | }; |
| 64 | |
| 65 | enum MouseButton { |
| 66 | NoneButton, |
| 67 | LeftButton, |
| 68 | RightButton, |
| 69 | MiddleButton, |
| 70 | X1Button, |
| 71 | X2Button, |
| 72 | }; |
| 73 | |
| 74 | Event() : m_type(None), |
| 75 | m_window(nullptr), |
| 76 | m_scancode(kKeyNil), |
| 77 | m_modifiers(kKeyUninitializedModifier), |
| 78 | m_unicodeChar(0), |
| 79 | m_isDead(false), |
| 80 | m_repeat(0), |
| 81 | m_preciseWheel(false), |
| 82 | m_pointerType(PointerType::Unknown), |
| 83 | m_button(NoneButton), |
| 84 | m_magnification(0.0f), |
| 85 | m_pressure(0.0f) { |
| 86 | } |
| 87 | |
| 88 | Type type() const { return m_type; } |
| 89 | const WindowRef& window() const { return m_window; } |
| 90 | const base::paths& files() const { return m_files; } |
| 91 | // TODO Rename this to virtualKey(), which is the real |
| 92 | // meaning. Then we need another kind of "scan code" with the |
| 93 | // position in the keyboard, which might be useful to identify |
| 94 | // keys by its position (e.g. WASD keys in other keyboard |
| 95 | // layouts). |
| 96 | KeyScancode scancode() const { return m_scancode; } |
| 97 | KeyModifiers modifiers() const { return m_modifiers; } |
| 98 | int unicodeChar() const { return m_unicodeChar; } |
| 99 | bool isDeadKey() const { return m_isDead; } |
| 100 | int repeat() const { return m_repeat; } |
| 101 | gfx::Point position() const { return m_position; } |
| 102 | gfx::Point wheelDelta() const { return m_wheelDelta; } |
| 103 | |
| 104 | // We suppose that if we are receiving precise scrolling deltas, |
| 105 | // it means that the user is using a touch-like surface (trackpad, |
| 106 | // magic mouse scrolling, touch wacom tablet, etc.) |
| 107 | bool preciseWheel() const { return m_preciseWheel; } |
| 108 | |
| 109 | PointerType pointerType() const { return m_pointerType; } |
| 110 | MouseButton button() const { return m_button; } |
| 111 | float magnification() const { return m_magnification; } |
| 112 | float pressure() const { return m_pressure; } |
| 113 | |
| 114 | void setType(Type type) { m_type = type; } |
| 115 | void setWindow(const WindowRef& window) { m_window = window; } |
| 116 | void setFiles(const base::paths& files) { m_files = files; } |
| 117 | void setCallback(std::function<void()>&& func) { m_callback = std::move(func); } |
| 118 | |
| 119 | void setScancode(KeyScancode scancode) { m_scancode = scancode; } |
| 120 | void setModifiers(KeyModifiers modifiers) { m_modifiers = modifiers; } |
| 121 | void setUnicodeChar(int unicodeChar) { m_unicodeChar = unicodeChar; } |
| 122 | void setDeadKey(bool state) { m_isDead = state; } |
| 123 | void setRepeat(int repeat) { m_repeat = repeat; } |
| 124 | void setPosition(const gfx::Point& pos) { m_position = pos; } |
| 125 | void setWheelDelta(const gfx::Point& delta) { m_wheelDelta = delta; } |
| 126 | void setPreciseWheel(bool precise) { m_preciseWheel = precise; } |
| 127 | void setPointerType(PointerType pointerType) { m_pointerType = pointerType; } |
| 128 | void setButton(MouseButton button) { m_button = button; } |
| 129 | void setMagnification(float magnification) { m_magnification = magnification; } |
| 130 | void setPressure(float pressure) { m_pressure = pressure; } |
| 131 | |
| 132 | void execCallback() { if (m_callback) m_callback(); } |
| 133 | |
| 134 | private: |
| 135 | Type m_type; |
| 136 | WindowRef m_window; |
| 137 | base::paths m_files; |
| 138 | std::function<void()> m_callback; |
| 139 | KeyScancode m_scancode; |
| 140 | KeyModifiers m_modifiers; |
| 141 | int m_unicodeChar; |
| 142 | bool m_isDead; |
| 143 | int m_repeat; // repeat=0 means the first time the key is pressed |
| 144 | gfx::Point m_position; |
| 145 | gfx::Point m_wheelDelta; |
| 146 | bool m_preciseWheel; |
| 147 | PointerType m_pointerType; |
| 148 | MouseButton m_button; |
| 149 | |
| 150 | // For TouchMagnify event |
| 151 | float m_magnification; |
| 152 | |
| 153 | // Pressure of stylus used in mouse-like events |
| 154 | float m_pressure; |
| 155 | }; |
| 156 | |
| 157 | } // namespace os |
| 158 | |
| 159 | #pragma pop_macro("None") |
| 160 | |
| 161 | #endif |
| 162 | |