]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkdevice-broadway.c
Merge branch 'broadway'
[~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   GdkDisplay *display;
157   GdkBroadwayDisplay *broadway_display;
158   GdkWindowImplBroadway *impl;
159   guint32 serial;
160   GdkScreen *screen;
161   char *reply;
162   gint device_root_x, device_root_y, device_win_x, device_win_y, id;
163
164   if (gdk_device_get_source (device) != GDK_SOURCE_MOUSE)
165     return FALSE;
166
167   display = gdk_device_get_display (device);
168   broadway_display = GDK_BROADWAY_DISPLAY (display);
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 (mask)
177     *mask = 0; /* TODO */
178
179   if (broadway_display->output)
180     {
181       impl = GDK_WINDOW_IMPL_BROADWAY (window->impl);
182
183       serial = broadway_output_query_pointer (broadway_display->output, impl->id);
184
185       reply = _gdk_broadway_display_block_for_input (display, 'q', serial, TRUE);
186
187       if (reply != NULL)
188         {
189           char *p;
190           char cmd;
191           guint32 reply_serial;
192
193           p = reply;
194
195           cmd = *p++;
196           reply_serial = (guint32)strtol(p, &p, 10);
197           p++; /* Skip , */
198
199           device_root_x = strtol(p, &p, 10);
200           p++; /* Skip , */
201           device_root_y = strtol(p, &p, 10);
202           p++; /* Skip , */
203           device_win_x = strtol(p, &p, 10);
204           p++; /* Skip , */
205           device_win_y = strtol(p, &p, 10);
206           p++; /* Skip , */
207           id = strtol(p, &p, 10);
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_win_x;
215           if (win_y)
216             *win_y = device_win_y;
217           if (child_window)
218             {
219               if (gdk_window_get_window_type (window) == GDK_WINDOW_ROOT)
220                 *child_window = g_hash_table_lookup (broadway_display->id_ht, GINT_TO_POINTER (id));
221               else
222                 *child_window = window; /* No native children */
223             }
224           g_free (reply);
225           return TRUE;
226         }
227     }
228
229   /* Fallback when unconnected */
230
231   device_root_x = broadway_display->last_x;
232   device_root_y = broadway_display->last_y;
233
234   if (root_x)
235     *root_x = device_root_x;
236   if (root_y)
237     *root_y = device_root_y;
238   if (win_x)
239     *win_x = device_root_y - window->x;
240   if (win_y)
241     *win_y = device_root_y - window->y;
242   if (child_window)
243     {
244       if (gdk_window_get_window_type (window) == GDK_WINDOW_ROOT)
245         {
246           *child_window = broadway_display->mouse_in_toplevel;
247           if (*child_window == NULL)
248             *child_window = window;
249         }
250       else
251         {
252           /* No native children */
253           *child_window = window;
254         }
255     }
256
257   return TRUE;
258 }
259
260 static GdkGrabStatus
261 gdk_broadway_device_grab (GdkDevice    *device,
262                           GdkWindow    *window,
263                           gboolean      owner_events,
264                           GdkEventMask  event_mask,
265                           GdkWindow    *confine_to,
266                           GdkCursor    *cursor,
267                           guint32       time_)
268 {
269   GdkDisplay *display;
270   GdkBroadwayDisplay *broadway_display;
271   GdkWindowImplBroadway *impl;
272   guint32 serial;
273   char *reply;
274
275   display = gdk_device_get_display (device);
276   broadway_display = GDK_BROADWAY_DISPLAY (display);
277
278   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
279     {
280       /* Device is a keyboard */
281       return GDK_GRAB_SUCCESS;
282     }
283   else
284     {
285       /* Device is a pointer */
286
287       if (broadway_display->output)
288         {
289           impl = GDK_WINDOW_IMPL_BROADWAY (window->impl);
290
291           serial = broadway_output_grab_pointer (broadway_display->output,
292                                                  impl->id, owner_events, time_);
293           reply = _gdk_broadway_display_block_for_input (display, 'g', serial, FALSE);
294           if (reply != NULL)
295             {
296               char *p;
297               char cmd;
298               guint32 reply_serial;
299               int res;
300
301               p = reply;
302
303               cmd = *p++;
304               reply_serial = (guint32)strtol(p, &p, 10);
305               p++; /* Skip , */
306
307               res = strtol(p, &p, 10);
308
309               return res;
310             }
311         }
312
313       return GDK_GRAB_NOT_VIEWABLE;
314     }
315 }
316
317 #define TIME_IS_LATER(time1, time2)                        \
318   ( (( time1 > time2 ) && ( time1 - time2 < ((guint32)-1)/2 )) ||  \
319     (( time1 < time2 ) && ( time2 - time1 > ((guint32)-1)/2 ))     \
320   )
321
322 static void
323 gdk_broadway_device_ungrab (GdkDevice *device,
324                             guint32    time_)
325 {
326   GdkDisplay *display;
327   GdkBroadwayDisplay *broadway_display;
328   GdkDeviceGrabInfo *grab;
329   guint32 serial;
330
331   display = gdk_device_get_display (device);
332   broadway_display = GDK_BROADWAY_DISPLAY (display);
333
334   if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
335     {
336       /* Device is a keyboard */
337     }
338   else
339     {
340       /* Device is a pointer */
341
342       if (broadway_display->output)
343         {
344           serial = broadway_output_ungrab_pointer (broadway_display->output, time_);
345
346           gdk_display_flush (display);
347
348           grab = _gdk_display_get_last_device_grab (display, device);
349           if (grab &&
350               (time_ == GDK_CURRENT_TIME ||
351                grab->time == GDK_CURRENT_TIME ||
352                !TIME_IS_LATER (grab->time, time_)))
353             grab->serial_end = serial;
354         }
355     }
356 }
357
358 static GdkWindow *
359 gdk_broadway_device_window_at_position (GdkDevice       *device,
360                                         gint            *win_x,
361                                         gint            *win_y,
362                                         GdkModifierType *mask,
363                                         gboolean         get_toplevel)
364 {
365   gboolean res;
366   GdkScreen *screen;
367   GdkWindow *root_window;
368   GdkWindow *window;
369
370   screen = gdk_screen_get_default ();
371   root_window = gdk_screen_get_root_window (screen);
372
373   res = gdk_broadway_device_query_state (device, root_window, NULL, &window, NULL, NULL, win_x, win_y, mask);
374   if (res)
375     return window;
376
377   return NULL;
378 }
379
380 static void
381 gdk_broadway_device_select_window_events (GdkDevice    *device,
382                                           GdkWindow    *window,
383                                           GdkEventMask  event_mask)
384 {
385 }