1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
3
* st-clipboard.c: clipboard object
5
* Copyright 2009 Intel Corporation.
7
* This program is free software; you can redistribute it and/or modify it
8
* under the terms and conditions of the GNU Lesser General Public License,
9
* version 2.1, as published by the Free Software Foundation.
11
* This program is distributed in the hope it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program; if not, write to the Free Software Foundation,
18
* Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20
* Written by: Thomas Wood <thomas.wood@intel.com>
25
* SECTION:st-clipboard
26
* @short_description: a simple representation of the X clipboard
28
* #StCliboard is a very simple object representation of the clipboard
29
* available to applications. Text is always assumed to be UTF-8 and non-text
30
* items are not handled.
34
#include "st-clipboard.h"
36
#include <X11/Xatom.h>
37
#include <clutter/x11/clutter-x11.h>
40
G_DEFINE_TYPE (StClipboard, st_clipboard, G_TYPE_OBJECT)
42
#define CLIPBOARD_PRIVATE(o) \
43
(G_TYPE_INSTANCE_GET_PRIVATE ((o), ST_TYPE_CLIPBOARD, StClipboardPrivate))
45
struct _StClipboardPrivate
47
Window clipboard_window;
48
gchar *clipboard_text;
50
Atom *supported_targets;
54
typedef struct _EventFilterData EventFilterData;
55
struct _EventFilterData
57
StClipboard *clipboard;
58
StClipboardCallbackFunc callback;
62
static Atom __atom_clip = None;
63
static Atom __utf8_string = None;
64
static Atom __atom_targets = None;
67
st_clipboard_get_property (GObject *object,
75
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80
st_clipboard_set_property (GObject *object,
88
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
93
st_clipboard_dispose (GObject *object)
95
G_OBJECT_CLASS (st_clipboard_parent_class)->dispose (object);
99
st_clipboard_finalize (GObject *object)
101
StClipboardPrivate *priv = ((StClipboard *) object)->priv;
103
g_free (priv->clipboard_text);
104
priv->clipboard_text = NULL;
106
g_free (priv->supported_targets);
107
priv->supported_targets = NULL;
110
G_OBJECT_CLASS (st_clipboard_parent_class)->finalize (object);
113
static ClutterX11FilterReturn
114
st_clipboard_provider (XEvent *xev,
116
StClipboard *clipboard)
118
XSelectionEvent notify_event;
119
XSelectionRequestEvent *req_event;
121
if (xev->type != SelectionRequest)
122
return CLUTTER_X11_FILTER_CONTINUE;
124
req_event = &xev->xselectionrequest;
126
clutter_x11_trap_x_errors ();
128
if (req_event->target == __atom_targets)
130
XChangeProperty (req_event->display,
131
req_event->requestor,
136
(guchar*) clipboard->priv->supported_targets,
137
clipboard->priv->n_targets);
141
XChangeProperty (req_event->display,
142
req_event->requestor,
147
(guchar*) clipboard->priv->clipboard_text,
148
strlen (clipboard->priv->clipboard_text));
151
notify_event.type = SelectionNotify;
152
notify_event.display = req_event->display;
153
notify_event.requestor = req_event->requestor;
154
notify_event.selection = req_event->selection;
155
notify_event.target = req_event->target;
156
notify_event.time = req_event->time;
158
if (req_event->property == None)
159
notify_event.property = req_event->target;
161
notify_event.property = req_event->property;
163
/* notify the requestor that they have a copy of the selection */
164
XSendEvent (req_event->display, req_event->requestor, False, 0,
165
(XEvent *) ¬ify_event);
166
/* Make it happen non async */
167
XSync (clutter_x11_get_default_display(), FALSE);
169
clutter_x11_untrap_x_errors (); /* FIXME: Warn here on fail ? */
171
return CLUTTER_X11_FILTER_REMOVE;
176
st_clipboard_class_init (StClipboardClass *klass)
178
GObjectClass *object_class = G_OBJECT_CLASS (klass);
180
g_type_class_add_private (klass, sizeof (StClipboardPrivate));
182
object_class->get_property = st_clipboard_get_property;
183
object_class->set_property = st_clipboard_set_property;
184
object_class->dispose = st_clipboard_dispose;
185
object_class->finalize = st_clipboard_finalize;
189
st_clipboard_init (StClipboard *self)
192
StClipboardPrivate *priv;
194
priv = self->priv = CLIPBOARD_PRIVATE (self);
196
priv->clipboard_window =
197
XCreateSimpleWindow (clutter_x11_get_default_display (),
198
clutter_x11_get_root_window (),
199
-1, -1, 1, 1, 0, 0, 0);
201
dpy = clutter_x11_get_default_display ();
203
/* Only create once */
204
if (__atom_clip == None)
205
__atom_clip = XInternAtom (dpy, "CLIPBOARD", 0);
207
if (__utf8_string == None)
208
__utf8_string = XInternAtom (dpy, "UTF8_STRING", 0);
210
if (__atom_targets == None)
211
__atom_targets = XInternAtom (dpy, "TARGETS", 0);
214
priv->supported_targets = g_new (Atom, priv->n_targets);
216
priv->supported_targets[0] = __utf8_string;
217
priv->supported_targets[1] = __atom_targets;
219
clutter_x11_add_filter ((ClutterX11FilterFunc) st_clipboard_provider,
223
static ClutterX11FilterReturn
224
st_clipboard_x11_event_filter (XEvent *xev,
226
EventFilterData *filter_data)
229
int actual_format, result;
230
unsigned long nitems, bytes_after;
231
unsigned char *data = NULL;
233
if(xev->type != SelectionNotify)
234
return CLUTTER_X11_FILTER_CONTINUE;
236
if (xev->xselection.property == None)
238
/* clipboard empty */
239
filter_data->callback (filter_data->clipboard,
241
filter_data->user_data);
243
clutter_x11_remove_filter ((ClutterX11FilterFunc) st_clipboard_x11_event_filter,
245
g_free (filter_data);
246
return CLUTTER_X11_FILTER_REMOVE;
249
clutter_x11_trap_x_errors ();
251
result = XGetWindowProperty (xev->xselection.display,
252
xev->xselection.requestor,
253
xev->xselection.property,
263
if (clutter_x11_untrap_x_errors () || result != Success)
265
/* FIXME: handle failure better */
266
g_warning ("Clipboard: prop retrival failed");
269
filter_data->callback (filter_data->clipboard, (char*) data,
270
filter_data->user_data);
272
clutter_x11_remove_filter
273
((ClutterX11FilterFunc) st_clipboard_x11_event_filter,
276
g_free (filter_data);
281
return CLUTTER_X11_FILTER_REMOVE;
285
* st_clipboard_get_default:
287
* Get the global #StClipboard object that represents the clipboard.
289
* Returns: (transfer none): a #StClipboard owned by St and must not be
290
* unrefferenced or freed.
293
st_clipboard_get_default (void)
295
static StClipboard *default_clipboard = NULL;
297
if (!default_clipboard)
299
default_clipboard = g_object_new (ST_TYPE_CLIPBOARD, NULL);
302
return default_clipboard;
306
* st_clipboard_get_text:
307
* @clipboard: A #StCliboard
308
* @callback: function to be called when the text is retreived
309
* @user_data: data to be passed to the callback
311
* Request the data from the clipboard in text form. @callback is executed
312
* when the data is retreived.
316
st_clipboard_get_text (StClipboard *clipboard,
317
StClipboardCallbackFunc callback,
320
EventFilterData *data;
324
g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
325
g_return_if_fail (callback != NULL);
327
data = g_new0 (EventFilterData, 1);
328
data->clipboard = clipboard;
329
data->callback = callback;
330
data->user_data = user_data;
332
clutter_x11_add_filter ((ClutterX11FilterFunc) st_clipboard_x11_event_filter,
335
dpy = clutter_x11_get_default_display ();
337
clutter_x11_trap_x_errors (); /* safety on */
339
XConvertSelection (dpy,
341
__utf8_string, __utf8_string,
342
clipboard->priv->clipboard_window,
345
clutter_x11_untrap_x_errors ();
349
* st_clipboard_set_text:
350
* @clipboard: A #StClipboard
351
* @text: text to copy to the clipboard
353
* Sets text as the current contents of the clipboard.
357
st_clipboard_set_text (StClipboard *clipboard,
360
StClipboardPrivate *priv;
363
g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
364
g_return_if_fail (text != NULL);
366
priv = clipboard->priv;
368
/* make a copy of the text */
369
g_free (priv->clipboard_text);
370
priv->clipboard_text = g_strdup (text);
372
/* tell X we own the clipboard selection */
373
dpy = clutter_x11_get_default_display ();
375
clutter_x11_trap_x_errors ();
377
XSetSelectionOwner (dpy, __atom_clip, priv->clipboard_window, CurrentTime);
380
clutter_x11_untrap_x_errors ();