Debian Codegraph @

gdk/quartz/gdkdevice-core-quartz.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2009 Carlos Garnacho <[email protected]>
3 * Copyright (C) 2010 Kristian Rietveld <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "config.h"
20
21 #include <gdk/gdkdeviceprivate.h>
22 #include <gdk/gdkdisplayprivate.h>
23
24 #import "GdkQuartzView.h"
25 #include "gdkquartzwindow.h"
26 #include "gdkquartzcursor.h"
27 #include "gdkprivate-quartz.h"
28 #include "gdkquartzdevice-core.h"
29 #include "gdkinternal-quartz.h"
30 #include "gdkquartz-cocoa-access.h"
31
32 struct _GdkQuartzDeviceCore
33 {
34 GdkDevice parent_instance;
35
36 gboolean active;
37 NSUInteger device_id;
38 unsigned long long unique_id;
39 };
40
41 struct _GdkQuartzDeviceCoreClass
42 {
43 GdkDeviceClass parent_class;
44 };
45
46 static gboolean gdk_quartz_device_core_get_history (GdkDevice *device,
47 GdkWindow *window,
48 guint32 start,
49 guint32 stop,
50 GdkTimeCoord ***events,
51 gint *n_events);
52 static void gdk_quartz_device_core_get_state (GdkDevice *device,
53 GdkWindow *window,
54 gdouble *axes,
55 GdkModifierType *mask);
56 static void gdk_quartz_device_core_set_window_cursor (GdkDevice *device,
57 GdkWindow *window,
58 GdkCursor *cursor);
59 static void gdk_quartz_device_core_warp (GdkDevice *device,
60 GdkScreen *screen,
61 gdouble x,
62 gdouble y);
63 static void gdk_quartz_device_core_query_state (GdkDevice *device,
64 GdkWindow *window,
65 GdkWindow **root_window,
66 GdkWindow **child_window,
67 gdouble *root_x,
68 gdouble *root_y,
69 gdouble *win_x,
70 gdouble *win_y,
71 GdkModifierType *mask);
72 static GdkGrabStatus gdk_quartz_device_core_grab (GdkDevice *device,
73 GdkWindow *window,
74 gboolean owner_events,
75 GdkEventMask event_mask,
76 GdkWindow *confine_to,
77 GdkCursor *cursor,
78 guint32 time_);
79 static void gdk_quartz_device_core_ungrab (GdkDevice *device,
80 guint32 time_);
81 static GdkWindow * gdk_quartz_device_core_window_at_position (GdkDevice *device,
82 gdouble *win_x,
83 gdouble *win_y,
84 GdkModifierType *mask,
85 gboolean get_toplevel);
86 static void gdk_quartz_device_core_select_window_events (GdkDevice *device,
87 GdkWindow *window,
88 GdkEventMask event_mask);
89
90
91 G_DEFINE_TYPE (GdkQuartzDeviceCore, gdk_quartz_device_core, GDK_TYPE_DEVICE)
92
93 static void
94 gdk_quartz_device_core_class_init (GdkQuartzDeviceCoreClass *klass)
95 {
96 GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
97
98 device_class->get_history = gdk_quartz_device_core_get_history;
99 device_class->get_state = gdk_quartz_device_core_get_state;
100 device_class->set_window_cursor = gdk_quartz_device_core_set_window_cursor;
101 device_class->warp = gdk_quartz_device_core_warp;
102 device_class->query_state = gdk_quartz_device_core_query_state;
103 device_class->grab = gdk_quartz_device_core_grab;
104 device_class->ungrab = gdk_quartz_device_core_ungrab;
105 device_class->window_at_position = gdk_quartz_device_core_window_at_position;
106 device_class->select_window_events = gdk_quartz_device_core_select_window_events;
107 }
108
109 static void
110 gdk_quartz_device_core_init (GdkQuartzDeviceCore *quartz_device_core)
111 {
112 GdkDevice *device;
113
114 device = GDK_DEVICE (quartz_device_core);
115
116 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
117 _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
118 }
119
120 static gboolean
121 gdk_quartz_device_core_get_history (GdkDevice *device,
122 GdkWindow *window,
123 guint32 start,
124 guint32 stop,
125 GdkTimeCoord ***events,
126 gint *n_events)
127 {
128 return FALSE;
129 }
130
131 static void
132 gdk_quartz_device_core_get_state (GdkDevice *device,
133 GdkWindow *window,
134 gdouble *axes,
135 GdkModifierType *mask)
136 {
137 gint x_int, y_int;
138
139 gdk_window_get_device_position (window, device, &x_int, &y_int, mask);
140
141 if (axes)
142 {
143 axes[0] = x_int;
144 axes[1] = y_int;
145 }
146 }
147
148 static void
149 translate_coords_to_child_coords (GdkWindow *parent,
150 GdkWindow *child,
151 gint *x,
152 gint *y)
153 {
154 GdkWindow *current = child;
155
156 if (child == parent)
157 return;
158
159 while (current != parent)
160 {
161 gint tmp_x, tmp_y;
162
163 gdk_window_get_origin (current, &tmp_x, &tmp_y);
164
165 *x -= tmp_x;
166 *y -= tmp_y;
167
168 current = gdk_window_get_effective_parent (current);
169 }
170 }
171
172 static void
173 gdk_quartz_device_core_set_window_cursor (GdkDevice *device,
174 GdkWindow *window,
175 GdkCursor *cursor)
176 {
177 NSCursor *nscursor;
178
179 if (GDK_WINDOW_DESTROYED (window))
180 return;
181
182 nscursor = _gdk_quartz_cursor_get_ns_cursor (cursor);
183
184 [nscursor set];
185 }
186
187 static void
188 gdk_quartz_device_core_warp (GdkDevice *device,
189 GdkScreen *screen,
190 gdouble x,
191 gdouble y)
192 {
193 CGDisplayMoveCursorToPoint (CGMainDisplayID (), CGPointMake (x, y));
194 }
195
196 static GdkWindow *
197 gdk_quartz_device_core_query_state_helper (GdkWindow *window,
198 GdkDevice *device,
199 gdouble *x,
200 gdouble *y,
201 GdkModifierType *mask)
202 {
203 GdkWindow *toplevel;
204 NSPoint point;
205 gint x_tmp, y_tmp;
206 GdkWindow *found_window;
207
208 g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
209
210 if (GDK_WINDOW_DESTROYED (window))
211 {
212 *x = 0;
213 *y = 0;
214 *mask = 0;
215 return NULL;
216 }
217
218 toplevel = gdk_window_get_effective_toplevel (window);
219
220 if (mask)
221 *mask = _gdk_quartz_events_get_current_keyboard_modifiers () |
222 _gdk_quartz_events_get_current_mouse_modifiers ();
223
224 /* Get the y coordinate, needs to be flipped. */
225 if (window == _gdk_root)
226 {
227 point = [NSEvent mouseLocation];
228 _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
229 }
230 else
231 {
232 NSWindow *nswindow;
233
234 nswindow = gdk_quartz_window_get_nswindow (window);
235
236 point = [nswindow mouseLocationOutsideOfEventStream];
237
238 x_tmp = point.x;
239 y_tmp = toplevel->height - point.y;
240
241 window = toplevel;
242 }
243
244 found_window = _gdk_quartz_window_find_child (window, x_tmp, y_tmp,
245 FALSE);
246
247 if (found_window == _gdk_root)
248 found_window = NULL;
249 else if (found_window)
250 translate_coords_to_child_coords (window, found_window,
251 &x_tmp, &y_tmp);
252
253 if (x)
254 *x = x_tmp;
255
256 if (y)
257 *y = y_tmp;
258
259 return found_window;
260 }
261
262 static void
263 gdk_quartz_device_core_query_state (GdkDevice *device,
264 GdkWindow *window,
265 GdkWindow **root_window,
266 GdkWindow **child_window,
267 gdouble *root_x,
268 gdouble *root_y,
269 gdouble *win_x,
270 gdouble *win_y,
271 GdkModifierType *mask)
272 {
273 GdkWindow *found_window;
274 NSPoint point;
275 gint x_tmp, y_tmp;
276
277 found_window = gdk_quartz_device_core_query_state_helper (window, device,
278 win_x, win_y,
279 mask);
280
281 if (root_window)
282 *root_window = _gdk_root;
283
284 if (child_window)
285 *child_window = found_window;
286
287 point = [NSEvent mouseLocation];
288 _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
289
290 if (root_x)
291 *root_x = x_tmp;
292
293 if (root_y)
294 *root_y = y_tmp;
295 }
296
297 static GdkGrabStatus
298 gdk_quartz_device_core_grab (GdkDevice *device,
299 GdkWindow *window,
300 gboolean owner_events,
301 GdkEventMask event_mask,
302 GdkWindow *confine_to,
303 GdkCursor *cursor,
304 guint32 time_)
305 {
306 /* Should remain empty */
307 return GDK_GRAB_SUCCESS;
308 }
309
310 static void
311 gdk_quartz_device_core_ungrab (GdkDevice *device,
312 guint32 time_)
313 {
314 GdkDeviceGrabInfo *grab;
315
316 grab = _gdk_display_get_last_device_grab (_gdk_display, device);
317 if (grab)
318 grab->serial_end = 0;
319
320 _gdk_display_device_grab_update (_gdk_display, device, NULL, 0);
321 }
322
323 static GdkWindow *
324 gdk_quartz_device_core_window_at_position (GdkDevice *device,
325 gdouble *win_x,
326 gdouble *win_y,
327 GdkModifierType *mask,
328 gboolean get_toplevel)
329 {
330 GdkDisplay *display;
331 GdkScreen *screen;
332 GdkWindow *found_window;
333 NSPoint point;
334 gint x_tmp, y_tmp;
335
336 display = gdk_device_get_display (device);
337 screen = gdk_display_get_default_screen (display);
338
339 /* Get mouse coordinates, find window under the mouse pointer */
340 point = [NSEvent mouseLocation];
341 _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
342
343 found_window = _gdk_quartz_window_find_child (_gdk_root, x_tmp, y_tmp,
344 get_toplevel);
345
346 if (found_window)
347 translate_coords_to_child_coords (_gdk_root, found_window,
348 &x_tmp, &y_tmp);
349
350 if (win_x)
351 *win_x = found_window ? x_tmp : -1;
352
353 if (win_y)
354 *win_y = found_window ? y_tmp : -1;
355
356 if (mask)
357 *mask = _gdk_quartz_events_get_current_keyboard_modifiers () |
358 _gdk_quartz_events_get_current_mouse_modifiers ();
359
360 return found_window;
361 }
362
363 static void
364 gdk_quartz_device_core_select_window_events (GdkDevice *device,
365 GdkWindow *window,
366 GdkEventMask event_mask)
367 {
368 /* The mask is set in the common code. */
369 }
370
371 void
372 _gdk_quartz_device_core_set_active (GdkDevice *device,
373 gboolean active,
374 NSUInteger device_id)
375 {
376 GdkQuartzDeviceCore *self = GDK_QUARTZ_DEVICE_CORE (device);
377
378 self->active = active;
379 self->device_id = device_id;
380 }
381
382 gboolean
383 _gdk_quartz_device_core_is_active (GdkDevice *device,
384 NSUInteger device_id)
385 {
386 GdkQuartzDeviceCore *self = GDK_QUARTZ_DEVICE_CORE (device);
387
388 return (self->active && self->device_id == device_id);
389 }
390
391 void
392 _gdk_quartz_device_core_set_unique (GdkDevice *device,
393 unsigned long long unique_id)
394 {
395 GDK_QUARTZ_DEVICE_CORE (device)->unique_id = unique_id;
396 }
397
398 unsigned long long
399 _gdk_quartz_device_core_get_unique (GdkDevice *device)
400 {
401 return GDK_QUARTZ_DEVICE_CORE (device)->unique_id;
402 }