summaryrefslogtreecommitdiff
path: root/hw/xquartz/pbproxy/x-selection.m
blob: 5b2ba9cc0169f3e1074b7178d20249250e3e0bd7 (plain)
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/* 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"

#include <X11/Xatom.h>

#include <unistd.h>

@implementation x_selection

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;
}

float
get_time (void)
{
  extern void Microseconds ();
  UnsignedWide usec;
  long long ll;

  Microseconds (&usec);
  ll = ((long long) usec.hi << 32) | usec.lo;

  return ll / 1e6;
}

static Bool
IfEventWithTimeout (Display *dpy, XEvent *e, int timeout,
		    Bool (*pred) (Display *, XEvent *, XPointer),
		    XPointer arg)
{
    float start = get_time ();
    fd_set fds;
    struct timeval tv;

    do {
	if (XCheckIfEvent (x_dpy, e, pred, arg))
	    return True;

	FD_ZERO (&fds);
	FD_SET (ConnectionNumber (x_dpy), &fds);
	tv.tv_usec = 0;
	tv.tv_sec = timeout;

	if (select (FD_SETSIZE, &fds, NULL, NULL, &tv) != 1)
	    break;

    } while (start + timeout > get_time ());

    return False;
}

/* Called when X11 becomes active (i.e. has key focus) */
- (void) x_active:(Time)timestamp
{
    TRACE ();

    if ([_pasteboard changeCount] != _my_last_change)
    {
	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 */

	    XSetSelectionOwner (x_dpy, x_atom_clipboard,
				_selection_window, timestamp);
	    XSetSelectionOwner (x_dpy, XA_PRIMARY,
				_selection_window, timestamp);
	}
    }
}

/* Called when X11 loses key focus */
- (void) x_inactive:(Time)timestamp
{
    Window w;

    TRACE ();

    if (_proxied_selection == XA_PRIMARY)
      return;

    w = XGetSelectionOwner (x_dpy, x_atom_clipboard);

    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];
	_proxied_selection = x_atom_clipboard;
    }
}

/* 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;

    /* Lazily copies the PRIMARY selection to the pasteboard. */

    w = XGetSelectionOwner (x_dpy, XA_PRIMARY);

    if (w != None && w != _selection_window)
    {
	XSetSelectionOwner (x_dpy, x_atom_clipboard,
			    _selection_window, timestamp);
	_my_last_change = [_pasteboard declareTypes:_known_types owner:self];
	_proxied_selection = XA_PRIMARY;
    }
    else
    {
	XBell (x_dpy, 0);
    }
}


/* X events */

- (void) clear_event:(XSelectionClearEvent *)e
{
    TRACE ();

    /* Right now we don't care about this. */
}

static Atom
convert_1 (XSelectionRequestEvent *e, NSString *data, Atom target, Atom prop)
{
    Atom ret = None;

    if (data == nil)
	return ret;

    if (target == x_atom_text)
	target = x_atom_utf8_string;

    if (target == XA_STRING
	|| target == x_atom_cstring
	|| target == x_atom_utf8_string)
    {
	const char *bytes;

	if (target == XA_STRING)
	    bytes = [data lossyCString];
	else
	    bytes = [data UTF8String];

	if (bytes != NULL)
	{
	    XChangeProperty (x_dpy, e->requestor, prop, target,
			     8, PropModeReplace, (unsigned char *) bytes,
			     strlen (bytes));
	    ret = prop;
	}
    }
    /* FIXME: handle COMPOUND_TEXT target */

    return ret;
}

- (void) request_event:(XSelectionRequestEvent *)e
{
    /* Someone's asking us for the data on the pasteboard */

    XEvent reply;
    NSString *data;
    Atom target;

    TRACE ();

    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;

    target = e->target;

    if (target == x_atom_targets)
    {
	long data[2];

	data[0] = x_atom_utf8_string;
	data[1] = XA_STRING;

	XChangeProperty (x_dpy, e->requestor, e->property, target,
			 8, PropModeReplace, (unsigned char *) &data,
			 sizeof (data));
	reply.xselection.property = e->property;
    }
    else if (target == x_atom_multiple)
    {
	if (e->property != None)
	{
	    int i, nitems;
	    unsigned long *atoms;

	    atoms = read_prop_32 (e->requestor, e->property, &nitems);

	    if (atoms != NULL)
	    {
		data = [_pasteboard stringForType:NSStringPboardType];

		for (i = 0; i < nitems; i += 2)
		{
		    Atom target = atoms[i], prop = atoms[i+1];

		    atoms[i+1] = convert_1 (e, data, target, prop);
		}

		XChangeProperty (x_dpy, e->requestor, e->property, target,
				 32, PropModeReplace, (unsigned char *) atoms,
				 nitems);
		XFree (atoms);
	    }
	}
    }

    data = [_pasteboard stringForType:NSStringPboardType];
    if (data != nil)
    {
	reply.xselection.property = convert_1 (e, data, target, e->property);
    }

    XSendEvent (x_dpy, e->requestor, False, 0, &reply);
}

- (void) notify_event:(XSelectionEvent *)e
{
    /* Someone sent us data we're waiting for. */

    Atom type;
    int format, r, offset;
    unsigned long nitems, bytes_after;
    unsigned char *data, *buf;
    NSString *string;

    TRACE ();

    if (e->target == x_atom_targets)
    {
	/* Was trying to fetch the TARGETS property; it lists the
	   formats supported by the selection owner. */

	unsigned long *atoms;
	int natoms;
	int i, utf8_i = -1, string_i = -1;

	if (e->property != None
	    && (atoms = read_prop_32 (e->requestor,
				      e->property, &natoms)) != NULL)
	{
	    for (i = 0; i < natoms; i++)
	    {
		if (atoms[i] == XA_STRING)
		    string_i = i;
		else if (atoms[i] == x_atom_utf8_string)
		    utf8_i = i;
	    }
	    XFree (atoms);
	}

	/* May as well try as STRING if nothing else, it can only
	   fail, and it will help broken clients who don't support
	   the TARGETS selection.. */

	if (utf8_i >= 0)
	    type = x_atom_utf8_string;
	else
	    type = XA_STRING;

	XConvertSelection (x_dpy, e->selection, type,
			   e->selection, e->requestor, e->time);
	_pending_notify = YES;
	return;
    }

    if (e->property == None)
	return;				/* FIXME: notify pasteboard? */

    /* Should be the data. Find out how big it is and what format it's in. */

    r = XGetWindowProperty (x_dpy, e->requestor, e->property,
			    0, 0, False, AnyPropertyType, &type,
			    &format, &nitems, &bytes_after, &data);
    if (r != Success)
	return;

    XFree (data);
    if (type == None || format != 8)
	return;

    bytes_after += nitems;
    
    /* Read it into a buffer. */

    buf = malloc (bytes_after + 1);
    if (buf == NULL)
	return;

    for (offset = 0; bytes_after > 0; offset += nitems)
    {
	r = XGetWindowProperty (x_dpy, e->requestor, e->property,
				offset / 4, (bytes_after / 4) + 1,
				False, AnyPropertyType, &type,
				&format, &nitems, &bytes_after, &data);
	if (r != Success)
	{
	    free (buf);
	    return;
	}

	memcpy (buf + offset, data, nitems);
	XFree (data);
    }
    buf[offset] = 0;
    XDeleteProperty (x_dpy, e->requestor, e->property);

    /* Convert to an NSString and write to the pasteboard. */

    if (type == XA_STRING)
	string = [NSString stringWithCString:(char *) buf];
    else /* if (type == x_atom_utf8_string) */
	string = [NSString stringWithUTF8String:(char *) buf];

    free (buf);

    [_pasteboard setString:string forType:NSStringPboardType];
}


/* NSPasteboard-required methods */

static Bool
selnotify_pred (Display *dpy, XEvent *e, XPointer arg)
{
    return e->type == SelectionNotify;
}

- (void) pasteboard:(NSPasteboard *)sender provideDataForType:(NSString *)type
{
    XEvent e;
    Atom request;

    TRACE ();

    /* Don't ask for the data yet, first find out which formats
       the selection owner supports. */

    request = x_atom_targets;

again:
    XConvertSelection (x_dpy, _proxied_selection, request,
		       _proxied_selection, _selection_window, CurrentTime);

    _pending_notify = YES;

    /* Seems like we need to be synchronous here.. Actually, this really
       sucks, since it means we could get deadlocked if people don't
       respond to our request. So we need to implement our own timeout
       code.. */

    while (_pending_notify
	   && IfEventWithTimeout (x_dpy, &e, 1, selnotify_pred, NULL))
    {
	_pending_notify = NO;
	[self notify_event:&e.xselection];
    }

    if (_pending_notify && request == x_atom_targets)
    {
	/* App didn't respond to request for TARGETS selection. Let's
	   try the STRING selection as a last resort.. Helps broken
	   applications (e.g. nedit, see #3199867) */

	request = XA_STRING;
	goto again;
    }

    _pending_notify = NO;
}

- (void) pasteboardChangedOwner:(NSPasteboard *)sender
{
    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);

    return self;
}

- (void) dealloc
{
    [_pasteboard releaseGlobally];
    [_pasteboard release];
    _pasteboard = nil;

    [_known_types release];
    _known_types = nil;

    if (_selection_window != 0)
    {
	XDestroyWindow (x_dpy, _selection_window);
	_selection_window = 0;
    }

    [super dealloc];
}

@end