2008-09-03 18:23:06 +02:00
|
|
|
|
/* x-selection.m -- proxies between NSPasteboard and X11 selections
|
|
|
|
|
$Id: x-selection.m,v 1.9 2006-07-07 18:24:28 jharper Exp $
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person
|
|
|
|
|
obtaining a copy of this software and associated documentation files
|
|
|
|
|
(the "Software"), to deal in the Software without restriction,
|
|
|
|
|
including without limitation the rights to use, copy, modify, merge,
|
|
|
|
|
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
|
and to permit persons to whom the Software is furnished to do so,
|
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
|
|
|
|
|
HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
|
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
|
|
Except as contained in this notice, the name(s) of the above
|
|
|
|
|
copyright holders shall not be used in advertising or otherwise to
|
|
|
|
|
promote the sale, use or other dealings in this Software without
|
|
|
|
|
prior written authorization. */
|
|
|
|
|
|
|
|
|
|
#import "x-selection.h"
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2008-09-03 18:23:06 +02:00
|
|
|
|
#include <X11/Xatom.h>
|
2008-09-17 19:59:36 +02:00
|
|
|
|
#import <AppKit/NSBitmapImageRep.h>
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The basic design of the pbproxy code is as follows.
|
|
|
|
|
*
|
|
|
|
|
* When a client selects text, say from an xterm - we only copy it when the
|
|
|
|
|
* X11 Edit->Copy menu item is pressed or the shortcut activated. In this
|
|
|
|
|
* case we take the PRIMARY selection, and set it as the NSPasteboard data.
|
|
|
|
|
*
|
|
|
|
|
* When an X11 client copies something to the CLIPBOARD, pbproxy greedily grabs
|
|
|
|
|
* the data, sets it as the NSPasteboard data, and finally sets itself as
|
|
|
|
|
* owner of the CLIPBOARD.
|
|
|
|
|
*
|
|
|
|
|
* When an X11 window is activated we check to see if the NSPasteboard has
|
|
|
|
|
* changed. If the NSPasteboard has changed, then we set pbproxy as owner
|
|
|
|
|
* of the PRIMARY and CLIPBOARD and respond to requests for text and images.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* TODO:
|
|
|
|
|
* 1. finish handling these pbproxy control knobs.
|
|
|
|
|
* 2. handle MULTIPLE - I need to study the ICCCM further.
|
|
|
|
|
* 3. handle COMPOUND_TEXT (if possible) - it's a variant of iso2022 from
|
|
|
|
|
* what I've read.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-09-15 18:33:55 +02:00
|
|
|
|
// These will be set by X11Controller.m once this is integrated into a server thread
|
|
|
|
|
BOOL pbproxy_active = YES;
|
|
|
|
|
BOOL pbproxy_primary_on_grab = NO; // This is provided as an option for people who want it and has issues that won't ever be addressed to make it *always* work
|
|
|
|
|
BOOL pbproxy_clipboard_to_pasteboard = YES;
|
|
|
|
|
BOOL pbproxy_pasteboard_to_primary = YES;
|
|
|
|
|
BOOL pbproxy_pasteboard_to_clipboard = YES;
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
@implementation x_selection
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
static struct propdata null_propdata = {NULL, 0};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
init_propdata (struct propdata *pdata)
|
|
|
|
|
{
|
|
|
|
|
*pdata = null_propdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
free_propdata (struct propdata *pdata)
|
|
|
|
|
{
|
|
|
|
|
free (pdata->data);
|
|
|
|
|
*pdata = null_propdata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Return True if this is an INCR-style transfer. */
|
|
|
|
|
static Bool
|
|
|
|
|
is_incr_type (XSelectionEvent *e)
|
|
|
|
|
{
|
|
|
|
|
Atom seltype;
|
|
|
|
|
int format;
|
|
|
|
|
unsigned long numitems = 0UL, bytesleft = 0UL;
|
|
|
|
|
unsigned char *chunk;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (Success != XGetWindowProperty (x_dpy, e->requestor, e->property,
|
|
|
|
|
/*offset*/ 0L, /*length*/ 4UL,
|
|
|
|
|
/*Delete*/ False,
|
|
|
|
|
AnyPropertyType, &seltype, &format,
|
|
|
|
|
&numitems, &bytesleft, &chunk))
|
|
|
|
|
{
|
|
|
|
|
return False;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(chunk)
|
|
|
|
|
XFree(chunk);
|
|
|
|
|
|
|
|
|
|
return (seltype == atoms->incr) ? True : False;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* This finds the preferred type from a TARGETS list.*/
|
|
|
|
|
static Atom
|
|
|
|
|
find_preferred (struct propdata *pdata)
|
|
|
|
|
{
|
|
|
|
|
Atom a = None;
|
|
|
|
|
size_t i;
|
|
|
|
|
Bool png = False, utf8 = False, string = False;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (pdata->length % sizeof (a))
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Atom list is not a multiple of the size of an atom!\n");
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < pdata->length; i += sizeof (a))
|
|
|
|
|
{
|
|
|
|
|
memcpy (&a, pdata->data + i, sizeof (a));
|
|
|
|
|
|
|
|
|
|
if (a == atoms->image_png)
|
|
|
|
|
{
|
|
|
|
|
png = True;
|
|
|
|
|
}
|
|
|
|
|
else if (a == atoms->utf8_string)
|
|
|
|
|
{
|
|
|
|
|
utf8 = True;
|
|
|
|
|
}
|
|
|
|
|
else if (a == atoms->string)
|
|
|
|
|
{
|
|
|
|
|
string = True;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*We prefer PNG over strings, and UTF8 over a Latin-1 string.*/
|
|
|
|
|
if (png)
|
|
|
|
|
return atoms->image_png;
|
|
|
|
|
|
|
|
|
|
if (utf8)
|
|
|
|
|
return atoms->utf8_string;
|
|
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
|
return atoms->string;
|
|
|
|
|
|
|
|
|
|
/* This is evidently something we don't know how to handle.*/
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Return True if an error occurs. Return False if pdata has data
|
|
|
|
|
* and we finished.
|
|
|
|
|
* The property is only deleted when bytesleft is 0 if delete is True.
|
|
|
|
|
*/
|
|
|
|
|
static Bool
|
|
|
|
|
get_property(Window win, Atom property, struct propdata *pdata, Bool delete, Atom *type)
|
|
|
|
|
{
|
|
|
|
|
long offset = 0;
|
|
|
|
|
unsigned long numitems, bytesleft = 0;
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
/* This is used to test the growth handling. */
|
|
|
|
|
unsigned long length = 4UL;
|
|
|
|
|
#else
|
|
|
|
|
unsigned long length = (100000UL + 3) / 4;
|
|
|
|
|
#endif
|
|
|
|
|
unsigned char *buf = NULL, *chunk = NULL;
|
|
|
|
|
size_t buflen = 0, chunkbytesize = 0;
|
|
|
|
|
int format;
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
TRACE ();
|
2008-09-14 18:23:17 +02:00
|
|
|
|
|
|
|
|
|
if(None == property)
|
|
|
|
|
return True;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
unsigned long newbuflen;
|
|
|
|
|
unsigned char *newbuf;
|
|
|
|
|
|
|
|
|
|
if (Success != XGetWindowProperty (x_dpy, win, property,
|
|
|
|
|
offset, length, delete,
|
|
|
|
|
AnyPropertyType,
|
|
|
|
|
type, &format, &numitems,
|
|
|
|
|
&bytesleft, &chunk)) {
|
|
|
|
|
free (buf);
|
|
|
|
|
return True;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
printf("format %d numitems %lu bytesleft %lu\n",
|
|
|
|
|
format, numitems, bytesleft);
|
|
|
|
|
|
|
|
|
|
printf("type %s\n", XGetAtomName(dis, *type));
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Format is the number of bits. */
|
|
|
|
|
chunkbytesize = numitems * (format / 8);
|
|
|
|
|
|
|
|
|
|
#ifdef TEST
|
|
|
|
|
printf("chunkbytesize %zu\n", chunkbytesize);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
newbuflen = buflen + chunkbytesize;
|
|
|
|
|
newbuf = realloc (buf, newbuflen);
|
|
|
|
|
if (NULL == newbuf) {
|
|
|
|
|
XFree (chunk);
|
|
|
|
|
free (buf);
|
|
|
|
|
return True;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy (newbuf + buflen, chunk, chunkbytesize);
|
|
|
|
|
XFree (chunk);
|
|
|
|
|
buf = newbuf;
|
|
|
|
|
buflen = newbuflen;
|
|
|
|
|
/* offset is a multiple of 32 bits*/
|
|
|
|
|
offset += chunkbytesize / 4;
|
|
|
|
|
} while (bytesleft > 0);
|
|
|
|
|
|
|
|
|
|
pdata->data = buf;
|
|
|
|
|
pdata->length = buflen;
|
|
|
|
|
|
|
|
|
|
return /*success*/ False;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
static unsigned long *
|
|
|
|
|
read_prop_32 (Window id, Atom prop, int *nitems_ret)
|
|
|
|
|
{
|
|
|
|
|
int r, format;
|
|
|
|
|
Atom type;
|
|
|
|
|
unsigned long nitems, bytes_after;
|
|
|
|
|
unsigned char *data;
|
|
|
|
|
|
|
|
|
|
r = XGetWindowProperty (x_dpy, id, prop, 0, 0,
|
|
|
|
|
False, AnyPropertyType, &type, &format,
|
|
|
|
|
&nitems, &bytes_after, &data);
|
|
|
|
|
|
|
|
|
|
if (r == Success && bytes_after != 0)
|
|
|
|
|
{
|
|
|
|
|
XFree (data);
|
|
|
|
|
r = XGetWindowProperty (x_dpy, id, prop, 0,
|
|
|
|
|
(bytes_after / 4) + 1, False,
|
|
|
|
|
AnyPropertyType, &type, &format,
|
|
|
|
|
&nitems, &bytes_after, &data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (r != Success)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
if (format != 32)
|
|
|
|
|
{
|
|
|
|
|
XFree (data);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*nitems_ret = nitems;
|
|
|
|
|
return (unsigned long *) data;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/*
|
|
|
|
|
* This should be called after a selection has been copied,
|
|
|
|
|
* or when the selection is unfinished before a transfer completes.
|
|
|
|
|
*/
|
|
|
|
|
- (void) release_pending
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
free_propdata (&pending.propdata);
|
|
|
|
|
pending.requestor = None;
|
|
|
|
|
pending.selection = None;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* Return True if an error occurs during an append.*/
|
|
|
|
|
/* Return False if the append succeeds. */
|
|
|
|
|
- (Bool) append_to_pending:(struct propdata *)pdata requestor:(Window)requestor
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-14 18:23:17 +02:00
|
|
|
|
unsigned char *newdata;
|
|
|
|
|
size_t newlength;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (requestor != pending.requestor)
|
|
|
|
|
{
|
|
|
|
|
[self release_pending];
|
|
|
|
|
pending.requestor = requestor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newlength = pending.propdata.length + pdata->length;
|
|
|
|
|
newdata = realloc(pending.propdata.data, newlength);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if(NULL == newdata)
|
|
|
|
|
{
|
|
|
|
|
perror("realloc propdata");
|
|
|
|
|
[self release_pending];
|
|
|
|
|
return True;
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
memcpy(newdata + pending.propdata.length, pdata->data, pdata->length);
|
|
|
|
|
pending.propdata.data = newdata;
|
|
|
|
|
pending.propdata.length = newlength;
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
return False;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
/* Called when X11 becomes active (i.e. has key focus) */
|
|
|
|
|
- (void) x_active:(Time)timestamp
|
|
|
|
|
{
|
2008-09-18 21:10:41 +02:00
|
|
|
|
static NSInteger changeCount;
|
|
|
|
|
NSInteger countNow;
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
countNow = [_pasteboard changeCount];
|
|
|
|
|
|
|
|
|
|
if (countNow != changeCount)
|
|
|
|
|
{
|
|
|
|
|
DB ("changed pasteboard!\n");
|
|
|
|
|
changeCount = countNow;
|
|
|
|
|
|
|
|
|
|
/*HMM should we pass CurrentTime instead?*/
|
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->primary, _selection_window, timestamp);
|
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->clipboard, _selection_window, timestamp);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
#if 0
|
2008-09-03 18:23:06 +02:00
|
|
|
|
if ([_pasteboard changeCount] != _my_last_change)
|
|
|
|
|
{
|
2008-09-19 08:02:48 +02:00
|
|
|
|
/*gstaplin: we should perhaps investigate something like this branch above...*/
|
2008-09-03 18:23:06 +02:00
|
|
|
|
if ([_pasteboard availableTypeFromArray: _known_types] != nil)
|
|
|
|
|
{
|
|
|
|
|
/* Pasteboard has data we should proxy; I think it makes
|
|
|
|
|
sense to put it on both CLIPBOARD and PRIMARY */
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->clipboard,
|
2008-09-03 18:23:06 +02:00
|
|
|
|
_selection_window, timestamp);
|
|
|
|
|
XSetSelectionOwner (x_dpy, XA_PRIMARY,
|
|
|
|
|
_selection_window, timestamp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
#endif
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called when X11 loses key focus */
|
|
|
|
|
- (void) x_inactive:(Time)timestamp
|
|
|
|
|
{
|
|
|
|
|
Window w;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
2008-09-14 18:23:17 +02:00
|
|
|
|
#if 0
|
2008-09-03 18:23:06 +02:00
|
|
|
|
if (_proxied_selection == XA_PRIMARY)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
w = XGetSelectionOwner (x_dpy, atoms->clipboard);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
|
|
|
|
if (w != None && w != _selection_window)
|
|
|
|
|
{
|
|
|
|
|
/* An X client has the selection, proxy it to the pasteboard */
|
|
|
|
|
|
|
|
|
|
_my_last_change = [_pasteboard declareTypes:_known_types owner:self];
|
2008-09-14 18:23:17 +02:00
|
|
|
|
_proxied_selection = atoms->clipboard;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
#endif
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Called when the Edit/Copy item on the main X11 menubar is selected
|
|
|
|
|
and no appkit window claims it. */
|
|
|
|
|
- (void) x_copy:(Time)timestamp
|
|
|
|
|
{
|
|
|
|
|
Window w;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
w = XGetSelectionOwner (x_dpy, atoms->primary);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None != w)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("requesting targets\n");
|
2008-09-14 18:23:17 +02:00
|
|
|
|
request_atom = atoms->targets;
|
|
|
|
|
XConvertSelection (x_dpy, atoms->primary, atoms->targets,
|
|
|
|
|
atoms->primary, _selection_window, CurrentTime);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
XBell (x_dpy, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/*
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
- (void) set_clipboard_manager
|
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None != XGetSelectionOwner (x_dpy, atoms->clipboard_manager))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "A clipboard manager is already running!\n"
|
|
|
|
|
"pbproxy can not continue!\n");
|
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->clipboard_manager, _selection_window,
|
|
|
|
|
CurrentTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This occurs when we previously owned a selection,
|
|
|
|
|
* and then lost it from another client.
|
|
|
|
|
*/
|
2008-09-03 18:23:06 +02:00
|
|
|
|
- (void) clear_event:(XSelectionClearEvent *)e
|
|
|
|
|
{
|
|
|
|
|
TRACE ();
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
DB ("e->selection %s\n", XGetAtomName (x_dpy, e->selection));
|
2008-09-14 18:23:17 +02:00
|
|
|
|
|
|
|
|
|
if (atoms->clipboard == e->selection)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* We lost ownership of the CLIPBOARD.
|
|
|
|
|
*/
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
[self claim_clipboard];
|
2008-09-14 18:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
else if (atoms->clipboard_manager == e->selection)
|
|
|
|
|
{
|
|
|
|
|
/* Another CLIPBOARD_MANAGER has set itself as owner.
|
|
|
|
|
* a) we can call [self set_clipboard_manager] here and risk a war.
|
2008-09-16 23:21:18 +02:00
|
|
|
|
* b) we can print a message and exit. Ideally we would popup a message box.
|
2008-09-14 18:23:17 +02:00
|
|
|
|
*/
|
|
|
|
|
fprintf (stderr, "error: another clipboard manager was started!\n");
|
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We greedily acquire the clipboard after it changes, and on startup.
|
|
|
|
|
*/
|
2008-09-15 21:35:46 +02:00
|
|
|
|
- (void) claim_clipboard
|
2008-09-14 18:23:17 +02:00
|
|
|
|
{
|
|
|
|
|
Window owner;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
|
|
|
|
if (NO == pbproxy_clipboard_to_pasteboard)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
owner = XGetSelectionOwner (x_dpy, atoms->clipboard);
|
|
|
|
|
if (None == owner)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* The owner probably died or we are just starting up pbproxy.
|
|
|
|
|
* Set pbproxy's _selection_window as the owner, and continue.
|
|
|
|
|
*/
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("No clipboard owner.\n");
|
2008-09-16 23:21:18 +02:00
|
|
|
|
[self own_clipboard];
|
2008-09-14 18:23:17 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("requesting targets\n");
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
request_atom = atoms->targets;
|
|
|
|
|
XConvertSelection (x_dpy, atoms->clipboard, atoms->targets,
|
|
|
|
|
atoms->clipboard, _selection_window, CurrentTime);
|
2008-09-16 23:21:18 +02:00
|
|
|
|
XFlush (x_dpy);
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* Now we will get a SelectionNotify event in the future. */
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
/* Greedily acquire the clipboard. */
|
|
|
|
|
- (void) own_clipboard
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
|
|
|
|
|
|
|
|
|
/* We should perhaps have a boundary limit on the number of iterations... */
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->clipboard, _selection_window,
|
|
|
|
|
CurrentTime);
|
|
|
|
|
} while (_selection_window != XGetSelectionOwner (x_dpy,
|
|
|
|
|
atoms->clipboard));
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
- (void) init_reply:(XEvent *)reply request:(XSelectionRequestEvent *)e
|
|
|
|
|
{
|
|
|
|
|
reply->xselection.type = SelectionNotify;
|
|
|
|
|
reply->xselection.selection = e->selection;
|
|
|
|
|
reply->xselection.target = e->target;
|
|
|
|
|
reply->xselection.requestor = e->requestor;
|
|
|
|
|
reply->xselection.time = e->time;
|
|
|
|
|
reply->xselection.property = None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) send_reply:(XEvent *)reply
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* We are supposed to use an empty event mask, and not propagate
|
|
|
|
|
* the event, according to the ICCCM.
|
|
|
|
|
*/
|
|
|
|
|
XSendEvent (x_dpy, reply->xselection.requestor, False, 0, reply);
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
/*
|
|
|
|
|
* This responds to a TARGETS request.
|
2008-09-18 21:10:41 +02:00
|
|
|
|
* The result is a list of a ATOMs that correspond to the types available
|
2008-09-16 23:21:18 +02:00
|
|
|
|
* for a selection.
|
|
|
|
|
* For instance an application might provide a UTF8_STRING and a STRING
|
|
|
|
|
* (in Latin-1 encoding). The requestor can then make the choice based on
|
2008-09-18 21:10:41 +02:00
|
|
|
|
* the list.
|
2008-09-16 23:21:18 +02:00
|
|
|
|
*/
|
|
|
|
|
- (void) send_targets:(XSelectionRequestEvent *)e
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
|
|
|
|
XEvent reply;
|
2008-09-18 21:10:41 +02:00
|
|
|
|
NSArray *pbtypes;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self init_reply:&reply request:e];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
pbtypes = [_pasteboard types];
|
|
|
|
|
if (pbtypes)
|
|
|
|
|
{
|
2008-09-19 08:02:48 +02:00
|
|
|
|
long list[6];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
long count = 0;
|
|
|
|
|
|
|
|
|
|
if ([pbtypes containsObject:NSStringPboardType])
|
|
|
|
|
{
|
2008-09-19 08:02:48 +02:00
|
|
|
|
/* We have a string type that we can convert to UTF8, or Latin-1... */
|
2008-09-18 21:10:41 +02:00
|
|
|
|
DB ("NSStringPboardType\n");
|
|
|
|
|
list[count] = atoms->utf8_string;
|
|
|
|
|
++count;
|
|
|
|
|
list[count] = atoms->string;
|
|
|
|
|
++count;
|
2008-09-19 08:02:48 +02:00
|
|
|
|
list[count] = atoms->compound_text;
|
|
|
|
|
++count;
|
2008-09-18 21:10:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([pbtypes containsObject:NSTIFFPboardType]
|
|
|
|
|
|| [pbtypes containsObject:NSPICTPboardType])
|
|
|
|
|
{
|
|
|
|
|
/* We can convert a TIFF or PICT to a PNG or JPEG. */
|
|
|
|
|
DB ("NSTIFFPboardType or NSPICTPboardType\n");
|
|
|
|
|
list[count] = atoms->image_png;
|
|
|
|
|
++count;
|
|
|
|
|
list[count] = atoms->image_jpeg;
|
|
|
|
|
++count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
|
{
|
|
|
|
|
/* We have a list of ATOMs to send. */
|
|
|
|
|
XChangeProperty (x_dpy, e->requestor, e->property, atoms->atom, 32,
|
|
|
|
|
PropModeReplace, (unsigned char *) list, count);
|
|
|
|
|
|
|
|
|
|
reply.xselection.property = e->property;
|
|
|
|
|
}
|
2008-09-16 23:21:18 +02:00
|
|
|
|
}
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self send_reply:&reply];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
- (void) send_string:(XSelectionRequestEvent *)e utf8:(BOOL)utf8
|
|
|
|
|
{
|
|
|
|
|
XEvent reply;
|
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self init_reply:&reply request:e];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
pbtypes = [_pasteboard types];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
if ([pbtypes containsObject: NSStringPboardType])
|
|
|
|
|
{
|
|
|
|
|
NSString *data = [_pasteboard stringForType:NSStringPboardType];
|
|
|
|
|
if (nil != data)
|
|
|
|
|
{
|
|
|
|
|
const char *bytes;
|
|
|
|
|
NSUInteger length;
|
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
if (utf8)
|
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
bytes = [data UTF8String];
|
|
|
|
|
/*
|
|
|
|
|
* We don't want the UTF-8 string length here.
|
|
|
|
|
* We want the length in bytes.
|
|
|
|
|
*/
|
|
|
|
|
length = [data lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
DB ("UTF-8\n");
|
2008-09-19 08:02:48 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-09-18 21:10:41 +02:00
|
|
|
|
DB ("Latin-1\n");
|
2008-09-16 23:21:18 +02:00
|
|
|
|
bytes = [data cStringUsingEncoding:NSISOLatin1StringEncoding];
|
|
|
|
|
length = [data lengthOfBytesUsingEncoding:NSASCIIStringEncoding];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
XChangeProperty (x_dpy, e->requestor, e->property, e->target,
|
|
|
|
|
8, PropModeReplace, (unsigned char *) bytes, length);
|
|
|
|
|
|
|
|
|
|
reply.xselection.property = e->property;
|
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
/*Should we [data release] here?*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self send_reply:&reply];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
- (void) send_compound_text:(XSelectionRequestEvent *)e
|
|
|
|
|
{
|
|
|
|
|
XEvent reply;
|
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
|
|
|
|
|
|
|
|
|
[self init_reply:&reply request:e];
|
|
|
|
|
|
|
|
|
|
pbtypes = [_pasteboard types];
|
|
|
|
|
|
|
|
|
|
if ([pbtypes containsObject: NSStringPboardType])
|
|
|
|
|
{
|
|
|
|
|
NSString *data = [_pasteboard stringForType:NSStringPboardType];
|
|
|
|
|
if (nil != data)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* Cast to (void *) to avoid a const warning.
|
|
|
|
|
* AFAIK Xutf8TextListToTextProperty does not modify the input memory.
|
|
|
|
|
*/
|
|
|
|
|
void *utf8 = (void *)[data UTF8String];
|
|
|
|
|
char *list[] = { utf8, NULL };
|
|
|
|
|
XTextProperty textprop;
|
|
|
|
|
|
|
|
|
|
textprop.value = NULL;
|
|
|
|
|
|
|
|
|
|
if (Success == Xutf8TextListToTextProperty (x_dpy, list, 1,
|
|
|
|
|
XCompoundTextStyle,
|
|
|
|
|
&textprop))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (8 != textprop.format)
|
|
|
|
|
DB ("textprop.format is unexpectedly not 8 - it's %d instead\n",
|
|
|
|
|
textprop.format);
|
|
|
|
|
|
|
|
|
|
XChangeProperty (x_dpy, e->requestor, e->property,
|
|
|
|
|
atoms->compound_text, textprop.format,
|
|
|
|
|
PropModeReplace, textprop.value,
|
|
|
|
|
textprop.nitems);
|
|
|
|
|
|
|
|
|
|
reply.xselection.property = e->property;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (textprop.value)
|
|
|
|
|
XFree (textprop.value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self send_reply:&reply];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) send_multiple:(XSelectionRequestEvent *)e
|
|
|
|
|
{
|
|
|
|
|
XEvent reply;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
|
|
|
|
|
|
|
|
|
[self init_reply:&reply request:e];
|
|
|
|
|
|
|
|
|
|
if (None != e->property)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[self send_reply:&reply];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
- (void) send_image:(XSelectionRequestEvent *)e
|
|
|
|
|
{
|
|
|
|
|
XEvent reply;
|
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
NSString *type = nil;
|
|
|
|
|
NSBitmapImageFileType imagetype = /*quiet warning*/ NSPNGFileType;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self init_reply:&reply request:e];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
|
|
|
|
pbtypes = [_pasteboard types];
|
|
|
|
|
|
|
|
|
|
if (pbtypes)
|
|
|
|
|
{
|
|
|
|
|
if ([pbtypes containsObject:NSTIFFPboardType])
|
|
|
|
|
type = NSTIFFPboardType;
|
|
|
|
|
|
|
|
|
|
if ([pbtypes containsObject:NSPICTPboardType])
|
|
|
|
|
type = NSPICTPboardType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e->target == atoms->image_png)
|
|
|
|
|
imagetype = NSPNGFileType;
|
|
|
|
|
else if (e->target == atoms->image_jpeg)
|
|
|
|
|
imagetype = NSJPEGFileType;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (type)
|
|
|
|
|
{
|
|
|
|
|
NSData *data;
|
2008-09-19 08:02:48 +02:00
|
|
|
|
data = [_pasteboard dataForType:type];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
|
|
|
|
if (data)
|
|
|
|
|
{
|
|
|
|
|
NSBitmapImageRep *bmimage = [[NSBitmapImageRep alloc] initWithData:data];
|
|
|
|
|
|
|
|
|
|
if (bmimage)
|
|
|
|
|
{
|
|
|
|
|
NSDictionary *dict;
|
|
|
|
|
NSData *encdata;
|
|
|
|
|
|
|
|
|
|
dict = [[NSDictionary alloc] init];
|
|
|
|
|
encdata = [bmimage representationUsingType:imagetype properties:dict];
|
|
|
|
|
if (encdata)
|
|
|
|
|
{
|
|
|
|
|
NSUInteger length;
|
|
|
|
|
const void *bytes;
|
|
|
|
|
|
|
|
|
|
length = [encdata length];
|
|
|
|
|
bytes = [encdata bytes];
|
|
|
|
|
|
|
|
|
|
XChangeProperty (x_dpy, e->requestor, e->property, e->target,
|
|
|
|
|
8, PropModeReplace, bytes, length);
|
|
|
|
|
|
|
|
|
|
reply.xselection.property = e->property;
|
|
|
|
|
|
|
|
|
|
DB ("changed property for %s\n", XGetAtomName (x_dpy, e->target));
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self send_reply:&reply];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
- (void)send_none:(XSelectionRequestEvent *)e
|
2008-09-16 23:21:18 +02:00
|
|
|
|
{
|
2008-09-18 21:10:41 +02:00
|
|
|
|
XEvent reply;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self init_reply:&reply request:e];
|
|
|
|
|
[self send_reply:&reply];
|
2008-09-18 21:10:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Another client requested the data or targets of data available from the clipboard. */
|
|
|
|
|
- (void)request_event:(XSelectionRequestEvent *)e
|
|
|
|
|
{
|
|
|
|
|
TRACE ();
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
/* TODO We should also keep track of the time of the selection, and
|
|
|
|
|
* according to the ICCCM "refuse the request" if the event timestamp
|
|
|
|
|
* is before we owned it.
|
|
|
|
|
* What should we base the time on? How can we get the current time just
|
|
|
|
|
* before an XSetSelectionOwner? Is it the server's time, or the clients?
|
|
|
|
|
* According to the XSelectionRequestEvent manual page, the Time value
|
|
|
|
|
* may be set to CurrentTime or a time, so that makes it a bit different.
|
|
|
|
|
* Perhaps we should just punt and ignore races.
|
|
|
|
|
*/
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
2008-09-19 08:02:48 +02:00
|
|
|
|
/*TODO we need a COMPOUND_TEXT test app*/
|
|
|
|
|
/*TODO we need a MULTIPLE test app*/
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
DB ("e->target %s\n", XGetAtomName (x_dpy, e->target));
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
if (e->target == atoms->targets)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
/* The paste requestor wants to know what TARGETS we support. */
|
|
|
|
|
[self send_targets:e];
|
|
|
|
|
}
|
|
|
|
|
else if (e->target == atoms->multiple)
|
|
|
|
|
{
|
|
|
|
|
[self send_multiple:e];
|
|
|
|
|
}
|
|
|
|
|
else if (e->target == atoms->utf8_string)
|
|
|
|
|
{
|
|
|
|
|
[self send_string:e utf8:YES];
|
|
|
|
|
}
|
|
|
|
|
else if (e->target == atoms->string)
|
|
|
|
|
{
|
|
|
|
|
[self send_string:e utf8:NO];
|
2008-09-19 08:02:48 +02:00
|
|
|
|
}
|
|
|
|
|
else if (e->target == atoms->compound_text)
|
|
|
|
|
{
|
|
|
|
|
[self send_compound_text:e];
|
|
|
|
|
}
|
|
|
|
|
else if (e->target == atoms->multiple)
|
|
|
|
|
{
|
|
|
|
|
[self send_multiple:e];
|
|
|
|
|
}
|
2008-09-18 21:10:41 +02:00
|
|
|
|
else if (e->target == atoms->image_png || e->target == atoms->image_jpeg)
|
|
|
|
|
{
|
|
|
|
|
[self send_image:e];
|
|
|
|
|
}
|
2008-09-16 23:21:18 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
2008-09-18 21:10:41 +02:00
|
|
|
|
[self send_none:e];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* This handles the events resulting from an XConvertSelection request. */
|
2008-09-03 18:23:06 +02:00
|
|
|
|
- (void) notify_event:(XSelectionEvent *)e
|
|
|
|
|
{
|
|
|
|
|
Atom type;
|
2008-09-14 18:23:17 +02:00
|
|
|
|
struct propdata pdata;
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
[self release_pending];
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None == e->property) {
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("e->property is None.\n");
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
|
|
|
|
if (pbproxy_clipboard_to_pasteboard && e->selection == atoms->clipboard)
|
|
|
|
|
[self own_clipboard];
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* Nothing is selected. */
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
DB ("e->selection %s\n", XGetAtomName (x_dpy, e->selection));
|
|
|
|
|
DB ("e->property %s\n", XGetAtomName (x_dpy, e->property));
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (is_incr_type (e))
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/*
|
|
|
|
|
* This is an INCR-style transfer, which means that we
|
|
|
|
|
* will get the data after a series of PropertyNotify events.
|
|
|
|
|
*/
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("is INCR\n");
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (get_property (e->requestor, e->property, &pdata, /*Delete*/ True, &type))
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-17 05:09:22 +02:00
|
|
|
|
/*
|
|
|
|
|
* The get_property error could have occured with the clipboard atom.
|
|
|
|
|
* Greedily own the clipboard again.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
if (pbproxy_clipboard_to_pasteboard && e->selection == atoms->clipboard)
|
|
|
|
|
[self own_clipboard];
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
return;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
free_propdata (&pdata);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
pending.requestor = e->requestor;
|
|
|
|
|
pending.selection = e->selection;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (get_property (e->requestor, e->property, &pdata, /*Delete*/ True, &type))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* We have the complete selection data.*/
|
|
|
|
|
[self handle_selection: e->selection type:type propdata:&pdata];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
DB ("handled selection with the first notify_event\n");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This may have been the end of the clipboard request from [self claim_clipboard].
|
|
|
|
|
* If so, then we should own the contents now.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
if (pbproxy_clipboard_to_pasteboard && e->selection == atoms->clipboard)
|
|
|
|
|
[self own_clipboard];
|
2008-09-14 18:23:17 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
/* This is used for INCR transfers. See the ICCCM for the details. */
|
2008-09-17 05:09:22 +02:00
|
|
|
|
/* This is used to retrieve PRIMARY and CLIPBOARD selections. */
|
2008-09-14 18:23:17 +02:00
|
|
|
|
- (void) property_event:(XPropertyEvent *)e
|
|
|
|
|
{
|
|
|
|
|
struct propdata pdata;
|
|
|
|
|
Atom type;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
|
|
|
|
if (None != e->atom)
|
|
|
|
|
DB ("e->atom %s\n", XGetAtomName (x_dpy, e->atom));
|
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None != pending.requestor && PropertyNewValue == e->state)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
DB ("pending.requestor 0x%lx\n", pending.requestor);
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (get_property (e->window, e->atom, &pdata, /*Delete*/ True, &type))
|
2008-09-16 23:21:18 +02:00
|
|
|
|
{
|
|
|
|
|
if (pbproxy_clipboard_to_pasteboard && pending.selection == atoms->clipboard)
|
|
|
|
|
[self own_clipboard];
|
|
|
|
|
|
|
|
|
|
[self release_pending];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (0 == pdata.length)
|
|
|
|
|
{
|
|
|
|
|
/* We completed the transfer. */
|
2008-09-17 05:09:22 +02:00
|
|
|
|
[self handle_selection:pending.selection type:type propdata:&pending.propdata];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
if (pbproxy_clipboard_to_pasteboard && pending.selection == atoms->clipboard)
|
|
|
|
|
[self own_clipboard];
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
pending.propdata = null_propdata;
|
|
|
|
|
pending.requestor = None;
|
|
|
|
|
pending.selection = None;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
[self append_to_pending: &pdata requestor: e->window];
|
|
|
|
|
}
|
2008-09-17 05:09:22 +02:00
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
- (void) handle_targets: (Atom)selection propdata:(struct propdata *)pdata
|
|
|
|
|
{
|
2008-09-15 21:35:46 +02:00
|
|
|
|
/* Find a type we can handle and prefer from the list of ATOMs. */
|
2008-09-16 23:21:18 +02:00
|
|
|
|
Atom preferred;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
preferred = find_preferred (pdata);
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None == preferred)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This isn't required by the ICCCM, but some apps apparently
|
|
|
|
|
* don't respond to TARGETS properly.
|
|
|
|
|
*/
|
|
|
|
|
preferred = XA_STRING;
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
DB ("requesting %s\n", XGetAtomName (x_dpy, preferred));
|
2008-09-14 18:23:17 +02:00
|
|
|
|
request_atom = preferred;
|
|
|
|
|
XConvertSelection (x_dpy, selection, preferred, selection,
|
|
|
|
|
_selection_window, CurrentTime);
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
/* This handles the image type of selection (typically in CLIPBOARD). */
|
2008-09-17 19:59:36 +02:00
|
|
|
|
/* We convert to a TIFF, so that other applications can paste more easily. */
|
2008-09-19 08:02:48 +02:00
|
|
|
|
- (void) handle_image: (struct propdata *)pdata
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-15 21:35:46 +02:00
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
NSUInteger length;
|
2008-09-17 19:59:36 +02:00
|
|
|
|
NSData *data, *tiff;
|
|
|
|
|
NSBitmapImageRep *bmimage;
|
2008-09-15 21:35:46 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
TRACE ();
|
|
|
|
|
|
2008-09-17 19:59:36 +02:00
|
|
|
|
length = pdata->length;
|
|
|
|
|
data = [[NSData alloc] initWithBytes:pdata->data length:length];
|
|
|
|
|
|
|
|
|
|
if (nil == data)
|
2008-09-15 21:35:46 +02:00
|
|
|
|
{
|
2008-09-17 19:59:36 +02:00
|
|
|
|
DB ("unable to create NSData object!\n");
|
2008-09-15 21:35:46 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2008-09-17 19:59:36 +02:00
|
|
|
|
|
|
|
|
|
bmimage = [[NSBitmapImageRep alloc] initWithData:data];
|
|
|
|
|
|
|
|
|
|
if (nil == bmimage)
|
2008-09-15 21:35:46 +02:00
|
|
|
|
{
|
2008-09-17 19:59:36 +02:00
|
|
|
|
DB ("unable to create NSBitmapImageRep!\n");
|
|
|
|
|
return;
|
2008-09-15 21:35:46 +02:00
|
|
|
|
}
|
2008-09-17 19:59:36 +02:00
|
|
|
|
|
|
|
|
|
@try
|
2008-09-15 21:35:46 +02:00
|
|
|
|
{
|
2008-09-17 19:59:36 +02:00
|
|
|
|
tiff = [bmimage TIFFRepresentation];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@catch (NSException *e)
|
|
|
|
|
{
|
|
|
|
|
DB ("NSTIFFException!\n");
|
|
|
|
|
[data release];
|
|
|
|
|
[bmimage release];
|
2008-09-15 21:35:46 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2008-09-17 19:59:36 +02:00
|
|
|
|
|
|
|
|
|
pbtypes = [NSArray arrayWithObjects:NSTIFFPboardType, nil];
|
|
|
|
|
|
|
|
|
|
if (nil == pbtypes)
|
|
|
|
|
{
|
|
|
|
|
[tiff release];
|
|
|
|
|
[data release];
|
|
|
|
|
[bmimage release];
|
|
|
|
|
}
|
2008-09-15 21:35:46 +02:00
|
|
|
|
|
|
|
|
|
[_pasteboard declareTypes:pbtypes owner:self];
|
2008-09-17 19:59:36 +02:00
|
|
|
|
if (YES != [_pasteboard setData:data forType:NSTIFFPboardType])
|
2008-09-15 21:35:46 +02:00
|
|
|
|
{
|
|
|
|
|
DB ("writing pasteboard data failed!\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[pbtypes release];
|
|
|
|
|
[data release];
|
2008-09-17 19:59:36 +02:00
|
|
|
|
[tiff release];
|
|
|
|
|
[bmimage release];
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* This handles the UTF8_STRING type of selection. */
|
|
|
|
|
- (void) handle_utf8_string: (struct propdata *)pdata
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
NSString *string;
|
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
|
|
|
|
|
TRACE ();
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
2008-09-16 23:21:18 +02:00
|
|
|
|
string = [[NSString alloc] initWithBytes:pdata->data length:pdata->length encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
if (nil == string)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
pbtypes = [NSArray arrayWithObjects:NSStringPboardType, nil];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
if (nil != pbtypes)
|
|
|
|
|
{
|
|
|
|
|
[_pasteboard declareTypes:pbtypes owner:self];
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
|
|
|
|
if (YES != [_pasteboard setString:string forType:NSStringPboardType]) {
|
|
|
|
|
DB ("_pasteboard setString:forType: failed!\n");
|
|
|
|
|
}
|
2008-09-16 23:21:18 +02:00
|
|
|
|
[pbtypes release];
|
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
[string release];
|
2008-09-17 05:09:22 +02:00
|
|
|
|
|
|
|
|
|
DB ("done handling utf8 string\n");
|
2008-09-14 18:23:17 +02:00
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
/* This handles the STRING type, which should be in Latin-1. */
|
2008-09-14 18:23:17 +02:00
|
|
|
|
- (void) handle_string: (struct propdata *)pdata
|
|
|
|
|
{
|
2008-09-16 23:21:18 +02:00
|
|
|
|
NSString *string;
|
|
|
|
|
NSArray *pbtypes;
|
|
|
|
|
|
|
|
|
|
string = [[NSString alloc] initWithBytes:pdata->data length:pdata->length encoding:NSISOLatin1StringEncoding];
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
if (nil == string)
|
|
|
|
|
return;
|
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
pbtypes = [NSArray arrayWithObjects:NSStringPboardType, nil];
|
2008-09-16 23:21:18 +02:00
|
|
|
|
|
|
|
|
|
if (nil != pbtypes)
|
|
|
|
|
{
|
|
|
|
|
[_pasteboard declareTypes:pbtypes owner:self];
|
|
|
|
|
[_pasteboard setString:string forType:NSStringPboardType];
|
|
|
|
|
[pbtypes release];
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
[string release];
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
/* This is called when the selection is completely retrieved from another client. */
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* Warning: this frees the propdata in most cases. */
|
|
|
|
|
- (void) handle_selection:(Atom)selection type:(Atom)type propdata:(struct propdata *)pdata
|
|
|
|
|
{
|
|
|
|
|
if (request_atom == atoms->targets && type == atoms->atom)
|
|
|
|
|
{
|
|
|
|
|
[self handle_targets:selection propdata:pdata];
|
|
|
|
|
}
|
|
|
|
|
else if (type == atoms->image_png)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self handle_image:pdata];
|
2008-09-14 18:23:17 +02:00
|
|
|
|
}
|
2008-09-15 21:35:46 +02:00
|
|
|
|
else if (type == atoms->image_jpeg)
|
|
|
|
|
{
|
2008-09-19 08:02:48 +02:00
|
|
|
|
[self handle_image:pdata];
|
2008-09-15 21:35:46 +02:00
|
|
|
|
}
|
2008-09-14 18:23:17 +02:00
|
|
|
|
else if (type == atoms->utf8_string)
|
|
|
|
|
{
|
|
|
|
|
[self handle_utf8_string:pdata];
|
|
|
|
|
}
|
|
|
|
|
else if (type == atoms->string)
|
|
|
|
|
{
|
|
|
|
|
[self handle_string:pdata];
|
|
|
|
|
}
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
|
|
|
|
free_propdata(pdata);
|
2008-09-14 18:23:17 +02:00
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
if (selection == atoms->clipboard)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
2008-09-14 18:23:17 +02:00
|
|
|
|
/* We greedily take the CLIPBOARD selection whenever it changes. */
|
|
|
|
|
XSetSelectionOwner (x_dpy, atoms->clipboard, _selection_window,
|
|
|
|
|
CurrentTime);
|
|
|
|
|
}
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
|
|
|
|
|
/* NSPasteboard-required methods */
|
|
|
|
|
|
2008-09-17 05:09:22 +02:00
|
|
|
|
- (void)paste:(id)sender
|
|
|
|
|
{
|
|
|
|
|
TRACE ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)pasteboard:(NSPasteboard *)pb provideDataForType:(NSString *)type
|
|
|
|
|
{
|
|
|
|
|
TRACE ();
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-18 21:10:41 +02:00
|
|
|
|
- (void)pasteboardChangedOwner:(NSPasteboard *)pb
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
|
|
|
|
TRACE ();
|
|
|
|
|
|
|
|
|
|
/* Right now we don't care with this. */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Allocation */
|
|
|
|
|
|
|
|
|
|
- init
|
|
|
|
|
{
|
|
|
|
|
unsigned long pixel;
|
|
|
|
|
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self == nil)
|
|
|
|
|
return nil;
|
|
|
|
|
|
|
|
|
|
_pasteboard = [[NSPasteboard generalPasteboard] retain];
|
|
|
|
|
|
|
|
|
|
_known_types = [[NSArray arrayWithObject:NSStringPboardType] retain];
|
|
|
|
|
|
|
|
|
|
pixel = BlackPixel (x_dpy, DefaultScreen (x_dpy));
|
|
|
|
|
_selection_window = XCreateSimpleWindow (x_dpy, DefaultRootWindow (x_dpy),
|
|
|
|
|
0, 0, 1, 1, 0, pixel, pixel);
|
|
|
|
|
|
2008-09-15 21:35:46 +02:00
|
|
|
|
/* This is used to get PropertyNotify events when doing INCR transfers. */
|
2008-09-14 18:23:17 +02:00
|
|
|
|
XSelectInput (x_dpy, _selection_window, PropertyChangeMask);
|
|
|
|
|
|
|
|
|
|
request_atom = None;
|
|
|
|
|
|
|
|
|
|
init_propdata (&pending.propdata);
|
|
|
|
|
pending.requestor = None;
|
|
|
|
|
pending.selection = None;
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
2008-09-18 21:10:41 +02:00
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
[_pasteboard releaseGlobally];
|
|
|
|
|
[_pasteboard release];
|
|
|
|
|
_pasteboard = nil;
|
|
|
|
|
|
|
|
|
|
[_known_types release];
|
|
|
|
|
_known_types = nil;
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
if (None != _selection_window)
|
2008-09-03 18:23:06 +02:00
|
|
|
|
{
|
|
|
|
|
XDestroyWindow (x_dpy, _selection_window);
|
2008-09-14 18:23:17 +02:00
|
|
|
|
_selection_window = None;
|
2008-09-03 18:23:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-14 18:23:17 +02:00
|
|
|
|
free_propdata (&pending.propdata);
|
|
|
|
|
|
2008-09-03 18:23:06 +02:00
|
|
|
|
[super dealloc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|