2009-09-29 15:08:01 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
2009-09-10 04:39:27 -04:00
|
|
|
/*
|
|
|
|
* st-clipboard.c: clipboard object
|
|
|
|
*
|
|
|
|
* Copyright 2009 Intel Corporation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU Lesser General Public License,
|
|
|
|
* version 2.1, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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
|
2010-11-10 17:00:45 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2009-09-10 04:39:27 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:st-clipboard
|
|
|
|
* @short_description: a simple representation of the X clipboard
|
|
|
|
*
|
|
|
|
* #StCliboard is a very simple object representation of the clipboard
|
|
|
|
* available to applications. Text is always assumed to be UTF-8 and non-text
|
|
|
|
* items are not handled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "st-clipboard.h"
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xatom.h>
|
2012-07-18 11:55:24 -04:00
|
|
|
#include <gdk/gdk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
2009-09-10 04:39:27 -04:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (StClipboard, st_clipboard, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
#define CLIPBOARD_PRIVATE(o) \
|
|
|
|
(G_TYPE_INSTANCE_GET_PRIVATE ((o), ST_TYPE_CLIPBOARD, StClipboardPrivate))
|
|
|
|
|
|
|
|
struct _StClipboardPrivate
|
|
|
|
{
|
|
|
|
Window clipboard_window;
|
|
|
|
gchar *clipboard_text;
|
|
|
|
|
|
|
|
Atom *supported_targets;
|
|
|
|
gint n_targets;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _EventFilterData EventFilterData;
|
|
|
|
struct _EventFilterData
|
|
|
|
{
|
|
|
|
StClipboard *clipboard;
|
|
|
|
StClipboardCallbackFunc callback;
|
|
|
|
gpointer user_data;
|
|
|
|
};
|
|
|
|
|
2011-11-09 11:19:47 -05:00
|
|
|
static Atom __atom_primary = None;
|
2009-09-10 04:39:27 -04:00
|
|
|
static Atom __atom_clip = None;
|
|
|
|
static Atom __utf8_string = None;
|
|
|
|
static Atom __atom_targets = None;
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_dispose (GObject *object)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (st_clipboard_parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
StClipboardPrivate *priv = ((StClipboard *) object)->priv;
|
|
|
|
|
|
|
|
g_free (priv->clipboard_text);
|
|
|
|
priv->clipboard_text = NULL;
|
|
|
|
|
|
|
|
g_free (priv->supported_targets);
|
|
|
|
priv->supported_targets = NULL;
|
|
|
|
priv->n_targets = 0;
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (st_clipboard_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
static GdkFilterReturn
|
|
|
|
st_clipboard_provider (GdkXEvent *xevent_p,
|
|
|
|
GdkEvent *gev,
|
|
|
|
void *user_data)
|
2009-09-10 04:39:27 -04:00
|
|
|
{
|
2012-07-18 11:55:24 -04:00
|
|
|
StClipboard *clipboard = user_data;
|
|
|
|
XEvent *xev = (XEvent *) xevent_p;
|
2009-09-10 04:39:27 -04:00
|
|
|
XSelectionEvent notify_event;
|
|
|
|
XSelectionRequestEvent *req_event;
|
2012-07-18 11:55:24 -04:00
|
|
|
GdkDisplay *display = gdk_display_get_default ();
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2014-10-10 13:12:27 -04:00
|
|
|
if (xev->type != SelectionRequest ||
|
|
|
|
!clipboard->priv->clipboard_text)
|
2012-07-18 11:55:24 -04:00
|
|
|
return GDK_FILTER_CONTINUE;
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
req_event = &xev->xselectionrequest;
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_x11_display_error_trap_push (display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
if (req_event->target == __atom_targets)
|
|
|
|
{
|
|
|
|
XChangeProperty (req_event->display,
|
|
|
|
req_event->requestor,
|
|
|
|
req_event->property,
|
|
|
|
XA_ATOM,
|
|
|
|
32,
|
|
|
|
PropModeReplace,
|
|
|
|
(guchar*) clipboard->priv->supported_targets,
|
|
|
|
clipboard->priv->n_targets);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XChangeProperty (req_event->display,
|
|
|
|
req_event->requestor,
|
|
|
|
req_event->property,
|
|
|
|
req_event->target,
|
|
|
|
8,
|
|
|
|
PropModeReplace,
|
|
|
|
(guchar*) clipboard->priv->clipboard_text,
|
|
|
|
strlen (clipboard->priv->clipboard_text));
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_event.type = SelectionNotify;
|
|
|
|
notify_event.display = req_event->display;
|
|
|
|
notify_event.requestor = req_event->requestor;
|
|
|
|
notify_event.selection = req_event->selection;
|
|
|
|
notify_event.target = req_event->target;
|
|
|
|
notify_event.time = req_event->time;
|
|
|
|
|
|
|
|
if (req_event->property == None)
|
|
|
|
notify_event.property = req_event->target;
|
|
|
|
else
|
|
|
|
notify_event.property = req_event->property;
|
|
|
|
|
|
|
|
/* notify the requestor that they have a copy of the selection */
|
|
|
|
XSendEvent (req_event->display, req_event->requestor, False, 0,
|
|
|
|
(XEvent *) ¬ify_event);
|
|
|
|
/* Make it happen non async */
|
2012-07-18 11:55:24 -04:00
|
|
|
XSync (GDK_DISPLAY_XDISPLAY (display), FALSE);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
if (gdk_x11_display_error_trap_pop (display))
|
|
|
|
{
|
|
|
|
/* FIXME: Warn here on fail ? */
|
|
|
|
}
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
return GDK_FILTER_REMOVE;
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_class_init (StClipboardClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
g_type_class_add_private (klass, sizeof (StClipboardPrivate));
|
|
|
|
|
|
|
|
object_class->get_property = st_clipboard_get_property;
|
|
|
|
object_class->set_property = st_clipboard_set_property;
|
|
|
|
object_class->dispose = st_clipboard_dispose;
|
|
|
|
object_class->finalize = st_clipboard_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
st_clipboard_init (StClipboard *self)
|
|
|
|
{
|
2012-07-18 11:55:24 -04:00
|
|
|
GdkDisplay *gdk_display;
|
2009-09-10 04:39:27 -04:00
|
|
|
Display *dpy;
|
|
|
|
StClipboardPrivate *priv;
|
|
|
|
|
|
|
|
priv = self->priv = CLIPBOARD_PRIVATE (self);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_display = gdk_display_get_default ();
|
|
|
|
dpy = GDK_DISPLAY_XDISPLAY (gdk_display);
|
|
|
|
|
2009-09-10 04:39:27 -04:00
|
|
|
priv->clipboard_window =
|
2012-07-18 11:55:24 -04:00
|
|
|
XCreateSimpleWindow (dpy,
|
|
|
|
gdk_x11_get_default_root_xwindow (),
|
2009-09-10 04:39:27 -04:00
|
|
|
-1, -1, 1, 1, 0, 0, 0);
|
|
|
|
|
|
|
|
/* Only create once */
|
2011-11-09 11:19:47 -05:00
|
|
|
if (__atom_primary == None)
|
|
|
|
__atom_primary = XInternAtom (dpy, "PRIMARY", 0);
|
|
|
|
|
2009-09-10 04:39:27 -04:00
|
|
|
if (__atom_clip == None)
|
|
|
|
__atom_clip = XInternAtom (dpy, "CLIPBOARD", 0);
|
|
|
|
|
|
|
|
if (__utf8_string == None)
|
|
|
|
__utf8_string = XInternAtom (dpy, "UTF8_STRING", 0);
|
|
|
|
|
|
|
|
if (__atom_targets == None)
|
|
|
|
__atom_targets = XInternAtom (dpy, "TARGETS", 0);
|
|
|
|
|
|
|
|
priv->n_targets = 2;
|
|
|
|
priv->supported_targets = g_new (Atom, priv->n_targets);
|
|
|
|
|
|
|
|
priv->supported_targets[0] = __utf8_string;
|
|
|
|
priv->supported_targets[1] = __atom_targets;
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_window_add_filter (NULL, /* all windows */
|
|
|
|
st_clipboard_provider,
|
|
|
|
self);
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
static GdkFilterReturn
|
|
|
|
st_clipboard_x11_event_filter (GdkXEvent *xevent_p,
|
|
|
|
GdkEvent *gev,
|
|
|
|
void *user_data)
|
2009-09-10 04:39:27 -04:00
|
|
|
{
|
2012-07-18 11:55:24 -04:00
|
|
|
XEvent *xev = (XEvent *) xevent_p;
|
|
|
|
EventFilterData *filter_data = user_data;
|
2009-09-10 04:39:27 -04:00
|
|
|
Atom actual_type;
|
|
|
|
int actual_format, result;
|
|
|
|
unsigned long nitems, bytes_after;
|
|
|
|
unsigned char *data = NULL;
|
2012-07-18 11:55:24 -04:00
|
|
|
GdkDisplay *display = gdk_display_get_default ();
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
if(xev->type != SelectionNotify)
|
2012-07-18 11:55:24 -04:00
|
|
|
return GDK_FILTER_CONTINUE;
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
if (xev->xselection.property == None)
|
|
|
|
{
|
|
|
|
/* clipboard empty */
|
|
|
|
filter_data->callback (filter_data->clipboard,
|
|
|
|
NULL,
|
|
|
|
filter_data->user_data);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_window_remove_filter (NULL,
|
|
|
|
st_clipboard_x11_event_filter,
|
|
|
|
filter_data);
|
2009-09-10 04:39:27 -04:00
|
|
|
g_free (filter_data);
|
2012-07-18 11:55:24 -04:00
|
|
|
return GDK_FILTER_REMOVE;
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_x11_display_error_trap_push (display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
result = XGetWindowProperty (xev->xselection.display,
|
|
|
|
xev->xselection.requestor,
|
|
|
|
xev->xselection.property,
|
|
|
|
0L, G_MAXINT,
|
|
|
|
True,
|
|
|
|
AnyPropertyType,
|
|
|
|
&actual_type,
|
|
|
|
&actual_format,
|
|
|
|
&nitems,
|
|
|
|
&bytes_after,
|
|
|
|
&data);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
if (gdk_x11_display_error_trap_pop (display) || result != Success)
|
2009-09-10 04:39:27 -04:00
|
|
|
{
|
|
|
|
/* FIXME: handle failure better */
|
|
|
|
g_warning ("Clipboard: prop retrival failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
filter_data->callback (filter_data->clipboard, (char*) data,
|
|
|
|
filter_data->user_data);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_window_remove_filter (NULL,
|
|
|
|
st_clipboard_x11_event_filter,
|
|
|
|
filter_data);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
g_free (filter_data);
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
XFree (data);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
return GDK_FILTER_REMOVE;
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_clipboard_get_default:
|
|
|
|
*
|
|
|
|
* Get the global #StClipboard object that represents the clipboard.
|
|
|
|
*
|
2009-09-16 19:07:23 -04:00
|
|
|
* Returns: (transfer none): a #StClipboard owned by St and must not be
|
|
|
|
* unrefferenced or freed.
|
2009-09-10 04:39:27 -04:00
|
|
|
*/
|
|
|
|
StClipboard*
|
|
|
|
st_clipboard_get_default (void)
|
|
|
|
{
|
|
|
|
static StClipboard *default_clipboard = NULL;
|
|
|
|
|
|
|
|
if (!default_clipboard)
|
|
|
|
{
|
|
|
|
default_clipboard = g_object_new (ST_TYPE_CLIPBOARD, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_clipboard;
|
|
|
|
}
|
|
|
|
|
2011-11-09 11:19:47 -05:00
|
|
|
static Atom
|
|
|
|
atom_for_clipboard_type (StClipboardType type)
|
|
|
|
{
|
|
|
|
return type == ST_CLIPBOARD_TYPE_CLIPBOARD ? __atom_clip : __atom_primary;
|
|
|
|
}
|
|
|
|
|
2009-09-10 04:39:27 -04:00
|
|
|
/**
|
|
|
|
* st_clipboard_get_text:
|
|
|
|
* @clipboard: A #StCliboard
|
2011-11-09 11:19:47 -05:00
|
|
|
* @type: The type of clipboard data you want
|
2010-09-01 20:48:11 -04:00
|
|
|
* @callback: (scope async): function to be called when the text is retreived
|
2009-09-10 04:39:27 -04:00
|
|
|
* @user_data: data to be passed to the callback
|
|
|
|
*
|
|
|
|
* Request the data from the clipboard in text form. @callback is executed
|
|
|
|
* when the data is retreived.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
st_clipboard_get_text (StClipboard *clipboard,
|
2011-11-09 11:19:47 -05:00
|
|
|
StClipboardType type,
|
2009-09-10 04:39:27 -04:00
|
|
|
StClipboardCallbackFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
EventFilterData *data;
|
2012-07-18 11:55:24 -04:00
|
|
|
GdkDisplay *gdk_display;
|
2009-09-10 04:39:27 -04:00
|
|
|
Display *dpy;
|
|
|
|
|
|
|
|
g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
|
|
|
|
g_return_if_fail (callback != NULL);
|
|
|
|
|
|
|
|
data = g_new0 (EventFilterData, 1);
|
|
|
|
data->clipboard = clipboard;
|
|
|
|
data->callback = callback;
|
|
|
|
data->user_data = user_data;
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_window_add_filter (NULL, /* all windows */
|
|
|
|
st_clipboard_x11_event_filter,
|
|
|
|
data);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_display = gdk_display_get_default ();
|
|
|
|
dpy = GDK_DISPLAY_XDISPLAY (gdk_display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_x11_display_error_trap_push (gdk_display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
|
|
|
XConvertSelection (dpy,
|
2011-11-09 11:19:47 -05:00
|
|
|
atom_for_clipboard_type (type),
|
2009-09-10 04:39:27 -04:00
|
|
|
__utf8_string, __utf8_string,
|
|
|
|
clipboard->priv->clipboard_window,
|
|
|
|
CurrentTime);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
if (gdk_x11_display_error_trap_pop (gdk_display))
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
}
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* st_clipboard_set_text:
|
|
|
|
* @clipboard: A #StClipboard
|
2011-11-09 11:19:47 -05:00
|
|
|
* @type: The type of clipboard that you want to set
|
2009-09-10 04:39:27 -04:00
|
|
|
* @text: text to copy to the clipboard
|
|
|
|
*
|
|
|
|
* Sets text as the current contents of the clipboard.
|
|
|
|
*/
|
|
|
|
void
|
2011-11-09 11:19:47 -05:00
|
|
|
st_clipboard_set_text (StClipboard *clipboard,
|
|
|
|
StClipboardType type,
|
|
|
|
const gchar *text)
|
2009-09-10 04:39:27 -04:00
|
|
|
{
|
|
|
|
StClipboardPrivate *priv;
|
2012-07-18 11:55:24 -04:00
|
|
|
GdkDisplay *gdk_display;
|
2009-09-10 04:39:27 -04:00
|
|
|
Display *dpy;
|
|
|
|
|
|
|
|
g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
|
|
|
|
g_return_if_fail (text != NULL);
|
|
|
|
|
|
|
|
priv = clipboard->priv;
|
|
|
|
|
|
|
|
/* make a copy of the text */
|
|
|
|
g_free (priv->clipboard_text);
|
|
|
|
priv->clipboard_text = g_strdup (text);
|
|
|
|
|
|
|
|
/* tell X we own the clipboard selection */
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_display = gdk_display_get_default ();
|
|
|
|
dpy = GDK_DISPLAY_XDISPLAY (gdk_display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
gdk_x11_display_error_trap_push (gdk_display);
|
2009-09-10 04:39:27 -04:00
|
|
|
|
2011-11-09 11:19:47 -05:00
|
|
|
XSetSelectionOwner (dpy, atom_for_clipboard_type (type), priv->clipboard_window, CurrentTime);
|
|
|
|
|
2009-09-10 04:39:27 -04:00
|
|
|
XSync (dpy, FALSE);
|
|
|
|
|
2012-07-18 11:55:24 -04:00
|
|
|
if (gdk_x11_display_error_trap_pop (gdk_display))
|
|
|
|
{
|
|
|
|
/* FIXME */
|
|
|
|
}
|
2009-09-10 04:39:27 -04:00
|
|
|
}
|