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 "eggtrayicon.h"
30 #include <X11/Xatom.h>
31 #define SYSTEM_TRAY_REQUEST_DOCK 0
32 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
33 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
35 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
36 #define SYSTEM_TRAY_ORIENTATION_VERT 1
43 static GtkPlugClass *parent_class = NULL;
45 static void egg_tray_icon_init (EggTrayIcon *icon);
46 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
48 static void egg_tray_icon_get_property (GObject *object,
53 static void egg_tray_icon_realize (GtkWidget *widget);
54 static void egg_tray_icon_unrealize (GtkWidget *widget);
56 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
59 egg_tray_icon_get_type (void)
61 static GType our_type = 0;
65 static const GTypeInfo our_info =
67 sizeof (EggTrayIconClass),
69 (GBaseFinalizeFunc) NULL,
70 (GClassInitFunc) egg_tray_icon_class_init,
71 NULL, /* class_finalize */
72 NULL, /* class_data */
75 (GInstanceInitFunc) egg_tray_icon_init
78 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
85 egg_tray_icon_init (EggTrayIcon *icon)
88 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
90 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
94 egg_tray_icon_class_init (EggTrayIconClass *klass)
96 GObjectClass *gobject_class = (GObjectClass *)klass;
97 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
99 parent_class = g_type_class_peek_parent (klass);
101 gobject_class->get_property = egg_tray_icon_get_property;
103 widget_class->realize = egg_tray_icon_realize;
104 widget_class->unrealize = egg_tray_icon_unrealize;
106 g_object_class_install_property (gobject_class,
108 g_param_spec_enum ("orientation",
110 _("The orientation of the tray."),
111 GTK_TYPE_ORIENTATION,
112 GTK_ORIENTATION_HORIZONTAL,
117 egg_tray_icon_get_property (GObject *object,
122 EggTrayIcon *icon = EGG_TRAY_ICON (object);
126 case PROP_ORIENTATION:
127 g_value_set_enum (value, icon->orientation);
130 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
136 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
149 g_assert (icon->manager_window != None);
151 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
153 gdk_error_trap_push ();
155 result = XGetWindowProperty (xdisplay,
156 icon->manager_window,
157 icon->orientation_atom,
160 &type, &format, &nitems,
161 &bytes_after, &(prop.prop_ch));
162 error = gdk_error_trap_pop ();
164 if (error || result != Success)
167 if (type == XA_CARDINAL)
169 GtkOrientation orientation;
171 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
172 GTK_ORIENTATION_HORIZONTAL :
173 GTK_ORIENTATION_VERTICAL;
175 if (icon->orientation != orientation)
177 icon->orientation = orientation;
179 g_object_notify (G_OBJECT (icon), "orientation");
187 static GdkFilterReturn
188 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
190 EggTrayIcon *icon = user_data;
191 XEvent *xev = (XEvent *)xevent;
193 if (xev->xany.type == ClientMessage &&
194 xev->xclient.message_type == icon->manager_atom &&
195 xev->xclient.data.l[1] == icon->selection_atom)
197 egg_tray_icon_update_manager_window (icon);
199 else if (xev->xany.window == icon->manager_window)
201 if (xev->xany.type == PropertyNotify &&
202 xev->xproperty.atom == icon->orientation_atom)
204 egg_tray_icon_get_orientation_property (icon);
206 if (xev->xany.type == DestroyNotify)
208 egg_tray_icon_update_manager_window (icon);
212 return GDK_FILTER_CONTINUE;
216 egg_tray_icon_unrealize (GtkWidget *widget)
218 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
219 GdkWindow *root_window;
221 if (icon->manager_window != None)
225 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
226 icon->manager_window);
228 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
231 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
233 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
235 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
236 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
240 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
247 XClientMessageEvent ev;
250 ev.type = ClientMessage;
252 ev.message_type = icon->system_tray_opcode_atom;
254 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
255 ev.data.l[1] = message;
256 ev.data.l[2] = data1;
257 ev.data.l[3] = data2;
258 ev.data.l[4] = data3;
260 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
262 gdk_error_trap_push ();
264 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
265 XSync (display, False);
266 gdk_error_trap_pop ();
270 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
272 egg_tray_icon_send_manager_message (icon,
273 SYSTEM_TRAY_REQUEST_DOCK,
274 icon->manager_window,
275 gtk_plug_get_id (GTK_PLUG (icon)),
280 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
284 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
286 if (icon->manager_window != None)
290 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
291 icon->manager_window);
293 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
296 XGrabServer (xdisplay);
298 icon->manager_window = XGetSelectionOwner (xdisplay,
299 icon->selection_atom);
301 if (icon->manager_window != None)
302 XSelectInput (xdisplay,
303 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
305 XUngrabServer (xdisplay);
308 if (icon->manager_window != None)
312 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
313 icon->manager_window);
315 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
317 /* Send a request that we'd like to dock */
318 egg_tray_icon_send_dock_request (icon);
320 egg_tray_icon_get_orientation_property (icon);
325 egg_tray_icon_realize (GtkWidget *widget)
327 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
332 GdkWindow *root_window;
334 if (GTK_WIDGET_CLASS (parent_class)->realize)
335 GTK_WIDGET_CLASS (parent_class)->realize (widget);
337 screen = gtk_widget_get_screen (widget);
338 display = gdk_screen_get_display (screen);
339 xdisplay = gdk_x11_display_get_xdisplay (display);
341 /* Now see if there's a manager window around */
342 g_snprintf (buffer, sizeof (buffer),
343 "_NET_SYSTEM_TRAY_S%d",
344 gdk_screen_get_number (screen));
346 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
348 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
350 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
351 "_NET_SYSTEM_TRAY_OPCODE",
354 icon->orientation_atom = XInternAtom (xdisplay,
355 "_NET_SYSTEM_TRAY_ORIENTATION",
358 egg_tray_icon_update_manager_window (icon);
360 root_window = gdk_screen_get_root_window (screen);
362 /* Add a root window filter so that we get changes on MANAGER */
363 gdk_window_add_filter (root_window,
364 egg_tray_icon_manager_filter, icon);
368 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
373 display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
374 screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
376 return egg_tray_icon_new_for_screen (screen, name);
380 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
382 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
384 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
388 egg_tray_icon_new (const gchar *name)
390 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
394 egg_tray_icon_send_message (EggTrayIcon *icon,
396 const gchar *message,
401 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
402 g_return_val_if_fail (timeout >= 0, 0);
403 g_return_val_if_fail (message != NULL, 0);
405 if (icon->manager_window == None)
409 len = strlen (message);
411 stamp = icon->stamp++;
413 /* Get ready to send the message */
414 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
415 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
416 timeout, len, stamp);
418 /* Now to send the actual message */
419 gdk_error_trap_push ();
422 XClientMessageEvent ev;
425 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
427 ev.type = ClientMessage;
428 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
430 ev.message_type = XInternAtom (xdisplay,
431 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
434 memcpy (&ev.data, message, 20);
440 memcpy (&ev.data, message, len);
444 XSendEvent (xdisplay,
445 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
446 XSync (xdisplay, False);
448 gdk_error_trap_pop ();
454 egg_tray_icon_cancel_message (EggTrayIcon *icon,
457 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
458 g_return_if_fail (id > 0);
460 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
461 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
466 egg_tray_icon_get_orientation (EggTrayIcon *icon)
468 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
470 return icon->orientation;