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.
22 #include "eggtrayicon.h"
25 #include <X11/Xatom.h>
26 #define SYSTEM_TRAY_REQUEST_DOCK 0
27 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
28 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
30 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
31 #define SYSTEM_TRAY_ORIENTATION_VERT 1
38 static GtkPlugClass *parent_class = NULL;
40 static void egg_tray_icon_init (EggTrayIcon *icon);
41 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
43 static void egg_tray_icon_get_property (GObject *object,
48 static void egg_tray_icon_realize (GtkWidget *widget);
49 static void egg_tray_icon_unrealize (GtkWidget *widget);
51 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
54 egg_tray_icon_get_type (void)
56 static GType our_type = 0;
60 static const GTypeInfo our_info =
62 sizeof (EggTrayIconClass),
64 (GBaseFinalizeFunc) NULL,
65 (GClassInitFunc) egg_tray_icon_class_init,
66 NULL, /* class_finalize */
67 NULL, /* class_data */
70 (GInstanceInitFunc) egg_tray_icon_init
73 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
80 egg_tray_icon_init (EggTrayIcon *icon)
83 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
85 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
89 egg_tray_icon_class_init (EggTrayIconClass *klass)
91 GObjectClass *gobject_class = (GObjectClass *)klass;
92 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
94 parent_class = g_type_class_peek_parent (klass);
96 gobject_class->get_property = egg_tray_icon_get_property;
98 widget_class->realize = egg_tray_icon_realize;
99 widget_class->unrealize = egg_tray_icon_unrealize;
101 g_object_class_install_property (gobject_class,
103 g_param_spec_enum ("orientation",
105 _("The orientation of the tray."),
106 GTK_TYPE_ORIENTATION,
107 GTK_ORIENTATION_HORIZONTAL,
112 egg_tray_icon_get_property (GObject *object,
117 EggTrayIcon *icon = EGG_TRAY_ICON (object);
121 case PROP_ORIENTATION:
122 g_value_set_enum (value, icon->orientation);
125 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
144 g_assert (icon->manager_window != None);
146 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
148 gdk_error_trap_push ();
150 result = XGetWindowProperty (xdisplay,
151 icon->manager_window,
152 icon->orientation_atom,
155 &type, &format, &nitems,
156 &bytes_after, &(prop.prop_ch));
157 error = gdk_error_trap_pop ();
159 if (error || result != Success)
162 if (type == XA_CARDINAL)
164 GtkOrientation orientation;
166 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
167 GTK_ORIENTATION_HORIZONTAL :
168 GTK_ORIENTATION_VERTICAL;
170 if (icon->orientation != orientation)
172 icon->orientation = orientation;
174 g_object_notify (G_OBJECT (icon), "orientation");
182 static GdkFilterReturn
183 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
185 EggTrayIcon *icon = user_data;
186 XEvent *xev = (XEvent *)xevent;
188 if (xev->xany.type == ClientMessage &&
189 xev->xclient.message_type == icon->manager_atom &&
190 xev->xclient.data.l[1] == icon->selection_atom)
192 egg_tray_icon_update_manager_window (icon);
194 else if (xev->xany.window == icon->manager_window)
196 if (xev->xany.type == PropertyNotify &&
197 xev->xproperty.atom == icon->orientation_atom)
199 egg_tray_icon_get_orientation_property (icon);
201 if (xev->xany.type == DestroyNotify)
203 egg_tray_icon_update_manager_window (icon);
207 return GDK_FILTER_CONTINUE;
211 egg_tray_icon_unrealize (GtkWidget *widget)
213 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
214 GdkWindow *root_window;
216 if (icon->manager_window != None)
220 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
221 icon->manager_window);
223 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
226 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
228 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
230 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
231 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
235 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
242 XClientMessageEvent ev;
245 ev.type = ClientMessage;
247 ev.message_type = icon->system_tray_opcode_atom;
249 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
250 ev.data.l[1] = message;
251 ev.data.l[2] = data1;
252 ev.data.l[3] = data2;
253 ev.data.l[4] = data3;
255 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
257 gdk_error_trap_push ();
259 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
260 XSync (display, False);
261 gdk_error_trap_pop ();
265 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
267 egg_tray_icon_send_manager_message (icon,
268 SYSTEM_TRAY_REQUEST_DOCK,
269 icon->manager_window,
270 gtk_plug_get_id (GTK_PLUG (icon)),
275 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
279 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
281 if (icon->manager_window != None)
285 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
286 icon->manager_window);
288 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
291 XGrabServer (xdisplay);
293 icon->manager_window = XGetSelectionOwner (xdisplay,
294 icon->selection_atom);
296 if (icon->manager_window != None)
297 XSelectInput (xdisplay,
298 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
300 XUngrabServer (xdisplay);
303 if (icon->manager_window != None)
307 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
308 icon->manager_window);
310 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
312 /* Send a request that we'd like to dock */
313 egg_tray_icon_send_dock_request (icon);
315 egg_tray_icon_get_orientation_property (icon);
320 egg_tray_icon_realize (GtkWidget *widget)
322 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
327 GdkWindow *root_window;
329 if (GTK_WIDGET_CLASS (parent_class)->realize)
330 GTK_WIDGET_CLASS (parent_class)->realize (widget);
332 screen = gtk_widget_get_screen (widget);
333 display = gdk_screen_get_display (screen);
334 xdisplay = gdk_x11_display_get_xdisplay (display);
336 /* Now see if there's a manager window around */
337 g_snprintf (buffer, sizeof (buffer),
338 "_NET_SYSTEM_TRAY_S%d",
339 gdk_screen_get_number (screen));
341 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
343 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
345 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
346 "_NET_SYSTEM_TRAY_OPCODE",
349 icon->orientation_atom = XInternAtom (xdisplay,
350 "_NET_SYSTEM_TRAY_ORIENTATION",
353 egg_tray_icon_update_manager_window (icon);
355 root_window = gdk_screen_get_root_window (screen);
357 /* Add a root window filter so that we get changes on MANAGER */
358 gdk_window_add_filter (root_window,
359 egg_tray_icon_manager_filter, icon);
363 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
368 display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
369 screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
371 return egg_tray_icon_new_for_screen (screen, name);
375 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
377 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
379 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
383 egg_tray_icon_new (const gchar *name)
385 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
389 egg_tray_icon_send_message (EggTrayIcon *icon,
391 const gchar *message,
396 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
397 g_return_val_if_fail (timeout >= 0, 0);
398 g_return_val_if_fail (message != NULL, 0);
400 if (icon->manager_window == None)
404 len = strlen (message);
406 stamp = icon->stamp++;
408 /* Get ready to send the message */
409 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
410 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
411 timeout, len, stamp);
413 /* Now to send the actual message */
414 gdk_error_trap_push ();
417 XClientMessageEvent ev;
420 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
422 ev.type = ClientMessage;
423 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
425 ev.message_type = XInternAtom (xdisplay,
426 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
429 memcpy (&ev.data, message, 20);
435 memcpy (&ev.data, message, len);
439 XSendEvent (xdisplay,
440 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
441 XSync (xdisplay, False);
443 gdk_error_trap_pop ();
449 egg_tray_icon_cancel_message (EggTrayIcon *icon,
452 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
453 g_return_if_fail (id > 0);
455 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
456 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
461 egg_tray_icon_get_orientation (EggTrayIcon *icon)
463 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
465 return icon->orientation;