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 "eggtrayicon.h"
29 #include <X11/Xatom.h>
31 #ifndef EGG_COMPILATION
33 #define _(x) dgettext (GETTEXT_PACKAGE, x)
41 #define SYSTEM_TRAY_REQUEST_DOCK 0
42 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
43 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
45 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
46 #define SYSTEM_TRAY_ORIENTATION_VERT 1
53 static GtkPlugClass *parent_class = NULL;
55 static void egg_tray_icon_init (EggTrayIcon *icon);
56 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
58 static void egg_tray_icon_get_property (GObject *object,
63 static void egg_tray_icon_realize (GtkWidget *widget);
64 static void egg_tray_icon_unrealize (GtkWidget *widget);
66 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
69 egg_tray_icon_get_type (void)
71 static GType our_type = 0;
75 static const GTypeInfo our_info =
77 sizeof (EggTrayIconClass),
79 (GBaseFinalizeFunc) NULL,
80 (GClassInitFunc) egg_tray_icon_class_init,
81 NULL, /* class_finalize */
82 NULL, /* class_data */
85 (GInstanceInitFunc) egg_tray_icon_init
88 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, (GTypeFlags)0);
95 egg_tray_icon_init (EggTrayIcon *icon)
98 icon->orientation = GTK_ORIENTATION_HORIZONTAL;
100 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
104 egg_tray_icon_class_init (EggTrayIconClass *klass)
106 GObjectClass *gobject_class = (GObjectClass *)klass;
107 GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
109 parent_class = (GtkPlugClass*)g_type_class_peek_parent (klass);
111 gobject_class->get_property = egg_tray_icon_get_property;
113 widget_class->realize = egg_tray_icon_realize;
114 widget_class->unrealize = egg_tray_icon_unrealize;
116 g_object_class_install_property (gobject_class,
118 g_param_spec_enum ("orientation",
120 "The orientation of the tray.",
121 GTK_TYPE_ORIENTATION,
122 GTK_ORIENTATION_HORIZONTAL,
127 egg_tray_icon_get_property (GObject *object,
132 EggTrayIcon *icon = EGG_TRAY_ICON (object);
136 case PROP_ORIENTATION:
137 g_value_set_enum (value, icon->orientation);
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
159 g_assert (icon->manager_window != None);
161 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 = (EggTrayIcon*)user_data;
201 XEvent *xev = (XEvent *)xevent;
203 if (xev->xany.type == ClientMessage &&
204 xev->xclient.message_type == icon->manager_atom &&
205 (unsigned long)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 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
236 icon->manager_window);
238 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
241 root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
243 gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
245 if (GTK_WIDGET_CLASS (parent_class)->unrealize)
246 (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
250 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
257 XClientMessageEvent ev;
260 ev.type = ClientMessage;
262 ev.message_type = icon->system_tray_opcode_atom;
264 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
265 ev.data.l[1] = message;
266 ev.data.l[2] = data1;
267 ev.data.l[3] = data2;
268 ev.data.l[4] = data3;
270 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
272 gdk_error_trap_push ();
274 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
275 XSync (display, False);
276 gdk_error_trap_pop ();
280 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
282 egg_tray_icon_send_manager_message (icon,
283 SYSTEM_TRAY_REQUEST_DOCK,
284 icon->manager_window,
285 gtk_plug_get_id (GTK_PLUG (icon)),
290 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
294 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
296 if (icon->manager_window != None)
300 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
301 icon->manager_window);
303 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
306 XGrabServer (xdisplay);
308 icon->manager_window = XGetSelectionOwner (xdisplay,
309 icon->selection_atom);
311 if (icon->manager_window != None)
312 XSelectInput (xdisplay,
313 icon->manager_window, StructureNotifyMask|PropertyChangeMask);
315 XUngrabServer (xdisplay);
318 if (icon->manager_window != None)
322 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
323 icon->manager_window);
325 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
327 /* Send a request that we'd like to dock */
328 egg_tray_icon_send_dock_request (icon);
330 egg_tray_icon_get_orientation_property (icon);
335 egg_tray_icon_realize (GtkWidget *widget)
337 EggTrayIcon *icon = EGG_TRAY_ICON (widget);
342 GdkWindow *root_window;
344 if (GTK_WIDGET_CLASS (parent_class)->realize)
345 GTK_WIDGET_CLASS (parent_class)->realize (widget);
347 screen = gtk_widget_get_screen (widget);
348 display = gdk_screen_get_display (screen);
349 xdisplay = gdk_x11_display_get_xdisplay (display);
351 /* Now see if there's a manager window around */
352 g_snprintf (buffer, sizeof (buffer),
353 "_NET_SYSTEM_TRAY_S%d",
354 gdk_screen_get_number (screen));
356 icon->selection_atom = XInternAtom (xdisplay, buffer, False);
358 icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
360 icon->system_tray_opcode_atom = XInternAtom (xdisplay,
361 "_NET_SYSTEM_TRAY_OPCODE",
364 icon->orientation_atom = XInternAtom (xdisplay,
365 "_NET_SYSTEM_TRAY_ORIENTATION",
368 egg_tray_icon_update_manager_window (icon);
370 root_window = gdk_screen_get_root_window (screen);
372 /* Add a root window filter so that we get changes on MANAGER */
373 gdk_window_add_filter (root_window,
374 egg_tray_icon_manager_filter, icon);
378 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
383 display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
384 screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
386 return egg_tray_icon_new_for_screen (screen, name);
390 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
392 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
394 return (EggTrayIcon*)g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
398 egg_tray_icon_new (const gchar *name)
400 return (EggTrayIcon*)g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
404 egg_tray_icon_send_message (EggTrayIcon *icon,
406 const gchar *message,
411 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
412 g_return_val_if_fail (timeout >= 0, 0);
413 g_return_val_if_fail (message != NULL, 0);
415 if (icon->manager_window == None)
419 len = strlen (message);
421 stamp = icon->stamp++;
423 /* Get ready to send the message */
424 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
425 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
426 timeout, len, stamp);
428 /* Now to send the actual message */
429 gdk_error_trap_push ();
432 XClientMessageEvent ev;
435 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
437 ev.type = ClientMessage;
438 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
440 ev.message_type = XInternAtom (xdisplay,
441 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
444 memcpy (&ev.data, message, 20);
450 memcpy (&ev.data, message, len);
454 XSendEvent (xdisplay,
455 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
456 XSync (xdisplay, False);
458 gdk_error_trap_pop ();
464 egg_tray_icon_cancel_message (EggTrayIcon *icon,
467 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
468 g_return_if_fail (id > 0);
470 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
471 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
476 egg_tray_icon_get_orientation (EggTrayIcon *icon)
478 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
480 return icon->orientation;