3510
|
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
|
|
2 /* eggtrayicon.c
|
|
3 * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
|
|
4 *
|
|
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.
|
|
9 *
|
|
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.
|
|
14 *
|
|
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.
|
|
19 */
|
|
20
|
|
21 #include <string.h>
|
|
22 #include <gdk/gdkx.h>
|
|
23 #include "eggtrayicon.h"
|
|
24
|
|
25 #define SYSTEM_TRAY_REQUEST_DOCK 0
|
|
26 #define SYSTEM_TRAY_BEGIN_MESSAGE 1
|
|
27 #define SYSTEM_TRAY_CANCEL_MESSAGE 2
|
|
28
|
|
29 static GtkPlugClass *parent_class = NULL;
|
|
30
|
|
31 static void egg_tray_icon_init (EggTrayIcon *icon);
|
|
32 static void egg_tray_icon_class_init (EggTrayIconClass *klass);
|
|
33
|
|
34 static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
|
|
35
|
|
36 GType
|
|
37 egg_tray_icon_get_type (void)
|
|
38 {
|
|
39 static GType our_type = 0;
|
|
40
|
|
41 our_type = g_type_from_name("EggTrayIcon");
|
|
42
|
|
43 if (our_type == 0)
|
|
44 {
|
|
45 static const GTypeInfo our_info =
|
|
46 {
|
|
47 sizeof (EggTrayIconClass),
|
|
48 (GBaseInitFunc) NULL,
|
|
49 (GBaseFinalizeFunc) NULL,
|
|
50 (GClassInitFunc) egg_tray_icon_class_init,
|
|
51 NULL, /* class_finalize */
|
|
52 NULL, /* class_data */
|
|
53 sizeof (EggTrayIcon),
|
|
54 0, /* n_preallocs */
|
|
55 (GInstanceInitFunc) egg_tray_icon_init
|
|
56 };
|
|
57
|
|
58 our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
|
|
59 }
|
|
60
|
|
61 return our_type;
|
|
62 }
|
|
63
|
|
64 static void
|
|
65 egg_tray_icon_init (EggTrayIcon *icon)
|
|
66 {
|
|
67 icon->stamp = 1;
|
|
68
|
|
69 gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
|
|
70 }
|
|
71
|
|
72 static void
|
|
73 egg_tray_icon_class_init (EggTrayIconClass *klass)
|
|
74 {
|
|
75 parent_class = g_type_class_peek_parent (klass);
|
|
76 }
|
|
77
|
|
78 static GdkFilterReturn
|
|
79 egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
|
|
80 {
|
|
81 EggTrayIcon *icon = user_data;
|
|
82 XEvent *xev = (XEvent *)xevent;
|
|
83
|
|
84 if (xev->xany.type == ClientMessage &&
|
|
85 xev->xclient.message_type == icon->manager_atom &&
|
|
86 xev->xclient.data.l[1] == icon->selection_atom)
|
|
87 {
|
|
88 egg_tray_icon_update_manager_window (icon);
|
|
89 }
|
|
90 else if (xev->xany.window == icon->manager_window)
|
|
91 {
|
|
92 if (xev->xany.type == DestroyNotify)
|
|
93 {
|
|
94 egg_tray_icon_update_manager_window (icon);
|
|
95 }
|
|
96 }
|
|
97
|
|
98 return GDK_FILTER_CONTINUE;
|
|
99 }
|
|
100
|
|
101 static void
|
|
102 egg_tray_icon_send_manager_message (EggTrayIcon *icon,
|
|
103 long message,
|
|
104 Window window,
|
|
105 long data1,
|
|
106 long data2,
|
|
107 long data3)
|
|
108 {
|
|
109 XClientMessageEvent ev;
|
|
110 Display *display;
|
|
111
|
|
112 ev.type = ClientMessage;
|
|
113 ev.window = window;
|
|
114 ev.message_type = icon->system_tray_opcode_atom;
|
|
115 ev.format = 32;
|
|
116 ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
|
|
117 ev.data.l[1] = message;
|
|
118 ev.data.l[2] = data1;
|
|
119 ev.data.l[3] = data2;
|
|
120 ev.data.l[4] = data3;
|
|
121
|
|
122 #if HAVE_GTK_MULTIHEAD
|
|
123 display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
|
|
124 #else
|
|
125 display = gdk_display;
|
|
126 #endif
|
|
127
|
|
128 gdk_error_trap_push ();
|
|
129 XSendEvent (display,
|
|
130 icon->manager_window, False, NoEventMask, (XEvent *)&ev);
|
|
131 XSync (display, False);
|
|
132 gdk_error_trap_pop ();
|
|
133 }
|
|
134
|
|
135 static void
|
|
136 egg_tray_icon_send_dock_request (EggTrayIcon *icon)
|
|
137 {
|
|
138 egg_tray_icon_send_manager_message (icon,
|
|
139 SYSTEM_TRAY_REQUEST_DOCK,
|
|
140 icon->manager_window,
|
|
141 gtk_plug_get_id (GTK_PLUG (icon)),
|
|
142 0, 0);
|
|
143 }
|
|
144
|
|
145 static void
|
|
146 egg_tray_icon_update_manager_window (EggTrayIcon *icon)
|
|
147 {
|
|
148 Display *xdisplay;
|
|
149
|
|
150 #if HAVE_GTK_MULTIHEAD
|
|
151 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
|
|
152 #else
|
|
153 xdisplay = gdk_display;
|
|
154 #endif
|
|
155
|
|
156 if (icon->manager_window != None)
|
|
157 {
|
|
158 GdkWindow *gdkwin;
|
|
159
|
|
160 #if HAVE_GTK_MULTIHEAD
|
|
161 gdkwin = gdk_window_lookup_for_display (display,
|
|
162 icon->manager_window);
|
|
163 #else
|
|
164 gdkwin = gdk_window_lookup (icon->manager_window);
|
|
165 #endif
|
|
166
|
|
167 gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
|
|
168 }
|
|
169
|
|
170 XGrabServer (xdisplay);
|
|
171
|
|
172 icon->manager_window = XGetSelectionOwner (xdisplay,
|
|
173 icon->selection_atom);
|
|
174
|
|
175 if (icon->manager_window != None)
|
|
176 XSelectInput (xdisplay,
|
|
177 icon->manager_window, StructureNotifyMask);
|
|
178
|
|
179 XUngrabServer (xdisplay);
|
|
180 XFlush (xdisplay);
|
|
181
|
|
182 if (icon->manager_window != None)
|
|
183 {
|
|
184 GdkWindow *gdkwin;
|
|
185
|
|
186 #if HAVE_GTK_MULTIHEAD
|
|
187 gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
|
|
188 icon->manager_window);
|
|
189 #else
|
|
190 gdkwin = gdk_window_lookup (icon->manager_window);
|
|
191 #endif
|
|
192
|
|
193 gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
|
|
194
|
|
195 /* Send a request that we'd like to dock */
|
|
196 egg_tray_icon_send_dock_request (icon);
|
|
197 }
|
|
198 }
|
|
199
|
|
200 EggTrayIcon *
|
|
201 egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
|
|
202 {
|
|
203 EggTrayIcon *icon;
|
|
204 char buffer[256];
|
|
205 GdkWindow *root_window;
|
|
206
|
|
207 g_return_val_if_fail (xscreen != NULL, NULL);
|
|
208
|
|
209 icon = g_object_new (EGG_TYPE_TRAY_ICON, NULL);
|
|
210 gtk_window_set_title (GTK_WINDOW (icon), name);
|
|
211
|
|
212 #if HAVE_GTK_MULTIHEAD
|
|
213 gtk_plug_construct_for_display (GTK_PLUG (icon),
|
|
214 gdk_screen_get_display (screen), 0);
|
|
215 #else
|
|
216 gtk_plug_construct (GTK_PLUG (icon), 0);
|
|
217 #endif
|
|
218
|
|
219 gtk_widget_realize (GTK_WIDGET (icon));
|
|
220
|
|
221 /* Now see if there's a manager window around */
|
|
222 g_snprintf (buffer, sizeof (buffer),
|
|
223 "_NET_SYSTEM_TRAY_S%d",
|
|
224 XScreenNumberOfScreen (xscreen));
|
|
225
|
|
226 icon->selection_atom = XInternAtom (DisplayOfScreen (xscreen),
|
|
227 buffer, False);
|
|
228
|
|
229 icon->manager_atom = XInternAtom (DisplayOfScreen (xscreen),
|
|
230 "MANAGER", False);
|
|
231
|
|
232 icon->system_tray_opcode_atom = XInternAtom (DisplayOfScreen (xscreen),
|
|
233 "_NET_SYSTEM_TRAY_OPCODE", False);
|
|
234
|
|
235 egg_tray_icon_update_manager_window (icon);
|
|
236
|
|
237 #if HAVE_GTK_MULTIHEAD
|
|
238 root_window = gdk_screen_get_root_window (screen);
|
|
239 #else
|
|
240 root_window = gdk_window_lookup (gdk_x11_get_default_root_xwindow ());
|
|
241 #endif
|
|
242
|
|
243 /* Add a root window filter so that we get changes on MANAGER */
|
|
244 gdk_window_add_filter (root_window,
|
|
245 egg_tray_icon_manager_filter, icon);
|
|
246
|
|
247 return icon;
|
|
248 }
|
|
249
|
|
250 #if HAVE_GTK_MULTIHEAD
|
|
251 EggTrayIcon *
|
|
252 egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
|
|
253 {
|
|
254 EggTrayIcon *icon;
|
|
255 char buffer[256];
|
|
256
|
|
257 g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
|
|
258
|
|
259 return egg_tray_icon_new_for_xscreen (GDK_SCREEN_XSCREEN (screen), name);
|
|
260 }
|
|
261 #endif
|
|
262
|
|
263 EggTrayIcon*
|
|
264 egg_tray_icon_new (const gchar *name)
|
|
265 {
|
|
266 return egg_tray_icon_new_for_xscreen (DefaultScreenOfDisplay (gdk_display), name);
|
|
267 }
|
|
268
|
|
269 guint
|
|
270 egg_tray_icon_send_message (EggTrayIcon *icon,
|
|
271 gint timeout,
|
|
272 const gchar *message,
|
|
273 gint len)
|
|
274 {
|
|
275 guint stamp;
|
|
276
|
|
277 g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
|
|
278 g_return_val_if_fail (timeout >= 0, 0);
|
|
279 g_return_val_if_fail (message != NULL, 0);
|
|
280
|
|
281 if (icon->manager_window == None)
|
|
282 return 0;
|
|
283
|
|
284 if (len < 0)
|
|
285 len = strlen (message);
|
|
286
|
|
287 stamp = icon->stamp++;
|
|
288
|
|
289 /* Get ready to send the message */
|
|
290 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
|
|
291 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
|
|
292 timeout, len, stamp);
|
|
293
|
|
294 /* Now to send the actual message */
|
|
295 gdk_error_trap_push ();
|
|
296 while (len > 0)
|
|
297 {
|
|
298 XClientMessageEvent ev;
|
|
299 Display *xdisplay;
|
|
300
|
|
301 #if HAVE_GTK_MULTIHEAD
|
|
302 xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
|
|
303 #else
|
|
304 xdisplay = gdk_display;
|
|
305 #endif
|
|
306
|
|
307 ev.type = ClientMessage;
|
|
308 ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
|
|
309 ev.format = 8;
|
|
310 ev.message_type = XInternAtom (xdisplay,
|
|
311 "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
|
|
312 if (len > 20)
|
|
313 {
|
|
314 memcpy (&ev.data, message, 20);
|
|
315 len -= 20;
|
|
316 message += 20;
|
|
317 }
|
|
318 else
|
|
319 {
|
|
320 memcpy (&ev.data, message, len);
|
|
321 len = 0;
|
|
322 }
|
|
323
|
|
324 XSendEvent (xdisplay,
|
|
325 icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
|
|
326 XSync (xdisplay, False);
|
|
327 }
|
|
328 gdk_error_trap_pop ();
|
|
329
|
|
330 return stamp;
|
|
331 }
|
|
332
|
|
333 void
|
|
334 egg_tray_icon_cancel_message (EggTrayIcon *icon,
|
|
335 guint id)
|
|
336 {
|
|
337 g_return_if_fail (EGG_IS_TRAY_ICON (icon));
|
|
338 g_return_if_fail (id > 0);
|
|
339
|
|
340 egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
|
|
341 (Window)gtk_plug_get_id (GTK_PLUG (icon)),
|
|
342 id, 0, 0);
|
|
343 }
|