File: uniquebackend.c

package info (click to toggle)
libunique 1.1.6-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,704 kB
  • sloc: sh: 10,371; ansic: 2,472; xml: 1,345; makefile: 238
file content (338 lines) | stat: -rw-r--r-- 8,301 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* Unique - Single Instance Backendlication library
 * uniquebackend.c: Base class for IPC transports
 *
 * Copyright (C) 2007  Emmanuele Bassi  <ebassi@o-hand.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

/**
 * SECTION:unique-backend
 * @short_description: Backend abstraction
 *
 * #UniqueBackend is the base, abstract class implemented by the different
 * IPC mechanisms used by Unique. Each #UniqueApp instance creates a
 * #UniqueBackend to request the name or to send messages.
 */

#include <config.h>

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

#include <glib/gi18n.h>

#include <gdk/gdk.h>
#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#endif

#include "uniquebackend.h"
#include "uniqueinternals.h"

G_DEFINE_ABSTRACT_TYPE (UniqueBackend, unique_backend, G_TYPE_OBJECT);

static void
unique_backend_finalize (GObject *gobject)
{
  UniqueBackend *backend = UNIQUE_BACKEND (gobject);

  g_free (backend->name);
  g_free (backend->startup_id);

  G_OBJECT_CLASS (unique_backend_parent_class)->finalize (gobject);
}

static void
unique_backend_class_init (UniqueBackendClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

  gobject_class->finalize = unique_backend_finalize;
}

static void
unique_backend_init (UniqueBackend *backend)
{
  backend->name = NULL;
  backend->startup_id = NULL;
  backend->screen = gdk_screen_get_default ();
  backend->workspace = -1;
}

/**
 * unique_backend_set_name:
 * @backend: FIXME
 * @name: FIXME
 *
 * FIXME
 */
void
unique_backend_set_name (UniqueBackend *backend,
                         const gchar   *name)
{
  g_return_if_fail (UNIQUE_IS_BACKEND (backend));
  g_return_if_fail (name != NULL);

  if (!backend->name)
    {
      backend->name = g_strdup (name);
      return;
    }

  if (strcmp (backend->name, name) != 0)
    {
      g_free (backend->name);
      backend->name = g_strdup (name);
    }
}

/**
 * unique_backend_get_name:
 * @backend: FIXME
 *
 * FIXME
 *
 * Return value: FIXME
 */
const gchar *
unique_backend_get_name (UniqueBackend *backend)
{
  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), NULL);

  return backend->name;
}

/**
 * unique_backend_set_startup_id:
 * @backend: FIXME
 * @startup_id: FIXME
 *
 * FIXME
 */
void
unique_backend_set_startup_id (UniqueBackend *backend,
                               const gchar   *startup_id)
{
  g_return_if_fail (UNIQUE_IS_BACKEND (backend));
  g_return_if_fail (startup_id != NULL);

  if (!backend->startup_id)
    {
      backend->startup_id = g_strdup (startup_id);
      return;
    }

  if (strcmp (backend->startup_id, startup_id) != 0)
    {
      g_free (backend->startup_id);
      backend->startup_id = g_strdup (startup_id);
    }
}

/**
 * unique_backend_get_startup_id:
 * @backend: FIXME
 *
 * FIXME
 *
 * Return value: FIXME
 */
const gchar *
unique_backend_get_startup_id (UniqueBackend *backend)
{
  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), NULL);

  return backend->startup_id;
}

/**
 * unique_backend_set_screen:
 * @backend: FIXME
 * @screen: FIXME
 *
 * FIXME
 */
void
unique_backend_set_screen (UniqueBackend *backend,
                           GdkScreen     *screen)
{
  g_return_if_fail (UNIQUE_IS_BACKEND (backend));
  g_return_if_fail (screen == NULL || GDK_IS_SCREEN (screen));

  if (!screen)
    screen = gdk_screen_get_default ();

  backend->screen = screen;
}

/**
 * unique_backend_get_screen:
 * @backend: FIXME
 *
 * FIXME
 *
 * Return value: FIXME
 */
GdkScreen *
unique_backend_get_screen (UniqueBackend *backend)
{
  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), NULL);

  return backend->screen;
}

/**
 * unique_backend_get_workspace:
 * @backend: a #UniqueBackend
 *
 * Retrieves the current workspace.
 *
 * Return value: a workspace number
 */
guint
unique_backend_get_workspace (UniqueBackend *backend)
{
  GdkDisplay *display;
  GdkWindow *root_win;
#ifdef GDK_WINDOWING_X11
  Atom _net_current_desktop, type;
  int format;
  unsigned long n_items, bytes_after;
  unsigned char *data_return = 0;
#endif

  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), 0);

  if (backend->workspace != -1)
    return backend->workspace;

  display = gdk_screen_get_display (backend->screen);
  root_win = gdk_screen_get_root_window (backend->screen);

#ifdef GDK_WINDOWING_X11
  _net_current_desktop =
    gdk_x11_get_xatom_by_name_for_display (display, "_NET_CURRENT_DESKTOP");

  XGetWindowProperty (GDK_DISPLAY_XDISPLAY (display),
                      GDK_WINDOW_XID (root_win),
                      _net_current_desktop,
                      0, G_MAXLONG,
                      False, XA_CARDINAL,
                      &type, &format, &n_items, &bytes_after,
                      &data_return);

  if (type == XA_CARDINAL && format == 32 && n_items > 0)
    {
      backend->workspace = (guint) data_return[0];
      XFree (data_return);
    }
#endif

  return backend->workspace;
}

/**
 * unique_backend_request_name:
 * @backend: a #UniqueBackend
 *
 * Requests the name set using unique_backend_set_name() using @backend.
 *
 * Return value: %TRUE if the name was assigned to us, %FALSE if there
 *   already is a registered name
 */
gboolean
unique_backend_request_name (UniqueBackend *backend)
{
  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), FALSE);

  return UNIQUE_BACKEND_GET_CLASS (backend)->request_name (backend);
}

/**
 * unique_backend_send_message:
 * @backend: a #UniqueBackend
 * @command_id: command to send
 * @message_data: message to send, or %NULL
 * @time_: time of the command emission, or 0 for the current time
 *
 * Sends @command_id, and optionally @message_data, to a running instance
 * using @backend.
 *
 * Return value: a #UniqueResponse value sent by the running instance
 */
UniqueResponse
unique_backend_send_message (UniqueBackend     *backend,
                             gint               command_id,
                             UniqueMessageData *message_data,
                             guint              time_)
{
  g_return_val_if_fail (UNIQUE_IS_BACKEND (backend), UNIQUE_RESPONSE_INVALID);
  g_return_val_if_fail (command_id != 0, UNIQUE_RESPONSE_INVALID);
  
  if (time_ == 0)
    time_ = (guint) time (NULL);

  return UNIQUE_BACKEND_GET_CLASS (backend)->send_message (backend, command_id,
                                                           message_data,
                                                           time_);
}

#include "bacon/uniquebackend-bacon.h"
#ifdef HAVE_DBUS
#include "dbus/uniquebackend-dbus.h"
#endif

/**
 * unique_backend_create:
 * 
 * Creates a #UniqueBackend using the default backend defined at
 * compile time. You can override the default backend by setting the
 * <literal>UNIQUE_BACKEND</literal> environment variable with the
 * name of the desired backend.
 *
 * Return value: the newly created #UniqueBackend instance
 */
UniqueBackend *
unique_backend_create (void)
{
  const gchar *backend_name;
  GType backend_gtype = G_TYPE_INVALID;

  backend_name = g_getenv ("UNIQUE_BACKEND");
  if (!backend_name)
    backend_name = UNIQUE_DEFAULT_BACKEND_S;

  if (backend_name && backend_name[0] != '\0')
    {
#ifdef HAVE_BACON
      if (strcmp (backend_name, "bacon") == 0)
        backend_gtype = unique_backend_bacon_get_type ();
#endif
#ifdef HAVE_DBUS
      if (strcmp (backend_name, "dbus") == 0)
        backend_gtype = unique_backend_dbus_get_type ();
#endif /* HAVE_DBUS */
#if !defined(HAVE_BACON) && !defined(HAVE_DBUS)
#error Need either bacon or dbus
#endif 
    }

  return g_object_new (backend_gtype, NULL);
}