]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkdevice-broadway.c
edbc3eb5f178a988ab0ef3422cfcdc1e26bbdaf0
[~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 void
238 _gdk_broadway_window_grab_check_destroy (GdkWindow *window)
239 {
240   GdkDisplay *display = gdk_window_get_display (window);
241   GdkBroadwayDisplay *broadway_display;
242   GdkDeviceManager *device_manager;
243   GdkDeviceGrabInfo *grab;
244   GList *devices, *d;
245
246   broadway_display = GDK_BROADWAY_DISPLAY (display);
247
248   device_manager = gdk_display_get_device_manager (display);
249
250   /* Get all devices */
251   devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
252
253   for (d = devices; d; d = d->next)
254     {
255       /* Make sure there is no lasting grab in this native window */
256       grab = _gdk_display_get_last_device_grab (display, d->data);
257
258       if (grab && grab->native_window == window)
259         {
260           grab->serial_end = grab->serial_start;
261           grab->implicit_ungrab = TRUE;
262
263           broadway_display->pointer_grab_window = NULL;
264         }
265
266     }
267
268   g_list_free (devices);
269 }
270
271
272 static GdkGrabStatus
273 gdk_broadway_device_grab (GdkDevice    *device,
274                           GdkWindow    *window,
275                           gboolean      owner_events,
276                           GdkEventMask  event_mask,
277                           GdkWindow    *confine_to,
278                           GdkCursor    *cursor,
279                           guint32       time_)
280 {
281   GdkDisplay *display;
282   GdkBroadwayDisplay *broadway_display;
283
284   display = gdk_device_get_display (device);
285   broadway_display = GDK_BROADWAY_DISPLAY (display);
286
287   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
288     {
289       /* Device is a keyboard */
290       return GDK_GRAB_SUCCESS;
291     }
292   else
293     {
294       /* Device is a pointer */
295
296       if (broadway_display->pointer_grab_window != NULL &&
297           time_ != 0 && broadway_display->pointer_grab_time > time_)
298         return GDK_GRAB_ALREADY_GRABBED;
299
300       if (time_ == 0)
301         time_ = broadway_display->last_seen_time;
302
303       broadway_display->pointer_grab_window = window;
304       broadway_display->pointer_grab_owner_events = owner_events;
305       broadway_display->pointer_grab_time = time_;
306
307       if (broadway_display->output)
308         {
309           broadway_output_grab_pointer (broadway_display->output,
310                                         GDK_WINDOW_IMPL_BROADWAY (window->impl)->id,
311                                         owner_events);
312           gdk_display_flush (display);
313         }
314
315       /* TODO: What about toplevel grab events if we're not connected? */
316
317       return GDK_GRAB_SUCCESS;
318     }
319 }
320
321 #define TIME_IS_LATER(time1, time2)                        \
322   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
323     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
324   )
325
326 static void
327 gdk_broadway_device_ungrab (GdkDevice *device,
328                             guint32    time_)
329 {
330   GdkDisplay *display;
331   GdkBroadwayDisplay *broadway_display;
332   GdkDeviceGrabInfo *grab;
333   guint32 serial;
334
335   display = gdk_device_get_display (device);
336   broadway_display = GDK_BROADWAY_DISPLAY (display);
337
338   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
339     {
340       /* Device is a keyboard */
341     }
342   else
343     {
344       /* Device is a pointer */
345
346       if (broadway_display->pointer_grab_window != NULL &&
347           time_ != 0 && broadway_display->pointer_grab_time > time_)
348         return;
349
350       /* TODO: What about toplevel grab events if we're not connected? */
351
352       if (broadway_display->output)
353         {
354           serial = broadway_output_ungrab_pointer (broadway_display->output);
355           gdk_display_flush (display);
356         }
357       else
358         {
359           serial = broadway_display->saved_serial;
360         }
361
362       grab = _gdk_display_get_last_device_grab (display, device);
363       if (grab &&
364           (time_ == GDK_CURRENT_TIME ||
365            grab->time == GDK_CURRENT_TIME ||
366            !TIME_IS_LATER (grab->time, time_)))
367         grab->serial_end = serial;
368
369       broadway_display->pointer_grab_window = NULL;
370     }
371 }
372
373 static GdkWindow *
374 gdk_broadway_device_window_at_position (GdkDevice       *device,
375                                         gint            *win_x,
376                                         gint            *win_y,
377                                         GdkModifierType *mask,
378                                         gboolean         get_toplevel)
379 {
380   gboolean res;
381   GdkScreen *screen;
382   GdkWindow *root_window;
383   GdkWindow *window;
384
385   screen = gdk_screen_get_default ();
386   root_window = gdk_screen_get_root_window (screen);
387
388   res = gdk_broadway_device_query_state (device, root_window, NULL, &window, NULL, NULL, win_x, win_y, mask);
389   if (res)
390     return window;
391
392   return NULL;
393 }
394
395 static void
396 gdk_broadway_device_select_window_events (GdkDevice    *device,
397                                           GdkWindow    *window,
398                                           GdkEventMask  event_mask)
399 {
400 }