aboutsummaryrefslogtreecommitdiffstats
path: root/src/xselect.c
diff options
context:
space:
mode:
authorJoakim Verona2011-06-16 00:22:07 +0200
committerJoakim Verona2011-06-16 00:22:07 +0200
commita7513ade3bc0fe79430d5541d88c9dcda0932bec (patch)
tree4383951ba698a11e9f8933a9d8c72e00aa872a10 /src/xselect.c
parent4bd51ad5c3445b644dfb017d5b57b10a90aa325f (diff)
parent4bba86e6210a74326e843a8fdc8409127105e1fe (diff)
downloademacs-a7513ade3bc0fe79430d5541d88c9dcda0932bec.tar.gz
emacs-a7513ade3bc0fe79430d5541d88c9dcda0932bec.zip
merge from upstream
Diffstat (limited to 'src/xselect.c')
-rw-r--r--src/xselect.c1529
1 files changed, 762 insertions, 767 deletions
diff --git a/src/xselect.c b/src/xselect.c
index 96c8b9c4c47..5b01fc22a42 100644
--- a/src/xselect.c
+++ b/src/xselect.c
@@ -20,6 +20,7 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20/* Rewritten by jwz */ 20/* Rewritten by jwz */
21 21
22#include <config.h> 22#include <config.h>
23#include <limits.h>
23#include <stdio.h> /* termhooks.h needs this */ 24#include <stdio.h> /* termhooks.h needs this */
24#include <setjmp.h> 25#include <setjmp.h>
25 26
@@ -38,32 +39,33 @@ along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
38#include "process.h" 39#include "process.h"
39#include "termhooks.h" 40#include "termhooks.h"
40#include "keyboard.h" 41#include "keyboard.h"
42#include "character.h"
41 43
42#include <X11/Xproto.h> 44#include <X11/Xproto.h>
43 45
44struct prop_location; 46struct prop_location;
47struct selection_data;
45 48
46static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom); 49static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
47static Atom symbol_to_x_atom (struct x_display_info *, Display *, 50static Atom symbol_to_x_atom (struct x_display_info *, Lisp_Object);
48 Lisp_Object); 51static void x_own_selection (Lisp_Object, Lisp_Object, Lisp_Object);
49static void x_own_selection (Lisp_Object, Lisp_Object); 52static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int,
50static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int); 53 struct x_display_info *);
51static void x_decline_selection_request (struct input_event *); 54static void x_decline_selection_request (struct input_event *);
52static Lisp_Object x_selection_request_lisp_error (Lisp_Object); 55static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
53static Lisp_Object queue_selection_requests_unwind (Lisp_Object); 56static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
54static Lisp_Object some_frame_on_display (struct x_display_info *);
55static Lisp_Object x_catch_errors_unwind (Lisp_Object); 57static Lisp_Object x_catch_errors_unwind (Lisp_Object);
56static void x_reply_selection_request (struct input_event *, int, 58static void x_reply_selection_request (struct input_event *, struct x_display_info *);
57 unsigned char *, int, Atom); 59static int x_convert_selection (struct input_event *, Lisp_Object, Lisp_Object,
60 Atom, int, struct x_display_info *);
58static int waiting_for_other_props_on_window (Display *, Window); 61static int waiting_for_other_props_on_window (Display *, Window);
59static struct prop_location *expect_property_change (Display *, Window, 62static struct prop_location *expect_property_change (Display *, Window,
60 Atom, int); 63 Atom, int);
61static void unexpect_property_change (struct prop_location *); 64static void unexpect_property_change (struct prop_location *);
62static Lisp_Object wait_for_property_change_unwind (Lisp_Object); 65static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
63static void wait_for_property_change (struct prop_location *); 66static void wait_for_property_change (struct prop_location *);
64static Lisp_Object x_get_foreign_selection (Lisp_Object, 67static Lisp_Object x_get_foreign_selection (Lisp_Object, Lisp_Object,
65 Lisp_Object, 68 Lisp_Object, Lisp_Object);
66 Lisp_Object);
67static void x_get_window_property (Display *, Window, Atom, 69static void x_get_window_property (Display *, Window, Atom,
68 unsigned char **, int *, 70 unsigned char **, int *,
69 Atom *, int *, unsigned long *, int); 71 Atom *, int *, unsigned long *, int);
@@ -97,18 +99,17 @@ static Lisp_Object clean_local_selection_data (Lisp_Object);
97#define TRACE0(fmt) (void) 0 99#define TRACE0(fmt) (void) 0
98#define TRACE1(fmt, a0) (void) 0 100#define TRACE1(fmt, a0) (void) 0
99#define TRACE2(fmt, a0, a1) (void) 0 101#define TRACE2(fmt, a0, a1) (void) 0
100#define TRACE3(fmt, a0, a1) (void) 0
101#endif 102#endif
102 103
103 104
104Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP, 105static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
105 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL, 106 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
106 QATOM_PAIR; 107 QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS;
107 108
108Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */ 109static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
109Lisp_Object QUTF8_STRING; /* This is a type of selection. */ 110static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
110 111
111Lisp_Object Qcompound_text_with_extensions; 112static Lisp_Object Qcompound_text_with_extensions;
112 113
113static Lisp_Object Qforeign_selection; 114static Lisp_Object Qforeign_selection;
114 115
@@ -122,10 +123,8 @@ static Lisp_Object Qforeign_selection;
122 123
123#define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100) 124#define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
124 125
125/* The timestamp of the last input event Emacs received from the X server. */ 126#define LOCAL_SELECTION(selection_symbol,dpyinfo) \
126/* Defined in keyboard.c. */ 127 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
127extern unsigned long last_event_timestamp;
128
129 128
130 129
131/* Define a queue to save up SELECTION_REQUEST_EVENT events for later 130/* Define a queue to save up SELECTION_REQUEST_EVENT events for later
@@ -156,7 +155,7 @@ x_queue_event (struct input_event *event)
156 { 155 {
157 if (!memcmp (&queue_tmp->event, event, sizeof (*event))) 156 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
158 { 157 {
159 TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp); 158 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
160 x_decline_selection_request (event); 159 x_decline_selection_request (event);
161 return; 160 return;
162 } 161 }
@@ -167,7 +166,7 @@ x_queue_event (struct input_event *event)
167 166
168 if (queue_tmp != NULL) 167 if (queue_tmp != NULL)
169 { 168 {
170 TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp); 169 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
171 queue_tmp->event = *event; 170 queue_tmp->event = *event;
172 queue_tmp->next = selection_queue; 171 queue_tmp->next = selection_queue;
173 selection_queue = queue_tmp; 172 selection_queue = queue_tmp;
@@ -200,7 +199,7 @@ x_stop_queuing_selection_requests (void)
200 while (selection_queue != NULL) 199 while (selection_queue != NULL)
201 { 200 {
202 struct selection_event_queue *queue_tmp = selection_queue; 201 struct selection_event_queue *queue_tmp = selection_queue;
203 TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp); 202 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
204 kbd_buffer_unget_event (&queue_tmp->event); 203 kbd_buffer_unget_event (&queue_tmp->event);
205 selection_queue = queue_tmp->next; 204 selection_queue = queue_tmp->next;
206 xfree ((char *)queue_tmp); 205 xfree ((char *)queue_tmp);
@@ -212,7 +211,7 @@ x_stop_queuing_selection_requests (void)
212 roundtrip whenever possible. */ 211 roundtrip whenever possible. */
213 212
214static Atom 213static Atom
215symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym) 214symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
216{ 215{
217 Atom val; 216 Atom val;
218 if (NILP (sym)) return 0; 217 if (NILP (sym)) return 0;
@@ -236,7 +235,7 @@ symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object
236 235
237 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym))); 236 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
238 BLOCK_INPUT; 237 BLOCK_INPUT;
239 val = XInternAtom (display, SSDATA (SYMBOL_NAME (sym)), False); 238 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
240 UNBLOCK_INPUT; 239 UNBLOCK_INPUT;
241 return val; 240 return val;
242} 241}
@@ -270,6 +269,8 @@ x_atom_to_symbol (Display *dpy, Atom atom)
270 } 269 }
271 270
272 dpyinfo = x_display_info_for_display (dpy); 271 dpyinfo = x_display_info_for_display (dpy);
272 if (dpyinfo == NULL)
273 return Qnil;
273 if (atom == dpyinfo->Xatom_CLIPBOARD) 274 if (atom == dpyinfo->Xatom_CLIPBOARD)
274 return QCLIPBOARD; 275 return QCLIPBOARD;
275 if (atom == dpyinfo->Xatom_TIMESTAMP) 276 if (atom == dpyinfo->Xatom_TIMESTAMP)
@@ -307,59 +308,50 @@ x_atom_to_symbol (Display *dpy, Atom atom)
307} 308}
308 309
309/* Do protocol to assert ourself as a selection owner. 310/* Do protocol to assert ourself as a selection owner.
311 FRAME shall be the owner; it must be a valid X frame.
310 Update the Vselection_alist so that we can reply to later requests for 312 Update the Vselection_alist so that we can reply to later requests for
311 our selection. */ 313 our selection. */
312 314
313static void 315static void
314x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value) 316x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
317 Lisp_Object frame)
315{ 318{
316 struct frame *sf = SELECTED_FRAME (); 319 struct frame *f = XFRAME (frame);
317 Window selecting_window; 320 Window selecting_window = FRAME_X_WINDOW (f);
318 Display *display; 321 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
319 Time time = last_event_timestamp; 322 Display *display = dpyinfo->display;
320 Atom selection_atom; 323 Time timestamp = last_event_timestamp;
321 struct x_display_info *dpyinfo; 324 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
322
323 if (! FRAME_X_P (sf))
324 return;
325
326 selecting_window = FRAME_X_WINDOW (sf);
327 display = FRAME_X_DISPLAY (sf);
328 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
329
330 CHECK_SYMBOL (selection_name);
331 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
332 325
333 BLOCK_INPUT; 326 BLOCK_INPUT;
334 x_catch_errors (display); 327 x_catch_errors (display);
335 XSetSelectionOwner (display, selection_atom, selecting_window, time); 328 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
336 x_check_errors (display, "Can't set selection: %s"); 329 x_check_errors (display, "Can't set selection: %s");
337 x_uncatch_errors (); 330 x_uncatch_errors ();
338 UNBLOCK_INPUT; 331 UNBLOCK_INPUT;
339 332
340 /* Now update the local cache */ 333 /* Now update the local cache */
341 { 334 {
342 Lisp_Object selection_time;
343 Lisp_Object selection_data; 335 Lisp_Object selection_data;
344 Lisp_Object prev_value; 336 Lisp_Object prev_value;
345 337
346 selection_time = long_to_cons ((unsigned long) time);
347 selection_data = list4 (selection_name, selection_value, 338 selection_data = list4 (selection_name, selection_value,
348 selection_time, selected_frame); 339 INTEGER_TO_CONS (timestamp), frame);
349 prev_value = assq_no_quit (selection_name, Vselection_alist); 340 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
350 341
351 Vselection_alist = Fcons (selection_data, Vselection_alist); 342 dpyinfo->terminal->Vselection_alist
343 = Fcons (selection_data, dpyinfo->terminal->Vselection_alist);
352 344
353 /* If we already owned the selection, remove the old selection data. 345 /* If we already owned the selection, remove the old selection
354 Perhaps we should destructively modify it instead. 346 data. Don't use Fdelq as that may QUIT. */
355 Don't use Fdelq as that may QUIT. */
356 if (!NILP (prev_value)) 347 if (!NILP (prev_value))
357 { 348 {
358 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */ 349 /* We know it's not the CAR, so it's easy. */
359 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) 350 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
351 for (; CONSP (rest); rest = XCDR (rest))
360 if (EQ (prev_value, Fcar (XCDR (rest)))) 352 if (EQ (prev_value, Fcar (XCDR (rest))))
361 { 353 {
362 XSETCDR (rest, Fcdr (XCDR (rest))); 354 XSETCDR (rest, XCDR (XCDR (rest)));
363 break; 355 break;
364 } 356 }
365 } 357 }
@@ -375,59 +367,23 @@ x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
375 This calls random Lisp code, and may signal or gc. */ 367 This calls random Lisp code, and may signal or gc. */
376 368
377static Lisp_Object 369static Lisp_Object
378x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request) 370x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
371 int local_request, struct x_display_info *dpyinfo)
379{ 372{
380 Lisp_Object local_value; 373 Lisp_Object local_value;
381 Lisp_Object handler_fn, value, type, check; 374 Lisp_Object handler_fn, value, check;
382 int count; 375 int count;
383 376
384 local_value = assq_no_quit (selection_symbol, Vselection_alist); 377 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
385 378
386 if (NILP (local_value)) return Qnil; 379 if (NILP (local_value)) return Qnil;
387 380
388 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */ 381 /* TIMESTAMP is a special case. */
389 if (EQ (target_type, QTIMESTAMP)) 382 if (EQ (target_type, QTIMESTAMP))
390 { 383 {
391 handler_fn = Qnil; 384 handler_fn = Qnil;
392 value = XCAR (XCDR (XCDR (local_value))); 385 value = XCAR (XCDR (XCDR (local_value)));
393 } 386 }
394#if 0
395 else if (EQ (target_type, QDELETE))
396 {
397 handler_fn = Qnil;
398 Fx_disown_selection_internal
399 (selection_symbol,
400 XCAR (XCDR (XCDR (local_value))));
401 value = QNULL;
402 }
403#endif
404
405#if 0 /* #### MULTIPLE doesn't work yet */
406 else if (CONSP (target_type)
407 && XCAR (target_type) == QMULTIPLE)
408 {
409 Lisp_Object pairs;
410 int size;
411 int i;
412 pairs = XCDR (target_type);
413 size = XVECTOR (pairs)->size;
414 /* If the target is MULTIPLE, then target_type looks like
415 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
416 We modify the second element of each pair in the vector and
417 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
418 */
419 for (i = 0; i < size; i++)
420 {
421 Lisp_Object pair;
422 pair = XVECTOR (pairs)->contents [i];
423 XVECTOR (pair)->contents [1]
424 = x_get_local_selection (XVECTOR (pair)->contents [0],
425 XVECTOR (pair)->contents [1],
426 local_request);
427 }
428 return pairs;
429 }
430#endif
431 else 387 else
432 { 388 {
433 /* Don't allow a quit within the converter. 389 /* Don't allow a quit within the converter.
@@ -456,7 +412,6 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, in
456 check = value; 412 check = value;
457 if (CONSP (value) 413 if (CONSP (value)
458 && SYMBOLP (XCAR (value))) 414 && SYMBOLP (XCAR (value)))
459 type = XCAR (value),
460 check = XCDR (value); 415 check = XCDR (value);
461 416
462 if (STRINGP (check) 417 if (STRINGP (check)
@@ -465,7 +420,7 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, in
465 || INTEGERP (check) 420 || INTEGERP (check)
466 || NILP (value)) 421 || NILP (value))
467 return value; 422 return value;
468 /* Check for a value that cons_to_long could handle. */ 423 /* Check for a value that CONS_TO_INTEGER could handle. */
469 else if (CONSP (check) 424 else if (CONSP (check)
470 && INTEGERP (XCAR (check)) 425 && INTEGERP (XCAR (check))
471 && (INTEGERP (XCDR (check)) 426 && (INTEGERP (XCDR (check))
@@ -487,22 +442,23 @@ x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, in
487static void 442static void
488x_decline_selection_request (struct input_event *event) 443x_decline_selection_request (struct input_event *event)
489{ 444{
490 XSelectionEvent reply; 445 XEvent reply_base;
446 XSelectionEvent *reply = &(reply_base.xselection);
491 447
492 reply.type = SelectionNotify; 448 reply->type = SelectionNotify;
493 reply.display = SELECTION_EVENT_DISPLAY (event); 449 reply->display = SELECTION_EVENT_DISPLAY (event);
494 reply.requestor = SELECTION_EVENT_REQUESTOR (event); 450 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
495 reply.selection = SELECTION_EVENT_SELECTION (event); 451 reply->selection = SELECTION_EVENT_SELECTION (event);
496 reply.time = SELECTION_EVENT_TIME (event); 452 reply->time = SELECTION_EVENT_TIME (event);
497 reply.target = SELECTION_EVENT_TARGET (event); 453 reply->target = SELECTION_EVENT_TARGET (event);
498 reply.property = None; 454 reply->property = None;
499 455
500 /* The reason for the error may be that the receiver has 456 /* The reason for the error may be that the receiver has
501 died in the meantime. Handle that case. */ 457 died in the meantime. Handle that case. */
502 BLOCK_INPUT; 458 BLOCK_INPUT;
503 x_catch_errors (reply.display); 459 x_catch_errors (reply->display);
504 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply); 460 XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base);
505 XFlush (reply.display); 461 XFlush (reply->display);
506 x_uncatch_errors (); 462 x_uncatch_errors ();
507 UNBLOCK_INPUT; 463 UNBLOCK_INPUT;
508} 464}
@@ -515,6 +471,30 @@ static struct input_event *x_selection_current_request;
515 471
516static struct x_display_info *selection_request_dpyinfo; 472static struct x_display_info *selection_request_dpyinfo;
517 473
474/* Raw selection data, for sending to a requestor window. */
475
476struct selection_data
477{
478 unsigned char *data;
479 unsigned int size;
480 int format;
481 Atom type;
482 int nofree;
483 Atom property;
484 /* This can be set to non-NULL during x_reply_selection_request, if
485 the selection is waiting for an INCR transfer to complete. Don't
486 free these; that's done by unexpect_property_change. */
487 struct prop_location *wait_object;
488 struct selection_data *next;
489};
490
491/* Linked list of the above (in support of MULTIPLE targets). */
492
493static struct selection_data *converted_selections;
494
495/* "Data" to send a requestor for a failed MULTIPLE subtarget. */
496static Atom conversion_fail_tag;
497
518/* Used as an unwind-protect clause so that, if a selection-converter signals 498/* Used as an unwind-protect clause so that, if a selection-converter signals
519 an error, we tell the requester that we were unable to do what they wanted 499 an error, we tell the requester that we were unable to do what they wanted
520 before we throw to top-level or go into the debugger or whatever. */ 500 before we throw to top-level or go into the debugger or whatever. */
@@ -522,6 +502,17 @@ static struct x_display_info *selection_request_dpyinfo;
522static Lisp_Object 502static Lisp_Object
523x_selection_request_lisp_error (Lisp_Object ignore) 503x_selection_request_lisp_error (Lisp_Object ignore)
524{ 504{
505 struct selection_data *cs, *next;
506
507 for (cs = converted_selections; cs; cs = next)
508 {
509 next = cs->next;
510 if (cs->nofree == 0 && cs->data)
511 xfree (cs->data);
512 xfree (cs);
513 }
514 converted_selections = NULL;
515
525 if (x_selection_current_request != 0 516 if (x_selection_current_request != 0
526 && selection_request_dpyinfo->display) 517 && selection_request_dpyinfo->display)
527 x_decline_selection_request (x_selection_current_request); 518 x_decline_selection_request (x_selection_current_request);
@@ -575,57 +566,37 @@ queue_selection_requests_unwind (Lisp_Object tem)
575 return Qnil; 566 return Qnil;
576} 567}
577 568
578/* Return some frame whose display info is DPYINFO.
579 Return nil if there is none. */
580
581static Lisp_Object
582some_frame_on_display (struct x_display_info *dpyinfo)
583{
584 Lisp_Object list, frame;
585
586 FOR_EACH_FRAME (list, frame)
587 {
588 if (FRAME_X_P (XFRAME (frame))
589 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
590 return frame;
591 }
592
593 return Qnil;
594}
595 569
596/* Send the reply to a selection request event EVENT. 570/* Send the reply to a selection request event EVENT. */
597 TYPE is the type of selection data requested.
598 DATA and SIZE describe the data to send, already converted.
599 FORMAT is the unit-size (in bits) of the data to be transmitted. */
600 571
601#ifdef TRACE_SELECTION 572#ifdef TRACE_SELECTION
602static int x_reply_selection_request_cnt; 573static int x_reply_selection_request_cnt;
603#endif /* TRACE_SELECTION */ 574#endif /* TRACE_SELECTION */
604 575
605static void 576static void
606x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type) 577x_reply_selection_request (struct input_event *event, struct x_display_info *dpyinfo)
607{ 578{
608 XSelectionEvent reply; 579 XEvent reply_base;
580 XSelectionEvent *reply = &(reply_base.xselection);
609 Display *display = SELECTION_EVENT_DISPLAY (event); 581 Display *display = SELECTION_EVENT_DISPLAY (event);
610 Window window = SELECTION_EVENT_REQUESTOR (event); 582 Window window = SELECTION_EVENT_REQUESTOR (event);
611 int bytes_remaining; 583 int bytes_remaining;
612 int format_bytes = format/8;
613 int max_bytes = SELECTION_QUANTUM (display); 584 int max_bytes = SELECTION_QUANTUM (display);
614 struct x_display_info *dpyinfo = x_display_info_for_display (display);
615 int count = SPECPDL_INDEX (); 585 int count = SPECPDL_INDEX ();
586 struct selection_data *cs;
616 587
617 if (max_bytes > MAX_SELECTION_QUANTUM) 588 if (max_bytes > MAX_SELECTION_QUANTUM)
618 max_bytes = MAX_SELECTION_QUANTUM; 589 max_bytes = MAX_SELECTION_QUANTUM;
619 590
620 reply.type = SelectionNotify; 591 reply->type = SelectionNotify;
621 reply.display = display; 592 reply->display = display;
622 reply.requestor = window; 593 reply->requestor = window;
623 reply.selection = SELECTION_EVENT_SELECTION (event); 594 reply->selection = SELECTION_EVENT_SELECTION (event);
624 reply.time = SELECTION_EVENT_TIME (event); 595 reply->time = SELECTION_EVENT_TIME (event);
625 reply.target = SELECTION_EVENT_TARGET (event); 596 reply->target = SELECTION_EVENT_TARGET (event);
626 reply.property = SELECTION_EVENT_PROPERTY (event); 597 reply->property = SELECTION_EVENT_PROPERTY (event);
627 if (reply.property == None) 598 if (reply->property == None)
628 reply.property = reply.target; 599 reply->property = reply->target;
629 600
630 BLOCK_INPUT; 601 BLOCK_INPUT;
631 /* The protected block contains wait_for_property_change, which can 602 /* The protected block contains wait_for_property_change, which can
@@ -634,142 +605,133 @@ x_reply_selection_request (struct input_event *event, int format, unsigned char
634 record_unwind_protect (x_catch_errors_unwind, Qnil); 605 record_unwind_protect (x_catch_errors_unwind, Qnil);
635 x_catch_errors (display); 606 x_catch_errors (display);
636 607
637#ifdef TRACE_SELECTION 608 /* Loop over converted selections, storing them in the requested
638 { 609 properties. If data is large, only store the first N bytes
639 char *sel = XGetAtomName (display, reply.selection); 610 (section 2.7.2 of ICCCM). Note that we store the data for a
640 char *tgt = XGetAtomName (display, reply.target); 611 MULTIPLE request in the opposite order; the ICCM says only that
641 TRACE3 ("%s, target %s (%d)", sel, tgt, ++x_reply_selection_request_cnt); 612 the conversion itself must be done in the same order. */
642 if (sel) XFree (sel); 613 for (cs = converted_selections; cs; cs = cs->next)
643 if (tgt) XFree (tgt);
644 }
645#endif /* TRACE_SELECTION */
646
647 /* Store the data on the requested property.
648 If the selection is large, only store the first N bytes of it.
649 */
650 bytes_remaining = size * format_bytes;
651 if (bytes_remaining <= max_bytes)
652 {
653 /* Send all the data at once, with minimal handshaking. */
654 TRACE1 ("Sending all %d bytes", bytes_remaining);
655 XChangeProperty (display, window, reply.property, type, format,
656 PropModeReplace, data, size);
657 /* At this point, the selection was successfully stored; ack it. */
658 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
659 }
660 else
661 { 614 {
662 /* Send an INCR selection. */ 615 if (cs->property == None)
663 struct prop_location *wait_object; 616 continue;
664 int had_errors;
665 Lisp_Object frame;
666
667 frame = some_frame_on_display (dpyinfo);
668 617
669 /* If the display no longer has frames, we can't expect 618 bytes_remaining = cs->size * (cs->format / 8);
670 to get many more selection requests from it, so don't 619 if (bytes_remaining <= max_bytes)
671 bother trying to queue them. */
672 if (!NILP (frame))
673 { 620 {
674 x_start_queuing_selection_requests (); 621 /* Send all the data at once, with minimal handshaking. */
675 622 TRACE1 ("Sending all %d bytes", bytes_remaining);
676 record_unwind_protect (queue_selection_requests_unwind, 623 XChangeProperty (display, window, cs->property,
677 Qnil); 624 cs->type, cs->format, PropModeReplace,
625 cs->data, cs->size);
678 } 626 }
627 else
628 {
629 /* Send an INCR tag to initiate incremental transfer. */
630 long value[1];
679 631
680 if (x_window_to_frame (dpyinfo, window)) /* #### debug */ 632 TRACE2 ("Start sending %d bytes incrementally (%s)",
681 error ("Attempt to transfer an INCR to ourself!"); 633 bytes_remaining, XGetAtomName (display, cs->property));
682 634 cs->wait_object
683 TRACE2 ("Start sending %d bytes incrementally (%s)", 635 = expect_property_change (display, window, cs->property,
684 bytes_remaining, XGetAtomName (display, reply.property)); 636 PropertyDelete);
685 wait_object = expect_property_change (display, window, reply.property,
686 PropertyDelete);
687
688 TRACE1 ("Set %s to number of bytes to send",
689 XGetAtomName (display, reply.property));
690 {
691 /* XChangeProperty expects an array of long even if long is more than
692 32 bits. */
693 long value[1];
694
695 value[0] = bytes_remaining;
696 XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
697 32, PropModeReplace,
698 (unsigned char *) value, 1);
699 }
700
701 XSelectInput (display, window, PropertyChangeMask);
702
703 /* Tell 'em the INCR data is there... */
704 TRACE0 ("Send SelectionNotify event");
705 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
706 XFlush (display);
707
708 had_errors = x_had_errors_p (display);
709 UNBLOCK_INPUT;
710 637
711 /* First, wait for the requester to ack by deleting the property. 638 /* XChangeProperty expects an array of long even if long is
712 This can run random lisp code (process handlers) or signal. */ 639 more than 32 bits. */
713 if (! had_errors) 640 value[0] = bytes_remaining;
714 { 641 XChangeProperty (display, window, cs->property,
715 TRACE1 ("Waiting for ACK (deletion of %s)", 642 dpyinfo->Xatom_INCR, 32, PropModeReplace,
716 XGetAtomName (display, reply.property)); 643 (unsigned char *) value, 1);
717 wait_for_property_change (wait_object); 644 XSelectInput (display, window, PropertyChangeMask);
718 } 645 }
719 else 646 }
720 unexpect_property_change (wait_object);
721 647
722 TRACE0 ("Got ACK"); 648 /* Now issue the SelectionNotify event. */
723 while (bytes_remaining) 649 XSendEvent (display, window, False, 0L, &reply_base);
724 { 650 XFlush (display);
725 int i = ((bytes_remaining < max_bytes)
726 ? bytes_remaining
727 : max_bytes) / format_bytes;
728 651
729 BLOCK_INPUT; 652#ifdef TRACE_SELECTION
653 {
654 char *sel = XGetAtomName (display, reply->selection);
655 char *tgt = XGetAtomName (display, reply->target);
656 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
657 sel, tgt, ++x_reply_selection_request_cnt);
658 if (sel) XFree (sel);
659 if (tgt) XFree (tgt);
660 }
661#endif /* TRACE_SELECTION */
730 662
731 wait_object 663 /* Finish sending the rest of each of the INCR values. This should
732 = expect_property_change (display, window, reply.property, 664 be improved; there's a chance of deadlock if more than one
733 PropertyDelete); 665 subtarget in a MULTIPLE selection requires an INCR transfer, and
666 the requestor and Emacs loop waiting on different transfers. */
667 for (cs = converted_selections; cs; cs = cs->next)
668 if (cs->wait_object)
669 {
670 int format_bytes = cs->format / 8;
671 int had_errors = x_had_errors_p (display);
672 UNBLOCK_INPUT;
734 673
735 TRACE1 ("Sending increment of %d elements", i); 674 bytes_remaining = cs->size * format_bytes;
736 TRACE1 ("Set %s to increment data",
737 XGetAtomName (display, reply.property));
738 675
739 /* Append the next chunk of data to the property. */ 676 /* Wait for the requester to ack by deleting the property.
740 XChangeProperty (display, window, reply.property, type, format, 677 This can run Lisp code (process handlers) or signal. */
741 PropModeAppend, data, i); 678 if (! had_errors)
742 bytes_remaining -= i * format_bytes; 679 {
743 if (format == 32) 680 TRACE1 ("Waiting for ACK (deletion of %s)",
744 data += i * sizeof (long); 681 XGetAtomName (display, cs->property));
745 else 682 wait_for_property_change (cs->wait_object);
746 data += i * format_bytes; 683 }
747 XFlush (display); 684 else
748 had_errors = x_had_errors_p (display); 685 unexpect_property_change (cs->wait_object);
749 UNBLOCK_INPUT;
750 686
751 if (had_errors) 687 while (bytes_remaining)
752 break; 688 {
689 int i = ((bytes_remaining < max_bytes)
690 ? bytes_remaining
691 : max_bytes) / format_bytes;
692 BLOCK_INPUT;
693
694 cs->wait_object
695 = expect_property_change (display, window, cs->property,
696 PropertyDelete);
697
698 TRACE1 ("Sending increment of %d elements", i);
699 TRACE1 ("Set %s to increment data",
700 XGetAtomName (display, cs->property));
701
702 /* Append the next chunk of data to the property. */
703 XChangeProperty (display, window, cs->property,
704 cs->type, cs->format, PropModeAppend,
705 cs->data, i);
706 bytes_remaining -= i * format_bytes;
707 cs->data += i * ((cs->format == 32) ? sizeof (long)
708 : format_bytes);
709 XFlush (display);
710 had_errors = x_had_errors_p (display);
711 UNBLOCK_INPUT;
753 712
754 /* Now wait for the requester to ack this chunk by deleting the 713 if (had_errors) break;
755 property. This can run random lisp code or signal. */
756 TRACE1 ("Waiting for increment ACK (deletion of %s)",
757 XGetAtomName (display, reply.property));
758 wait_for_property_change (wait_object);
759 }
760 714
761 /* Now write a zero-length chunk to the property to tell the 715 /* Wait for the requester to ack this chunk by deleting
762 requester that we're done. */ 716 the property. This can run Lisp code or signal. */
763 BLOCK_INPUT; 717 TRACE1 ("Waiting for increment ACK (deletion of %s)",
764 if (! waiting_for_other_props_on_window (display, window)) 718 XGetAtomName (display, cs->property));
765 XSelectInput (display, window, 0L); 719 wait_for_property_change (cs->wait_object);
766 720 }
767 TRACE1 ("Set %s to a 0-length chunk to indicate EOF", 721
768 XGetAtomName (display, reply.property)); 722 /* Now write a zero-length chunk to the property to tell the
769 XChangeProperty (display, window, reply.property, type, format, 723 requester that we're done. */
770 PropModeReplace, data, 0); 724 BLOCK_INPUT;
771 TRACE0 ("Done sending incrementally"); 725 if (! waiting_for_other_props_on_window (display, window))
772 } 726 XSelectInput (display, window, 0L);
727
728 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
729 XGetAtomName (display, cs->property));
730 XChangeProperty (display, window, cs->property,
731 cs->type, cs->format, PropModeReplace,
732 cs->data, 0);
733 TRACE0 ("Done sending incrementally");
734 }
773 735
774 /* rms, 2003-01-03: I think I have fixed this bug. */ 736 /* rms, 2003-01-03: I think I have fixed this bug. */
775 /* The window we're communicating with may have been deleted 737 /* The window we're communicating with may have been deleted
@@ -798,117 +760,169 @@ x_reply_selection_request (struct input_event *event, int format, unsigned char
798static void 760static void
799x_handle_selection_request (struct input_event *event) 761x_handle_selection_request (struct input_event *event)
800{ 762{
801 struct gcpro gcpro1, gcpro2, gcpro3; 763 struct gcpro gcpro1, gcpro2;
802 Lisp_Object local_selection_data;
803 Lisp_Object selection_symbol;
804 Lisp_Object target_symbol;
805 Lisp_Object converted_selection;
806 Time local_selection_time; 764 Time local_selection_time;
807 Lisp_Object successful_p;
808 int count;
809 struct x_display_info *dpyinfo
810 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
811
812 TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
813 (unsigned long) SELECTION_EVENT_REQUESTOR (event),
814 (unsigned long) SELECTION_EVENT_TIME (event));
815
816 local_selection_data = Qnil;
817 target_symbol = Qnil;
818 converted_selection = Qnil;
819 successful_p = Qnil;
820 765
821 GCPRO3 (local_selection_data, converted_selection, target_symbol); 766 Display *display = SELECTION_EVENT_DISPLAY (event);
767 struct x_display_info *dpyinfo = x_display_info_for_display (display);
768 Atom selection = SELECTION_EVENT_SELECTION (event);
769 Lisp_Object selection_symbol = x_atom_to_symbol (display, selection);
770 Atom target = SELECTION_EVENT_TARGET (event);
771 Lisp_Object target_symbol = x_atom_to_symbol (display, target);
772 Atom property = SELECTION_EVENT_PROPERTY (event);
773 Lisp_Object local_selection_data;
774 int success = 0;
775 int count = SPECPDL_INDEX ();
776 GCPRO2 (local_selection_data, target_symbol);
822 777
823 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event), 778 if (!dpyinfo) goto DONE;
824 SELECTION_EVENT_SELECTION (event));
825 779
826 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist); 780 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
827 781
828 if (NILP (local_selection_data)) 782 /* Decline if we don't own any selections. */
829 { 783 if (NILP (local_selection_data)) goto DONE;
830 /* Someone asked for the selection, but we don't have it any more.
831 */
832 x_decline_selection_request (event);
833 goto DONE;
834 }
835
836 local_selection_time = (Time)
837 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
838 784
785 /* Decline requests issued prior to our acquiring the selection. */
786 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
787 Time, local_selection_time);
839 if (SELECTION_EVENT_TIME (event) != CurrentTime 788 if (SELECTION_EVENT_TIME (event) != CurrentTime
840 && local_selection_time > SELECTION_EVENT_TIME (event)) 789 && local_selection_time > SELECTION_EVENT_TIME (event))
841 { 790 goto DONE;
842 /* Someone asked for the selection, and we have one, but not the one
843 they're looking for.
844 */
845 x_decline_selection_request (event);
846 goto DONE;
847 }
848 791
849 x_selection_current_request = event; 792 x_selection_current_request = event;
850 count = SPECPDL_INDEX ();
851 selection_request_dpyinfo = dpyinfo; 793 selection_request_dpyinfo = dpyinfo;
852 record_unwind_protect (x_selection_request_lisp_error, Qnil); 794 record_unwind_protect (x_selection_request_lisp_error, Qnil);
853 795
854 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event), 796 /* We might be able to handle nested x_handle_selection_requests,
855 SELECTION_EVENT_TARGET (event)); 797 but this is difficult to test, and seems unimportant. */
856 798 x_start_queuing_selection_requests ();
857#if 0 /* #### MULTIPLE doesn't work yet */ 799 record_unwind_protect (queue_selection_requests_unwind, Qnil);
858 if (EQ (target_symbol, QMULTIPLE))
859 target_symbol = fetch_multiple_target (event);
860#endif
861
862 /* Convert lisp objects back into binary data */
863 800
864 converted_selection 801 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
865 = x_get_local_selection (selection_symbol, target_symbol, 0); 802 SDATA (SYMBOL_NAME (selection_symbol)),
803 SDATA (SYMBOL_NAME (target_symbol)));
866 804
867 if (! NILP (converted_selection)) 805 if (EQ (target_symbol, QMULTIPLE))
868 { 806 {
869 unsigned char *data; 807 /* For MULTIPLE targets, the event property names a list of atom
870 unsigned int size; 808 pairs; the first atom names a target and the second names a
871 int format; 809 non-None property. */
872 Atom type; 810 Window requestor = SELECTION_EVENT_REQUESTOR (event);
873 int nofree; 811 Lisp_Object multprop;
874 812 int j, nselections;
875 if (CONSP (converted_selection) && NILP (XCDR (converted_selection))) 813
876 { 814 if (property == None) goto DONE;
877 x_decline_selection_request (event); 815 multprop
878 goto DONE2; 816 = x_get_window_property_as_lisp_data (display, requestor, property,
879 } 817 QMULTIPLE, selection);
818
819 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
820 goto DONE;
821
822 nselections = ASIZE (multprop) / 2;
823 /* Perform conversions. This can signal. */
824 for (j = 0; j < nselections; j++)
825 {
826 Lisp_Object subtarget = AREF (multprop, 2*j);
827 Atom subproperty = symbol_to_x_atom (dpyinfo,
828 AREF (multprop, 2*j+1));
880 829
881 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event), 830 if (subproperty != None)
882 converted_selection, 831 x_convert_selection (event, selection_symbol, subtarget,
883 &data, &type, &size, &format, &nofree); 832 subproperty, 1, dpyinfo);
833 }
834 success = 1;
835 }
836 else
837 {
838 if (property == None)
839 property = SELECTION_EVENT_TARGET (event);
840 success = x_convert_selection (event, selection_symbol,
841 target_symbol, property,
842 0, dpyinfo);
843 }
884 844
885 x_reply_selection_request (event, format, data, size, type); 845 DONE:
886 successful_p = Qt;
887 846
888 /* Indicate we have successfully processed this event. */ 847 if (success)
889 x_selection_current_request = 0; 848 x_reply_selection_request (event, dpyinfo);
849 else
850 x_decline_selection_request (event);
851 x_selection_current_request = 0;
890 852
891 /* Use xfree, not XFree, because lisp_data_to_selection_data 853 /* Run the `x-sent-selection-functions' abnormal hook. */
892 calls xmalloc itself. */ 854 if (!NILP (Vx_sent_selection_functions)
893 if (!nofree) 855 && !EQ (Vx_sent_selection_functions, Qunbound))
894 xfree (data); 856 {
857 Lisp_Object args[4];
858 args[0] = Vx_sent_selection_functions;
859 args[1] = selection_symbol;
860 args[2] = target_symbol;
861 args[3] = success ? Qt : Qnil;
862 Frun_hook_with_args (4, args);
895 } 863 }
896 864
897 DONE2:
898 unbind_to (count, Qnil); 865 unbind_to (count, Qnil);
866 UNGCPRO;
867}
899 868
900 DONE: 869/* Perform the requested selection conversion, and write the data to
870 the converted_selections linked list, where it can be accessed by
871 x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out
872 the data even if conversion fails, using conversion_fail_tag.
901 873
902 /* Let random lisp code notice that the selection has been asked for. */ 874 Return 0 if the selection failed to convert, 1 otherwise. */
903 { 875
904 Lisp_Object rest; 876static int
905 rest = Vx_sent_selection_functions; 877x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
906 if (!EQ (rest, Qunbound)) 878 Lisp_Object target_symbol, Atom property,
907 for (; CONSP (rest); rest = Fcdr (rest)) 879 int for_multiple, struct x_display_info *dpyinfo)
908 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p); 880{
909 } 881 struct gcpro gcpro1;
882 Lisp_Object lisp_selection;
883 struct selection_data *cs;
884 GCPRO1 (lisp_selection);
885
886 lisp_selection
887 = x_get_local_selection (selection_symbol, target_symbol,
888 0, dpyinfo);
889
890 /* A nil return value means we can't perform the conversion. */
891 if (NILP (lisp_selection)
892 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
893 {
894 if (for_multiple)
895 {
896 cs = xmalloc (sizeof (struct selection_data));
897 cs->data = (unsigned char *) &conversion_fail_tag;
898 cs->size = 1;
899 cs->format = 32;
900 cs->type = XA_ATOM;
901 cs->nofree = 1;
902 cs->property = property;
903 cs->wait_object = NULL;
904 cs->next = converted_selections;
905 converted_selections = cs;
906 }
907
908 UNGCPRO;
909 return 0;
910 }
910 911
912 /* Otherwise, record the converted selection to binary. */
913 cs = xmalloc (sizeof (struct selection_data));
914 cs->nofree = 1;
915 cs->property = property;
916 cs->wait_object = NULL;
917 cs->next = converted_selections;
918 converted_selections = cs;
919 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
920 lisp_selection,
921 &(cs->data), &(cs->type),
922 &(cs->size), &(cs->format),
923 &(cs->nofree));
911 UNGCPRO; 924 UNGCPRO;
925 return 1;
912} 926}
913 927
914/* Handle a SelectionClear event EVENT, which indicates that some 928/* Handle a SelectionClear event EVENT, which indicates that some
@@ -925,92 +939,65 @@ x_handle_selection_clear (struct input_event *event)
925 Lisp_Object selection_symbol, local_selection_data; 939 Lisp_Object selection_symbol, local_selection_data;
926 Time local_selection_time; 940 Time local_selection_time;
927 struct x_display_info *dpyinfo = x_display_info_for_display (display); 941 struct x_display_info *dpyinfo = x_display_info_for_display (display);
928 struct x_display_info *t_dpyinfo; 942 Lisp_Object Vselection_alist;
929 943
930 TRACE0 ("x_handle_selection_clear"); 944 TRACE0 ("x_handle_selection_clear");
931 945
932 /* If the new selection owner is also Emacs, 946 if (!dpyinfo) return;
933 don't clear the new selection. */
934 BLOCK_INPUT;
935 /* Check each display on the same terminal,
936 to see if this Emacs job now owns the selection
937 through that display. */
938 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
939 if (t_dpyinfo->terminal->kboard == dpyinfo->terminal->kboard)
940 {
941 Window owner_window
942 = XGetSelectionOwner (t_dpyinfo->display, selection);
943 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
944 {
945 UNBLOCK_INPUT;
946 return;
947 }
948 }
949 UNBLOCK_INPUT;
950 947
951 selection_symbol = x_atom_to_symbol (display, selection); 948 selection_symbol = x_atom_to_symbol (display, selection);
952 949 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
953 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
954 950
955 /* Well, we already believe that we don't own it, so that's just fine. */ 951 /* Well, we already believe that we don't own it, so that's just fine. */
956 if (NILP (local_selection_data)) return; 952 if (NILP (local_selection_data)) return;
957 953
958 local_selection_time = (Time) 954 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
959 cons_to_long (XCAR (XCDR (XCDR (local_selection_data)))); 955 Time, local_selection_time);
960
961 /* This SelectionClear is for a selection that we no longer own, so we can
962 disregard it. (That is, we have reasserted the selection since this
963 request was generated.) */
964 956
957 /* We have reasserted the selection since this SelectionClear was
958 generated, so we can disregard it. */
965 if (changed_owner_time != CurrentTime 959 if (changed_owner_time != CurrentTime
966 && local_selection_time > changed_owner_time) 960 && local_selection_time > changed_owner_time)
967 return; 961 return;
968 962
969 /* Otherwise, we're really honest and truly being told to drop it. 963 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
970 Don't use Fdelq as that may QUIT;. */ 964 Vselection_alist = dpyinfo->terminal->Vselection_alist;
971 965 if (EQ (local_selection_data, CAR (Vselection_alist)))
972 if (EQ (local_selection_data, Fcar (Vselection_alist))) 966 Vselection_alist = XCDR (Vselection_alist);
973 Vselection_alist = Fcdr (Vselection_alist);
974 else 967 else
975 { 968 {
976 Lisp_Object rest; 969 Lisp_Object rest;
977 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) 970 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
978 if (EQ (local_selection_data, Fcar (XCDR (rest)))) 971 if (EQ (local_selection_data, CAR (XCDR (rest))))
979 { 972 {
980 XSETCDR (rest, Fcdr (XCDR (rest))); 973 XSETCDR (rest, XCDR (XCDR (rest)));
981 break; 974 break;
982 } 975 }
983 } 976 }
977 dpyinfo->terminal->Vselection_alist = Vselection_alist;
984 978
985 /* Let random lisp code notice that the selection has been stolen. */ 979 /* Run the `x-lost-selection-functions' abnormal hook. */
986
987 { 980 {
988 Lisp_Object rest; 981 Lisp_Object args[2];
989 rest = Vx_lost_selection_functions; 982 args[0] = Vx_lost_selection_functions;
990 if (!EQ (rest, Qunbound)) 983 args[1] = selection_symbol;
991 { 984 Frun_hook_with_args (2, args);
992 for (; CONSP (rest); rest = Fcdr (rest))
993 call1 (Fcar (rest), selection_symbol);
994 prepare_menu_bars ();
995 redisplay_preserve_echo_area (20);
996 }
997 } 985 }
986
987 prepare_menu_bars ();
988 redisplay_preserve_echo_area (20);
998} 989}
999 990
1000void 991void
1001x_handle_selection_event (struct input_event *event) 992x_handle_selection_event (struct input_event *event)
1002{ 993{
1003 TRACE0 ("x_handle_selection_event"); 994 TRACE0 ("x_handle_selection_event");
1004 995 if (event->kind != SELECTION_REQUEST_EVENT)
1005 if (event->kind == SELECTION_REQUEST_EVENT)
1006 {
1007 if (x_queue_selection_requests)
1008 x_queue_event (event);
1009 else
1010 x_handle_selection_request (event);
1011 }
1012 else
1013 x_handle_selection_clear (event); 996 x_handle_selection_clear (event);
997 else if (x_queue_selection_requests)
998 x_queue_event (event);
999 else
1000 x_handle_selection_request (event);
1014} 1001}
1015 1002
1016 1003
@@ -1022,55 +1009,34 @@ x_clear_frame_selections (FRAME_PTR f)
1022{ 1009{
1023 Lisp_Object frame; 1010 Lisp_Object frame;
1024 Lisp_Object rest; 1011 Lisp_Object rest;
1012 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1013 struct terminal *t = dpyinfo->terminal;
1025 1014
1026 XSETFRAME (frame, f); 1015 XSETFRAME (frame, f);
1027 1016
1028 /* Otherwise, we're really honest and truly being told to drop it.
1029 Don't use Fdelq as that may QUIT;. */
1030
1031 /* Delete elements from the beginning of Vselection_alist. */ 1017 /* Delete elements from the beginning of Vselection_alist. */
1032 while (!NILP (Vselection_alist) 1018 while (CONSP (t->Vselection_alist)
1033 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist))))))) 1019 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
1034 { 1020 {
1035 /* Let random Lisp code notice that the selection has been stolen. */ 1021 /* Run the `x-lost-selection-functions' abnormal hook. */
1036 Lisp_Object hooks, selection_symbol; 1022 Lisp_Object args[2];
1037 1023 args[0] = Vx_lost_selection_functions;
1038 hooks = Vx_lost_selection_functions; 1024 args[1] = Fcar (Fcar (t->Vselection_alist));
1039 selection_symbol = Fcar (Fcar (Vselection_alist)); 1025 Frun_hook_with_args (2, args);
1040
1041 if (!EQ (hooks, Qunbound))
1042 {
1043 for (; CONSP (hooks); hooks = Fcdr (hooks))
1044 call1 (Fcar (hooks), selection_symbol);
1045#if 0 /* This can crash when deleting a frame
1046 from x_connection_closed. Anyway, it seems unnecessary;
1047 something else should cause a redisplay. */
1048 redisplay_preserve_echo_area (21);
1049#endif
1050 }
1051 1026
1052 Vselection_alist = Fcdr (Vselection_alist); 1027 t->Vselection_alist = XCDR (t->Vselection_alist);
1053 } 1028 }
1054 1029
1055 /* Delete elements after the beginning of Vselection_alist. */ 1030 /* Delete elements after the beginning of Vselection_alist. */
1056 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) 1031 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
1057 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest)))))))) 1032 if (CONSP (XCDR (rest))
1033 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
1058 { 1034 {
1059 /* Let random Lisp code notice that the selection has been stolen. */ 1035 Lisp_Object args[2];
1060 Lisp_Object hooks, selection_symbol; 1036 args[0] = Vx_lost_selection_functions;
1061 1037 args[1] = XCAR (XCAR (XCDR (rest)));
1062 hooks = Vx_lost_selection_functions; 1038 Frun_hook_with_args (2, args);
1063 selection_symbol = Fcar (Fcar (XCDR (rest))); 1039 XSETCDR (rest, XCDR (XCDR (rest)));
1064
1065 if (!EQ (hooks, Qunbound))
1066 {
1067 for (; CONSP (hooks); hooks = Fcdr (hooks))
1068 call1 (Fcar (hooks), selection_symbol);
1069#if 0 /* See above */
1070 redisplay_preserve_echo_area (22);
1071#endif
1072 }
1073 XSETCDR (rest, Fcdr (XCDR (rest)));
1074 break; 1040 break;
1075 } 1041 }
1076} 1042}
@@ -1190,9 +1156,9 @@ wait_for_property_change (struct prop_location *location)
1190void 1156void
1191x_handle_property_notify (XPropertyEvent *event) 1157x_handle_property_notify (XPropertyEvent *event)
1192{ 1158{
1193 struct prop_location *prev = 0, *rest = property_change_wait_list; 1159 struct prop_location *rest;
1194 1160
1195 while (rest) 1161 for (rest = property_change_wait_list; rest; rest = rest->next)
1196 { 1162 {
1197 if (!rest->arrived 1163 if (!rest->arrived
1198 && rest->property == event->atom 1164 && rest->property == event->atom
@@ -1213,145 +1179,67 @@ x_handle_property_notify (XPropertyEvent *event)
1213 1179
1214 return; 1180 return;
1215 } 1181 }
1216
1217 prev = rest;
1218 rest = rest->next;
1219 } 1182 }
1220} 1183}
1221 1184
1222 1185
1223 1186
1224#if 0 /* #### MULTIPLE doesn't work yet */
1225
1226static Lisp_Object
1227fetch_multiple_target (event)
1228 XSelectionRequestEvent *event;
1229{
1230 Display *display = event->display;
1231 Window window = event->requestor;
1232 Atom target = event->target;
1233 Atom selection_atom = event->selection;
1234 int result;
1235
1236 return
1237 Fcons (QMULTIPLE,
1238 x_get_window_property_as_lisp_data (display, window, target,
1239 QMULTIPLE, selection_atom));
1240}
1241
1242static Lisp_Object
1243copy_multiple_data (obj)
1244 Lisp_Object obj;
1245{
1246 Lisp_Object vec;
1247 int i;
1248 int size;
1249 if (CONSP (obj))
1250 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1251
1252 CHECK_VECTOR (obj);
1253 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1254 for (i = 0; i < size; i++)
1255 {
1256 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1257 CHECK_VECTOR (vec2);
1258 if (XVECTOR (vec2)->size != 2)
1259 /* ??? Confusing error message */
1260 signal_error ("Vectors must be of length 2", vec2);
1261 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1262 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1263 = XVECTOR (vec2)->contents [0];
1264 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1265 = XVECTOR (vec2)->contents [1];
1266 }
1267 return vec;
1268}
1269
1270#endif
1271
1272
1273/* Variables for communication with x_handle_selection_notify. */ 1187/* Variables for communication with x_handle_selection_notify. */
1274static Atom reading_which_selection; 1188static Atom reading_which_selection;
1275static Lisp_Object reading_selection_reply; 1189static Lisp_Object reading_selection_reply;
1276static Window reading_selection_window; 1190static Window reading_selection_window;
1277 1191
1278/* Do protocol to read selection-data from the server. 1192/* Do protocol to read selection-data from the server.
1279 Converts this to Lisp data and returns it. */ 1193 Converts this to Lisp data and returns it.
1194 FRAME is the frame whose X window shall request the selection. */
1280 1195
1281static Lisp_Object 1196static Lisp_Object
1282x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp) 1197x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1198 Lisp_Object time_stamp, Lisp_Object frame)
1283{ 1199{
1284 struct frame *sf = SELECTED_FRAME (); 1200 struct frame *f = XFRAME (frame);
1285 Window requestor_window; 1201 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1286 Display *display; 1202 Display *display = dpyinfo->display;
1287 struct x_display_info *dpyinfo; 1203 Window requestor_window = FRAME_X_WINDOW (f);
1288 Time requestor_time = last_event_timestamp; 1204 Time requestor_time = last_event_timestamp;
1289 Atom target_property; 1205 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1290 Atom selection_atom; 1206 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1291 Atom type_atom; 1207 Atom type_atom = (CONSP (target_type)
1208 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1209 : symbol_to_x_atom (dpyinfo, target_type));
1292 int secs, usecs; 1210 int secs, usecs;
1293 int count = SPECPDL_INDEX ();
1294 Lisp_Object frame;
1295 1211
1296 if (! FRAME_X_P (sf)) 1212 if (!FRAME_LIVE_P (f))
1297 return Qnil; 1213 return Qnil;
1298 1214
1299 requestor_window = FRAME_X_WINDOW (sf);
1300 display = FRAME_X_DISPLAY (sf);
1301 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1302 target_property = dpyinfo->Xatom_EMACS_TMP;
1303 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1304
1305 if (CONSP (target_type))
1306 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1307 else
1308 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1309
1310 if (! NILP (time_stamp)) 1215 if (! NILP (time_stamp))
1311 { 1216 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1312 if (CONSP (time_stamp))
1313 requestor_time = (Time) cons_to_long (time_stamp);
1314 else if (INTEGERP (time_stamp))
1315 requestor_time = (Time) XUINT (time_stamp);
1316 else if (FLOATP (time_stamp))
1317 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1318 else
1319 error ("TIME_STAMP must be cons or number");
1320 }
1321 1217
1322 BLOCK_INPUT; 1218 BLOCK_INPUT;
1323
1324 /* The protected block contains wait_reading_process_output, which
1325 can run random lisp code (process handlers) or signal.
1326 Therefore, we put the x_uncatch_errors call in an unwind. */
1327 record_unwind_protect (x_catch_errors_unwind, Qnil);
1328 x_catch_errors (display);
1329
1330 TRACE2 ("Get selection %s, type %s", 1219 TRACE2 ("Get selection %s, type %s",
1331 XGetAtomName (display, type_atom), 1220 XGetAtomName (display, type_atom),
1332 XGetAtomName (display, target_property)); 1221 XGetAtomName (display, target_property));
1333 1222
1223 x_catch_errors (display);
1334 XConvertSelection (display, selection_atom, type_atom, target_property, 1224 XConvertSelection (display, selection_atom, type_atom, target_property,
1335 requestor_window, requestor_time); 1225 requestor_window, requestor_time);
1336 XFlush (display); 1226 x_check_errors (display, "Can't convert selection: %s");
1227 x_uncatch_errors ();
1337 1228
1338 /* Prepare to block until the reply has been read. */ 1229 /* Prepare to block until the reply has been read. */
1339 reading_selection_window = requestor_window; 1230 reading_selection_window = requestor_window;
1340 reading_which_selection = selection_atom; 1231 reading_which_selection = selection_atom;
1341 XSETCAR (reading_selection_reply, Qnil); 1232 XSETCAR (reading_selection_reply, Qnil);
1342 1233
1343 frame = some_frame_on_display (dpyinfo); 1234 /* It should not be necessary to stop handling selection requests
1344 1235 during this time. In fact, the SAVE_TARGETS mechanism requires
1345 /* If the display no longer has frames, we can't expect 1236 us to handle a clipboard manager's requests before it returns
1346 to get many more selection requests from it, so don't 1237 SelectionNotify. */
1347 bother trying to queue them. */ 1238#if 0
1348 if (!NILP (frame)) 1239 x_start_queuing_selection_requests ();
1349 { 1240 record_unwind_protect (queue_selection_requests_unwind, Qnil);
1350 x_start_queuing_selection_requests (); 1241#endif
1351 1242
1352 record_unwind_protect (queue_selection_requests_unwind,
1353 Qnil);
1354 }
1355 UNBLOCK_INPUT; 1243 UNBLOCK_INPUT;
1356 1244
1357 /* This allows quits. Also, don't wait forever. */ 1245 /* This allows quits. Also, don't wait forever. */
@@ -1362,13 +1250,6 @@ x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1362 reading_selection_reply, NULL, 0); 1250 reading_selection_reply, NULL, 0);
1363 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply))); 1251 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1364 1252
1365 BLOCK_INPUT;
1366 if (x_had_errors_p (display))
1367 error ("Cannot get selection");
1368 /* This calls x_uncatch_errors. */
1369 unbind_to (count, Qnil);
1370 UNBLOCK_INPUT;
1371
1372 if (NILP (XCAR (reading_selection_reply))) 1253 if (NILP (XCAR (reading_selection_reply)))
1373 error ("Timed out waiting for reply from selection owner"); 1254 error ("Timed out waiting for reply from selection owner");
1374 if (EQ (XCAR (reading_selection_reply), Qlambda)) 1255 if (EQ (XCAR (reading_selection_reply), Qlambda))
@@ -1433,7 +1314,7 @@ x_get_window_property (Display *display, Window window, Atom property,
1433 while (bytes_remaining) 1314 while (bytes_remaining)
1434 { 1315 {
1435#ifdef TRACE_SELECTION 1316#ifdef TRACE_SELECTION
1436 int last = bytes_remaining; 1317 unsigned long last = bytes_remaining;
1437#endif 1318#endif
1438 result 1319 result
1439 = XGetWindowProperty (display, window, property, 1320 = XGetWindowProperty (display, window, property,
@@ -1443,7 +1324,7 @@ x_get_window_property (Display *display, Window window, Atom property,
1443 actual_type_ret, actual_format_ret, 1324 actual_type_ret, actual_format_ret,
1444 actual_size_ret, &bytes_remaining, &tmp_data); 1325 actual_size_ret, &bytes_remaining, &tmp_data);
1445 1326
1446 TRACE2 ("Read %ld bytes from property %s", 1327 TRACE2 ("Read %lu bytes from property %s",
1447 last - bytes_remaining, 1328 last - bytes_remaining,
1448 XGetAtomName (display, property)); 1329 XGetAtomName (display, property));
1449 1330
@@ -1466,7 +1347,7 @@ x_get_window_property (Display *display, Window window, Atom property,
1466 The bytes and offsets passed to XGetWindowProperty refers to the 1347 The bytes and offsets passed to XGetWindowProperty refers to the
1467 property and those are indeed in 32 bit quantities if format is 32. */ 1348 property and those are indeed in 32 bit quantities if format is 32. */
1468 1349
1469 if (*actual_format_ret == 32 && *actual_format_ret < BITS_PER_LONG) 1350 if (32 < BITS_PER_LONG && *actual_format_ret == 32)
1470 { 1351 {
1471 unsigned long i; 1352 unsigned long i;
1472 int *idata = (int *) ((*data_ret) + offset); 1353 int *idata = (int *) ((*data_ret) + offset);
@@ -1586,9 +1467,9 @@ receive_incremental_selection (Display *display, Window window, Atom property,
1586} 1467}
1587 1468
1588 1469
1589/* Once a requested selection is "ready" (we got a SelectionNotify event), 1470/* Fetch a value from property PROPERTY of X window WINDOW on display
1590 fetch value from property PROPERTY of X window WINDOW on display DISPLAY. 1471 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1591 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */ 1472 if this fails. */
1592 1473
1593static Lisp_Object 1474static Lisp_Object
1594x_get_window_property_as_lisp_data (Display *display, Window window, 1475x_get_window_property_as_lisp_data (Display *display, Window window,
@@ -1720,9 +1601,10 @@ selection_data_to_lisp_data (Display *display, const unsigned char *data,
1720 return str; 1601 return str;
1721 } 1602 }
1722 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to 1603 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1723 a vector of symbols. 1604 a vector of symbols. */
1724 */ 1605 else if (type == XA_ATOM
1725 else if (type == XA_ATOM) 1606 /* Treat ATOM_PAIR type similar to list of atoms. */
1607 || type == dpyinfo->Xatom_ATOM_PAIR)
1726 { 1608 {
1727 int i; 1609 int i;
1728 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8. 1610 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
@@ -1749,7 +1631,7 @@ selection_data_to_lisp_data (Display *display, const unsigned char *data,
1749 convert it to a cons of integers, 16 bits in each half. 1631 convert it to a cons of integers, 16 bits in each half.
1750 */ 1632 */
1751 else if (format == 32 && size == sizeof (int)) 1633 else if (format == 32 && size == sizeof (int))
1752 return long_to_cons (((unsigned int *) data) [0]); 1634 return INTEGER_TO_CONS (((unsigned int *) data) [0]);
1753 else if (format == 16 && size == sizeof (short)) 1635 else if (format == 16 && size == sizeof (short))
1754 return make_number ((int) (((unsigned short *) data) [0])); 1636 return make_number ((int) (((unsigned short *) data) [0]));
1755 1637
@@ -1775,7 +1657,7 @@ selection_data_to_lisp_data (Display *display, const unsigned char *data,
1775 for (i = 0; i < size / 4; i++) 1657 for (i = 0; i < size / 4; i++)
1776 { 1658 {
1777 unsigned int j = ((unsigned int *) data) [i]; 1659 unsigned int j = ((unsigned int *) data) [i];
1778 Faset (v, make_number (i), long_to_cons (j)); 1660 Faset (v, make_number (i), INTEGER_TO_CONS (j));
1779 } 1661 }
1780 return v; 1662 return v;
1781 } 1663 }
@@ -1828,7 +1710,7 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1828 *size_ret = 1; 1710 *size_ret = 1;
1829 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1); 1711 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1830 (*data_ret) [sizeof (Atom)] = 0; 1712 (*data_ret) [sizeof (Atom)] = 0;
1831 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj); 1713 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, obj);
1832 if (NILP (type)) type = QATOM; 1714 if (NILP (type)) type = QATOM;
1833 } 1715 }
1834 else if (INTEGERP (obj) 1716 else if (INTEGERP (obj)
@@ -1852,7 +1734,7 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1852 *size_ret = 1; 1734 *size_ret = 1;
1853 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1); 1735 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1854 (*data_ret) [sizeof (long)] = 0; 1736 (*data_ret) [sizeof (long)] = 0;
1855 (*(unsigned long **) data_ret) [0] = cons_to_long (obj); 1737 (*(unsigned long **) data_ret) [0] = cons_to_unsigned (obj, ULONG_MAX);
1856 if (NILP (type)) type = QINTEGER; 1738 if (NILP (type)) type = QINTEGER;
1857 } 1739 }
1858 else if (VECTORP (obj)) 1740 else if (VECTORP (obj))
@@ -1867,52 +1749,22 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1867 /* This vector is an ATOM set */ 1749 /* This vector is an ATOM set */
1868 { 1750 {
1869 if (NILP (type)) type = QATOM; 1751 if (NILP (type)) type = QATOM;
1870 *size_ret = XVECTOR (obj)->size; 1752 *size_ret = ASIZE (obj);
1871 *format_ret = 32; 1753 *format_ret = 32;
1872 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1873 for (i = 0; i < *size_ret; i++) 1754 for (i = 0; i < *size_ret; i++)
1874 if (SYMBOLP (XVECTOR (obj)->contents [i])) 1755 if (!SYMBOLP (XVECTOR (obj)->contents [i]))
1875 (*(Atom **) data_ret) [i]
1876 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
1877 else
1878 signal_error ("All elements of selection vector must have same type", obj); 1756 signal_error ("All elements of selection vector must have same type", obj);
1879 }
1880#if 0 /* #### MULTIPLE doesn't work yet */
1881 else if (VECTORP (XVECTOR (obj)->contents [0]))
1882 /* This vector is an ATOM_PAIR set */
1883 {
1884 if (NILP (type)) type = QATOM_PAIR;
1885 *size_ret = XVECTOR (obj)->size;
1886 *format_ret = 32;
1887 *data_ret = (unsigned char *)
1888 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1889 for (i = 0; i < *size_ret; i++)
1890 if (VECTORP (XVECTOR (obj)->contents [i]))
1891 {
1892 Lisp_Object pair = XVECTOR (obj)->contents [i];
1893 if (XVECTOR (pair)->size != 2)
1894 signal_error (
1895 "Elements of the vector must be vectors of exactly two elements",
1896 pair);
1897
1898 (*(Atom **) data_ret) [i * 2]
1899 = symbol_to_x_atom (dpyinfo, display,
1900 XVECTOR (pair)->contents [0]);
1901 (*(Atom **) data_ret) [(i * 2) + 1]
1902 = symbol_to_x_atom (dpyinfo, display,
1903 XVECTOR (pair)->contents [1]);
1904 }
1905 else
1906 signal_error ("All elements of the vector must be of the same type",
1907 obj);
1908 1757
1758 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1759 for (i = 0; i < *size_ret; i++)
1760 (*(Atom **) data_ret) [i]
1761 = symbol_to_x_atom (dpyinfo, XVECTOR (obj)->contents [i]);
1909 } 1762 }
1910#endif
1911 else 1763 else
1912 /* This vector is an INTEGER set, or something like it */ 1764 /* This vector is an INTEGER set, or something like it */
1913 { 1765 {
1914 int data_size = 2; 1766 int data_size = 2;
1915 *size_ret = XVECTOR (obj)->size; 1767 *size_ret = ASIZE (obj);
1916 if (NILP (type)) type = QINTEGER; 1768 if (NILP (type)) type = QINTEGER;
1917 *format_ret = 16; 1769 *format_ret = 16;
1918 for (i = 0; i < *size_ret; i++) 1770 for (i = 0; i < *size_ret; i++)
@@ -1930,17 +1782,17 @@ lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1930 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size); 1782 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1931 for (i = 0; i < *size_ret; i++) 1783 for (i = 0; i < *size_ret; i++)
1932 if (*format_ret == 32) 1784 if (*format_ret == 32)
1933 (*((unsigned long **) data_ret)) [i] 1785 (*((unsigned long **) data_ret)) [i] =
1934 = cons_to_long (XVECTOR (obj)->contents [i]); 1786 cons_to_unsigned (XVECTOR (obj)->contents [i], ULONG_MAX);
1935 else 1787 else
1936 (*((unsigned short **) data_ret)) [i] 1788 (*((unsigned short **) data_ret)) [i] =
1937 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]); 1789 cons_to_unsigned (XVECTOR (obj)->contents [i], USHRT_MAX);
1938 } 1790 }
1939 } 1791 }
1940 else 1792 else
1941 signal_error (/* Qselection_error */ "Unrecognized selection data", obj); 1793 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1942 1794
1943 *type_ret = symbol_to_x_atom (dpyinfo, display, type); 1795 *type_ret = symbol_to_x_atom (dpyinfo, type);
1944} 1796}
1945 1797
1946static Lisp_Object 1798static Lisp_Object
@@ -1965,7 +1817,7 @@ clean_local_selection_data (Lisp_Object obj)
1965 if (VECTORP (obj)) 1817 if (VECTORP (obj))
1966 { 1818 {
1967 int i; 1819 int i;
1968 int size = XVECTOR (obj)->size; 1820 int size = ASIZE (obj);
1969 Lisp_Object copy; 1821 Lisp_Object copy;
1970 if (size == 1) 1822 if (size == 1)
1971 return clean_local_selection_data (XVECTOR (obj)->contents [0]); 1823 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
@@ -1997,20 +1849,75 @@ x_handle_selection_notify (XSelectionEvent *event)
1997} 1849}
1998 1850
1999 1851
1852/* From a Lisp_Object, return a suitable frame for selection
1853 operations. OBJECT may be a frame, a terminal object, or nil
1854 (which stands for the selected frame--or, if that is not an X
1855 frame, the first X display on the list). If no suitable frame can
1856 be found, return NULL. */
1857
1858static struct frame *
1859frame_for_x_selection (Lisp_Object object)
1860{
1861 Lisp_Object tail;
1862 struct frame *f;
1863
1864 if (NILP (object))
1865 {
1866 f = XFRAME (selected_frame);
1867 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1868 return f;
1869
1870 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1871 {
1872 f = XFRAME (XCAR (tail));
1873 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1874 return f;
1875 }
1876 }
1877 else if (TERMINALP (object))
1878 {
1879 struct terminal *t = get_terminal (object, 1);
1880 if (t->type == output_x_window)
1881 {
1882 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1883 {
1884 f = XFRAME (XCAR (tail));
1885 if (FRAME_LIVE_P (f) && f->terminal == t)
1886 return f;
1887 }
1888 }
1889 }
1890 else if (FRAMEP (object))
1891 {
1892 f = XFRAME (object);
1893 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1894 return f;
1895 }
1896
1897 return NULL;
1898}
1899
1900
2000DEFUN ("x-own-selection-internal", Fx_own_selection_internal, 1901DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
2001 Sx_own_selection_internal, 2, 2, 0, 1902 Sx_own_selection_internal, 2, 3, 0,
2002 doc: /* Assert an X selection of the given TYPE with the given VALUE. 1903 doc: /* Assert an X selection of type SELECTION and value VALUE.
2003TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. 1904SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2004\(Those are literal upper-case symbol names, since that's what X expects.) 1905\(Those are literal upper-case symbol names, since that's what X expects.)
2005VALUE is typically a string, or a cons of two markers, but may be 1906VALUE is typically a string, or a cons of two markers, but may be
2006anything that the functions on `selection-converter-alist' know about. */) 1907anything that the functions on `selection-converter-alist' know about.
2007 (Lisp_Object selection_name, Lisp_Object selection_value) 1908
1909FRAME should be a frame that should own the selection. If omitted or
1910nil, it defaults to the selected frame. */)
1911 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
2008{ 1912{
2009 check_x (); 1913 if (NILP (frame)) frame = selected_frame;
2010 CHECK_SYMBOL (selection_name); 1914 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
2011 if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil"); 1915 error ("X selection unavailable for this frame");
2012 x_own_selection (selection_name, selection_value); 1916
2013 return selection_value; 1917 CHECK_SYMBOL (selection);
1918 if (NILP (value)) error ("VALUE may not be nil");
1919 x_own_selection (selection, value, frame);
1920 return value;
2014} 1921}
2015 1922
2016 1923
@@ -2019,59 +1926,61 @@ anything that the functions on `selection-converter-alist' know about. */)
2019 will block until all of the data has arrived. */ 1926 will block until all of the data has arrived. */
2020 1927
2021DEFUN ("x-get-selection-internal", Fx_get_selection_internal, 1928DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
2022 Sx_get_selection_internal, 2, 3, 0, 1929 Sx_get_selection_internal, 2, 4, 0,
2023 doc: /* Return text selected from some X window. 1930 doc: /* Return text selected from some X window.
2024SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. 1931SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2025\(Those are literal upper-case symbol names, since that's what X expects.) 1932\(Those are literal upper-case symbol names, since that's what X expects.)
2026TYPE is the type of data desired, typically `STRING'. 1933TYPE is the type of data desired, typically `STRING'.
2027TIME_STAMP is the time to use in the XConvertSelection call for foreign 1934TIME_STAMP is the time to use in the XConvertSelection call for foreign
2028selections. If omitted, defaults to the time for the last event. */) 1935selections. If omitted, defaults to the time for the last event.
2029 (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp) 1936
1937TERMINAL should be a terminal object or a frame specifying the X
1938server to query. If omitted or nil, that stands for the selected
1939frame's display, or the first available X display. */)
1940 (Lisp_Object selection_symbol, Lisp_Object target_type,
1941 Lisp_Object time_stamp, Lisp_Object terminal)
2030{ 1942{
2031 Lisp_Object val = Qnil; 1943 Lisp_Object val = Qnil;
2032 struct gcpro gcpro1, gcpro2; 1944 struct gcpro gcpro1, gcpro2;
1945 struct frame *f = frame_for_x_selection (terminal);
2033 GCPRO2 (target_type, val); /* we store newly consed data into these */ 1946 GCPRO2 (target_type, val); /* we store newly consed data into these */
2034 check_x ();
2035 CHECK_SYMBOL (selection_symbol);
2036 1947
2037#if 0 /* #### MULTIPLE doesn't work yet */ 1948 CHECK_SYMBOL (selection_symbol);
2038 if (CONSP (target_type) 1949 CHECK_SYMBOL (target_type);
2039 && XCAR (target_type) == QMULTIPLE) 1950 if (EQ (target_type, QMULTIPLE))
2040 { 1951 error ("Retrieving MULTIPLE selections is currently unimplemented");
2041 CHECK_VECTOR (XCDR (target_type)); 1952 if (!f)
2042 /* So we don't destructively modify this... */ 1953 error ("X selection unavailable for this frame");
2043 target_type = copy_multiple_data (target_type);
2044 }
2045 else
2046#endif
2047 CHECK_SYMBOL (target_type);
2048 1954
2049 val = x_get_local_selection (selection_symbol, target_type, 1); 1955 val = x_get_local_selection (selection_symbol, target_type, 1,
1956 FRAME_X_DISPLAY_INFO (f));
2050 1957
2051 if (NILP (val)) 1958 if (NILP (val) && FRAME_LIVE_P (f))
2052 { 1959 {
2053 val = x_get_foreign_selection (selection_symbol, target_type, time_stamp); 1960 Lisp_Object frame;
2054 goto DONE; 1961 XSETFRAME (frame, f);
1962 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1963 time_stamp, frame));
2055 } 1964 }
2056 1965
2057 if (CONSP (val) 1966 if (CONSP (val) && SYMBOLP (XCAR (val)))
2058 && SYMBOLP (XCAR (val)))
2059 { 1967 {
2060 val = XCDR (val); 1968 val = XCDR (val);
2061 if (CONSP (val) && NILP (XCDR (val))) 1969 if (CONSP (val) && NILP (XCDR (val)))
2062 val = XCAR (val); 1970 val = XCAR (val);
2063 } 1971 }
2064 val = clean_local_selection_data (val); 1972 RETURN_UNGCPRO (clean_local_selection_data (val));
2065 DONE:
2066 UNGCPRO;
2067 return val;
2068} 1973}
2069 1974
2070DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal, 1975DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2071 Sx_disown_selection_internal, 1, 2, 0, 1976 Sx_disown_selection_internal, 1, 3, 0,
2072 doc: /* If we own the selection SELECTION, disown it. 1977 doc: /* If we own the selection SELECTION, disown it.
2073Disowning it means there is no such selection. */) 1978Disowning it means there is no such selection.
2074 (Lisp_Object selection, Lisp_Object time) 1979
1980TERMINAL should be a terminal object or a frame specifying the X
1981server to query. If omitted or nil, that stands for the selected
1982frame's display, or the first available X display. */)
1983 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
2075{ 1984{
2076 Time timestamp; 1985 Time timestamp;
2077 Atom selection_atom; 1986 Atom selection_atom;
@@ -2079,29 +1988,27 @@ Disowning it means there is no such selection. */)
2079 struct selection_input_event sie; 1988 struct selection_input_event sie;
2080 struct input_event ie; 1989 struct input_event ie;
2081 } event; 1990 } event;
2082 Display *display; 1991 struct frame *f = frame_for_x_selection (terminal);
2083 struct x_display_info *dpyinfo; 1992 struct x_display_info *dpyinfo;
2084 struct frame *sf = SELECTED_FRAME ();
2085 1993
2086 check_x (); 1994 if (!f)
2087 if (! FRAME_X_P (sf))
2088 return Qnil; 1995 return Qnil;
2089 1996
2090 display = FRAME_X_DISPLAY (sf); 1997 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2091 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2092 CHECK_SYMBOL (selection); 1998 CHECK_SYMBOL (selection);
2093 if (NILP (time))
2094 timestamp = last_event_timestamp;
2095 else
2096 timestamp = cons_to_long (time);
2097 1999
2098 if (NILP (assq_no_quit (selection, Vselection_alist))) 2000 /* Don't disown the selection when we're not the owner. */
2099 return Qnil; /* Don't disown the selection when we're not the owner. */ 2001 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2002 return Qnil;
2100 2003
2101 selection_atom = symbol_to_x_atom (dpyinfo, display, selection); 2004 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2102 2005
2103 BLOCK_INPUT; 2006 BLOCK_INPUT;
2104 XSetSelectionOwner (display, selection_atom, None, timestamp); 2007 if (NILP (time_object))
2008 timestamp = last_event_timestamp;
2009 else
2010 CONS_TO_INTEGER (time_object, Time, timestamp);
2011 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2105 UNBLOCK_INPUT; 2012 UNBLOCK_INPUT;
2106 2013
2107 /* It doesn't seem to be guaranteed that a SelectionClear event will be 2014 /* It doesn't seem to be guaranteed that a SelectionClear event will be
@@ -2109,7 +2016,7 @@ Disowning it means there is no such selection. */)
2109 the selection owner to None. The NCD server does, the MIT Sun4 server 2016 the selection owner to None. The NCD server does, the MIT Sun4 server
2110 doesn't. So we synthesize one; this means we might get two, but 2017 doesn't. So we synthesize one; this means we might get two, but
2111 that's ok, because the second one won't have any effect. */ 2018 that's ok, because the second one won't have any effect. */
2112 SELECTION_EVENT_DISPLAY (&event.sie) = display; 2019 SELECTION_EVENT_DISPLAY (&event.sie) = dpyinfo->display;
2113 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom; 2020 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2114 SELECTION_EVENT_TIME (&event.sie) = timestamp; 2021 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2115 x_handle_selection_clear (&event.ie); 2022 x_handle_selection_clear (&event.ie);
@@ -2117,87 +2024,178 @@ Disowning it means there is no such selection. */)
2117 return Qt; 2024 return Qt;
2118} 2025}
2119 2026
2120/* Get rid of all the selections in buffer BUFFER.
2121 This is used when we kill a buffer. */
2122
2123void
2124x_disown_buffer_selections (Lisp_Object buffer)
2125{
2126 Lisp_Object tail;
2127 struct buffer *buf = XBUFFER (buffer);
2128
2129 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
2130 {
2131 Lisp_Object elt, value;
2132 elt = XCAR (tail);
2133 value = XCDR (elt);
2134 if (CONSP (value) && MARKERP (XCAR (value))
2135 && XMARKER (XCAR (value))->buffer == buf)
2136 Fx_disown_selection_internal (XCAR (elt), Qnil);
2137 }
2138}
2139
2140DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p, 2027DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2141 0, 1, 0, 2028 0, 2, 0,
2142 doc: /* Whether the current Emacs process owns the given X Selection. 2029 doc: /* Whether the current Emacs process owns the given X Selection.
2143The arg should be the name of the selection in question, typically one of 2030The arg should be the name of the selection in question, typically one of
2144the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. 2031the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2145\(Those are literal upper-case symbol names, since that's what X expects.) 2032\(Those are literal upper-case symbol names, since that's what X expects.)
2146For convenience, the symbol nil is the same as `PRIMARY', 2033For convenience, the symbol nil is the same as `PRIMARY',
2147and t is the same as `SECONDARY'. */) 2034and t is the same as `SECONDARY'.
2148 (Lisp_Object selection) 2035
2036TERMINAL should be a terminal object or a frame specifying the X
2037server to query. If omitted or nil, that stands for the selected
2038frame's display, or the first available X display. */)
2039 (Lisp_Object selection, Lisp_Object terminal)
2149{ 2040{
2150 check_x (); 2041 struct frame *f = frame_for_x_selection (terminal);
2042
2151 CHECK_SYMBOL (selection); 2043 CHECK_SYMBOL (selection);
2152 if (EQ (selection, Qnil)) selection = QPRIMARY; 2044 if (EQ (selection, Qnil)) selection = QPRIMARY;
2153 if (EQ (selection, Qt)) selection = QSECONDARY; 2045 if (EQ (selection, Qt)) selection = QSECONDARY;
2154 2046
2155 if (NILP (Fassq (selection, Vselection_alist))) 2047 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f))))
2048 return Qt;
2049 else
2156 return Qnil; 2050 return Qnil;
2157 return Qt;
2158} 2051}
2159 2052
2160DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, 2053DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2161 0, 1, 0, 2054 0, 2, 0,
2162 doc: /* Whether there is an owner for the given X Selection. 2055 doc: /* Whether there is an owner for the given X selection.
2163The arg should be the name of the selection in question, typically one of 2056SELECTION should be the name of the selection in question, typically
2164the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. 2057one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
2165\(Those are literal upper-case symbol names, since that's what X expects.) 2058these literal upper-case names.) The symbol nil is the same as
2166For convenience, the symbol nil is the same as `PRIMARY', 2059`PRIMARY', and t is the same as `SECONDARY'.
2167and t is the same as `SECONDARY'. */) 2060
2168 (Lisp_Object selection) 2061TERMINAL should be a terminal object or a frame specifying the X
2062server to query. If omitted or nil, that stands for the selected
2063frame's display, or the first available X display. */)
2064 (Lisp_Object selection, Lisp_Object terminal)
2169{ 2065{
2170 Window owner; 2066 Window owner;
2171 Atom atom; 2067 Atom atom;
2172 Display *dpy; 2068 struct frame *f = frame_for_x_selection (terminal);
2173 struct frame *sf = SELECTED_FRAME (); 2069 struct x_display_info *dpyinfo;
2174
2175 /* It should be safe to call this before we have an X frame. */
2176 if (! FRAME_X_P (sf))
2177 return Qnil;
2178 2070
2179 dpy = FRAME_X_DISPLAY (sf);
2180 CHECK_SYMBOL (selection); 2071 CHECK_SYMBOL (selection);
2181 if (!NILP (Fx_selection_owner_p (selection)))
2182 return Qt;
2183 if (EQ (selection, Qnil)) selection = QPRIMARY; 2072 if (EQ (selection, Qnil)) selection = QPRIMARY;
2184 if (EQ (selection, Qt)) selection = QSECONDARY; 2073 if (EQ (selection, Qt)) selection = QSECONDARY;
2185 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection); 2074
2186 if (atom == 0) 2075 if (!f)
2187 return Qnil; 2076 return Qnil;
2077
2078 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2079
2080 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2081 return Qt;
2082
2083 atom = symbol_to_x_atom (dpyinfo, selection);
2084 if (atom == 0) return Qnil;
2188 BLOCK_INPUT; 2085 BLOCK_INPUT;
2189 owner = XGetSelectionOwner (dpy, atom); 2086 owner = XGetSelectionOwner (dpyinfo->display, atom);
2190 UNBLOCK_INPUT; 2087 UNBLOCK_INPUT;
2191 return (owner ? Qt : Qnil); 2088 return (owner ? Qt : Qnil);
2192} 2089}
2193 2090
2194 2091
2092/* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2093 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2094
2095static Lisp_Object
2096x_clipboard_manager_save (Lisp_Object frame)
2097{
2098 struct frame *f = XFRAME (frame);
2099 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2100 Atom data = dpyinfo->Xatom_UTF8_STRING;
2101
2102 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2103 dpyinfo->Xatom_EMACS_TMP,
2104 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2105 (unsigned char *) &data, 1);
2106 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2107 Qnil, frame);
2108 return Qt;
2109}
2110
2111/* Error handler for x_clipboard_manager_save_frame. */
2112
2113static Lisp_Object
2114x_clipboard_manager_error_1 (Lisp_Object err)
2115{
2116 Lisp_Object args[2];
2117 args[0] = build_string ("X clipboard manager error: %s\n\
2118If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2119 args[1] = CAR (CDR (err));
2120 Fmessage (2, args);
2121 return Qnil;
2122}
2123
2124/* Error handler for x_clipboard_manager_save_all. */
2125
2126static Lisp_Object
2127x_clipboard_manager_error_2 (Lisp_Object err)
2128{
2129 fprintf (stderr, "Error saving to X clipboard manager.\n\
2130If the problem persists, set `x-select-enable-clipboard-manager' \
2131to nil.\n");
2132 return Qnil;
2133}
2134
2135/* Called from delete_frame: save any clipboard owned by FRAME to the
2136 clipboard manager. Do nothing if FRAME does not own the clipboard,
2137 or if no clipboard manager is present. */
2138
2139void
2140x_clipboard_manager_save_frame (Lisp_Object frame)
2141{
2142 struct frame *f;
2143
2144 if (!NILP (Vx_select_enable_clipboard_manager)
2145 && FRAMEP (frame)
2146 && (f = XFRAME (frame), FRAME_X_P (f))
2147 && FRAME_LIVE_P (f))
2148 {
2149 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2150 Lisp_Object local_selection
2151 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2152
2153 if (!NILP (local_selection)
2154 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2155 && XGetSelectionOwner (dpyinfo->display,
2156 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2157 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2158 x_clipboard_manager_error_1);
2159 }
2160}
2161
2162/* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2163 clipboard manager. Do nothing if FRAME does not own the clipboard,
2164 or if no clipboard manager is present. */
2165
2166void
2167x_clipboard_manager_save_all (void)
2168{
2169 /* Loop through all X displays, saving owned clipboards. */
2170 struct x_display_info *dpyinfo;
2171 Lisp_Object local_selection, local_frame;
2172
2173 if (NILP (Vx_select_enable_clipboard_manager))
2174 return;
2175
2176 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2177 {
2178 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2179 if (NILP (local_selection)
2180 || !XGetSelectionOwner (dpyinfo->display,
2181 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2182 continue;
2183
2184 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2185 if (FRAME_LIVE_P (XFRAME (local_frame)))
2186 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2187 Qt, x_clipboard_manager_error_2);
2188 }
2189}
2190
2191
2195/*********************************************************************** 2192/***********************************************************************
2196 Drag and drop support 2193 Drag and drop support
2197***********************************************************************/ 2194***********************************************************************/
2198/* Check that lisp values are of correct type for x_fill_property_data. 2195/* Check that lisp values are of correct type for x_fill_property_data.
2199 That is, number, string or a cons with two numbers (low and high 16 2196 That is, number, string or a cons with two numbers (low and high 16
2200 bit parts of a 32 bit number). */ 2197 bit parts of a 32 bit number). Return the number of items in DATA,
2198 or -1 if there is an error. */
2201 2199
2202int 2200int
2203x_check_property_data (Lisp_Object data) 2201x_check_property_data (Lisp_Object data)
@@ -2205,15 +2203,16 @@ x_check_property_data (Lisp_Object data)
2205 Lisp_Object iter; 2203 Lisp_Object iter;
2206 int size = 0; 2204 int size = 0;
2207 2205
2208 for (iter = data; CONSP (iter) && size != -1; iter = XCDR (iter), ++size) 2206 for (iter = data; CONSP (iter); iter = XCDR (iter))
2209 { 2207 {
2210 Lisp_Object o = XCAR (iter); 2208 Lisp_Object o = XCAR (iter);
2211 2209
2212 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o)) 2210 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2213 size = -1; 2211 return -1;
2214 else if (CONSP (o) && 2212 else if (CONSP (o) &&
2215 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o)))) 2213 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2216 size = -1; 2214 return -1;
2215 size++;
2217 } 2216 }
2218 2217
2219 return size; 2218 return size;
@@ -2245,12 +2244,8 @@ x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2245 { 2244 {
2246 Lisp_Object o = XCAR (iter); 2245 Lisp_Object o = XCAR (iter);
2247 2246
2248 if (INTEGERP (o)) 2247 if (INTEGERP (o) || FLOATP (o) || CONSP (o))
2249 val = (long) XFASTINT (o); 2248 val = cons_to_signed (o, LONG_MIN, LONG_MAX);
2250 else if (FLOATP (o))
2251 val = (long) XFLOAT_DATA (o);
2252 else if (CONSP (o))
2253 val = (long) cons_to_long (o);
2254 else if (STRINGP (o)) 2249 else if (STRINGP (o))
2255 { 2250 {
2256 BLOCK_INPUT; 2251 BLOCK_INPUT;
@@ -2261,9 +2256,19 @@ x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2261 error ("Wrong type, must be string, number or cons"); 2256 error ("Wrong type, must be string, number or cons");
2262 2257
2263 if (format == 8) 2258 if (format == 8)
2264 *d08++ = (char) val; 2259 {
2260 if (CHAR_MIN <= val && val <= CHAR_MAX)
2261 *d08++ = val;
2262 else
2263 error ("Out of 'char' range");
2264 }
2265 else if (format == 16) 2265 else if (format == 16)
2266 *d16++ = (short) val; 2266 {
2267 if (SHRT_MIN <= val && val <= SHRT_MAX)
2268 *d16++ = val;
2269 else
2270 error ("Out of 'short' range");
2271 }
2267 else 2272 else
2268 *d32++ = val; 2273 *d32++ = val;
2269 } 2274 }
@@ -2347,14 +2352,7 @@ If the value is 0 or the atom is not known, return the empty string. */)
2347 Atom atom; 2352 Atom atom;
2348 int had_errors; 2353 int had_errors;
2349 2354
2350 if (INTEGERP (value)) 2355 CONS_TO_INTEGER (value, Atom, atom);
2351 atom = (Atom) XUINT (value);
2352 else if (FLOATP (value))
2353 atom = (Atom) XFLOAT_DATA (value);
2354 else if (CONSP (value))
2355 atom = (Atom) cons_to_long (value);
2356 else
2357 error ("Wrong type, value must be number or cons");
2358 2356
2359 BLOCK_INPUT; 2357 BLOCK_INPUT;
2360 x_catch_errors (dpy); 2358 x_catch_errors (dpy);
@@ -2387,7 +2385,7 @@ FRAME is on. If FRAME is nil, the selected frame is used. */)
2387 2385
2388 2386
2389 if (SYMBOLP (atom)) 2387 if (SYMBOLP (atom))
2390 x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom); 2388 x_atom = symbol_to_x_atom (dpyinfo, atom);
2391 else if (STRINGP (atom)) 2389 else if (STRINGP (atom))
2392 { 2390 {
2393 BLOCK_INPUT; 2391 BLOCK_INPUT;
@@ -2439,9 +2437,8 @@ x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_disp
2439 function expects them to be of size int (i.e. 32). So to be able to 2437 function expects them to be of size int (i.e. 32). So to be able to
2440 use that function, put the data in the form it expects if format is 32. */ 2438 use that function, put the data in the form it expects if format is 32. */
2441 2439
2442 if (event->format == 32 && event->format < BITS_PER_LONG) 2440 if (32 < BITS_PER_LONG && event->format == 32)
2443 { 2441 {
2444 int i;
2445 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */ 2442 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2446 idata[i] = (int) event->data.l[i]; 2443 idata[i] = (int) event->data.l[i];
2447 data = (unsigned char *) idata; 2444 data = (unsigned char *) idata;
@@ -2545,17 +2542,8 @@ x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, At
2545 else 2542 else
2546 error ("DEST as a string must be one of PointerWindow or InputFocus"); 2543 error ("DEST as a string must be one of PointerWindow or InputFocus");
2547 } 2544 }
2548 else if (INTEGERP (dest)) 2545 else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest))
2549 wdest = (Window) XFASTINT (dest); 2546 CONS_TO_INTEGER (dest, Window, wdest);
2550 else if (FLOATP (dest))
2551 wdest = (Window) XFLOAT_DATA (dest);
2552 else if (CONSP (dest))
2553 {
2554 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2555 error ("Both car and cdr for DEST must be numbers");
2556 else
2557 wdest = (Window) cons_to_long (dest);
2558 }
2559 else 2547 else
2560 error ("DEST must be a frame, nil, string, number or cons"); 2548 error ("DEST must be a frame, nil, string, number or cons");
2561 2549
@@ -2615,8 +2603,8 @@ syms_of_xselect (void)
2615 property_change_reply = Fcons (Qnil, Qnil); 2603 property_change_reply = Fcons (Qnil, Qnil);
2616 staticpro (&property_change_reply); 2604 staticpro (&property_change_reply);
2617 2605
2618 Vselection_alist = Qnil; 2606 converted_selections = NULL;
2619 staticpro (&Vselection_alist); 2607 conversion_fail_tag = None;
2620 2608
2621 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist, 2609 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2622 doc: /* An alist associating X Windows selection-types with functions. 2610 doc: /* An alist associating X Windows selection-types with functions.
@@ -2643,7 +2631,7 @@ The functions are called with one argument, the selection type
2643 2631
2644 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions, 2632 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2645 doc: /* A list of functions to be called when Emacs answers a selection request. 2633 doc: /* A list of functions to be called when Emacs answers a selection request.
2646The functions are called with four arguments: 2634The functions are called with three arguments:
2647 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD'); 2635 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2648 - the selection-type which Emacs was asked to convert the 2636 - the selection-type which Emacs was asked to convert the
2649 selection into before sending (for example, `STRING' or `LENGTH'); 2637 selection into before sending (for example, `STRING' or `LENGTH');
@@ -2655,6 +2643,14 @@ This hook doesn't let you change the behavior of Emacs's selection replies,
2655it merely informs you that they have happened. */); 2643it merely informs you that they have happened. */);
2656 Vx_sent_selection_functions = Qnil; 2644 Vx_sent_selection_functions = Qnil;
2657 2645
2646 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2647 Vx_select_enable_clipboard_manager,
2648 doc: /* Whether to enable X clipboard manager support.
2649If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2650while owning the X clipboard, the clipboard contents are saved to the
2651clipboard manager if one is present. */);
2652 Vx_select_enable_clipboard_manager = Qt;
2653
2658 DEFVAR_INT ("x-selection-timeout", x_selection_timeout, 2654 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2659 doc: /* Number of milliseconds to wait for a selection reply. 2655 doc: /* Number of milliseconds to wait for a selection reply.
2660If the selection owner doesn't reply in this time, we give up. 2656If the selection owner doesn't reply in this time, we give up.
@@ -2663,25 +2659,24 @@ A value of 0 means wait as long as necessary. This is initialized from the
2663 x_selection_timeout = 0; 2659 x_selection_timeout = 0;
2664 2660
2665 /* QPRIMARY is defined in keyboard.c. */ 2661 /* QPRIMARY is defined in keyboard.c. */
2666 QSECONDARY = intern_c_string ("SECONDARY"); staticpro (&QSECONDARY); 2662 DEFSYM (QSECONDARY, "SECONDARY");
2667 QSTRING = intern_c_string ("STRING"); staticpro (&QSTRING); 2663 DEFSYM (QSTRING, "STRING");
2668 QINTEGER = intern_c_string ("INTEGER"); staticpro (&QINTEGER); 2664 DEFSYM (QINTEGER, "INTEGER");
2669 QCLIPBOARD = intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD); 2665 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2670 QTIMESTAMP = intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP); 2666 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2671 QTEXT = intern_c_string ("TEXT"); staticpro (&QTEXT); 2667 DEFSYM (QTEXT, "TEXT");
2672 QCOMPOUND_TEXT = intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT); 2668 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2673 QUTF8_STRING = intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING); 2669 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2674 QDELETE = intern_c_string ("DELETE"); staticpro (&QDELETE); 2670 DEFSYM (QDELETE, "DELETE");
2675 QMULTIPLE = intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE); 2671 DEFSYM (QMULTIPLE, "MULTIPLE");
2676 QINCR = intern_c_string ("INCR"); staticpro (&QINCR); 2672 DEFSYM (QINCR, "INCR");
2677 QEMACS_TMP = intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP); 2673 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2678 QTARGETS = intern_c_string ("TARGETS"); staticpro (&QTARGETS); 2674 DEFSYM (QTARGETS, "TARGETS");
2679 QATOM = intern_c_string ("ATOM"); staticpro (&QATOM); 2675 DEFSYM (QATOM, "ATOM");
2680 QATOM_PAIR = intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR); 2676 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2681 QNULL = intern_c_string ("NULL"); staticpro (&QNULL); 2677 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2682 Qcompound_text_with_extensions = intern_c_string ("compound-text-with-extensions"); 2678 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2683 staticpro (&Qcompound_text_with_extensions); 2679 DEFSYM (QNULL, "NULL");
2684 2680 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2685 Qforeign_selection = intern_c_string ("foreign-selection"); 2681 DEFSYM (Qforeign_selection, "foreign-selection");
2686 staticpro (&Qforeign_selection);
2687} 2682}