Debian Codegraph @

src/sdl/sdl_touch.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * SDL touch driver.
12 *
13 * See LICENSE.txt for copyright information.
14 */
15 #include "allegro5/allegro.h"
16 #include "allegro5/internal/aintern_system.h"
17 #include "allegro5/platform/allegro_internal_sdl.h"
18
19 ALLEGRO_DEBUG_CHANNEL("SDL")
20
21 typedef struct ALLEGRO_TOUCH_INPUT_SDL
22 {
23 ALLEGRO_TOUCH_INPUT touch_input;
24 ALLEGRO_TOUCH_INPUT_STATE state;
25 ALLEGRO_DISPLAY *display;
26 int touches;
27 } ALLEGRO_TOUCH_INPUT_SDL;
28
29 static ALLEGRO_TOUCH_INPUT_DRIVER *vt;
30 static ALLEGRO_TOUCH_INPUT_SDL *touch_input;
31 static ALLEGRO_MOUSE_STATE mouse_state;
32
33 static void generate_touch_input_event(unsigned int type, double timestamp,
34 int id, float x, float y, float dx, float dy, bool primary,
35 ALLEGRO_DISPLAY *disp)
36 {
37 ALLEGRO_EVENT event;
38
39 bool want_touch_event = _al_event_source_needs_to_generate_event(&touch_input->touch_input.es);
40 bool want_mouse_emulation_event;
41
42 if (touch_input->touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_5_0_x) {
43 want_mouse_emulation_event = _al_event_source_needs_to_generate_event(&touch_input->touch_input.mouse_emulation_es) && al_is_mouse_installed();
44 }
45 else {
46 want_mouse_emulation_event = _al_event_source_needs_to_generate_event(&touch_input->touch_input.mouse_emulation_es) && primary && al_is_mouse_installed();
47 }
48
49 if (touch_input->touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_NONE)
50 want_mouse_emulation_event = false;
51 else if (touch_input->touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_INCLUSIVE)
52 want_touch_event = al_is_mouse_installed() ? (want_touch_event && !primary) : want_touch_event;
53 else if (touch_input->touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_EXCLUSIVE)
54 want_touch_event = al_is_mouse_installed() ? false : want_touch_event;
55
56
57 if (!want_touch_event && !want_mouse_emulation_event)
58 return;
59
60 if (want_touch_event) {
61 event.touch.type = type;
62 event.touch.display = (ALLEGRO_DISPLAY*)disp;
63 event.touch.timestamp = timestamp;
64 event.touch.id = id;
65 event.touch.x = x;
66 event.touch.y = y;
67 event.touch.dx = dx;
68 event.touch.dy = dy;
69 event.touch.primary = primary;
70
71 _al_event_source_lock(&touch_input->touch_input.es);
72 _al_event_source_emit_event(&touch_input->touch_input.es, &event);
73 _al_event_source_unlock(&touch_input->touch_input.es);
74 }
75
76 if (touch_input->touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_NONE) {
77 mouse_state.x = (int)x;
78 mouse_state.y = (int)y;
79 if (type == ALLEGRO_EVENT_TOUCH_BEGIN)
80 mouse_state.buttons++;
81 else if (type == ALLEGRO_EVENT_TOUCH_END)
82 mouse_state.buttons--;
83
84 mouse_state.pressure = mouse_state.buttons ? 1.0 : 0.0; /* TODO */
85
86 _al_event_source_lock(&touch_input->touch_input.mouse_emulation_es);
87 if (want_mouse_emulation_event) {
88
89 switch (type) {
90 case ALLEGRO_EVENT_TOUCH_BEGIN: type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN; break;
91 case ALLEGRO_EVENT_TOUCH_CANCEL:
92 case ALLEGRO_EVENT_TOUCH_END: type = ALLEGRO_EVENT_MOUSE_BUTTON_UP; break;
93 case ALLEGRO_EVENT_TOUCH_MOVE: type = ALLEGRO_EVENT_MOUSE_AXES; break;
94 }
95
96 event.mouse.type = type;
97 event.mouse.timestamp = timestamp;
98 event.mouse.display = (ALLEGRO_DISPLAY*)disp;
99 event.mouse.x = (int)x;
100 event.mouse.y = (int)y;
101 event.mouse.dx = (int)dx;
102 event.mouse.dy = (int)dy;
103 event.mouse.dz = 0;
104 event.mouse.dw = 0;
105 if (touch_input->touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_5_0_x) {
106 event.mouse.button = 1;
107 }
108 else {
109 event.mouse.button = id;
110 }
111 event.mouse.pressure = mouse_state.pressure;
112
113 if (touch_input->touch_input.mouse_emulation_mode != ALLEGRO_MOUSE_EMULATION_5_0_x) {
114 al_set_mouse_xy(event.mouse.display, event.mouse.x, event.mouse.y);
115 }
116
117 _al_event_source_emit_event(&touch_input->touch_input.mouse_emulation_es, &event);
118 }
119 _al_event_source_unlock(&touch_input->touch_input.mouse_emulation_es);
120 }
121 }
122
123 static int find_free_touch_state_index(void)
124 {
125 int i;
126
127 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++)
128 if (touch_input->state.touches[i].id < 0)
129 return i;
130
131 return -1;
132 }
133
134 static int find_touch_state_index_with_id(int id)
135 {
136 int i;
137
138 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++)
139 if (touch_input->state.touches[i].id == id)
140 return i;
141
142 return -1;
143 }
144
145 void _al_sdl_touch_input_event(SDL_Event *e)
146 {
147 if (!touch_input)
148 return;
149
150 ALLEGRO_EVENT_TYPE type;
151
152 ALLEGRO_DISPLAY *d = NULL;
153 /* Use the first display as event source if we have any displays. */
154 ALLEGRO_SYSTEM *s = al_get_system_driver();
155 if (_al_vector_size(&s->displays) > 0) {
156 void **v = (void **)_al_vector_ref(&s->displays, 0);
157 d = *v;
158 }
159 if (!d) {
160 return;
161 }
162
163 int touch_idx = find_touch_state_index_with_id(e->tfinger.fingerId);
164
165 if (touch_idx < 0)
166 touch_idx = find_free_touch_state_index();
167 if (touch_idx < 0)
168 return;
169
170 // SDL2 touch events also handle indirect touch devices that aren't directly related to the display.
171 // Allegro doesn't really have an equivalent in its API, so filter them out.
172 #if SDL_VERSION_ATLEAST(2,0,10)
173 if (SDL_GetTouchDeviceType(e->tfinger.touchId) != SDL_TOUCH_DEVICE_DIRECT)
174 return;
175 #endif
176
177 touch_input->state.touches[touch_idx].x = e->tfinger.x * al_get_display_width(d);
178 touch_input->state.touches[touch_idx].y = e->tfinger.y * al_get_display_height(d);
179 touch_input->state.touches[touch_idx].dx = e->tfinger.dx * al_get_display_width(d);
180 touch_input->state.touches[touch_idx].dy = e->tfinger.dy * al_get_display_height(d);
181
182 if (e->type == SDL_FINGERDOWN) {
183 type = ALLEGRO_EVENT_TOUCH_BEGIN;
184 touch_input->state.touches[touch_idx].id = e->tfinger.fingerId;
185 touch_input->state.touches[touch_idx].primary = (touch_input->touches == 0);
186 touch_input->touches++;
187 }
188 else if (e->type == SDL_FINGERMOTION) {
189 type = ALLEGRO_EVENT_TOUCH_MOVE;
190 }
191 else if (e->type == SDL_FINGERUP) {
192 type = ALLEGRO_EVENT_TOUCH_END;
193 touch_input->touches--;
194 touch_input->state.touches[touch_idx].id = -1;
195 } else {
196 return;
197 }
198
199 generate_touch_input_event(type, e->tfinger.timestamp / 1000.0, touch_idx,
200 touch_input->state.touches[touch_idx].x,
201 touch_input->state.touches[touch_idx].y,
202 touch_input->state.touches[touch_idx].dx,
203 touch_input->state.touches[touch_idx].dy,
204 touch_input->state.touches[touch_idx].primary,
205 d);
206 }
207
208 static bool sdl_init_touch_input(void)
209 {
210 touch_input = al_calloc(1, sizeof *touch_input);
211 _al_event_source_init(&touch_input->touch_input.es);
212
213 _al_event_source_init(&touch_input->touch_input.mouse_emulation_es);
214 touch_input->touch_input.mouse_emulation_mode = ALLEGRO_MOUSE_EMULATION_TRANSPARENT;
215
216 int i;
217 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; i++) {
218 touch_input->state.touches[i].id = -1;
219 }
220
221 return true;
222 }
223
224 static void sdl_exit_touch_input(void)
225 {
226 }
227
228 static ALLEGRO_TOUCH_INPUT *sdl_get_touch_input(void)
229 {
230 return &touch_input->touch_input;
231 }
232
233
234 static void sdl_get_touch_input_state(ALLEGRO_TOUCH_INPUT_STATE *ret_state)
235 {
236 _al_event_source_lock(&touch_input->touch_input.es);
237 *ret_state = touch_input->state;
238 _al_event_source_unlock(&touch_input->touch_input.es);
239 }
240
241 static void touch_input_handle_cancel(int index, double timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY *disp)
242 {
243 ALLEGRO_TOUCH_STATE* state = touch_input->state.touches + index;
244 (void)primary;
245
246 if (NULL == state)
247 return;
248
249 _al_event_source_lock(&touch_input->touch_input.es);
250 state->dx = x - state->x;
251 state->dy = y - state->y;
252 state->x = x;
253 state->y = y;
254 _al_event_source_unlock(&touch_input->touch_input.es);
255
256 generate_touch_input_event(ALLEGRO_EVENT_TOUCH_CANCEL, timestamp,
257 state->id, state->x, state->y, state->dx, state->dy, state->primary, disp);
258
259 _al_event_source_lock(&touch_input->touch_input.es);
260 state->id = -1;
261 _al_event_source_unlock(&touch_input->touch_input.es);
262 }
263
264 static void sdl_set_mouse_emulation_mode(int mode)
265 {
266 if (touch_input->touch_input.mouse_emulation_mode != mode) {
267
268 int i;
269
270 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; ++i) {
271
272 ALLEGRO_TOUCH_STATE* touch = touch_input->state.touches + i;
273
274 if (touch->id > 0) {
275 touch_input_handle_cancel(i, al_get_time(),
276 touch->x, touch->y, touch->primary, touch->display);
277 }
278 }
279
280 touch_input->touch_input.mouse_emulation_mode = mode;
281 }
282 }
283
284 ALLEGRO_TOUCH_INPUT_DRIVER *_al_sdl_touch_input_driver(void)
285 {
286 if (vt)
287 return vt;
288
289 vt = al_calloc(1, sizeof *vt);
290 vt->init_touch_input = sdl_init_touch_input;
291 vt->exit_touch_input = sdl_exit_touch_input;
292 vt->get_touch_input = sdl_get_touch_input;
293 vt->get_touch_input_state = sdl_get_touch_input_state;
294 vt->set_mouse_emulation_mode = sdl_set_mouse_emulation_mode;
295
296 return vt;
297 }