1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
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, see <http://www.gnu.org/licenses/>.
20 #include "gdkx11device-core.h"
21 #include "gdkdeviceprivate.h"
23 #include "gdkinternals.h"
24 #include "gdkwindow.h"
25 #include "gdkprivate-x11.h"
28 struct _GdkX11DeviceCore
30 GdkDevice parent_instance;
33 struct _GdkX11DeviceCoreClass
35 GdkDeviceClass parent_class;
38 static gboolean gdk_x11_device_core_get_history (GdkDevice *device,
42 GdkTimeCoord ***events,
44 static void gdk_x11_device_core_get_state (GdkDevice *device,
47 GdkModifierType *mask);
48 static void gdk_x11_device_core_set_window_cursor (GdkDevice *device,
51 static void gdk_x11_device_core_warp (GdkDevice *device,
55 static gboolean gdk_x11_device_core_query_state (GdkDevice *device,
57 GdkWindow **root_window,
58 GdkWindow **child_window,
63 GdkModifierType *mask);
64 static GdkGrabStatus gdk_x11_device_core_grab (GdkDevice *device,
66 gboolean owner_events,
67 GdkEventMask event_mask,
68 GdkWindow *confine_to,
71 static void gdk_x11_device_core_ungrab (GdkDevice *device,
73 static GdkWindow * gdk_x11_device_core_window_at_position (GdkDevice *device,
76 GdkModifierType *mask,
77 gboolean get_toplevel);
78 static void gdk_x11_device_core_select_window_events (GdkDevice *device,
80 GdkEventMask event_mask);
82 G_DEFINE_TYPE (GdkX11DeviceCore, gdk_x11_device_core, GDK_TYPE_DEVICE)
85 gdk_x11_device_core_class_init (GdkX11DeviceCoreClass *klass)
87 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
89 device_class->get_history = gdk_x11_device_core_get_history;
90 device_class->get_state = gdk_x11_device_core_get_state;
91 device_class->set_window_cursor = gdk_x11_device_core_set_window_cursor;
92 device_class->warp = gdk_x11_device_core_warp;
93 device_class->query_state = gdk_x11_device_core_query_state;
94 device_class->grab = gdk_x11_device_core_grab;
95 device_class->ungrab = gdk_x11_device_core_ungrab;
96 device_class->window_at_position = gdk_x11_device_core_window_at_position;
97 device_class->select_window_events = gdk_x11_device_core_select_window_events;
101 gdk_x11_device_core_init (GdkX11DeviceCore *device_core)
105 device = GDK_DEVICE (device_core);
107 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
108 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
112 impl_coord_in_window (GdkWindow *window,
116 if (impl_x < window->abs_x ||
117 impl_x >= window->abs_x + window->width)
120 if (impl_y < window->abs_y ||
121 impl_y >= window->abs_y + window->height)
128 gdk_x11_device_core_get_history (GdkDevice *device,
132 GdkTimeCoord ***events,
136 GdkTimeCoord **coords;
137 GdkWindow *impl_window;
141 impl_window = _gdk_window_get_impl_window (window);
142 xcoords = XGetMotionEvents (GDK_WINDOW_XDISPLAY (window),
143 GDK_WINDOW_XID (impl_window),
144 start, stop, &tmp_n_events);
148 coords = _gdk_device_allocate_history (device, tmp_n_events);
150 for (i = 0, j = 0; i < tmp_n_events; i++)
152 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
154 coords[j]->time = xcoords[i].time;
155 coords[j]->axes[0] = xcoords[i].x - window->abs_x;
156 coords[j]->axes[1] = xcoords[i].y - window->abs_y;
163 /* free the events we allocated too much */
164 for (i = j; i < tmp_n_events; i++)
172 if (tmp_n_events == 0)
174 gdk_device_free_history (coords, tmp_n_events);
179 *n_events = tmp_n_events;
184 gdk_device_free_history (coords, tmp_n_events);
190 gdk_x11_device_core_get_state (GdkDevice *device,
193 GdkModifierType *mask)
197 gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
207 gdk_x11_device_core_set_window_cursor (GdkDevice *device,
216 xcursor = gdk_x11_cursor_get_xcursor (cursor);
218 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
219 GDK_WINDOW_XID (window),
224 gdk_x11_device_core_warp (GdkDevice *device,
232 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
233 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
235 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
239 gdk_x11_device_core_query_state (GdkDevice *device,
241 GdkWindow **root_window,
242 GdkWindow **child_window,
247 GdkModifierType *mask)
250 GdkScreen *default_screen;
251 Window xroot_window, xchild_window;
252 int xroot_x, xroot_y, xwin_x, xwin_y;
255 display = gdk_window_get_display (window);
256 default_screen = gdk_display_get_default_screen (display);
258 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
260 if (!XQueryPointer (GDK_WINDOW_XDISPLAY (window),
261 GDK_WINDOW_XID (window),
271 XSetWindowAttributes attributes;
275 /* FIXME: untrusted clients not multidevice-safe */
276 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
277 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
279 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
280 CopyFromParent, InputOnly, CopyFromParent,
282 XQueryPointer (xdisplay, w,
288 XDestroyWindow (xdisplay, w);
292 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
295 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
316 gdk_x11_device_core_grab (GdkDevice *device,
318 gboolean owner_events,
319 GdkEventMask event_mask,
320 GdkWindow *confine_to,
325 Window xwindow, xconfine_to;
328 display = gdk_device_get_display (device);
330 xwindow = GDK_WINDOW_XID (window);
333 confine_to = _gdk_window_get_impl_window (confine_to);
335 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
338 xconfine_to = GDK_WINDOW_XID (confine_to);
340 #ifdef G_ENABLE_DEBUG
341 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
342 status = GrabSuccess;
345 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
347 /* Device is a keyboard */
348 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
351 GrabModeAsync, GrabModeAsync,
360 /* Device is a pointer */
365 _gdk_x11_cursor_update_theme (cursor);
366 xcursor = gdk_x11_cursor_get_xcursor (cursor);
371 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
373 if (event_mask & (1 << (i + 1)))
374 xevent_mask |= _gdk_x11_event_mask_table[i];
377 /* We don't want to set a native motion hint mask, as we're emulating motion
378 * hints. If we set a native one we just wouldn't get any events.
380 xevent_mask &= ~PointerMotionHintMask;
382 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
386 GrabModeAsync, GrabModeAsync,
392 _gdk_x11_display_update_grab_info (display, device, status);
394 return _gdk_x11_convert_grab_status (status);
398 gdk_x11_device_core_ungrab (GdkDevice *device,
404 display = gdk_device_get_display (device);
405 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
407 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
408 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
410 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
412 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
416 gdk_x11_device_core_window_at_position (GdkDevice *device,
419 GdkModifierType *mask,
420 gboolean get_toplevel)
426 Window xwindow, root, child, last;
427 int xroot_x, xroot_y, xwin_x, xwin_y;
431 display = gdk_device_get_display (device);
432 screen = gdk_display_get_default_screen (display);
434 /* This function really only works if the mouse pointer is held still
435 * during its operation. If it moves from one leaf window to another
436 * than we'll end up with inaccurate values for win_x, win_y
439 gdk_x11_display_grab (display);
441 xdisplay = GDK_SCREEN_XDISPLAY (screen);
442 xwindow = GDK_SCREEN_XROOTWIN (screen);
444 if (G_LIKELY (GDK_X11_DISPLAY (display)->trusted_client))
446 XQueryPointer (xdisplay, xwindow,
459 gint i, screens, width, height;
460 GList *toplevels, *list;
461 Window pointer_window, root, child;
462 int rootx = -1, rooty = -1;
466 /* FIXME: untrusted clients case not multidevice-safe */
467 pointer_window = None;
468 screens = gdk_display_get_n_screens (display);
470 for (i = 0; i < screens; ++i)
472 screen = gdk_display_get_screen (display, i);
473 toplevels = gdk_screen_get_toplevel_windows (screen);
474 for (list = toplevels; list != NULL; list = g_list_next (list))
476 window = GDK_WINDOW (list->data);
477 xwindow = GDK_WINDOW_XID (window);
478 gdk_x11_display_error_trap_push (display);
479 XQueryPointer (xdisplay, xwindow,
484 if (gdk_x11_display_error_trap_pop (display))
488 pointer_window = child;
491 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
492 if (winx >= 0 && winy >= 0 && winx < width && winy < height)
494 /* A childless toplevel, or below another window? */
495 XSetWindowAttributes attributes;
498 w = XCreateWindow (xdisplay, xwindow, winx, winy, 1, 1, 0,
499 CopyFromParent, InputOnly, CopyFromParent,
501 XMapWindow (xdisplay, w);
502 XQueryPointer (xdisplay, xwindow,
507 XDestroyWindow (xdisplay, w);
510 pointer_window = xwindow;
516 g_list_free (toplevels);
517 if (pointer_window != None)
521 xwindow = pointer_window;
527 gdk_x11_display_error_trap_push (display);
528 XQueryPointer (xdisplay, xwindow,
533 if (gdk_x11_display_error_trap_pop (display))
536 if (get_toplevel && last != root &&
537 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
538 window->window_type != GDK_WINDOW_FOREIGN)
545 gdk_x11_display_ungrab (display);
547 window = gdk_x11_window_lookup_for_display (display, last);
550 *win_x = (window) ? xwin_x : -1;
553 *win_y = (window) ? xwin_y : -1;
562 gdk_x11_device_core_select_window_events (GdkDevice *device,
564 GdkEventMask event_mask)
566 GdkEventMask filter_mask, window_mask;
570 window_mask = gdk_window_get_events (window);
571 filter_mask = (GDK_POINTER_MOTION_MASK &
572 GDK_POINTER_MOTION_HINT_MASK &
573 GDK_BUTTON_MOTION_MASK &
574 GDK_BUTTON1_MOTION_MASK &
575 GDK_BUTTON2_MOTION_MASK &
576 GDK_BUTTON3_MOTION_MASK &
577 GDK_BUTTON_PRESS_MASK &
578 GDK_BUTTON_RELEASE_MASK &
580 GDK_KEY_RELEASE_MASK &
581 GDK_ENTER_NOTIFY_MASK &
582 GDK_LEAVE_NOTIFY_MASK &
583 GDK_FOCUS_CHANGE_MASK &
584 GDK_PROXIMITY_IN_MASK &
585 GDK_PROXIMITY_OUT_MASK &
588 /* Filter out non-device events */
589 event_mask &= filter_mask;
591 /* Unset device events on window mask */
592 window_mask &= ~(filter_mask);
595 event_mask |= window_mask;
597 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
599 if (event_mask & (1 << (i + 1)))
600 xmask |= _gdk_x11_event_mask_table[i];
603 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
604 xmask |= StructureNotifyMask | PropertyChangeMask;
606 XSelectInput (GDK_WINDOW_XDISPLAY (window),
607 GDK_WINDOW_XID (window),