]> Pileus Git - ~andy/gtk/blob - gtk/gtkplug-x11.c
[broadway] Add initial keyboard event support
[~andy/gtk] / gtk / gtkplug-x11.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /* By Owen Taylor <otaylor@gtk.org>              98/4/4 */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include "config.h"
29
30 #ifdef XINPUT_2
31
32 /* Hack to have keyboard events interpreted
33  * regardless of the default device manager
34  */
35 #define GDK_COMPILATION
36 #include "x11/gdkdevicemanager-core.h"
37 #include "x11/gdkdevicemanager-xi2.h"
38 #include "x11/gdkeventtranslator.h"
39 #undef GDK_COMPILATION
40
41 #endif /* XINPUT_2 */
42
43 #include "gtkmain.h"
44 #include "gtkmarshalers.h"
45 #include "gtkplug.h"
46 #include "gtkprivate.h"
47 #include "gtkplugprivate.h"
48 #include "gtkdebug.h"
49
50 #include "x11/gdkx.h"
51
52 #include "gtkxembed.h"
53
54 static void xembed_set_info            (GdkWindow     *window,
55                                         unsigned long  flags);
56
57 GdkNativeWindow
58 _gtk_plug_windowing_get_id (GtkPlug *plug)
59 {
60   return GDK_WINDOW_XWINDOW (gtk_widget_get_window (GTK_WIDGET (plug)));
61 }
62
63 void
64 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
65 {
66   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
67 }
68
69 void
70 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
71 {
72   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
73 }
74
75 void
76 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
77 {
78   xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
79 }
80
81 void
82 _gtk_plug_windowing_set_focus (GtkPlug *plug)
83 {
84   GtkPlugPrivate *priv = plug->priv;
85
86   _gtk_xembed_send_message (priv->socket_window,
87                             XEMBED_REQUEST_FOCUS, 0, 0, 0);
88 }
89
90 void
91 _gtk_plug_windowing_add_grabbed_key (GtkPlug        *plug,
92                                      guint           accelerator_key,
93                                      GdkModifierType accelerator_mods)
94 {
95   GtkPlugPrivate *priv = plug->priv;
96
97   _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
98                             accelerator_key, accelerator_mods);
99 }
100
101 void
102 _gtk_plug_windowing_remove_grabbed_key (GtkPlug        *plug,
103                                         guint           accelerator_key,
104                                         GdkModifierType accelerator_mods)
105 {
106   GtkPlugPrivate *priv = plug->priv;
107
108   _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
109                             accelerator_key, accelerator_mods);
110 }
111
112 void
113 _gtk_plug_windowing_focus_to_parent (GtkPlug         *plug,
114                                      GtkDirectionType direction)
115 {
116   GtkPlugPrivate *priv = plug->priv;
117   XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
118   
119   switch (direction)
120     {
121     case GTK_DIR_UP:
122     case GTK_DIR_LEFT:
123     case GTK_DIR_TAB_BACKWARD:
124       message = XEMBED_FOCUS_PREV;
125       break;
126     case GTK_DIR_DOWN:
127     case GTK_DIR_RIGHT:
128     case GTK_DIR_TAB_FORWARD:
129       message = XEMBED_FOCUS_NEXT;
130       break;
131     }
132
133   _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
134 }
135
136 static void
137 xembed_set_info (GdkWindow     *window,
138                  unsigned long  flags)
139 {
140   GdkDisplay *display = gdk_window_get_display (window);
141   unsigned long buffer[2];
142
143   Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
144
145   buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
146   buffer[1] = flags;
147
148   XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
149                    GDK_WINDOW_XWINDOW (window),
150                    xembed_info_atom, xembed_info_atom, 32,
151                    PropModeReplace,
152                    (unsigned char *)buffer, 2);
153 }
154
155 static void
156 handle_xembed_message (GtkPlug           *plug,
157                        XEmbedMessageType  message,
158                        glong              detail,
159                        glong              data1,
160                        glong              data2,
161                        guint32            time)
162 {
163   GtkWindow *window = GTK_WINDOW (plug);
164
165   GTK_NOTE (PLUGSOCKET,
166             g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
167   
168   switch (message)
169     {
170     case XEMBED_EMBEDDED_NOTIFY:
171       break;
172     case XEMBED_WINDOW_ACTIVATE:
173       _gtk_window_set_is_active (window, TRUE);
174       break;
175     case XEMBED_WINDOW_DEACTIVATE:
176       _gtk_window_set_is_active (window, FALSE);
177       break;
178       
179     case XEMBED_MODALITY_ON:
180       _gtk_plug_handle_modality_on (plug);
181       break;
182     case XEMBED_MODALITY_OFF:
183       _gtk_plug_handle_modality_off (plug);
184       break;
185
186     case XEMBED_FOCUS_IN:
187       _gtk_window_set_has_toplevel_focus (window, TRUE);
188       switch (detail)
189         {
190         case XEMBED_FOCUS_FIRST:
191           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
192           break;
193         case XEMBED_FOCUS_LAST:
194           _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
195           break;
196         case XEMBED_FOCUS_CURRENT:
197           break;
198         }
199       break;
200
201     case XEMBED_FOCUS_OUT:
202       _gtk_window_set_has_toplevel_focus (window, FALSE);
203       break;
204       
205     case XEMBED_GRAB_KEY:
206     case XEMBED_UNGRAB_KEY:
207     case XEMBED_GTK_GRAB_KEY:
208     case XEMBED_GTK_UNGRAB_KEY:
209     case XEMBED_REQUEST_FOCUS:
210     case XEMBED_FOCUS_NEXT:
211     case XEMBED_FOCUS_PREV:
212       g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
213       break;
214       
215     default:
216       GTK_NOTE(PLUGSOCKET,
217                g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
218       break;
219     }
220 }
221
222 GdkFilterReturn
223 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
224                                  GdkEvent  *event,
225                                  gpointer   data)
226 {
227   GdkScreen *screen = gdk_window_get_screen (event->any.window);
228   GdkDisplay *display = gdk_screen_get_display (screen);
229   GtkPlug *plug = GTK_PLUG (data);
230   GtkPlugPrivate *priv = plug->priv;
231   XEvent *xevent = (XEvent *)gdk_xevent;
232
233   GdkFilterReturn return_val;
234
235   return_val = GDK_FILTER_CONTINUE;
236
237   switch (xevent->type)
238     {
239     case ClientMessage:
240       if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
241         {
242           _gtk_xembed_push_message (xevent);
243           handle_xembed_message (plug,
244                                  xevent->xclient.data.l[1],
245                                  xevent->xclient.data.l[2],
246                                  xevent->xclient.data.l[3],
247                                  xevent->xclient.data.l[4],
248                                  xevent->xclient.data.l[0]);
249           _gtk_xembed_pop_message ();
250
251           return_val = GDK_FILTER_REMOVE;
252         }
253       else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
254         {
255           /* We filter these out because we take being reparented back to the
256            * root window as the reliable end of the embedding protocol
257            */
258
259           return_val = GDK_FILTER_REMOVE;
260         }
261       break;
262     case ReparentNotify:
263       {
264         XReparentEvent *xre = &xevent->xreparent;
265         gboolean was_embedded = priv->socket_window != NULL;
266
267         GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
268
269         return_val = GDK_FILTER_REMOVE;
270         
271         g_object_ref (plug);
272         
273         if (was_embedded)
274           {
275             /* End of embedding protocol for previous socket */
276             
277             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
278             /* FIXME: race if we remove from another socket and
279              * then add to a local window before we get notification
280              * Probably need check in _gtk_plug_add_to_socket
281              */
282
283             if (xre->parent != GDK_WINDOW_XWINDOW (priv->socket_window))
284               {
285                 GtkWidget *widget = GTK_WIDGET (plug);
286
287                 gdk_window_set_user_data (priv->socket_window, NULL);
288                 g_object_unref (priv->socket_window);
289                 priv->socket_window = NULL;
290
291                 /* Emit a delete window, as if the user attempted
292                  * to close the toplevel. Simple as to how we
293                  * handle WM_DELETE_WINDOW, if it isn't handled
294                  * we destroy the widget. BUt only do this if
295                  * we are being reparented to the root window.
296                  * Moving from one embedder to another should
297                  * be invisible to the app.
298                  */
299
300                 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
301                   {
302                     GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
303                     _gtk_plug_send_delete_event (widget);
304
305                     g_object_notify (G_OBJECT (plug), "embedded");
306                   }
307               }
308             else
309               goto done;
310           }
311
312         if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
313           {
314             /* Start of embedding protocol */
315
316             GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
317
318             priv->socket_window = gdk_window_lookup_for_display (display, xre->parent);
319             if (priv->socket_window)
320               {
321                 gpointer user_data = NULL;
322                 gdk_window_get_user_data (priv->socket_window, &user_data);
323
324                 if (user_data)
325                   {
326                     g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
327                     priv->socket_window = NULL;
328                     break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
329                   }
330
331                 g_object_ref (priv->socket_window);
332               }
333             else
334               {
335                 priv->socket_window = gdk_window_foreign_new_for_display (display, xre->parent);
336                 if (!priv->socket_window) /* Already gone */
337                   break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
338               }
339
340             _gtk_plug_add_all_grabbed_keys (plug);
341
342             if (!was_embedded)
343               g_signal_emit_by_name (plug, "embedded");
344
345             g_object_notify (G_OBJECT (plug), "embedded");
346           }
347
348       done:
349         g_object_unref (plug);
350         
351         break;
352       }
353
354 #ifdef XINPUT_2
355     case KeyPress:
356     case KeyRelease:
357       {
358         static GdkDeviceManager *core_device_manager = NULL;
359         GdkDeviceManager *device_manager;
360         GdkEvent *translated_event;
361         GList *devices, *d;
362         GdkDevice *keyboard = NULL;
363
364         device_manager = gdk_display_get_device_manager (display);
365
366         /* bail out if the device manager already
367          * interprets core keyboard events.
368          */
369         if (!GDK_IS_DEVICE_MANAGER_XI2 (device_manager))
370           return GDK_FILTER_CONTINUE;
371
372         /* Find out the first keyboard device, the
373          * generated event will be assigned to it.
374          */
375         devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
376
377         for (d = devices; d; d = d->next)
378           {
379             GdkDevice *device = d->data;
380
381             if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
382               keyboard = device;
383           }
384
385         g_list_free (devices);
386
387         if (!keyboard)
388           return GDK_FILTER_CONTINUE;
389
390         /* This is a crude hack so key events
391          * are interpreted as if there was a
392          * GdkDeviceManagerCore available.
393          */
394         if (G_UNLIKELY (!core_device_manager))
395           core_device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
396                                          "display", display,
397                                          NULL);
398
399         translated_event = gdk_event_translator_translate (GDK_EVENT_TRANSLATOR (core_device_manager), display, xevent);
400         gdk_event_set_device (translated_event, keyboard);
401
402         gtk_main_do_event (translated_event);
403         gdk_event_free (translated_event);
404
405         return_val = GDK_FILTER_REMOVE;
406       }
407 #endif
408     }
409
410   return return_val;
411 }