1 /* 2 * Copyright 2017 The Chromium Authors. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 */ 23 #ifndef _RUNTIME_ARC_INPUT_BRIDGE_PROTOCOL_H 24 #define _RUNTIME_ARC_INPUT_BRIDGE_PROTOCOL_H 25 26 #include <cstdint> 27 28 // Hack: major/minor may be defined by stdlib in error. 29 // see https://sourceware.org/bugzilla/show_bug.cgi?id=19239 30 #undef major 31 #undef minor 32 33 namespace arc { 34 35 // Location of the named pipe for communicating events 36 static const char* kArcInputBridgePipe = "/var/run/inputbridge/inputbridge"; 37 38 enum class InputEventType { 39 RESET = 0, 40 41 POINTER_ENTER, 42 POINTER_MOVE, 43 POINTER_LEAVE, 44 POINTER_BUTTON, 45 POINTER_SCROLL_X, 46 POINTER_SCROLL_Y, 47 POINTER_SCROLL_DISCRETE, 48 POINTER_SCROLL_STOP, 49 POINTER_FRAME, 50 51 TOUCH_DOWN, 52 TOUCH_MOVE, 53 TOUCH_UP, 54 TOUCH_CANCEL, 55 TOUCH_SHAPE, 56 TOUCH_TOOL_TYPE, 57 TOUCH_FORCE, 58 TOUCH_TILT, 59 TOUCH_FRAME, 60 61 GESTURE_PINCH_BEGIN, 62 GESTURE_PINCH_UPDATE, 63 GESTURE_PINCH_END, 64 65 GESTURE_SWIPE_BEGIN, 66 GESTURE_SWIPE_UPDATE, 67 GESTURE_SWIPE_END, 68 69 KEY, 70 KEY_MODIFIERS, 71 KEY_RESET, 72 73 GAMEPAD_CONNECTED, 74 GAMEPAD_DISCONNECTED, 75 GAMEPAD_AXIS, 76 GAMEPAD_BUTTON, 77 GAMEPAD_FRAME, 78 79 // event for reporting the state of a switch, such as a lid switch 80 SWITCH, 81 82 // event for reporting dimensions and properties of the display 83 DISPLAY_METRICS 84 } __attribute__((packed)); 85 86 struct PointerArgs { 87 float x; 88 float y; 89 bool discrete; 90 } __attribute__((packed)); 91 92 struct KeyArgs { 93 // Android AKEYCODE_ code after application of keyboard layout. 94 uint32_t keyCode; 95 // Linux KEY_ scan code as it was received from the kernel. 96 uint32_t scanCode; 97 // 0 = released, 1 = pressed. 98 uint32_t state; 99 // serial number of this key events in wayland. 100 uint32_t serial; 101 } __attribute__((packed)); 102 103 struct MetaArgs { 104 // Android AMETA_ bitmask of meta state. 105 uint32_t metaState; 106 }; 107 108 struct ButtonArgs { 109 // Android AMOTION_EVENT_BUTTON_ code 110 uint32_t code; 111 // 0 = released, 1 = pressed. 112 uint32_t state; 113 }; 114 115 enum class TouchToolType { TOUCH = 0, PEN, ERASER } __attribute__((packed)); 116 117 struct TouchArgs { 118 int32_t id; 119 union { 120 float x; 121 float major; 122 float force; 123 TouchToolType tool; 124 }; 125 union { 126 float y; 127 float minor; 128 }; 129 } __attribute__((packed)); 130 131 struct GestureArgs { 132 uint32_t fingers; 133 bool cancelled; 134 } __attribute__((packed)); 135 136 struct GesturePinchArgs { 137 float scale; 138 } __attribute__((packed)); 139 140 struct GestureSwipeArgs { 141 float dx; 142 float dy; 143 } __attribute__((packed)); 144 145 struct GamepadArgs { 146 int32_t id; 147 union { 148 // Used by GAMEPAD_BUTTON and GAMEPAD_AXIS respectively 149 int32_t button; 150 int32_t axis; 151 }; 152 bool pressed; 153 float value; 154 } __attribute__((packed)); 155 156 struct SwitchArgs { 157 // Switch ID defined at bionic/libc/kernel/uapi/linux/input.h. 158 int32_t switchCode; 159 // Switch value defined at frameworks/native/include/android/input.h. 160 int32_t state; 161 } __attribute__((packed)); 162 163 struct DisplayMetricsArgs { 164 int32_t width; 165 int32_t height; 166 float ui_scale; 167 } __attribute__((packed)); 168 169 170 // Union-like class describing an event. The InputEventType describes which 171 // of member of the union contains the data of this event. 172 struct BridgeInputEvent { 173 uint64_t timestamp; 174 int32_t taskId; 175 int32_t windowId; 176 177 InputEventType type; 178 179 union { 180 PointerArgs pointer; 181 KeyArgs key; 182 TouchArgs touch; 183 MetaArgs meta; 184 ButtonArgs button; 185 GestureArgs gesture; 186 GesturePinchArgs gesture_pinch; 187 GestureSwipeArgs gesture_swipe; 188 GamepadArgs gamepad; 189 SwitchArgs switches; 190 DisplayMetricsArgs display_metrics; 191 }; 192 ResetEventBridgeInputEvent193 static BridgeInputEvent ResetEvent(uint64_t timestamp) { 194 return {timestamp, -1, 0, InputEventType::RESET, {}}; 195 } 196 KeyEventBridgeInputEvent197 static BridgeInputEvent KeyEvent(uint64_t timestamp, uint32_t keyCode, uint32_t scanCode, 198 uint32_t state, uint32_t serial) { 199 BridgeInputEvent event{timestamp, -1, 0, InputEventType::KEY, {}}; 200 event.key = {keyCode, scanCode, state, serial}; 201 return event; 202 } 203 KeyModifiersEventBridgeInputEvent204 static BridgeInputEvent KeyModifiersEvent(uint64_t timestamp, uint32_t modifiers) { 205 BridgeInputEvent event{timestamp, -1, 0, InputEventType::KEY_MODIFIERS, {}}; 206 event.meta = {modifiers}; 207 return event; 208 } 209 210 static BridgeInputEvent PointerEvent(uint64_t timestamp, InputEventType type, float x = 0, 211 float y = 0, bool discrete = false) { 212 BridgeInputEvent event{timestamp, -1, 0, type, {}}; 213 event.pointer = {x, y, discrete}; 214 return event; 215 } 216 PointerButtonEventBridgeInputEvent217 static BridgeInputEvent PointerButtonEvent(uint64_t timestamp, uint32_t code, uint32_t state) { 218 BridgeInputEvent event{timestamp, -1, 0, InputEventType::POINTER_BUTTON, {}}; 219 event.button = {code, state}; 220 return event; 221 } 222 PinchBeginEventBridgeInputEvent223 static BridgeInputEvent PinchBeginEvent(uint64_t timestamp) { 224 return {timestamp, -1, 0, InputEventType::GESTURE_PINCH_BEGIN, {}}; 225 } 226 PinchUpdateEventBridgeInputEvent227 static BridgeInputEvent PinchUpdateEvent(uint64_t timestamp, float scale) { 228 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GESTURE_PINCH_UPDATE, {}}; 229 event.gesture_pinch.scale = scale; 230 return event; 231 } 232 PinchEndEventBridgeInputEvent233 static BridgeInputEvent PinchEndEvent(uint64_t timestamp, bool cancelled) { 234 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GESTURE_PINCH_END, {}}; 235 event.gesture.cancelled = cancelled; 236 return event; 237 } 238 SwipeBeginEventBridgeInputEvent239 static BridgeInputEvent SwipeBeginEvent(uint64_t timestamp, uint32_t fingers) { 240 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GESTURE_SWIPE_BEGIN, {}}; 241 event.gesture.fingers = fingers; 242 return event; 243 } 244 SwipeUpdateEventBridgeInputEvent245 static BridgeInputEvent SwipeUpdateEvent(uint64_t timestamp, float dx, float dy) { 246 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GESTURE_SWIPE_UPDATE, {}}; 247 event.gesture_swipe.dx = dx; 248 event.gesture_swipe.dy = dy; 249 return event; 250 } 251 SwipeEndEventBridgeInputEvent252 static BridgeInputEvent SwipeEndEvent(uint64_t timestamp, bool cancelled) { 253 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GESTURE_SWIPE_END, {}}; 254 event.gesture.cancelled = cancelled; 255 return event; 256 } 257 GamepadConnectedEventBridgeInputEvent258 static BridgeInputEvent GamepadConnectedEvent(int32_t id) { 259 BridgeInputEvent event{0, -1, 0, InputEventType::GAMEPAD_CONNECTED, {}}; 260 event.gamepad.id = id; 261 return event; 262 } 263 GamepadDisconnectedEventBridgeInputEvent264 static BridgeInputEvent GamepadDisconnectedEvent(int32_t id) { 265 BridgeInputEvent event{0, -1, 0, InputEventType::GAMEPAD_DISCONNECTED, {}}; 266 event.gamepad.id = id; 267 return event; 268 } 269 GamepadAxisEventBridgeInputEvent270 static BridgeInputEvent GamepadAxisEvent(uint64_t timestamp, int32_t id, int32_t axis, 271 float value) { 272 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GAMEPAD_AXIS, {}}; 273 event.gamepad.id = id; 274 event.gamepad.axis = axis; 275 event.gamepad.value = value; 276 return event; 277 } 278 GamepadButtonEventBridgeInputEvent279 static BridgeInputEvent GamepadButtonEvent(uint64_t timestamp, int32_t id, int32_t button, 280 bool pressed, float value) { 281 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GAMEPAD_BUTTON, {}}; 282 event.gamepad.id = id; 283 event.gamepad.button = button; 284 event.gamepad.pressed = pressed; 285 event.gamepad.value = value; 286 return event; 287 } 288 GamepadFrameEventBridgeInputEvent289 static BridgeInputEvent GamepadFrameEvent(uint64_t timestamp, int32_t id) { 290 BridgeInputEvent event{timestamp, -1, 0, InputEventType::GAMEPAD_FRAME, {}}; 291 event.gamepad.id = id; 292 return event; 293 } 294 SwitchEventBridgeInputEvent295 static BridgeInputEvent SwitchEvent(uint64_t timestamp, int32_t switchCode, int32_t state) { 296 BridgeInputEvent event{timestamp, -1, 0, InputEventType::SWITCH, {}}; 297 event.switches.switchCode = switchCode; 298 event.switches.state = state; 299 return event; 300 } 301 } __attribute__((packed)); 302 303 } // namespace arc 304 305 #endif // _RUNTIME_ARC_INPUT_BRIDGE_PROTOCOL_H 306