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.
26 #include <glib/gi18n.h>
29 #include "eggtrayicon.h"
32 #include <X11/Xatom.h>
33 #define SYSTEM_TRAY_REQUEST_DOCK 0
34 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
35 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
37 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
38 #define SYSTEM_TRAY_ORIENTATION_VERT 1
45 static GtkPlugClass *parent_class = NULL;
47 static void egg_tray_icon_init (EggTrayIcon *icon);
48 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
50 static void egg_tray_icon_get_property (GObject *object,
55 static void egg_tray_icon_realize (GtkWidget *widget);
56 static void egg_tray_icon_unrealize (GtkWidget *widget);
58 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
61 egg_tray_icon_get_type (void)
63 static GType our_type = 0;
67 static const GTypeInfo our_info =
69 sizeof (EggTrayIconClass),
71 (GBaseFinalizeFunc) NULL,
72 (GClassInitFunc) egg_tray_icon_class_init,
73 NULL, /* class_finalize */
74 NULL, /* class_data */
77 (GInstanceInitFunc) egg_tray_icon_init
80 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
87 egg_tray_icon_init (EggTrayIcon *icon)
90 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
92 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
96 egg_tray_icon_class_init (EggTrayIconClass *klass)
98 GObjectClass *gobject_class = (GObjectClass *)klass;
99 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
101 parent_class = g_type_class_peek_parent (klass);
103 gobject_class->get_property = egg_tray_icon_get_property;
105 widget_class->realize = egg_tray_icon_realize;
106 widget_class->unrealize = egg_tray_icon_unrealize;
108 g_object_class_install_property (gobject_class,
110 g_param_spec_enum ("orientation",
112 _("The orientation of the tray."),
113 GTK_TYPE_ORIENTATION,
114 GTK_ORIENTATION_HORIZONTAL,
119 egg_tray_icon_get_property (GObject *object,
124 EggTrayIcon *icon = EGG_TRAY_ICON (object);
128 case PROP_ORIENTATION:
129 g_value_set_enum (value, icon->orientation);
132 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
138 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
151 g_assert (icon->manager_window != None);
153 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
155 gdk_error_trap_push ();
157 result = XGetWindowProperty (xdisplay,
158 icon->manager_window,
159 icon->orientation_atom,
162 &type, &format, &nitems,
163 &bytes_after, &(prop.prop_ch));
164 error = gdk_error_trap_pop ();
166 if (error || result != Success)
169 if (type == XA_CARDINAL)
171 GtkOrientation orientation;
173 orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
174 GTK_ORIENTATION_HORIZONTAL :
175 GTK_ORIENTATION_VERTICAL;
177 if (icon->orientation != orientation)
179 icon->orientation = orientation;
181 g_object_notify (G_OBJECT (icon), "orientation");
189 static GdkFilterReturn
190 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
192 EggTrayIcon *icon = user_data;
193 XEvent *xev = (XEvent *)xevent;
195 if (xev->xany.type == ClientMessage &&
196 xev->xclient.message_type == icon->manager_atom &&
197 xev->xclient.data.l[1] == icon->selection_atom)
199 egg_tray_icon_update_manager_window (icon);
201 else if (xev->xany.window == icon->manager_window)
203 if (xev->xany.type == PropertyNotify &&
204 xev->xproperty.atom == icon->orientation_atom)
206 egg_tray_icon_get_orientation_property (icon);
208 if (xev->xany.type == DestroyNotify)
210 egg_tray_icon_update_manager_window (icon);
214 return GDK_FILTER_CONTINUE;
218 egg_tray_icon_unrealize (GtkWidget *widget)
220 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
221 GdkWindow *root_window;
223 if (icon->manager_window != None)
227 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
228 icon->manager_window);
230 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
233 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
235 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
237 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
238 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
242 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
249 XClientMessageEvent ev;
252 ev.type = ClientMessage;
254 ev.message_type = icon->system_tray_opcode_atom;
256 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
257 ev.data.l[1] = message;
258 ev.data.l[2] = data1;
259 ev.data.l[3] = data2;
260 ev.data.l[4] = data3;
262 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
264 gdk_error_trap_push ();
266 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
267 XSync (display, False);
268 gdk_error_trap_pop ();
272 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
274 egg_tray_icon_send_manager_message (icon,
275 SYSTEM_TRAY_REQUEST_DOCK,
276 icon->manager_window,
277 gtk_plug_get_id (GTK_PLUG (icon)),
282 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
286 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
288 if (icon->manager_window != None)
292 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
293 icon->manager_window);
295 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
298 XGrabServer (xdisplay);
300 icon->manager_window = XGetSelectionOwner (xdisplay,
301 icon->selection_atom);
303 if (icon->manager_window != None)
304 XSelectInput (xdisplay,
305 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
307 XUngrabServer (xdisplay);
310 if (icon->manager_window != None)
314 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
315 icon->manager_window);
317 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
319 /* Send a request that we'd like to dock */
320 egg_tray_icon_send_dock_request (icon);
322 egg_tray_icon_get_orientation_property (icon);
327 egg_tray_icon_realize (GtkWidget *widget)
329 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
334 GdkWindow *root_window;
336 if (GTK_WIDGET_CLASS (parent_class)->realize)
337 GTK_WIDGET_CLASS (parent_class)->realize (widget);
339 screen = gtk_widget_get_screen (widget);
340 display = gdk_screen_get_display (screen);
341 xdisplay = gdk_x11_display_get_xdisplay (display);
343 /* Now see if there's a manager window around */
344 g_snprintf (buffer, sizeof (buffer),
345 "_NET_SYSTEM_TRAY_S%d",
346 gdk_screen_get_number (screen));
348 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
350 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
352 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
353 "_NET_SYSTEM_TRAY_OPCODE",
356 icon->orientation_atom = XInternAtom (xdisplay,
357 "_NET_SYSTEM_TRAY_ORIENTATION",
360 egg_tray_icon_update_manager_window (icon);
362 root_window = gdk_screen_get_root_window (screen);
364 /* Add a root window filter so that we get changes on MANAGER */
365 gdk_window_add_filter (root_window,
366 egg_tray_icon_manager_filter, icon);
370 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
375 display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
376 screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
378 return egg_tray_icon_new_for_screen (screen, name);
382 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
384 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
386 return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
390 egg_tray_icon_new (const gchar *name)
392 return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
396 egg_tray_icon_send_message (EggTrayIcon *icon,
398 const gchar *message,
403 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
404 g_return_val_if_fail (timeout >= 0, 0);
405 g_return_val_if_fail (message != NULL, 0);
407 if (icon->manager_window == None)
411 len = strlen (message);
413 stamp = icon->stamp++;
415 /* Get ready to send the message */
416 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
417 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
418 timeout, len, stamp);
420 /* Now to send the actual message */
421 gdk_error_trap_push ();
424 XClientMessageEvent ev;
427 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
429 ev.type = ClientMessage;
430 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
432 ev.message_type = XInternAtom (xdisplay,
433 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
436 memcpy (&ev.data, message, 20);
442 memcpy (&ev.data, message, len);
446 XSendEvent (xdisplay,
447 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
448 XSync (xdisplay, False);
450 gdk_error_trap_pop ();
456 egg_tray_icon_cancel_message (EggTrayIcon *icon,
459 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
460 g_return_if_fail (id > 0);
462 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
463 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
468 egg_tray_icon_get_orientation (EggTrayIcon *icon)
470 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
472 return icon->orientation;