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.
21 /*#include <config.h>*/
24 #include "eggtrayicon.h"
27 #include <X11/Xatom.h>
32 #define SYSTEM_TRAY_REQUEST_DOCK 0
33 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
34 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
36 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
37 #define SYSTEM_TRAY_ORIENTATION_VERT 1
44 static GtkPlugClass *parent_class = NULL;
46 static void egg_tray_icon_init (EggTrayIcon *icon);
47 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
49 static void egg_tray_icon_get_property (GObject *object,
54 static void egg_tray_icon_realize (GtkWidget *widget);
55 static void egg_tray_icon_unrealize (GtkWidget *widget);
57 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon,
58 gboolean dock_if_realized);
59 static void egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon);
62 egg_tray_icon_get_type (void)
64 static GType our_type = 0;
68 our_type = g_type_from_name("EggTrayIcon");
72 static const GTypeInfo our_info =
74 sizeof (EggTrayIconClass),
76 (GBaseFinalizeFunc) NULL,
77 (GClassInitFunc) egg_tray_icon_class_init,
78 NULL, /* class_finalize */
79 NULL, /* class_data */
82 (GInstanceInitFunc) egg_tray_icon_init
85 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
87 else if (parent_class == NULL)
89 /* we're reheating the old class from a previous instance - engage ugly hack =( */
90 egg_tray_icon_class_init((EggTrayIconClass *)g_type_class_peek(our_type));
98 egg_tray_icon_init (EggTrayIcon *icon)
101 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
103 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
107 egg_tray_icon_class_init (EggTrayIconClass *klass)
109 GObjectClass *gobject_class = (GObjectClass *)klass;
110 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
112 parent_class = g_type_class_peek_parent (klass);
114 gobject_class->get_property = egg_tray_icon_get_property;
116 widget_class->realize = egg_tray_icon_realize;
117 widget_class->unrealize = egg_tray_icon_unrealize;
119 g_object_class_install_property (gobject_class,
121 g_param_spec_enum ("orientation",
123 _("The orientation of the tray."),
124 GTK_TYPE_ORIENTATION,
125 GTK_ORIENTATION_HORIZONTAL,
130 egg_tray_icon_get_property (GObject *object,
135 EggTrayIcon *icon = EGG_TRAY_ICON (object);
139 case PROP_ORIENTATION:
140 g_value_set_enum (value, icon->orientation);
143 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
149 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
162 g_assert (icon->manager_window != None);
164 #if GTK_CHECK_VERSION(2,1,0)
165 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
167 xdisplay = gdk_display;
170 gdk_error_trap_push ();
172 result = XGetWindowProperty (xdisplay,
173 icon->manager_window,
174 icon->orientation_atom,
177 &type, &format, &nitems,
178 &bytes_after, &(prop.prop_ch));
179 error = gdk_error_trap_pop ();
181 if (error || result != Success)
184 if (type == XA_CARDINAL)
186 GtkOrientation orientation;
188 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
189 GTK_ORIENTATION_HORIZONTAL :
190 GTK_ORIENTATION_VERTICAL;
192 if (icon->orientation != orientation)
194 icon->orientation = orientation;
196 g_object_notify (G_OBJECT (icon), "orientation");
204 static GdkFilterReturn
205 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
207 EggTrayIcon *icon = user_data;
208 XEvent *xev = (XEvent *)xevent;
210 if (xev->xany.type == ClientMessage &&
211 xev->xclient.message_type == icon->manager_atom &&
212 xev->xclient.data.l[1] == icon->selection_atom)
214 egg_tray_icon_update_manager_window (icon, TRUE);
216 else if (xev->xany.window == icon->manager_window)
218 if (xev->xany.type == PropertyNotify &&
219 xev->xproperty.atom == icon->orientation_atom)
221 egg_tray_icon_get_orientation_property (icon);
223 if (xev->xany.type == DestroyNotify)
225 egg_tray_icon_manager_window_destroyed (icon);
229 return GDK_FILTER_CONTINUE;
233 egg_tray_icon_unrealize (GtkWidget *widget)
235 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
236 GdkWindow *root_window;
238 if (icon->manager_window != None)
242 #if GTK_CHECK_VERSION(2,1,0)
243 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
244 icon->manager_window);
246 gdkwin = gdk_window_lookup (icon->manager_window);
249 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
252 #if GTK_CHECK_VERSION(2,1,0)
253 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
255 root_window = gdk_window_lookup (gdk_x11_get_default_root_xwindow ());
258 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
260 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
261 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
265 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
272 XClientMessageEvent ev;
275 ev.type = ClientMessage;
277 ev.message_type = icon->system_tray_opcode_atom;
279 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
280 ev.data.l[1] = message;
281 ev.data.l[2] = data1;
282 ev.data.l[3] = data2;
283 ev.data.l[4] = data3;
285 #if GTK_CHECK_VERSION(2,1,0)
286 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
288 display = gdk_display;
291 gdk_error_trap_push ();
293 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
294 XSync (display, False);
295 gdk_error_trap_pop ();
299 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
301 egg_tray_icon_send_manager_message (icon,
302 SYSTEM_TRAY_REQUEST_DOCK,
303 icon->manager_window,
304 gtk_plug_get_id (GTK_PLUG (icon)),
309 egg_tray_icon_update_manager_window (EggTrayIcon *icon,
310 gboolean dock_if_realized)
314 if (icon->manager_window != None)
317 #if GTK_CHECK_VERSION(2,1,0)
318 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
320 xdisplay = gdk_display;
323 XGrabServer (xdisplay);
325 icon->manager_window = XGetSelectionOwner (xdisplay,
326 icon->selection_atom);
328 if (icon->manager_window != None)
329 XSelectInput (xdisplay,
330 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
332 XUngrabServer (xdisplay);
335 if (icon->manager_window != None)
339 #if GTK_CHECK_VERSION(2,1,0)
340 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
341 icon->manager_window);
343 gdkwin = gdk_window_lookup (icon->manager_window);
346 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
348 if (dock_if_realized && GTK_WIDGET_REALIZED (icon))
349 egg_tray_icon_send_dock_request (icon);
351 egg_tray_icon_get_orientation_property (icon);
356 egg_tray_icon_manager_window_destroyed (EggTrayIcon *icon)
360 g_return_if_fail (icon->manager_window != None);
362 #if GTK_CHECK_VERSION(2,1,0)
363 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
364 icon->manager_window);
366 gdkwin = gdk_window_lookup (icon->manager_window);
369 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
371 icon->manager_window = None;
373 egg_tray_icon_update_manager_window (icon, TRUE);
377 egg_tray_icon_realize (GtkWidget *widget)
379 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
383 GdkWindow *root_window;
385 if (GTK_WIDGET_CLASS (parent_class)->realize)
386 GTK_WIDGET_CLASS (parent_class)->realize (widget);
388 #if GTK_CHECK_VERSION(2,1,0)
389 screen = gdk_screen_get_number (gtk_widget_get_screen (widget));
390 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (widget));
392 screen = XScreenNumberOfScreen (DefaultScreenOfDisplay (gdk_display));
393 xdisplay = gdk_display;
396 /* Now see if there's a manager window around */
397 g_snprintf (buffer, sizeof (buffer),
398 "_NET_SYSTEM_TRAY_S%d",
401 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
403 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
405 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
406 "_NET_SYSTEM_TRAY_OPCODE",
409 icon->orientation_atom = XInternAtom (xdisplay,
410 "_NET_SYSTEM_TRAY_ORIENTATION",
413 egg_tray_icon_update_manager_window (icon, FALSE);
414 egg_tray_icon_send_dock_request (icon);
416 #if GTK_CHECK_VERSION(2,1,0)
417 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
419 root_window = gdk_window_lookup (gdk_x11_get_default_root_xwindow ());
422 /* Add a root window filter so that we get changes on MANAGER */
423 gdk_window_add_filter (root_window,
424 egg_tray_icon_manager_filter, icon);
427 #if GTK_CHECK_VERSION(2,1,0)
429 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
431 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
433 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
438 egg_tray_icon_new (const gchar *name)
440 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
444 egg_tray_icon_send_message (EggTrayIcon *icon,
446 const gchar *message,
451 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
452 g_return_val_if_fail (timeout >= 0, 0);
453 g_return_val_if_fail (message != NULL, 0);
455 if (icon->manager_window == None)
459 len = strlen (message);
461 stamp = icon->stamp++;
463 /* Get ready to send the message */
464 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
465 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
466 timeout, len, stamp);
468 /* Now to send the actual message */
469 gdk_error_trap_push ();
472 XClientMessageEvent ev;
475 #if GTK_CHECK_VERSION(2,1,0)
476 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
478 xdisplay = gdk_display;
481 ev.type = ClientMessage;
482 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
484 ev.message_type = XInternAtom (xdisplay,
485 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
488 memcpy (&ev.data, message, 20);
494 memcpy (&ev.data, message, len);
498 XSendEvent (xdisplay,
499 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
500 XSync (xdisplay, False);
502 gdk_error_trap_pop ();
508 egg_tray_icon_cancel_message (EggTrayIcon *icon,
511 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
512 g_return_if_fail (id > 0);
514 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
515 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
520 egg_tray_icon_get_orientation (EggTrayIcon *icon)
522 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
524 return icon->orientation;