* src/xselect.c (syms_of_xselect): Include character.h; use DEFSYM.
[bpt/emacs.git] / src / xselect.c
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2011 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19
20 /* Rewritten by jwz */
21
22 #include <config.h>
23 #include <stdio.h> /* termhooks.h needs this */
24 #include <setjmp.h>
25
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
29
30 #include <unistd.h>
31
32 #include "lisp.h"
33 #include "xterm.h" /* for all of the X includes */
34 #include "dispextern.h" /* frame.h seems to want this */
35 #include "frame.h" /* Need this to get the X window of selected_frame */
36 #include "blockinput.h"
37 #include "buffer.h"
38 #include "process.h"
39 #include "termhooks.h"
40 #include "keyboard.h"
41 #include "character.h"
42
43 #include <X11/Xproto.h>
44
45 struct prop_location;
46
47 static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
48 static Atom symbol_to_x_atom (struct x_display_info *, Display *,
49 Lisp_Object);
50 static void x_own_selection (Lisp_Object, Lisp_Object);
51 static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int);
52 static void x_decline_selection_request (struct input_event *);
53 static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
54 static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
55 static Lisp_Object some_frame_on_display (struct x_display_info *);
56 static Lisp_Object x_catch_errors_unwind (Lisp_Object);
57 static void x_reply_selection_request (struct input_event *, int,
58 unsigned char *, int, Atom);
59 static int waiting_for_other_props_on_window (Display *, Window);
60 static struct prop_location *expect_property_change (Display *, Window,
61 Atom, int);
62 static void unexpect_property_change (struct prop_location *);
63 static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
64 static void wait_for_property_change (struct prop_location *);
65 static Lisp_Object x_get_foreign_selection (Lisp_Object,
66 Lisp_Object,
67 Lisp_Object);
68 static void x_get_window_property (Display *, Window, Atom,
69 unsigned char **, int *,
70 Atom *, int *, unsigned long *, int);
71 static void receive_incremental_selection (Display *, Window, Atom,
72 Lisp_Object, unsigned,
73 unsigned char **, int *,
74 Atom *, int *, unsigned long *);
75 static Lisp_Object x_get_window_property_as_lisp_data (Display *,
76 Window, Atom,
77 Lisp_Object, Atom);
78 static Lisp_Object selection_data_to_lisp_data (Display *,
79 const unsigned char *,
80 int, Atom, int);
81 static void lisp_data_to_selection_data (Display *, Lisp_Object,
82 unsigned char **, Atom *,
83 unsigned *, int *, int *);
84 static Lisp_Object clean_local_selection_data (Lisp_Object);
85
86 /* Printing traces to stderr. */
87
88 #ifdef TRACE_SELECTION
89 #define TRACE0(fmt) \
90 fprintf (stderr, "%d: " fmt "\n", getpid ())
91 #define TRACE1(fmt, a0) \
92 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
93 #define TRACE2(fmt, a0, a1) \
94 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
95 #define TRACE3(fmt, a0, a1, a2) \
96 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
97 #else
98 #define TRACE0(fmt) (void) 0
99 #define TRACE1(fmt, a0) (void) 0
100 #define TRACE2(fmt, a0, a1) (void) 0
101 #endif
102
103
104 static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
105 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
106 QATOM_PAIR;
107
108 static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
109 static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
110
111 static Lisp_Object Qcompound_text_with_extensions;
112
113 static Lisp_Object Qforeign_selection;
114
115 /* If this is a smaller number than the max-request-size of the display,
116 emacs will use INCR selection transfer when the selection is larger
117 than this. The max-request-size is usually around 64k, so if you want
118 emacs to use incremental selection transfers when the selection is
119 smaller than that, set this. I added this mostly for debugging the
120 incremental transfer stuff, but it might improve server performance. */
121 #define MAX_SELECTION_QUANTUM 0xFFFFFF
122
123 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
124
125 /* This is an association list whose elements are of the form
126 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
127 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
128 SELECTION-VALUE is the value that emacs owns for that selection.
129 It may be any kind of Lisp object.
130 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
131 as a cons of two 16-bit numbers (making a 32 bit time.)
132 FRAME is the frame for which we made the selection.
133 If there is an entry in this alist, then it can be assumed that Emacs owns
134 that selection.
135 The only (eq) parts of this list that are visible from Lisp are the
136 selection-values. */
137 static Lisp_Object Vselection_alist;
138
139
140 \f
141 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
142 handling. */
143
144 struct selection_event_queue
145 {
146 struct input_event event;
147 struct selection_event_queue *next;
148 };
149
150 static struct selection_event_queue *selection_queue;
151
152 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
153
154 static int x_queue_selection_requests;
155
156 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
157
158 static void
159 x_queue_event (struct input_event *event)
160 {
161 struct selection_event_queue *queue_tmp;
162
163 /* Don't queue repeated requests.
164 This only happens for large requests which uses the incremental protocol. */
165 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
166 {
167 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
168 {
169 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
170 x_decline_selection_request (event);
171 return;
172 }
173 }
174
175 queue_tmp
176 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
177
178 if (queue_tmp != NULL)
179 {
180 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
181 queue_tmp->event = *event;
182 queue_tmp->next = selection_queue;
183 selection_queue = queue_tmp;
184 }
185 }
186
187 /* Start queuing SELECTION_REQUEST_EVENT events. */
188
189 static void
190 x_start_queuing_selection_requests (void)
191 {
192 if (x_queue_selection_requests)
193 abort ();
194
195 x_queue_selection_requests++;
196 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
197 }
198
199 /* Stop queuing SELECTION_REQUEST_EVENT events. */
200
201 static void
202 x_stop_queuing_selection_requests (void)
203 {
204 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
205 --x_queue_selection_requests;
206
207 /* Take all the queued events and put them back
208 so that they get processed afresh. */
209
210 while (selection_queue != NULL)
211 {
212 struct selection_event_queue *queue_tmp = selection_queue;
213 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
214 kbd_buffer_unget_event (&queue_tmp->event);
215 selection_queue = queue_tmp->next;
216 xfree ((char *)queue_tmp);
217 }
218 }
219 \f
220
221 /* This converts a Lisp symbol to a server Atom, avoiding a server
222 roundtrip whenever possible. */
223
224 static Atom
225 symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym)
226 {
227 Atom val;
228 if (NILP (sym)) return 0;
229 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
230 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
231 if (EQ (sym, QSTRING)) return XA_STRING;
232 if (EQ (sym, QINTEGER)) return XA_INTEGER;
233 if (EQ (sym, QATOM)) return XA_ATOM;
234 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
235 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
236 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
237 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
238 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
239 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
240 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
241 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
242 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
243 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
244 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
245 if (!SYMBOLP (sym)) abort ();
246
247 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
248 BLOCK_INPUT;
249 val = XInternAtom (display, SSDATA (SYMBOL_NAME (sym)), False);
250 UNBLOCK_INPUT;
251 return val;
252 }
253
254
255 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
256 and calls to intern whenever possible. */
257
258 static Lisp_Object
259 x_atom_to_symbol (Display *dpy, Atom atom)
260 {
261 struct x_display_info *dpyinfo;
262 char *str;
263 Lisp_Object val;
264
265 if (! atom)
266 return Qnil;
267
268 switch (atom)
269 {
270 case XA_PRIMARY:
271 return QPRIMARY;
272 case XA_SECONDARY:
273 return QSECONDARY;
274 case XA_STRING:
275 return QSTRING;
276 case XA_INTEGER:
277 return QINTEGER;
278 case XA_ATOM:
279 return QATOM;
280 }
281
282 dpyinfo = x_display_info_for_display (dpy);
283 if (atom == dpyinfo->Xatom_CLIPBOARD)
284 return QCLIPBOARD;
285 if (atom == dpyinfo->Xatom_TIMESTAMP)
286 return QTIMESTAMP;
287 if (atom == dpyinfo->Xatom_TEXT)
288 return QTEXT;
289 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
290 return QCOMPOUND_TEXT;
291 if (atom == dpyinfo->Xatom_UTF8_STRING)
292 return QUTF8_STRING;
293 if (atom == dpyinfo->Xatom_DELETE)
294 return QDELETE;
295 if (atom == dpyinfo->Xatom_MULTIPLE)
296 return QMULTIPLE;
297 if (atom == dpyinfo->Xatom_INCR)
298 return QINCR;
299 if (atom == dpyinfo->Xatom_EMACS_TMP)
300 return QEMACS_TMP;
301 if (atom == dpyinfo->Xatom_TARGETS)
302 return QTARGETS;
303 if (atom == dpyinfo->Xatom_NULL)
304 return QNULL;
305
306 BLOCK_INPUT;
307 str = XGetAtomName (dpy, atom);
308 UNBLOCK_INPUT;
309 TRACE1 ("XGetAtomName --> %s", str);
310 if (! str) return Qnil;
311 val = intern (str);
312 BLOCK_INPUT;
313 /* This was allocated by Xlib, so use XFree. */
314 XFree (str);
315 UNBLOCK_INPUT;
316 return val;
317 }
318 \f
319 /* Do protocol to assert ourself as a selection owner.
320 Update the Vselection_alist so that we can reply to later requests for
321 our selection. */
322
323 static void
324 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
325 {
326 struct frame *sf = SELECTED_FRAME ();
327 Window selecting_window;
328 Display *display;
329 Time timestamp = last_event_timestamp;
330 Atom selection_atom;
331 struct x_display_info *dpyinfo;
332
333 if (! FRAME_X_P (sf))
334 return;
335
336 selecting_window = FRAME_X_WINDOW (sf);
337 display = FRAME_X_DISPLAY (sf);
338 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
339
340 CHECK_SYMBOL (selection_name);
341 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
342
343 BLOCK_INPUT;
344 x_catch_errors (display);
345 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
346 x_check_errors (display, "Can't set selection: %s");
347 x_uncatch_errors ();
348 UNBLOCK_INPUT;
349
350 /* Now update the local cache */
351 {
352 Lisp_Object selection_time;
353 Lisp_Object selection_data;
354 Lisp_Object prev_value;
355
356 selection_time = long_to_cons (timestamp);
357 selection_data = list4 (selection_name, selection_value,
358 selection_time, selected_frame);
359 prev_value = assq_no_quit (selection_name, Vselection_alist);
360
361 Vselection_alist = Fcons (selection_data, Vselection_alist);
362
363 /* If we already owned the selection, remove the old selection data.
364 Perhaps we should destructively modify it instead.
365 Don't use Fdelq as that may QUIT. */
366 if (!NILP (prev_value))
367 {
368 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
369 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
370 if (EQ (prev_value, Fcar (XCDR (rest))))
371 {
372 XSETCDR (rest, Fcdr (XCDR (rest)));
373 break;
374 }
375 }
376 }
377 }
378 \f
379 /* Given a selection-name and desired type, look up our local copy of
380 the selection value and convert it to the type.
381 The value is nil or a string.
382 This function is used both for remote requests (LOCAL_REQUEST is zero)
383 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
384
385 This calls random Lisp code, and may signal or gc. */
386
387 static Lisp_Object
388 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request)
389 {
390 Lisp_Object local_value;
391 Lisp_Object handler_fn, value, check;
392 int count;
393
394 local_value = assq_no_quit (selection_symbol, Vselection_alist);
395
396 if (NILP (local_value)) return Qnil;
397
398 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
399 if (EQ (target_type, QTIMESTAMP))
400 {
401 handler_fn = Qnil;
402 value = XCAR (XCDR (XCDR (local_value)));
403 }
404 #if 0 /* #### MULTIPLE doesn't work yet */
405 else if (CONSP (target_type)
406 && XCAR (target_type) == QMULTIPLE)
407 {
408 Lisp_Object pairs;
409 int size;
410 int i;
411 pairs = XCDR (target_type);
412 size = ASIZE (pairs);
413 /* If the target is MULTIPLE, then target_type looks like
414 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
415 We modify the second element of each pair in the vector and
416 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
417 */
418 for (i = 0; i < size; i++)
419 {
420 Lisp_Object pair;
421 pair = XVECTOR (pairs)->contents [i];
422 XVECTOR (pair)->contents [1]
423 = x_get_local_selection (XVECTOR (pair)->contents [0],
424 XVECTOR (pair)->contents [1],
425 local_request);
426 }
427 return pairs;
428 }
429 #endif
430 else
431 {
432 /* Don't allow a quit within the converter.
433 When the user types C-g, he would be surprised
434 if by luck it came during a converter. */
435 count = SPECPDL_INDEX ();
436 specbind (Qinhibit_quit, Qt);
437
438 CHECK_SYMBOL (target_type);
439 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
440 /* gcpro is not needed here since nothing but HANDLER_FN
441 is live, and that ought to be a symbol. */
442
443 if (!NILP (handler_fn))
444 value = call3 (handler_fn,
445 selection_symbol, (local_request ? Qnil : target_type),
446 XCAR (XCDR (local_value)));
447 else
448 value = Qnil;
449 unbind_to (count, Qnil);
450 }
451
452 /* Make sure this value is of a type that we could transmit
453 to another X client. */
454
455 check = value;
456 if (CONSP (value)
457 && SYMBOLP (XCAR (value)))
458 check = XCDR (value);
459
460 if (STRINGP (check)
461 || VECTORP (check)
462 || SYMBOLP (check)
463 || INTEGERP (check)
464 || NILP (value))
465 return value;
466 /* Check for a value that cons_to_long could handle. */
467 else if (CONSP (check)
468 && INTEGERP (XCAR (check))
469 && (INTEGERP (XCDR (check))
470 ||
471 (CONSP (XCDR (check))
472 && INTEGERP (XCAR (XCDR (check)))
473 && NILP (XCDR (XCDR (check))))))
474 return value;
475
476 signal_error ("Invalid data returned by selection-conversion function",
477 list2 (handler_fn, value));
478 }
479 \f
480 /* Subroutines of x_reply_selection_request. */
481
482 /* Send a SelectionNotify event to the requestor with property=None,
483 meaning we were unable to do what they wanted. */
484
485 static void
486 x_decline_selection_request (struct input_event *event)
487 {
488 XEvent reply_base;
489 XSelectionEvent *reply = &(reply_base.xselection);
490
491 reply->type = SelectionNotify;
492 reply->display = SELECTION_EVENT_DISPLAY (event);
493 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
494 reply->selection = SELECTION_EVENT_SELECTION (event);
495 reply->time = SELECTION_EVENT_TIME (event);
496 reply->target = SELECTION_EVENT_TARGET (event);
497 reply->property = None;
498
499 /* The reason for the error may be that the receiver has
500 died in the meantime. Handle that case. */
501 BLOCK_INPUT;
502 x_catch_errors (reply->display);
503 XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base);
504 XFlush (reply->display);
505 x_uncatch_errors ();
506 UNBLOCK_INPUT;
507 }
508
509 /* This is the selection request currently being processed.
510 It is set to zero when the request is fully processed. */
511 static struct input_event *x_selection_current_request;
512
513 /* Display info in x_selection_request. */
514
515 static struct x_display_info *selection_request_dpyinfo;
516
517 /* Used as an unwind-protect clause so that, if a selection-converter signals
518 an error, we tell the requester that we were unable to do what they wanted
519 before we throw to top-level or go into the debugger or whatever. */
520
521 static Lisp_Object
522 x_selection_request_lisp_error (Lisp_Object ignore)
523 {
524 if (x_selection_current_request != 0
525 && selection_request_dpyinfo->display)
526 x_decline_selection_request (x_selection_current_request);
527 return Qnil;
528 }
529
530 static Lisp_Object
531 x_catch_errors_unwind (Lisp_Object dummy)
532 {
533 BLOCK_INPUT;
534 x_uncatch_errors ();
535 UNBLOCK_INPUT;
536 return Qnil;
537 }
538 \f
539
540 /* This stuff is so that INCR selections are reentrant (that is, so we can
541 be servicing multiple INCR selection requests simultaneously.) I haven't
542 actually tested that yet. */
543
544 /* Keep a list of the property changes that are awaited. */
545
546 struct prop_location
547 {
548 int identifier;
549 Display *display;
550 Window window;
551 Atom property;
552 int desired_state;
553 int arrived;
554 struct prop_location *next;
555 };
556
557 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
558 static void wait_for_property_change (struct prop_location *location);
559 static void unexpect_property_change (struct prop_location *location);
560 static int waiting_for_other_props_on_window (Display *display, Window window);
561
562 static int prop_location_identifier;
563
564 static Lisp_Object property_change_reply;
565
566 static struct prop_location *property_change_reply_object;
567
568 static struct prop_location *property_change_wait_list;
569
570 static Lisp_Object
571 queue_selection_requests_unwind (Lisp_Object tem)
572 {
573 x_stop_queuing_selection_requests ();
574 return Qnil;
575 }
576
577 /* Return some frame whose display info is DPYINFO.
578 Return nil if there is none. */
579
580 static Lisp_Object
581 some_frame_on_display (struct x_display_info *dpyinfo)
582 {
583 Lisp_Object list, frame;
584
585 FOR_EACH_FRAME (list, frame)
586 {
587 if (FRAME_X_P (XFRAME (frame))
588 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
589 return frame;
590 }
591
592 return Qnil;
593 }
594 \f
595 /* Send the reply to a selection request event EVENT.
596 TYPE is the type of selection data requested.
597 DATA and SIZE describe the data to send, already converted.
598 FORMAT is the unit-size (in bits) of the data to be transmitted. */
599
600 #ifdef TRACE_SELECTION
601 static int x_reply_selection_request_cnt;
602 #endif /* TRACE_SELECTION */
603
604 static void
605 x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type)
606 {
607 XEvent reply_base;
608 XSelectionEvent *reply = &(reply_base.xselection);
609 Display *display = SELECTION_EVENT_DISPLAY (event);
610 Window window = SELECTION_EVENT_REQUESTOR (event);
611 int bytes_remaining;
612 int format_bytes = format/8;
613 int max_bytes = SELECTION_QUANTUM (display);
614 struct x_display_info *dpyinfo = x_display_info_for_display (display);
615 int count = SPECPDL_INDEX ();
616
617 if (max_bytes > MAX_SELECTION_QUANTUM)
618 max_bytes = MAX_SELECTION_QUANTUM;
619
620 reply->type = SelectionNotify;
621 reply->display = display;
622 reply->requestor = window;
623 reply->selection = SELECTION_EVENT_SELECTION (event);
624 reply->time = SELECTION_EVENT_TIME (event);
625 reply->target = SELECTION_EVENT_TARGET (event);
626 reply->property = SELECTION_EVENT_PROPERTY (event);
627 if (reply->property == None)
628 reply->property = reply->target;
629
630 BLOCK_INPUT;
631 /* The protected block contains wait_for_property_change, which can
632 run random lisp code (process handlers) or signal. Therefore, we
633 put the x_uncatch_errors call in an unwind. */
634 record_unwind_protect (x_catch_errors_unwind, Qnil);
635 x_catch_errors (display);
636
637 #ifdef TRACE_SELECTION
638 {
639 char *sel = XGetAtomName (display, reply->selection);
640 char *tgt = XGetAtomName (display, reply->target);
641 TRACE3 ("%s, target %s (%d)", sel, tgt, ++x_reply_selection_request_cnt);
642 if (sel) XFree (sel);
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, &reply_base);
659 }
660 else
661 {
662 /* Send an INCR selection. */
663 struct prop_location *wait_object;
664 int had_errors;
665 Lisp_Object frame;
666
667 frame = some_frame_on_display (dpyinfo);
668
669 /* If the display no longer has frames, we can't expect
670 to get many more selection requests from it, so don't
671 bother trying to queue them. */
672 if (!NILP (frame))
673 {
674 x_start_queuing_selection_requests ();
675
676 record_unwind_protect (queue_selection_requests_unwind,
677 Qnil);
678 }
679
680 if (x_window_to_frame (dpyinfo, window)) /* #### debug */
681 error ("Attempt to transfer an INCR to ourself!");
682
683 TRACE2 ("Start sending %d bytes incrementally (%s)",
684 bytes_remaining, XGetAtomName (display, reply->property));
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, &reply_base);
706 XFlush (display);
707
708 had_errors = x_had_errors_p (display);
709 UNBLOCK_INPUT;
710
711 /* First, wait for the requester to ack by deleting the property.
712 This can run random lisp code (process handlers) or signal. */
713 if (! had_errors)
714 {
715 TRACE1 ("Waiting for ACK (deletion of %s)",
716 XGetAtomName (display, reply->property));
717 wait_for_property_change (wait_object);
718 }
719 else
720 unexpect_property_change (wait_object);
721
722 TRACE0 ("Got ACK");
723 while (bytes_remaining)
724 {
725 int i = ((bytes_remaining < max_bytes)
726 ? bytes_remaining
727 : max_bytes) / format_bytes;
728
729 BLOCK_INPUT;
730
731 wait_object
732 = expect_property_change (display, window, reply->property,
733 PropertyDelete);
734
735 TRACE1 ("Sending increment of %d elements", i);
736 TRACE1 ("Set %s to increment data",
737 XGetAtomName (display, reply->property));
738
739 /* Append the next chunk of data to the property. */
740 XChangeProperty (display, window, reply->property, type, format,
741 PropModeAppend, data, i);
742 bytes_remaining -= i * format_bytes;
743 if (format == 32)
744 data += i * sizeof (long);
745 else
746 data += i * format_bytes;
747 XFlush (display);
748 had_errors = x_had_errors_p (display);
749 UNBLOCK_INPUT;
750
751 if (had_errors)
752 break;
753
754 /* Now wait for the requester to ack this chunk by deleting the
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
761 /* Now write a zero-length chunk to the property to tell the
762 requester that we're done. */
763 BLOCK_INPUT;
764 if (! waiting_for_other_props_on_window (display, window))
765 XSelectInput (display, window, 0L);
766
767 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
768 XGetAtomName (display, reply->property));
769 XChangeProperty (display, window, reply->property, type, format,
770 PropModeReplace, data, 0);
771 TRACE0 ("Done sending incrementally");
772 }
773
774 /* rms, 2003-01-03: I think I have fixed this bug. */
775 /* The window we're communicating with may have been deleted
776 in the meantime (that's a real situation from a bug report).
777 In this case, there may be events in the event queue still
778 refering to the deleted window, and we'll get a BadWindow error
779 in XTread_socket when processing the events. I don't have
780 an idea how to fix that. gerd, 2001-01-98. */
781 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
782 delivered before uncatch errors. */
783 XSync (display, False);
784 UNBLOCK_INPUT;
785
786 /* GTK queues events in addition to the queue in Xlib. So we
787 UNBLOCK to enter the event loop and get possible errors delivered,
788 and then BLOCK again because x_uncatch_errors requires it. */
789 BLOCK_INPUT;
790 /* This calls x_uncatch_errors. */
791 unbind_to (count, Qnil);
792 UNBLOCK_INPUT;
793 }
794 \f
795 /* Handle a SelectionRequest event EVENT.
796 This is called from keyboard.c when such an event is found in the queue. */
797
798 static void
799 x_handle_selection_request (struct input_event *event)
800 {
801 struct gcpro gcpro1, gcpro2, gcpro3;
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;
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
821 GCPRO3 (local_selection_data, converted_selection, target_symbol);
822
823 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
824 SELECTION_EVENT_SELECTION (event));
825
826 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
827
828 if (NILP (local_selection_data))
829 {
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
839 if (SELECTION_EVENT_TIME (event) != CurrentTime
840 && local_selection_time > SELECTION_EVENT_TIME (event))
841 {
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
849 x_selection_current_request = event;
850 count = SPECPDL_INDEX ();
851 selection_request_dpyinfo = dpyinfo;
852 record_unwind_protect (x_selection_request_lisp_error, Qnil);
853
854 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
855 SELECTION_EVENT_TARGET (event));
856
857 #if 0 /* #### MULTIPLE doesn't work yet */
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
864 converted_selection
865 = x_get_local_selection (selection_symbol, target_symbol, 0);
866
867 if (! NILP (converted_selection))
868 {
869 unsigned char *data;
870 unsigned int size;
871 int format;
872 Atom type;
873 int nofree;
874
875 if (CONSP (converted_selection) && NILP (XCDR (converted_selection)))
876 {
877 x_decline_selection_request (event);
878 goto DONE2;
879 }
880
881 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
882 converted_selection,
883 &data, &type, &size, &format, &nofree);
884
885 x_reply_selection_request (event, format, data, size, type);
886 successful_p = Qt;
887
888 /* Indicate we have successfully processed this event. */
889 x_selection_current_request = 0;
890
891 /* Use xfree, not XFree, because lisp_data_to_selection_data
892 calls xmalloc itself. */
893 if (!nofree)
894 xfree (data);
895 }
896
897 DONE2:
898 unbind_to (count, Qnil);
899
900 DONE:
901
902 /* Let random lisp code notice that the selection has been asked for. */
903 {
904 Lisp_Object rest;
905 rest = Vx_sent_selection_functions;
906 if (!EQ (rest, Qunbound))
907 for (; CONSP (rest); rest = Fcdr (rest))
908 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
909 }
910
911 UNGCPRO;
912 }
913 \f
914 /* Handle a SelectionClear event EVENT, which indicates that some
915 client cleared out our previously asserted selection.
916 This is called from keyboard.c when such an event is found in the queue. */
917
918 static void
919 x_handle_selection_clear (struct input_event *event)
920 {
921 Display *display = SELECTION_EVENT_DISPLAY (event);
922 Atom selection = SELECTION_EVENT_SELECTION (event);
923 Time changed_owner_time = SELECTION_EVENT_TIME (event);
924
925 Lisp_Object selection_symbol, local_selection_data;
926 Time local_selection_time;
927 struct x_display_info *dpyinfo = x_display_info_for_display (display);
928 struct x_display_info *t_dpyinfo;
929
930 TRACE0 ("x_handle_selection_clear");
931
932 /* If the new selection owner is also Emacs,
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
951 selection_symbol = x_atom_to_symbol (display, selection);
952
953 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
954
955 /* Well, we already believe that we don't own it, so that's just fine. */
956 if (NILP (local_selection_data)) return;
957
958 local_selection_time = (Time)
959 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
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
965 if (changed_owner_time != CurrentTime
966 && local_selection_time > changed_owner_time)
967 return;
968
969 /* Otherwise, we're really honest and truly being told to drop it.
970 Don't use Fdelq as that may QUIT;. */
971
972 if (EQ (local_selection_data, Fcar (Vselection_alist)))
973 Vselection_alist = Fcdr (Vselection_alist);
974 else
975 {
976 Lisp_Object rest;
977 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
978 if (EQ (local_selection_data, Fcar (XCDR (rest))))
979 {
980 XSETCDR (rest, Fcdr (XCDR (rest)));
981 break;
982 }
983 }
984
985 /* Let random lisp code notice that the selection has been stolen. */
986
987 {
988 Lisp_Object rest;
989 rest = Vx_lost_selection_functions;
990 if (!EQ (rest, Qunbound))
991 {
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 }
998 }
999
1000 void
1001 x_handle_selection_event (struct input_event *event)
1002 {
1003 TRACE0 ("x_handle_selection_event");
1004
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);
1014 }
1015
1016
1017 /* Clear all selections that were made from frame F.
1018 We do this when about to delete a frame. */
1019
1020 void
1021 x_clear_frame_selections (FRAME_PTR f)
1022 {
1023 Lisp_Object frame;
1024 Lisp_Object rest;
1025
1026 XSETFRAME (frame, f);
1027
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. */
1032 while (!NILP (Vselection_alist)
1033 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
1034 {
1035 /* Let random Lisp code notice that the selection has been stolen. */
1036 Lisp_Object hooks, selection_symbol;
1037
1038 hooks = Vx_lost_selection_functions;
1039 selection_symbol = Fcar (Fcar (Vselection_alist));
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
1052 Vselection_alist = Fcdr (Vselection_alist);
1053 }
1054
1055 /* Delete elements after the beginning of Vselection_alist. */
1056 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
1057 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
1058 {
1059 /* Let random Lisp code notice that the selection has been stolen. */
1060 Lisp_Object hooks, selection_symbol;
1061
1062 hooks = Vx_lost_selection_functions;
1063 selection_symbol = Fcar (Fcar (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;
1075 }
1076 }
1077 \f
1078 /* Nonzero if any properties for DISPLAY and WINDOW
1079 are on the list of what we are waiting for. */
1080
1081 static int
1082 waiting_for_other_props_on_window (Display *display, Window window)
1083 {
1084 struct prop_location *rest = property_change_wait_list;
1085 while (rest)
1086 if (rest->display == display && rest->window == window)
1087 return 1;
1088 else
1089 rest = rest->next;
1090 return 0;
1091 }
1092
1093 /* Add an entry to the list of property changes we are waiting for.
1094 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1095 The return value is a number that uniquely identifies
1096 this awaited property change. */
1097
1098 static struct prop_location *
1099 expect_property_change (Display *display, Window window, Atom property, int state)
1100 {
1101 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1102 pl->identifier = ++prop_location_identifier;
1103 pl->display = display;
1104 pl->window = window;
1105 pl->property = property;
1106 pl->desired_state = state;
1107 pl->next = property_change_wait_list;
1108 pl->arrived = 0;
1109 property_change_wait_list = pl;
1110 return pl;
1111 }
1112
1113 /* Delete an entry from the list of property changes we are waiting for.
1114 IDENTIFIER is the number that uniquely identifies the entry. */
1115
1116 static void
1117 unexpect_property_change (struct prop_location *location)
1118 {
1119 struct prop_location *prev = 0, *rest = property_change_wait_list;
1120 while (rest)
1121 {
1122 if (rest == location)
1123 {
1124 if (prev)
1125 prev->next = rest->next;
1126 else
1127 property_change_wait_list = rest->next;
1128 xfree (rest);
1129 return;
1130 }
1131 prev = rest;
1132 rest = rest->next;
1133 }
1134 }
1135
1136 /* Remove the property change expectation element for IDENTIFIER. */
1137
1138 static Lisp_Object
1139 wait_for_property_change_unwind (Lisp_Object loc)
1140 {
1141 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1142
1143 unexpect_property_change (location);
1144 if (location == property_change_reply_object)
1145 property_change_reply_object = 0;
1146 return Qnil;
1147 }
1148
1149 /* Actually wait for a property change.
1150 IDENTIFIER should be the value that expect_property_change returned. */
1151
1152 static void
1153 wait_for_property_change (struct prop_location *location)
1154 {
1155 int secs, usecs;
1156 int count = SPECPDL_INDEX ();
1157
1158 if (property_change_reply_object)
1159 abort ();
1160
1161 /* Make sure to do unexpect_property_change if we quit or err. */
1162 record_unwind_protect (wait_for_property_change_unwind,
1163 make_save_value (location, 0));
1164
1165 XSETCAR (property_change_reply, Qnil);
1166 property_change_reply_object = location;
1167
1168 /* If the event we are waiting for arrives beyond here, it will set
1169 property_change_reply, because property_change_reply_object says so. */
1170 if (! location->arrived)
1171 {
1172 secs = x_selection_timeout / 1000;
1173 usecs = (x_selection_timeout % 1000) * 1000;
1174 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1175 wait_reading_process_output (secs, usecs, 0, 0,
1176 property_change_reply, NULL, 0);
1177
1178 if (NILP (XCAR (property_change_reply)))
1179 {
1180 TRACE0 (" Timed out");
1181 error ("Timed out waiting for property-notify event");
1182 }
1183 }
1184
1185 unbind_to (count, Qnil);
1186 }
1187
1188 /* Called from XTread_socket in response to a PropertyNotify event. */
1189
1190 void
1191 x_handle_property_notify (XPropertyEvent *event)
1192 {
1193 struct prop_location *rest;
1194
1195 for (rest = property_change_wait_list; rest; rest = rest->next)
1196 {
1197 if (!rest->arrived
1198 && rest->property == event->atom
1199 && rest->window == event->window
1200 && rest->display == event->display
1201 && rest->desired_state == event->state)
1202 {
1203 TRACE2 ("Expected %s of property %s",
1204 (event->state == PropertyDelete ? "deletion" : "change"),
1205 XGetAtomName (event->display, event->atom));
1206
1207 rest->arrived = 1;
1208
1209 /* If this is the one wait_for_property_change is waiting for,
1210 tell it to wake up. */
1211 if (rest == property_change_reply_object)
1212 XSETCAR (property_change_reply, Qt);
1213
1214 return;
1215 }
1216 }
1217 }
1218
1219
1220 \f
1221 #if 0 /* #### MULTIPLE doesn't work yet */
1222
1223 static Lisp_Object
1224 fetch_multiple_target (event)
1225 XSelectionRequestEvent *event;
1226 {
1227 Display *display = event->display;
1228 Window window = event->requestor;
1229 Atom target = event->target;
1230 Atom selection_atom = event->selection;
1231 int result;
1232
1233 return
1234 Fcons (QMULTIPLE,
1235 x_get_window_property_as_lisp_data (display, window, target,
1236 QMULTIPLE, selection_atom));
1237 }
1238
1239 static Lisp_Object
1240 copy_multiple_data (obj)
1241 Lisp_Object obj;
1242 {
1243 Lisp_Object vec;
1244 int i;
1245 int size;
1246 if (CONSP (obj))
1247 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1248
1249 CHECK_VECTOR (obj);
1250 vec = Fmake_vector (size = ASIZE (obj), Qnil);
1251 for (i = 0; i < size; i++)
1252 {
1253 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1254 CHECK_VECTOR (vec2);
1255 if (ASIZE (vec2) != 2)
1256 /* ??? Confusing error message */
1257 signal_error ("Vectors must be of length 2", vec2);
1258 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1259 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1260 = XVECTOR (vec2)->contents [0];
1261 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1262 = XVECTOR (vec2)->contents [1];
1263 }
1264 return vec;
1265 }
1266
1267 #endif
1268
1269 \f
1270 /* Variables for communication with x_handle_selection_notify. */
1271 static Atom reading_which_selection;
1272 static Lisp_Object reading_selection_reply;
1273 static Window reading_selection_window;
1274
1275 /* Do protocol to read selection-data from the server.
1276 Converts this to Lisp data and returns it. */
1277
1278 static Lisp_Object
1279 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
1280 {
1281 struct frame *sf = SELECTED_FRAME ();
1282 Window requestor_window;
1283 Display *display;
1284 struct x_display_info *dpyinfo;
1285 Time requestor_time = last_event_timestamp;
1286 Atom target_property;
1287 Atom selection_atom;
1288 Atom type_atom;
1289 int secs, usecs;
1290 int count = SPECPDL_INDEX ();
1291 Lisp_Object frame;
1292
1293 if (! FRAME_X_P (sf))
1294 return Qnil;
1295
1296 requestor_window = FRAME_X_WINDOW (sf);
1297 display = FRAME_X_DISPLAY (sf);
1298 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1299 target_property = dpyinfo->Xatom_EMACS_TMP;
1300 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1301
1302 if (CONSP (target_type))
1303 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1304 else
1305 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1306
1307 if (! NILP (time_stamp))
1308 {
1309 if (CONSP (time_stamp))
1310 requestor_time = (Time) cons_to_long (time_stamp);
1311 else if (INTEGERP (time_stamp))
1312 requestor_time = (Time) XUINT (time_stamp);
1313 else if (FLOATP (time_stamp))
1314 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1315 else
1316 error ("TIME_STAMP must be cons or number");
1317 }
1318
1319 BLOCK_INPUT;
1320
1321 /* The protected block contains wait_reading_process_output, which
1322 can run random lisp code (process handlers) or signal.
1323 Therefore, we put the x_uncatch_errors call in an unwind. */
1324 record_unwind_protect (x_catch_errors_unwind, Qnil);
1325 x_catch_errors (display);
1326
1327 TRACE2 ("Get selection %s, type %s",
1328 XGetAtomName (display, type_atom),
1329 XGetAtomName (display, target_property));
1330
1331 XConvertSelection (display, selection_atom, type_atom, target_property,
1332 requestor_window, requestor_time);
1333 XFlush (display);
1334
1335 /* Prepare to block until the reply has been read. */
1336 reading_selection_window = requestor_window;
1337 reading_which_selection = selection_atom;
1338 XSETCAR (reading_selection_reply, Qnil);
1339
1340 frame = some_frame_on_display (dpyinfo);
1341
1342 /* If the display no longer has frames, we can't expect
1343 to get many more selection requests from it, so don't
1344 bother trying to queue them. */
1345 if (!NILP (frame))
1346 {
1347 x_start_queuing_selection_requests ();
1348
1349 record_unwind_protect (queue_selection_requests_unwind,
1350 Qnil);
1351 }
1352 UNBLOCK_INPUT;
1353
1354 /* This allows quits. Also, don't wait forever. */
1355 secs = x_selection_timeout / 1000;
1356 usecs = (x_selection_timeout % 1000) * 1000;
1357 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1358 wait_reading_process_output (secs, usecs, 0, 0,
1359 reading_selection_reply, NULL, 0);
1360 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1361
1362 BLOCK_INPUT;
1363 if (x_had_errors_p (display))
1364 error ("Cannot get selection");
1365 /* This calls x_uncatch_errors. */
1366 unbind_to (count, Qnil);
1367 UNBLOCK_INPUT;
1368
1369 if (NILP (XCAR (reading_selection_reply)))
1370 error ("Timed out waiting for reply from selection owner");
1371 if (EQ (XCAR (reading_selection_reply), Qlambda))
1372 return Qnil;
1373
1374 /* Otherwise, the selection is waiting for us on the requested property. */
1375 return
1376 x_get_window_property_as_lisp_data (display, requestor_window,
1377 target_property, target_type,
1378 selection_atom);
1379 }
1380 \f
1381 /* Subroutines of x_get_window_property_as_lisp_data */
1382
1383 /* Use xfree, not XFree, to free the data obtained with this function. */
1384
1385 static void
1386 x_get_window_property (Display *display, Window window, Atom property,
1387 unsigned char **data_ret, int *bytes_ret,
1388 Atom *actual_type_ret, int *actual_format_ret,
1389 unsigned long *actual_size_ret, int delete_p)
1390 {
1391 int total_size;
1392 unsigned long bytes_remaining;
1393 int offset = 0;
1394 unsigned char *tmp_data = 0;
1395 int result;
1396 int buffer_size = SELECTION_QUANTUM (display);
1397
1398 if (buffer_size > MAX_SELECTION_QUANTUM)
1399 buffer_size = MAX_SELECTION_QUANTUM;
1400
1401 BLOCK_INPUT;
1402
1403 /* First probe the thing to find out how big it is. */
1404 result = XGetWindowProperty (display, window, property,
1405 0L, 0L, False, AnyPropertyType,
1406 actual_type_ret, actual_format_ret,
1407 actual_size_ret,
1408 &bytes_remaining, &tmp_data);
1409 if (result != Success)
1410 {
1411 UNBLOCK_INPUT;
1412 *data_ret = 0;
1413 *bytes_ret = 0;
1414 return;
1415 }
1416
1417 /* This was allocated by Xlib, so use XFree. */
1418 XFree ((char *) tmp_data);
1419
1420 if (*actual_type_ret == None || *actual_format_ret == 0)
1421 {
1422 UNBLOCK_INPUT;
1423 return;
1424 }
1425
1426 total_size = bytes_remaining + 1;
1427 *data_ret = (unsigned char *) xmalloc (total_size);
1428
1429 /* Now read, until we've gotten it all. */
1430 while (bytes_remaining)
1431 {
1432 #ifdef TRACE_SELECTION
1433 unsigned long last = bytes_remaining;
1434 #endif
1435 result
1436 = XGetWindowProperty (display, window, property,
1437 (long)offset/4, (long)buffer_size/4,
1438 False,
1439 AnyPropertyType,
1440 actual_type_ret, actual_format_ret,
1441 actual_size_ret, &bytes_remaining, &tmp_data);
1442
1443 TRACE2 ("Read %lu bytes from property %s",
1444 last - bytes_remaining,
1445 XGetAtomName (display, property));
1446
1447 /* If this doesn't return Success at this point, it means that
1448 some clod deleted the selection while we were in the midst of
1449 reading it. Deal with that, I guess.... */
1450 if (result != Success)
1451 break;
1452
1453 /* The man page for XGetWindowProperty says:
1454 "If the returned format is 32, the returned data is represented
1455 as a long array and should be cast to that type to obtain the
1456 elements."
1457 This applies even if long is more than 32 bits, the X library
1458 converts from 32 bit elements received from the X server to long
1459 and passes the long array to us. Thus, for that case memcpy can not
1460 be used. We convert to a 32 bit type here, because so much code
1461 assume on that.
1462
1463 The bytes and offsets passed to XGetWindowProperty refers to the
1464 property and those are indeed in 32 bit quantities if format is 32. */
1465
1466 if (32 < BITS_PER_LONG && *actual_format_ret == 32)
1467 {
1468 unsigned long i;
1469 int *idata = (int *) ((*data_ret) + offset);
1470 long *ldata = (long *) tmp_data;
1471
1472 for (i = 0; i < *actual_size_ret; ++i)
1473 {
1474 idata[i]= (int) ldata[i];
1475 offset += 4;
1476 }
1477 }
1478 else
1479 {
1480 *actual_size_ret *= *actual_format_ret / 8;
1481 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1482 offset += *actual_size_ret;
1483 }
1484
1485 /* This was allocated by Xlib, so use XFree. */
1486 XFree ((char *) tmp_data);
1487 }
1488
1489 XFlush (display);
1490 UNBLOCK_INPUT;
1491 *bytes_ret = offset;
1492 }
1493 \f
1494 /* Use xfree, not XFree, to free the data obtained with this function. */
1495
1496 static void
1497 receive_incremental_selection (Display *display, Window window, Atom property,
1498 Lisp_Object target_type,
1499 unsigned int min_size_bytes,
1500 unsigned char **data_ret, int *size_bytes_ret,
1501 Atom *type_ret, int *format_ret,
1502 unsigned long *size_ret)
1503 {
1504 int offset = 0;
1505 struct prop_location *wait_object;
1506 *size_bytes_ret = min_size_bytes;
1507 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1508
1509 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1510
1511 /* At this point, we have read an INCR property.
1512 Delete the property to ack it.
1513 (But first, prepare to receive the next event in this handshake.)
1514
1515 Now, we must loop, waiting for the sending window to put a value on
1516 that property, then reading the property, then deleting it to ack.
1517 We are done when the sender places a property of length 0.
1518 */
1519 BLOCK_INPUT;
1520 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1521 TRACE1 (" Delete property %s",
1522 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1523 XDeleteProperty (display, window, property);
1524 TRACE1 (" Expect new value of property %s",
1525 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1526 wait_object = expect_property_change (display, window, property,
1527 PropertyNewValue);
1528 XFlush (display);
1529 UNBLOCK_INPUT;
1530
1531 while (1)
1532 {
1533 unsigned char *tmp_data;
1534 int tmp_size_bytes;
1535
1536 TRACE0 (" Wait for property change");
1537 wait_for_property_change (wait_object);
1538
1539 /* expect it again immediately, because x_get_window_property may
1540 .. no it won't, I don't get it.
1541 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1542 TRACE0 (" Get property value");
1543 x_get_window_property (display, window, property,
1544 &tmp_data, &tmp_size_bytes,
1545 type_ret, format_ret, size_ret, 1);
1546
1547 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1548
1549 if (tmp_size_bytes == 0) /* we're done */
1550 {
1551 TRACE0 ("Done reading incrementally");
1552
1553 if (! waiting_for_other_props_on_window (display, window))
1554 XSelectInput (display, window, STANDARD_EVENT_SET);
1555 /* Use xfree, not XFree, because x_get_window_property
1556 calls xmalloc itself. */
1557 xfree (tmp_data);
1558 break;
1559 }
1560
1561 BLOCK_INPUT;
1562 TRACE1 (" ACK by deleting property %s",
1563 XGetAtomName (display, property));
1564 XDeleteProperty (display, window, property);
1565 wait_object = expect_property_change (display, window, property,
1566 PropertyNewValue);
1567 XFlush (display);
1568 UNBLOCK_INPUT;
1569
1570 if (*size_bytes_ret < offset + tmp_size_bytes)
1571 {
1572 *size_bytes_ret = offset + tmp_size_bytes;
1573 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1574 }
1575
1576 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1577 offset += tmp_size_bytes;
1578
1579 /* Use xfree, not XFree, because x_get_window_property
1580 calls xmalloc itself. */
1581 xfree (tmp_data);
1582 }
1583 }
1584
1585 \f
1586 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1587 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1588 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1589
1590 static Lisp_Object
1591 x_get_window_property_as_lisp_data (Display *display, Window window,
1592 Atom property,
1593 Lisp_Object target_type,
1594 Atom selection_atom)
1595 {
1596 Atom actual_type;
1597 int actual_format;
1598 unsigned long actual_size;
1599 unsigned char *data = 0;
1600 int bytes = 0;
1601 Lisp_Object val;
1602 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1603
1604 TRACE0 ("Reading selection data");
1605
1606 x_get_window_property (display, window, property, &data, &bytes,
1607 &actual_type, &actual_format, &actual_size, 1);
1608 if (! data)
1609 {
1610 int there_is_a_selection_owner;
1611 BLOCK_INPUT;
1612 there_is_a_selection_owner
1613 = XGetSelectionOwner (display, selection_atom);
1614 UNBLOCK_INPUT;
1615 if (there_is_a_selection_owner)
1616 signal_error ("Selection owner couldn't convert",
1617 actual_type
1618 ? list2 (target_type,
1619 x_atom_to_symbol (display, actual_type))
1620 : target_type);
1621 else
1622 signal_error ("No selection",
1623 x_atom_to_symbol (display, selection_atom));
1624 }
1625
1626 if (actual_type == dpyinfo->Xatom_INCR)
1627 {
1628 /* That wasn't really the data, just the beginning. */
1629
1630 unsigned int min_size_bytes = * ((unsigned int *) data);
1631 BLOCK_INPUT;
1632 /* Use xfree, not XFree, because x_get_window_property
1633 calls xmalloc itself. */
1634 xfree ((char *) data);
1635 UNBLOCK_INPUT;
1636 receive_incremental_selection (display, window, property, target_type,
1637 min_size_bytes, &data, &bytes,
1638 &actual_type, &actual_format,
1639 &actual_size);
1640 }
1641
1642 BLOCK_INPUT;
1643 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1644 XDeleteProperty (display, window, property);
1645 XFlush (display);
1646 UNBLOCK_INPUT;
1647
1648 /* It's been read. Now convert it to a lisp object in some semi-rational
1649 manner. */
1650 val = selection_data_to_lisp_data (display, data, bytes,
1651 actual_type, actual_format);
1652
1653 /* Use xfree, not XFree, because x_get_window_property
1654 calls xmalloc itself. */
1655 xfree ((char *) data);
1656 return val;
1657 }
1658 \f
1659 /* These functions convert from the selection data read from the server into
1660 something that we can use from Lisp, and vice versa.
1661
1662 Type: Format: Size: Lisp Type:
1663 ----- ------- ----- -----------
1664 * 8 * String
1665 ATOM 32 1 Symbol
1666 ATOM 32 > 1 Vector of Symbols
1667 * 16 1 Integer
1668 * 16 > 1 Vector of Integers
1669 * 32 1 if <=16 bits: Integer
1670 if > 16 bits: Cons of top16, bot16
1671 * 32 > 1 Vector of the above
1672
1673 When converting a Lisp number to C, it is assumed to be of format 16 if
1674 it is an integer, and of format 32 if it is a cons of two integers.
1675
1676 When converting a vector of numbers from Lisp to C, it is assumed to be
1677 of format 16 if every element in the vector is an integer, and is assumed
1678 to be of format 32 if any element is a cons of two integers.
1679
1680 When converting an object to C, it may be of the form (SYMBOL . <data>)
1681 where SYMBOL is what we should claim that the type is. Format and
1682 representation are as above.
1683
1684 Important: When format is 32, data should contain an array of int,
1685 not an array of long as the X library returns. This makes a difference
1686 when sizeof(long) != sizeof(int). */
1687
1688
1689
1690 static Lisp_Object
1691 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1692 int size, Atom type, int format)
1693 {
1694 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1695
1696 if (type == dpyinfo->Xatom_NULL)
1697 return QNULL;
1698
1699 /* Convert any 8-bit data to a string, for compactness. */
1700 else if (format == 8)
1701 {
1702 Lisp_Object str, lispy_type;
1703
1704 str = make_unibyte_string ((char *) data, size);
1705 /* Indicate that this string is from foreign selection by a text
1706 property `foreign-selection' so that the caller of
1707 x-get-selection-internal (usually x-get-selection) can know
1708 that the string must be decode. */
1709 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1710 lispy_type = QCOMPOUND_TEXT;
1711 else if (type == dpyinfo->Xatom_UTF8_STRING)
1712 lispy_type = QUTF8_STRING;
1713 else
1714 lispy_type = QSTRING;
1715 Fput_text_property (make_number (0), make_number (size),
1716 Qforeign_selection, lispy_type, str);
1717 return str;
1718 }
1719 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1720 a vector of symbols.
1721 */
1722 else if (type == XA_ATOM)
1723 {
1724 int i;
1725 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1726 But the callers of these function has made sure the data for
1727 format == 32 is an array of int. Thus, use int instead
1728 of Atom. */
1729 int *idata = (int *) data;
1730
1731 if (size == sizeof (int))
1732 return x_atom_to_symbol (display, (Atom) idata[0]);
1733 else
1734 {
1735 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1736 make_number (0));
1737 for (i = 0; i < size / sizeof (int); i++)
1738 Faset (v, make_number (i),
1739 x_atom_to_symbol (display, (Atom) idata[i]));
1740 return v;
1741 }
1742 }
1743
1744 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1745 If the number is 32 bits and won't fit in a Lisp_Int,
1746 convert it to a cons of integers, 16 bits in each half.
1747 */
1748 else if (format == 32 && size == sizeof (int))
1749 return long_to_cons (((unsigned int *) data) [0]);
1750 else if (format == 16 && size == sizeof (short))
1751 return make_number ((int) (((unsigned short *) data) [0]));
1752
1753 /* Convert any other kind of data to a vector of numbers, represented
1754 as above (as an integer, or a cons of two 16 bit integers.)
1755 */
1756 else if (format == 16)
1757 {
1758 int i;
1759 Lisp_Object v;
1760 v = Fmake_vector (make_number (size / 2), make_number (0));
1761 for (i = 0; i < size / 2; i++)
1762 {
1763 int j = (int) ((unsigned short *) data) [i];
1764 Faset (v, make_number (i), make_number (j));
1765 }
1766 return v;
1767 }
1768 else
1769 {
1770 int i;
1771 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1772 for (i = 0; i < size / 4; i++)
1773 {
1774 unsigned int j = ((unsigned int *) data) [i];
1775 Faset (v, make_number (i), long_to_cons (j));
1776 }
1777 return v;
1778 }
1779 }
1780
1781
1782 /* Use xfree, not XFree, to free the data obtained with this function. */
1783
1784 static void
1785 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1786 unsigned char **data_ret, Atom *type_ret,
1787 unsigned int *size_ret,
1788 int *format_ret, int *nofree_ret)
1789 {
1790 Lisp_Object type = Qnil;
1791 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1792
1793 *nofree_ret = 0;
1794
1795 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1796 {
1797 type = XCAR (obj);
1798 obj = XCDR (obj);
1799 if (CONSP (obj) && NILP (XCDR (obj)))
1800 obj = XCAR (obj);
1801 }
1802
1803 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1804 { /* This is not the same as declining */
1805 *format_ret = 32;
1806 *size_ret = 0;
1807 *data_ret = 0;
1808 type = QNULL;
1809 }
1810 else if (STRINGP (obj))
1811 {
1812 if (SCHARS (obj) < SBYTES (obj))
1813 /* OBJ is a multibyte string containing a non-ASCII char. */
1814 signal_error ("Non-ASCII string must be encoded in advance", obj);
1815 if (NILP (type))
1816 type = QSTRING;
1817 *format_ret = 8;
1818 *size_ret = SBYTES (obj);
1819 *data_ret = SDATA (obj);
1820 *nofree_ret = 1;
1821 }
1822 else if (SYMBOLP (obj))
1823 {
1824 *format_ret = 32;
1825 *size_ret = 1;
1826 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1827 (*data_ret) [sizeof (Atom)] = 0;
1828 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
1829 if (NILP (type)) type = QATOM;
1830 }
1831 else if (INTEGERP (obj)
1832 && XINT (obj) < 0xFFFF
1833 && XINT (obj) > -0xFFFF)
1834 {
1835 *format_ret = 16;
1836 *size_ret = 1;
1837 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1838 (*data_ret) [sizeof (short)] = 0;
1839 (*(short **) data_ret) [0] = (short) XINT (obj);
1840 if (NILP (type)) type = QINTEGER;
1841 }
1842 else if (INTEGERP (obj)
1843 || (CONSP (obj) && INTEGERP (XCAR (obj))
1844 && (INTEGERP (XCDR (obj))
1845 || (CONSP (XCDR (obj))
1846 && INTEGERP (XCAR (XCDR (obj)))))))
1847 {
1848 *format_ret = 32;
1849 *size_ret = 1;
1850 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1851 (*data_ret) [sizeof (long)] = 0;
1852 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1853 if (NILP (type)) type = QINTEGER;
1854 }
1855 else if (VECTORP (obj))
1856 {
1857 /* Lisp_Vectors may represent a set of ATOMs;
1858 a set of 16 or 32 bit INTEGERs;
1859 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1860 */
1861 int i;
1862
1863 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1864 /* This vector is an ATOM set */
1865 {
1866 if (NILP (type)) type = QATOM;
1867 *size_ret = ASIZE (obj);
1868 *format_ret = 32;
1869 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1870 for (i = 0; i < *size_ret; i++)
1871 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1872 (*(Atom **) data_ret) [i]
1873 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
1874 else
1875 signal_error ("All elements of selection vector must have same type", obj);
1876 }
1877 #if 0 /* #### MULTIPLE doesn't work yet */
1878 else if (VECTORP (XVECTOR (obj)->contents [0]))
1879 /* This vector is an ATOM_PAIR set */
1880 {
1881 if (NILP (type)) type = QATOM_PAIR;
1882 *size_ret = ASIZE (obj);
1883 *format_ret = 32;
1884 *data_ret = (unsigned char *)
1885 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1886 for (i = 0; i < *size_ret; i++)
1887 if (VECTORP (XVECTOR (obj)->contents [i]))
1888 {
1889 Lisp_Object pair = XVECTOR (obj)->contents [i];
1890 if (ASIZE (pair) != 2)
1891 signal_error (
1892 "Elements of the vector must be vectors of exactly two elements",
1893 pair);
1894
1895 (*(Atom **) data_ret) [i * 2]
1896 = symbol_to_x_atom (dpyinfo, display,
1897 XVECTOR (pair)->contents [0]);
1898 (*(Atom **) data_ret) [(i * 2) + 1]
1899 = symbol_to_x_atom (dpyinfo, display,
1900 XVECTOR (pair)->contents [1]);
1901 }
1902 else
1903 signal_error ("All elements of the vector must be of the same type",
1904 obj);
1905
1906 }
1907 #endif
1908 else
1909 /* This vector is an INTEGER set, or something like it */
1910 {
1911 int data_size = 2;
1912 *size_ret = ASIZE (obj);
1913 if (NILP (type)) type = QINTEGER;
1914 *format_ret = 16;
1915 for (i = 0; i < *size_ret; i++)
1916 if (CONSP (XVECTOR (obj)->contents [i]))
1917 *format_ret = 32;
1918 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1919 signal_error (/* Qselection_error */
1920 "Elements of selection vector must be integers or conses of integers",
1921 obj);
1922
1923 /* Use sizeof(long) even if it is more than 32 bits. See comment
1924 in x_get_window_property and x_fill_property_data. */
1925
1926 if (*format_ret == 32) data_size = sizeof(long);
1927 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1928 for (i = 0; i < *size_ret; i++)
1929 if (*format_ret == 32)
1930 (*((unsigned long **) data_ret)) [i]
1931 = cons_to_long (XVECTOR (obj)->contents [i]);
1932 else
1933 (*((unsigned short **) data_ret)) [i]
1934 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1935 }
1936 }
1937 else
1938 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1939
1940 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
1941 }
1942
1943 static Lisp_Object
1944 clean_local_selection_data (Lisp_Object obj)
1945 {
1946 if (CONSP (obj)
1947 && INTEGERP (XCAR (obj))
1948 && CONSP (XCDR (obj))
1949 && INTEGERP (XCAR (XCDR (obj)))
1950 && NILP (XCDR (XCDR (obj))))
1951 obj = Fcons (XCAR (obj), XCDR (obj));
1952
1953 if (CONSP (obj)
1954 && INTEGERP (XCAR (obj))
1955 && INTEGERP (XCDR (obj)))
1956 {
1957 if (XINT (XCAR (obj)) == 0)
1958 return XCDR (obj);
1959 if (XINT (XCAR (obj)) == -1)
1960 return make_number (- XINT (XCDR (obj)));
1961 }
1962 if (VECTORP (obj))
1963 {
1964 int i;
1965 int size = ASIZE (obj);
1966 Lisp_Object copy;
1967 if (size == 1)
1968 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1969 copy = Fmake_vector (make_number (size), Qnil);
1970 for (i = 0; i < size; i++)
1971 XVECTOR (copy)->contents [i]
1972 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1973 return copy;
1974 }
1975 return obj;
1976 }
1977 \f
1978 /* Called from XTread_socket to handle SelectionNotify events.
1979 If it's the selection we are waiting for, stop waiting
1980 by setting the car of reading_selection_reply to non-nil.
1981 We store t there if the reply is successful, lambda if not. */
1982
1983 void
1984 x_handle_selection_notify (XSelectionEvent *event)
1985 {
1986 if (event->requestor != reading_selection_window)
1987 return;
1988 if (event->selection != reading_which_selection)
1989 return;
1990
1991 TRACE0 ("Received SelectionNotify");
1992 XSETCAR (reading_selection_reply,
1993 (event->property != 0 ? Qt : Qlambda));
1994 }
1995
1996 \f
1997 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1998 Sx_own_selection_internal, 2, 2, 0,
1999 doc: /* Assert an X selection of the given TYPE with the given VALUE.
2000 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2001 \(Those are literal upper-case symbol names, since that's what X expects.)
2002 VALUE is typically a string, or a cons of two markers, but may be
2003 anything that the functions on `selection-converter-alist' know about. */)
2004 (Lisp_Object selection_name, Lisp_Object selection_value)
2005 {
2006 check_x ();
2007 CHECK_SYMBOL (selection_name);
2008 if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil");
2009 x_own_selection (selection_name, selection_value);
2010 return selection_value;
2011 }
2012
2013
2014 /* Request the selection value from the owner. If we are the owner,
2015 simply return our selection value. If we are not the owner, this
2016 will block until all of the data has arrived. */
2017
2018 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
2019 Sx_get_selection_internal, 2, 3, 0,
2020 doc: /* Return text selected from some X window.
2021 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2022 \(Those are literal upper-case symbol names, since that's what X expects.)
2023 TYPE is the type of data desired, typically `STRING'.
2024 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2025 selections. If omitted, defaults to the time for the last event. */)
2026 (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
2027 {
2028 Lisp_Object val = Qnil;
2029 struct gcpro gcpro1, gcpro2;
2030 GCPRO2 (target_type, val); /* we store newly consed data into these */
2031 check_x ();
2032 CHECK_SYMBOL (selection_symbol);
2033
2034 #if 0 /* #### MULTIPLE doesn't work yet */
2035 if (CONSP (target_type)
2036 && XCAR (target_type) == QMULTIPLE)
2037 {
2038 CHECK_VECTOR (XCDR (target_type));
2039 /* So we don't destructively modify this... */
2040 target_type = copy_multiple_data (target_type);
2041 }
2042 else
2043 #endif
2044 CHECK_SYMBOL (target_type);
2045
2046 val = x_get_local_selection (selection_symbol, target_type, 1);
2047
2048 if (NILP (val))
2049 {
2050 val = x_get_foreign_selection (selection_symbol, target_type, time_stamp);
2051 goto DONE;
2052 }
2053
2054 if (CONSP (val)
2055 && SYMBOLP (XCAR (val)))
2056 {
2057 val = XCDR (val);
2058 if (CONSP (val) && NILP (XCDR (val)))
2059 val = XCAR (val);
2060 }
2061 val = clean_local_selection_data (val);
2062 DONE:
2063 UNGCPRO;
2064 return val;
2065 }
2066
2067 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2068 Sx_disown_selection_internal, 1, 2, 0,
2069 doc: /* If we own the selection SELECTION, disown it.
2070 Disowning it means there is no such selection. */)
2071 (Lisp_Object selection, Lisp_Object time_object)
2072 {
2073 Time timestamp;
2074 Atom selection_atom;
2075 union {
2076 struct selection_input_event sie;
2077 struct input_event ie;
2078 } event;
2079 Display *display;
2080 struct x_display_info *dpyinfo;
2081 struct frame *sf = SELECTED_FRAME ();
2082
2083 check_x ();
2084 if (! FRAME_X_P (sf))
2085 return Qnil;
2086
2087 display = FRAME_X_DISPLAY (sf);
2088 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2089 CHECK_SYMBOL (selection);
2090 if (NILP (time_object))
2091 timestamp = last_event_timestamp;
2092 else
2093 timestamp = cons_to_long (time_object);
2094
2095 if (NILP (assq_no_quit (selection, Vselection_alist)))
2096 return Qnil; /* Don't disown the selection when we're not the owner. */
2097
2098 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
2099
2100 BLOCK_INPUT;
2101 XSetSelectionOwner (display, selection_atom, None, timestamp);
2102 UNBLOCK_INPUT;
2103
2104 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2105 generated for a window which owns the selection when that window sets
2106 the selection owner to None. The NCD server does, the MIT Sun4 server
2107 doesn't. So we synthesize one; this means we might get two, but
2108 that's ok, because the second one won't have any effect. */
2109 SELECTION_EVENT_DISPLAY (&event.sie) = display;
2110 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2111 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2112 x_handle_selection_clear (&event.ie);
2113
2114 return Qt;
2115 }
2116
2117 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2118 0, 1, 0,
2119 doc: /* Whether the current Emacs process owns the given X Selection.
2120 The arg should be the name of the selection in question, typically one of
2121 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2122 \(Those are literal upper-case symbol names, since that's what X expects.)
2123 For convenience, the symbol nil is the same as `PRIMARY',
2124 and t is the same as `SECONDARY'. */)
2125 (Lisp_Object selection)
2126 {
2127 check_x ();
2128 CHECK_SYMBOL (selection);
2129 if (EQ (selection, Qnil)) selection = QPRIMARY;
2130 if (EQ (selection, Qt)) selection = QSECONDARY;
2131
2132 if (NILP (Fassq (selection, Vselection_alist)))
2133 return Qnil;
2134 return Qt;
2135 }
2136
2137 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2138 0, 1, 0,
2139 doc: /* Whether there is an owner for the given X Selection.
2140 The arg should be the name of the selection in question, typically one of
2141 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2142 \(Those are literal upper-case symbol names, since that's what X expects.)
2143 For convenience, the symbol nil is the same as `PRIMARY',
2144 and t is the same as `SECONDARY'. */)
2145 (Lisp_Object selection)
2146 {
2147 Window owner;
2148 Atom atom;
2149 Display *dpy;
2150 struct frame *sf = SELECTED_FRAME ();
2151
2152 /* It should be safe to call this before we have an X frame. */
2153 if (! FRAME_X_P (sf))
2154 return Qnil;
2155
2156 dpy = FRAME_X_DISPLAY (sf);
2157 CHECK_SYMBOL (selection);
2158 if (!NILP (Fx_selection_owner_p (selection)))
2159 return Qt;
2160 if (EQ (selection, Qnil)) selection = QPRIMARY;
2161 if (EQ (selection, Qt)) selection = QSECONDARY;
2162 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
2163 if (atom == 0)
2164 return Qnil;
2165 BLOCK_INPUT;
2166 owner = XGetSelectionOwner (dpy, atom);
2167 UNBLOCK_INPUT;
2168 return (owner ? Qt : Qnil);
2169 }
2170
2171 \f
2172 /***********************************************************************
2173 Drag and drop support
2174 ***********************************************************************/
2175 /* Check that lisp values are of correct type for x_fill_property_data.
2176 That is, number, string or a cons with two numbers (low and high 16
2177 bit parts of a 32 bit number). Return the number of items in DATA,
2178 or -1 if there is an error. */
2179
2180 int
2181 x_check_property_data (Lisp_Object data)
2182 {
2183 Lisp_Object iter;
2184 int size = 0;
2185
2186 for (iter = data; CONSP (iter); iter = XCDR (iter))
2187 {
2188 Lisp_Object o = XCAR (iter);
2189
2190 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2191 return -1;
2192 else if (CONSP (o) &&
2193 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2194 return -1;
2195 size++;
2196 }
2197
2198 return size;
2199 }
2200
2201 /* Convert lisp values to a C array. Values may be a number, a string
2202 which is taken as an X atom name and converted to the atom value, or
2203 a cons containing the two 16 bit parts of a 32 bit number.
2204
2205 DPY is the display use to look up X atoms.
2206 DATA is a Lisp list of values to be converted.
2207 RET is the C array that contains the converted values. It is assumed
2208 it is big enough to hold all values.
2209 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2210 be stored in RET. Note that long is used for 32 even if long is more
2211 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2212 XClientMessageEvent). */
2213
2214 void
2215 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2216 {
2217 long val;
2218 long *d32 = (long *) ret;
2219 short *d16 = (short *) ret;
2220 char *d08 = (char *) ret;
2221 Lisp_Object iter;
2222
2223 for (iter = data; CONSP (iter); iter = XCDR (iter))
2224 {
2225 Lisp_Object o = XCAR (iter);
2226
2227 if (INTEGERP (o))
2228 val = (long) XFASTINT (o);
2229 else if (FLOATP (o))
2230 val = (long) XFLOAT_DATA (o);
2231 else if (CONSP (o))
2232 val = (long) cons_to_long (o);
2233 else if (STRINGP (o))
2234 {
2235 BLOCK_INPUT;
2236 val = (long) XInternAtom (dpy, SSDATA (o), False);
2237 UNBLOCK_INPUT;
2238 }
2239 else
2240 error ("Wrong type, must be string, number or cons");
2241
2242 if (format == 8)
2243 *d08++ = (char) val;
2244 else if (format == 16)
2245 *d16++ = (short) val;
2246 else
2247 *d32++ = val;
2248 }
2249 }
2250
2251 /* Convert an array of C values to a Lisp list.
2252 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2253 DATA is a C array of values to be converted.
2254 TYPE is the type of the data. Only XA_ATOM is special, it converts
2255 each number in DATA to its corresponfing X atom as a symbol.
2256 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2257 be stored in RET.
2258 SIZE is the number of elements in DATA.
2259
2260 Important: When format is 32, data should contain an array of int,
2261 not an array of long as the X library returns. This makes a difference
2262 when sizeof(long) != sizeof(int).
2263
2264 Also see comment for selection_data_to_lisp_data above. */
2265
2266 Lisp_Object
2267 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2268 Atom type, int format, long unsigned int size)
2269 {
2270 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2271 data, size*format/8, type, format);
2272 }
2273
2274 /* Get the mouse position in frame relative coordinates. */
2275
2276 static void
2277 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2278 {
2279 Window root, dummy_window;
2280 int dummy;
2281
2282 BLOCK_INPUT;
2283
2284 XQueryPointer (FRAME_X_DISPLAY (f),
2285 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2286
2287 /* The root window which contains the pointer. */
2288 &root,
2289
2290 /* Window pointer is on, not used */
2291 &dummy_window,
2292
2293 /* The position on that root window. */
2294 x, y,
2295
2296 /* x/y in dummy_window coordinates, not used. */
2297 &dummy, &dummy,
2298
2299 /* Modifier keys and pointer buttons, about which
2300 we don't care. */
2301 (unsigned int *) &dummy);
2302
2303
2304 /* Absolute to relative. */
2305 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2306 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2307
2308 UNBLOCK_INPUT;
2309 }
2310
2311 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2312 Sx_get_atom_name, 1, 2, 0,
2313 doc: /* Return the X atom name for VALUE as a string.
2314 VALUE may be a number or a cons where the car is the upper 16 bits and
2315 the cdr is the lower 16 bits of a 32 bit value.
2316 Use the display for FRAME or the current frame if FRAME is not given or nil.
2317
2318 If the value is 0 or the atom is not known, return the empty string. */)
2319 (Lisp_Object value, Lisp_Object frame)
2320 {
2321 struct frame *f = check_x_frame (frame);
2322 char *name = 0;
2323 char empty[] = "";
2324 Lisp_Object ret = Qnil;
2325 Display *dpy = FRAME_X_DISPLAY (f);
2326 Atom atom;
2327 int had_errors;
2328
2329 if (INTEGERP (value))
2330 atom = (Atom) XUINT (value);
2331 else if (FLOATP (value))
2332 atom = (Atom) XFLOAT_DATA (value);
2333 else if (CONSP (value))
2334 atom = (Atom) cons_to_long (value);
2335 else
2336 error ("Wrong type, value must be number or cons");
2337
2338 BLOCK_INPUT;
2339 x_catch_errors (dpy);
2340 name = atom ? XGetAtomName (dpy, atom) : empty;
2341 had_errors = x_had_errors_p (dpy);
2342 x_uncatch_errors ();
2343
2344 if (!had_errors)
2345 ret = make_string (name, strlen (name));
2346
2347 if (atom && name) XFree (name);
2348 if (NILP (ret)) ret = empty_unibyte_string;
2349
2350 UNBLOCK_INPUT;
2351
2352 return ret;
2353 }
2354
2355 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2356 Sx_register_dnd_atom, 1, 2, 0,
2357 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2358 ATOM can be a symbol or a string. The ATOM is interned on the display that
2359 FRAME is on. If FRAME is nil, the selected frame is used. */)
2360 (Lisp_Object atom, Lisp_Object frame)
2361 {
2362 Atom x_atom;
2363 struct frame *f = check_x_frame (frame);
2364 size_t i;
2365 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2366
2367
2368 if (SYMBOLP (atom))
2369 x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom);
2370 else if (STRINGP (atom))
2371 {
2372 BLOCK_INPUT;
2373 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2374 UNBLOCK_INPUT;
2375 }
2376 else
2377 error ("ATOM must be a symbol or a string");
2378
2379 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2380 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2381 return Qnil;
2382
2383 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2384 {
2385 dpyinfo->x_dnd_atoms_size *= 2;
2386 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2387 sizeof (*dpyinfo->x_dnd_atoms)
2388 * dpyinfo->x_dnd_atoms_size);
2389 }
2390
2391 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2392 return Qnil;
2393 }
2394
2395 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2396
2397 int
2398 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2399 {
2400 Lisp_Object vec;
2401 Lisp_Object frame;
2402 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2403 unsigned long size = 160/event->format;
2404 int x, y;
2405 unsigned char *data = (unsigned char *) event->data.b;
2406 int idata[5];
2407 size_t i;
2408
2409 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2410 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2411
2412 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2413
2414 XSETFRAME (frame, f);
2415
2416 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2417 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2418 function expects them to be of size int (i.e. 32). So to be able to
2419 use that function, put the data in the form it expects if format is 32. */
2420
2421 if (32 < BITS_PER_LONG && event->format == 32)
2422 {
2423 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2424 idata[i] = (int) event->data.l[i];
2425 data = (unsigned char *) idata;
2426 }
2427
2428 vec = Fmake_vector (make_number (4), Qnil);
2429 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2430 event->message_type)));
2431 ASET (vec, 1, frame);
2432 ASET (vec, 2, make_number (event->format));
2433 ASET (vec, 3, x_property_data_to_lisp (f,
2434 data,
2435 event->message_type,
2436 event->format,
2437 size));
2438
2439 mouse_position_for_drop (f, &x, &y);
2440 bufp->kind = DRAG_N_DROP_EVENT;
2441 bufp->frame_or_window = frame;
2442 bufp->timestamp = CurrentTime;
2443 bufp->x = make_number (x);
2444 bufp->y = make_number (y);
2445 bufp->arg = vec;
2446 bufp->modifiers = 0;
2447
2448 return 1;
2449 }
2450
2451 DEFUN ("x-send-client-message", Fx_send_client_event,
2452 Sx_send_client_message, 6, 6, 0,
2453 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2454
2455 For DISPLAY, specify either a frame or a display name (a string).
2456 If DISPLAY is nil, that stands for the selected frame's display.
2457 DEST may be a number, in which case it is a Window id. The value 0 may
2458 be used to send to the root window of the DISPLAY.
2459 If DEST is a cons, it is converted to a 32 bit number
2460 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2461 number is then used as a window id.
2462 If DEST is a frame the event is sent to the outer window of that frame.
2463 A value of nil means the currently selected frame.
2464 If DEST is the string "PointerWindow" the event is sent to the window that
2465 contains the pointer. If DEST is the string "InputFocus" the event is
2466 sent to the window that has the input focus.
2467 FROM is the frame sending the event. Use nil for currently selected frame.
2468 MESSAGE-TYPE is the name of an Atom as a string.
2469 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2470 bits. VALUES is a list of numbers, cons and/or strings containing the values
2471 to send. If a value is a string, it is converted to an Atom and the value of
2472 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2473 with the high 16 bits from the car and the lower 16 bit from the cdr.
2474 If more values than fits into the event is given, the excessive values
2475 are ignored. */)
2476 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2477 {
2478 struct x_display_info *dpyinfo = check_x_display_info (display);
2479
2480 CHECK_STRING (message_type);
2481 x_send_client_event(display, dest, from,
2482 XInternAtom (dpyinfo->display,
2483 SSDATA (message_type),
2484 False),
2485 format, values);
2486
2487 return Qnil;
2488 }
2489
2490 void
2491 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2492 {
2493 struct x_display_info *dpyinfo = check_x_display_info (display);
2494 Window wdest;
2495 XEvent event;
2496 struct frame *f = check_x_frame (from);
2497 int to_root;
2498
2499 CHECK_NUMBER (format);
2500 CHECK_CONS (values);
2501
2502 if (x_check_property_data (values) == -1)
2503 error ("Bad data in VALUES, must be number, cons or string");
2504
2505 event.xclient.type = ClientMessage;
2506 event.xclient.format = XFASTINT (format);
2507
2508 if (event.xclient.format != 8 && event.xclient.format != 16
2509 && event.xclient.format != 32)
2510 error ("FORMAT must be one of 8, 16 or 32");
2511
2512 if (FRAMEP (dest) || NILP (dest))
2513 {
2514 struct frame *fdest = check_x_frame (dest);
2515 wdest = FRAME_OUTER_WINDOW (fdest);
2516 }
2517 else if (STRINGP (dest))
2518 {
2519 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2520 wdest = PointerWindow;
2521 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2522 wdest = InputFocus;
2523 else
2524 error ("DEST as a string must be one of PointerWindow or InputFocus");
2525 }
2526 else if (INTEGERP (dest))
2527 wdest = (Window) XFASTINT (dest);
2528 else if (FLOATP (dest))
2529 wdest = (Window) XFLOAT_DATA (dest);
2530 else if (CONSP (dest))
2531 {
2532 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2533 error ("Both car and cdr for DEST must be numbers");
2534 else
2535 wdest = (Window) cons_to_long (dest);
2536 }
2537 else
2538 error ("DEST must be a frame, nil, string, number or cons");
2539
2540 if (wdest == 0) wdest = dpyinfo->root_window;
2541 to_root = wdest == dpyinfo->root_window;
2542
2543 BLOCK_INPUT;
2544
2545 event.xclient.message_type = message_type;
2546 event.xclient.display = dpyinfo->display;
2547
2548 /* Some clients (metacity for example) expects sending window to be here
2549 when sending to the root window. */
2550 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2551
2552
2553 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2554 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2555 event.xclient.format);
2556
2557 /* If event mask is 0 the event is sent to the client that created
2558 the destination window. But if we are sending to the root window,
2559 there is no such client. Then we set the event mask to 0xffff. The
2560 event then goes to clients selecting for events on the root window. */
2561 x_catch_errors (dpyinfo->display);
2562 {
2563 int propagate = to_root ? False : True;
2564 unsigned mask = to_root ? 0xffff : 0;
2565 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2566 XFlush (dpyinfo->display);
2567 }
2568 x_uncatch_errors ();
2569 UNBLOCK_INPUT;
2570 }
2571
2572 \f
2573 void
2574 syms_of_xselect (void)
2575 {
2576 defsubr (&Sx_get_selection_internal);
2577 defsubr (&Sx_own_selection_internal);
2578 defsubr (&Sx_disown_selection_internal);
2579 defsubr (&Sx_selection_owner_p);
2580 defsubr (&Sx_selection_exists_p);
2581
2582 defsubr (&Sx_get_atom_name);
2583 defsubr (&Sx_send_client_message);
2584 defsubr (&Sx_register_dnd_atom);
2585
2586 reading_selection_reply = Fcons (Qnil, Qnil);
2587 staticpro (&reading_selection_reply);
2588 reading_selection_window = 0;
2589 reading_which_selection = 0;
2590
2591 property_change_wait_list = 0;
2592 prop_location_identifier = 0;
2593 property_change_reply = Fcons (Qnil, Qnil);
2594 staticpro (&property_change_reply);
2595
2596 Vselection_alist = Qnil;
2597 staticpro (&Vselection_alist);
2598
2599 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2600 doc: /* An alist associating X Windows selection-types with functions.
2601 These functions are called to convert the selection, with three args:
2602 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2603 a desired type to which the selection should be converted;
2604 and the local selection value (whatever was given to `x-own-selection').
2605
2606 The function should return the value to send to the X server
2607 \(typically a string). A return value of nil
2608 means that the conversion could not be done.
2609 A return value which is the symbol `NULL'
2610 means that a side-effect was executed,
2611 and there is no meaningful selection value. */);
2612 Vselection_converter_alist = Qnil;
2613
2614 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2615 doc: /* A list of functions to be called when Emacs loses an X selection.
2616 \(This happens when some other X client makes its own selection
2617 or when a Lisp program explicitly clears the selection.)
2618 The functions are called with one argument, the selection type
2619 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2620 Vx_lost_selection_functions = Qnil;
2621
2622 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2623 doc: /* A list of functions to be called when Emacs answers a selection request.
2624 The functions are called with four arguments:
2625 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2626 - the selection-type which Emacs was asked to convert the
2627 selection into before sending (for example, `STRING' or `LENGTH');
2628 - a flag indicating success or failure for responding to the request.
2629 We might have failed (and declined the request) for any number of reasons,
2630 including being asked for a selection that we no longer own, or being asked
2631 to convert into a type that we don't know about or that is inappropriate.
2632 This hook doesn't let you change the behavior of Emacs's selection replies,
2633 it merely informs you that they have happened. */);
2634 Vx_sent_selection_functions = Qnil;
2635
2636 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2637 doc: /* Number of milliseconds to wait for a selection reply.
2638 If the selection owner doesn't reply in this time, we give up.
2639 A value of 0 means wait as long as necessary. This is initialized from the
2640 \"*selectionTimeout\" resource. */);
2641 x_selection_timeout = 0;
2642
2643 /* QPRIMARY is defined in keyboard.c. */
2644 DEFSYM (QSECONDARY, "SECONDARY");
2645 DEFSYM (QSTRING, "STRING");
2646 DEFSYM (QINTEGER, "INTEGER");
2647 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2648 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2649 DEFSYM (QTEXT, "TEXT");
2650 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2651 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2652 DEFSYM (QDELETE, "DELETE");
2653 DEFSYM (QMULTIPLE, "MULTIPLE");
2654 DEFSYM (QINCR, "INCR");
2655 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2656 DEFSYM (QTARGETS, "TARGETS");
2657 DEFSYM (QATOM, "ATOM");
2658 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2659 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2660 DEFSYM (QNULL, "NULL");
2661 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2662 DEFSYM (Qforeign_selection, "foreign-selection");
2663 }