Debian Codegraph @

gdk/x11/gdkinput-x11.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 */
26
27 #include "config.h"
28 #include "gdkinputprivate.h"
29 #include "gdkinternals.h"
30 #include "gdkx.h"
31 #include "gdk.h" /* For gdk_error_trap_push()/pop() */
32 #include "gdkdisplay-x11.h"
33 #include "gdkalias.h"
34
35 #include <string.h>
36
37 /* Forward declarations */
38 static GdkDevicePrivate *gdk_input_device_new (GdkDisplay *display,
39 XDeviceInfo *device,
40 gint include_core);
41 static void gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
42 GdkWindow *window,
43 gint *axis_data,
44 gdouble *axis_out,
45 gdouble *x_out,
46 gdouble *y_out);
47 static void gdk_input_update_axes (GdkDevicePrivate *gdkdev,
48 gint axes_count,
49 gint first_axis,
50 gint *axis_data);
51 static guint gdk_input_translate_state (guint state,
52 guint device_state);
53
54 /* A temporary error handler for ignoring device unplugging-related errors. */
55 static int
56 ignore_errors (Display *display, XErrorEvent *event)
57 {
58 return True;
59 }
60
61 GdkDevicePrivate *
62 _gdk_input_find_device (GdkDisplay *display,
63 guint32 id)
64 {
65 GList *tmp_list = GDK_DISPLAY_X11 (display)->input_devices;
66 GdkDevicePrivate *gdkdev;
67 while (tmp_list)
68 {
69 gdkdev = (GdkDevicePrivate *)(tmp_list->data);
70 if (gdkdev->deviceid == id)
71 return gdkdev;
72 tmp_list = tmp_list->next;
73 }
74 return NULL;
75 }
76
77 void
78 _gdk_input_get_root_relative_geometry (GdkWindow *window,
79 int *x_ret, int *y_ret)
80 {
81 Window child;
82 gint x,y;
83
84 XTranslateCoordinates (GDK_WINDOW_XDISPLAY (window),
85 GDK_WINDOW_XWINDOW (window),
86 GDK_WINDOW_XROOTWIN (window),
87 0, 0, &x, &y, &child);
88
89 if (x_ret)
90 *x_ret = x;
91 if (y_ret)
92 *y_ret = y;
93 }
94
95 static GdkDevicePrivate *
96 gdk_input_device_new (GdkDisplay *display,
97 XDeviceInfo *device,
98 gint include_core)
99 {
100 GdkDevicePrivate *gdkdev;
101 gchar *tmp_name;
102 XAnyClassPtr class;
103 gint i,j;
104
105 gdkdev = g_object_new (GDK_TYPE_DEVICE, NULL);
106
107 gdkdev->deviceid = device->id;
108 gdkdev->display = display;
109
110 if (device->name[0])
111 gdkdev->info.name = g_strdup (device->name);
112 else
113 /* XFree86 3.2 gives an empty name to the default core devices,
114 (fixed in 3.2A) */
115 gdkdev->info.name = g_strdup ("pointer");
116
117 gdkdev->info.mode = GDK_MODE_DISABLED;
118
119 /* Try to figure out what kind of device this is by its name -
120 could invite a very, very, long list... Lowercase name
121 for comparison purposes */
122
123 tmp_name = g_ascii_strdown (gdkdev->info.name, -1);
124
125 if (strstr (tmp_name, "eraser"))
126 gdkdev->info.source = GDK_SOURCE_ERASER;
127 else if (strstr (tmp_name, "cursor"))
128 gdkdev->info.source = GDK_SOURCE_CURSOR;
129 else if (strstr (tmp_name, "wacom") ||
130 strstr (tmp_name, "pen"))
131 gdkdev->info.source = GDK_SOURCE_PEN;
132 else
133 gdkdev->info.source = GDK_SOURCE_MOUSE;
134
135 g_free(tmp_name);
136
137 gdkdev->xdevice = NULL;
138
139 /* step through the classes */
140
141 gdkdev->info.num_axes = 0;
142 gdkdev->info.num_keys = 0;
143 gdkdev->info.axes = NULL;
144 gdkdev->info.keys = NULL;
145 gdkdev->axes = 0;
146 gdkdev->info.has_cursor = 0;
147 gdkdev->needs_update = FALSE;
148 gdkdev->claimed = FALSE;
149 memset(gdkdev->button_state, 0, sizeof (gdkdev->button_state));
150 gdkdev->button_count = 0;
151
152 class = device->inputclassinfo;
153 for (i=0;i<device->num_classes;i++)
154 {
155 switch (class->class) {
156 case ButtonClass:
157 break;
158 case KeyClass:
159 {
160 XKeyInfo *xki = (XKeyInfo *)class;
161 /* Hack to catch XFree86 3.3.1 bug. Other devices better
162 * not have exactly 25 keys...
163 */
164 if ((xki->min_keycode == 8) && (xki->max_keycode == 32))
165 {
166 gdkdev->info.num_keys = 32;
167 gdkdev->min_keycode = 1;
168 }
169 else
170 {
171 gdkdev->info.num_keys = xki->max_keycode - xki->min_keycode + 1;
172 gdkdev->min_keycode = xki->min_keycode;
173 }
174 gdkdev->info.keys = g_new (GdkDeviceKey, gdkdev->info.num_keys);
175
176 for (j=0; j<gdkdev->info.num_keys; j++)
177 {
178 gdkdev->info.keys[j].keyval = 0;
179 gdkdev->info.keys[j].modifiers = 0;
180 }
181
182 break;
183 }
184 case ValuatorClass:
185 {
186 XValuatorInfo *xvi = (XValuatorInfo *)class;
187 gdkdev->info.num_axes = xvi->num_axes;
188 gdkdev->axes = g_new (GdkAxisInfo, xvi->num_axes);
189 gdkdev->axis_data = g_new0 (gint, xvi->num_axes);
190 gdkdev->info.axes = g_new0 (GdkDeviceAxis, xvi->num_axes);
191 for (j=0;j<xvi->num_axes;j++)
192 {
193 gdkdev->axes[j].resolution =
194 gdkdev->axes[j].xresolution = xvi->axes[j].resolution;
195 gdkdev->axes[j].min_value =
196 gdkdev->axes[j].xmin_value = xvi->axes[j].min_value;
197 gdkdev->axes[j].max_value =
198 gdkdev->axes[j].xmax_value = xvi->axes[j].max_value;
199 gdkdev->info.axes[j].use = GDK_AXIS_IGNORE;
200 }
201 j=0;
202 if (j<xvi->num_axes)
203 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_X);
204 if (j<xvi->num_axes)
205 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_Y);
206 if (j<xvi->num_axes)
207 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_PRESSURE);
208 if (j<xvi->num_axes)
209 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_XTILT);
210 if (j<xvi->num_axes)
211 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_YTILT);
212 if (j<xvi->num_axes)
213 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_WHEEL);
214
215 break;
216 }
217 }
218 class = (XAnyClassPtr)(((char *)class) + class->length);
219 }
220 /* return NULL if no axes */
221 if (!gdkdev->info.num_axes || !gdkdev->axes ||
222 (!include_core && device->use == IsXPointer))
223 goto error;
224
225 if (device->use != IsXPointer)
226 {
227 gdk_error_trap_push ();
228 gdkdev->xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (display),
229 gdkdev->deviceid);
230
231 /* return NULL if device is not ready */
232 if (gdk_error_trap_pop ())
233 goto error;
234 }
235
236 gdkdev->buttonpress_type = 0;
237 gdkdev->buttonrelease_type = 0;
238 gdkdev->keypress_type = 0;
239 gdkdev->keyrelease_type = 0;
240 gdkdev->motionnotify_type = 0;
241 gdkdev->proximityin_type = 0;
242 gdkdev->proximityout_type = 0;
243 gdkdev->changenotify_type = 0;
244
245 return gdkdev;
246
247 error:
248
249 g_object_unref (gdkdev);
250
251 return NULL;
252 }
253
254 void
255 _gdk_input_common_find_events (GdkDevicePrivate *gdkdev,
256 gint mask,
257 XEventClass *classes,
258 int *num_classes)
259 {
260 gint i;
261 XEventClass class;
262
263 i = 0;
264 if (mask & GDK_BUTTON_PRESS_MASK)
265 {
266 DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
267 class);
268 if (class != 0)
269 classes[i++] = class;
270 DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
271 if (class != 0)
272 classes[i++] = class;
273 }
274 if (mask & GDK_BUTTON_RELEASE_MASK)
275 {
276 DeviceButtonRelease (gdkdev->xdevice, gdkdev->buttonrelease_type,
277 class);
278 if (class != 0)
279 classes[i++] = class;
280 }
281 if (mask & (GDK_POINTER_MOTION_MASK |
282 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
283 GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK))
284 {
285 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
286 if (class != 0)
287 classes[i++] = class;
288 DeviceStateNotify (gdkdev->xdevice, gdkdev->devicestatenotify_type, class);
289 if (class != 0)
290 classes[i++] = class;
291 }
292 if (mask & GDK_KEY_PRESS_MASK)
293 {
294 DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
295 if (class != 0)
296 classes[i++] = class;
297 }
298 if (mask & GDK_KEY_RELEASE_MASK)
299 {
300 DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
301 if (class != 0)
302 classes[i++] = class;
303 }
304 if (mask & GDK_PROXIMITY_IN_MASK)
305 {
306 ProximityIn (gdkdev->xdevice, gdkdev->proximityin_type, class);
307 if (class != 0)
308 classes[i++] = class;
309 }
310 if (mask & GDK_PROXIMITY_OUT_MASK)
311 {
312 ProximityOut (gdkdev->xdevice, gdkdev->proximityout_type, class);
313 if (class != 0)
314 classes[i++] = class;
315 }
316
317 *num_classes = i;
318 }
319
320 void
321 _gdk_input_select_events (GdkWindow *impl_window,
322 GdkDevicePrivate *gdkdev)
323 {
324 int (*old_handler) (Display *, XErrorEvent *);
325 XEventClass classes[GDK_MAX_DEVICE_CLASSES];
326 gint num_classes;
327 guint event_mask;
328 GdkWindowObject *w;
329 GdkInputWindow *iw;
330 GList *l;
331
332 event_mask = 0;
333 iw = ((GdkWindowObject *)impl_window)->input_window;
334
335 if (gdkdev->info.mode != GDK_MODE_DISABLED &&
336 iw != NULL)
337 {
338 for (l = iw->windows; l != NULL; l = l->next)
339 {
340 w = l->data;
341 if (gdkdev->info.has_cursor || (w->extension_events & GDK_ALL_DEVICES_MASK))
342 event_mask |= w->extension_events;
343 }
344 }
345 event_mask &= ~GDK_ALL_DEVICES_MASK;
346
347 if (event_mask)
348 event_mask |= GDK_PROXIMITY_OUT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK;
349
350 _gdk_input_common_find_events (gdkdev, event_mask,
351 classes, &num_classes);
352
353 /* From X11 doc:
354 * "XSelectExtensionEvent can generate a BadWindow or BadClass error."
355 * In particular when a device is unplugged, a requested event class
356 * could no longer be valid and raise a BadClass, which would cause
357 * the program to crash.
358 *
359 * To handle this case gracefully, we simply ignore XSelectExtensionEvent() errors.
360 * This is OK since there is no events to report for the unplugged device anyway.
361 * So simply the device remains "silent".
362 */
363 old_handler = XSetErrorHandler (ignore_errors);
364 XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (impl_window),
365 GDK_WINDOW_XWINDOW (impl_window),
366 classes, num_classes);
367 XSetErrorHandler (old_handler);
368 }
369
370 gint
371 _gdk_input_common_init (GdkDisplay *display,
372 gint include_core)
373 {
374 XDeviceInfo *devices;
375 int num_devices, loop;
376 int ignore, event_base;
377 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
378
379 /* Init XInput extension */
380
381 display_x11->input_devices = NULL;
382 if (XQueryExtension (display_x11->xdisplay, "XInputExtension",
383 &ignore, &event_base, &ignore))
384 {
385 gdk_x11_register_standard_event_type (display,
386 event_base, 15 /* Number of events */);
387
388 devices = XListInputDevices(display_x11->xdisplay, &num_devices);
389 if (devices)
390 {
391 for(loop=0; loop<num_devices; loop++)
392 {
393 GdkDevicePrivate *gdkdev = gdk_input_device_new(display,
394 &devices[loop],
395 include_core);
396 if (gdkdev)
397 display_x11->input_devices = g_list_append(display_x11->input_devices, gdkdev);
398 }
399 XFreeDeviceList(devices);
400 }
401 }
402
403 display_x11->input_devices = g_list_append (display_x11->input_devices, display->core_pointer);
404
405 return TRUE;
406 }
407
408 static void
409 gdk_input_update_axes (GdkDevicePrivate *gdkdev,
410 gint axes_count,
411 gint first_axis,
412 gint *axis_data)
413 {
414 int i;
415 g_return_if_fail (first_axis >= 0 && first_axis + axes_count <= gdkdev->info.num_axes);
416
417 for (i = 0; i < axes_count; i++)
418 gdkdev->axis_data[first_axis + i] = axis_data[i];
419 }
420
421 static void
422 gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
423 GdkWindow *window,
424 gint *axis_data,
425 gdouble *axis_out,
426 gdouble *x_out,
427 gdouble *y_out)
428 {
429 GdkWindowObject *priv, *impl_window;
430
431 int i;
432 int x_axis = 0;
433 int y_axis = 0;
434
435 double device_width, device_height, x_min, y_min;
436 double x_offset, y_offset, x_scale, y_scale;
437
438 priv = (GdkWindowObject *) window;
439 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
440
441 for (i=0; i<gdkdev->info.num_axes; i++)
442 {
443 switch (gdkdev->info.axes[i].use)
444 {
445 case GDK_AXIS_X:
446 x_axis = i;
447 break;
448 case GDK_AXIS_Y:
449 y_axis = i;
450 break;
451 default:
452 break;
453 }
454 }
455
456 device_width = gdkdev->axes[x_axis].max_value - gdkdev->axes[x_axis].min_value;
457 if (device_width > 0)
458 {
459 x_min = gdkdev->axes[x_axis].min_value;
460 }
461 else
462 {
463 device_width = gdk_screen_get_width (gdk_drawable_get_screen (window));
464 x_min = 0;
465 }
466
467 device_height = gdkdev->axes[y_axis].max_value - gdkdev->axes[y_axis].min_value;
468 if (device_height > 0)
469 {
470 y_min = gdkdev->axes[y_axis].min_value;
471 }
472 else
473 {
474 device_height = gdk_screen_get_height (gdk_drawable_get_screen (window));
475 y_min = 0;
476 }
477
478 if (gdkdev->info.mode == GDK_MODE_SCREEN)
479 {
480 x_scale = gdk_screen_get_width (gdk_drawable_get_screen (window)) / device_width;
481 y_scale = gdk_screen_get_height (gdk_drawable_get_screen (window)) / device_height;
482
483 x_offset = - impl_window->input_window->root_x - priv->abs_x;
484 y_offset = - impl_window->input_window->root_y - priv->abs_y;
485 }
486 else /* GDK_MODE_WINDOW */
487 {
488 double x_resolution = gdkdev->axes[x_axis].resolution;
489 double y_resolution = gdkdev->axes[y_axis].resolution;
490 double device_aspect;
491 /*
492 * Some drivers incorrectly report the resolution of the device
493 * as zero (in partiular linuxwacom < 0.5.3 with usb tablets).
494 * This causes the device_aspect to become NaN and totally
495 * breaks windowed mode. If this is the case, the best we can
496 * do is to assume the resolution is non-zero is equal in both
497 * directions (which is true for many devices). The absolute
498 * value of the resolution doesn't matter since we only use the
499 * ratio.
500 */
501 if ((x_resolution == 0) || (y_resolution == 0))
502 {
503 x_resolution = 1;
504 y_resolution = 1;
505 }
506 device_aspect = (device_height*y_resolution) /
507 (device_width*x_resolution);
508 if (device_aspect * priv->width >= priv->height)
509 {
510 /* device taller than window */
511 x_scale = priv->width / device_width;
512 y_scale = (x_scale * x_resolution) / y_resolution;
513
514 x_offset = 0;
515 y_offset = -(device_height * y_scale - priv->height)/2;
516 }
517 else
518 {
519 /* window taller than device */
520 y_scale = priv->height / device_height;
521 x_scale = (y_scale * y_resolution) / x_resolution;
522
523 y_offset = 0;
524 x_offset = - (device_width * x_scale - priv->width)/2;
525 }
526 }
527
528 for (i=0; i<gdkdev->info.num_axes; i++)
529 {
530 switch (gdkdev->info.axes[i].use)
531 {
532 case GDK_AXIS_X:
533 axis_out[i] = x_offset + x_scale * (axis_data[x_axis] - x_min);
534 if (x_out)
535 *x_out = axis_out[i];
536 break;
537 case GDK_AXIS_Y:
538 axis_out[i] = y_offset + y_scale * (axis_data[y_axis] - y_min);
539 if (y_out)
540 *y_out = axis_out[i];
541 break;
542 default:
543 axis_out[i] =
544 (gdkdev->info.axes[i].max * (axis_data[i] - gdkdev->axes[i].min_value) +
545 gdkdev->info.axes[i].min * (gdkdev->axes[i].max_value - axis_data[i])) /
546 (gdkdev->axes[i].max_value - gdkdev->axes[i].min_value);
547 break;
548 }
549 }
550 }
551
552 /* combine the state of the core device and the device state
553 * into one - for now we do this in a simple-minded manner -
554 * we just take the keyboard portion of the core device and
555 * the button portion (all of?) the device state.
556 * Any button remapping should go on here.
557 */
558 static guint
559 gdk_input_translate_state(guint state, guint device_state)
560 {
561 return device_state | (state & 0xFF);
562 }
563
564
565 gboolean
566 _gdk_input_common_other_event (GdkEvent *event,
567 XEvent *xevent,
568 GdkWindow *window,
569 GdkDevicePrivate *gdkdev)
570 {
571 GdkWindowObject *priv, *impl_window;
572 GdkInputWindow *input_window;
573
574 priv = (GdkWindowObject *) window;
575 impl_window = (GdkWindowObject *)_gdk_window_get_impl_window (window);
576 input_window = impl_window->input_window;
577
578 if ((xevent->type == gdkdev->buttonpress_type) ||
579 (xevent->type == gdkdev->buttonrelease_type))
580 {
581 XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);
582
583 g_return_val_if_fail (xdbe->button < 256, FALSE);
584 if (xdbe->type == gdkdev->buttonpress_type)
585 {
586 event->button.type = GDK_BUTTON_PRESS;
587 if (!(gdkdev->button_state[xdbe->button/8] & 1 << (xdbe->button%8)))
588 {
589 gdkdev->button_state[xdbe->button/8] |= 1 << (xdbe->button%8);
590 gdkdev->button_count++;
591 }
592 }
593 else
594 {
595 event->button.type = GDK_BUTTON_RELEASE;
596 if (gdkdev->button_state[xdbe->button/8] & 1 << (xdbe->button%8))
597 {
598 gdkdev->button_state[xdbe->button/8] &= ~(1 << (xdbe->button%8));
599 gdkdev->button_count--;
600 }
601 }
602 event->button.device = &gdkdev->info;
603 event->button.window = window;
604 event->button.time = xdbe->time;
605
606 event->button.axes = g_new (gdouble, gdkdev->info.num_axes);
607 gdk_input_update_axes (gdkdev, xdbe->axes_count, xdbe->first_axis,
608 xdbe->axis_data);
609 gdk_input_translate_coordinates (gdkdev, window, gdkdev->axis_data,
610 event->button.axes,
611 &event->button.x, &event->button.y);
612 event->button.x_root = event->button.x + priv->abs_x + input_window->root_x;
613 event->button.y_root = event->button.y + priv->abs_y + input_window->root_y;
614 event->button.state = gdk_input_translate_state (xdbe->state,xdbe->device_state);
615 event->button.button = xdbe->button;
616
617 if (event->button.type == GDK_BUTTON_PRESS)
618 _gdk_event_button_generate (gdk_drawable_get_display (event->button.window),
619 event);
620
621 GDK_NOTE (EVENTS,
622 g_print ("button %s:\t\twindow: %ld device: %ld x,y: %f %f button: %d\n",
623 (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
624 xdbe->window,
625 xdbe->deviceid,
626 event->button.x, event->button.y,
627 xdbe->button));
628
629 /* Update the timestamp of the latest user interaction, if the event has
630 * a valid timestamp.
631 */
632 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
633 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
634 gdk_event_get_time (event));
635 return TRUE;
636 }
637
638 if ((xevent->type == gdkdev->keypress_type) ||
639 (xevent->type == gdkdev->keyrelease_type))
640 {
641 XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);
642
643 GDK_NOTE (EVENTS,
644 g_print ("device key %s:\twindow: %ld device: %ld keycode: %d\n",
645 (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
646 xdke->window,
647 xdke->deviceid,
648 xdke->keycode));
649
650 if (xdke->keycode < gdkdev->min_keycode ||
651 xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
652 {
653 g_warning ("Invalid device key code received");
654 return FALSE;
655 }
656
657 event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;
658
659 if (event->key.keyval == 0)
660 {
661 GDK_NOTE (EVENTS,
662 g_print ("\t\ttranslation - NONE\n"));
663
664 return FALSE;
665 }
666
667 event->key.type = (xdke->type == gdkdev->keypress_type) ?
668 GDK_KEY_PRESS : GDK_KEY_RELEASE;
669
670 event->key.window = window;
671 event->key.time = xdke->time;
672
673 event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
674 | gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;
675
676 /* Add a string translation for the key event */
677 if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
678 {
679 event->key.length = 1;
680 event->key.string = g_new (gchar, 2);
681 event->key.string[0] = (gchar)event->key.keyval;
682 event->key.string[1] = 0;
683 }
684 else
685 {
686 event->key.length = 0;
687 event->key.string = g_new0 (gchar, 1);
688 }
689
690 GDK_NOTE (EVENTS,
691 g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
692 event->key.keyval,
693 event->key.state));
694
695 /* Update the timestamp of the latest user interaction, if the event has
696 * a valid timestamp.
697 */
698 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
699 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
700 gdk_event_get_time (event));
701 return TRUE;
702 }
703
704 if (xevent->type == gdkdev->motionnotify_type)
705 {
706 XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);
707
708 event->motion.device = &gdkdev->info;
709
710 event->motion.axes = g_new (gdouble, gdkdev->info.num_axes);
711 gdk_input_update_axes (gdkdev, xdme->axes_count, xdme->first_axis, xdme->axis_data);
712 gdk_input_translate_coordinates(gdkdev, window, gdkdev->axis_data,
713 event->motion.axes,
714 &event->motion.x,&event->motion.y);
715 event->motion.x_root = event->motion.x + priv->abs_x + input_window->root_x;
716 event->motion.y_root = event->motion.y + priv->abs_y + input_window->root_y;
717
718 event->motion.type = GDK_MOTION_NOTIFY;
719 event->motion.window = window;
720 event->motion.time = xdme->time;
721 event->motion.state = gdk_input_translate_state(xdme->state,
722 xdme->device_state);
723 event->motion.is_hint = xdme->is_hint;
724
725 GDK_NOTE (EVENTS,
726 g_print ("motion notify:\t\twindow: %ld device: %ld x,y: %f %f state %#4x hint: %s\n",
727 xdme->window,
728 xdme->deviceid,
729 event->motion.x, event->motion.y,
730 event->motion.state,
731 (xdme->is_hint) ? "true" : "false"));
732
733
734 /* Update the timestamp of the latest user interaction, if the event has
735 * a valid timestamp.
736 */
737 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
738 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
739 gdk_event_get_time (event));
740 return TRUE;
741 }
742
743 if (xevent->type == gdkdev->devicestatenotify_type)
744 {
745 int i;
746 XDeviceStateNotifyEvent *xdse = (XDeviceStateNotifyEvent *)(xevent);
747 XInputClass *input_class = (XInputClass *)xdse->data;
748 for (i=0; i<xdse->num_classes; i++)
749 {
750 if (input_class->class == ValuatorClass)
751 gdk_input_update_axes (gdkdev, gdkdev->info.num_axes, 0,
752 ((XValuatorState *)input_class)->valuators);
753 input_class = (XInputClass *)(((char *)input_class)+input_class->length);
754 }
755
756 GDK_NOTE (EVENTS,
757 g_print ("device state notify:\t\twindow: %ld device: %ld\n",
758 xdse->window,
759 xdse->deviceid));
760
761 return FALSE;
762 }
763 if (xevent->type == gdkdev->proximityin_type ||
764 xevent->type == gdkdev->proximityout_type)
765 {
766 XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);
767
768 event->proximity.device = &gdkdev->info;
769 event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
770 GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
771 event->proximity.window = window;
772 event->proximity.time = xpne->time;
773
774 /* Update the timestamp of the latest user interaction, if the event has
775 * a valid timestamp.
776 */
777 if (gdk_event_get_time (event) != GDK_CURRENT_TIME)
778 gdk_x11_window_set_user_time (gdk_window_get_toplevel (window),
779 gdk_event_get_time (event));
780 return TRUE;
781 }
782
783 return FALSE; /* wasn't one of our event types */
784 }
785
786 gboolean
787 _gdk_input_common_event_selected (GdkEvent *event,
788 GdkWindow *window,
789 GdkDevicePrivate *gdkdev)
790 {
791 GdkWindowObject *priv = (GdkWindowObject *) window;
792
793 switch (event->type) {
794 case GDK_BUTTON_PRESS:
795 return priv->extension_events & GDK_BUTTON_PRESS_MASK;
796
797 case GDK_BUTTON_RELEASE:
798 return priv->extension_events & GDK_BUTTON_RELEASE_MASK;
799
800 case GDK_KEY_PRESS:
801 return priv->extension_events & GDK_KEY_PRESS_MASK;
802
803 case GDK_KEY_RELEASE:
804 return priv->extension_events & GDK_KEY_RELEASE_MASK;
805
806 case GDK_MOTION_NOTIFY:
807 if (priv->extension_events & GDK_POINTER_MOTION_MASK)
808 return TRUE;
809 if (gdkdev->button_count && (priv->extension_events & GDK_BUTTON_MOTION_MASK))
810 return TRUE;
811
812 if ((gdkdev->button_state[0] & 1 << 1) && (priv->extension_events & GDK_BUTTON1_MOTION_MASK))
813 return TRUE;
814 if ((gdkdev->button_state[0] & 1 << 2) && (priv->extension_events & GDK_BUTTON2_MOTION_MASK))
815 return TRUE;
816 if ((gdkdev->button_state[0] & 1 << 3) && (priv->extension_events & GDK_BUTTON3_MOTION_MASK))
817 return TRUE;
818
819 return FALSE;
820
821 case GDK_PROXIMITY_IN:
822 return priv->extension_events & GDK_PROXIMITY_IN_MASK;
823
824 case GDK_PROXIMITY_OUT:
825 return priv->extension_events & GDK_PROXIMITY_OUT_MASK;
826
827 default:
828 return FALSE;
829 }
830
831
832 }
833
834
835 gboolean
836 _gdk_device_get_history (GdkDevice *device,
837 GdkWindow *window,
838 guint32 start,
839 guint32 stop,
840 GdkTimeCoord ***events,
841 gint *n_events)
842 {
843 GdkTimeCoord **coords;
844 XDeviceTimeCoord *device_coords;
845 GdkWindow *impl_window;
846 GdkDevicePrivate *gdkdev;
847 gint mode_return;
848 gint axis_count_return;
849 gint i;
850
851 gdkdev = (GdkDevicePrivate *)device;
852
853 impl_window = _gdk_window_get_impl_window (window);
854
855 device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (impl_window),
856 gdkdev->xdevice,
857 start, stop,
858 n_events, &mode_return,
859 &axis_count_return);
860
861 if (device_coords)
862 {
863 coords = _gdk_device_allocate_history (device, *n_events);
864
865 for (i = 0; i < *n_events; i++)
866 {
867 coords[i]->time = device_coords[i].time;
868
869 gdk_input_translate_coordinates (gdkdev, window,
870 device_coords[i].data,
871 coords[i]->axes, NULL, NULL);
872 }
873
874 XFreeDeviceMotionEvents (device_coords);
875
876 *events = coords;
877
878 return TRUE;
879 }
880 else
881 return FALSE;
882 }
883
884 /**
885 * gdk_device_get_state:
886 * @device: a #GdkDevice.
887 * @window: a #GdkWindow.
888 * @axes: an array of doubles to store the values of the axes of @device in,
889 * or %NULL.
890 * @mask: location to store the modifiers, or %NULL.
891 *
892 * Gets the current state of a device.
893 */
894 void
895 gdk_device_get_state (GdkDevice *device,
896 GdkWindow *window,
897 gdouble *axes,
898 GdkModifierType *mask)
899 {
900 gint i;
901
902 g_return_if_fail (device != NULL);
903 g_return_if_fail (GDK_IS_WINDOW (window));
904
905 if (GDK_IS_CORE (device))
906 {
907 gint x_int, y_int;
908
909 gdk_window_get_pointer (window, &x_int, &y_int, mask);
910
911 if (axes)
912 {
913 axes[0] = x_int;
914 axes[1] = y_int;
915 }
916 }
917 else
918 {
919 int (*old_handler) (Display *, XErrorEvent *);
920 GdkDevicePrivate *gdkdev;
921 XDeviceState *state = NULL;
922 XInputClass *input_class;
923
924 if (mask)
925 gdk_window_get_pointer (window, NULL, NULL, mask);
926
927 gdkdev = (GdkDevicePrivate *)device;
928
929 /* From X11 doc: "XQueryDeviceState can generate a BadDevice error."
930 * This would occur in particular when a device is unplugged,
931 * which would cause the program to crash (see bug 575767).
932 *
933 * To handle this case gracefully, we simply ignore the device.
934 * GTK+ 3 handles this better with XInput 2's hotplugging support;
935 * but this is better than a crash in GTK+ 2.
936 */
937 old_handler = XSetErrorHandler (ignore_errors);
938 state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
939 gdkdev->xdevice);
940 XSetErrorHandler (old_handler);
941
942 if (! state)
943 return;
944
945 input_class = state->data;
946 for (i=0; i<state->num_classes; i++)
947 {
948 switch (input_class->class)
949 {
950 case ValuatorClass:
951 if (axes)
952 gdk_input_translate_coordinates (gdkdev, window,
953 ((XValuatorState *)input_class)->valuators,
954 axes, NULL, NULL);
955 break;
956
957 case ButtonClass:
958 if (mask)
959 {
960 *mask &= 0xFF;
961 if (((XButtonState *)input_class)->num_buttons > 0)
962 *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
963 /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
964 * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
965 }
966 break;
967 }
968 input_class = (XInputClass *)(((char *)input_class)+input_class->length);
969 }
970 XFreeDeviceState (state);
971 }
972 }
973
974 #define __GDK_INPUT_X11_C__
975 #include "gdkaliasdef.c"