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 /* We have to track press and release events in pairs to keep
271 track of button state correctly and implement grabbing for
272 the gxi support. FIXME - is this needed any more since gxi is gone? */
273 if (mask & GDK_BUTTON_PRESS_MASK || mask & GDK_BUTTON_RELEASE_MASK)
275 DeviceButtonPress (gdkdev->xdevice, gdkdev->buttonpress_type,
278 classes[i++] = class;
279 DeviceButtonPressGrab (gdkdev->xdevice, 0, class);
281 classes[i++] = class;
282 DeviceButtonRelease (gdkdev->xdevice, gdkdev->buttonrelease_type,
285 classes[i++] = class;
287 if (mask & GDK_POINTER_MOTION_MASK)
289 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
291 classes[i++] = class;
294 if (mask & (GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
295 GDK_BUTTON3_MOTION_MASK | GDK_BUTTON_MOTION_MASK |
296 GDK_POINTER_MOTION_HINT_MASK))
298 /* Make sure gdkdev->motionnotify_type is set */
299 DeviceMotionNotify (gdkdev->xdevice, gdkdev->motionnotify_type, class);
301 if (mask & GDK_BUTTON1_MOTION_MASK)
303 DeviceButton1Motion (gdkdev->xdevice, 0, class);
305 classes[i++] = class;
307 if (mask & GDK_BUTTON2_MOTION_MASK)
309 DeviceButton2Motion (gdkdev->xdevice, 0, class);
311 classes[i++] = class;
313 if (mask & GDK_BUTTON3_MOTION_MASK)
315 DeviceButton3Motion (gdkdev->xdevice, 0, class);
317 classes[i++] = class;
319 if (mask & GDK_BUTTON_MOTION_MASK)
321 DeviceButtonMotion (gdkdev->xdevice, 0, class);
323 classes[i++] = class;
325 if (mask & GDK_POINTER_MOTION_HINT_MASK)
327 /* We'll get into trouble if the macros change, but at least we'll
328 know about it, and we avoid warnings now */
329 DevicePointerMotionHint (gdkdev->xdevice, 0, class);
331 classes[i++] = class;
333 if (mask & GDK_KEY_PRESS_MASK)
335 DeviceKeyPress (gdkdev->xdevice, gdkdev->keypress_type, class);
337 classes[i++] = class;
339 if (mask & GDK_KEY_RELEASE_MASK)
341 DeviceKeyRelease (gdkdev->xdevice, gdkdev->keyrelease_type, class);
343 classes[i++] = class;
345 if (mask & GDK_PROXIMITY_IN_MASK)
347 ProximityIn (gdkdev->xdevice, gdkdev->proximityin_type, class);
349 classes[i++] = class;
351 if (mask & GDK_PROXIMITY_OUT_MASK)
353 ProximityOut (gdkdev->xdevice, gdkdev->proximityout_type, class);
355 classes[i++] = class;
362 _gdk_input_common_select_events(GdkWindow *window,
363 GdkDevicePrivate *gdkdev)
365 XEventClass classes[GDK_MAX_DEVICE_CLASSES];
368 if (gdkdev->info.mode == GDK_MODE_DISABLED)
369 _gdk_input_common_find_events(window, gdkdev, 0, classes, &num_classes);
371 _gdk_input_common_find_events(window, gdkdev,
372 ((GdkWindowObject *)window)->extension_events,
373 classes, &num_classes);
375 XSelectExtensionEvent (GDK_WINDOW_XDISPLAY (window),
376 GDK_WINDOW_XWINDOW (window),
377 classes, num_classes);
381 _gdk_input_common_init (GdkDisplay *display,
385 XDeviceInfo *devices;
387 int num_extensions, loop;
388 GdkDisplayX11 *display_x11 = GDK_DISPLAY_X11 (display);
390 /* Init XInput extension */
392 extensions = XListExtensions(display_x11->xdisplay, &num_extensions);
393 for (loop = 0; loop < num_extensions &&
394 (strcmp(extensions[loop], "XInputExtension") != 0); loop++);
395 XFreeExtensionList(extensions);
396 display_x11->input_devices = NULL;
397 if (loop < num_extensions)
399 /* XInput extension found */
401 devices = XListInputDevices(display_x11->xdisplay, &num_devices);
403 for(loop=0; loop<num_devices; loop++)
405 GdkDevicePrivate *gdkdev = gdk_input_device_new(display,
409 display_x11->input_devices = g_list_append(display_x11->input_devices, gdkdev);
411 XFreeDeviceList(devices);
414 display_x11->input_devices = g_list_append (display_x11->input_devices, display->core_pointer);
420 gdk_input_translate_coordinates (GdkDevicePrivate *gdkdev,
421 GdkInputWindow *input_window,
427 GdkWindowImplX11 *impl;
432 double device_width, device_height;
433 double x_offset, y_offset, x_scale, y_scale;
435 impl = GDK_WINDOW_IMPL_X11 (((GdkWindowObject *) input_window->window)->impl);
437 for (i=0; i<gdkdev->info.num_axes; i++)
439 switch (gdkdev->info.axes[i].use)
452 device_width = gdkdev->axes[x_axis].max_value -
453 gdkdev->axes[x_axis].min_value;
454 device_height = gdkdev->axes[y_axis].max_value -
455 gdkdev->axes[y_axis].min_value;
457 if (gdkdev->info.mode == GDK_MODE_SCREEN)
459 x_scale = gdk_screen_get_width (gdk_drawable_get_screen (input_window->window)) / device_width;
460 y_scale = gdk_screen_get_height (gdk_drawable_get_screen (input_window->window)) / device_height;
462 x_offset = - input_window->root_x;
463 y_offset = - input_window->root_y;
465 else /* GDK_MODE_WINDOW */
467 double device_aspect = (device_height*gdkdev->axes[y_axis].resolution) /
468 (device_width*gdkdev->axes[x_axis].resolution);
470 if (device_aspect * impl->width >= impl->height)
472 /* device taller than window */
473 x_scale = impl->width / device_width;
474 y_scale = (x_scale * gdkdev->axes[x_axis].resolution)
475 / gdkdev->axes[y_axis].resolution;
478 y_offset = -(device_height * y_scale -
483 /* window taller than device */
484 y_scale = impl->height / device_height;
485 x_scale = (y_scale * gdkdev->axes[y_axis].resolution)
486 / gdkdev->axes[x_axis].resolution;
489 x_offset = - (device_width * x_scale - impl->width)/2;
493 for (i=0; i<gdkdev->info.num_axes; i++)
495 switch (gdkdev->info.axes[i].use)
498 axis_out[i] = x_offset + x_scale*axis_data[x_axis];
500 *x_out = axis_out[i];
503 axis_out[i] = y_offset + y_scale*axis_data[y_axis];
505 *y_out = axis_out[i];
509 (gdkdev->info.axes[i].max * (axis_data[i] - gdkdev->axes[i].min_value) +
510 gdkdev->info.axes[i].min * (gdkdev->axes[i].max_value - axis_data[i])) /
511 (gdkdev->axes[i].max_value - gdkdev->axes[i].min_value);
517 /* combine the state of the core device and the device state
518 * into one - for now we do this in a simple-minded manner -
519 * we just take the keyboard portion of the core device and
520 * the button portion (all of?) the device state.
521 * Any button remapping should go on here.
524 gdk_input_translate_state(guint state, guint device_state)
526 return device_state | (state & 0xFF);
531 _gdk_input_common_other_event (GdkEvent *event,
533 GdkInputWindow *input_window,
534 GdkDevicePrivate *gdkdev)
536 if ((xevent->type == gdkdev->buttonpress_type) ||
537 (xevent->type == gdkdev->buttonrelease_type))
539 XDeviceButtonEvent *xdbe = (XDeviceButtonEvent *)(xevent);
541 if (xdbe->type == gdkdev->buttonpress_type)
543 event->button.type = GDK_BUTTON_PRESS;
544 gdkdev->button_state |= 1 << xdbe->button;
548 event->button.type = GDK_BUTTON_RELEASE;
549 gdkdev->button_state &= ~(1 << xdbe->button);
551 event->button.device = &gdkdev->info;
552 event->button.window = input_window->window;
553 event->button.time = xdbe->time;
555 event->button.axes = g_new (gdouble, gdkdev->info.num_axes);
556 gdk_input_translate_coordinates (gdkdev,input_window, xdbe->axis_data,
558 &event->button.x,&event->button.y);
559 event->button.state = gdk_input_translate_state(xdbe->state,xdbe->device_state);
560 event->button.button = xdbe->button;
563 g_print ("button %s:\t\twindow: %ld device: %ld x,y: %f %f button: %d\n",
564 (event->button.type == GDK_BUTTON_PRESS) ? "press" : "release",
567 event->button.x, event->button.y,
573 if ((xevent->type == gdkdev->keypress_type) ||
574 (xevent->type == gdkdev->keyrelease_type))
576 XDeviceKeyEvent *xdke = (XDeviceKeyEvent *)(xevent);
579 g_print ("device key %s:\twindow: %ld device: %ld keycode: %d\n",
580 (event->key.type == GDK_KEY_PRESS) ? "press" : "release",
585 if (xdke->keycode < gdkdev->min_keycode ||
586 xdke->keycode >= gdkdev->min_keycode + gdkdev->info.num_keys)
588 g_warning ("Invalid device key code received");
592 event->key.keyval = gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].keyval;
594 if (event->key.keyval == 0)
597 g_print ("\t\ttranslation - NONE\n"));
602 event->key.type = (xdke->type == gdkdev->keypress_type) ?
603 GDK_KEY_PRESS : GDK_KEY_RELEASE;
605 event->key.window = input_window->window;
606 event->key.time = xdke->time;
608 event->key.state = gdk_input_translate_state(xdke->state, xdke->device_state)
609 | gdkdev->info.keys[xdke->keycode - gdkdev->min_keycode].modifiers;
611 /* Add a string translation for the key event */
612 if ((event->key.keyval >= 0x20) && (event->key.keyval <= 0xFF))
614 event->key.length = 1;
615 event->key.string = g_new (gchar, 2);
616 event->key.string[0] = (gchar)event->key.keyval;
617 event->key.string[1] = 0;
621 event->key.length = 0;
622 event->key.string = g_new0 (gchar, 1);
626 g_print ("\t\ttranslation - keyval: %d modifiers: %#x\n",
633 if (xevent->type == gdkdev->motionnotify_type)
635 XDeviceMotionEvent *xdme = (XDeviceMotionEvent *)(xevent);
637 event->motion.device = &gdkdev->info;
639 event->motion.axes = g_new (gdouble, gdkdev->info.num_axes);
640 gdk_input_translate_coordinates(gdkdev,input_window,xdme->axis_data,
642 &event->motion.x,&event->motion.y);
644 event->motion.type = GDK_MOTION_NOTIFY;
645 event->motion.window = input_window->window;
646 event->motion.time = xdme->time;
647 event->motion.state = gdk_input_translate_state(xdme->state,
649 event->motion.is_hint = xdme->is_hint;
652 g_print ("motion notify:\t\twindow: %ld device: %ld x,y: %f %f state %#4x hint: %s\n",
655 event->motion.x, event->motion.y,
657 (xdme->is_hint) ? "true" : "false"));
663 if (xevent->type == gdkdev->proximityin_type ||
664 xevent->type == gdkdev->proximityout_type)
666 XProximityNotifyEvent *xpne = (XProximityNotifyEvent *)(xevent);
668 event->proximity.device = &gdkdev->info;
669 event->proximity.type = (xevent->type == gdkdev->proximityin_type)?
670 GDK_PROXIMITY_IN:GDK_PROXIMITY_OUT;
671 event->proximity.window = input_window->window;
672 event->proximity.time = xpne->time;
677 return -1; /* wasn't one of our event types */
681 _gdk_device_get_history (GdkDevice *device,
685 GdkTimeCoord ***events,
688 GdkTimeCoord **coords;
689 XDeviceTimeCoord *device_coords;
690 GdkInputWindow *input_window;
691 GdkDevicePrivate *gdkdev;
693 gint axis_count_return;
696 gdkdev = (GdkDevicePrivate *)device;
697 input_window = _gdk_input_window_find (window);
699 g_return_val_if_fail (input_window != NULL, FALSE);
701 device_coords = XGetDeviceMotionEvents (GDK_WINDOW_XDISPLAY (window),
704 n_events, &mode_return,
709 coords = _gdk_device_allocate_history (device, *n_events);
711 for (i=0; i<*n_events; i++)
712 gdk_input_translate_coordinates (gdkdev, input_window,
713 device_coords[i].data,
714 coords[i]->axes, NULL, NULL);
715 XFreeDeviceMotionEvents (device_coords);
726 gdk_device_get_state (GdkDevice *device,
729 GdkModifierType *mask)
733 g_return_if_fail (device != NULL);
734 g_return_if_fail (GDK_IS_WINDOW (window));
736 if (GDK_IS_CORE (device))
740 gdk_window_get_pointer (window, &x_int, &y_int, mask);
750 GdkDevicePrivate *gdkdev;
751 GdkInputWindow *input_window;
753 XInputClass *input_class;
756 gdk_window_get_pointer (window, NULL, NULL, mask);
758 gdkdev = (GdkDevicePrivate *)device;
759 input_window = _gdk_input_window_find (window);
760 g_return_if_fail (input_window != NULL);
762 state = XQueryDeviceState (GDK_WINDOW_XDISPLAY (window),
764 input_class = state->data;
765 for (i=0; i<state->num_classes; i++)
767 switch (input_class->class)
771 gdk_input_translate_coordinates (gdkdev, input_window,
772 ((XValuatorState *)input_class)->valuators,
780 if (((XButtonState *)input_class)->num_buttons > 0)
781 *mask |= ((XButtonState *)input_class)->buttons[0] << 7;
782 /* GDK_BUTTON1_MASK = 1 << 8, and button n is stored
783 * in bit 1<<(n%8) in byte n/8. n = 1,2,... */
787 input_class = (XInputClass *)(((char *)input_class)+input_class->length);
789 XFreeDeviceState (state);