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"
48 #include "gtkwindowprivate.h"
53 #include "gtkxembed.h"
55 static void xembed_set_info (GdkWindow *window,
59 _gtk_plug_windowing_get_id (GtkPlug *plug)
61 return GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (plug)));
65 _gtk_plug_windowing_realize_toplevel (GtkPlug *plug)
67 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
71 _gtk_plug_windowing_map_toplevel (GtkPlug *plug)
73 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), XEMBED_MAPPED);
77 _gtk_plug_windowing_unmap_toplevel (GtkPlug *plug)
79 xembed_set_info (gtk_widget_get_window (GTK_WIDGET (plug)), 0);
83 _gtk_plug_windowing_set_focus (GtkPlug *plug)
85 GtkPlugPrivate *priv = plug->priv;
87 _gtk_xembed_send_message (priv->socket_window,
88 XEMBED_REQUEST_FOCUS, 0, 0, 0);
92 _gtk_plug_windowing_add_grabbed_key (GtkPlug *plug,
93 guint accelerator_key,
94 GdkModifierType accelerator_mods)
96 GtkPlugPrivate *priv = plug->priv;
98 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_GRAB_KEY, 0,
99 accelerator_key, accelerator_mods);
103 _gtk_plug_windowing_remove_grabbed_key (GtkPlug *plug,
104 guint accelerator_key,
105 GdkModifierType accelerator_mods)
107 GtkPlugPrivate *priv = plug->priv;
109 _gtk_xembed_send_message (priv->socket_window, XEMBED_GTK_UNGRAB_KEY, 0,
110 accelerator_key, accelerator_mods);
114 _gtk_plug_windowing_focus_to_parent (GtkPlug *plug,
115 GtkDirectionType direction)
117 GtkPlugPrivate *priv = plug->priv;
118 XEmbedMessageType message = XEMBED_FOCUS_PREV; /* Quiet GCC */
124 case GTK_DIR_TAB_BACKWARD:
125 message = XEMBED_FOCUS_PREV;
129 case GTK_DIR_TAB_FORWARD:
130 message = XEMBED_FOCUS_NEXT;
134 _gtk_xembed_send_focus_message (priv->socket_window, message, 0);
138 xembed_set_info (GdkWindow *window,
141 GdkDisplay *display = gdk_window_get_display (window);
142 unsigned long buffer[2];
144 Atom xembed_info_atom = gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED_INFO");
146 buffer[0] = GTK_XEMBED_PROTOCOL_VERSION;
149 XChangeProperty (GDK_DISPLAY_XDISPLAY (display),
150 GDK_WINDOW_XID (window),
151 xembed_info_atom, xembed_info_atom, 32,
153 (unsigned char *)buffer, 2);
157 handle_xembed_message (GtkPlug *plug,
158 XEmbedMessageType message,
164 GtkWindow *window = GTK_WINDOW (plug);
166 GTK_NOTE (PLUGSOCKET,
167 g_message ("GtkPlug: %s received", _gtk_xembed_message_name (message)));
171 case XEMBED_EMBEDDED_NOTIFY:
173 case XEMBED_WINDOW_ACTIVATE:
174 _gtk_window_set_is_active (window, TRUE);
176 case XEMBED_WINDOW_DEACTIVATE:
177 _gtk_window_set_is_active (window, FALSE);
180 case XEMBED_MODALITY_ON:
181 _gtk_plug_handle_modality_on (plug);
183 case XEMBED_MODALITY_OFF:
184 _gtk_plug_handle_modality_off (plug);
187 case XEMBED_FOCUS_IN:
188 _gtk_window_set_has_toplevel_focus (window, TRUE);
191 case XEMBED_FOCUS_FIRST:
192 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_FORWARD);
194 case XEMBED_FOCUS_LAST:
195 _gtk_plug_focus_first_last (plug, GTK_DIR_TAB_BACKWARD);
197 case XEMBED_FOCUS_CURRENT:
202 case XEMBED_FOCUS_OUT:
203 _gtk_window_set_has_toplevel_focus (window, FALSE);
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));
218 g_message ("GtkPlug: Ignoring unknown _XEMBED message of type %d", message));
224 _gtk_plug_windowing_filter_func (GdkXEvent *gdk_xevent,
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;
234 GdkFilterReturn return_val;
236 return_val = GDK_FILTER_CONTINUE;
238 switch (xevent->type)
241 if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "_XEMBED"))
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 ();
252 return_val = GDK_FILTER_REMOVE;
254 else if (xevent->xclient.message_type == gdk_x11_get_xatom_by_name_for_display (display, "WM_DELETE_WINDOW"))
256 /* We filter these out because we take being reparented back to the
257 * root window as the reliable end of the embedding protocol
260 return_val = GDK_FILTER_REMOVE;
265 XReparentEvent *xre = &xevent->xreparent;
266 gboolean was_embedded = priv->socket_window != NULL;
268 GTK_NOTE (PLUGSOCKET, g_message("GtkPlug: ReparentNotify received"));
270 return_val = GDK_FILTER_REMOVE;
276 /* End of embedding protocol for previous socket */
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
284 if (xre->parent != GDK_WINDOW_XID (priv->socket_window))
286 GtkWidget *widget = GTK_WIDGET (plug);
288 gdk_window_set_user_data (priv->socket_window, NULL);
289 g_object_unref (priv->socket_window);
290 priv->socket_window = NULL;
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.
301 if (xre->parent == GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
303 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: calling gtk_plug_send_delete_event()"));
304 _gtk_plug_send_delete_event (widget);
306 g_object_notify (G_OBJECT (plug), "embedded");
313 if (xre->parent != GDK_WINDOW_XID (gdk_screen_get_root_window (screen)))
315 /* Start of embedding protocol */
317 GTK_NOTE (PLUGSOCKET, g_message ("GtkPlug: start of embedding"));
319 priv->socket_window = gdk_x11_window_lookup_for_display (display, xre->parent);
320 if (priv->socket_window)
322 gpointer user_data = NULL;
323 gdk_window_get_user_data (priv->socket_window, &user_data);
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 */
332 g_object_ref (priv->socket_window);
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 */
341 _gtk_plug_add_all_grabbed_keys (plug);
344 g_signal_emit_by_name (plug, "embedded");
346 g_object_notify (G_OBJECT (plug), "embedded");
350 g_object_unref (plug);
356 /* FIXME: this needs some X11 backend api to do things
362 static GdkDeviceManager *core_device_manager = NULL;
363 GdkDeviceManager *device_manager;
364 GdkEvent *translated_event;
366 GdkDevice *keyboard = NULL;
368 device_manager = gdk_display_get_device_manager (display);
370 /* bail out if the device manager already
371 * interprets core keyboard events.
373 if (!GDK_IS_DEVICE_MANAGER_XI2 (device_manager))
374 return GDK_FILTER_CONTINUE;
376 /* Find out the first keyboard device, the
377 * generated event will be assigned to it.
379 devices = gdk_device_manager_list_devices (device_manager, GDK_DEVICE_TYPE_MASTER);
381 for (d = devices; d; d = d->next)
383 GdkDevice *device = d->data;
385 if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
389 g_list_free (devices);
392 return GDK_FILTER_CONTINUE;
394 /* This is a crude hack so key events
395 * are interpreted as if there was a
396 * GdkDeviceManagerCore available.
398 if (G_UNLIKELY (!core_device_manager))
399 core_device_manager = g_object_new (GDK_TYPE_DEVICE_MANAGER_CORE,
403 translated_event = gdk_event_translator_translate (GDK_EVENT_TRANSLATOR (core_device_manager), display, xevent);
404 gdk_event_set_device (translated_event, keyboard);
406 gtk_main_do_event (translated_event);
407 gdk_event_free (translated_event);
409 return_val = GDK_FILTER_REMOVE;