1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
19 /* By Owen Taylor <otaylor@gtk.org> 98/4/4 */
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/.
32 /* Hack to have keyboard events interpreted
33 * regardless of the default device manager
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
44 #include "gtkmarshalers.h"
46 #include "gtkprivate.h"
47 #include "gtkplugprivate.h"
52 #include "gtkxembed.h"
54 static void xembed_set_info (GdkWindow *window,
58 _gtk_plug_windowing_get_id (GtkPlug *plug)
60 return GDK_WINDOW_XWINDOW (gtk_widget_get_window (GTK_WIDGET (plug)));
64 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
66 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
70 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
72 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
76 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
78 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
82 _gtk_plug_windowing_set_focus (GtkPlug *plug)
84 GtkPlugPrivate *priv = plug->priv;
86 _gtk_xembed_send_message (priv->socket_window,
87 XEMBED_REQUEST_FOCUS, 0, 0, 0);
91 _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
92 guint accelerator_key,
93 GdkModifierType accelerator_mods)
95 GtkPlugPrivate *priv = plug->priv;
97 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
98 accelerator_key, accelerator_mods);
102 _gtk_plug_windowing_remove_grabbed_key (GtkPlug *plug,
103 guint accelerator_key,
104 GdkModifierType accelerator_mods)
106 GtkPlugPrivate *priv = plug->priv;
108 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
109 accelerator_key, accelerator_mods);
113 _gtk_plug_windowing_focus_to_parent (GtkPlug *plug,
114 GtkDirectionType direction)
116 GtkPlugPrivate *priv = plug->priv;
117 XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
123 case GTK_DIR_TAB_BACKWARD:
124 message = XEMBED_FOCUS_PREV;
128 case GTK_DIR_TAB_FORWARD:
129 message = XEMBED_FOCUS_NEXT;
133 _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
137 xembed_set_info (GdkWindow *window,
140 GdkDisplay *display = gdk_window_get_display (window);
141 unsigned long buffer[2];
143 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
145 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
148 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
149 GDK_WINDOW_XWINDOW (window),
150 xembed_info_atom, xembed_info_atom, 32,
152 (unsigned char *)buffer, 2);
156 handle_xembed_message (GtkPlug *plug,
157 XEmbedMessageType message,
163 GtkWindow *window = GTK_WINDOW (plug);
165 GTK_NOTE (PLUGSOCKET,
166 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
170 case XEMBED_EMBEDDED_NOTIFY:
172 case XEMBED_WINDOW_ACTIVATE:
173 _gtk_window_set_is_active (window, TRUE);
175 case XEMBED_WINDOW_DEACTIVATE:
176 _gtk_window_set_is_active (window, FALSE);
179 case XEMBED_MODALITY_ON:
180 _gtk_plug_handle_modality_on (plug);
182 case XEMBED_MODALITY_OFF:
183 _gtk_plug_handle_modality_off (plug);
186 case XEMBED_FOCUS_IN:
187 _gtk_window_set_has_toplevel_focus (window, TRUE);
190 case XEMBED_FOCUS_FIRST:
191 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
193 case XEMBED_FOCUS_LAST:
194 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
196 case XEMBED_FOCUS_CURRENT:
201 case XEMBED_FOCUS_OUT:
202 _gtk_window_set_has_toplevel_focus (window, FALSE);
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));
217 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
223 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
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;
233 GdkFilterReturn return_val;
235 return_val = GDK_FILTER_CONTINUE;
237 switch (xevent->type)
240 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
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 ();
251 return_val = GDK_FILTER_REMOVE;
253 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
255 /* We filter these out because we take being reparented back to the
256 * root window as the reliable end of the embedding protocol
259 return_val = GDK_FILTER_REMOVE;
264 XReparentEvent *xre = &xevent->xreparent;
265 gboolean was_embedded = priv->socket_window != NULL;
267 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
269 return_val = GDK_FILTER_REMOVE;
275 /* End of embedding protocol for previous socket */
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
283 if (xre->parent != GDK_WINDOW_XWINDOW (priv->socket_window))
285 GtkWidget *widget = GTK_WIDGET (plug);
287 gdk_window_set_user_data (priv->socket_window, NULL);
288 g_object_unref (priv->socket_window);
289 priv->socket_window = NULL;
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.
300 if (xre->parent == GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
302 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
303 _gtk_plug_send_delete_event (widget);
305 g_object_notify (G_OBJECT (plug), "embedded");
312 if (xre->parent != GDK_WINDOW_XWINDOW (gdk_screen_get_root_window (screen)))
314 /* Start of embedding protocol */
316 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
318 priv->socket_window = gdk_window_lookup_for_display (display, xre->parent);
319 if (priv->socket_window)
321 gpointer user_data = NULL;
322 gdk_window_get_user_data (priv->socket_window, &user_data);
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 */
331 g_object_ref (priv->socket_window);
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 */
340 _gtk_plug_add_all_grabbed_keys (plug);
343 g_signal_emit_by_name (plug, "embedded");
345 g_object_notify (G_OBJECT (plug), "embedded");
349 g_object_unref (plug);
358 static GdkDeviceManager *core_device_manager = NULL;
359 GdkDeviceManager *device_manager;
360 GdkEvent *translated_event;
362 GdkDevice *keyboard = NULL;
364 device_manager = gdk_display_get_device_manager (display);
366 /* bail out if the device manager already
367 * interprets core keyboard events.
369 if (!GDK_IS_DEVICE_MANAGER_XI2 (device_manager))
370 return GDK_FILTER_CONTINUE;
372 /* Find out the first keyboard device, the
373 * generated event will be assigned to it.
375 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
377 for (d = devices; d; d = d->next)
379 GdkDevice *device = d->data;
381 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
385 g_list_free (devices);
388 return GDK_FILTER_CONTINUE;
390 /* This is a crude hack so key events
391 * are interpreted as if there was a
392 * GdkDeviceManagerCore available.
394 if (G_UNLIKELY (!core_device_manager))
395 core_device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
399 translated_event = gdk_event_translator_translate (GDK_EVENT_TRANSLATOR (core_device_manager), display, xevent);
400 gdk_event_set_device (translated_event, keyboard);
402 gtk_main_do_event (translated_event);
403 gdk_event_free (translated_event);
405 return_val = GDK_FILTER_REMOVE;