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