~ubuntu-branches/ubuntu/utopic/rhythmbox/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/eggtrayicon.c

Tags: upstream-0.9.2
ImportĀ upstreamĀ versionĀ 0.9.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 (GETTEXT_PACKAGE, 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
 
 
47
 
enum {
48
 
  PROP_0,
49
 
  PROP_ORIENTATION
50
 
};
51
 
         
52
 
static GtkPlugClass *parent_class = NULL;
53
 
 
54
 
static void egg_tray_icon_init (EggTrayIcon *icon);
55
 
static void egg_tray_icon_class_init (EggTrayIconClass *klass);
56
 
 
57
 
static void egg_tray_icon_get_property (GObject    *object,
58
 
                                        guint       prop_id,
59
 
                                        GValue     *value,
60
 
                                        GParamSpec *pspec);
61
 
 
62
 
static void egg_tray_icon_realize   (GtkWidget *widget);
63
 
static void egg_tray_icon_unrealize (GtkWidget *widget);
64
 
 
65
 
EggTrayIcon * egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name);
66
 
 
67
 
static void egg_tray_icon_update_manager_window (EggTrayIcon *icon);
68
 
 
69
 
GType
70
 
egg_tray_icon_get_type (void)
71
 
{
72
 
  static GType our_type = 0;
73
 
 
74
 
  if (our_type == 0)
75
 
    {
76
 
      static const GTypeInfo our_info =
77
 
      {
78
 
        sizeof (EggTrayIconClass),
79
 
        (GBaseInitFunc) NULL,
80
 
        (GBaseFinalizeFunc) NULL,
81
 
        (GClassInitFunc) egg_tray_icon_class_init,
82
 
        NULL, /* class_finalize */
83
 
        NULL, /* class_data */
84
 
        sizeof (EggTrayIcon),
85
 
        0,    /* n_preallocs */
86
 
        (GInstanceInitFunc) egg_tray_icon_init
87
 
      };
88
 
 
89
 
      our_type = g_type_register_static (GTK_TYPE_PLUG, "EggTrayIcon", &our_info, 0);
90
 
    }
91
 
 
92
 
  return our_type;
93
 
}
94
 
 
95
 
static void
96
 
egg_tray_icon_init (EggTrayIcon *icon)
97
 
{
98
 
  icon->stamp = 1;
99
 
  icon->orientation = GTK_ORIENTATION_HORIZONTAL;
100
 
  
101
 
  gtk_widget_add_events (GTK_WIDGET (icon), GDK_PROPERTY_CHANGE_MASK);
102
 
}
103
 
 
104
 
static void
105
 
egg_tray_icon_class_init (EggTrayIconClass *klass)
106
 
{
107
 
  GObjectClass *gobject_class = (GObjectClass *)klass;
108
 
  GtkWidgetClass *widget_class = (GtkWidgetClass *)klass;
109
 
 
110
 
  parent_class = g_type_class_peek_parent (klass);
111
 
 
112
 
  gobject_class->get_property = egg_tray_icon_get_property;
113
 
 
114
 
  widget_class->realize   = egg_tray_icon_realize;
115
 
  widget_class->unrealize = egg_tray_icon_unrealize;
116
 
 
117
 
  g_object_class_install_property (gobject_class,
118
 
                                   PROP_ORIENTATION,
119
 
                                   g_param_spec_enum ("orientation",
120
 
                                                      _("Orientation"),
121
 
                                                      _("The orientation of the tray."),
122
 
                                                      GTK_TYPE_ORIENTATION,
123
 
                                                      GTK_ORIENTATION_HORIZONTAL,
124
 
                                                      G_PARAM_READABLE));
125
 
}
126
 
 
127
 
static void
128
 
egg_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
 
 
146
 
static void
147
 
egg_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
 
 
198
 
static GdkFilterReturn
199
 
egg_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
 
 
226
 
static void
227
 
egg_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
 
 
250
 
static void
251
 
egg_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
 
 
280
 
static void
281
 
egg_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
 
 
290
 
static void
291
 
egg_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
 
 
335
 
static void
336
 
egg_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
 
 
378
 
EggTrayIcon *
379
 
egg_tray_icon_new_for_xscreen (Screen *xscreen, const char *name)
380
 
{
381
 
  GdkDisplay *display;
382
 
  GdkScreen *screen;
383
 
 
384
 
  display = gdk_x11_lookup_xdisplay (DisplayOfScreen (xscreen));
385
 
  screen = gdk_display_get_screen (display, XScreenNumberOfScreen (xscreen));
386
 
 
387
 
  return egg_tray_icon_new_for_screen (screen, name);
388
 
}
389
 
 
390
 
EggTrayIcon *
391
 
egg_tray_icon_new_for_screen (GdkScreen *screen, const char *name)
392
 
{
393
 
  g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
394
 
 
395
 
  return g_object_new (EGG_TYPE_TRAY_ICON, "screen", screen, "title", name, NULL);
396
 
}
397
 
 
398
 
EggTrayIcon*
399
 
egg_tray_icon_new (const gchar *name)
400
 
{
401
 
  return g_object_new (EGG_TYPE_TRAY_ICON, "title", name, NULL);
402
 
}
403
 
 
404
 
guint
405
 
egg_tray_icon_send_message (EggTrayIcon *icon,
406
 
                            gint         timeout,
407
 
                            const gchar *message,
408
 
                            gint         len)
409
 
{
410
 
  guint stamp;
411
 
  
412
 
  g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), 0);
413
 
  g_return_val_if_fail (timeout >= 0, 0);
414
 
  g_return_val_if_fail (message != NULL, 0);
415
 
                     
416
 
  if (icon->manager_window == None)
417
 
    return 0;
418
 
 
419
 
  if (len < 0)
420
 
    len = strlen (message);
421
 
 
422
 
  stamp = icon->stamp++;
423
 
  
424
 
  /* Get ready to send the message */
425
 
  egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_BEGIN_MESSAGE,
426
 
                                      (Window)gtk_plug_get_id (GTK_PLUG (icon)),
427
 
                                      timeout, len, stamp);
428
 
 
429
 
  /* Now to send the actual message */
430
 
  gdk_error_trap_push ();
431
 
  while (len > 0)
432
 
    {
433
 
      XClientMessageEvent ev;
434
 
      Display *xdisplay;
435
 
 
436
 
      xdisplay = GDK_DISPLAY_XDISPLAY (gtk_widget_get_display (GTK_WIDGET (icon)));
437
 
      
438
 
      ev.type = ClientMessage;
439
 
      ev.window = (Window)gtk_plug_get_id (GTK_PLUG (icon));
440
 
      ev.format = 8;
441
 
      ev.message_type = XInternAtom (xdisplay,
442
 
                                     "_NET_SYSTEM_TRAY_MESSAGE_DATA", False);
443
 
      if (len > 20)
444
 
        {
445
 
          memcpy (&ev.data, message, 20);
446
 
          len -= 20;
447
 
          message += 20;
448
 
        }
449
 
      else
450
 
        {
451
 
          memcpy (&ev.data, message, len);
452
 
          len = 0;
453
 
        }
454
 
 
455
 
      XSendEvent (xdisplay,
456
 
                  icon->manager_window, False, StructureNotifyMask, (XEvent *)&ev);
457
 
      XSync (xdisplay, False);
458
 
    }
459
 
  gdk_error_trap_pop ();
460
 
 
461
 
  return stamp;
462
 
}
463
 
 
464
 
void
465
 
egg_tray_icon_cancel_message (EggTrayIcon *icon,
466
 
                              guint        id)
467
 
{
468
 
  g_return_if_fail (EGG_IS_TRAY_ICON (icon));
469
 
  g_return_if_fail (id > 0);
470
 
  
471
 
  egg_tray_icon_send_manager_message (icon, SYSTEM_TRAY_CANCEL_MESSAGE,
472
 
                                      (Window)gtk_plug_get_id (GTK_PLUG (icon)),
473
 
                                      id, 0, 0);
474
 
}
475
 
 
476
 
GtkOrientation
477
 
egg_tray_icon_get_orientation (EggTrayIcon *icon)
478
 
{
479
 
  g_return_val_if_fail (EGG_IS_TRAY_ICON (icon), GTK_ORIENTATION_HORIZONTAL);
480
 
 
481
 
  return icon->orientation;
482
 
}