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