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