]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkdevice-broadway.c
[broadway] Serialize event times
[~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 (broadway_display->output)
179     {
180       _gdk_broadway_display_consume_all_input (display);
181       if (root_x)
182         *root_x = broadway_display->future_root_x;
183       if (root_y)
184         *root_y = broadway_display->future_root_y;
185       /* TODO: Should really use future_x/y when we get configure events */
186       if (win_x)
187         *win_x = broadway_display->future_root_x - toplevel->x;
188       if (win_y)
189         *win_y = broadway_display->future_root_y - toplevel->y;
190       if (mask)
191         *mask = broadway_display->future_state;
192       if (child_window)
193         {
194           if (gdk_window_get_window_type (toplevel) == GDK_WINDOW_ROOT)
195             *child_window =
196               g_hash_table_lookup (broadway_display->id_ht,
197                                    GINT_TO_POINTER (broadway_display->future_mouse_in_toplevel));
198           else
199             *child_window = toplevel; /* No native children */
200         }
201       return TRUE;
202     }
203
204   /* Fallback when unconnected */
205
206   device_root_x = broadway_display->last_x;
207   device_root_y = broadway_display->last_y;
208
209   if (root_x)
210     *root_x = device_root_x;
211   if (root_y)
212     *root_y = device_root_y;
213   if (win_x)
214     *win_x = device_root_y - toplevel->x;
215   if (win_y)
216     *win_y = device_root_y - toplevel->y;
217   if (mask)
218     *mask = broadway_display->last_state;
219   if (child_window)
220     {
221       if (gdk_window_get_window_type (toplevel) == GDK_WINDOW_ROOT)
222         {
223           *child_window = broadway_display->mouse_in_toplevel;
224           if (*child_window == NULL)
225             *child_window = toplevel;
226         }
227       else
228         {
229           /* No native children */
230           *child_window = toplevel;
231         }
232     }
233
234   return TRUE;
235 }
236
237 static GdkGrabStatus
238 gdk_broadway_device_grab (GdkDevice    *device,
239                           GdkWindow    *window,
240                           gboolean      owner_events,
241                           GdkEventMask  event_mask,
242                           GdkWindow    *confine_to,
243                           GdkCursor    *cursor,
244                           guint32       time_)
245 {
246   GdkDisplay *display;
247   GdkBroadwayDisplay *broadway_display;
248
249   display = gdk_device_get_display (device);
250   broadway_display = GDK_BROADWAY_DISPLAY (display);
251
252   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
253     {
254       /* Device is a keyboard */
255       return GDK_GRAB_SUCCESS;
256     }
257   else
258     {
259       /* Device is a pointer */
260
261       if (broadway_display->pointer_grab_window != NULL &&
262           time_ != 0 && broadway_display->pointer_grab_time > time_)
263         return GDK_GRAB_ALREADY_GRABBED;
264
265       if (time_ == 0)
266         time_ = broadway_display->last_seen_time;
267
268       broadway_display->pointer_grab_window = window;
269       broadway_display->pointer_grab_owner_events = owner_events;
270       broadway_display->pointer_grab_time = time_;
271
272       if (broadway_display->output)
273         {
274           broadway_output_grab_pointer (broadway_display->output,
275                                         GDK_WINDOW_IMPL_BROADWAY (window->impl)->id,
276                                         owner_events);
277           gdk_display_flush (display);
278         }
279
280       /* TODO: What about toplevel grab events if we're not connected? */
281
282       return GDK_GRAB_SUCCESS;
283     }
284 }
285
286 #define TIME_IS_LATER(time1, time2)                        \
287   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
288     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
289   )
290
291 static void
292 gdk_broadway_device_ungrab (GdkDevice *device,
293                             guint32    time_)
294 {
295   GdkDisplay *display;
296   GdkBroadwayDisplay *broadway_display;
297   GdkDeviceGrabInfo *grab;
298   guint32 serial;
299
300   display = gdk_device_get_display (device);
301   broadway_display = GDK_BROADWAY_DISPLAY (display);
302
303   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
304     {
305       /* Device is a keyboard */
306     }
307   else
308     {
309       /* Device is a pointer */
310
311       if (broadway_display->pointer_grab_window != NULL &&
312           time_ != 0 && broadway_display->pointer_grab_time > time_)
313         return;
314
315       /* TODO: What about toplevel grab events if we're not connected? */
316
317       if (broadway_display->output)
318         {
319           serial = broadway_output_ungrab_pointer (broadway_display->output);
320           gdk_display_flush (display);
321         }
322       else
323         {
324           serial = broadway_display->saved_serial;
325         }
326
327       grab = _gdk_display_get_last_device_grab (display, device);
328       if (grab &&
329           (time_ == GDK_CURRENT_TIME ||
330            grab->time == GDK_CURRENT_TIME ||
331            !TIME_IS_LATER (grab->time, time_)))
332         grab->serial_end = serial;
333
334       broadway_display->pointer_grab_window = NULL;
335     }
336 }
337
338 static GdkWindow *
339 gdk_broadway_device_window_at_position (GdkDevice       *device,
340                                         gint            *win_x,
341                                         gint            *win_y,
342                                         GdkModifierType *mask,
343                                         gboolean         get_toplevel)
344 {
345   gboolean res;
346   GdkScreen *screen;
347   GdkWindow *root_window;
348   GdkWindow *window;
349
350   screen = gdk_screen_get_default ();
351   root_window = gdk_screen_get_root_window (screen);
352
353   res = gdk_broadway_device_query_state (device, root_window, NULL, &window, NULL, NULL, win_x, win_y, mask);
354   if (res)
355     return window;
356
357   return NULL;
358 }
359
360 static void
361 gdk_broadway_device_select_window_events (GdkDevice    *device,
362                                           GdkWindow    *window,
363                                           GdkEventMask  event_mask)
364 {
365 }