]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkdevice-broadway.c
[broadway] Report mouse pointer coordinates right
[~andy/gtk] / gdk / broadway / gdkdevice-broadway.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2009 Carlos Garnacho <carlosg@gnome.org>
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 #include "config.h"
21 #include <stdlib.h>
22
23 #include "gdkdevice-broadway.h"
24
25 #include "gdkwindow.h"
26 #include "gdkprivate-broadway.h"
27
28 static gboolean gdk_broadway_device_get_history (GdkDevice      *device,
29                                                  GdkWindow      *window,
30                                                  guint32         start,
31                                                  guint32         stop,
32                                                  GdkTimeCoord ***events,
33                                                  gint           *n_events);
34 static void gdk_broadway_device_get_state (GdkDevice       *device,
35                                            GdkWindow       *window,
36                                            gdouble         *axes,
37                                            GdkModifierType *mask);
38 static void gdk_broadway_device_set_window_cursor (GdkDevice *device,
39                                                    GdkWindow *window,
40                                                    GdkCursor *cursor);
41 static void gdk_broadway_device_warp (GdkDevice *device,
42                                       GdkScreen *screen,
43                                       gint       x,
44                                       gint       y);
45 static gboolean gdk_broadway_device_query_state (GdkDevice        *device,
46                                                  GdkWindow        *window,
47                                                  GdkWindow       **root_window,
48                                                  GdkWindow       **child_window,
49                                                  gint             *root_x,
50                                                  gint             *root_y,
51                                                  gint             *win_x,
52                                                  gint             *win_y,
53                                                  GdkModifierType  *mask);
54 static GdkGrabStatus gdk_broadway_device_grab   (GdkDevice     *device,
55                                                  GdkWindow     *window,
56                                                  gboolean       owner_events,
57                                                  GdkEventMask   event_mask,
58                                                  GdkWindow     *confine_to,
59                                                  GdkCursor     *cursor,
60                                                  guint32        time_);
61 static void          gdk_broadway_device_ungrab (GdkDevice     *device,
62                                                  guint32        time_);
63 static GdkWindow * gdk_broadway_device_window_at_position (GdkDevice       *device,
64                                                            gint            *win_x,
65                                                            gint            *win_y,
66                                                            GdkModifierType *mask,
67                                                            gboolean         get_toplevel);
68 static void      gdk_broadway_device_select_window_events (GdkDevice       *device,
69                                                            GdkWindow       *window,
70                                                            GdkEventMask     event_mask);
71
72
73 G_DEFINE_TYPE (GdkBroadwayDevice, gdk_broadway_device, GDK_TYPE_DEVICE)
74
75 static void
76 gdk_broadway_device_class_init (GdkBroadwayDeviceClass *klass)
77 {
78   GdkDeviceClass *device_class = GDK_DEVICE_CLASS (klass);
79
80   device_class->get_history = gdk_broadway_device_get_history;
81   device_class->get_state = gdk_broadway_device_get_state;
82   device_class->set_window_cursor = gdk_broadway_device_set_window_cursor;
83   device_class->warp = gdk_broadway_device_warp;
84   device_class->query_state = gdk_broadway_device_query_state;
85   device_class->grab = gdk_broadway_device_grab;
86   device_class->ungrab = gdk_broadway_device_ungrab;
87   device_class->window_at_position = gdk_broadway_device_window_at_position;
88   device_class->select_window_events = gdk_broadway_device_select_window_events;
89 }
90
91 static void
92 gdk_broadway_device_init (GdkBroadwayDevice *device_core)
93 {
94   GdkDevice *device;
95
96   device = GDK_DEVICE (device_core);
97
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);
100 }
101
102 static gboolean
103 gdk_broadway_device_get_history (GdkDevice      *device,
104                                  GdkWindow      *window,
105                                  guint32         start,
106                                  guint32         stop,
107                                  GdkTimeCoord ***events,
108                                  gint           *n_events)
109 {
110   return FALSE;
111 }
112
113 static void
114 gdk_broadway_device_get_state (GdkDevice       *device,
115                                GdkWindow       *window,
116                                gdouble         *axes,
117                                GdkModifierType *mask)
118 {
119   gint x_int, y_int;
120
121   gdk_window_get_pointer (window, &x_int, &y_int, mask);
122
123   if (axes)
124     {
125       axes[0] = x_int;
126       axes[1] = y_int;
127     }
128 }
129
130 static void
131 gdk_broadway_device_set_window_cursor (GdkDevice *device,
132                                        GdkWindow *window,
133                                        GdkCursor *cursor)
134 {
135 }
136
137 static void
138 gdk_broadway_device_warp (GdkDevice *device,
139                           GdkScreen *screen,
140                           gint       x,
141                           gint       y)
142 {
143 }
144
145 static gboolean
146 gdk_broadway_device_query_state (GdkDevice        *device,
147                                  GdkWindow        *window,
148                                  GdkWindow       **root_window,
149                                  GdkWindow       **child_window,
150                                  gint             *root_x,
151                                  gint             *root_y,
152                                  gint             *win_x,
153                                  gint             *win_y,
154                                  GdkModifierType  *mask)
155 {
156   GdkWindow *toplevel;
157   GdkWindowImplBroadway *impl;
158   GdkDisplay *display;
159   GdkBroadwayDisplay *broadway_display;
160   GdkScreen *screen;
161   gint device_root_x, device_root_y;
162
163   if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
164     return FALSE;
165
166   display = gdk_device_get_display (device);
167   broadway_display = GDK_BROADWAY_DISPLAY (display);
168
169   impl = GDK_WINDOW_IMPL_BROADWAY (window->impl);
170   toplevel = impl->wrapper;
171
172   if (root_window)
173     {
174       screen = gdk_window_get_screen (window);
175       *root_window = gdk_screen_get_root_window (screen);
176     }
177
178   if (mask)
179     *mask = 0; /* TODO */
180
181   if (broadway_display->output)
182     {
183       _gdk_broadway_display_consume_all_input (display);
184       if (root_x)
185         *root_x = broadway_display->future_root_x;
186       if (root_y)
187         *root_y = broadway_display->future_root_y;
188       /* TODO: Should really use future_x/y when we get configure events */
189       if (win_x)
190         *win_x = broadway_display->future_root_x - toplevel->x;
191       if (win_y)
192         *win_y = broadway_display->future_root_y - toplevel->y;
193       if (child_window)
194         {
195           if (gdk_window_get_window_type (toplevel) == GDK_WINDOW_ROOT)
196             *child_window =
197               g_hash_table_lookup (broadway_display->id_ht,
198                                    GINT_TO_POINTER (broadway_display->future_mouse_in_toplevel));
199           else
200             *child_window = toplevel; /* No native children */
201         }
202       return TRUE;
203     }
204
205   /* Fallback when unconnected */
206
207   device_root_x = broadway_display->last_x;
208   device_root_y = broadway_display->last_y;
209
210   if (root_x)
211     *root_x = device_root_x;
212   if (root_y)
213     *root_y = device_root_y;
214   if (win_x)
215     *win_x = device_root_y - toplevel->x;
216   if (win_y)
217     *win_y = device_root_y - toplevel->y;
218   if (child_window)
219     {
220       if (gdk_window_get_window_type (toplevel) == GDK_WINDOW_ROOT)
221         {
222           *child_window = broadway_display->mouse_in_toplevel;
223           if (*child_window == NULL)
224             *child_window = toplevel;
225         }
226       else
227         {
228           /* No native children */
229           *child_window = toplevel;
230         }
231     }
232
233   return TRUE;
234 }
235
236 static GdkGrabStatus
237 gdk_broadway_device_grab (GdkDevice    *device,
238                           GdkWindow    *window,
239                           gboolean      owner_events,
240                           GdkEventMask  event_mask,
241                           GdkWindow    *confine_to,
242                           GdkCursor    *cursor,
243                           guint32       time_)
244 {
245   GdkDisplay *display;
246   GdkBroadwayDisplay *broadway_display;
247
248   display = gdk_device_get_display (device);
249   broadway_display = GDK_BROADWAY_DISPLAY (display);
250
251   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
252     {
253       /* Device is a keyboard */
254       return GDK_GRAB_SUCCESS;
255     }
256   else
257     {
258       /* Device is a pointer */
259
260       if (broadway_display->pointer_grab_window != NULL &&
261           time_ != 0 && broadway_display->pointer_grab_time > time_)
262         return GDK_GRAB_ALREADY_GRABBED;
263
264       if (time_ == 0)
265         time_ = broadway_display->last_event_time;
266
267       broadway_display->pointer_grab_window = window;
268       broadway_display->pointer_grab_owner_events = owner_events;
269       broadway_display->pointer_grab_time = time_;
270
271       if (broadway_display->output)
272         {
273           broadway_output_grab_pointer (broadway_display->output,
274                                         GDK_WINDOW_IMPL_BROADWAY (window->impl)->id,
275                                         owner_events);
276           gdk_display_flush (display);
277         }
278
279       /* TODO: What about toplevel grab events if we're not connected? */
280
281       return GDK_GRAB_SUCCESS;
282     }
283 }
284
285 #define TIME_IS_LATER(time1, time2)                        \
286   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
287     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
288   )
289
290 static void
291 gdk_broadway_device_ungrab (GdkDevice *device,
292                             guint32    time_)
293 {
294   GdkDisplay *display;
295   GdkBroadwayDisplay *broadway_display;
296   GdkDeviceGrabInfo *grab;
297   guint32 serial;
298
299   display = gdk_device_get_display (device);
300   broadway_display = GDK_BROADWAY_DISPLAY (display);
301
302   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
303     {
304       /* Device is a keyboard */
305     }
306   else
307     {
308       /* Device is a pointer */
309
310       if (broadway_display->pointer_grab_window != NULL &&
311           time_ != 0 && broadway_display->pointer_grab_time > time_)
312         return;
313
314       /* TODO: What about toplevel grab events if we're not connected? */
315
316       if (broadway_display->output)
317         {
318           serial = broadway_output_ungrab_pointer (broadway_display->output);
319           gdk_display_flush (display);
320         }
321       else
322         {
323           serial = broadway_display->saved_serial;
324         }
325
326       grab = _gdk_display_get_last_device_grab (display, device);
327       if (grab &&
328           (time_ == GDK_CURRENT_TIME ||
329            grab->time == GDK_CURRENT_TIME ||
330            !TIME_IS_LATER (grab->time, time_)))
331         grab->serial_end = serial;
332
333       broadway_display->pointer_grab_window = NULL;
334     }
335 }
336
337 static GdkWindow *
338 gdk_broadway_device_window_at_position (GdkDevice       *device,
339                                         gint            *win_x,
340                                         gint            *win_y,
341                                         GdkModifierType *mask,
342                                         gboolean         get_toplevel)
343 {
344   gboolean res;
345   GdkScreen *screen;
346   GdkWindow *root_window;
347   GdkWindow *window;
348
349   screen = gdk_screen_get_default ();
350   root_window = gdk_screen_get_root_window (screen);
351
352   res = gdk_broadway_device_query_state (device, root_window, NULL, &window, NULL, NULL, win_x, win_y, mask);
353   if (res)
354     return window;
355
356   return NULL;
357 }
358
359 static void
360 gdk_broadway_device_select_window_events (GdkDevice    *device,
361                                           GdkWindow    *window,
362                                           GdkEventMask  event_mask)
363 {
364 }