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