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,
203 GdkCursorPrivate *cursor_private;
206 cursor_private = (GdkCursorPrivate*) cursor;
211 xcursor = cursor_private->xcursor;
213 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
214 GDK_WINDOW_XID (window),
219 gdk_device_core_warp (GdkDevice *device,
227 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
228 dest = GDK_WINDOW_XID (gdk_screen_get_root_window (screen));
230 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
234 gdk_device_core_query_state (GdkDevice *device,
236 GdkWindow **root_window,
237 GdkWindow **child_window,
242 GdkModifierType *mask)
245 Window xroot_window, xchild_window;
246 int xroot_x, xroot_y, xwin_x, xwin_y;
249 display = gdk_window_get_display (window);
251 if (!XQueryPointer (GDK_WINDOW_XDISPLAY (window),
252 GDK_WINDOW_XID (window),
265 *root_window = gdk_window_lookup_for_display (display, xroot_window);
268 *child_window = gdk_window_lookup_for_display (display, xchild_window);
289 gdk_device_core_grab (GdkDevice *device,
291 gboolean owner_events,
292 GdkEventMask event_mask,
293 GdkWindow *confine_to,
298 Window xwindow, xconfine_to;
301 display = gdk_device_get_display (device);
303 xwindow = GDK_WINDOW_XID (window);
306 confine_to = _gdk_window_get_impl_window (confine_to);
308 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
311 xconfine_to = GDK_WINDOW_XID (confine_to);
313 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
315 /* Device is a keyboard */
316 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
319 GrabModeAsync, GrabModeAsync,
328 /* Device is a pointer */
333 _gdk_x11_cursor_update_theme (cursor);
334 xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
339 for (i = 0; i < _gdk_nenvent_masks; i++)
341 if (event_mask & (1 << (i + 1)))
342 xevent_mask |= _gdk_event_mask_table[i];
345 /* We don't want to set a native motion hint mask, as we're emulating motion
346 * hints. If we set a native one we just wouldn't get any events.
348 xevent_mask &= ~PointerMotionHintMask;
350 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
354 GrabModeAsync, GrabModeAsync,
360 return _gdk_x11_convert_grab_status (status);
364 gdk_device_core_ungrab (GdkDevice *device,
369 display = gdk_device_get_display (device);
371 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
372 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
374 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
378 gdk_device_core_window_at_position (GdkDevice *device,
381 GdkModifierType *mask,
382 gboolean get_toplevel)
388 Window xwindow, root, child, last;
389 int xroot_x, xroot_y, xwin_x, xwin_y;
393 display = gdk_device_get_display (device);
394 screen = gdk_display_get_default_screen (display);
396 /* This function really only works if the mouse pointer is held still
397 * during its operation. If it moves from one leaf window to another
398 * than we'll end up with inaccurate values for win_x, win_y
401 gdk_x11_display_grab (display);
403 xdisplay = GDK_SCREEN_XDISPLAY (screen);
404 xwindow = GDK_SCREEN_XROOTWIN (screen);
406 XQueryPointer (xdisplay, xwindow,
420 XQueryPointer (xdisplay, xwindow,
426 if (get_toplevel && last != root &&
427 (window = gdk_window_lookup_for_display (display, last)) != NULL &&
428 window->window_type != GDK_WINDOW_FOREIGN)
435 gdk_x11_display_ungrab (display);
437 window = gdk_window_lookup_for_display (display, last);
440 *win_x = (window) ? xwin_x : -1;
443 *win_y = (window) ? xwin_y : -1;
452 gdk_device_core_select_window_events (GdkDevice *device,
454 GdkEventMask event_mask)
456 GdkEventMask filter_mask, window_mask;
460 window_mask = gdk_window_get_events (window);
461 filter_mask = (GDK_POINTER_MOTION_MASK &
462 GDK_POINTER_MOTION_HINT_MASK &
463 GDK_BUTTON_MOTION_MASK &
464 GDK_BUTTON1_MOTION_MASK &
465 GDK_BUTTON2_MOTION_MASK &
466 GDK_BUTTON3_MOTION_MASK &
467 GDK_BUTTON_PRESS_MASK &
468 GDK_BUTTON_RELEASE_MASK &
470 GDK_KEY_RELEASE_MASK &
471 GDK_ENTER_NOTIFY_MASK &
472 GDK_LEAVE_NOTIFY_MASK &
473 GDK_FOCUS_CHANGE_MASK &
474 GDK_PROXIMITY_IN_MASK &
475 GDK_PROXIMITY_OUT_MASK &
478 /* Filter out non-device events */
479 event_mask &= filter_mask;
481 /* Unset device events on window mask */
482 window_mask &= ~(filter_mask);
485 event_mask |= window_mask;
487 for (i = 0; i < _gdk_nenvent_masks; i++)
489 if (event_mask & (1 << (i + 1)))
490 xmask |= _gdk_event_mask_table[i];
493 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
494 xmask |= StructureNotifyMask | PropertyChangeMask;
496 XSelectInput (GDK_WINDOW_XDISPLAY (window),
497 GDK_WINDOW_XID (window),