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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
22 #include "gdkdevice-core.h"
24 #include "gdkinternals.h"
25 #include "gdkwindow.h"
26 #include "gdkprivate-x11.h"
29 static gboolean gdk_device_core_get_history (GdkDevice *device,
33 GdkTimeCoord ***events,
35 static void gdk_device_core_get_state (GdkDevice *device,
38 GdkModifierType *mask);
39 static void gdk_device_core_set_window_cursor (GdkDevice *device,
42 static void gdk_device_core_warp (GdkDevice *device,
46 static gboolean gdk_device_core_query_state (GdkDevice *device,
48 GdkWindow **root_window,
49 GdkWindow **child_window,
54 GdkModifierType *mask);
55 static GdkGrabStatus gdk_device_core_grab (GdkDevice *device,
57 gboolean owner_events,
58 GdkEventMask event_mask,
59 GdkWindow *confine_to,
62 static void gdk_device_core_ungrab (GdkDevice *device,
64 static GdkWindow * gdk_device_core_window_at_position (GdkDevice *device,
67 GdkModifierType *mask,
68 gboolean get_toplevel);
69 static void gdk_device_core_select_window_events (GdkDevice *device,
71 GdkEventMask event_mask);
74 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
77 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
79 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
81 device_class->get_history = gdk_device_core_get_history;
82 device_class->get_state = gdk_device_core_get_state;
83 device_class->set_window_cursor = gdk_device_core_set_window_cursor;
84 device_class->warp = gdk_device_core_warp;
85 device_class->query_state = gdk_device_core_query_state;
86 device_class->grab = gdk_device_core_grab;
87 device_class->ungrab = gdk_device_core_ungrab;
88 device_class->window_at_position = gdk_device_core_window_at_position;
89 device_class->select_window_events = gdk_device_core_select_window_events;
93 gdk_device_core_init (GdkDeviceCore *device_core)
97 device = GDK_DEVICE (device_core);
99 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
100 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
104 impl_coord_in_window (GdkWindow *window,
108 if (impl_x < window->abs_x ||
109 impl_x >= window->abs_x + window->width)
112 if (impl_y < window->abs_y ||
113 impl_y >= window->abs_y + window->height)
120 gdk_device_core_get_history (GdkDevice *device,
124 GdkTimeCoord ***events,
128 GdkTimeCoord **coords;
129 GdkWindow *impl_window;
133 impl_window = _gdk_window_get_impl_window (window);
134 xcoords = XGetMotionEvents (GDK_WINDOW_XDISPLAY (window),
135 GDK_WINDOW_XID (impl_window),
136 start, stop, &tmp_n_events);
140 coords = _gdk_device_allocate_history (device, tmp_n_events);
142 for (i = 0, j = 0; i < tmp_n_events; i++)
144 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
146 coords[j]->time = xcoords[i].time;
147 coords[j]->axes[0] = xcoords[i].x - window->abs_x;
148 coords[j]->axes[1] = xcoords[i].y - window->abs_y;
155 /* free the events we allocated too much */
156 for (i = j; i < tmp_n_events; i++)
164 if (tmp_n_events == 0)
166 gdk_device_free_history (coords, tmp_n_events);
171 *n_events = tmp_n_events;
176 gdk_device_free_history (coords, tmp_n_events);
182 gdk_device_core_get_state (GdkDevice *device,
185 GdkModifierType *mask)
189 gdk_window_get_pointer (window, &x_int, &y_int, mask);
199 gdk_device_core_set_window_cursor (GdkDevice *device,
208 xcursor = gdk_x11_cursor_get_xcursor (cursor);
210 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
211 GDK_WINDOW_XID (window),
216 gdk_device_core_warp (GdkDevice *device,
224 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
225 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
227 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
231 gdk_device_core_query_state (GdkDevice *device,
233 GdkWindow **root_window,
234 GdkWindow **child_window,
239 GdkModifierType *mask)
242 GdkScreen *default_screen;
243 Window xroot_window, xchild_window;
244 int xroot_x, xroot_y, xwin_x, xwin_y;
247 display = gdk_window_get_display (window);
248 default_screen = gdk_display_get_default_screen (display);
250 if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
252 if (!XQueryPointer (GDK_WINDOW_XDISPLAY (window),
253 GDK_WINDOW_XID (window),
265 XSetWindowAttributes attributes;
269 /* FIXME: untrusted clients not multidevice-safe */
270 xdisplay = GDK_SCREEN_XDISPLAY (default_screen);
271 xwindow = GDK_SCREEN_XROOTWIN (default_screen);
273 w = XCreateWindow (xdisplay, xwindow, 0, 0, 1, 1, 0,
274 CopyFromParent, InputOnly, CopyFromParent,
276 XQueryPointer (xdisplay, w,
284 XDestroyWindow (xdisplay, w);
288 *root_window = gdk_x11_window_lookup_for_display (display, xroot_window);
291 *child_window = gdk_x11_window_lookup_for_display (display, xchild_window);
312 gdk_device_core_grab (GdkDevice *device,
314 gboolean owner_events,
315 GdkEventMask event_mask,
316 GdkWindow *confine_to,
321 Window xwindow, xconfine_to;
324 display = gdk_device_get_display (device);
326 xwindow = GDK_WINDOW_XID (window);
329 confine_to = _gdk_window_get_impl_window (confine_to);
331 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
334 xconfine_to = GDK_WINDOW_XID (confine_to);
336 #ifdef G_ENABLE_DEBUG
337 if (_gdk_debug_flags & GDK_DEBUG_NOGRABS)
338 status = GrabSuccess;
341 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
343 /* Device is a keyboard */
344 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
347 GrabModeAsync, GrabModeAsync,
356 /* Device is a pointer */
361 _gdk_x11_cursor_update_theme (cursor);
362 xcursor = gdk_x11_cursor_get_xcursor (cursor);
367 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
369 if (event_mask & (1 << (i + 1)))
370 xevent_mask |= _gdk_x11_event_mask_table[i];
373 /* We don't want to set a native motion hint mask, as we're emulating motion
374 * hints. If we set a native one we just wouldn't get any events.
376 xevent_mask &= ~PointerMotionHintMask;
378 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
382 GrabModeAsync, GrabModeAsync,
388 _gdk_x11_display_update_grab_info (display, device, status);
390 return _gdk_x11_convert_grab_status (status);
394 gdk_device_core_ungrab (GdkDevice *device,
400 display = gdk_device_get_display (device);
401 serial = NextRequest (GDK_DISPLAY_XDISPLAY (display));
403 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
404 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
406 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
408 _gdk_x11_display_update_grab_info_ungrab (display, device, time_, serial);
412 gdk_device_core_window_at_position (GdkDevice *device,
415 GdkModifierType *mask,
416 gboolean get_toplevel)
422 Window xwindow, root, child, last;
423 int xroot_x, xroot_y, xwin_x, xwin_y;
427 display = gdk_device_get_display (device);
428 screen = gdk_display_get_default_screen (display);
430 /* This function really only works if the mouse pointer is held still
431 * during its operation. If it moves from one leaf window to another
432 * than we'll end up with inaccurate values for win_x, win_y
435 gdk_x11_display_grab (display);
437 xdisplay = GDK_SCREEN_XDISPLAY (screen);
438 xwindow = GDK_SCREEN_XROOTWIN (screen);
440 if (G_LIKELY (GDK_DISPLAY_X11 (display)->trusted_client))
442 XQueryPointer (xdisplay, xwindow,
455 gint i, screens, width, height;
456 GList *toplevels, *list;
457 Window pointer_window, root, child;
458 int rootx = -1, rooty = -1;
462 /* FIXME: untrusted clients case not multidevice-safe */
463 pointer_window = None;
464 screens = gdk_display_get_n_screens (display);
466 for (i = 0; i < screens; ++i)
468 screen = gdk_display_get_screen (display, i);
469 toplevels = gdk_screen_get_toplevel_windows (screen);
470 for (list = toplevels; list != NULL; list = g_list_next (list))
472 window = GDK_WINDOW (list->data);
473 xwindow = GDK_WINDOW_XID (window);
474 gdk_x11_display_error_trap_push (display);
475 XQueryPointer (xdisplay, xwindow,
476 &root, &child, &rootx, &rooty, &winx, &winy, &xmask);
477 if (gdk_x11_display_error_trap_pop (display))
481 pointer_window = child;
484 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
485 if (winx >= 0 && winy >= 0 && winx < width && winy < height)
487 /* A childless toplevel, or below another window? */
488 XSetWindowAttributes attributes;
491 w = XCreateWindow (xdisplay, xwindow, winx, winy, 1, 1, 0,
492 CopyFromParent, InputOnly, CopyFromParent,
494 XMapWindow (xdisplay, w);
495 XQueryPointer (xdisplay, xwindow,
497 &rootx, &rooty, &winx, &winy, &xmask);
498 XDestroyWindow (xdisplay, w);
501 pointer_window = xwindow;
507 g_list_free (toplevels);
508 if (pointer_window != None)
512 xwindow = pointer_window;
518 gdk_x11_display_error_trap_push (display);
519 XQueryPointer (xdisplay, xwindow,
524 if (gdk_x11_display_error_trap_pop (display))
527 if (get_toplevel && last != root &&
528 (window = gdk_x11_window_lookup_for_display (display, last)) != NULL &&
529 window->window_type != GDK_WINDOW_FOREIGN)
536 gdk_x11_display_ungrab (display);
538 window = gdk_x11_window_lookup_for_display (display, last);
541 *win_x = (window) ? xwin_x : -1;
544 *win_y = (window) ? xwin_y : -1;
553 gdk_device_core_select_window_events (GdkDevice *device,
555 GdkEventMask event_mask)
557 GdkEventMask filter_mask, window_mask;
561 window_mask = gdk_window_get_events (window);
562 filter_mask = (GDK_POINTER_MOTION_MASK &
563 GDK_POINTER_MOTION_HINT_MASK &
564 GDK_BUTTON_MOTION_MASK &
565 GDK_BUTTON1_MOTION_MASK &
566 GDK_BUTTON2_MOTION_MASK &
567 GDK_BUTTON3_MOTION_MASK &
568 GDK_BUTTON_PRESS_MASK &
569 GDK_BUTTON_RELEASE_MASK &
571 GDK_KEY_RELEASE_MASK &
572 GDK_ENTER_NOTIFY_MASK &
573 GDK_LEAVE_NOTIFY_MASK &
574 GDK_FOCUS_CHANGE_MASK &
575 GDK_PROXIMITY_IN_MASK &
576 GDK_PROXIMITY_OUT_MASK &
579 /* Filter out non-device events */
580 event_mask &= filter_mask;
582 /* Unset device events on window mask */
583 window_mask &= ~(filter_mask);
586 event_mask |= window_mask;
588 for (i = 0; i < _gdk_x11_event_mask_table_size; i++)
590 if (event_mask & (1 << (i + 1)))
591 xmask |= _gdk_x11_event_mask_table[i];
594 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
595 xmask |= StructureNotifyMask | PropertyChangeMask;
597 XSelectInput (GDK_WINDOW_XDISPLAY (window),
598 GDK_WINDOW_XID (window),