-
Notifications
You must be signed in to change notification settings - Fork 2
/
sommelier-inpututils.cc
198 lines (164 loc) · 6.72 KB
/
sommelier-inpututils.cc
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
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sommelier.h" // NOLINT(build/include_directory)
#include "sommelier-inpututils.h" // NOLINT(build/include_directory)
#include "sommelier-transform.h" // NOLINT(build/include_directory)
#include <algorithm>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <wayland-client.h>
#include <wayland-util.h>
struct sl_touchrecorder {
struct wl_touch* proxy;
sl_touchrecorder_frame_cb* frame_cb;
sl_touchrecorder_cancel_cb* cancel_cb;
void* data;
unsigned events_size;
unsigned events_alloc;
struct sl_touchrecorder_event events[32];
};
static void sl_touchrecorder_reset(struct sl_touchrecorder* recorder) {
recorder->events_size = 0;
}
static void sl_touchrecorder_record_event(struct sl_touchrecorder* recorder,
enum sl_touchrecorder_event_type type,
uint32_t serial,
uint32_t time,
struct wl_surface* surface,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) {
unsigned index = recorder->events_size;
if (index < recorder->events_alloc) {
struct sl_touchrecorder_event* event = &recorder->events[index];
if (index == recorder->events_alloc - 1) {
if (type == SL_TOUCHRECORDER_EVENT_FRAME ||
type == SL_TOUCHRECORDER_EVENT_CANCEL) {
event->type = type;
recorder->events_size++;
}
} else {
event->type = type;
event->serial = serial;
event->time = time;
event->surface = surface;
event->id = id;
event->x = x;
event->y = y;
recorder->events_size++;
}
}
if (type == SL_TOUCHRECORDER_EVENT_FRAME) {
if (recorder->frame_cb)
recorder->frame_cb(recorder->data, recorder);
sl_touchrecorder_reset(recorder);
} else if (type == SL_TOUCHRECORDER_EVENT_CANCEL) {
if (recorder->cancel_cb)
recorder->cancel_cb(recorder->data, recorder);
sl_touchrecorder_reset(recorder);
}
}
static void sl_touchrecorder_down(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
struct wl_surface* surface,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) {
struct sl_touchrecorder* recorder = static_cast<sl_touchrecorder*>(data);
sl_touchrecorder_record_event(recorder, SL_TOUCHRECORDER_EVENT_DOWN, serial,
time, surface, id, x, y);
}
static void sl_touchrecorder_up(void* data,
struct wl_touch* wl_touch,
uint32_t serial,
uint32_t time,
int32_t id) {
struct sl_touchrecorder* recorder = static_cast<sl_touchrecorder*>(data);
sl_touchrecorder_record_event(recorder, SL_TOUCHRECORDER_EVENT_UP, serial,
time, nullptr, id, 0, 0);
}
static void sl_touchrecorder_motion(void* data,
struct wl_touch* wl_touch,
uint32_t time,
int32_t id,
wl_fixed_t x,
wl_fixed_t y) {
struct sl_touchrecorder* recorder = static_cast<sl_touchrecorder*>(data);
sl_touchrecorder_record_event(recorder, SL_TOUCHRECORDER_EVENT_MOTION, 0,
time, nullptr, id, x, y);
}
static void sl_touchrecorder_frame(void* data, struct wl_touch* wl_touch) {
struct sl_touchrecorder* recorder = static_cast<sl_touchrecorder*>(data);
sl_touchrecorder_record_event(recorder, SL_TOUCHRECORDER_EVENT_FRAME, 0, 0,
nullptr, 0, 0, 0);
}
static void sl_touchrecorder_cancel(void* data, struct wl_touch* wl_touch) {
struct sl_touchrecorder* recorder = static_cast<sl_touchrecorder*>(data);
sl_touchrecorder_record_event(recorder, SL_TOUCHRECORDER_EVENT_CANCEL, 0, 0,
nullptr, 0, 0, 0);
}
static const struct wl_touch_listener sl_touchrecorder_listener = {
sl_touchrecorder_down, sl_touchrecorder_up, sl_touchrecorder_motion,
sl_touchrecorder_frame, sl_touchrecorder_cancel};
struct sl_touchrecorder* sl_touchrecorder_attach(
struct wl_touch* proxy,
sl_touchrecorder_frame_cb* frame_cb,
sl_touchrecorder_cancel_cb* cancel_cb,
void* data) {
struct sl_touchrecorder* recorder = new sl_touchrecorder();
recorder->proxy = proxy;
recorder->frame_cb = frame_cb;
recorder->cancel_cb = cancel_cb;
recorder->data = data;
recorder->events_size = 0;
recorder->events_alloc =
sizeof(recorder->events) / sizeof(recorder->events[0]);
wl_touch_add_listener(proxy, &sl_touchrecorder_listener, recorder);
return recorder;
}
void sl_touchrecorder_destroy(struct sl_touchrecorder* recorder) {
delete recorder;
}
void sl_touchrecorder_replay_to_listener(struct sl_touchrecorder* recorder,
const struct wl_touch_listener* listen,
void* data) {
for (unsigned i = 0; i < recorder->events_size; i++) {
struct sl_touchrecorder_event* event = &recorder->events[i];
switch (event->type) {
case SL_TOUCHRECORDER_EVENT_NONE:
break;
case SL_TOUCHRECORDER_EVENT_DOWN:
listen->down(data, recorder->proxy, event->serial, event->time,
event->surface, event->id, event->x, event->y);
break;
case SL_TOUCHRECORDER_EVENT_UP:
listen->up(data, recorder->proxy, event->serial, event->time,
event->id);
break;
case SL_TOUCHRECORDER_EVENT_MOTION:
listen->motion(data, recorder->proxy, event->time, event->id, event->x,
event->y);
break;
case SL_TOUCHRECORDER_EVENT_FRAME:
listen->frame(data, recorder->proxy);
break;
case SL_TOUCHRECORDER_EVENT_CANCEL:
listen->cancel(data, recorder->proxy);
break;
default:
abort();
}
}
}
void sl_touchrecorder_purge_id(struct sl_touchrecorder* recorder, int32_t id) {
for (unsigned i = 0; i < recorder->events_size; i++) {
struct sl_touchrecorder_event* event = &recorder->events[i];
if (event->type < SL_TOUCHRECORDER_EVENT_FRAME && event->id == id) {
event->type = SL_TOUCHRECORDER_EVENT_NONE;
}
}
}