Debian Codegraph @

src/win/wtouch_input.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows touch input driver.
12 *
13 * By Michał Cichoń.
14 *
15 * See readme.txt for copyright information.
16 */
17
18 #include <windows.h>
19
20 #include "allegro5/allegro.h"
21 #include "allegro5/internal/aintern.h"
22 #include "allegro5/internal/aintern_driver.h"
23 #include "allegro5/internal/aintern_touch_input.h"
24 #include "allegro5/platform/aintwin.h"
25 #include "allegro5/internal/aintern_display.h"
26
27 ALLEGRO_DEBUG_CHANNEL("touch")
28
29 static ALLEGRO_TOUCH_INPUT_STATE touch_input_state;
30 static ALLEGRO_TOUCH_INPUT touch_input;
31 static bool installed = false;
32 static size_t initiali_time_stamp = ~0;
33
34
35 /* WinAPI import related stuff. */
36 static int touch_input_api_reference_counter = 0;
37 static void* user32_module = NULL;
38
39 CLOSETOUCHINPUTHANDLEPROC _al_win_close_touch_input_handle = NULL;
40 GETTOUCHINPUTINFOPROC _al_win_get_touch_input_info = NULL;
41 ISTOUCHWINDOWPROC _al_win_is_touch_window = NULL;
42 REGISTERTOUCHWINDOWPROC _al_win_register_touch_window = NULL;
43 UNREGISTERTOUCHWINDOWPROC _al_win_unregister_touch_window = NULL;
44
45 bool _al_win_init_touch_input_api(void)
46 {
47 /* Reference counter will be negative if we already tried to initialize the driver.
48 * Lack of touch input API is persistent state, so do not test for it again. */
49 if (touch_input_api_reference_counter < 0)
50 return false;
51
52 /* Increase API reference counter if it is already initialized. */
53 if (touch_input_api_reference_counter > 0)
54 {
55 touch_input_api_reference_counter++;
56 return true;
57 }
58
59 /* Open handle to user32.dll. This one should be always present. */
60 user32_module = _al_open_library("user32.dll");
61 if (NULL == user32_module)
62 return false;
63
64 _al_win_close_touch_input_handle = (CLOSETOUCHINPUTHANDLEPROC)_al_import_symbol(user32_module, "CloseTouchInputHandle");
65 _al_win_get_touch_input_info = (GETTOUCHINPUTINFOPROC) _al_import_symbol(user32_module, "GetTouchInputInfo");
66 _al_win_is_touch_window = (ISTOUCHWINDOWPROC) _al_import_symbol(user32_module, "IsTouchWindow");
67 _al_win_register_touch_window = (REGISTERTOUCHWINDOWPROC) _al_import_symbol(user32_module, "RegisterTouchWindow");
68 _al_win_unregister_touch_window = (UNREGISTERTOUCHWINDOWPROC)_al_import_symbol(user32_module, "UnregisterTouchWindow");
69
70 if (NULL == _al_win_close_touch_input_handle || NULL == _al_win_get_touch_input_info ||
71 NULL == _al_win_is_touch_window || NULL == _al_win_register_touch_window ||
72 NULL == _al_win_unregister_touch_window) {
73
74 _al_close_library(user32_module);
75
76 _al_win_close_touch_input_handle = NULL;
77 _al_win_get_touch_input_info = NULL;
78 _al_win_is_touch_window = NULL;
79 _al_win_register_touch_window = NULL;
80 _al_win_unregister_touch_window = NULL;
81
82 /* Mark as 'do not try this again'. */
83 touch_input_api_reference_counter = -1;
84 ALLEGRO_WARN("failed loading the touch input API\n");
85 return false;
86 }
87
88 /* Set initial reference count. */
89 touch_input_api_reference_counter = 1;
90 ALLEGRO_INFO("touch input API installed successfully\n");
91 return true;
92 }
93
94 void _al_win_exit_touch_input_api(void)
95 {
96 if (touch_input_api_reference_counter > 1)
97 {
98 --touch_input_api_reference_counter;
99 return;
100 }
101
102 if (touch_input_api_reference_counter != 1)
103 return;
104
105 _al_close_library(user32_module);
106
107 _al_win_close_touch_input_handle = NULL;
108 _al_win_get_touch_input_info = NULL;
109 _al_win_is_touch_window = NULL;
110 _al_win_register_touch_window = NULL;
111 _al_win_unregister_touch_window = NULL;
112
113 touch_input_api_reference_counter = 0;
114 }
115
116
117 /* Actual driver implementation. */
118
119 static void generate_touch_input_event(int type, double timestamp, int id, float x, float y, float dx, float dy, bool primary, ALLEGRO_DISPLAY_WIN *win_disp)
120 {
121 ALLEGRO_EVENT event;
122
123 bool want_touch_event = _al_event_source_needs_to_generate_event(&touch_input.es);
124 bool want_mouse_emulation_event = _al_event_source_needs_to_generate_event(&touch_input.mouse_emulation_es) && primary && al_is_mouse_installed();
125
126 if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_NONE)
127 want_mouse_emulation_event = false;
128 else if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_INCLUSIVE)
129 want_touch_event = want_mouse_emulation_event ? (want_touch_event && !primary) : want_touch_event;
130 else if (touch_input.mouse_emulation_mode == ALLEGRO_MOUSE_EMULATION_EXCLUSIVE)
131 want_touch_event = want_mouse_emulation_event ? false : want_touch_event;
132
133 if (!want_touch_event && !want_mouse_emulation_event)
134 return;
135
136 if (want_touch_event) {
137
138 event.touch.type = type;
139 event.touch.display = (ALLEGRO_DISPLAY*)win_disp;
140 event.touch.timestamp = timestamp;
141 event.touch.id = id;
142 event.touch.x = x;
143 event.touch.y = y;
144 event.touch.dx = dx;
145 event.touch.dy = dy;
146 event.touch.primary = primary;
147
148 _al_event_source_lock(&touch_input.es);
149 _al_event_source_emit_event(&touch_input.es, &event);
150 _al_event_source_unlock(&touch_input.es);
151 }
152
153 if (want_mouse_emulation_event) {
154
155 ALLEGRO_MOUSE_STATE state;
156
157 switch (type) {
158 case ALLEGRO_EVENT_TOUCH_BEGIN: type = ALLEGRO_EVENT_MOUSE_BUTTON_DOWN; break;
159 case ALLEGRO_EVENT_TOUCH_CANCEL:
160 case ALLEGRO_EVENT_TOUCH_END: type = ALLEGRO_EVENT_MOUSE_BUTTON_UP; break;
161 case ALLEGRO_EVENT_TOUCH_MOVE: type = ALLEGRO_EVENT_MOUSE_AXES; break;
162 }
163
164 al_get_mouse_state(&state);
165
166 event.mouse.type = type;
167 event.mouse.timestamp = timestamp;
168 event.mouse.display = (ALLEGRO_DISPLAY*)win_disp;
169 event.mouse.x = (int)x;
170 event.mouse.y = (int)y;
171 event.mouse.z = state.z;
172 event.mouse.w = state.w;
173 event.mouse.dx = (int)dx;
174 event.mouse.dy = (int)dy;
175 event.mouse.dz = 0;
176 event.mouse.dw = 0;
177 event.mouse.button = 1;
178 event.mouse.pressure = state.pressure;
179
180 al_set_mouse_xy(event.mouse.display, event.mouse.x, event.mouse.y);
181
182 _al_event_source_lock(&touch_input.mouse_emulation_es);
183 _al_event_source_emit_event(&touch_input.mouse_emulation_es, &event);
184 _al_event_source_unlock(&touch_input.mouse_emulation_es);
185 }
186 }
187
188 static bool init_touch_input(void)
189 {
190 unsigned i;
191 ALLEGRO_SYSTEM* system;
192
193 if (installed)
194 return false;
195
196 if (!_al_win_init_touch_input_api())
197 return false;
198
199 memset(&touch_input_state, 0, sizeof(touch_input_state));
200
201 _al_event_source_init(&touch_input.es);
202 _al_event_source_init(&touch_input.mouse_emulation_es);
203
204 touch_input.mouse_emulation_mode = ALLEGRO_MOUSE_EMULATION_TRANSPARENT;
205
206 installed = true;
207
208 system = al_get_system_driver();
209 for (i = 0; i < _al_vector_size(&system->displays); ++i) {
210 bool r;
211 ALLEGRO_DISPLAY_WIN *win_display = *((ALLEGRO_DISPLAY_WIN**)_al_vector_ref(&system->displays, i));
212 r = _al_win_register_touch_window(win_display->window, 0);
213 ALLEGRO_INFO("registering touch window %p: %d\n", win_display, r);
214 if (!r) {
215 ALLEGRO_ERROR("RegisterTouchWindow failed: %s\n", _al_win_last_error());
216 return false;
217 }
218 }
219
220 return true;
221 }
222
223
224 static void exit_touch_input(void)
225 {
226 if (!installed)
227 return;
228
229 memset(&touch_input_state, 0, sizeof(touch_input_state));
230
231 _al_event_source_free(&touch_input.es);
232 _al_event_source_free(&touch_input.mouse_emulation_es);
233
234 _al_win_exit_touch_input_api();
235
236 installed = false;
237 }
238
239
240 static ALLEGRO_TOUCH_INPUT* get_touch_input(void)
241 {
242 return &touch_input;
243 }
244
245
246 static void get_touch_input_state(ALLEGRO_TOUCH_INPUT_STATE *ret_state)
247 {
248 _al_event_source_lock(&touch_input.es);
249 *ret_state = touch_input_state;
250 _al_event_source_unlock(&touch_input.es);
251 }
252
253
254 static void set_mouse_emulation_mode(int mode)
255 {
256 if (touch_input.mouse_emulation_mode != mode) {
257
258 int i;
259
260 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; ++i) {
261
262 ALLEGRO_TOUCH_STATE* touch = touch_input_state.touches + i;
263
264 if (touch->id > 0) {
265 _al_win_touch_input_handle_cancel(touch->id, initiali_time_stamp,
266 touch->x, touch->y, touch->primary, (ALLEGRO_DISPLAY_WIN*)touch->display);
267 }
268 }
269
270 touch_input.mouse_emulation_mode = mode;
271 }
272 }
273
274
275 static ALLEGRO_TOUCH_STATE* find_free_touch_state(void)
276 {
277 int i;
278
279 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; ++i)
280 if (touch_input_state.touches[i].id <= 0)
281 return touch_input_state.touches + i;
282
283 return NULL;
284 }
285
286
287 static ALLEGRO_TOUCH_STATE* find_touch_state_with_id(int id)
288 {
289 int i;
290
291 for (i = 0; i < ALLEGRO_TOUCH_INPUT_MAX_TOUCH_COUNT; ++i)
292 if (touch_input_state.touches[i].id == id)
293 return touch_input_state.touches + i;
294
295 return NULL;
296 }
297
298
299 static double get_time_stamp(size_t timestamp)
300 {
301 return al_get_time() - ((timestamp - initiali_time_stamp) / 1000.0);
302 }
303
304
305 void _al_win_touch_input_set_time_stamp(size_t timestamp)
306 {
307 if (initiali_time_stamp != (size_t)~0)
308 initiali_time_stamp = timestamp;
309 }
310
311
312 void _al_win_touch_input_handle_begin(int id, size_t timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY_WIN *win_disp)
313 {
314 ALLEGRO_TOUCH_STATE* state = find_free_touch_state();
315 if (NULL == state)
316 return;
317
318 _al_event_source_lock(&touch_input.es);
319 state->id = id;
320 state->x = x;
321 state->y = y;
322 state->dx = 0.0f;
323 state->dy = 0.0f;
324 state->primary = primary;
325 _al_event_source_unlock(&touch_input.es);
326
327 generate_touch_input_event(ALLEGRO_EVENT_TOUCH_BEGIN, get_time_stamp(timestamp),
328 state->id, state->x, state->y, state->dx, state->dy, state->primary, win_disp);
329 }
330
331
332 void _al_win_touch_input_handle_end(int id, size_t timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY_WIN *win_disp)
333 {
334 ALLEGRO_TOUCH_STATE* state;
335 (void)primary;
336
337 state = find_touch_state_with_id(id);
338 if (NULL == state)
339 return;
340
341 _al_event_source_lock(&touch_input.es);
342 state->dx = x - state->x;
343 state->dy = y - state->y;
344 state->x = x;
345 state->y = y;
346 _al_event_source_unlock(&touch_input.es);
347
348 generate_touch_input_event(ALLEGRO_EVENT_TOUCH_END, get_time_stamp(timestamp),
349 state->id, state->x, state->y, state->dx, state->dy, state->primary, win_disp);
350
351 _al_event_source_lock(&touch_input.es);
352 memset(state, 0, sizeof(ALLEGRO_TOUCH_STATE));
353 _al_event_source_unlock(&touch_input.es);
354 }
355
356
357 void _al_win_touch_input_handle_move(int id, size_t timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY_WIN *win_disp)
358 {
359 ALLEGRO_TOUCH_STATE* state;
360 (void)primary;
361
362 state = find_touch_state_with_id(id);
363 if (NULL == state)
364 return;
365
366 _al_event_source_lock(&touch_input.es);
367 state->dx = x - state->x;
368 state->dy = y - state->y;
369 state->x = x;
370 state->y = y;
371 _al_event_source_unlock(&touch_input.es);
372
373 generate_touch_input_event(ALLEGRO_EVENT_TOUCH_MOVE, get_time_stamp(timestamp),
374 state->id, state->x, state->y, state->dx, state->dy, state->primary, win_disp);
375 }
376
377
378 void _al_win_touch_input_handle_cancel(int id, size_t timestamp, float x, float y, bool primary, ALLEGRO_DISPLAY_WIN *win_disp)
379 {
380 ALLEGRO_TOUCH_STATE* state;
381 (void)primary;
382
383 state = find_touch_state_with_id(id);
384 if (NULL == state)
385 return;
386
387 _al_event_source_lock(&touch_input.es);
388 state->dx = x - state->x;
389 state->dy = y - state->y;
390 state->x = x;
391 state->y = y;
392 _al_event_source_unlock(&touch_input.es);
393
394 generate_touch_input_event(ALLEGRO_EVENT_TOUCH_CANCEL, get_time_stamp(timestamp),
395 state->id, state->x, state->y, state->dx, state->dy, state->primary, win_disp);
396
397 _al_event_source_lock(&touch_input.es);
398 memset(state, 0, sizeof(ALLEGRO_TOUCH_STATE));
399 _al_event_source_unlock(&touch_input.es);
400 }
401
402
403 /* the driver vtable */
404 #define TOUCH_INPUT_WINAPI AL_ID('W','T','I','D')
405
406 static ALLEGRO_TOUCH_INPUT_DRIVER touch_input_driver =
407 {
408 TOUCH_INPUT_WINAPI,
409 init_touch_input,
410 exit_touch_input,
411 get_touch_input,
412 get_touch_input_state,
413 set_mouse_emulation_mode,
414 NULL
415 };
416
417 _AL_DRIVER_INFO _al_touch_input_driver_list[] =
418 {
419 {TOUCH_INPUT_WINAPI, &touch_input_driver, true},
420 {0, NULL, 0}
421 };