~ubuntu-branches/debian/sid/xfce4-clipman-plugin/sid

« back to all changes in this revision

Viewing changes to .pc/0001-daemon-Fix-possible-NULL-values-bug-6119-6120.patch/daemon/gsd-clipboard-manager.c

  • Committer: Bazaar Package Importer
  • Author(s): Yves-Alexis Perez, Lionel Le Folgoc, Yves-Alexis Perez
  • Date: 2010-05-30 19:44:10 UTC
  • Revision ID: james.westby@ubuntu.com-20100530194410-4e6bx2wsdmkey5p1
Tags: 2:1.1.3-3
[ Lionel Le Folgoc ]
* debian/patches/0001-daemon-Fix-possible-NULL-values-bug-6119-6120.patch:
  fix crash occurring on panel shutdown. lp: #532537

[ Yves-Alexis Perez ]
* Switch format to 3.0 (quilt).
* debian/control:
  - add Lionel to Uploaders, with his permission.
  - update standards version to 3.8.4.
  - update debhelper build-dep for overrides.
* debian/rules:
  - switch to dh7 rules.
* debian/*.docs: add docs using dh_installdocs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2007 Matthias Clasen
 
4
 * Copyright (C) 2007 Anders Carlsson
 
5
 * Copyright (C) 2007 Rodrigo Moya
 
6
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 
7
 * Copyright (C) 2009 Mike Massonnet <mmassonnet@xfce.org>
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation; either version 2 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include <glib.h>
 
27
#include <gdk/gdkx.h>
 
28
#include <gtk/gtk.h>
 
29
#include <X11/Xlib.h>
 
30
#include <X11/Xatom.h>
 
31
 
 
32
#include "gsd-clipboard-manager.h"
 
33
 
 
34
#define GSD_CLIPBOARD_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_CLIPBOARD_MANAGER, GsdClipboardManagerPrivate))
 
35
 
 
36
struct GsdClipboardManagerPrivate
 
37
{
 
38
        GtkClipboard *default_clipboard;
 
39
        GtkClipboard *primary_clipboard;
 
40
 
 
41
        GSList       *default_cache;
 
42
        gchar        *primary_cache;
 
43
 
 
44
        gboolean      internal_change;
 
45
 
 
46
        GtkWidget    *window;
 
47
};
 
48
 
 
49
static void     gsd_clipboard_manager_class_init  (GsdClipboardManagerClass *klass);
 
50
static void     gsd_clipboard_manager_init        (GsdClipboardManager      *clipboard_manager);
 
51
static void     gsd_clipboard_manager_finalize    (GObject                  *object);
 
52
 
 
53
G_DEFINE_TYPE (GsdClipboardManager, gsd_clipboard_manager, G_TYPE_OBJECT)
 
54
 
 
55
static gpointer manager_object = NULL;
 
56
 
 
57
 
 
58
Atom XA_CLIPBOARD_MANAGER;
 
59
Atom XA_MANAGER;
 
60
 
 
61
static void
 
62
init_atoms (Display *display)
 
63
{
 
64
        static int _init_atoms = 0;
 
65
 
 
66
        if (_init_atoms > 0) {
 
67
                return;
 
68
        }
 
69
 
 
70
        XA_CLIPBOARD_MANAGER = XInternAtom (display, "CLIPBOARD_MANAGER", False);
 
71
        XA_MANAGER = XInternAtom (display, "MANAGER", False);
 
72
 
 
73
        _init_atoms = 1;
 
74
}
 
75
 
 
76
 
 
77
static void
 
78
default_clipboard_store (GsdClipboardManager *manager)
 
79
{
 
80
        GtkSelectionData *selection_data;
 
81
        GdkAtom          *atoms;
 
82
        gint              n_atoms;
 
83
        gint              i;
 
84
 
 
85
        if (!gtk_clipboard_wait_for_targets (manager->priv->default_clipboard, &atoms, &n_atoms)) {
 
86
                return;
 
87
        }
 
88
 
 
89
        if (manager->priv->default_cache != NULL) {
 
90
                g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
 
91
                g_slist_free (manager->priv->default_cache);
 
92
                manager->priv->default_cache = NULL;
 
93
        }
 
94
 
 
95
        for (i = 0; i < n_atoms; i++) {
 
96
                if (atoms[i] == gdk_atom_intern_static_string ("TARGETS")
 
97
                    || atoms[i] == gdk_atom_intern_static_string ("MULTIPLE")
 
98
                    || atoms[i] == gdk_atom_intern_static_string ("DELETE")
 
99
                    || atoms[i] == gdk_atom_intern_static_string ("INSERT_PROPERTY")
 
100
                    || atoms[i] == gdk_atom_intern_static_string ("INSERT_SELECTION")
 
101
                    || atoms[i] == gdk_atom_intern_static_string ("PIXMAP")) {
 
102
                        continue;
 
103
                }
 
104
 
 
105
                selection_data = gtk_clipboard_wait_for_contents (manager->priv->default_clipboard, atoms[i]);
 
106
                if (selection_data == NULL) {
 
107
                        continue;
 
108
                }
 
109
 
 
110
                manager->priv->default_cache = g_slist_prepend (manager->priv->default_cache, selection_data);
 
111
        }
 
112
}
 
113
 
 
114
static void
 
115
default_clipboard_get_func (GtkClipboard *clipboard,
 
116
                            GtkSelectionData *selection_data,
 
117
                            guint info,
 
118
                            GsdClipboardManager *manager)
 
119
{
 
120
        GSList           *list;
 
121
        GtkSelectionData *selection_data_cache = NULL;
 
122
 
 
123
        list = manager->priv->default_cache;
 
124
        for (; list->next != NULL; list = list->next) {
 
125
                selection_data_cache = list->data;
 
126
                if (selection_data->target == selection_data_cache->target) {
 
127
                        break;
 
128
                }
 
129
                selection_data_cache = NULL;
 
130
        }
 
131
        if (selection_data_cache == NULL) {
 
132
                return;
 
133
        }
 
134
        gtk_selection_data_set (selection_data, selection_data->target,
 
135
                                selection_data_cache->format,
 
136
                                selection_data_cache->data,
 
137
                                selection_data_cache->length);
 
138
}
 
139
 
 
140
static void
 
141
default_clipboard_clear_func (GtkClipboard *clipboard,
 
142
                              GsdClipboardManager *manager)
 
143
{
 
144
        return;
 
145
}
 
146
 
 
147
 
 
148
static void
 
149
default_clipboard_restore (GsdClipboardManager *manager)
 
150
{
 
151
        GtkTargetList    *target_list;
 
152
        GtkTargetEntry   *targets;
 
153
        gint              n_targets;
 
154
        GtkSelectionData *sdata;
 
155
        GSList           *list;
 
156
 
 
157
        target_list = gtk_target_list_new (NULL, 0);
 
158
        list = manager->priv->default_cache;
 
159
        for (; list->next != NULL; list = list->next) {
 
160
                sdata = list->data;
 
161
                gtk_target_list_add (target_list, sdata->target, 0, 0);
 
162
        }
 
163
        targets = gtk_target_table_new_from_list (target_list, &n_targets);
 
164
 
 
165
        gtk_clipboard_set_with_data (manager->priv->default_clipboard,
 
166
                                     targets, n_targets,
 
167
                                     (GtkClipboardGetFunc)default_clipboard_get_func,
 
168
                                     (GtkClipboardClearFunc)default_clipboard_clear_func,
 
169
                                     manager);
 
170
}
 
171
 
 
172
static void
 
173
default_clipboard_owner_change (GsdClipboardManager *manager,
 
174
                                GdkEventOwnerChange *event)
 
175
{
 
176
        if (event->send_event == TRUE) {
 
177
                return;
 
178
        }
 
179
 
 
180
        if (event->owner != 0) {
 
181
                if (manager->priv->internal_change) {
 
182
                        manager->priv->internal_change = FALSE;
 
183
                        return;
 
184
                }
 
185
                default_clipboard_store (manager);
 
186
        }
 
187
        else {
 
188
                /* This 'bug' happens with Mozilla applications, it means that
 
189
                 * we restored the clipboard (we own it now) but somehow we are
 
190
                 * being noticed twice about that fact where first the owner is
 
191
                 * 0 (which is when we must restore) but then again where the
 
192
                 * owner is ourself (which is what normally only happens and
 
193
                 * also that means that we have to store the clipboard content
 
194
                 * e.g. owner is not 0). By the second time we would store
 
195
                 * ourself back with an empty clipboard... solution is to jump
 
196
                 * over the first time and don't try to restore empty data. */
 
197
                if (manager->priv->internal_change) {
 
198
                        return;
 
199
                }
 
200
 
 
201
                manager->priv->internal_change = TRUE;
 
202
                default_clipboard_restore (manager);
 
203
        }
 
204
}
 
205
 
 
206
static void
 
207
primary_clipboard_owner_change (GsdClipboardManager *manager,
 
208
                                GdkEventOwnerChange *event)
 
209
{
 
210
        gchar *text;
 
211
 
 
212
        if (event->send_event == TRUE) {
 
213
                return;
 
214
        }
 
215
 
 
216
        if (event->owner != 0) {
 
217
                text = gtk_clipboard_wait_for_text (manager->priv->primary_clipboard);
 
218
                if (text != NULL) {
 
219
                        g_free (manager->priv->primary_cache);
 
220
                        manager->priv->primary_cache = text;
 
221
                }
 
222
        }
 
223
        else {
 
224
                if (manager->priv->primary_cache != NULL) {
 
225
                        gtk_clipboard_set_text (manager->priv->primary_clipboard,
 
226
                                                manager->priv->primary_cache,
 
227
                                                -1);
 
228
                }
 
229
        }
 
230
}
 
231
 
 
232
static gboolean
 
233
start_clipboard_idle_cb (GsdClipboardManager *manager)
 
234
{
 
235
        XClientMessageEvent     xev;
 
236
        gboolean                ownership;
 
237
        Display                *display;
 
238
        Window                  window;
 
239
        Time                    timestamp;
 
240
 
 
241
        display = GDK_DISPLAY ();
 
242
        init_atoms (display);
 
243
 
 
244
        /* Check if there is a clipboard manager running */
 
245
        if (gdk_display_supports_clipboard_persistence (gdk_display_get_default ())) {
 
246
                g_warning ("Clipboard manager is already running.");
 
247
                return FALSE;
 
248
        }
 
249
 
 
250
        manager->priv->window = gtk_invisible_new ();
 
251
        gtk_widget_realize (manager->priv->window);
 
252
 
 
253
        window = GDK_WINDOW_XID (manager->priv->window->window);
 
254
        timestamp = GDK_CURRENT_TIME;
 
255
 
 
256
        XSelectInput (display, window, PropertyChangeMask);
 
257
        XSetSelectionOwner (display, XA_CLIPBOARD_MANAGER, window, timestamp);
 
258
 
 
259
        g_signal_connect_swapped (manager->priv->default_clipboard, "owner-change",
 
260
                                  G_CALLBACK (default_clipboard_owner_change), manager);
 
261
        g_signal_connect_swapped (manager->priv->primary_clipboard, "owner-change",
 
262
                                  G_CALLBACK (primary_clipboard_owner_change), manager);
 
263
 
 
264
        /* Check to see if we managed to claim the selection. If not,
 
265
         * we treat it as if we got it then immediately lost it
 
266
         */
 
267
        if (XGetSelectionOwner (display, XA_CLIPBOARD_MANAGER) == window) {
 
268
                xev.type = ClientMessage;
 
269
                xev.window = DefaultRootWindow (display);
 
270
                xev.message_type = XA_MANAGER;
 
271
                xev.format = 32;
 
272
                xev.data.l[0] = timestamp;
 
273
                xev.data.l[1] = XA_CLIPBOARD_MANAGER;
 
274
                xev.data.l[2] = window;
 
275
                xev.data.l[3] = 0;      /* manager specific data */
 
276
                xev.data.l[4] = 0;      /* manager specific data */
 
277
 
 
278
                XSendEvent (display, DefaultRootWindow (display), False,
 
279
                            StructureNotifyMask, (XEvent *)&xev);
 
280
        } else {
 
281
                gsd_clipboard_manager_stop (manager);
 
282
        }
 
283
 
 
284
        return FALSE;
 
285
}
 
286
 
 
287
gboolean
 
288
gsd_clipboard_manager_start (GsdClipboardManager *manager,
 
289
                             GError             **error)
 
290
{
 
291
        g_idle_add ((GSourceFunc) start_clipboard_idle_cb, manager);
 
292
        return TRUE;
 
293
}
 
294
 
 
295
void
 
296
gsd_clipboard_manager_stop (GsdClipboardManager *manager)
 
297
{
 
298
        g_debug ("Stopping clipboard manager");
 
299
 
 
300
        g_signal_handlers_disconnect_by_func (manager->priv->default_clipboard,
 
301
                                              default_clipboard_owner_change, manager);
 
302
        g_signal_handlers_disconnect_by_func (manager->priv->primary_clipboard,
 
303
                                              primary_clipboard_owner_change, manager);
 
304
        gtk_widget_destroy (manager->priv->window);
 
305
 
 
306
        if (manager->priv->default_cache != NULL) {
 
307
                g_slist_foreach (manager->priv->default_cache, (GFunc)gtk_selection_data_free, NULL);
 
308
                g_slist_free (manager->priv->default_cache);
 
309
                manager->priv->default_cache = NULL;
 
310
        }
 
311
        if (manager->priv->primary_cache != NULL) {
 
312
                g_free (manager->priv->primary_cache);
 
313
        }
 
314
}
 
315
 
 
316
static void
 
317
gsd_clipboard_manager_set_property (GObject        *object,
 
318
                                    guint           prop_id,
 
319
                                    const GValue   *value,
 
320
                                    GParamSpec     *pspec)
 
321
{
 
322
        GsdClipboardManager *self;
 
323
 
 
324
        self = GSD_CLIPBOARD_MANAGER (object);
 
325
 
 
326
        switch (prop_id) {
 
327
        default:
 
328
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
329
                break;
 
330
        }
 
331
}
 
332
 
 
333
static void
 
334
gsd_clipboard_manager_get_property (GObject        *object,
 
335
                                    guint           prop_id,
 
336
                                    GValue         *value,
 
337
                                    GParamSpec     *pspec)
 
338
{
 
339
        GsdClipboardManager *self;
 
340
 
 
341
        self = GSD_CLIPBOARD_MANAGER (object);
 
342
 
 
343
        switch (prop_id) {
 
344
        default:
 
345
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
346
                break;
 
347
        }
 
348
}
 
349
 
 
350
static GObject *
 
351
gsd_clipboard_manager_constructor (GType                  type,
 
352
                                   guint                  n_construct_properties,
 
353
                                   GObjectConstructParam *construct_properties)
 
354
{
 
355
        GsdClipboardManager      *clipboard_manager;
 
356
        GsdClipboardManagerClass *klass;
 
357
 
 
358
        klass = GSD_CLIPBOARD_MANAGER_CLASS (g_type_class_peek (GSD_TYPE_CLIPBOARD_MANAGER));
 
359
 
 
360
        clipboard_manager = GSD_CLIPBOARD_MANAGER (G_OBJECT_CLASS (gsd_clipboard_manager_parent_class)->constructor (type,
 
361
                                                                                                      n_construct_properties,
 
362
                                                                                                      construct_properties));
 
363
 
 
364
        return G_OBJECT (clipboard_manager);
 
365
}
 
366
 
 
367
static void
 
368
gsd_clipboard_manager_dispose (GObject *object)
 
369
{
 
370
        GsdClipboardManager *clipboard_manager;
 
371
 
 
372
        clipboard_manager = GSD_CLIPBOARD_MANAGER (object);
 
373
 
 
374
        G_OBJECT_CLASS (gsd_clipboard_manager_parent_class)->dispose (object);
 
375
}
 
376
 
 
377
static void
 
378
gsd_clipboard_manager_class_init (GsdClipboardManagerClass *klass)
 
379
{
 
380
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
 
381
 
 
382
        object_class->get_property = gsd_clipboard_manager_get_property;
 
383
        object_class->set_property = gsd_clipboard_manager_set_property;
 
384
        object_class->constructor = gsd_clipboard_manager_constructor;
 
385
        object_class->dispose = gsd_clipboard_manager_dispose;
 
386
        object_class->finalize = gsd_clipboard_manager_finalize;
 
387
 
 
388
        g_type_class_add_private (klass, sizeof (GsdClipboardManagerPrivate));
 
389
}
 
390
 
 
391
static void
 
392
gsd_clipboard_manager_init (GsdClipboardManager *manager)
 
393
{
 
394
        manager->priv = GSD_CLIPBOARD_MANAGER_GET_PRIVATE (manager);
 
395
 
 
396
        manager->priv->default_clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
 
397
        manager->priv->primary_clipboard = gtk_clipboard_get (GDK_SELECTION_PRIMARY);
 
398
 
 
399
        manager->priv->default_cache = NULL;
 
400
        manager->priv->primary_cache = NULL;
 
401
}
 
402
 
 
403
static void
 
404
gsd_clipboard_manager_finalize (GObject *object)
 
405
{
 
406
        GsdClipboardManager *clipboard_manager;
 
407
 
 
408
        g_return_if_fail (object != NULL);
 
409
        g_return_if_fail (GSD_IS_CLIPBOARD_MANAGER (object));
 
410
 
 
411
        clipboard_manager = GSD_CLIPBOARD_MANAGER (object);
 
412
 
 
413
        g_return_if_fail (clipboard_manager->priv != NULL);
 
414
 
 
415
        G_OBJECT_CLASS (gsd_clipboard_manager_parent_class)->finalize (object);
 
416
}
 
417
 
 
418
GsdClipboardManager *
 
419
gsd_clipboard_manager_new (void)
 
420
{
 
421
        if (manager_object != NULL) {
 
422
                g_object_ref (manager_object);
 
423
        } else {
 
424
                manager_object = g_object_new (GSD_TYPE_CLIPBOARD_MANAGER, NULL);
 
425
                g_object_add_weak_pointer (manager_object,
 
426
                                           (gpointer *) &manager_object);
 
427
        }
 
428
 
 
429
        return GSD_CLIPBOARD_MANAGER (manager_object);
 
430
}