]> Pileus Git - ~andy/gtk/blob - gtk/gtktrayicon-x11.c
16d8bc35c7356a3ca6446246ac4921f7d4f45613
[~andy/gtk] / gtk / gtktrayicon-x11.c
1 /* gtktrayicon.c
2  * Copyright (C) 2002 Anders Carlsson <andersca@gnu.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * This is an implementation of the freedesktop.org "system tray" spec,
22  * http://www.freedesktop.org/wiki/Standards/systemtray-spec
23  */
24
25 #include <config.h>
26 #include <string.h>
27 #include <libintl.h>
28
29 #include "gtkintl.h"
30 #include "gtkprivate.h"
31 #include "gtktrayicon.h"
32
33 #include "gtkalias.h"
34
35 #include "x11/gdkx.h"
36 #include <X11/Xatom.h>
37
38 #define SYSTEM_TRAY_REQUEST_DOCK    0
39 #define SYSTEM_TRAY_BEGIN_MESSAGE   1
40 #define SYSTEM_TRAY_CANCEL_MESSAGE  2
41
42 #define SYSTEM_TRAY_ORIENTATION_HORZ 0
43 #define SYSTEM_TRAY_ORIENTATION_VERT 1
44
45 enum {
46   PROP_0,
47   PROP_ORIENTATION
48 };
49
50 struct _GtkTrayIconPrivate
51 {
52   guint stamp;
53   
54   Atom selection_atom;
55   Atom manager_atom;
56   Atom system_tray_opcode_atom;
57   Atom orientation_atom;
58   Window manager_window;
59
60   GtkOrientation orientation;
61 };
62          
63 static void gtk_tray_icon_get_property  (GObject     *object,
64                                          guint        prop_id,
65                                          GValue      *value,
66                                          GParamSpec  *pspec);
67
68 static void     gtk_tray_icon_realize   (GtkWidget   *widget);
69 static void     gtk_tray_icon_unrealize (GtkWidget   *widget);
70 static gboolean gtk_tray_icon_delete    (GtkWidget   *widget,
71                                          GdkEventAny *event);
72 static gboolean gtk_tray_icon_expose    (GtkWidget      *widget, 
73                                          GdkEventExpose *event);
74
75 static void gtk_tray_icon_update_manager_window    (GtkTrayIcon *icon,
76                                                     gboolean     dock_if_realized);
77 static void gtk_tray_icon_manager_window_destroyed (GtkTrayIcon *icon);
78
79 G_DEFINE_TYPE (GtkTrayIcon, gtk_tray_icon, GTK_TYPE_PLUG)
80
81 static void
82 gtk_tray_icon_class_init (GtkTrayIconClass *class)
83 {
84   GObjectClass *gobject_class = (GObjectClass *)class;
85   GtkWidgetClass *widget_class = (GtkWidgetClass *)class;
86
87   gobject_class->get_property = gtk_tray_icon_get_property;
88
89   widget_class->realize   = gtk_tray_icon_realize;
90   widget_class->unrealize = gtk_tray_icon_unrealize;
91   widget_class->delete_event = gtk_tray_icon_delete;
92   widget_class->expose_event = gtk_tray_icon_expose;
93
94   g_object_class_install_property (gobject_class,
95                                    PROP_ORIENTATION,
96                                    g_param_spec_enum ("orientation",
97                                                       P_("Orientation"),
98                                                       P_("The orientation of the tray"),
99                                                       GTK_TYPE_ORIENTATION,
100                                                       GTK_ORIENTATION_HORIZONTAL,
101                                                       GTK_PARAM_READABLE));
102
103   g_type_class_add_private (class, sizeof (GtkTrayIconPrivate));
104 }
105
106 static void
107 gtk_tray_icon_init (GtkTrayIcon *icon)
108 {
109   icon->priv = G_TYPE_INSTANCE_GET_PRIVATE (icon, GTK_TYPE_TRAY_ICON,
110                                             GtkTrayIconPrivate);
111   
112   icon->priv->stamp = 1;
113   icon->priv->orientation = GTK_ORIENTATION_HORIZONTAL;
114
115   gtk_widget_set_app_paintable (GTK_WIDGET (icon), TRUE);
116   gtk_widget_set_double_buffered (GTK_WIDGET (icon), FALSE);
117   gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
118 }
119
120 static void
121 gtk_tray_icon_get_property (GObject    *object,
122                             guint       prop_id,
123                             GValue     *value,
124                             GParamSpec *pspec)
125 {
126   GtkTrayIcon *icon = GTK_TRAY_ICON (object);
127
128   switch (prop_id)
129     {
130     case PROP_ORIENTATION:
131       g_value_set_enum (value, icon->priv->orientation);
132       break;
133     default:
134       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135       break;
136     }
137 }
138
139 static gboolean
140 gtk_tray_icon_expose (GtkWidget      *widget, 
141                       GdkEventExpose *event)
142 {
143   gdk_window_clear_area (widget->window, event->area.x, event->area.y,
144                          event->area.width, event->area.height);
145
146   if (GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->expose_event)  
147     return GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->expose_event (widget, event);
148
149   return FALSE;
150 }
151
152 static void
153 gtk_tray_icon_get_orientation_property (GtkTrayIcon *icon)
154 {
155   Display *xdisplay;
156   Atom type;
157   int format;
158   union {
159         gulong *prop;
160         guchar *prop_ch;
161   } prop = { NULL };
162   gulong nitems;
163   gulong bytes_after;
164   int error, result;
165
166   g_assert (icon->priv->manager_window != None);
167   
168   xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
169
170   gdk_error_trap_push ();
171   type = None;
172   result = XGetWindowProperty (xdisplay,
173                                icon->priv->manager_window,
174                                icon->priv->orientation_atom,
175                                0, G_MAXLONG, FALSE,
176                                XA_CARDINAL,
177                                &type, &format, &nitems,
178                                &bytes_after, &(prop.prop_ch));
179   error = gdk_error_trap_pop ();
180
181   if (error || result != Success)
182     return;
183
184   if (type == XA_CARDINAL)
185     {
186       GtkOrientation orientation;
187
188       orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
189                                         GTK_ORIENTATION_HORIZONTAL :
190                                         GTK_ORIENTATION_VERTICAL;
191
192       if (icon->priv->orientation != orientation)
193         {
194           icon->priv->orientation = orientation;
195
196           g_object_notify (G_OBJECT (icon), "orientation");
197         }
198     }
199
200   if (prop.prop)
201     XFree (prop.prop);
202 }
203
204 static GdkFilterReturn
205 gtk_tray_icon_manager_filter (GdkXEvent *xevent, 
206                               GdkEvent  *event, 
207                               gpointer   user_data)
208 {
209   GtkTrayIcon *icon = user_data;
210   XEvent *xev = (XEvent *)xevent;
211
212   if (xev->xany.type == ClientMessage &&
213       xev->xclient.message_type == icon->priv->manager_atom &&
214       xev->xclient.data.l[1] == icon->priv->selection_atom)
215     {
216       gtk_tray_icon_update_manager_window (icon, TRUE);
217     }
218   else if (xev->xany.window == icon->priv->manager_window)
219     {
220       if (xev->xany.type == PropertyNotify &&
221           xev->xproperty.atom == icon->priv->orientation_atom)
222         {
223           gtk_tray_icon_get_orientation_property (icon);
224         }
225       if (xev->xany.type == DestroyNotify)
226         {
227           gtk_tray_icon_manager_window_destroyed (icon);
228         }
229     }
230   
231   return GDK_FILTER_CONTINUE;
232 }
233
234 static void
235 gtk_tray_icon_unrealize (GtkWidget *widget)
236 {
237   GtkTrayIcon *icon = GTK_TRAY_ICON (widget);
238   GdkWindow *root_window;
239
240   if (icon->priv->manager_window != None)
241     {
242       GdkWindow *gdkwin;
243
244       gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
245                                               icon->priv->manager_window);
246       
247       gdk_window_remove_filter (gdkwin, gtk_tray_icon_manager_filter, icon);
248     }
249
250   root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
251
252   gdk_window_remove_filter (root_window, gtk_tray_icon_manager_filter, icon);
253
254   if (GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->unrealize)
255     (* GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->unrealize) (widget);
256 }
257
258 static void
259 gtk_tray_icon_send_manager_message (GtkTrayIcon *icon,
260                                     long         message,
261                                     Window       window,
262                                     long         data1,
263                                     long         data2,
264                                     long         data3)
265 {
266   XClientMessageEvent ev;
267   Display *display;
268   
269   memset (&ev, 0, sizeof (ev));
270   ev.type = ClientMessage;
271   ev.window = window;
272   ev.message_type = icon->priv->system_tray_opcode_atom;
273   ev.format = 32;
274   ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
275   ev.data.l[1] = message;
276   ev.data.l[2] = data1;
277   ev.data.l[3] = data2;
278   ev.data.l[4] = data3;
279
280   display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
281   
282   gdk_error_trap_push ();
283   XSendEvent (display,
284               icon->priv->manager_window, False, NoEventMask, (XEvent *)&ev);
285   XSync (display, False);
286   gdk_error_trap_pop ();
287 }
288
289 static void
290 gtk_tray_icon_send_dock_request (GtkTrayIcon *icon)
291 {
292   gtk_tray_icon_send_manager_message (icon,
293                                       SYSTEM_TRAY_REQUEST_DOCK,
294                                       icon->priv->manager_window,
295                                       gtk_plug_get_id (GTK_PLUG (icon)),
296                                       0, 0);
297 }
298
299 static void
300 gtk_tray_icon_update_manager_window (GtkTrayIcon *icon,
301                                      gboolean     dock_if_realized)
302 {
303   Display *xdisplay;
304   
305   if (icon->priv->manager_window != None)
306     return;
307
308   xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
309   
310   XGrabServer (xdisplay);
311   
312   icon->priv->manager_window = XGetSelectionOwner (xdisplay,
313                                                    icon->priv->selection_atom);
314
315   if (icon->priv->manager_window != None)
316     XSelectInput (xdisplay,
317                   icon->priv->manager_window, StructureNotifyMask|PropertyChangeMask);
318
319   XUngrabServer (xdisplay);
320   XFlush (xdisplay);
321   
322   if (icon->priv->manager_window != None)
323     {
324       GdkWindow *gdkwin;
325
326       gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
327                                               icon->priv->manager_window);
328       
329       gdk_window_add_filter (gdkwin, gtk_tray_icon_manager_filter, icon);
330
331       if (dock_if_realized && GTK_WIDGET_REALIZED (icon))
332         gtk_tray_icon_send_dock_request (icon);
333
334       gtk_tray_icon_get_orientation_property (icon);
335     }
336 }
337
338 static void
339 gtk_tray_icon_manager_window_destroyed (GtkTrayIcon *icon)
340 {
341   GdkWindow *gdkwin;
342   
343   g_return_if_fail (icon->priv->manager_window != None);
344
345   gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
346                                           icon->priv->manager_window);
347       
348   gdk_window_remove_filter (gdkwin, gtk_tray_icon_manager_filter, icon);
349
350   icon->priv->manager_window = None;
351
352   gtk_tray_icon_update_manager_window (icon, TRUE);
353 }
354
355 static gboolean 
356 gtk_tray_icon_delete (GtkWidget   *widget,
357                       GdkEventAny *event)
358 {
359   GtkTrayIcon *icon = GTK_TRAY_ICON (widget);
360   GdkWindow *gdkwin;
361
362   if (icon->priv->manager_window != None)
363     {  
364       gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
365                                               icon->priv->manager_window);
366       
367       gdk_window_remove_filter (gdkwin, gtk_tray_icon_manager_filter, icon);
368       
369       icon->priv->manager_window = None;
370     }
371
372   gtk_tray_icon_update_manager_window (icon, TRUE);  
373
374   return TRUE;
375 }
376
377 static void
378 gtk_tray_icon_realize (GtkWidget *widget)
379 {
380   GtkTrayIcon *icon = GTK_TRAY_ICON (widget);
381   GdkScreen *screen;
382   GdkDisplay *display;
383   Display *xdisplay;
384   char buffer[256];
385   GdkWindow *root_window;
386
387   if (GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->realize)
388     GTK_WIDGET_CLASS (gtk_tray_icon_parent_class)->realize (widget);
389
390   screen = gtk_widget_get_screen (widget);
391   display = gdk_screen_get_display (screen);
392   xdisplay = gdk_x11_display_get_xdisplay (display);
393
394   /* Now see if there's a manager window around */
395   g_snprintf (buffer, sizeof (buffer),
396               "_NET_SYSTEM_TRAY_S%d",
397               gdk_screen_get_number (screen));
398
399   icon->priv->selection_atom = XInternAtom (xdisplay, buffer, False);
400   
401   icon->priv->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
402   
403   icon->priv->system_tray_opcode_atom = XInternAtom (xdisplay,
404                                                      "_NET_SYSTEM_TRAY_OPCODE",
405                                                      False);
406
407   icon->priv->orientation_atom = XInternAtom (xdisplay,
408                                               "_NET_SYSTEM_TRAY_ORIENTATION",
409                                               False);
410
411   gtk_tray_icon_update_manager_window (icon, FALSE);
412   gtk_tray_icon_send_dock_request (icon);
413
414   root_window = gdk_screen_get_root_window (screen);
415   
416   /* Add a root window filter so that we get changes on MANAGER */
417   gdk_window_add_filter (root_window,
418                          gtk_tray_icon_manager_filter, icon);
419 }
420
421 guint
422 _gtk_tray_icon_send_message (GtkTrayIcon *icon,
423                              gint         timeout,
424                              const gchar *message,
425                              gint         len)
426 {
427   guint stamp;
428   
429   g_return_val_if_fail (GTK_IS_TRAY_ICON (icon), 0);
430   g_return_val_if_fail (timeout >= 0, 0);
431   g_return_val_if_fail (message != NULL, 0);
432                      
433   if (icon->priv->manager_window == None)
434     return 0;
435
436   if (len < 0)
437     len = strlen (message);
438
439   stamp = icon->priv->stamp++;
440   
441   /* Get ready to send the message */
442   gtk_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
443                                       (Window)gtk_plug_get_id (GTK_PLUG (icon)),
444                                       timeout, len, stamp);
445
446   /* Now to send the actual message */
447   gdk_error_trap_push ();
448   while (len > 0)
449     {
450       XClientMessageEvent ev;
451       Display *xdisplay;
452
453       xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
454       
455       memset (&ev, 0, sizeof (ev));
456       ev.type = ClientMessage;
457       ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
458       ev.format = 8;
459       ev.message_type = XInternAtom (xdisplay,
460                                      "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
461       if (len > 20)
462         {
463           memcpy (&ev.data, message, 20);
464           len -= 20;
465           message += 20;
466         }
467       else
468         {
469           memcpy (&ev.data, message, len);
470           len = 0;
471         }
472
473       XSendEvent (xdisplay,
474                   icon->priv->manager_window, False, 
475                   StructureNotifyMask, (XEvent *)&ev);
476       XSync (xdisplay, False);
477     }
478
479   gdk_error_trap_pop ();
480
481   return stamp;
482 }
483
484 void
485 _gtk_tray_icon_cancel_message (GtkTrayIcon *icon,
486                                guint        id)
487 {
488   g_return_if_fail (GTK_IS_TRAY_ICON (icon));
489   g_return_if_fail (id > 0);
490   
491   gtk_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
492                                       (Window)gtk_plug_get_id (GTK_PLUG (icon)),
493                                       id, 0, 0);
494 }
495
496 GtkTrayIcon *
497 _gtk_tray_icon_new_for_screen (GdkScreen  *screen, 
498                                const gchar *name)
499 {
500   g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
501
502   return g_object_new (GTK_TYPE_TRAY_ICON, 
503                        "screen", screen, 
504                        "title", name, 
505                        NULL);
506 }
507
508 GtkTrayIcon*
509 _gtk_tray_icon_new (const gchar *name)
510 {
511   return g_object_new (GTK_TYPE_TRAY_ICON, 
512                        "title", name, 
513                        NULL);
514 }
515
516 GtkOrientation
517 _gtk_tray_icon_get_orientation (GtkTrayIcon *icon)
518 {
519   g_return_val_if_fail (GTK_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
520
521   return icon->priv->orientation;
522 }
523
524
525 #define __GTK_TRAY_ICON_X11_C__
526 #include "gtkaliasdef.c"
527