1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include <glib/gi18n.h>
30 #include "eggtrayicon.h"
33 #include <X11/Xatom.h>
34 #define SYSTEM_TRAY_REQUEST_DOCK 0
35 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
36 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
38 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
39 #define SYSTEM_TRAY_ORIENTATION_VERT 1
46 static GtkPlugClass *parent_class = NULL;
48 static void egg_tray_icon_init (EggTrayIcon *icon);
49 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
51 static void egg_tray_icon_get_property (GObject *object,
56 static void egg_tray_icon_realize (GtkWidget *widget);
57 static void egg_tray_icon_unrealize (GtkWidget *widget);
59 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
62 egg_tray_icon_get_type (void)
64 static GType our_type = 0;
68 gchar *type_name = g_strdup_printf("EggTrayIcon%d", (int)time(NULL));
69 static const GTypeInfo our_info =
71 sizeof (EggTrayIconClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) egg_tray_icon_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) egg_tray_icon_init
82 our_type = g_type_register_static (GTK_TYPE_PLUG, type_name,
91 egg_tray_icon_init (EggTrayIcon *icon)
94 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
96 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
100 egg_tray_icon_class_init (EggTrayIconClass *klass)
102 GObjectClass *gobject_class = (GObjectClass *)klass;
103 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
105 parent_class = g_type_class_peek_parent (klass);
107 gobject_class->get_property = egg_tray_icon_get_property;
109 widget_class->realize = egg_tray_icon_realize;
110 widget_class->unrealize = egg_tray_icon_unrealize;
112 g_object_class_install_property (gobject_class,
114 g_param_spec_enum ("orientation",
116 _("The orientation of the tray."),
117 GTK_TYPE_ORIENTATION,
118 GTK_ORIENTATION_HORIZONTAL,
123 egg_tray_icon_get_property (GObject *object,
128 EggTrayIcon *icon = EGG_TRAY_ICON (object);
132 case PROP_ORIENTATION:
133 g_value_set_enum (value, icon->orientation);
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
155 g_assert (icon->manager_window != None);
157 #ifndef GDK_MULTIHEAD_SAFE
158 xdisplay = gdk_display;
160 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
163 gdk_error_trap_push ();
165 result = XGetWindowProperty (xdisplay,
166 icon->manager_window,
167 icon->orientation_atom,
170 &type, &format, &nitems,
171 &bytes_after, &(prop.prop_ch));
172 error = gdk_error_trap_pop ();
174 if (error || result != Success)
177 if (type == XA_CARDINAL)
179 GtkOrientation orientation;
181 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
182 GTK_ORIENTATION_HORIZONTAL :
183 GTK_ORIENTATION_VERTICAL;
185 if (icon->orientation != orientation)
187 icon->orientation = orientation;
189 g_object_notify (G_OBJECT (icon), "orientation");
197 static GdkFilterReturn
198 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
200 EggTrayIcon *icon = user_data;
201 XEvent *xev = (XEvent *)xevent;
203 if (xev->xany.type == ClientMessage &&
204 xev->xclient.message_type == icon->manager_atom &&
205 xev->xclient.data.l[1] == icon->selection_atom)
207 egg_tray_icon_update_manager_window (icon);
209 else if (xev->xany.window == icon->manager_window)
211 if (xev->xany.type == PropertyNotify &&
212 xev->xproperty.atom == icon->orientation_atom)
214 egg_tray_icon_get_orientation_property (icon);
216 if (xev->xany.type == DestroyNotify)
218 egg_tray_icon_update_manager_window (icon);
222 return GDK_FILTER_CONTINUE;
226 egg_tray_icon_unrealize (GtkWidget *widget)
228 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
229 GdkWindow *root_window;
231 if (icon->manager_window != None)
235 #ifndef GDK_MULTIHEAD_SAFE
236 gdkwin = gdk_window_lookup (icon->manager_window);
238 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
239 icon->manager_window);
242 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
245 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
247 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
249 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
250 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
254 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
261 XClientMessageEvent ev;
264 ev.type = ClientMessage;
266 ev.message_type = icon->system_tray_opcode_atom;
268 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
269 ev.data.l[1] = message;
270 ev.data.l[2] = data1;
271 ev.data.l[3] = data2;
272 ev.data.l[4] = data3;
274 #ifndef GDK_MULTIHEAD_SAFE
275 display = gdk_display;
277 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
280 gdk_error_trap_push ();
282 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
283 XSync (display, False);
284 gdk_error_trap_pop ();
288 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
290 egg_tray_icon_send_manager_message (icon,
291 SYSTEM_TRAY_REQUEST_DOCK,
292 icon->manager_window,
293 gtk_plug_get_id (GTK_PLUG (icon)),
298 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
302 #ifndef GDK_MULTIHEAD_SAFE
303 xdisplay = gdk_display;
305 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
308 if (icon->manager_window != None)
312 #ifndef GDK_MULTIHEAD_SAFE
313 gdkwin = gdk_window_lookup (icon->manager_window);
315 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
316 icon->manager_window);
319 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
322 XGrabServer (xdisplay);
324 icon->manager_window = XGetSelectionOwner (xdisplay,
325 icon->selection_atom);
327 if (icon->manager_window != None)
328 XSelectInput (xdisplay,
329 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
331 XUngrabServer (xdisplay);
334 if (icon->manager_window != None)
338 #ifndef GDK_MULTIHEAD_SAFE
339 gdkwin = gdk_window_lookup (icon->manager_window);
341 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
342 icon->manager_window);
345 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
347 /* Send a request that we'd like to dock */
348 egg_tray_icon_send_dock_request (icon);
350 egg_tray_icon_get_orientation_property (icon);
355 egg_tray_icon_realize (GtkWidget *widget)
357 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
362 GdkWindow *root_window;
364 if (GTK_WIDGET_CLASS (parent_class)->realize)
365 GTK_WIDGET_CLASS (parent_class)->realize (widget);
367 screen = gtk_widget_get_screen (widget);
368 display = gdk_screen_get_display (screen);
369 xdisplay = gdk_x11_display_get_xdisplay (display);
371 /* Now see if there's a manager window around */
372 g_snprintf (buffer, sizeof (buffer),
373 "_NET_SYSTEM_TRAY_S%d",
374 gdk_screen_get_number (screen));
376 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
378 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
380 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
381 "_NET_SYSTEM_TRAY_OPCODE",
384 icon->orientation_atom = XInternAtom (xdisplay,
385 "_NET_SYSTEM_TRAY_ORIENTATION",
388 egg_tray_icon_update_manager_window (icon);
390 root_window = gdk_screen_get_root_window (screen);
392 /* Add a root window filter so that we get changes on MANAGER */
393 gdk_window_add_filter (root_window,
394 egg_tray_icon_manager_filter, icon);
398 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
403 display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
404 screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
406 return egg_tray_icon_new_for_screen (screen, name);
410 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
412 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
414 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
418 egg_tray_icon_new (const gchar *name)
420 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
424 egg_tray_icon_send_message (EggTrayIcon *icon,
426 const gchar *message,
431 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
432 g_return_val_if_fail (timeout >= 0, 0);
433 g_return_val_if_fail (message != NULL, 0);
435 if (icon->manager_window == None)
439 len = strlen (message);
441 stamp = icon->stamp++;
443 /* Get ready to send the message */
444 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
445 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
446 timeout, len, stamp);
448 /* Now to send the actual message */
449 gdk_error_trap_push ();
452 XClientMessageEvent ev;
455 #ifndef GDK_MULTIHEAD_SAFE
456 xdisplay = gdk_display;
458 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
461 ev.type = ClientMessage;
462 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
464 ev.message_type = XInternAtom (xdisplay,
465 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
468 memcpy (&ev.data, message, 20);
474 memcpy (&ev.data, message, len);
478 XSendEvent (xdisplay,
479 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
480 XSync (xdisplay, False);
482 gdk_error_trap_pop ();
488 egg_tray_icon_cancel_message (EggTrayIcon *icon,
491 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
492 g_return_if_fail (id > 0);
494 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
495 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
500 egg_tray_icon_get_orientation (EggTrayIcon *icon)
502 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
504 return icon->orientation;