1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
27 #include "gdkinputprivate.h"
28 #include "gdkinternals.h"
30 #include "gdk.h" /* For gdk_error_trap_push()/pop() */
31 #include "gdkdisplay-x11.h"
35 /* Forward declarations */
36 static GdkDevicePrivate *gdk_input_device_new (GdkDisplay *display,
39 static void gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
40 GdkInputWindow *input_window,
45 static guint gdk_input_translate_state (guint state,
49 _gdk_input_find_device (GdkDisplay *display,
52 GList *tmp_list = GDK_DISPLAY_X11 (display)->input_devices;
53 GdkDevicePrivate *gdkdev;
56 gdkdev = (GdkDevicePrivate *)(tmp_list->data);
57 if (gdkdev->deviceid == id)
59 tmp_list = tmp_list->next;
65 _gdk_input_get_root_relative_geometry(Display *display, Window w, int *x_ret, int *y_ret,
66 int *width_ret, int *height_ret)
68 Window root, parent, child;
73 guint border_widthc, depthc;
75 XQueryTree (display, w, &root, &parent, &children, &nchildren);
79 XGetGeometry (display, w, &root, &x, &y, &width, &height, &border_widthc, &depthc);
81 XTranslateCoordinates (display, w, root, 0, 0, &x, &y, &child);
93 static GdkDevicePrivate *
94 gdk_input_device_new (GdkDisplay *display,
98 GdkDevicePrivate *gdkdev;
103 gdkdev = g_object_new (GDK_TYPE_DEVICE, NULL);
105 gdkdev->deviceid = device->id;
106 gdkdev->display = display;
109 gdkdev->info.name = g_strdup (device->name);
111 /* XFree86 3.2 gives an empty name to the default core devices,
113 gdkdev->info.name = g_strdup ("pointer");
115 gdkdev->info.mode = GDK_MODE_DISABLED;
117 /* Try to figure out what kind of device this is by its name -
118 could invite a very, very, long list... Lowercase name
119 for comparison purposes */
121 tmp_name = g_ascii_strdown (gdkdev->info.name, -1);
123 if (!strcmp (tmp_name, "pointer"))
124 gdkdev->info.source = GDK_SOURCE_MOUSE;
125 else if (!strcmp (tmp_name, "wacom") ||
126 !strcmp (tmp_name, "pen"))
127 gdkdev->info.source = GDK_SOURCE_PEN;
128 else if (!strcmp (tmp_name, "eraser"))
129 gdkdev->info.source = GDK_SOURCE_ERASER;
130 else if (!strcmp (tmp_name, "cursor"))
131 gdkdev->info.source = GDK_SOURCE_CURSOR;
133 gdkdev->info.source = GDK_SOURCE_PEN;
137 gdkdev->xdevice = NULL;
139 /* step through the classes */
141 gdkdev->info.num_axes = 0;
142 gdkdev->info.num_keys = 0;
143 gdkdev->info.axes = NULL;
144 gdkdev->info.keys = NULL;
146 gdkdev->info.has_cursor = 0;
147 gdkdev->needs_update = FALSE;
148 gdkdev->claimed = FALSE;
149 gdkdev->button_state = 0;
151 class = device->inputclassinfo;
152 for (i=0;i<device->num_classes;i++)
154 switch (class->class) {
159 XKeyInfo *xki = (XKeyInfo *)class;
160 /* Hack to catch XFree86 3.3.1 bug. Other devices better
161 * not have exactly 25 keys...
163 if ((xki->min_keycode == 8) && (xki->max_keycode == 32))
165 gdkdev->info.num_keys = 32;
166 gdkdev->min_keycode = 1;
170 gdkdev->info.num_keys = xki->max_keycode - xki->min_keycode + 1;
171 gdkdev->min_keycode = xki->min_keycode;
173 gdkdev->info.keys = g_new (GdkDeviceKey, gdkdev->info.num_keys);
175 for (j=0; j<gdkdev->info.num_keys; j++)
177 gdkdev->info.keys[j].keyval = 0;
178 gdkdev->info.keys[j].modifiers = 0;
185 XValuatorInfo *xvi = (XValuatorInfo *)class;
186 gdkdev->info.num_axes = xvi->num_axes;
187 gdkdev->axes = g_new (GdkAxisInfo, xvi->num_axes);
188 gdkdev->info.axes = g_new0 (GdkDeviceAxis, xvi->num_axes);
189 for (j=0;j<xvi->num_axes;j++)
191 gdkdev->axes[j].resolution =
192 gdkdev->axes[j].xresolution = xvi->axes[j].resolution;
193 gdkdev->axes[j].min_value =
194 gdkdev->axes[j].xmin_value = xvi->axes[j].min_value;
195 gdkdev->axes[j].max_value =
196 gdkdev->axes[j].xmax_value = xvi->axes[j].max_value;
197 gdkdev->info.axes[j].use = GDK_AXIS_IGNORE;
201 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_X);
203 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_Y);
205 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_PRESSURE);
207 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_XTILT);
209 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_YTILT);
211 gdk_device_set_axis_use (&gdkdev->info, j++, GDK_AXIS_WHEEL);
216 class = (XAnyClassPtr)(((char *)class) + class->length);
218 /* return NULL if no axes */
219 if (!gdkdev->info.num_axes || !gdkdev->axes ||
220 (!include_core && device->use == IsXPointer))
223 if (device->use != IsXPointer)
225 gdk_error_trap_push ();
226 gdkdev->xdevice = XOpenDevice (GDK_DISPLAY_XDISPLAY (display),
229 /* return NULL if device is not ready */
230 if (gdk_error_trap_pop ())
234 gdkdev->buttonpress_type = 0;
235 gdkdev->buttonrelease_type = 0;
236 gdkdev->keypress_type = 0;
237 gdkdev->keyrelease_type = 0;
238 gdkdev->motionnotify_type = 0;
239 gdkdev->proximityin_type = 0;
240 gdkdev->proximityout_type = 0;
241 gdkdev->changenotify_type = 0;
247 g_free (gdkdev->info.name);
249 g_free (gdkdev->axes);
250 if (gdkdev->info.keys)
251 g_free (gdkdev->info.keys);
252 if (gdkdev->info.axes)
253 g_free (gdkdev->info.axes);
254 g_object_unref (gdkdev);
260 _gdk_input_common_find_events(GdkWindow *window,
261 GdkDevicePrivate *gdkdev,
263 XEventClass *classes,
270 if (mask & GDK_BUTTON_PRESS_MASK || mask & GDK_BUTTON_RELEASE_MASK)
272 DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
275 classes[i++] = class;
276 DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
278 classes[i++] = class;
280 if (mask & GDK_POINTER_MOTION_MASK)
282 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
284 classes[i++] = class;
287 if (mask & (GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
288 GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK |
289 GDK_POINTER_MOTION_HINT_MASK))
291 /* Make sure gdkdev->motionnotify_type is set */
292 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
294 if (mask & GDK_BUTTON1_MOTION_MASK)
296 DeviceButton1Motion (gdkdev->xdevice, 0, class);
298 classes[i++] = class;
300 if (mask & GDK_BUTTON2_MOTION_MASK)
302 DeviceButton2Motion (gdkdev->xdevice, 0, class);
304 classes[i++] = class;
306 if (mask & GDK_BUTTON3_MOTION_MASK)
308 DeviceButton3Motion (gdkdev->xdevice, 0, class);
310 classes[i++] = class;
312 if (mask & GDK_BUTTON_MOTION_MASK)
314 DeviceButtonMotion (gdkdev->xdevice, 0, class);
316 classes[i++] = class;
318 if (mask & GDK_POINTER_MOTION_HINT_MASK)
320 /* We'll get into trouble if the macros change, but at least we'll
321 know about it, and we avoid warnings now */
322 DevicePointerMotionHint (gdkdev->xdevice, 0, class);
324 classes[i++] = class;
326 if (mask & GDK_KEY_PRESS_MASK)
328 DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
330 classes[i++] = class;
332 if (mask & GDK_KEY_RELEASE_MASK)
334 DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
336 classes[i++] = class;
338 if (mask & GDK_PROXIMITY_IN_MASK)
340 ProximityIn (gdkdev->xdevice, gdkdev->proximityin_type, class);
342 classes[i++] = class;
344 if (mask & GDK_PROXIMITY_OUT_MASK)
346 ProximityOut (gdkdev->xdevice, gdkdev->proximityout_type, class);
348 classes[i++] = class;
355 _gdk_input_common_select_events(GdkWindow *window,
356 GdkDevicePrivate *gdkdev)
358 XEventClass classes[GDK_MAX_DEVICE_CLASSES];
361 if (gdkdev->info.mode == GDK_MODE_DISABLED)
362 _gdk_input_common_find_events(window, gdkdev, 0, classes, &num_classes);
364 _gdk_input_common_find_events(window, gdkdev,
365 ((GdkWindowObject *)window)->extension_events,
366 classes, &num_classes);
368 XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (window),
369 GDK_WINDOW_XWINDOW (window),
370 classes, num_classes);
374 _gdk_input_common_init (GdkDisplay *display,
378 XDeviceInfo *devices;
380 int num_extensions, loop;
381 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
383 /* Init XInput extension */
385 extensions = XListExtensions(display_x11->xdisplay, &num_extensions);
386 for (loop = 0; loop < num_extensions &&
387 (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
388 XFreeExtensionList(extensions);
389 display_x11->input_devices = NULL;
390 if (loop < num_extensions)
392 /* XInput extension found */
394 devices = XListInputDevices(display_x11->xdisplay, &num_devices);
396 for(loop=0; loop<num_devices; loop++)
398 GdkDevicePrivate *gdkdev = gdk_input_device_new(display,
402 display_x11->input_devices = g_list_append(display_x11->input_devices, gdkdev);
404 XFreeDeviceList(devices);
407 display_x11->input_devices = g_list_append (display_x11->input_devices, display->core_pointer);
413 gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
414 GdkInputWindow *input_window,
420 GdkWindowImplX11 *impl;
425 double device_width, device_height;
426 double x_offset, y_offset, x_scale, y_scale;
428 impl = GDK_WINDOW_IMPL_X11 (((GdkWindowObject *) input_window->window)->impl);
430 for (i=0; i<gdkdev->info.num_axes; i++)
432 switch (gdkdev->info.axes[i].use)
445 device_width = gdkdev->axes[x_axis].max_value -
446 gdkdev->axes[x_axis].min_value;
447 device_height = gdkdev->axes[y_axis].max_value -
448 gdkdev->axes[y_axis].min_value;
450 if (gdkdev->info.mode == GDK_MODE_SCREEN)
452 x_scale = gdk_screen_get_width (gdk_drawable_get_screen (input_window->window)) / device_width;
453 y_scale = gdk_screen_get_height (gdk_drawable_get_screen (input_window->window)) / device_height;
455 x_offset = - input_window->root_x;
456 y_offset = - input_window->root_y;
458 else /* GDK_MODE_WINDOW */
460 double device_aspect = (device_height*gdkdev->axes[y_axis].resolution) /
461 (device_width*gdkdev->axes[x_axis].resolution);
463 if (device_aspect * impl->width >= impl->height)
465 /* device taller than window */
466 x_scale = impl->width / device_width;
467 y_scale = (x_scale * gdkdev->axes[x_axis].resolution)
468 / gdkdev->axes[y_axis].resolution;
471 y_offset = -(device_height * y_scale -
476 /* window taller than device */
477 y_scale = impl->height / device_height;
478 x_scale = (y_scale * gdkdev->axes[y_axis].resolution)
479 / gdkdev->axes[x_axis].resolution;
482 x_offset = - (device_width * x_scale - impl->width)/2;
486 for (i=0; i<gdkdev->info.num_axes; i++)
488 switch (gdkdev->info.axes[i].use)
491 axis_out[i] = x_offset + x_scale*axis_data[x_axis];
493 *x_out = axis_out[i];
496 axis_out[i] = y_offset + y_scale*axis_data[y_axis];
498 *y_out = axis_out[i];
502 (gdkdev->info.axes[i].max * (axis_data[i] - gdkdev->axes[i].min_value) +
503 gdkdev->info.axes[i].min * (gdkdev->axes[i].max_value - axis_data[i])) /
504 (gdkdev->axes[i].max_value - gdkdev->axes[i].min_value);
510 /* combine the state of the core device and the device state
511 * into one - for now we do this in a simple-minded manner -
512 * we just take the keyboard portion of the core device and
513 * the button portion (all of?) the device state.
514 * Any button remapping should go on here.
517 gdk_input_translate_state(guint state, guint device_state)
519 return device_state | (state & 0xFF);
524 _gdk_input_common_other_event (GdkEvent *event,
526 GdkInputWindow *input_window,
527 GdkDevicePrivate *gdkdev)
529 if ((xevent->type == gdkdev->buttonpress_type) ||
530 (xevent->type == gdkdev->buttonrelease_type))
532 XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);
534 if (xdbe->type == gdkdev->buttonpress_type)
536 event->button.type = GDK_BUTTON_PRESS;
537 gdkdev->button_state |= 1 << xdbe->button;
541 event->button.type = GDK_BUTTON_RELEASE;
542 gdkdev->button_state &= ~(1 << xdbe->button);
544 event->button.device = &gdkdev->info;
545 event->button.window = input_window->window;
546 event->button.time = xdbe->time;
548 event->button.axes = g_new (gdouble, gdkdev->info.num_axes);
549 gdk_input_translate_coordinates (gdkdev,input_window, xdbe->axis_data,
551 &event->button.x,&event->button.y);
552 event->button.state = gdk_input_translate_state(xdbe->state,xdbe->device_state);
553 event->button.button = xdbe->button;
556 g_print ("button %s:\t\twindow: %ld device: %ld x,y: %f %f button: %d\n",
557 (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
560 event->button.x, event->button.y,
566 if ((xevent->type == gdkdev->keypress_type) ||
567 (xevent->type == gdkdev->keyrelease_type))
569 XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);
572 g_print ("device key %s:\twindow: %ld device: %ld keycode: %d\n",
573 (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
578 if (xdke->keycode < gdkdev->min_keycode ||
579 xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
581 g_warning ("Invalid device key code received");
585 event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;
587 if (event->key.keyval == 0)
590 g_print ("\t\ttranslation - NONE\n"));
595 event->key.type = (xdke->type == gdkdev->keypress_type) ?
596 GDK_KEY_PRESS : GDK_KEY_RELEASE;
598 event->key.window = input_window->window;
599 event->key.time = xdke->time;
601 event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
602 | gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;
604 /* Add a string translation for the key event */
605 if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
607 event->key.length = 1;
608 event->key.string = g_new (gchar, 2);
609 event->key.string[0] = (gchar)event->key.keyval;
610 event->key.string[1] = 0;
614 event->key.length = 0;
615 event->key.string = g_new0 (gchar, 1);
619 g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
626 if (xevent->type == gdkdev->motionnotify_type)
628 XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);
630 event->motion.device = &gdkdev->info;
632 event->motion.axes = g_new (gdouble, gdkdev->info.num_axes);
633 gdk_input_translate_coordinates(gdkdev,input_window,xdme->axis_data,
635 &event->motion.x,&event->motion.y);
637 event->motion.type = GDK_MOTION_NOTIFY;
638 event->motion.window = input_window->window;
639 event->motion.time = xdme->time;
640 event->motion.state = gdk_input_translate_state(xdme->state,
642 event->motion.is_hint = xdme->is_hint;
645 g_print ("motion notify:\t\twindow: %ld device: %ld x,y: %f %f state %#4x hint: %s\n",
648 event->motion.x, event->motion.y,
650 (xdme->is_hint) ? "true" : "false"));
656 if (xevent->type == gdkdev->proximityin_type ||
657 xevent->type == gdkdev->proximityout_type)
659 XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);
661 event->proximity.device = &gdkdev->info;
662 event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
663 GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
664 event->proximity.window = input_window->window;
665 event->proximity.time = xpne->time;
670 return -1; /* wasn't one of our event types */
674 _gdk_device_get_history (GdkDevice *device,
678 GdkTimeCoord ***events,
681 GdkTimeCoord **coords;
682 XDeviceTimeCoord *device_coords;
683 GdkInputWindow *input_window;
684 GdkDevicePrivate *gdkdev;
686 gint axis_count_return;
689 gdkdev = (GdkDevicePrivate *)device;
690 input_window = _gdk_input_window_find (window);
692 g_return_val_if_fail (input_window != NULL, FALSE);
694 device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (window),
697 n_events, &mode_return,
702 coords = _gdk_device_allocate_history (device, *n_events);
704 for (i=0; i<*n_events; i++)
705 gdk_input_translate_coordinates (gdkdev, input_window,
706 device_coords[i].data,
707 coords[i]->axes, NULL, NULL);
708 XFreeDeviceMotionEvents (device_coords);
719 gdk_device_get_state (GdkDevice *device,
722 GdkModifierType *mask)
726 g_return_if_fail (device != NULL);
727 g_return_if_fail (GDK_IS_WINDOW (window));
729 if (GDK_IS_CORE (device))
733 gdk_window_get_pointer (window, &x_int, &y_int, mask);
743 GdkDevicePrivate *gdkdev;
744 GdkInputWindow *input_window;
746 XInputClass *input_class;
749 gdk_window_get_pointer (window, NULL, NULL, mask);
751 gdkdev = (GdkDevicePrivate *)device;
752 input_window = _gdk_input_window_find (window);
753 g_return_if_fail (input_window != NULL);
755 state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
757 input_class = state->data;
758 for (i=0; i<state->num_classes; i++)
760 switch (input_class->class)
764 gdk_input_translate_coordinates (gdkdev, input_window,
765 ((XValuatorState *)input_class)->valuators,
773 if (((XButtonState *)input_class)->num_buttons > 0)
774 *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
775 /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
776 * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
780 input_class = (XInputClass *)(((char *)input_class)+input_class->length);
782 XFreeDeviceState (state);