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