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-broadway.h"
24 #include "gdkwindow.h"
25 #include "gdkprivate-broadway.h"
28 static gboolean gdk_device_core_get_history (GdkDevice *device,
32 GdkTimeCoord ***events,
34 static void gdk_device_core_get_state (GdkDevice *device,
37 GdkModifierType *mask);
38 static void gdk_device_core_set_window_cursor (GdkDevice *device,
41 static void gdk_device_core_warp (GdkDevice *device,
45 static gboolean gdk_device_core_query_state (GdkDevice *device,
47 GdkWindow **root_window,
48 GdkWindow **child_window,
53 GdkModifierType *mask);
54 static GdkGrabStatus gdk_device_core_grab (GdkDevice *device,
56 gboolean owner_events,
57 GdkEventMask event_mask,
58 GdkWindow *confine_to,
61 static void gdk_device_core_ungrab (GdkDevice *device,
63 static GdkWindow * gdk_device_core_window_at_position (GdkDevice *device,
66 GdkModifierType *mask,
67 gboolean get_toplevel);
68 static void gdk_device_core_select_window_events (GdkDevice *device,
70 GdkEventMask event_mask);
73 G_DEFINE_TYPE (GdkDeviceCore, gdk_device_core, GDK_TYPE_DEVICE)
76 gdk_device_core_class_init (GdkDeviceCoreClass *klass)
78 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
80 device_class->get_history = gdk_device_core_get_history;
81 device_class->get_state = gdk_device_core_get_state;
82 device_class->set_window_cursor = gdk_device_core_set_window_cursor;
83 device_class->warp = gdk_device_core_warp;
84 device_class->query_state = gdk_device_core_query_state;
85 device_class->grab = gdk_device_core_grab;
86 device_class->ungrab = gdk_device_core_ungrab;
87 device_class->window_at_position = gdk_device_core_window_at_position;
88 device_class->select_window_events = gdk_device_core_select_window_events;
92 gdk_device_core_init (GdkDeviceCore *device_core)
96 device = GDK_DEVICE (device_core);
98 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
99 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
103 impl_coord_in_window (GdkWindow *window,
107 GdkWindowObject *priv = (GdkWindowObject *) window;
109 if (impl_x < priv->abs_x ||
110 impl_x > priv->abs_x + priv->width)
113 if (impl_y < priv->abs_y ||
114 impl_y > priv->abs_y + priv->height)
121 gdk_device_core_get_history (GdkDevice *device,
125 GdkTimeCoord ***events,
128 GdkWindowObject *priv;
130 GdkTimeCoord **coords;
131 GdkWindow *impl_window;
135 impl_window = _gdk_window_get_impl_window (window);
136 xcoords = XGetMotionEvents (GDK_DRAWABLE_XDISPLAY (window),
137 GDK_DRAWABLE_XID (impl_window),
138 start, stop, &tmp_n_events);
142 priv = (GdkWindowObject *) window;
143 coords = _gdk_device_allocate_history (device, tmp_n_events);
145 for (i = 0, j = 0; i < tmp_n_events; i++)
147 if (impl_coord_in_window (window, xcoords[i].x, xcoords[i].y))
149 coords[j]->time = xcoords[i].time;
150 coords[j]->axes[0] = xcoords[i].x - priv->abs_x;
151 coords[j]->axes[1] = xcoords[i].y - priv->abs_y;
158 /* free the events we allocated too much */
159 for (i = j; i < tmp_n_events; i++)
167 if (tmp_n_events == 0)
169 gdk_device_free_history (coords, tmp_n_events);
174 *n_events = tmp_n_events;
179 gdk_device_free_history (coords, tmp_n_events);
185 gdk_device_core_get_state (GdkDevice *device,
188 GdkModifierType *mask)
192 gdk_window_get_pointer (window, &x_int, &y_int, mask);
202 gdk_device_core_set_window_cursor (GdkDevice *device,
206 GdkCursorPrivate *cursor_private;
209 cursor_private = (GdkCursorPrivate*) cursor;
214 xcursor = cursor_private->xcursor;
216 XDefineCursor (GDK_WINDOW_XDISPLAY (window),
217 GDK_WINDOW_XID (window),
222 gdk_device_core_warp (GdkDevice *device,
230 xdisplay = GDK_DISPLAY_XDISPLAY (gdk_device_get_display (device));
231 dest = GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen));
233 XWarpPointer (xdisplay, None, dest, 0, 0, 0, 0, x, y);
237 gdk_device_core_query_state (GdkDevice *device,
239 GdkWindow **root_window,
240 GdkWindow **child_window,
245 GdkModifierType *mask)
248 Window xroot_window, xchild_window;
249 int xroot_x, xroot_y, xwin_x, xwin_y;
252 display = gdk_window_get_display (window);
254 if (!XQueryPointer (GDK_WINDOW_XDISPLAY (window),
255 GDK_WINDOW_XID (window),
268 *root_window = gdk_window_lookup_for_display (display, xroot_window);
271 *child_window = gdk_window_lookup_for_display (display, xchild_window);
292 gdk_device_core_grab (GdkDevice *device,
294 gboolean owner_events,
295 GdkEventMask event_mask,
296 GdkWindow *confine_to,
301 Window xwindow, xconfine_to;
304 display = gdk_device_get_display (device);
306 xwindow = GDK_WINDOW_XID (window);
309 confine_to = _gdk_window_get_impl_window (confine_to);
311 if (!confine_to || GDK_WINDOW_DESTROYED (confine_to))
314 xconfine_to = GDK_WINDOW_XID (confine_to);
316 if (device->source == GDK_SOURCE_KEYBOARD)
318 /* Device is a keyboard */
319 status = XGrabKeyboard (GDK_DISPLAY_XDISPLAY (display),
322 GrabModeAsync, GrabModeAsync,
331 /* Device is a pointer */
336 _gdk_x11_cursor_update_theme (cursor);
337 xcursor = ((GdkCursorPrivate *) cursor)->xcursor;
342 for (i = 0; i < _gdk_nenvent_masks; i++)
344 if (event_mask & (1 << (i + 1)))
345 xevent_mask |= _gdk_event_mask_table[i];
348 /* We don't want to set a native motion hint mask, as we're emulating motion
349 * hints. If we set a native one we just wouldn't get any events.
351 xevent_mask &= ~PointerMotionHintMask;
353 status = XGrabPointer (GDK_DISPLAY_XDISPLAY (display),
357 GrabModeAsync, GrabModeAsync,
363 return _gdk_x11_convert_grab_status (status);
367 gdk_device_core_ungrab (GdkDevice *device,
372 display = gdk_device_get_display (device);
374 if (device->source == GDK_SOURCE_KEYBOARD)
375 XUngrabKeyboard (GDK_DISPLAY_XDISPLAY (display), time_);
377 XUngrabPointer (GDK_DISPLAY_XDISPLAY (display), time_);
381 gdk_device_core_window_at_position (GdkDevice *device,
384 GdkModifierType *mask,
385 gboolean get_toplevel)
391 Window xwindow, root, child, last;
392 int xroot_x, xroot_y, xwin_x, xwin_y;
396 display = gdk_device_get_display (device);
397 screen = gdk_display_get_default_screen (display);
399 /* This function really only works if the mouse pointer is held still
400 * during its operation. If it moves from one leaf window to another
401 * than we'll end up with inaccurate values for win_x, win_y
404 gdk_x11_display_grab (display);
406 xdisplay = GDK_SCREEN_XDISPLAY (screen);
407 xwindow = GDK_SCREEN_XROOTWIN (screen);
409 XQueryPointer (xdisplay, xwindow,
423 XQueryPointer (xdisplay, xwindow,
429 if (get_toplevel && last != root &&
430 (window = gdk_window_lookup_for_display (display, last)) != NULL &&
431 GDK_WINDOW_TYPE (window) != GDK_WINDOW_FOREIGN)
438 gdk_x11_display_ungrab (display);
440 window = gdk_window_lookup_for_display (display, last);
443 *win_x = (window) ? xwin_x : -1;
446 *win_y = (window) ? xwin_y : -1;
455 gdk_device_core_select_window_events (GdkDevice *device,
457 GdkEventMask event_mask)
459 GdkEventMask filter_mask, window_mask;
463 window_mask = gdk_window_get_events (window);
464 filter_mask = (GDK_POINTER_MOTION_MASK &
465 GDK_POINTER_MOTION_HINT_MASK &
466 GDK_BUTTON_MOTION_MASK &
467 GDK_BUTTON1_MOTION_MASK &
468 GDK_BUTTON2_MOTION_MASK &
469 GDK_BUTTON3_MOTION_MASK &
470 GDK_BUTTON_PRESS_MASK &
471 GDK_BUTTON_RELEASE_MASK &
473 GDK_KEY_RELEASE_MASK &
474 GDK_ENTER_NOTIFY_MASK &
475 GDK_LEAVE_NOTIFY_MASK &
476 GDK_FOCUS_CHANGE_MASK &
477 GDK_PROXIMITY_IN_MASK &
478 GDK_PROXIMITY_OUT_MASK &
481 /* Filter out non-device events */
482 event_mask &= filter_mask;
484 /* Unset device events on window mask */
485 window_mask &= ~(filter_mask);
488 event_mask |= window_mask;
490 for (i = 0; i < _gdk_nenvent_masks; i++)
492 if (event_mask & (1 << (i + 1)))
493 xmask |= _gdk_event_mask_table[i];
496 if (GDK_WINDOW_XID (window) != GDK_WINDOW_XROOTWIN (window))
497 xmask |= StructureNotifyMask | PropertyChangeMask;
499 XSelectInput (GDK_WINDOW_XDISPLAY (window),
500 GDK_WINDOW_XWINDOW (window),