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 <gdk/gdkwindow.h>
23 #include "gdkdevice-core.h"
24 #include "gdkprivate-x11.h"
27 static gboolean gdk_device_core_get_history (GdkDevice *device,
31 GdkTimeCoord ***events,
33 static void gdk_device_core_get_state (GdkDevice *device,
36 GdkModifierType *mask);
37 static void gdk_device_core_set_window_cursor (GdkDevice *device,
40 static void gdk_device_core_warp (GdkDevice *device,
44 static gboolean gdk_device_core_query_state (GdkDevice *device,
46 GdkWindow **root_window,
47 GdkWindow **child_window,
52 GdkModifierType *mask);
53 static GdkGrabStatus gdk_device_core_grab (GdkDevice *device,
55 gboolean owner_events,
56 GdkEventMask event_mask,
57 GdkWindow *confine_to,
60 static void gdk_device_core_ungrab (GdkDevice *device,
62 static GdkWindow * gdk_device_core_window_at_position (GdkDevice *device,
65 GdkModifierType *mask,
66 gboolean get_toplevel);
67 static void gdk_device_core_select_window_events (GdkDevice *device,
69 GdkEventMask event_mask);
72 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
75 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
77 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
79 device_class->get_history = gdk_device_core_get_history;
80 device_class->get_state = gdk_device_core_get_state;
81 device_class->set_window_cursor = gdk_device_core_set_window_cursor;
82 device_class->warp = gdk_device_core_warp;
83 device_class->query_state = gdk_device_core_query_state;
84 device_class->grab = gdk_device_core_grab;
85 device_class->ungrab = gdk_device_core_ungrab;
86 device_class->window_at_position = gdk_device_core_window_at_position;
87 device_class->select_window_events = gdk_device_core_select_window_events;
91 gdk_device_core_init (GdkDeviceCore *device_core)
95 device = GDK_DEVICE (device_core);
97 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
98 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
102 impl_coord_in_window (GdkWindow *window,
106 GdkWindowObject *priv = (GdkWindowObject *) window;
108 if (impl_x < priv->abs_x ||
109 impl_x > priv->abs_x + priv->width)
112 if (impl_y < priv->abs_y ||
113 impl_y > priv->abs_y + priv->height)
120 gdk_device_core_get_history (GdkDevice *device,
124 GdkTimeCoord ***events,
127 GdkWindowObject *priv;
129 GdkTimeCoord **coords;
130 GdkWindow *impl_window;
134 impl_window = _gdk_window_get_impl_window (window);
135 xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
136 GDK_DRAWABLE_XID (impl_window),
137 start, stop, &tmp_n_events);
141 priv = (GdkWindowObject *) window;
142 coords = _gdk_device_allocate_history (device, tmp_n_events);
144 for (i = 0, j = 0; i < tmp_n_events; i++)
146 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
148 coords[j]->time = xcoords[i].time;
149 coords[j]->axes[0] = xcoords[i].x - priv->abs_x;
150 coords[j]->axes[1] = xcoords[i].y - priv->abs_y;
157 /* free the events we allocated too much */
158 for (i = j; i < tmp_n_events; i++)
166 if (tmp_n_events == 0)
168 gdk_device_free_history (coords, tmp_n_events);
173 *n_events = tmp_n_events;
178 gdk_device_free_history (coords, tmp_n_events);
184 gdk_device_core_get_state (GdkDevice *device,
187 GdkModifierType *mask)
191 gdk_window_get_pointer (window, &x_int, &y_int, mask);
201 gdk_device_core_set_window_cursor (GdkDevice *device,
205 GdkCursorPrivate *cursor_private;
208 cursor_private = (GdkCursorPrivate*) cursor;
213 xcursor = cursor_private->xcursor;
215 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
216 GDK_WINDOW_XID (window),
221 gdk_device_core_warp (GdkDevice *device,
229 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
230 dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
232 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
236 gdk_device_core_query_state (GdkDevice *device,
238 GdkWindow **root_window,
239 GdkWindow **child_window,
244 GdkModifierType *mask)
247 Window xroot_window, xchild_window;
248 int xroot_x, xroot_y, xwin_x, xwin_y;
251 display = gdk_window_get_display (window);
253 if (!XQueryPointer (GDK_WINDOW_XDISPLAY (window),
254 GDK_WINDOW_XID (window),
267 *root_window = gdk_window_lookup_for_display (display, xroot_window);
270 *child_window = gdk_window_lookup_for_display (display, xchild_window);
291 gdk_device_core_grab (GdkDevice *device,
293 gboolean owner_events,
294 GdkEventMask event_mask,
295 GdkWindow *confine_to,
300 Window xwindow, xconfine_to;
303 display = gdk_device_get_display (device);
305 xwindow = GDK_WINDOW_XID (window);
308 confine_to = _gdk_window_get_impl_window (confine_to);
310 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
313 xconfine_to = GDK_WINDOW_XID (confine_to);
315 if (device->source == GDK_SOURCE_KEYBOARD)
317 /* Device is a keyboard */
318 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
321 GrabModeAsync, GrabModeAsync,
330 /* Device is a pointer */
335 _gdk_x11_cursor_update_theme (cursor);
336 xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
341 for (i = 0; i < _gdk_nenvent_masks; i++)
343 if (event_mask & (1 << (i + 1)))
344 xevent_mask |= _gdk_event_mask_table[i];
347 /* We don't want to set a native motion hint mask, as we're emulating motion
348 * hints. If we set a native one we just wouldn't get any events.
350 xevent_mask &= ~PointerMotionHintMask;
352 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
356 GrabModeAsync, GrabModeAsync,
362 return _gdk_x11_convert_grab_status (status);
366 gdk_device_core_ungrab (GdkDevice *device,
371 display = gdk_device_get_display (device);
373 if (device->source == GDK_SOURCE_KEYBOARD)
374 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
376 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
380 gdk_device_core_window_at_position (GdkDevice *device,
383 GdkModifierType *mask,
384 gboolean get_toplevel)
390 Window xwindow, root, child, last;
391 int xroot_x, xroot_y, xwin_x, xwin_y;
395 display = gdk_device_get_display (device);
396 screen = gdk_display_get_default_screen (display);
398 /* This function really only works if the mouse pointer is held still
399 * during its operation. If it moves from one leaf window to another
400 * than we'll end up with inaccurate values for win_x, win_y
403 gdk_x11_display_grab (display);
405 xdisplay = GDK_SCREEN_XDISPLAY (screen);
406 xwindow = GDK_SCREEN_XROOTWIN (screen);
408 XQueryPointer (xdisplay, xwindow,
422 XQueryPointer (xdisplay, xwindow,
428 if (get_toplevel && last != root &&
429 (window = gdk_window_lookup_for_display (display, last)) != NULL &&
430 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
437 gdk_x11_display_ungrab (display);
439 window = gdk_window_lookup_for_display (display, last);
442 *win_x = (window) ? xwin_x : -1;
445 *win_y = (window) ? xwin_y : -1;
454 gdk_device_core_select_window_events (GdkDevice *device,
456 GdkEventMask event_mask)
458 GdkEventMask filter_mask, window_mask;
462 window_mask = gdk_window_get_events (window);
463 filter_mask = (GDK_POINTER_MOTION_MASK &
464 GDK_POINTER_MOTION_HINT_MASK &
465 GDK_BUTTON_MOTION_MASK &
466 GDK_BUTTON1_MOTION_MASK &
467 GDK_BUTTON2_MOTION_MASK &
468 GDK_BUTTON3_MOTION_MASK &
469 GDK_BUTTON_PRESS_MASK &
470 GDK_BUTTON_RELEASE_MASK &
472 GDK_KEY_RELEASE_MASK &
473 GDK_ENTER_NOTIFY_MASK &
474 GDK_LEAVE_NOTIFY_MASK &
475 GDK_FOCUS_CHANGE_MASK &
476 GDK_PROXIMITY_IN_MASK &
477 GDK_PROXIMITY_OUT_MASK &
480 /* Filter out non-device events */
481 event_mask &= filter_mask;
483 /* Unset device events on window mask */
484 window_mask &= ~(filter_mask);
487 event_mask |= window_mask;
489 for (i = 0; i < _gdk_nenvent_masks; i++)
491 if (event_mask & (1 << (i + 1)))
492 xmask |= _gdk_event_mask_table[i];
495 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
496 xmask |= StructureNotifyMask | PropertyChangeMask;
498 XSelectInput (GDK_WINDOW_XDISPLAY (window),
499 GDK_WINDOW_XWINDOW (window),