root/branches/gajim_0.9/src/eggtrayicon.c

Revision 1063, 11.8 kB (checked in by asterix, 4 years ago)

reorder files

Line 
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 <config.h>
22#include <string.h>
23#include <libintl.h>
24
25#include "eggtrayicon.h"
26
27#include <gdk/gdkx.h>
28#include <X11/Xatom.h>
29
30/*#ifndef EGG_COMPILATION
31#ifndef _
32#define _(x) dgettext ('gajim', x)
33#define N_(x) x
34#endif
35#else
36#define _(x) x
37#define N_(x) x
38#endif
39*/
40#define SYSTEM_TRAY_REQUEST_DOCK    0
41#define SYSTEM_TRAY_BEGIN_MESSAGE   1
42#define SYSTEM_TRAY_CANCEL_MESSAGE  2
43
44#define SYSTEM_TRAY_ORIENTATION_HORZ 0
45#define SYSTEM_TRAY_ORIENTATION_VERT 1
46
47enum {
48  PROP_0,
49  PROP_ORIENTATION
50};
51         
52static GtkPlugClass *parent_class = NULL;
53
54static void egg_tray_icon_init (EggTrayIcon *icon);
55static void egg_tray_icon_class_init (EggTrayIconClass *klass);
56
57static void egg_tray_icon_get_property (GObject    *object,
58                                        guint       prop_id,
59                                        GValue     *value,
60                                        GParamSpec *pspec);
61
62static void egg_tray_icon_realize   (GtkWidget *widget);
63static void egg_tray_icon_unrealize (GtkWidget *widget);
64
65static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
66
67GType
68egg_tray_icon_get_type (void)
69{
70  static GType our_type = 0;
71
72  if (our_type == 0)
73    {
74      static const GTypeInfo our_info =
75      {
76        sizeof (EggTrayIconClass),
77        (GBaseInitFunc) NULL,
78        (GBaseFinalizeFunc) NULL,
79        (GClassInitFunc) egg_tray_icon_class_init,
80        NULL, /* class_finalize */
81        NULL, /* class_data */
82        sizeof (EggTrayIcon),
83        0,    /* n_preallocs */
84        (GInstanceInitFunc) egg_tray_icon_init
85      };
86
87      our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
88    }
89
90  return our_type;
91}
92
93static void
94egg_tray_icon_init (EggTrayIcon *icon)
95{
96  icon->stamp = 1;
97  icon->orientation = GTK_ORIENTATION_HORIZONTAL;
98 
99  gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
100}
101
102static void
103egg_tray_icon_class_init (EggTrayIconClass *klass)
104{
105  GObjectClass *gobject_class = (GObjectClass *)klass;
106  GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
107
108  parent_class = g_type_class_peek_parent (klass);
109
110  gobject_class->get_property = egg_tray_icon_get_property;
111
112  widget_class->realize   = egg_tray_icon_realize;
113  widget_class->unrealize = egg_tray_icon_unrealize;
114
115  g_object_class_install_property (gobject_class,
116                                   PROP_ORIENTATION,
117                                   g_param_spec_enum ("orientation",
118                                                      "Orientation",
119                                                      "The orientation of the tray.",
120//                                                    _("Orientation"),
121//                                                    _("The orientation of the tray."),
122                                                      GTK_TYPE_ORIENTATION,
123                                                      GTK_ORIENTATION_HORIZONTAL,
124                                                      G_PARAM_READABLE));
125}
126
127static void
128egg_tray_icon_get_property (GObject    *object,
129                            guint       prop_id,
130                            GValue     *value,
131                            GParamSpec *pspec)
132{
133  EggTrayIcon *icon = EGG_TRAY_ICON (object);
134
135  switch (prop_id)
136    {
137    case PROP_ORIENTATION:
138      g_value_set_enum (value, icon->orientation);
139      break;
140    default:
141      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
142      break;
143    }
144}
145
146static void
147egg_tray_icon_get_orientation_property (EggTrayIcon *icon)
148{
149  Display *xdisplay;
150  Atom type;
151  int format;
152  union {
153        gulong *prop;
154        guchar *prop_ch;
155  } prop = { NULL };
156  gulong nitems;
157  gulong bytes_after;
158  int error, result;
159
160  g_assert (icon->manager_window != None);
161 
162  xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
163
164  gdk_error_trap_push ();
165  type = None;
166  result = XGetWindowProperty (xdisplay,
167                               icon->manager_window,
168                               icon->orientation_atom,
169                               0, G_MAXLONG, FALSE,
170                               XA_CARDINAL,
171                               &type, &format, &nitems,
172                               &bytes_after, &(prop.prop_ch));
173  error = gdk_error_trap_pop ();
174
175  if (error || result != Success)
176    return;
177
178  if (type == XA_CARDINAL)
179    {
180      GtkOrientation orientation;
181
182      orientation = (prop.prop [0] == SYSTEM_TRAY_ORIENTATION_HORZ) ?
183                                        GTK_ORIENTATION_HORIZONTAL :
184                                        GTK_ORIENTATION_VERTICAL;
185
186      if (icon->orientation != orientation)
187        {
188          icon->orientation = orientation;
189
190          g_object_notify (G_OBJECT (icon), "orientation");
191        }
192    }
193
194  if (prop.prop)
195    XFree (prop.prop);
196}
197
198static GdkFilterReturn
199egg_tray_icon_manager_filter (GdkXEvent *xevent, GdkEvent *event, gpointer user_data)
200{
201  EggTrayIcon *icon = user_data;
202  XEvent *xev = (XEvent *)xevent;
203
204  if (xev->xany.type == ClientMessage &&
205      xev->xclient.message_type == icon->manager_atom &&
206      xev->xclient.data.l[1] == icon->selection_atom)
207    {
208      egg_tray_icon_update_manager_window (icon);
209    }
210  else if (xev->xany.window == icon->manager_window)
211    {
212      if (xev->xany.type == PropertyNotify &&
213          xev->xproperty.atom == icon->orientation_atom)
214        {
215          egg_tray_icon_get_orientation_property (icon);
216        }
217      if (xev->xany.type == DestroyNotify)
218        {
219          egg_tray_icon_update_manager_window (icon);
220        }
221    }
222 
223  return GDK_FILTER_CONTINUE;
224}
225
226static void
227egg_tray_icon_unrealize (GtkWidget *widget)
228{
229  EggTrayIcon *icon = EGG_TRAY_ICON (widget);
230  GdkWindow *root_window;
231
232  if (icon->manager_window != None)
233    {
234      GdkWindow *gdkwin;
235
236      gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (widget),
237                                              icon->manager_window);
238
239      gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
240    }
241
242  root_window = gdk_screen_get_root_window (gtk_widget_get_screen (widget));
243
244  gdk_window_remove_filter (root_window, egg_tray_icon_manager_filter, icon);
245
246  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
247    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
248}
249
250static void
251egg_tray_icon_send_manager_message (EggTrayIcon *icon,
252                                    long         message,
253                                    Window       window,
254                                    long         data1,
255                                    long         data2,
256                                    long         data3)
257{
258  XClientMessageEvent ev;
259  Display *display;
260 
261  ev.type = ClientMessage;
262  ev.window = window;
263  ev.message_type = icon->system_tray_opcode_atom;
264  ev.format = 32;
265  ev.data.l[0] = gdk_x11_get_server_time (GTK_WIDGET (icon)->window);
266  ev.data.l[1] = message;
267  ev.data.l[2] = data1;
268  ev.data.l[3] = data2;
269  ev.data.l[4] = data3;
270
271  display = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
272 
273  gdk_error_trap_push ();
274  XSendEvent (display,
275              icon->manager_window, False, NoEventMask, (XEvent *)&ev);
276  XSync (display, False);
277  gdk_error_trap_pop ();
278}
279
280static void
281egg_tray_icon_send_dock_request (EggTrayIcon *icon)
282{
283  egg_tray_icon_send_manager_message (icon,
284                                      SYSTEM_TRAY_REQUEST_DOCK,
285                                      icon->manager_window,
286                                      gtk_plug_get_id (GTK_PLUG (icon)),
287                                      0, 0);
288}
289
290static void
291egg_tray_icon_update_manager_window (EggTrayIcon *icon)
292{
293  Display *xdisplay;
294 
295  xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
296 
297  if (icon->manager_window != None)
298    {
299      GdkWindow *gdkwin;
300
301      gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
302                                              icon->manager_window);
303     
304      gdk_window_remove_filter (gdkwin, egg_tray_icon_manager_filter, icon);
305    }
306 
307  XGrabServer (xdisplay);
308 
309  icon->manager_window = XGetSelectionOwner (xdisplay,
310                                             icon->selection_atom);
311
312  if (icon->manager_window != None)
313    XSelectInput (xdisplay,
314                  icon->manager_window, StructureNotifyMask|PropertyChangeMask);
315
316  XUngrabServer (xdisplay);
317  XFlush (xdisplay);
318 
319  if (icon->manager_window != None)
320    {
321      GdkWindow *gdkwin;
322
323      gdkwin = gdk_window_lookup_for_display (gtk_widget_get_display (GTK_WIDGET (icon)),
324                                              icon->manager_window);
325     
326      gdk_window_add_filter (gdkwin, egg_tray_icon_manager_filter, icon);
327
328      /* Send a request that we'd like to dock */
329      egg_tray_icon_send_dock_request (icon);
330
331      egg_tray_icon_get_orientation_property (icon);
332    }
333}
334
335static void
336egg_tray_icon_realize (GtkWidget *widget)
337{
338  EggTrayIcon *icon = EGG_TRAY_ICON (widget);
339  GdkScreen *screen;
340  GdkDisplay *display;
341  Display *xdisplay;
342  char buffer[256];
343  GdkWindow *root_window;
344
345  if (GTK_WIDGET_CLASS (parent_class)->realize)
346    GTK_WIDGET_CLASS (parent_class)->realize (widget);
347
348  screen = gtk_widget_get_screen (widget);
349  display = gdk_screen_get_display (screen);
350  xdisplay = gdk_x11_display_get_xdisplay (display);
351
352  /* Now see if there's a manager window around */
353  g_snprintf (buffer, sizeof (buffer),
354              "_NET_SYSTEM_TRAY_S%d",
355              gdk_screen_get_number (screen));
356
357  icon->selection_atom = XInternAtom (xdisplay, buffer, False);
358 
359  icon->manager_atom = XInternAtom (xdisplay, "MANAGER", False);
360 
361  icon->system_tray_opcode_atom = XInternAtom (xdisplay,
362                                                   "_NET_SYSTEM_TRAY_OPCODE",
363                                                   False);
364
365  icon->orientation_atom = XInternAtom (xdisplay,
366                                        "_NET_SYSTEM_TRAY_ORIENTATION",
367                                        False);
368
369  egg_tray_icon_update_manager_window (icon);
370
371  root_window = gdk_screen_get_root_window (screen);
372 
373  /* Add a root window filter so that we get changes on MANAGER */
374  gdk_window_add_filter (root_window,
375                         egg_tray_icon_manager_filter, icon);
376}
377
378EggTrayIcon *
379egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
380{
381  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
382
383  return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
384}
385
386EggTrayIcon*
387egg_tray_icon_new (const gchar *name)
388{
389  return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
390}
391
392guint
393egg_tray_icon_send_message (EggTrayIcon *icon,
394                            gint         timeout,
395                            const gchar *message,
396                            gint         len)
397{
398  guint stamp;
399 
400  g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
401  g_return_val_if_fail (timeout >= 0, 0);
402  g_return_val_if_fail (message != NULL, 0);
403                     
404  if (icon->manager_window == None)
405    return 0;
406
407  if (len < 0)
408    len = strlen (message);
409
410  stamp = icon->stamp++;
411 
412  /* Get ready to send the message */
413  egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
414                                      (Window)gtk_plug_get_id (GTK_PLUG (icon)),
415                                      timeout, len, stamp);
416
417  /* Now to send the actual message */
418  gdk_error_trap_push ();
419  while (len > 0)
420    {
421      XClientMessageEvent ev;
422      Display *xdisplay;
423
424      xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
425     
426      ev.type = ClientMessage;
427      ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
428      ev.format = 8;
429      ev.message_type = XInternAtom (xdisplay,
430                                     "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
431      if (len > 20)
432        {
433          memcpy (&ev.data, message, 20);
434          len -= 20;
435          message += 20;
436        }
437      else
438        {
439          memcpy (&ev.data, message, len);
440          len = 0;
441        }
442
443      XSendEvent (xdisplay,
444                  icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
445      XSync (xdisplay, False);
446    }
447  gdk_error_trap_pop ();
448
449  return stamp;
450}
451
452void
453egg_tray_icon_cancel_message (EggTrayIcon *icon,
454                              guint        id)
455{
456  g_return_if_fail (EGG_IS_TRAY_ICON (icon));
457  g_return_if_fail (id > 0);
458 
459  egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
460                                      (Window)gtk_plug_get_id (GTK_PLUG (icon)),
461                                      id, 0, 0);
462}
463
464GtkOrientation
465egg_tray_icon_get_orientation (EggTrayIcon *icon)
466{
467  g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
468
469  return icon->orientation;
470}
Note: See TracBrowser for help on using the browser.