Debian Codegraph @

src/x/xmousenu.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * X-Windows mouse module.
12 *
13 * By Peter Wang.
14 *
15 * Original by Michael Bukin.
16 *
17 * See readme.txt for copyright information.
18 */
19
20
21 #define ALLEGRO_NO_COMPATIBILITY
22
23 #include <X11/Xlib.h>
24 #include <stdio.h>
25
26 #include "allegro5/allegro.h"
27 #include "allegro5/internal/aintern.h"
28 #include "allegro5/internal/aintern_mouse.h"
29 #include "allegro5/internal/aintern_x.h"
30 #include "allegro5/internal/aintern_xdisplay.h"
31 #include "allegro5/internal/aintern_xmouse.h"
32 #include "allegro5/internal/aintern_xsystem.h"
33
34 #ifdef ALLEGRO_RASPBERRYPI
35 #include "allegro5/internal/aintern_raspberrypi.h"
36 #include "allegro5/internal/aintern_vector.h"
37 #define ALLEGRO_SYSTEM_XGLX ALLEGRO_SYSTEM_RASPBERRYPI
38 #define ALLEGRO_DISPLAY_XGLX ALLEGRO_DISPLAY_RASPBERRYPI
39 #endif
40
41 ALLEGRO_DEBUG_CHANNEL("mouse")
42
43 typedef struct ALLEGRO_MOUSE_XWIN
44 {
45 ALLEGRO_MOUSE parent;
46 ALLEGRO_MOUSE_STATE state;
47 int min_x, min_y;
48 int max_x, max_y;
49 } ALLEGRO_MOUSE_XWIN;
50
51
52
53 static bool xmouse_installed = false;
54
55 /* the one and only mouse object */
56 static ALLEGRO_MOUSE_XWIN the_mouse;
57
58
59
60 /* forward declarations */
61 static bool xmouse_init(void);
62 static void xmouse_exit(void);
63 static ALLEGRO_MOUSE *xmouse_get_mouse(void);
64 static unsigned int xmouse_get_mouse_num_buttons(void);
65 static unsigned int xmouse_get_mouse_num_axes(void);
66 static bool xmouse_set_mouse_xy(ALLEGRO_DISPLAY *,int x, int y);
67 static bool xmouse_set_mouse_axis(int which, int z);
68 static void xmouse_get_state(ALLEGRO_MOUSE_STATE *ret_state);
69
70 static void wheel_motion_handler(int x_button, ALLEGRO_DISPLAY *display);
71 static unsigned int x_button_to_al_button(unsigned int x_button);
72 static void generate_mouse_event(unsigned int type,
73 int x, int y, int z, int w, float pressure,
74 int dx, int dy, int dz, int dw,
75 unsigned int button,
76 ALLEGRO_DISPLAY *display);
77
78
79
80 /* the driver vtable */
81 #define MOUSEDRV_XWIN AL_ID('X','W','I','N')
82
83 static ALLEGRO_MOUSE_DRIVER mousedrv_xwin =
84 {
85 MOUSEDRV_XWIN,
86 "",
87 "",
88 "X-Windows mouse",
89 xmouse_init,
90 xmouse_exit,
91 xmouse_get_mouse,
92 xmouse_get_mouse_num_buttons,
93 xmouse_get_mouse_num_axes,
94 xmouse_set_mouse_xy,
95 xmouse_set_mouse_axis,
96 xmouse_get_state
97 };
98
99
100
101 ALLEGRO_MOUSE_DRIVER *_al_xwin_mouse_driver(void)
102 {
103 return &mousedrv_xwin;
104 }
105
106
107 /* xmouse_init:
108 * Initialise the driver.
109 */
110 static bool xmouse_init(void)
111 {
112 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
113 ALLEGRO_DISPLAY *display;
114 int x, y;
115
116 if (system->x11display == NULL)
117 return false;
118 if (xmouse_installed)
119 return false;
120
121 /* Don't clobber mouse position in case the display was created before
122 * the mouse is installed.
123 */
124 display = the_mouse.state.display;
125 x = the_mouse.state.x;
126 y = the_mouse.state.y;
127 memset(&the_mouse, 0, sizeof the_mouse);
128 the_mouse.state.display = display;
129 the_mouse.state.x = x;
130 the_mouse.state.y = y;
131
132 _al_event_source_init(&the_mouse.parent.es);
133
134 xmouse_installed = true;
135
136 return true;
137 }
138
139
140
141 /* xmouse_exit:
142 * Shut down the mouse driver.
143 */
144 static void xmouse_exit(void)
145 {
146 if (!xmouse_installed)
147 return;
148 xmouse_installed = false;
149
150 _al_event_source_free(&the_mouse.parent.es);
151 }
152
153
154
155 /* xmouse_get_mouse:
156 * Returns the address of a ALLEGRO_MOUSE structure representing the mouse.
157 */
158 static ALLEGRO_MOUSE *xmouse_get_mouse(void)
159 {
160 ASSERT(xmouse_installed);
161
162 return (ALLEGRO_MOUSE *)&the_mouse;
163 }
164
165
166
167 /* xmouse_get_mouse_num_buttons:
168 * Return the number of buttons on the mouse.
169 */
170 static unsigned int xmouse_get_mouse_num_buttons(void)
171 {
172 int num_buttons;
173 unsigned char map[32];
174 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
175
176 ASSERT(xmouse_installed);
177
178 _al_mutex_lock(&system->lock);
179 num_buttons = XGetPointerMapping(system->x11display, map, sizeof(map));
180 _al_mutex_unlock(&system->lock);
181
182 if (num_buttons > (int)sizeof(map))
183 num_buttons = sizeof(map);
184
185 #ifdef DEBUGMODE
186 char debug[num_buttons * 4 + 1];
187 debug[0] = 0;
188 int i;
189 for (i = 0; i < num_buttons; i++) {
190 sprintf(debug + strlen(debug), "%2d,", map[i]);
191 }
192 ALLEGRO_DEBUG("XGetPointerMapping: %s\n", debug);
193 #endif
194
195 if (num_buttons < 1)
196 num_buttons = 1;
197
198 return num_buttons;
199 }
200
201
202
203 /* xmouse_get_mouse_num_axes:
204 * Return the number of axes on the mouse.
205 */
206 static unsigned int xmouse_get_mouse_num_axes(void)
207 {
208 ASSERT(xmouse_installed);
209
210 /* TODO: is there a way to detect whether z/w axis actually exist? */
211 return 4;
212 }
213
214
215
216 /* xmouse_set_mouse_xy:
217 * Set the mouse position. Return true if successful.
218 */
219 static bool xmouse_set_mouse_xy(ALLEGRO_DISPLAY *display, int x, int y)
220 {
221 if (!xmouse_installed)
222 return false;
223
224 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
225 Display *x11display = system->x11display;
226 ALLEGRO_DISPLAY_XGLX *d = (void *)display;
227
228 int window_width = al_get_display_width(display);
229 int window_height = al_get_display_height(display);
230 if (x < 0 || y < 0 || x >= window_width || y >= window_height)
231 return false;
232
233 the_mouse.state.x = x;
234 the_mouse.state.y = y;
235
236 #ifdef ALLEGRO_RASPBERRYPI
237 float scale_x, scale_y;
238 _al_raspberrypi_get_mouse_scale_ratios(&scale_x, &scale_y);
239 x /= scale_x;
240 y /= scale_y;
241 #endif
242
243 _al_mutex_lock(&system->lock);
244
245 /* We prepend the mouse motion event generated by XWarpPointer
246 * with our own event to distinguish real and generated
247 * events.
248 */
249 XEvent event;
250 memset(&event, 0, sizeof event);
251 event.xclient.type = ClientMessage;
252 event.xclient.serial = 0;
253 event.xclient.send_event = True;
254 event.xclient.display = x11display;
255 event.xclient.window = d->window;
256 event.xclient.message_type = system->AllegroAtom;
257 event.xclient.format = 32;
258 XSendEvent(x11display, d->window, False, NoEventMask, &event);
259
260 XWarpPointer(x11display, None, d->window, 0, 0, 0, 0, x, y);
261 _al_mutex_unlock(&system->lock);
262
263 return true;
264 }
265
266
267
268 /* xmouse_set_mouse_axis:
269 * Set the mouse wheel position. Return true if successful.
270 */
271 static bool xmouse_set_mouse_axis(int which, int v)
272 {
273 ASSERT(xmouse_installed);
274
275 if (which != 2 && which != 3) {
276 return false;
277 }
278
279 _al_event_source_lock(&the_mouse.parent.es);
280 {
281 int z = which == 2 ? v : the_mouse.state.z;
282 int w = which == 3 ? v : the_mouse.state.w;
283 int dz = z - the_mouse.state.z;
284 int dw = w - the_mouse.state.w;
285
286 if (dz != 0 || dw != 0) {
287 the_mouse.state.z = z;
288 the_mouse.state.w = w;
289
290 generate_mouse_event(
291 ALLEGRO_EVENT_MOUSE_AXES,
292 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z,
293 the_mouse.state.w, the_mouse.state.pressure,
294 0, 0, dz, dw,
295 0, the_mouse.state.display);
296 }
297 }
298 _al_event_source_unlock(&the_mouse.parent.es);
299
300 return true;
301 }
302
303
304
305
306 /* xmouse_get_state:
307 * Copy the current mouse state into RET_STATE, with any necessary locking.
308 */
309 static void xmouse_get_state(ALLEGRO_MOUSE_STATE *ret_state)
310 {
311 ASSERT(xmouse_installed);
312
313 _al_event_source_lock(&the_mouse.parent.es);
314 {
315 *ret_state = the_mouse.state;
316 }
317 _al_event_source_unlock(&the_mouse.parent.es);
318 }
319
320
321
322 /* _al_xwin_mouse_button_press_handler: [bgman thread]
323 * Called by _xwin_process_event() for ButtonPress events received from the X
324 * server.
325 */
326 void _al_xwin_mouse_button_press_handler(int x_button,
327 ALLEGRO_DISPLAY *display)
328 {
329 unsigned int al_button;
330
331 if (!xmouse_installed)
332 return;
333
334 wheel_motion_handler(x_button, display);
335
336 /* Is this button supported by the Allegro API? */
337 al_button = x_button_to_al_button(x_button);
338 if (al_button == 0)
339 return;
340
341 _al_event_source_lock(&the_mouse.parent.es);
342 {
343 the_mouse.state.buttons |= (1 << (al_button - 1));
344 the_mouse.state.pressure = the_mouse.state.buttons ? 1.0 : 0.0; /* TODO */
345
346 generate_mouse_event(
347 ALLEGRO_EVENT_MOUSE_BUTTON_DOWN,
348 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z,
349 the_mouse.state.w, the_mouse.state.pressure,
350 0, 0, 0, 0,
351 al_button, display);
352 }
353 _al_event_source_unlock(&the_mouse.parent.es);
354 }
355
356
357
358 /* wheel_motion_handler: [bgman thread]
359 * Called by _al_xwin_mouse_button_press_handler() if the ButtonPress event
360 * received from the X server is actually for a mouse wheel movement.
361 */
362 static void wheel_motion_handler(int x_button, ALLEGRO_DISPLAY *display)
363 {
364 int dz = 0, dw = 0;
365 if (x_button == Button4) dz = 1;
366 if (x_button == Button5) dz = -1;
367 if (x_button == 6) dw = -1;
368 if (x_button == 7) dw = 1;
369 if (dz == 0 && dw == 0) return;
370
371 dz *= al_get_mouse_wheel_precision();
372 dw *= al_get_mouse_wheel_precision();
373
374 _al_event_source_lock(&the_mouse.parent.es);
375 {
376 the_mouse.state.z += dz;
377 the_mouse.state.w += dw;
378
379 generate_mouse_event(
380 ALLEGRO_EVENT_MOUSE_AXES,
381 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z,
382 the_mouse.state.w, the_mouse.state.pressure,
383 0, 0, dz, dw,
384 0, display);
385 }
386 _al_event_source_unlock(&the_mouse.parent.es);
387 }
388
389
390
391 /* _al_xwin_mouse_button_release_handler: [bgman thread]
392 * Called by _xwin_process_event() for ButtonRelease events received from the
393 * X server.
394 */
395 void _al_xwin_mouse_button_release_handler(int x_button,
396 ALLEGRO_DISPLAY *display)
397 {
398 int al_button;
399
400 if (!xmouse_installed)
401 return;
402
403 al_button = x_button_to_al_button(x_button);
404 if (al_button == 0)
405 return;
406
407 _al_event_source_lock(&the_mouse.parent.es);
408 {
409 the_mouse.state.buttons &=~ (1 << (al_button - 1));
410 the_mouse.state.pressure = the_mouse.state.buttons ? 1.0 : 0.0; /* TODO */
411
412 generate_mouse_event(
413 ALLEGRO_EVENT_MOUSE_BUTTON_UP,
414 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z,
415 the_mouse.state.w, the_mouse.state.pressure,
416 0, 0, 0, 0,
417 al_button, display);
418 }
419 _al_event_source_unlock(&the_mouse.parent.es);
420 }
421
422
423
424 /* _al_xwin_mouse_motion_notify_handler: [bgman thread]
425 * Called by _xwin_process_event() for MotionNotify events received from the X
426 * server.
427 */
428 void _al_xwin_mouse_motion_notify_handler(int x, int y,
429 ALLEGRO_DISPLAY *display)
430 {
431 ALLEGRO_DISPLAY_XGLX *glx = (void *)display;
432 int event_type = ALLEGRO_EVENT_MOUSE_AXES;
433
434 if (!xmouse_installed)
435 return;
436
437 /* Is this an event generated in response to al_set_mouse_xy? */
438 if (glx->mouse_warp) {
439 glx->mouse_warp = false;
440 event_type = ALLEGRO_EVENT_MOUSE_WARPED;
441 }
442
443 _al_event_source_lock(&the_mouse.parent.es);
444
445 #ifdef ALLEGRO_RASPBERRYPI
446 float scale_x, scale_y;
447 _al_raspberrypi_get_mouse_scale_ratios(&scale_x, &scale_y);
448 x *= scale_x;
449 y *= scale_y;
450 #endif
451
452 int dx = x - the_mouse.state.x;
453 int dy = y - the_mouse.state.y;
454
455 the_mouse.state.x = x;
456 the_mouse.state.y = y;
457 the_mouse.state.display = display;
458
459 generate_mouse_event(
460 event_type,
461 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z,
462 the_mouse.state.w, the_mouse.state.pressure,
463 dx, dy, 0, 0,
464 0, display);
465
466 _al_event_source_unlock(&the_mouse.parent.es);
467 }
468
469
470
471 /* x_button_to_al_button: [bgman thread]
472 * Map a X button number to an Allegro button number.
473 */
474 static unsigned int x_button_to_al_button(unsigned int x_button)
475 {
476 switch (x_button) {
477 case Button1:
478 return 1;
479 case Button2:
480 return 3;
481 case Button3:
482 return 2;
483 case Button4:
484 case Button5:
485 case 6:
486 case 7:
487 // hardcoded for z and w wheel
488 return 0;
489 default:
490 if (x_button >= 8 && x_button <= 36) {
491 return 4 + x_button - 8;
492 }
493 return 0;
494 }
495 }
496
497
498
499 /* generate_mouse_event: [bgman thread]
500 * Helper to generate a mouse event.
501 */
502 static void generate_mouse_event(unsigned int type,
503 int x, int y, int z, int w, float pressure,
504 int dx, int dy, int dz, int dw,
505 unsigned int button,
506 ALLEGRO_DISPLAY *display)
507 {
508 ALLEGRO_EVENT event;
509
510 if (!_al_event_source_needs_to_generate_event(&the_mouse.parent.es))
511 return;
512
513 event.mouse.type = type;
514 event.mouse.timestamp = al_get_time();
515 event.mouse.display = display;
516 event.mouse.x = x;
517 event.mouse.y = y;
518 event.mouse.z = z;
519 event.mouse.w = w;
520 event.mouse.dx = dx;
521 event.mouse.dy = dy;
522 event.mouse.dz = dz;
523 event.mouse.dw = dw;
524 event.mouse.button = button;
525 event.mouse.pressure = pressure;
526 _al_event_source_emit_event(&the_mouse.parent.es, &event);
527 }
528
529
530
531 /* _al_xwin_mouse_switch_handler:
532 * Handle a focus switch event.
533 */
534 void _al_xwin_mouse_switch_handler(ALLEGRO_DISPLAY *display,
535 const XCrossingEvent *event)
536 {
537 int event_type;
538
539 /* Ignore events where any of the buttons are held down. */
540 if (event->state & (Button1Mask | Button2Mask | Button3Mask |
541 Button4Mask | Button5Mask)) {
542 return;
543 }
544
545 _al_event_source_lock(&the_mouse.parent.es);
546
547 switch (event->type) {
548 case EnterNotify:
549 the_mouse.state.display = display;
550 the_mouse.state.x = event->x;
551 the_mouse.state.y = event->y;
552 event_type = ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY;
553 break;
554 case LeaveNotify:
555 the_mouse.state.display = NULL;
556 the_mouse.state.x = event->x;
557 the_mouse.state.y = event->y;
558 event_type = ALLEGRO_EVENT_MOUSE_LEAVE_DISPLAY;
559 break;
560 default:
561 ASSERT(false);
562 event_type = 0;
563 break;
564 }
565
566 generate_mouse_event(
567 event_type,
568 the_mouse.state.x, the_mouse.state.y, the_mouse.state.z, the_mouse.state.pressure,
569 the_mouse.state.w,
570 0, 0, 0, 0,
571 0, display);
572
573 _al_event_source_unlock(&the_mouse.parent.es);
574 }
575
576 bool _al_xwin_grab_mouse(ALLEGRO_DISPLAY *display)
577 {
578 ALLEGRO_SYSTEM_XGLX *system = (ALLEGRO_SYSTEM_XGLX *)al_get_system_driver();
579 ALLEGRO_DISPLAY_XGLX *glx = (ALLEGRO_DISPLAY_XGLX *)display;
580 int grab;
581 bool ret;
582
583 _al_mutex_lock(&system->lock);
584 grab = XGrabPointer(system->x11display, glx->window, False,
585 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
586 GrabModeAsync, GrabModeAsync, glx->window, None, CurrentTime);
587 if (grab == GrabSuccess) {
588 system->mouse_grab_display = display;
589 ret = true;
590 }
591 else {
592 ret = false;
593 }
594 _al_mutex_unlock(&system->lock);
595 return ret;
596 }
597
598 bool _al_xwin_ungrab_mouse(void)
599 {
600 ALLEGRO_SYSTEM_XGLX *system = (void *)al_get_system_driver();
601
602 _al_mutex_lock(&system->lock);
603 XUngrabPointer(system->x11display, CurrentTime);
604 system->mouse_grab_display = NULL;
605 _al_mutex_unlock(&system->lock);
606 return true;
607 }
608
609
610 /* vim: set sts=3 sw=3 et: */