]> code.delx.au - gnu-emacs/blob - src/xselect.c
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / 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 #define TRACE3(fmt, a0, a1) (void) 0
101 #endif
102
103
104 Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
105 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
106 QATOM_PAIR;
107
108 Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
109 Lisp_Object QUTF8_STRING; /* This is a type of selection. */
110
111 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 /* The timestamp of the last input event Emacs received from the X server. */
126 /* Defined in keyboard.c. */
127 extern unsigned long last_event_timestamp;
128
129
130 \f
131 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
132 handling. */
133
134 struct selection_event_queue
135 {
136 struct input_event event;
137 struct selection_event_queue *next;
138 };
139
140 static struct selection_event_queue *selection_queue;
141
142 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
143
144 static int x_queue_selection_requests;
145
146 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
147
148 static void
149 x_queue_event (struct input_event *event)
150 {
151 struct selection_event_queue *queue_tmp;
152
153 /* Don't queue repeated requests.
154 This only happens for large requests which uses the incremental protocol. */
155 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
156 {
157 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
158 {
159 TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp);
160 x_decline_selection_request (event);
161 return;
162 }
163 }
164
165 queue_tmp
166 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
167
168 if (queue_tmp != NULL)
169 {
170 TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
171 queue_tmp->event = *event;
172 queue_tmp->next = selection_queue;
173 selection_queue = queue_tmp;
174 }
175 }
176
177 /* Start queuing SELECTION_REQUEST_EVENT events. */
178
179 static void
180 x_start_queuing_selection_requests (void)
181 {
182 if (x_queue_selection_requests)
183 abort ();
184
185 x_queue_selection_requests++;
186 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
187 }
188
189 /* Stop queuing SELECTION_REQUEST_EVENT events. */
190
191 static void
192 x_stop_queuing_selection_requests (void)
193 {
194 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
195 --x_queue_selection_requests;
196
197 /* Take all the queued events and put them back
198 so that they get processed afresh. */
199
200 while (selection_queue != NULL)
201 {
202 struct selection_event_queue *queue_tmp = selection_queue;
203 TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
204 kbd_buffer_unget_event (&queue_tmp->event);
205 selection_queue = queue_tmp->next;
206 xfree ((char *)queue_tmp);
207 }
208 }
209 \f
210
211 /* This converts a Lisp symbol to a server Atom, avoiding a server
212 roundtrip whenever possible. */
213
214 static Atom
215 symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym)
216 {
217 Atom val;
218 if (NILP (sym)) return 0;
219 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
220 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
221 if (EQ (sym, QSTRING)) return XA_STRING;
222 if (EQ (sym, QINTEGER)) return XA_INTEGER;
223 if (EQ (sym, QATOM)) return XA_ATOM;
224 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
225 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
226 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
227 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
228 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
229 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
230 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
231 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
232 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
233 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
234 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
235 if (!SYMBOLP (sym)) abort ();
236
237 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
238 BLOCK_INPUT;
239 val = XInternAtom (display, SSDATA (SYMBOL_NAME (sym)), False);
240 UNBLOCK_INPUT;
241 return val;
242 }
243
244
245 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
246 and calls to intern whenever possible. */
247
248 static Lisp_Object
249 x_atom_to_symbol (Display *dpy, Atom atom)
250 {
251 struct x_display_info *dpyinfo;
252 char *str;
253 Lisp_Object val;
254
255 if (! atom)
256 return Qnil;
257
258 switch (atom)
259 {
260 case XA_PRIMARY:
261 return QPRIMARY;
262 case XA_SECONDARY:
263 return QSECONDARY;
264 case XA_STRING:
265 return QSTRING;
266 case XA_INTEGER:
267 return QINTEGER;
268 case XA_ATOM:
269 return QATOM;
270 }
271
272 dpyinfo = x_display_info_for_display (dpy);
273 if (atom == dpyinfo->Xatom_CLIPBOARD)
274 return QCLIPBOARD;
275 if (atom == dpyinfo->Xatom_TIMESTAMP)
276 return QTIMESTAMP;
277 if (atom == dpyinfo->Xatom_TEXT)
278 return QTEXT;
279 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
280 return QCOMPOUND_TEXT;
281 if (atom == dpyinfo->Xatom_UTF8_STRING)
282 return QUTF8_STRING;
283 if (atom == dpyinfo->Xatom_DELETE)
284 return QDELETE;
285 if (atom == dpyinfo->Xatom_MULTIPLE)
286 return QMULTIPLE;
287 if (atom == dpyinfo->Xatom_INCR)
288 return QINCR;
289 if (atom == dpyinfo->Xatom_EMACS_TMP)
290 return QEMACS_TMP;
291 if (atom == dpyinfo->Xatom_TARGETS)
292 return QTARGETS;
293 if (atom == dpyinfo->Xatom_NULL)
294 return QNULL;
295
296 BLOCK_INPUT;
297 str = XGetAtomName (dpy, atom);
298 UNBLOCK_INPUT;
299 TRACE1 ("XGetAtomName --> %s", str);
300 if (! str) return Qnil;
301 val = intern (str);
302 BLOCK_INPUT;
303 /* This was allocated by Xlib, so use XFree. */
304 XFree (str);
305 UNBLOCK_INPUT;
306 return val;
307 }
308 \f
309 /* Do protocol to assert ourself as a selection owner.
310 Update the Vselection_alist so that we can reply to later requests for
311 our selection. */
312
313 static void
314 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value)
315 {
316 struct frame *sf = SELECTED_FRAME ();
317 Window selecting_window;
318 Display *display;
319 Time time = last_event_timestamp;
320 Atom selection_atom;
321 struct x_display_info *dpyinfo;
322
323 if (! FRAME_X_P (sf))
324 return;
325
326 selecting_window = FRAME_X_WINDOW (sf);
327 display = FRAME_X_DISPLAY (sf);
328 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
329
330 CHECK_SYMBOL (selection_name);
331 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
332
333 BLOCK_INPUT;
334 x_catch_errors (display);
335 XSetSelectionOwner (display, selection_atom, selecting_window, time);
336 x_check_errors (display, "Can't set selection: %s");
337 x_uncatch_errors ();
338 UNBLOCK_INPUT;
339
340 /* Now update the local cache */
341 {
342 Lisp_Object selection_time;
343 Lisp_Object selection_data;
344 Lisp_Object prev_value;
345
346 selection_time = long_to_cons ((unsigned long) time);
347 selection_data = list4 (selection_name, selection_value,
348 selection_time, selected_frame);
349 prev_value = assq_no_quit (selection_name, Vselection_alist);
350
351 Vselection_alist = Fcons (selection_data, Vselection_alist);
352
353 /* If we already owned the selection, remove the old selection data.
354 Perhaps we should destructively modify it instead.
355 Don't use Fdelq as that may QUIT. */
356 if (!NILP (prev_value))
357 {
358 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
359 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
360 if (EQ (prev_value, Fcar (XCDR (rest))))
361 {
362 XSETCDR (rest, Fcdr (XCDR (rest)));
363 break;
364 }
365 }
366 }
367 }
368 \f
369 /* Given a selection-name and desired type, look up our local copy of
370 the selection value and convert it to the type.
371 The value is nil or a string.
372 This function is used both for remote requests (LOCAL_REQUEST is zero)
373 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
374
375 This calls random Lisp code, and may signal or gc. */
376
377 static Lisp_Object
378 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request)
379 {
380 Lisp_Object local_value;
381 Lisp_Object handler_fn, value, type, check;
382 int count;
383
384 local_value = assq_no_quit (selection_symbol, Vselection_alist);
385
386 if (NILP (local_value)) return Qnil;
387
388 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
389 if (EQ (target_type, QTIMESTAMP))
390 {
391 handler_fn = Qnil;
392 value = XCAR (XCDR (XCDR (local_value)));
393 }
394 #if 0
395 else if (EQ (target_type, QDELETE))
396 {
397 handler_fn = Qnil;
398 Fx_disown_selection_internal
399 (selection_symbol,
400 XCAR (XCDR (XCDR (local_value))));
401 value = QNULL;
402 }
403 #endif
404
405 #if 0 /* #### MULTIPLE doesn't work yet */
406 else if (CONSP (target_type)
407 && XCAR (target_type) == QMULTIPLE)
408 {
409 Lisp_Object pairs;
410 int size;
411 int i;
412 pairs = XCDR (target_type);
413 size = XVECTOR (pairs)->size;
414 /* If the target is MULTIPLE, then target_type looks like
415 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
416 We modify the second element of each pair in the vector and
417 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
418 */
419 for (i = 0; i < size; i++)
420 {
421 Lisp_Object pair;
422 pair = XVECTOR (pairs)->contents [i];
423 XVECTOR (pair)->contents [1]
424 = x_get_local_selection (XVECTOR (pair)->contents [0],
425 XVECTOR (pair)->contents [1],
426 local_request);
427 }
428 return pairs;
429 }
430 #endif
431 else
432 {
433 /* Don't allow a quit within the converter.
434 When the user types C-g, he would be surprised
435 if by luck it came during a converter. */
436 count = SPECPDL_INDEX ();
437 specbind (Qinhibit_quit, Qt);
438
439 CHECK_SYMBOL (target_type);
440 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
441 /* gcpro is not needed here since nothing but HANDLER_FN
442 is live, and that ought to be a symbol. */
443
444 if (!NILP (handler_fn))
445 value = call3 (handler_fn,
446 selection_symbol, (local_request ? Qnil : target_type),
447 XCAR (XCDR (local_value)));
448 else
449 value = Qnil;
450 unbind_to (count, Qnil);
451 }
452
453 /* Make sure this value is of a type that we could transmit
454 to another X client. */
455
456 check = value;
457 if (CONSP (value)
458 && SYMBOLP (XCAR (value)))
459 type = XCAR (value),
460 check = XCDR (value);
461
462 if (STRINGP (check)
463 || VECTORP (check)
464 || SYMBOLP (check)
465 || INTEGERP (check)
466 || NILP (value))
467 return value;
468 /* Check for a value that cons_to_long could handle. */
469 else if (CONSP (check)
470 && INTEGERP (XCAR (check))
471 && (INTEGERP (XCDR (check))
472 ||
473 (CONSP (XCDR (check))
474 && INTEGERP (XCAR (XCDR (check)))
475 && NILP (XCDR (XCDR (check))))))
476 return value;
477
478 signal_error ("Invalid data returned by selection-conversion function",
479 list2 (handler_fn, value));
480 }
481 \f
482 /* Subroutines of x_reply_selection_request. */
483
484 /* Send a SelectionNotify event to the requestor with property=None,
485 meaning we were unable to do what they wanted. */
486
487 static void
488 x_decline_selection_request (struct input_event *event)
489 {
490 XSelectionEvent reply;
491
492 reply.type = SelectionNotify;
493 reply.display = SELECTION_EVENT_DISPLAY (event);
494 reply.requestor = SELECTION_EVENT_REQUESTOR (event);
495 reply.selection = SELECTION_EVENT_SELECTION (event);
496 reply.time = SELECTION_EVENT_TIME (event);
497 reply.target = SELECTION_EVENT_TARGET (event);
498 reply.property = None;
499
500 /* The reason for the error may be that the receiver has
501 died in the meantime. Handle that case. */
502 BLOCK_INPUT;
503 x_catch_errors (reply.display);
504 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
505 XFlush (reply.display);
506 x_uncatch_errors ();
507 UNBLOCK_INPUT;
508 }
509
510 /* This is the selection request currently being processed.
511 It is set to zero when the request is fully processed. */
512 static struct input_event *x_selection_current_request;
513
514 /* Display info in x_selection_request. */
515
516 static struct x_display_info *selection_request_dpyinfo;
517
518 /* Used as an unwind-protect clause so that, if a selection-converter signals
519 an error, we tell the requester that we were unable to do what they wanted
520 before we throw to top-level or go into the debugger or whatever. */
521
522 static Lisp_Object
523 x_selection_request_lisp_error (Lisp_Object ignore)
524 {
525 if (x_selection_current_request != 0
526 && selection_request_dpyinfo->display)
527 x_decline_selection_request (x_selection_current_request);
528 return Qnil;
529 }
530
531 static Lisp_Object
532 x_catch_errors_unwind (Lisp_Object dummy)
533 {
534 BLOCK_INPUT;
535 x_uncatch_errors ();
536 UNBLOCK_INPUT;
537 return Qnil;
538 }
539 \f
540
541 /* This stuff is so that INCR selections are reentrant (that is, so we can
542 be servicing multiple INCR selection requests simultaneously.) I haven't
543 actually tested that yet. */
544
545 /* Keep a list of the property changes that are awaited. */
546
547 struct prop_location
548 {
549 int identifier;
550 Display *display;
551 Window window;
552 Atom property;
553 int desired_state;
554 int arrived;
555 struct prop_location *next;
556 };
557
558 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
559 static void wait_for_property_change (struct prop_location *location);
560 static void unexpect_property_change (struct prop_location *location);
561 static int waiting_for_other_props_on_window (Display *display, Window window);
562
563 static int prop_location_identifier;
564
565 static Lisp_Object property_change_reply;
566
567 static struct prop_location *property_change_reply_object;
568
569 static struct prop_location *property_change_wait_list;
570
571 static Lisp_Object
572 queue_selection_requests_unwind (Lisp_Object tem)
573 {
574 x_stop_queuing_selection_requests ();
575 return Qnil;
576 }
577
578 /* Return some frame whose display info is DPYINFO.
579 Return nil if there is none. */
580
581 static Lisp_Object
582 some_frame_on_display (struct x_display_info *dpyinfo)
583 {
584 Lisp_Object list, frame;
585
586 FOR_EACH_FRAME (list, frame)
587 {
588 if (FRAME_X_P (XFRAME (frame))
589 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
590 return frame;
591 }
592
593 return Qnil;
594 }
595 \f
596 /* Send the reply to a selection request event EVENT.
597 TYPE is the type of selection data requested.
598 DATA and SIZE describe the data to send, already converted.
599 FORMAT is the unit-size (in bits) of the data to be transmitted. */
600
601 #ifdef TRACE_SELECTION
602 static int x_reply_selection_request_cnt;
603 #endif /* TRACE_SELECTION */
604
605 static void
606 x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type)
607 {
608 XSelectionEvent reply;
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, (XEvent *) &reply);
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, (XEvent *) &reply);
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 *prev = 0, *rest = property_change_wait_list;
1194
1195 while (rest)
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 prev = rest;
1218 rest = rest->next;
1219 }
1220 }
1221
1222
1223 \f
1224 #if 0 /* #### MULTIPLE doesn't work yet */
1225
1226 static Lisp_Object
1227 fetch_multiple_target (event)
1228 XSelectionRequestEvent *event;
1229 {
1230 Display *display = event->display;
1231 Window window = event->requestor;
1232 Atom target = event->target;
1233 Atom selection_atom = event->selection;
1234 int result;
1235
1236 return
1237 Fcons (QMULTIPLE,
1238 x_get_window_property_as_lisp_data (display, window, target,
1239 QMULTIPLE, selection_atom));
1240 }
1241
1242 static Lisp_Object
1243 copy_multiple_data (obj)
1244 Lisp_Object obj;
1245 {
1246 Lisp_Object vec;
1247 int i;
1248 int size;
1249 if (CONSP (obj))
1250 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1251
1252 CHECK_VECTOR (obj);
1253 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1254 for (i = 0; i < size; i++)
1255 {
1256 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1257 CHECK_VECTOR (vec2);
1258 if (XVECTOR (vec2)->size != 2)
1259 /* ??? Confusing error message */
1260 signal_error ("Vectors must be of length 2", vec2);
1261 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1262 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1263 = XVECTOR (vec2)->contents [0];
1264 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1265 = XVECTOR (vec2)->contents [1];
1266 }
1267 return vec;
1268 }
1269
1270 #endif
1271
1272 \f
1273 /* Variables for communication with x_handle_selection_notify. */
1274 static Atom reading_which_selection;
1275 static Lisp_Object reading_selection_reply;
1276 static Window reading_selection_window;
1277
1278 /* Do protocol to read selection-data from the server.
1279 Converts this to Lisp data and returns it. */
1280
1281 static Lisp_Object
1282 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
1283 {
1284 struct frame *sf = SELECTED_FRAME ();
1285 Window requestor_window;
1286 Display *display;
1287 struct x_display_info *dpyinfo;
1288 Time requestor_time = last_event_timestamp;
1289 Atom target_property;
1290 Atom selection_atom;
1291 Atom type_atom;
1292 int secs, usecs;
1293 int count = SPECPDL_INDEX ();
1294 Lisp_Object frame;
1295
1296 if (! FRAME_X_P (sf))
1297 return Qnil;
1298
1299 requestor_window = FRAME_X_WINDOW (sf);
1300 display = FRAME_X_DISPLAY (sf);
1301 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1302 target_property = dpyinfo->Xatom_EMACS_TMP;
1303 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1304
1305 if (CONSP (target_type))
1306 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1307 else
1308 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1309
1310 if (! NILP (time_stamp))
1311 {
1312 if (CONSP (time_stamp))
1313 requestor_time = (Time) cons_to_long (time_stamp);
1314 else if (INTEGERP (time_stamp))
1315 requestor_time = (Time) XUINT (time_stamp);
1316 else if (FLOATP (time_stamp))
1317 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1318 else
1319 error ("TIME_STAMP must be cons or number");
1320 }
1321
1322 BLOCK_INPUT;
1323
1324 /* The protected block contains wait_reading_process_output, which
1325 can run random lisp code (process handlers) or signal.
1326 Therefore, we put the x_uncatch_errors call in an unwind. */
1327 record_unwind_protect (x_catch_errors_unwind, Qnil);
1328 x_catch_errors (display);
1329
1330 TRACE2 ("Get selection %s, type %s",
1331 XGetAtomName (display, type_atom),
1332 XGetAtomName (display, target_property));
1333
1334 XConvertSelection (display, selection_atom, type_atom, target_property,
1335 requestor_window, requestor_time);
1336 XFlush (display);
1337
1338 /* Prepare to block until the reply has been read. */
1339 reading_selection_window = requestor_window;
1340 reading_which_selection = selection_atom;
1341 XSETCAR (reading_selection_reply, Qnil);
1342
1343 frame = some_frame_on_display (dpyinfo);
1344
1345 /* If the display no longer has frames, we can't expect
1346 to get many more selection requests from it, so don't
1347 bother trying to queue them. */
1348 if (!NILP (frame))
1349 {
1350 x_start_queuing_selection_requests ();
1351
1352 record_unwind_protect (queue_selection_requests_unwind,
1353 Qnil);
1354 }
1355 UNBLOCK_INPUT;
1356
1357 /* This allows quits. Also, don't wait forever. */
1358 secs = x_selection_timeout / 1000;
1359 usecs = (x_selection_timeout % 1000) * 1000;
1360 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1361 wait_reading_process_output (secs, usecs, 0, 0,
1362 reading_selection_reply, NULL, 0);
1363 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1364
1365 BLOCK_INPUT;
1366 if (x_had_errors_p (display))
1367 error ("Cannot get selection");
1368 /* This calls x_uncatch_errors. */
1369 unbind_to (count, Qnil);
1370 UNBLOCK_INPUT;
1371
1372 if (NILP (XCAR (reading_selection_reply)))
1373 error ("Timed out waiting for reply from selection owner");
1374 if (EQ (XCAR (reading_selection_reply), Qlambda))
1375 return Qnil;
1376
1377 /* Otherwise, the selection is waiting for us on the requested property. */
1378 return
1379 x_get_window_property_as_lisp_data (display, requestor_window,
1380 target_property, target_type,
1381 selection_atom);
1382 }
1383 \f
1384 /* Subroutines of x_get_window_property_as_lisp_data */
1385
1386 /* Use xfree, not XFree, to free the data obtained with this function. */
1387
1388 static void
1389 x_get_window_property (Display *display, Window window, Atom property,
1390 unsigned char **data_ret, int *bytes_ret,
1391 Atom *actual_type_ret, int *actual_format_ret,
1392 unsigned long *actual_size_ret, int delete_p)
1393 {
1394 int total_size;
1395 unsigned long bytes_remaining;
1396 int offset = 0;
1397 unsigned char *tmp_data = 0;
1398 int result;
1399 int buffer_size = SELECTION_QUANTUM (display);
1400
1401 if (buffer_size > MAX_SELECTION_QUANTUM)
1402 buffer_size = MAX_SELECTION_QUANTUM;
1403
1404 BLOCK_INPUT;
1405
1406 /* First probe the thing to find out how big it is. */
1407 result = XGetWindowProperty (display, window, property,
1408 0L, 0L, False, AnyPropertyType,
1409 actual_type_ret, actual_format_ret,
1410 actual_size_ret,
1411 &bytes_remaining, &tmp_data);
1412 if (result != Success)
1413 {
1414 UNBLOCK_INPUT;
1415 *data_ret = 0;
1416 *bytes_ret = 0;
1417 return;
1418 }
1419
1420 /* This was allocated by Xlib, so use XFree. */
1421 XFree ((char *) tmp_data);
1422
1423 if (*actual_type_ret == None || *actual_format_ret == 0)
1424 {
1425 UNBLOCK_INPUT;
1426 return;
1427 }
1428
1429 total_size = bytes_remaining + 1;
1430 *data_ret = (unsigned char *) xmalloc (total_size);
1431
1432 /* Now read, until we've gotten it all. */
1433 while (bytes_remaining)
1434 {
1435 #ifdef TRACE_SELECTION
1436 int last = bytes_remaining;
1437 #endif
1438 result
1439 = XGetWindowProperty (display, window, property,
1440 (long)offset/4, (long)buffer_size/4,
1441 False,
1442 AnyPropertyType,
1443 actual_type_ret, actual_format_ret,
1444 actual_size_ret, &bytes_remaining, &tmp_data);
1445
1446 TRACE2 ("Read %ld bytes from property %s",
1447 last - bytes_remaining,
1448 XGetAtomName (display, property));
1449
1450 /* If this doesn't return Success at this point, it means that
1451 some clod deleted the selection while we were in the midst of
1452 reading it. Deal with that, I guess.... */
1453 if (result != Success)
1454 break;
1455
1456 /* The man page for XGetWindowProperty says:
1457 "If the returned format is 32, the returned data is represented
1458 as a long array and should be cast to that type to obtain the
1459 elements."
1460 This applies even if long is more than 32 bits, the X library
1461 converts from 32 bit elements received from the X server to long
1462 and passes the long array to us. Thus, for that case memcpy can not
1463 be used. We convert to a 32 bit type here, because so much code
1464 assume on that.
1465
1466 The bytes and offsets passed to XGetWindowProperty refers to the
1467 property and those are indeed in 32 bit quantities if format is 32. */
1468
1469 if (*actual_format_ret == 32 && *actual_format_ret < BITS_PER_LONG)
1470 {
1471 unsigned long i;
1472 int *idata = (int *) ((*data_ret) + offset);
1473 long *ldata = (long *) tmp_data;
1474
1475 for (i = 0; i < *actual_size_ret; ++i)
1476 {
1477 idata[i]= (int) ldata[i];
1478 offset += 4;
1479 }
1480 }
1481 else
1482 {
1483 *actual_size_ret *= *actual_format_ret / 8;
1484 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1485 offset += *actual_size_ret;
1486 }
1487
1488 /* This was allocated by Xlib, so use XFree. */
1489 XFree ((char *) tmp_data);
1490 }
1491
1492 XFlush (display);
1493 UNBLOCK_INPUT;
1494 *bytes_ret = offset;
1495 }
1496 \f
1497 /* Use xfree, not XFree, to free the data obtained with this function. */
1498
1499 static void
1500 receive_incremental_selection (Display *display, Window window, Atom property,
1501 Lisp_Object target_type,
1502 unsigned int min_size_bytes,
1503 unsigned char **data_ret, int *size_bytes_ret,
1504 Atom *type_ret, int *format_ret,
1505 unsigned long *size_ret)
1506 {
1507 int offset = 0;
1508 struct prop_location *wait_object;
1509 *size_bytes_ret = min_size_bytes;
1510 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1511
1512 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1513
1514 /* At this point, we have read an INCR property.
1515 Delete the property to ack it.
1516 (But first, prepare to receive the next event in this handshake.)
1517
1518 Now, we must loop, waiting for the sending window to put a value on
1519 that property, then reading the property, then deleting it to ack.
1520 We are done when the sender places a property of length 0.
1521 */
1522 BLOCK_INPUT;
1523 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1524 TRACE1 (" Delete property %s",
1525 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1526 XDeleteProperty (display, window, property);
1527 TRACE1 (" Expect new value of property %s",
1528 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1529 wait_object = expect_property_change (display, window, property,
1530 PropertyNewValue);
1531 XFlush (display);
1532 UNBLOCK_INPUT;
1533
1534 while (1)
1535 {
1536 unsigned char *tmp_data;
1537 int tmp_size_bytes;
1538
1539 TRACE0 (" Wait for property change");
1540 wait_for_property_change (wait_object);
1541
1542 /* expect it again immediately, because x_get_window_property may
1543 .. no it won't, I don't get it.
1544 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1545 TRACE0 (" Get property value");
1546 x_get_window_property (display, window, property,
1547 &tmp_data, &tmp_size_bytes,
1548 type_ret, format_ret, size_ret, 1);
1549
1550 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1551
1552 if (tmp_size_bytes == 0) /* we're done */
1553 {
1554 TRACE0 ("Done reading incrementally");
1555
1556 if (! waiting_for_other_props_on_window (display, window))
1557 XSelectInput (display, window, STANDARD_EVENT_SET);
1558 /* Use xfree, not XFree, because x_get_window_property
1559 calls xmalloc itself. */
1560 xfree (tmp_data);
1561 break;
1562 }
1563
1564 BLOCK_INPUT;
1565 TRACE1 (" ACK by deleting property %s",
1566 XGetAtomName (display, property));
1567 XDeleteProperty (display, window, property);
1568 wait_object = expect_property_change (display, window, property,
1569 PropertyNewValue);
1570 XFlush (display);
1571 UNBLOCK_INPUT;
1572
1573 if (*size_bytes_ret < offset + tmp_size_bytes)
1574 {
1575 *size_bytes_ret = offset + tmp_size_bytes;
1576 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1577 }
1578
1579 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1580 offset += tmp_size_bytes;
1581
1582 /* Use xfree, not XFree, because x_get_window_property
1583 calls xmalloc itself. */
1584 xfree (tmp_data);
1585 }
1586 }
1587
1588 \f
1589 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1590 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1591 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1592
1593 static Lisp_Object
1594 x_get_window_property_as_lisp_data (Display *display, Window window,
1595 Atom property,
1596 Lisp_Object target_type,
1597 Atom selection_atom)
1598 {
1599 Atom actual_type;
1600 int actual_format;
1601 unsigned long actual_size;
1602 unsigned char *data = 0;
1603 int bytes = 0;
1604 Lisp_Object val;
1605 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1606
1607 TRACE0 ("Reading selection data");
1608
1609 x_get_window_property (display, window, property, &data, &bytes,
1610 &actual_type, &actual_format, &actual_size, 1);
1611 if (! data)
1612 {
1613 int there_is_a_selection_owner;
1614 BLOCK_INPUT;
1615 there_is_a_selection_owner
1616 = XGetSelectionOwner (display, selection_atom);
1617 UNBLOCK_INPUT;
1618 if (there_is_a_selection_owner)
1619 signal_error ("Selection owner couldn't convert",
1620 actual_type
1621 ? list2 (target_type,
1622 x_atom_to_symbol (display, actual_type))
1623 : target_type);
1624 else
1625 signal_error ("No selection",
1626 x_atom_to_symbol (display, selection_atom));
1627 }
1628
1629 if (actual_type == dpyinfo->Xatom_INCR)
1630 {
1631 /* That wasn't really the data, just the beginning. */
1632
1633 unsigned int min_size_bytes = * ((unsigned int *) data);
1634 BLOCK_INPUT;
1635 /* Use xfree, not XFree, because x_get_window_property
1636 calls xmalloc itself. */
1637 xfree ((char *) data);
1638 UNBLOCK_INPUT;
1639 receive_incremental_selection (display, window, property, target_type,
1640 min_size_bytes, &data, &bytes,
1641 &actual_type, &actual_format,
1642 &actual_size);
1643 }
1644
1645 BLOCK_INPUT;
1646 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1647 XDeleteProperty (display, window, property);
1648 XFlush (display);
1649 UNBLOCK_INPUT;
1650
1651 /* It's been read. Now convert it to a lisp object in some semi-rational
1652 manner. */
1653 val = selection_data_to_lisp_data (display, data, bytes,
1654 actual_type, actual_format);
1655
1656 /* Use xfree, not XFree, because x_get_window_property
1657 calls xmalloc itself. */
1658 xfree ((char *) data);
1659 return val;
1660 }
1661 \f
1662 /* These functions convert from the selection data read from the server into
1663 something that we can use from Lisp, and vice versa.
1664
1665 Type: Format: Size: Lisp Type:
1666 ----- ------- ----- -----------
1667 * 8 * String
1668 ATOM 32 1 Symbol
1669 ATOM 32 > 1 Vector of Symbols
1670 * 16 1 Integer
1671 * 16 > 1 Vector of Integers
1672 * 32 1 if <=16 bits: Integer
1673 if > 16 bits: Cons of top16, bot16
1674 * 32 > 1 Vector of the above
1675
1676 When converting a Lisp number to C, it is assumed to be of format 16 if
1677 it is an integer, and of format 32 if it is a cons of two integers.
1678
1679 When converting a vector of numbers from Lisp to C, it is assumed to be
1680 of format 16 if every element in the vector is an integer, and is assumed
1681 to be of format 32 if any element is a cons of two integers.
1682
1683 When converting an object to C, it may be of the form (SYMBOL . <data>)
1684 where SYMBOL is what we should claim that the type is. Format and
1685 representation are as above.
1686
1687 Important: When format is 32, data should contain an array of int,
1688 not an array of long as the X library returns. This makes a difference
1689 when sizeof(long) != sizeof(int). */
1690
1691
1692
1693 static Lisp_Object
1694 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1695 int size, Atom type, int format)
1696 {
1697 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1698
1699 if (type == dpyinfo->Xatom_NULL)
1700 return QNULL;
1701
1702 /* Convert any 8-bit data to a string, for compactness. */
1703 else if (format == 8)
1704 {
1705 Lisp_Object str, lispy_type;
1706
1707 str = make_unibyte_string ((char *) data, size);
1708 /* Indicate that this string is from foreign selection by a text
1709 property `foreign-selection' so that the caller of
1710 x-get-selection-internal (usually x-get-selection) can know
1711 that the string must be decode. */
1712 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1713 lispy_type = QCOMPOUND_TEXT;
1714 else if (type == dpyinfo->Xatom_UTF8_STRING)
1715 lispy_type = QUTF8_STRING;
1716 else
1717 lispy_type = QSTRING;
1718 Fput_text_property (make_number (0), make_number (size),
1719 Qforeign_selection, lispy_type, str);
1720 return str;
1721 }
1722 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1723 a vector of symbols.
1724 */
1725 else if (type == XA_ATOM)
1726 {
1727 int i;
1728 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1729 But the callers of these function has made sure the data for
1730 format == 32 is an array of int. Thus, use int instead
1731 of Atom. */
1732 int *idata = (int *) data;
1733
1734 if (size == sizeof (int))
1735 return x_atom_to_symbol (display, (Atom) idata[0]);
1736 else
1737 {
1738 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1739 make_number (0));
1740 for (i = 0; i < size / sizeof (int); i++)
1741 Faset (v, make_number (i),
1742 x_atom_to_symbol (display, (Atom) idata[i]));
1743 return v;
1744 }
1745 }
1746
1747 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1748 If the number is 32 bits and won't fit in a Lisp_Int,
1749 convert it to a cons of integers, 16 bits in each half.
1750 */
1751 else if (format == 32 && size == sizeof (int))
1752 return long_to_cons (((unsigned int *) data) [0]);
1753 else if (format == 16 && size == sizeof (short))
1754 return make_number ((int) (((unsigned short *) data) [0]));
1755
1756 /* Convert any other kind of data to a vector of numbers, represented
1757 as above (as an integer, or a cons of two 16 bit integers.)
1758 */
1759 else if (format == 16)
1760 {
1761 int i;
1762 Lisp_Object v;
1763 v = Fmake_vector (make_number (size / 2), make_number (0));
1764 for (i = 0; i < size / 2; i++)
1765 {
1766 int j = (int) ((unsigned short *) data) [i];
1767 Faset (v, make_number (i), make_number (j));
1768 }
1769 return v;
1770 }
1771 else
1772 {
1773 int i;
1774 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1775 for (i = 0; i < size / 4; i++)
1776 {
1777 unsigned int j = ((unsigned int *) data) [i];
1778 Faset (v, make_number (i), long_to_cons (j));
1779 }
1780 return v;
1781 }
1782 }
1783
1784
1785 /* Use xfree, not XFree, to free the data obtained with this function. */
1786
1787 static void
1788 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1789 unsigned char **data_ret, Atom *type_ret,
1790 unsigned int *size_ret,
1791 int *format_ret, int *nofree_ret)
1792 {
1793 Lisp_Object type = Qnil;
1794 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1795
1796 *nofree_ret = 0;
1797
1798 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1799 {
1800 type = XCAR (obj);
1801 obj = XCDR (obj);
1802 if (CONSP (obj) && NILP (XCDR (obj)))
1803 obj = XCAR (obj);
1804 }
1805
1806 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1807 { /* This is not the same as declining */
1808 *format_ret = 32;
1809 *size_ret = 0;
1810 *data_ret = 0;
1811 type = QNULL;
1812 }
1813 else if (STRINGP (obj))
1814 {
1815 if (SCHARS (obj) < SBYTES (obj))
1816 /* OBJ is a multibyte string containing a non-ASCII char. */
1817 signal_error ("Non-ASCII string must be encoded in advance", obj);
1818 if (NILP (type))
1819 type = QSTRING;
1820 *format_ret = 8;
1821 *size_ret = SBYTES (obj);
1822 *data_ret = SDATA (obj);
1823 *nofree_ret = 1;
1824 }
1825 else if (SYMBOLP (obj))
1826 {
1827 *format_ret = 32;
1828 *size_ret = 1;
1829 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1830 (*data_ret) [sizeof (Atom)] = 0;
1831 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
1832 if (NILP (type)) type = QATOM;
1833 }
1834 else if (INTEGERP (obj)
1835 && XINT (obj) < 0xFFFF
1836 && XINT (obj) > -0xFFFF)
1837 {
1838 *format_ret = 16;
1839 *size_ret = 1;
1840 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1841 (*data_ret) [sizeof (short)] = 0;
1842 (*(short **) data_ret) [0] = (short) XINT (obj);
1843 if (NILP (type)) type = QINTEGER;
1844 }
1845 else if (INTEGERP (obj)
1846 || (CONSP (obj) && INTEGERP (XCAR (obj))
1847 && (INTEGERP (XCDR (obj))
1848 || (CONSP (XCDR (obj))
1849 && INTEGERP (XCAR (XCDR (obj)))))))
1850 {
1851 *format_ret = 32;
1852 *size_ret = 1;
1853 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1854 (*data_ret) [sizeof (long)] = 0;
1855 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1856 if (NILP (type)) type = QINTEGER;
1857 }
1858 else if (VECTORP (obj))
1859 {
1860 /* Lisp_Vectors may represent a set of ATOMs;
1861 a set of 16 or 32 bit INTEGERs;
1862 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1863 */
1864 int i;
1865
1866 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1867 /* This vector is an ATOM set */
1868 {
1869 if (NILP (type)) type = QATOM;
1870 *size_ret = XVECTOR (obj)->size;
1871 *format_ret = 32;
1872 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1873 for (i = 0; i < *size_ret; i++)
1874 if (SYMBOLP (XVECTOR (obj)->contents [i]))
1875 (*(Atom **) data_ret) [i]
1876 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
1877 else
1878 signal_error ("All elements of selection vector must have same type", obj);
1879 }
1880 #if 0 /* #### MULTIPLE doesn't work yet */
1881 else if (VECTORP (XVECTOR (obj)->contents [0]))
1882 /* This vector is an ATOM_PAIR set */
1883 {
1884 if (NILP (type)) type = QATOM_PAIR;
1885 *size_ret = XVECTOR (obj)->size;
1886 *format_ret = 32;
1887 *data_ret = (unsigned char *)
1888 xmalloc ((*size_ret) * sizeof (Atom) * 2);
1889 for (i = 0; i < *size_ret; i++)
1890 if (VECTORP (XVECTOR (obj)->contents [i]))
1891 {
1892 Lisp_Object pair = XVECTOR (obj)->contents [i];
1893 if (XVECTOR (pair)->size != 2)
1894 signal_error (
1895 "Elements of the vector must be vectors of exactly two elements",
1896 pair);
1897
1898 (*(Atom **) data_ret) [i * 2]
1899 = symbol_to_x_atom (dpyinfo, display,
1900 XVECTOR (pair)->contents [0]);
1901 (*(Atom **) data_ret) [(i * 2) + 1]
1902 = symbol_to_x_atom (dpyinfo, display,
1903 XVECTOR (pair)->contents [1]);
1904 }
1905 else
1906 signal_error ("All elements of the vector must be of the same type",
1907 obj);
1908
1909 }
1910 #endif
1911 else
1912 /* This vector is an INTEGER set, or something like it */
1913 {
1914 int data_size = 2;
1915 *size_ret = XVECTOR (obj)->size;
1916 if (NILP (type)) type = QINTEGER;
1917 *format_ret = 16;
1918 for (i = 0; i < *size_ret; i++)
1919 if (CONSP (XVECTOR (obj)->contents [i]))
1920 *format_ret = 32;
1921 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1922 signal_error (/* Qselection_error */
1923 "Elements of selection vector must be integers or conses of integers",
1924 obj);
1925
1926 /* Use sizeof(long) even if it is more than 32 bits. See comment
1927 in x_get_window_property and x_fill_property_data. */
1928
1929 if (*format_ret == 32) data_size = sizeof(long);
1930 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1931 for (i = 0; i < *size_ret; i++)
1932 if (*format_ret == 32)
1933 (*((unsigned long **) data_ret)) [i]
1934 = cons_to_long (XVECTOR (obj)->contents [i]);
1935 else
1936 (*((unsigned short **) data_ret)) [i]
1937 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1938 }
1939 }
1940 else
1941 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1942
1943 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
1944 }
1945
1946 static Lisp_Object
1947 clean_local_selection_data (Lisp_Object obj)
1948 {
1949 if (CONSP (obj)
1950 && INTEGERP (XCAR (obj))
1951 && CONSP (XCDR (obj))
1952 && INTEGERP (XCAR (XCDR (obj)))
1953 && NILP (XCDR (XCDR (obj))))
1954 obj = Fcons (XCAR (obj), XCDR (obj));
1955
1956 if (CONSP (obj)
1957 && INTEGERP (XCAR (obj))
1958 && INTEGERP (XCDR (obj)))
1959 {
1960 if (XINT (XCAR (obj)) == 0)
1961 return XCDR (obj);
1962 if (XINT (XCAR (obj)) == -1)
1963 return make_number (- XINT (XCDR (obj)));
1964 }
1965 if (VECTORP (obj))
1966 {
1967 int i;
1968 int size = XVECTOR (obj)->size;
1969 Lisp_Object copy;
1970 if (size == 1)
1971 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1972 copy = Fmake_vector (make_number (size), Qnil);
1973 for (i = 0; i < size; i++)
1974 XVECTOR (copy)->contents [i]
1975 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1976 return copy;
1977 }
1978 return obj;
1979 }
1980 \f
1981 /* Called from XTread_socket to handle SelectionNotify events.
1982 If it's the selection we are waiting for, stop waiting
1983 by setting the car of reading_selection_reply to non-nil.
1984 We store t there if the reply is successful, lambda if not. */
1985
1986 void
1987 x_handle_selection_notify (XSelectionEvent *event)
1988 {
1989 if (event->requestor != reading_selection_window)
1990 return;
1991 if (event->selection != reading_which_selection)
1992 return;
1993
1994 TRACE0 ("Received SelectionNotify");
1995 XSETCAR (reading_selection_reply,
1996 (event->property != 0 ? Qt : Qlambda));
1997 }
1998
1999 \f
2000 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
2001 Sx_own_selection_internal, 2, 2, 0,
2002 doc: /* Assert an X selection of the given TYPE with the given VALUE.
2003 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2004 \(Those are literal upper-case symbol names, since that's what X expects.)
2005 VALUE is typically a string, or a cons of two markers, but may be
2006 anything that the functions on `selection-converter-alist' know about. */)
2007 (Lisp_Object selection_name, Lisp_Object selection_value)
2008 {
2009 check_x ();
2010 CHECK_SYMBOL (selection_name);
2011 if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil");
2012 x_own_selection (selection_name, selection_value);
2013 return selection_value;
2014 }
2015
2016
2017 /* Request the selection value from the owner. If we are the owner,
2018 simply return our selection value. If we are not the owner, this
2019 will block until all of the data has arrived. */
2020
2021 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
2022 Sx_get_selection_internal, 2, 3, 0,
2023 doc: /* Return text selected from some X window.
2024 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2025 \(Those are literal upper-case symbol names, since that's what X expects.)
2026 TYPE is the type of data desired, typically `STRING'.
2027 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2028 selections. If omitted, defaults to the time for the last event. */)
2029 (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp)
2030 {
2031 Lisp_Object val = Qnil;
2032 struct gcpro gcpro1, gcpro2;
2033 GCPRO2 (target_type, val); /* we store newly consed data into these */
2034 check_x ();
2035 CHECK_SYMBOL (selection_symbol);
2036
2037 #if 0 /* #### MULTIPLE doesn't work yet */
2038 if (CONSP (target_type)
2039 && XCAR (target_type) == QMULTIPLE)
2040 {
2041 CHECK_VECTOR (XCDR (target_type));
2042 /* So we don't destructively modify this... */
2043 target_type = copy_multiple_data (target_type);
2044 }
2045 else
2046 #endif
2047 CHECK_SYMBOL (target_type);
2048
2049 val = x_get_local_selection (selection_symbol, target_type, 1);
2050
2051 if (NILP (val))
2052 {
2053 val = x_get_foreign_selection (selection_symbol, target_type, time_stamp);
2054 goto DONE;
2055 }
2056
2057 if (CONSP (val)
2058 && SYMBOLP (XCAR (val)))
2059 {
2060 val = XCDR (val);
2061 if (CONSP (val) && NILP (XCDR (val)))
2062 val = XCAR (val);
2063 }
2064 val = clean_local_selection_data (val);
2065 DONE:
2066 UNGCPRO;
2067 return val;
2068 }
2069
2070 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2071 Sx_disown_selection_internal, 1, 2, 0,
2072 doc: /* If we own the selection SELECTION, disown it.
2073 Disowning it means there is no such selection. */)
2074 (Lisp_Object selection, Lisp_Object time)
2075 {
2076 Time timestamp;
2077 Atom selection_atom;
2078 union {
2079 struct selection_input_event sie;
2080 struct input_event ie;
2081 } event;
2082 Display *display;
2083 struct x_display_info *dpyinfo;
2084 struct frame *sf = SELECTED_FRAME ();
2085
2086 check_x ();
2087 if (! FRAME_X_P (sf))
2088 return Qnil;
2089
2090 display = FRAME_X_DISPLAY (sf);
2091 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2092 CHECK_SYMBOL (selection);
2093 if (NILP (time))
2094 timestamp = last_event_timestamp;
2095 else
2096 timestamp = cons_to_long (time);
2097
2098 if (NILP (assq_no_quit (selection, Vselection_alist)))
2099 return Qnil; /* Don't disown the selection when we're not the owner. */
2100
2101 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
2102
2103 BLOCK_INPUT;
2104 XSetSelectionOwner (display, selection_atom, None, timestamp);
2105 UNBLOCK_INPUT;
2106
2107 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2108 generated for a window which owns the selection when that window sets
2109 the selection owner to None. The NCD server does, the MIT Sun4 server
2110 doesn't. So we synthesize one; this means we might get two, but
2111 that's ok, because the second one won't have any effect. */
2112 SELECTION_EVENT_DISPLAY (&event.sie) = display;
2113 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2114 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2115 x_handle_selection_clear (&event.ie);
2116
2117 return Qt;
2118 }
2119
2120 /* Get rid of all the selections in buffer BUFFER.
2121 This is used when we kill a buffer. */
2122
2123 void
2124 x_disown_buffer_selections (Lisp_Object buffer)
2125 {
2126 Lisp_Object tail;
2127 struct buffer *buf = XBUFFER (buffer);
2128
2129 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
2130 {
2131 Lisp_Object elt, value;
2132 elt = XCAR (tail);
2133 value = XCDR (elt);
2134 if (CONSP (value) && MARKERP (XCAR (value))
2135 && XMARKER (XCAR (value))->buffer == buf)
2136 Fx_disown_selection_internal (XCAR (elt), Qnil);
2137 }
2138 }
2139
2140 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2141 0, 1, 0,
2142 doc: /* Whether the current Emacs process owns the given X Selection.
2143 The arg should be the name of the selection in question, typically one of
2144 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2145 \(Those are literal upper-case symbol names, since that's what X expects.)
2146 For convenience, the symbol nil is the same as `PRIMARY',
2147 and t is the same as `SECONDARY'. */)
2148 (Lisp_Object selection)
2149 {
2150 check_x ();
2151 CHECK_SYMBOL (selection);
2152 if (EQ (selection, Qnil)) selection = QPRIMARY;
2153 if (EQ (selection, Qt)) selection = QSECONDARY;
2154
2155 if (NILP (Fassq (selection, Vselection_alist)))
2156 return Qnil;
2157 return Qt;
2158 }
2159
2160 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2161 0, 1, 0,
2162 doc: /* Whether there is an owner for the given X Selection.
2163 The arg should be the name of the selection in question, typically one of
2164 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2165 \(Those are literal upper-case symbol names, since that's what X expects.)
2166 For convenience, the symbol nil is the same as `PRIMARY',
2167 and t is the same as `SECONDARY'. */)
2168 (Lisp_Object selection)
2169 {
2170 Window owner;
2171 Atom atom;
2172 Display *dpy;
2173 struct frame *sf = SELECTED_FRAME ();
2174
2175 /* It should be safe to call this before we have an X frame. */
2176 if (! FRAME_X_P (sf))
2177 return Qnil;
2178
2179 dpy = FRAME_X_DISPLAY (sf);
2180 CHECK_SYMBOL (selection);
2181 if (!NILP (Fx_selection_owner_p (selection)))
2182 return Qt;
2183 if (EQ (selection, Qnil)) selection = QPRIMARY;
2184 if (EQ (selection, Qt)) selection = QSECONDARY;
2185 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
2186 if (atom == 0)
2187 return Qnil;
2188 BLOCK_INPUT;
2189 owner = XGetSelectionOwner (dpy, atom);
2190 UNBLOCK_INPUT;
2191 return (owner ? Qt : Qnil);
2192 }
2193
2194 \f
2195 /***********************************************************************
2196 Drag and drop support
2197 ***********************************************************************/
2198 /* Check that lisp values are of correct type for x_fill_property_data.
2199 That is, number, string or a cons with two numbers (low and high 16
2200 bit parts of a 32 bit number). */
2201
2202 int
2203 x_check_property_data (Lisp_Object data)
2204 {
2205 Lisp_Object iter;
2206 int size = 0;
2207
2208 for (iter = data; CONSP (iter) && size != -1; iter = XCDR (iter), ++size)
2209 {
2210 Lisp_Object o = XCAR (iter);
2211
2212 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2213 size = -1;
2214 else if (CONSP (o) &&
2215 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2216 size = -1;
2217 }
2218
2219 return size;
2220 }
2221
2222 /* Convert lisp values to a C array. Values may be a number, a string
2223 which is taken as an X atom name and converted to the atom value, or
2224 a cons containing the two 16 bit parts of a 32 bit number.
2225
2226 DPY is the display use to look up X atoms.
2227 DATA is a Lisp list of values to be converted.
2228 RET is the C array that contains the converted values. It is assumed
2229 it is big enough to hold all values.
2230 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2231 be stored in RET. Note that long is used for 32 even if long is more
2232 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2233 XClientMessageEvent). */
2234
2235 void
2236 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2237 {
2238 long val;
2239 long *d32 = (long *) ret;
2240 short *d16 = (short *) ret;
2241 char *d08 = (char *) ret;
2242 Lisp_Object iter;
2243
2244 for (iter = data; CONSP (iter); iter = XCDR (iter))
2245 {
2246 Lisp_Object o = XCAR (iter);
2247
2248 if (INTEGERP (o))
2249 val = (long) XFASTINT (o);
2250 else if (FLOATP (o))
2251 val = (long) XFLOAT_DATA (o);
2252 else if (CONSP (o))
2253 val = (long) cons_to_long (o);
2254 else if (STRINGP (o))
2255 {
2256 BLOCK_INPUT;
2257 val = (long) XInternAtom (dpy, SSDATA (o), False);
2258 UNBLOCK_INPUT;
2259 }
2260 else
2261 error ("Wrong type, must be string, number or cons");
2262
2263 if (format == 8)
2264 *d08++ = (char) val;
2265 else if (format == 16)
2266 *d16++ = (short) val;
2267 else
2268 *d32++ = val;
2269 }
2270 }
2271
2272 /* Convert an array of C values to a Lisp list.
2273 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2274 DATA is a C array of values to be converted.
2275 TYPE is the type of the data. Only XA_ATOM is special, it converts
2276 each number in DATA to its corresponfing X atom as a symbol.
2277 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2278 be stored in RET.
2279 SIZE is the number of elements in DATA.
2280
2281 Important: When format is 32, data should contain an array of int,
2282 not an array of long as the X library returns. This makes a difference
2283 when sizeof(long) != sizeof(int).
2284
2285 Also see comment for selection_data_to_lisp_data above. */
2286
2287 Lisp_Object
2288 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2289 Atom type, int format, long unsigned int size)
2290 {
2291 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2292 data, size*format/8, type, format);
2293 }
2294
2295 /* Get the mouse position in frame relative coordinates. */
2296
2297 static void
2298 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2299 {
2300 Window root, dummy_window;
2301 int dummy;
2302
2303 BLOCK_INPUT;
2304
2305 XQueryPointer (FRAME_X_DISPLAY (f),
2306 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2307
2308 /* The root window which contains the pointer. */
2309 &root,
2310
2311 /* Window pointer is on, not used */
2312 &dummy_window,
2313
2314 /* The position on that root window. */
2315 x, y,
2316
2317 /* x/y in dummy_window coordinates, not used. */
2318 &dummy, &dummy,
2319
2320 /* Modifier keys and pointer buttons, about which
2321 we don't care. */
2322 (unsigned int *) &dummy);
2323
2324
2325 /* Absolute to relative. */
2326 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2327 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2328
2329 UNBLOCK_INPUT;
2330 }
2331
2332 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2333 Sx_get_atom_name, 1, 2, 0,
2334 doc: /* Return the X atom name for VALUE as a string.
2335 VALUE may be a number or a cons where the car is the upper 16 bits and
2336 the cdr is the lower 16 bits of a 32 bit value.
2337 Use the display for FRAME or the current frame if FRAME is not given or nil.
2338
2339 If the value is 0 or the atom is not known, return the empty string. */)
2340 (Lisp_Object value, Lisp_Object frame)
2341 {
2342 struct frame *f = check_x_frame (frame);
2343 char *name = 0;
2344 char empty[] = "";
2345 Lisp_Object ret = Qnil;
2346 Display *dpy = FRAME_X_DISPLAY (f);
2347 Atom atom;
2348 int had_errors;
2349
2350 if (INTEGERP (value))
2351 atom = (Atom) XUINT (value);
2352 else if (FLOATP (value))
2353 atom = (Atom) XFLOAT_DATA (value);
2354 else if (CONSP (value))
2355 atom = (Atom) cons_to_long (value);
2356 else
2357 error ("Wrong type, value must be number or cons");
2358
2359 BLOCK_INPUT;
2360 x_catch_errors (dpy);
2361 name = atom ? XGetAtomName (dpy, atom) : empty;
2362 had_errors = x_had_errors_p (dpy);
2363 x_uncatch_errors ();
2364
2365 if (!had_errors)
2366 ret = make_string (name, strlen (name));
2367
2368 if (atom && name) XFree (name);
2369 if (NILP (ret)) ret = empty_unibyte_string;
2370
2371 UNBLOCK_INPUT;
2372
2373 return ret;
2374 }
2375
2376 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2377 Sx_register_dnd_atom, 1, 2, 0,
2378 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2379 ATOM can be a symbol or a string. The ATOM is interned on the display that
2380 FRAME is on. If FRAME is nil, the selected frame is used. */)
2381 (Lisp_Object atom, Lisp_Object frame)
2382 {
2383 Atom x_atom;
2384 struct frame *f = check_x_frame (frame);
2385 size_t i;
2386 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2387
2388
2389 if (SYMBOLP (atom))
2390 x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom);
2391 else if (STRINGP (atom))
2392 {
2393 BLOCK_INPUT;
2394 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2395 UNBLOCK_INPUT;
2396 }
2397 else
2398 error ("ATOM must be a symbol or a string");
2399
2400 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2401 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2402 return Qnil;
2403
2404 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2405 {
2406 dpyinfo->x_dnd_atoms_size *= 2;
2407 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2408 sizeof (*dpyinfo->x_dnd_atoms)
2409 * dpyinfo->x_dnd_atoms_size);
2410 }
2411
2412 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2413 return Qnil;
2414 }
2415
2416 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2417
2418 int
2419 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2420 {
2421 Lisp_Object vec;
2422 Lisp_Object frame;
2423 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2424 unsigned long size = 160/event->format;
2425 int x, y;
2426 unsigned char *data = (unsigned char *) event->data.b;
2427 int idata[5];
2428 size_t i;
2429
2430 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2431 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2432
2433 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2434
2435 XSETFRAME (frame, f);
2436
2437 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2438 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2439 function expects them to be of size int (i.e. 32). So to be able to
2440 use that function, put the data in the form it expects if format is 32. */
2441
2442 if (event->format == 32 && event->format < BITS_PER_LONG)
2443 {
2444 int i;
2445 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2446 idata[i] = (int) event->data.l[i];
2447 data = (unsigned char *) idata;
2448 }
2449
2450 vec = Fmake_vector (make_number (4), Qnil);
2451 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2452 event->message_type)));
2453 ASET (vec, 1, frame);
2454 ASET (vec, 2, make_number (event->format));
2455 ASET (vec, 3, x_property_data_to_lisp (f,
2456 data,
2457 event->message_type,
2458 event->format,
2459 size));
2460
2461 mouse_position_for_drop (f, &x, &y);
2462 bufp->kind = DRAG_N_DROP_EVENT;
2463 bufp->frame_or_window = frame;
2464 bufp->timestamp = CurrentTime;
2465 bufp->x = make_number (x);
2466 bufp->y = make_number (y);
2467 bufp->arg = vec;
2468 bufp->modifiers = 0;
2469
2470 return 1;
2471 }
2472
2473 DEFUN ("x-send-client-message", Fx_send_client_event,
2474 Sx_send_client_message, 6, 6, 0,
2475 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2476
2477 For DISPLAY, specify either a frame or a display name (a string).
2478 If DISPLAY is nil, that stands for the selected frame's display.
2479 DEST may be a number, in which case it is a Window id. The value 0 may
2480 be used to send to the root window of the DISPLAY.
2481 If DEST is a cons, it is converted to a 32 bit number
2482 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2483 number is then used as a window id.
2484 If DEST is a frame the event is sent to the outer window of that frame.
2485 A value of nil means the currently selected frame.
2486 If DEST is the string "PointerWindow" the event is sent to the window that
2487 contains the pointer. If DEST is the string "InputFocus" the event is
2488 sent to the window that has the input focus.
2489 FROM is the frame sending the event. Use nil for currently selected frame.
2490 MESSAGE-TYPE is the name of an Atom as a string.
2491 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2492 bits. VALUES is a list of numbers, cons and/or strings containing the values
2493 to send. If a value is a string, it is converted to an Atom and the value of
2494 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2495 with the high 16 bits from the car and the lower 16 bit from the cdr.
2496 If more values than fits into the event is given, the excessive values
2497 are ignored. */)
2498 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2499 {
2500 struct x_display_info *dpyinfo = check_x_display_info (display);
2501
2502 CHECK_STRING (message_type);
2503 x_send_client_event(display, dest, from,
2504 XInternAtom (dpyinfo->display,
2505 SDATA (message_type),
2506 False),
2507 format, values);
2508
2509 return Qnil;
2510 }
2511
2512 void
2513 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2514 {
2515 struct x_display_info *dpyinfo = check_x_display_info (display);
2516 Window wdest;
2517 XEvent event;
2518 struct frame *f = check_x_frame (from);
2519 int to_root;
2520
2521 CHECK_NUMBER (format);
2522 CHECK_CONS (values);
2523
2524 if (x_check_property_data (values) == -1)
2525 error ("Bad data in VALUES, must be number, cons or string");
2526
2527 event.xclient.type = ClientMessage;
2528 event.xclient.format = XFASTINT (format);
2529
2530 if (event.xclient.format != 8 && event.xclient.format != 16
2531 && event.xclient.format != 32)
2532 error ("FORMAT must be one of 8, 16 or 32");
2533
2534 if (FRAMEP (dest) || NILP (dest))
2535 {
2536 struct frame *fdest = check_x_frame (dest);
2537 wdest = FRAME_OUTER_WINDOW (fdest);
2538 }
2539 else if (STRINGP (dest))
2540 {
2541 if (strcmp (SDATA (dest), "PointerWindow") == 0)
2542 wdest = PointerWindow;
2543 else if (strcmp (SDATA (dest), "InputFocus") == 0)
2544 wdest = InputFocus;
2545 else
2546 error ("DEST as a string must be one of PointerWindow or InputFocus");
2547 }
2548 else if (INTEGERP (dest))
2549 wdest = (Window) XFASTINT (dest);
2550 else if (FLOATP (dest))
2551 wdest = (Window) XFLOAT_DATA (dest);
2552 else if (CONSP (dest))
2553 {
2554 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2555 error ("Both car and cdr for DEST must be numbers");
2556 else
2557 wdest = (Window) cons_to_long (dest);
2558 }
2559 else
2560 error ("DEST must be a frame, nil, string, number or cons");
2561
2562 if (wdest == 0) wdest = dpyinfo->root_window;
2563 to_root = wdest == dpyinfo->root_window;
2564
2565 BLOCK_INPUT;
2566
2567 event.xclient.message_type = message_type;
2568 event.xclient.display = dpyinfo->display;
2569
2570 /* Some clients (metacity for example) expects sending window to be here
2571 when sending to the root window. */
2572 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2573
2574
2575 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2576 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2577 event.xclient.format);
2578
2579 /* If event mask is 0 the event is sent to the client that created
2580 the destination window. But if we are sending to the root window,
2581 there is no such client. Then we set the event mask to 0xffff. The
2582 event then goes to clients selecting for events on the root window. */
2583 x_catch_errors (dpyinfo->display);
2584 {
2585 int propagate = to_root ? False : True;
2586 unsigned mask = to_root ? 0xffff : 0;
2587 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2588 XFlush (dpyinfo->display);
2589 }
2590 x_uncatch_errors ();
2591 UNBLOCK_INPUT;
2592 }
2593
2594 \f
2595 void
2596 syms_of_xselect (void)
2597 {
2598 defsubr (&Sx_get_selection_internal);
2599 defsubr (&Sx_own_selection_internal);
2600 defsubr (&Sx_disown_selection_internal);
2601 defsubr (&Sx_selection_owner_p);
2602 defsubr (&Sx_selection_exists_p);
2603
2604 defsubr (&Sx_get_atom_name);
2605 defsubr (&Sx_send_client_message);
2606 defsubr (&Sx_register_dnd_atom);
2607
2608 reading_selection_reply = Fcons (Qnil, Qnil);
2609 staticpro (&reading_selection_reply);
2610 reading_selection_window = 0;
2611 reading_which_selection = 0;
2612
2613 property_change_wait_list = 0;
2614 prop_location_identifier = 0;
2615 property_change_reply = Fcons (Qnil, Qnil);
2616 staticpro (&property_change_reply);
2617
2618 Vselection_alist = Qnil;
2619 staticpro (&Vselection_alist);
2620
2621 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2622 doc: /* An alist associating X Windows selection-types with functions.
2623 These functions are called to convert the selection, with three args:
2624 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2625 a desired type to which the selection should be converted;
2626 and the local selection value (whatever was given to `x-own-selection').
2627
2628 The function should return the value to send to the X server
2629 \(typically a string). A return value of nil
2630 means that the conversion could not be done.
2631 A return value which is the symbol `NULL'
2632 means that a side-effect was executed,
2633 and there is no meaningful selection value. */);
2634 Vselection_converter_alist = Qnil;
2635
2636 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2637 doc: /* A list of functions to be called when Emacs loses an X selection.
2638 \(This happens when some other X client makes its own selection
2639 or when a Lisp program explicitly clears the selection.)
2640 The functions are called with one argument, the selection type
2641 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2642 Vx_lost_selection_functions = Qnil;
2643
2644 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2645 doc: /* A list of functions to be called when Emacs answers a selection request.
2646 The functions are called with four arguments:
2647 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2648 - the selection-type which Emacs was asked to convert the
2649 selection into before sending (for example, `STRING' or `LENGTH');
2650 - a flag indicating success or failure for responding to the request.
2651 We might have failed (and declined the request) for any number of reasons,
2652 including being asked for a selection that we no longer own, or being asked
2653 to convert into a type that we don't know about or that is inappropriate.
2654 This hook doesn't let you change the behavior of Emacs's selection replies,
2655 it merely informs you that they have happened. */);
2656 Vx_sent_selection_functions = Qnil;
2657
2658 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2659 doc: /* Number of milliseconds to wait for a selection reply.
2660 If the selection owner doesn't reply in this time, we give up.
2661 A value of 0 means wait as long as necessary. This is initialized from the
2662 \"*selectionTimeout\" resource. */);
2663 x_selection_timeout = 0;
2664
2665 /* QPRIMARY is defined in keyboard.c. */
2666 QSECONDARY = intern_c_string ("SECONDARY"); staticpro (&QSECONDARY);
2667 QSTRING = intern_c_string ("STRING"); staticpro (&QSTRING);
2668 QINTEGER = intern_c_string ("INTEGER"); staticpro (&QINTEGER);
2669 QCLIPBOARD = intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD);
2670 QTIMESTAMP = intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP);
2671 QTEXT = intern_c_string ("TEXT"); staticpro (&QTEXT);
2672 QCOMPOUND_TEXT = intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
2673 QUTF8_STRING = intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING);
2674 QDELETE = intern_c_string ("DELETE"); staticpro (&QDELETE);
2675 QMULTIPLE = intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE);
2676 QINCR = intern_c_string ("INCR"); staticpro (&QINCR);
2677 QEMACS_TMP = intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
2678 QTARGETS = intern_c_string ("TARGETS"); staticpro (&QTARGETS);
2679 QATOM = intern_c_string ("ATOM"); staticpro (&QATOM);
2680 QATOM_PAIR = intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
2681 QNULL = intern_c_string ("NULL"); staticpro (&QNULL);
2682 Qcompound_text_with_extensions = intern_c_string ("compound-text-with-extensions");
2683 staticpro (&Qcompound_text_with_extensions);
2684
2685 Qforeign_selection = intern_c_string ("foreign-selection");
2686 staticpro (&Qforeign_selection);
2687 }