]> Pileus Git - ~andy/gtk/blob - gdk/broadway/gdkeventsource.c
broadway: Get query_state window coords from browser side
[~andy/gtk] / gdk / broadway / gdkeventsource.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
22 #include "gdkeventsource.h"
23
24 #include "gdkinternals.h"
25
26 #include <stdlib.h>
27
28 static gboolean gdk_event_source_prepare  (GSource     *source,
29                                            gint        *timeout);
30 static gboolean gdk_event_source_check    (GSource     *source);
31 static gboolean gdk_event_source_dispatch (GSource     *source,
32                                            GSourceFunc  callback,
33                                            gpointer     user_data);
34 static void     gdk_event_source_finalize (GSource     *source);
35
36 #define HAS_FOCUS(toplevel)                           \
37   ((toplevel)->has_focus || (toplevel)->has_pointer_focus)
38
39 struct _GdkEventSource
40 {
41   GSource source;
42
43   GdkDisplay *display;
44   GPollFD event_poll_fd;
45 };
46
47 static GSourceFuncs event_funcs = {
48   gdk_event_source_prepare,
49   gdk_event_source_check,
50   gdk_event_source_dispatch,
51   gdk_event_source_finalize
52 };
53
54 static GList *event_sources = NULL;
55
56 static gboolean
57 gdk_event_source_prepare (GSource *source,
58                           gint    *timeout)
59 {
60   GdkDisplay *display = ((GdkEventSource*) source)->display;
61   gboolean retval;
62
63   GDK_THREADS_ENTER ();
64
65   *timeout = -1;
66   retval = (_gdk_event_queue_find_first (display) != NULL);
67
68   GDK_THREADS_LEAVE ();
69
70   return retval;
71 }
72
73 static gboolean
74 gdk_event_source_check (GSource *source)
75 {
76   GdkEventSource *event_source = (GdkEventSource*) source;
77   gboolean retval;
78
79   GDK_THREADS_ENTER ();
80
81   if (event_source->event_poll_fd.revents & G_IO_IN)
82     retval = (_gdk_event_queue_find_first (event_source->display) != NULL);
83   else
84     retval = FALSE;
85
86   GDK_THREADS_LEAVE ();
87
88   return retval;
89 }
90
91 typedef struct {
92   int id;
93   int root_x;
94   int root_y;
95   int win_x;
96   int win_y;
97   guint64 time;
98 } PointerData;
99
100 static char *
101 parse_pointer_data (char *p, PointerData *data)
102 {
103   data->id = strtol (p, &p, 10);
104   p++; /* Skip , */
105   data->root_x = strtol (p, &p, 10);
106   p++; /* Skip , */
107   data->root_y = strtol (p, &p, 10);
108   p++; /* Skip , */
109   data->win_x = strtol (p, &p, 10);
110   p++; /* Skip , */
111   data->win_y = strtol (p, &p, 10);
112   p++; /* Skip , */
113   data->time = strtol(p, &p, 10);
114
115   return p;
116 }
117
118
119 void
120 _gdk_broadway_events_got_input (GdkDisplay *display,
121                                 const char *message)
122 {
123   GdkBroadwayDisplay *display_broadway = GDK_BROADWAY_DISPLAY (display);
124   GdkScreen *screen;
125   GdkWindow *root, *window;
126   char *p;
127   int button, dir,key;
128   guint32 serial;
129   guint64 time;
130   GdkEvent *event = NULL;
131   char cmd;
132   GList *node;
133   PointerData data;
134
135   screen = gdk_display_get_default_screen (display);
136   root = gdk_screen_get_root_window (screen);
137
138   p = (char *)message;
139   cmd = *p++;
140   serial = (guint32)strtol (p, &p, 10);
141   p++; /* Skip , */
142   switch (cmd) {
143   case 'e': /* Enter */
144     p = parse_pointer_data (p, &data);
145
146     display_broadway->last_x = data.root_x;
147     display_broadway->last_y = data.root_y;
148
149     window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
150
151     /* TODO: Unset when it dies */
152     display_broadway->mouse_in_toplevel = window;
153
154     if (window)
155       {
156         event = gdk_event_new (GDK_ENTER_NOTIFY);
157         event->crossing.window = g_object_ref (window);
158         event->crossing.time = data.time;
159         event->crossing.x = data.win_x;
160         event->crossing.y = data.win_y;
161         event->crossing.x_root = data.root_x;
162         event->crossing.y_root = data.root_y;
163         event->crossing.mode = GDK_CROSSING_NORMAL;
164         event->crossing.detail = GDK_NOTIFY_ANCESTOR;
165         gdk_event_set_device (event, display->core_pointer);
166
167         node = _gdk_event_queue_append (display, event);
168         _gdk_windowing_got_event (display, node, event, serial);
169
170         event = gdk_event_new (GDK_FOCUS_CHANGE);
171         event->focus_change.window = g_object_ref (window);
172         event->focus_change.in = TRUE;
173         gdk_event_set_device (event, display->core_pointer);
174
175         node = _gdk_event_queue_append (display, event);
176         _gdk_windowing_got_event (display, node, event, serial);
177       }
178     break;
179   case 'l': /* Leave */
180     p = parse_pointer_data (p, &data);
181
182     display_broadway->last_x = data.root_x;
183     display_broadway->last_y = data.root_y;
184
185     window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
186
187     display_broadway->mouse_in_toplevel = NULL;
188
189     if (window)
190       {
191         event = gdk_event_new (GDK_LEAVE_NOTIFY);
192         event->crossing.window = g_object_ref (window);
193         event->crossing.time = data.time;
194         event->crossing.x = data.win_x;
195         event->crossing.y = data.win_y;
196         event->crossing.x_root = data.root_x;
197         event->crossing.y_root = data.root_y;
198         event->crossing.mode = GDK_CROSSING_NORMAL;
199         event->crossing.detail = GDK_NOTIFY_ANCESTOR;
200         gdk_event_set_device (event, display->core_pointer);
201
202         node = _gdk_event_queue_append (display, event);
203         _gdk_windowing_got_event (display, node, event, serial);
204
205         event = gdk_event_new (GDK_FOCUS_CHANGE);
206         event->focus_change.window = g_object_ref (window);
207         event->focus_change.in = FALSE;
208         gdk_event_set_device (event, display->core_pointer);
209
210         node = _gdk_event_queue_append (display, event);
211         _gdk_windowing_got_event (display, node, event, serial);
212       }
213     break;
214   case 'm': /* Mouse move */
215     p = parse_pointer_data (p, &data);
216
217     display_broadway->last_x = data.root_x;
218     display_broadway->last_y = data.root_y;
219
220     window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
221
222     if (window)
223       {
224         event = gdk_event_new (GDK_MOTION_NOTIFY);
225         event->motion.window = g_object_ref (window);
226         event->motion.time = data.time;
227         event->motion.x = data.win_x;
228         event->motion.y = data.win_y;
229         event->motion.x_root = data.root_x;
230         event->motion.y_root = data.root_y;
231         gdk_event_set_device (event, display->core_pointer);
232
233         node = _gdk_event_queue_append (display, event);
234         _gdk_windowing_got_event (display, node, event, serial);
235       }
236
237     break;
238   case 'b':
239   case 'B':
240     p = parse_pointer_data (p, &data);
241     p++; /* Skip , */
242     button = strtol(p, &p, 10);
243     display_broadway->last_x = data.root_x;
244     display_broadway->last_y = data.root_y;
245
246     window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
247
248     if (window)
249       {
250         event = gdk_event_new (cmd == 'b' ? GDK_BUTTON_PRESS : GDK_BUTTON_RELEASE);
251         event->button.window = g_object_ref (window);
252         event->button.time = data.time;
253         event->button.x = data.win_x;
254         event->button.y = data.win_y;
255         event->button.x_root = data.root_x;
256         event->button.y_root = data.root_y;
257         event->button.button = button + 1;
258         gdk_event_set_device (event, display->core_pointer);
259
260         node = _gdk_event_queue_append (display, event);
261         _gdk_windowing_got_event (display, node, event, serial);
262       }
263
264     break;
265   case 's':
266     p = parse_pointer_data (p, &data);
267     p++; /* Skip , */
268     dir = strtol(p, &p, 10);
269     display_broadway->last_x = data.root_x;
270     display_broadway->last_y = data.root_y;
271
272     window = g_hash_table_lookup (display_broadway->id_ht, GINT_TO_POINTER (data.id));
273
274     if (window)
275       {
276         event = gdk_event_new (GDK_SCROLL);
277         event->scroll.window = g_object_ref (window);
278         event->scroll.time = data.time;
279         event->scroll.x = data.win_x;
280         event->scroll.y = data.win_y;
281         event->scroll.x_root = data.root_x;
282         event->scroll.y_root = data.root_y;
283         event->scroll.direction = dir == 0 ? GDK_SCROLL_UP : GDK_SCROLL_DOWN;
284         gdk_event_set_device (event, display->core_pointer);
285
286         node = _gdk_event_queue_append (display, event);
287         _gdk_windowing_got_event (display, node, event, serial);
288       }
289
290     break;
291   case 'k':
292   case 'K':
293     key = strtol(p, &p, 10);
294     p++; /* Skip , */
295     time = strtol(p, &p, 10);
296
297     window = display_broadway->mouse_in_toplevel;
298
299     if (window)
300       {
301         event = gdk_event_new (cmd == 'k' ? GDK_KEY_PRESS : GDK_KEY_RELEASE);
302         event->key.window = g_object_ref (window);
303         event->key.time = time;
304         event->key.keyval = key;
305         event->key.length = 0;
306         gdk_event_set_device (event, display->core_pointer);
307
308         node = _gdk_event_queue_append (display, event);
309         _gdk_windowing_got_event (display, node, event, serial);
310       }
311
312     break;
313   case 'q':
314     g_printerr ("Got unexpected query pointer reply w serial %d\n", serial);
315     break;
316   default:
317     g_printerr ("Unknown input command %s\n", message);
318     break;
319   }
320 }
321
322 void
323 _gdk_broadway_display_queue_events (GdkDisplay *display)
324 {
325 }
326
327 static gboolean
328 gdk_event_source_dispatch (GSource     *source,
329                            GSourceFunc  callback,
330                            gpointer     user_data)
331 {
332   GdkDisplay *display = ((GdkEventSource*) source)->display;
333   GdkEvent *event;
334
335   GDK_THREADS_ENTER ();
336
337   event = gdk_display_get_event (display);
338
339   if (event)
340     {
341       _gdk_event_emit (event);
342
343       gdk_event_free (event);
344     }
345
346   GDK_THREADS_LEAVE ();
347
348   return TRUE;
349 }
350
351 static void
352 gdk_event_source_finalize (GSource *source)
353 {
354   GdkEventSource *event_source = (GdkEventSource *)source;
355
356   event_sources = g_list_remove (event_sources, event_source);
357 }
358
359 GSource *
360 _gdk_broadway_event_source_new (GdkDisplay *display)
361 {
362   GSource *source;
363   GdkEventSource *event_source;
364   char *name;
365
366   source = g_source_new (&event_funcs, sizeof (GdkEventSource));
367   name = g_strdup_printf ("GDK Broadway Event source (%s)",
368                           gdk_display_get_name (display));
369   g_source_set_name (source, name);
370   g_free (name);
371   event_source = (GdkEventSource *) source;
372   event_source->display = display;
373
374   g_source_set_priority (source, GDK_PRIORITY_EVENTS);
375   g_source_set_can_recurse (source, TRUE);
376   g_source_attach (source, NULL);
377
378   event_sources = g_list_prepend (event_sources, source);
379
380   return source;
381 }