]> Pileus Git - ~andy/gtk/blob - gdk/quartz/gdkdevice-core-quartz.c
Merge branch 'master' into broadway2
[~andy/gtk] / gdk / quartz / gdkdevice-core-quartz.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  * Copyright (C) 2010 Kristian Rietveld <kris@gtk.org>
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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <gdk/gdkdeviceprivate.h>
24 #include <gdk/gdkdisplayprivate.h>
25
26 #import "GdkQuartzView.h"
27 #include "gdkquartzwindow.h"
28 #include "gdkquartzcursor.h"
29 #include "gdkprivate-quartz.h"
30 #include "gdkquartzdevice-core.h"
31
32 struct _GdkQuartzDeviceCore
33 {
34   GdkDevice parent_instance;
35 };
36
37 struct _GdkQuartzDeviceCoreClass
38 {
39   GdkDeviceClass parent_class;
40 };
41
42 static gboolean gdk_quartz_device_core_get_history (GdkDevice      *device,
43                                                     GdkWindow      *window,
44                                                     guint32         start,
45                                                     guint32         stop,
46                                                     GdkTimeCoord ***events,
47                                                     gint           *n_events);
48 static void gdk_quartz_device_core_get_state (GdkDevice       *device,
49                                               GdkWindow       *window,
50                                               gdouble         *axes,
51                                               GdkModifierType *mask);
52 static void gdk_quartz_device_core_set_window_cursor (GdkDevice *device,
53                                                       GdkWindow *window,
54                                                       GdkCursor *cursor);
55 static void gdk_quartz_device_core_warp (GdkDevice *device,
56                                          GdkScreen *screen,
57                                          gint       x,
58                                          gint       y);
59 static gboolean gdk_quartz_device_core_query_state (GdkDevice        *device,
60                                                     GdkWindow        *window,
61                                                     GdkWindow       **root_window,
62                                                     GdkWindow       **child_window,
63                                                     gint             *root_x,
64                                                     gint             *root_y,
65                                                     gint             *win_x,
66                                                     gint             *win_y,
67                                                     GdkModifierType  *mask);
68 static GdkGrabStatus gdk_quartz_device_core_grab   (GdkDevice     *device,
69                                                     GdkWindow     *window,
70                                                     gboolean       owner_events,
71                                                     GdkEventMask   event_mask,
72                                                     GdkWindow     *confine_to,
73                                                     GdkCursor     *cursor,
74                                                     guint32        time_);
75 static void          gdk_quartz_device_core_ungrab (GdkDevice     *device,
76                                                     guint32        time_);
77 static GdkWindow * gdk_quartz_device_core_window_at_position (GdkDevice       *device,
78                                                               gint            *win_x,
79                                                               gint            *win_y,
80                                                               GdkModifierType *mask,
81                                                               gboolean         get_toplevel);
82 static void      gdk_quartz_device_core_select_window_events (GdkDevice       *device,
83                                                               GdkWindow       *window,
84                                                               GdkEventMask     event_mask);
85
86
87 G_DEFINE_TYPE (GdkQuartzDeviceCore, gdk_quartz_device_core, GDK_TYPE_DEVICE)
88
89 static void
90 gdk_quartz_device_core_class_init (GdkQuartzDeviceCoreClass *klass)
91 {
92   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
93
94   device_class->get_history = gdk_quartz_device_core_get_history;
95   device_class->get_state = gdk_quartz_device_core_get_state;
96   device_class->set_window_cursor = gdk_quartz_device_core_set_window_cursor;
97   device_class->warp = gdk_quartz_device_core_warp;
98   device_class->query_state = gdk_quartz_device_core_query_state;
99   device_class->grab = gdk_quartz_device_core_grab;
100   device_class->ungrab = gdk_quartz_device_core_ungrab;
101   device_class->window_at_position = gdk_quartz_device_core_window_at_position;
102   device_class->select_window_events = gdk_quartz_device_core_select_window_events;
103 }
104
105 static void
106 gdk_quartz_device_core_init (GdkQuartzDeviceCore *quartz_device_core)
107 {
108   GdkDevice *device;
109
110   device = GDK_DEVICE (quartz_device_core);
111
112   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_X, 0, 0, 1);
113   _gdk_device_add_axis (device, GDK_NONE, GDK_AXIS_Y, 0, 0, 1);
114 }
115
116 static gboolean
117 gdk_quartz_device_core_get_history (GdkDevice      *device,
118                                     GdkWindow      *window,
119                                     guint32         start,
120                                     guint32         stop,
121                                     GdkTimeCoord ***events,
122                                     gint           *n_events)
123 {
124   return FALSE;
125 }
126
127 static void
128 gdk_quartz_device_core_get_state (GdkDevice       *device,
129                                   GdkWindow       *window,
130                                   gdouble         *axes,
131                                   GdkModifierType *mask)
132 {
133   gint x_int, y_int;
134
135   gdk_window_get_pointer (window, &x_int, &y_int, mask);
136
137   if (axes)
138     {
139       axes[0] = x_int;
140       axes[1] = y_int;
141     }
142 }
143
144 static void
145 translate_coords_to_child_coords (GdkWindow *parent,
146                                   GdkWindow *child,
147                                   gint      *x,
148                                   gint      *y)
149 {
150   GdkWindow *current = child;
151
152   if (child == parent)
153     return;
154
155   while (current != parent)
156     {
157       gint tmp_x, tmp_y;
158
159       gdk_window_get_origin (current, &tmp_x, &tmp_y);
160
161       *x -= tmp_x;
162       *y -= tmp_y;
163
164       current = gdk_window_get_effective_parent (current);
165     }
166 }
167
168 static void
169 gdk_quartz_device_core_set_window_cursor (GdkDevice *device,
170                                           GdkWindow *window,
171                                           GdkCursor *cursor)
172 {
173   NSCursor *nscursor;
174
175   if (GDK_WINDOW_DESTROYED (window))
176     return;
177
178   nscursor = _gdk_quartz_cursor_get_ns_cursor (cursor);
179
180   [nscursor set];
181 }
182
183 static void
184 gdk_quartz_device_core_warp (GdkDevice *device,
185                              GdkScreen *screen,
186                              gint       x,
187                              gint       y)
188 {
189   CGDisplayMoveCursorToPoint (CGMainDisplayID (), CGPointMake (x, y));
190 }
191
192 static GdkWindow *
193 gdk_quartz_device_core_query_state_helper (GdkWindow       *window,
194                                            GdkDevice       *device,
195                                            gint            *x,
196                                            gint            *y,
197                                            GdkModifierType *mask)
198 {
199   GdkWindow *toplevel;
200   NSPoint point;
201   gint x_tmp, y_tmp;
202   GdkWindow *found_window;
203
204   g_return_val_if_fail (window == NULL || GDK_IS_WINDOW (window), NULL);
205
206   if (GDK_WINDOW_DESTROYED (window))
207     {
208       *x = 0;
209       *y = 0;
210       *mask = 0;
211       return NULL;
212     }
213
214   toplevel = gdk_window_get_effective_toplevel (window);
215
216   *mask = _gdk_quartz_events_get_current_event_mask ();
217
218   /* Get the y coordinate, needs to be flipped. */
219   if (window == _gdk_root)
220     {
221       point = [NSEvent mouseLocation];
222       _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
223     }
224   else
225     {
226       GdkWindowImplQuartz *impl;
227       NSWindow *nswindow;
228
229       impl = GDK_WINDOW_IMPL_QUARTZ (toplevel->impl);
230       nswindow = impl->toplevel;
231
232       point = [nswindow mouseLocationOutsideOfEventStream];
233
234       x_tmp = point.x;
235       y_tmp = toplevel->height - point.y;
236
237       window = toplevel;
238     }
239
240   found_window = _gdk_quartz_window_find_child (window, x_tmp, y_tmp,
241                                                 FALSE);
242
243   if (found_window == _gdk_root)
244     found_window = NULL;
245   else if (found_window)
246     translate_coords_to_child_coords (window, found_window,
247                                       &x_tmp, &y_tmp);
248
249   if (x)
250     *x = x_tmp;
251
252   if (y)
253     *y = y_tmp;
254
255   return found_window;
256 }
257
258 static gboolean
259 gdk_quartz_device_core_query_state (GdkDevice        *device,
260                                     GdkWindow        *window,
261                                     GdkWindow       **root_window,
262                                     GdkWindow       **child_window,
263                                     gint             *root_x,
264                                     gint             *root_y,
265                                     gint             *win_x,
266                                     gint             *win_y,
267                                     GdkModifierType  *mask)
268 {
269   GdkDisplay *display;
270   GdkWindow *found_window;
271   NSPoint point;
272   gint x_tmp, y_tmp;
273
274   found_window = gdk_quartz_device_core_query_state_helper (window, device,
275                                                             win_x, win_y,
276                                                             mask);
277   if (!found_window)
278     return FALSE;
279
280   display = gdk_window_get_display (window);
281
282   if (root_window)
283     *root_window = _gdk_root;
284
285   if (child_window)
286     *child_window = found_window;
287
288   point = [NSEvent mouseLocation];
289   _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
290
291   if (root_x)
292     *root_x = x_tmp;
293
294   if (root_y)
295     *root_y = y_tmp;
296
297   return TRUE;
298 }
299
300 static GdkGrabStatus
301 gdk_quartz_device_core_grab (GdkDevice    *device,
302                              GdkWindow    *window,
303                              gboolean      owner_events,
304                              GdkEventMask  event_mask,
305                              GdkWindow    *confine_to,
306                              GdkCursor    *cursor,
307                              guint32       time_)
308 {
309   /* Should remain empty */
310   return GDK_GRAB_SUCCESS;
311 }
312
313 static void
314 gdk_quartz_device_core_ungrab (GdkDevice *device,
315                                guint32    time_)
316 {
317   GdkDeviceGrabInfo *grab;
318
319   grab = _gdk_display_get_last_device_grab (_gdk_display, device);
320   if (grab)
321     grab->serial_end = 0;
322
323   _gdk_display_device_grab_update (_gdk_display, device, NULL, 0);
324 }
325
326 static GdkWindow *
327 gdk_quartz_device_core_window_at_position (GdkDevice       *device,
328                                            gint            *win_x,
329                                            gint            *win_y,
330                                            GdkModifierType *mask,
331                                            gboolean         get_toplevel)
332 {
333   GdkDisplay *display;
334   GdkScreen *screen;
335   GdkWindow *found_window;
336   NSPoint point;
337   gint x_tmp, y_tmp;
338
339   display = gdk_device_get_display (device);
340   screen = gdk_display_get_default_screen (display);
341
342   /* Get mouse coordinates, find window under the mouse pointer */
343   point = [NSEvent mouseLocation];
344   _gdk_quartz_window_nspoint_to_gdk_xy (point, &x_tmp, &y_tmp);
345
346   found_window = _gdk_quartz_window_find_child (_gdk_root, x_tmp, y_tmp,
347                                                 get_toplevel);
348
349   if (found_window)
350     translate_coords_to_child_coords (_gdk_root, found_window,
351                                       &x_tmp, &y_tmp);
352
353   if (win_x)
354     *win_x = found_window ? x_tmp : -1;
355
356   if (win_y)
357     *win_y = found_window ? y_tmp : -1;
358
359   if (mask)
360     *mask = _gdk_quartz_events_get_current_event_mask ();
361
362   return found_window;
363 }
364
365 static void
366 gdk_quartz_device_core_select_window_events (GdkDevice    *device,
367                                              GdkWindow    *window,
368                                              GdkEventMask  event_mask)
369 {
370   /* The mask is set in the common code. */
371 }