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/.
33 /* Hack to have keyboard events interpreted
34 * regardless of the default device manager
36 #define GDK_COMPILATION
37 #include "x11/gdkdevicemanager-core.h"
38 #include "x11/gdkdevicemanager-xi2.h"
39 #include "x11/gdkeventtranslator.h"
40 #undef GDK_COMPILATION
46 #include "gtkmarshalers.h"
48 #include "gtkprivate.h"
49 #include "gtkplugprivate.h"
50 #include "gtkwindowprivate.h"
55 #include "gtkxembed.h"
57 static void xembed_set_info (GdkWindow *window,
61 _gtk_plug_windowing_get_id (GtkPlug *plug)
63 return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
67 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
69 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
73 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
75 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
79 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
81 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
85 _gtk_plug_windowing_set_focus (GtkPlug *plug)
87 GtkPlugPrivate *priv = plug->priv;
89 _gtk_xembed_send_message (priv->socket_window,
90 XEMBED_REQUEST_FOCUS, 0, 0, 0);
94 _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
95 guint accelerator_key,
96 GdkModifierType accelerator_mods)
98 GtkPlugPrivate *priv = plug->priv;
100 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
101 accelerator_key, accelerator_mods);
105 _gtk_plug_windowing_remove_grabbed_key (GtkPlug *plug,
106 guint accelerator_key,
107 GdkModifierType accelerator_mods)
109 GtkPlugPrivate *priv = plug->priv;
111 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
112 accelerator_key, accelerator_mods);
116 _gtk_plug_windowing_focus_to_parent (GtkPlug *plug,
117 GtkDirectionType direction)
119 GtkPlugPrivate *priv = plug->priv;
120 XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
126 case GTK_DIR_TAB_BACKWARD:
127 message = XEMBED_FOCUS_PREV;
131 case GTK_DIR_TAB_FORWARD:
132 message = XEMBED_FOCUS_NEXT;
136 _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
140 xembed_set_info (GdkWindow *window,
143 GdkDisplay *display = gdk_window_get_display (window);
144 unsigned long buffer[2];
146 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
148 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
151 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
152 GDK_WINDOW_XID (window),
153 xembed_info_atom, xembed_info_atom, 32,
155 (unsigned char *)buffer, 2);
159 handle_xembed_message (GtkPlug *plug,
160 XEmbedMessageType message,
166 GtkWindow *window = GTK_WINDOW (plug);
168 GTK_NOTE (PLUGSOCKET,
169 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
173 case XEMBED_EMBEDDED_NOTIFY:
175 case XEMBED_WINDOW_ACTIVATE:
176 _gtk_window_set_is_active (window, TRUE);
178 case XEMBED_WINDOW_DEACTIVATE:
179 _gtk_window_set_is_active (window, FALSE);
182 case XEMBED_MODALITY_ON:
183 _gtk_plug_handle_modality_on (plug);
185 case XEMBED_MODALITY_OFF:
186 _gtk_plug_handle_modality_off (plug);
189 case XEMBED_FOCUS_IN:
190 _gtk_window_set_has_toplevel_focus (window, TRUE);
193 case XEMBED_FOCUS_FIRST:
194 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
196 case XEMBED_FOCUS_LAST:
197 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
199 case XEMBED_FOCUS_CURRENT:
204 case XEMBED_FOCUS_OUT:
205 _gtk_window_set_has_toplevel_focus (window, FALSE);
208 case XEMBED_GRAB_KEY:
209 case XEMBED_UNGRAB_KEY:
210 case XEMBED_GTK_GRAB_KEY:
211 case XEMBED_GTK_UNGRAB_KEY:
212 case XEMBED_REQUEST_FOCUS:
213 case XEMBED_FOCUS_NEXT:
214 case XEMBED_FOCUS_PREV:
215 g_warning ("GtkPlug: Invalid _XEMBED message %s received", _gtk_xembed_message_name (message));
220 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
226 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
230 GdkScreen *screen = gdk_window_get_screen (event->any.window);
231 GdkDisplay *display = gdk_screen_get_display (screen);
232 GtkPlug *plug = GTK_PLUG (data);
233 GtkPlugPrivate *priv = plug->priv;
234 XEvent *xevent = (XEvent *)gdk_xevent;
236 GdkFilterReturn return_val;
238 return_val = GDK_FILTER_CONTINUE;
240 switch (xevent->type)
243 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
245 _gtk_xembed_push_message (xevent);
246 handle_xembed_message (plug,
247 xevent->xclient.data.l[1],
248 xevent->xclient.data.l[2],
249 xevent->xclient.data.l[3],
250 xevent->xclient.data.l[4],
251 xevent->xclient.data.l[0]);
252 _gtk_xembed_pop_message ();
254 return_val = GDK_FILTER_REMOVE;
256 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
258 /* We filter these out because we take being reparented back to the
259 * root window as the reliable end of the embedding protocol
262 return_val = GDK_FILTER_REMOVE;
267 XReparentEvent *xre = &xevent->xreparent;
268 gboolean was_embedded = priv->socket_window != NULL;
270 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
272 return_val = GDK_FILTER_REMOVE;
278 /* End of embedding protocol for previous socket */
280 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: end of embedding"));
281 /* FIXME: race if we remove from another socket and
282 * then add to a local window before we get notification
283 * Probably need check in _gtk_plug_add_to_socket
286 if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
288 GtkWidget *widget = GTK_WIDGET (plug);
290 gdk_window_set_user_data (priv->socket_window, NULL);
291 g_object_unref (priv->socket_window);
292 priv->socket_window = NULL;
294 /* Emit a delete window, as if the user attempted
295 * to close the toplevel. Simple as to how we
296 * handle WM_DELETE_WINDOW, if it isn't handled
297 * we destroy the widget. BUt only do this if
298 * we are being reparented to the root window.
299 * Moving from one embedder to another should
300 * be invisible to the app.
303 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
305 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
306 _gtk_plug_send_delete_event (widget);
308 g_object_notify (G_OBJECT (plug), "embedded");
315 if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
317 /* Start of embedding protocol */
319 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
321 priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
322 if (priv->socket_window)
324 gpointer user_data = NULL;
325 gdk_window_get_user_data (priv->socket_window, &user_data);
329 g_warning (G_STRLOC "Plug reparented unexpectedly into window in the same process");
330 priv->socket_window = NULL;
331 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
334 g_object_ref (priv->socket_window);
338 priv->socket_window = gdk_x11_window_foreign_new_for_display (display, xre->parent);
339 if (!priv->socket_window) /* Already gone */
340 break; /* FIXME: shouldn't this unref the plug? i.e. "goto done;" instead */
343 _gtk_plug_add_all_grabbed_keys (plug);
346 g_signal_emit_by_name (plug, "embedded");
348 g_object_notify (G_OBJECT (plug), "embedded");
352 g_object_unref (plug);
358 /* FIXME: this needs some X11 backend api to do things
364 static GdkDeviceManager *core_device_manager = NULL;
365 GdkDeviceManager *device_manager;
366 GdkEvent *translated_event;
368 GdkDevice *keyboard = NULL;
370 device_manager = gdk_display_get_device_manager (display);
372 /* bail out if the device manager already
373 * interprets core keyboard events.
375 if (!GDK_IS_DEVICE_MANAGER_XI2 (device_manager))
376 return GDK_FILTER_CONTINUE;
378 /* Find out the first keyboard device, the
379 * generated event will be assigned to it.
381 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
383 for (d = devices; d; d = d->next)
385 GdkDevice *device = d->data;
387 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
391 g_list_free (devices);
394 return GDK_FILTER_CONTINUE;
396 /* This is a crude hack so key events
397 * are interpreted as if there was a
398 * GdkDeviceManagerCore available.
400 if (G_UNLIKELY (!core_device_manager))
401 core_device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
405 translated_event = gdk_event_translator_translate (GDK_EVENT_TRANSLATOR (core_device_manager), display, xevent);
406 gdk_event_set_device (translated_event, keyboard);
408 gtk_main_do_event (translated_event);
409 gdk_event_free (translated_event);
411 return_val = GDK_FILTER_REMOVE;