Merge from trunk
[emacs.git] / src / xselect.c
blob00d70eee4777d7a20a1414c73ef50ad82e43963d
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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.
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.
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/>. */
20 /* Rewritten by jwz */
22 #include <config.h>
23 #include <stdio.h> /* termhooks.h needs this */
24 #include <setjmp.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
28 #endif
30 #include <unistd.h>
32 #include "lisp.h"
33 #include "xterm.h" /* for all of the X includes */
34 #include "dispextern.h" /* frame.h seems to want this */
35 #include "frame.h" /* Need this to get the X window of selected_frame */
36 #include "blockinput.h"
37 #include "buffer.h"
38 #include "process.h"
39 #include "termhooks.h"
40 #include "keyboard.h"
41 #include "character.h"
43 #include <X11/Xproto.h>
45 struct prop_location;
46 struct selection_data;
48 static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
49 static Atom symbol_to_x_atom (struct x_display_info *, Lisp_Object);
50 static void x_own_selection (Lisp_Object, Lisp_Object, Lisp_Object);
51 static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int,
52 struct x_display_info *);
53 static void x_decline_selection_request (struct input_event *);
54 static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
55 static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
56 static Lisp_Object x_catch_errors_unwind (Lisp_Object);
57 static void x_reply_selection_request (struct input_event *, struct x_display_info *);
58 static int x_convert_selection (struct input_event *, Lisp_Object, Lisp_Object,
59 Atom, int, struct x_display_info *);
60 static int waiting_for_other_props_on_window (Display *, Window);
61 static struct prop_location *expect_property_change (Display *, Window,
62 Atom, int);
63 static void unexpect_property_change (struct prop_location *);
64 static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
65 static void wait_for_property_change (struct prop_location *);
66 static Lisp_Object x_get_foreign_selection (Lisp_Object, Lisp_Object,
67 Lisp_Object, Lisp_Object);
68 static void x_get_window_property (Display *, Window, Atom,
69 unsigned char **, int *,
70 Atom *, int *, unsigned long *, int);
71 static void receive_incremental_selection (Display *, Window, Atom,
72 Lisp_Object, unsigned,
73 unsigned char **, int *,
74 Atom *, int *, unsigned long *);
75 static Lisp_Object x_get_window_property_as_lisp_data (Display *,
76 Window, Atom,
77 Lisp_Object, Atom);
78 static Lisp_Object selection_data_to_lisp_data (Display *,
79 const unsigned char *,
80 int, Atom, int);
81 static void lisp_data_to_selection_data (Display *, Lisp_Object,
82 unsigned char **, Atom *,
83 unsigned *, int *, int *);
84 static Lisp_Object clean_local_selection_data (Lisp_Object);
86 /* Printing traces to stderr. */
88 #ifdef TRACE_SELECTION
89 #define TRACE0(fmt) \
90 fprintf (stderr, "%d: " fmt "\n", getpid ())
91 #define TRACE1(fmt, a0) \
92 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
93 #define TRACE2(fmt, a0, a1) \
94 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
95 #define TRACE3(fmt, a0, a1, a2) \
96 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
97 #else
98 #define TRACE0(fmt) (void) 0
99 #define TRACE1(fmt, a0) (void) 0
100 #define TRACE2(fmt, a0, a1) (void) 0
101 #endif
104 static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
105 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
106 QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS;
108 static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
109 static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
111 static Lisp_Object Qcompound_text_with_extensions;
113 static Lisp_Object Qforeign_selection;
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
123 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
125 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
126 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
129 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
130 handling. */
132 struct selection_event_queue
134 struct input_event event;
135 struct selection_event_queue *next;
138 static struct selection_event_queue *selection_queue;
140 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
142 static int x_queue_selection_requests;
144 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
146 static void
147 x_queue_event (struct input_event *event)
149 struct selection_event_queue *queue_tmp;
151 /* Don't queue repeated requests.
152 This only happens for large requests which uses the incremental protocol. */
153 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
155 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
157 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
158 x_decline_selection_request (event);
159 return;
163 queue_tmp
164 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
166 if (queue_tmp != NULL)
168 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
169 queue_tmp->event = *event;
170 queue_tmp->next = selection_queue;
171 selection_queue = queue_tmp;
175 /* Start queuing SELECTION_REQUEST_EVENT events. */
177 static void
178 x_start_queuing_selection_requests (void)
180 if (x_queue_selection_requests)
181 abort ();
183 x_queue_selection_requests++;
184 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
187 /* Stop queuing SELECTION_REQUEST_EVENT events. */
189 static void
190 x_stop_queuing_selection_requests (void)
192 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
193 --x_queue_selection_requests;
195 /* Take all the queued events and put them back
196 so that they get processed afresh. */
198 while (selection_queue != NULL)
200 struct selection_event_queue *queue_tmp = selection_queue;
201 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
202 kbd_buffer_unget_event (&queue_tmp->event);
203 selection_queue = queue_tmp->next;
204 xfree ((char *)queue_tmp);
209 /* This converts a Lisp symbol to a server Atom, avoiding a server
210 roundtrip whenever possible. */
212 static Atom
213 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
215 Atom val;
216 if (NILP (sym)) return 0;
217 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
218 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
219 if (EQ (sym, QSTRING)) return XA_STRING;
220 if (EQ (sym, QINTEGER)) return XA_INTEGER;
221 if (EQ (sym, QATOM)) return XA_ATOM;
222 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
223 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
224 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
225 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
226 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
227 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
228 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
229 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
230 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
231 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
232 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
233 if (!SYMBOLP (sym)) abort ();
235 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
236 BLOCK_INPUT;
237 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
238 UNBLOCK_INPUT;
239 return val;
243 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
244 and calls to intern whenever possible. */
246 static Lisp_Object
247 x_atom_to_symbol (Display *dpy, Atom atom)
249 struct x_display_info *dpyinfo;
250 char *str;
251 Lisp_Object val;
253 if (! atom)
254 return Qnil;
256 switch (atom)
258 case XA_PRIMARY:
259 return QPRIMARY;
260 case XA_SECONDARY:
261 return QSECONDARY;
262 case XA_STRING:
263 return QSTRING;
264 case XA_INTEGER:
265 return QINTEGER;
266 case XA_ATOM:
267 return QATOM;
270 dpyinfo = x_display_info_for_display (dpy);
271 if (dpyinfo == NULL)
272 return Qnil;
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;
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;
309 /* Do protocol to assert ourself as a selection owner.
310 FRAME shall be the owner; it must be a valid X frame.
311 Update the Vselection_alist so that we can reply to later requests for
312 our selection. */
314 static void
315 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
316 Lisp_Object frame)
318 struct frame *f = XFRAME (frame);
319 Window selecting_window = FRAME_X_WINDOW (f);
320 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
321 Display *display = dpyinfo->display;
322 Time timestamp = last_event_timestamp;
323 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
325 BLOCK_INPUT;
326 x_catch_errors (display);
327 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
328 x_check_errors (display, "Can't set selection: %s");
329 x_uncatch_errors ();
330 UNBLOCK_INPUT;
332 /* Now update the local cache */
334 Lisp_Object selection_data;
335 Lisp_Object prev_value;
337 selection_data = list4 (selection_name, selection_value,
338 long_to_cons (timestamp), frame);
339 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
341 dpyinfo->terminal->Vselection_alist
342 = Fcons (selection_data, dpyinfo->terminal->Vselection_alist);
344 /* If we already owned the selection, remove the old selection
345 data. Don't use Fdelq as that may QUIT. */
346 if (!NILP (prev_value))
348 /* We know it's not the CAR, so it's easy. */
349 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
350 for (; CONSP (rest); rest = XCDR (rest))
351 if (EQ (prev_value, Fcar (XCDR (rest))))
353 XSETCDR (rest, XCDR (XCDR (rest)));
354 break;
360 /* Given a selection-name and desired type, look up our local copy of
361 the selection value and convert it to the type.
362 The value is nil or a string.
363 This function is used both for remote requests (LOCAL_REQUEST is zero)
364 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
366 This calls random Lisp code, and may signal or gc. */
368 static Lisp_Object
369 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
370 int local_request, struct x_display_info *dpyinfo)
372 Lisp_Object local_value;
373 Lisp_Object handler_fn, value, check;
374 int count;
376 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
378 if (NILP (local_value)) return Qnil;
380 /* TIMESTAMP is a special case. */
381 if (EQ (target_type, QTIMESTAMP))
383 handler_fn = Qnil;
384 value = XCAR (XCDR (XCDR (local_value)));
386 else
388 /* Don't allow a quit within the converter.
389 When the user types C-g, he would be surprised
390 if by luck it came during a converter. */
391 count = SPECPDL_INDEX ();
392 specbind (Qinhibit_quit, Qt);
394 CHECK_SYMBOL (target_type);
395 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
396 /* gcpro is not needed here since nothing but HANDLER_FN
397 is live, and that ought to be a symbol. */
399 if (!NILP (handler_fn))
400 value = call3 (handler_fn,
401 selection_symbol, (local_request ? Qnil : target_type),
402 XCAR (XCDR (local_value)));
403 else
404 value = Qnil;
405 unbind_to (count, Qnil);
408 /* Make sure this value is of a type that we could transmit
409 to another X client. */
411 check = value;
412 if (CONSP (value)
413 && SYMBOLP (XCAR (value)))
414 check = XCDR (value);
416 if (STRINGP (check)
417 || VECTORP (check)
418 || SYMBOLP (check)
419 || INTEGERP (check)
420 || NILP (value))
421 return value;
422 /* Check for a value that cons_to_long could handle. */
423 else if (CONSP (check)
424 && INTEGERP (XCAR (check))
425 && (INTEGERP (XCDR (check))
427 (CONSP (XCDR (check))
428 && INTEGERP (XCAR (XCDR (check)))
429 && NILP (XCDR (XCDR (check))))))
430 return value;
432 signal_error ("Invalid data returned by selection-conversion function",
433 list2 (handler_fn, value));
436 /* Subroutines of x_reply_selection_request. */
438 /* Send a SelectionNotify event to the requestor with property=None,
439 meaning we were unable to do what they wanted. */
441 static void
442 x_decline_selection_request (struct input_event *event)
444 XEvent reply_base;
445 XSelectionEvent *reply = &(reply_base.xselection);
447 reply->type = SelectionNotify;
448 reply->display = SELECTION_EVENT_DISPLAY (event);
449 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
450 reply->selection = SELECTION_EVENT_SELECTION (event);
451 reply->time = SELECTION_EVENT_TIME (event);
452 reply->target = SELECTION_EVENT_TARGET (event);
453 reply->property = None;
455 /* The reason for the error may be that the receiver has
456 died in the meantime. Handle that case. */
457 BLOCK_INPUT;
458 x_catch_errors (reply->display);
459 XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base);
460 XFlush (reply->display);
461 x_uncatch_errors ();
462 UNBLOCK_INPUT;
465 /* This is the selection request currently being processed.
466 It is set to zero when the request is fully processed. */
467 static struct input_event *x_selection_current_request;
469 /* Display info in x_selection_request. */
471 static struct x_display_info *selection_request_dpyinfo;
473 /* Raw selection data, for sending to a requestor window. */
475 struct selection_data
477 unsigned char *data;
478 unsigned int size;
479 int format;
480 Atom type;
481 int nofree;
482 Atom property;
483 /* This can be set to non-NULL during x_reply_selection_request, if
484 the selection is waiting for an INCR transfer to complete. Don't
485 free these; that's done by unexpect_property_change. */
486 struct prop_location *wait_object;
487 struct selection_data *next;
490 /* Linked list of the above (in support of MULTIPLE targets). */
492 static struct selection_data *converted_selections;
494 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
495 static Atom conversion_fail_tag;
497 /* Used as an unwind-protect clause so that, if a selection-converter signals
498 an error, we tell the requester that we were unable to do what they wanted
499 before we throw to top-level or go into the debugger or whatever. */
501 static Lisp_Object
502 x_selection_request_lisp_error (Lisp_Object ignore)
504 struct selection_data *cs, *next;
506 for (cs = converted_selections; cs; cs = next)
508 next = cs->next;
509 if (cs->nofree == 0 && cs->data)
510 xfree (cs->data);
511 xfree (cs);
513 converted_selections = NULL;
515 if (x_selection_current_request != 0
516 && selection_request_dpyinfo->display)
517 x_decline_selection_request (x_selection_current_request);
518 return Qnil;
521 static Lisp_Object
522 x_catch_errors_unwind (Lisp_Object dummy)
524 BLOCK_INPUT;
525 x_uncatch_errors ();
526 UNBLOCK_INPUT;
527 return Qnil;
531 /* This stuff is so that INCR selections are reentrant (that is, so we can
532 be servicing multiple INCR selection requests simultaneously.) I haven't
533 actually tested that yet. */
535 /* Keep a list of the property changes that are awaited. */
537 struct prop_location
539 int identifier;
540 Display *display;
541 Window window;
542 Atom property;
543 int desired_state;
544 int arrived;
545 struct prop_location *next;
548 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
549 static void wait_for_property_change (struct prop_location *location);
550 static void unexpect_property_change (struct prop_location *location);
551 static int waiting_for_other_props_on_window (Display *display, Window window);
553 static int prop_location_identifier;
555 static Lisp_Object property_change_reply;
557 static struct prop_location *property_change_reply_object;
559 static struct prop_location *property_change_wait_list;
561 static Lisp_Object
562 queue_selection_requests_unwind (Lisp_Object tem)
564 x_stop_queuing_selection_requests ();
565 return Qnil;
569 /* Send the reply to a selection request event EVENT. */
571 #ifdef TRACE_SELECTION
572 static int x_reply_selection_request_cnt;
573 #endif /* TRACE_SELECTION */
575 static void
576 x_reply_selection_request (struct input_event *event, struct x_display_info *dpyinfo)
578 XEvent reply_base;
579 XSelectionEvent *reply = &(reply_base.xselection);
580 Display *display = SELECTION_EVENT_DISPLAY (event);
581 Window window = SELECTION_EVENT_REQUESTOR (event);
582 int bytes_remaining;
583 int max_bytes = SELECTION_QUANTUM (display);
584 int count = SPECPDL_INDEX ();
585 struct selection_data *cs;
587 if (max_bytes > MAX_SELECTION_QUANTUM)
588 max_bytes = MAX_SELECTION_QUANTUM;
590 reply->type = SelectionNotify;
591 reply->display = display;
592 reply->requestor = window;
593 reply->selection = SELECTION_EVENT_SELECTION (event);
594 reply->time = SELECTION_EVENT_TIME (event);
595 reply->target = SELECTION_EVENT_TARGET (event);
596 reply->property = SELECTION_EVENT_PROPERTY (event);
597 if (reply->property == None)
598 reply->property = reply->target;
600 BLOCK_INPUT;
601 /* The protected block contains wait_for_property_change, which can
602 run random lisp code (process handlers) or signal. Therefore, we
603 put the x_uncatch_errors call in an unwind. */
604 record_unwind_protect (x_catch_errors_unwind, Qnil);
605 x_catch_errors (display);
607 /* Loop over converted selections, storing them in the requested
608 properties. If data is large, only store the first N bytes
609 (section 2.7.2 of ICCCM). Note that we store the data for a
610 MULTIPLE request in the opposite order; the ICCM says only that
611 the conversion itself must be done in the same order. */
612 for (cs = converted_selections; cs; cs = cs->next)
614 if (cs->property == None)
615 continue;
617 bytes_remaining = cs->size * (cs->format / 8);
618 if (bytes_remaining <= max_bytes)
620 /* Send all the data at once, with minimal handshaking. */
621 TRACE1 ("Sending all %d bytes", bytes_remaining);
622 XChangeProperty (display, window, cs->property,
623 cs->type, cs->format, PropModeReplace,
624 cs->data, cs->size);
626 else
628 /* Send an INCR tag to initiate incremental transfer. */
629 long value[1];
631 TRACE2 ("Start sending %d bytes incrementally (%s)",
632 bytes_remaining, XGetAtomName (display, cs->property));
633 cs->wait_object
634 = expect_property_change (display, window, cs->property,
635 PropertyDelete);
637 /* XChangeProperty expects an array of long even if long is
638 more than 32 bits. */
639 value[0] = bytes_remaining;
640 XChangeProperty (display, window, cs->property,
641 dpyinfo->Xatom_INCR, 32, PropModeReplace,
642 (unsigned char *) value, 1);
643 XSelectInput (display, window, PropertyChangeMask);
647 /* Now issue the SelectionNotify event. */
648 XSendEvent (display, window, False, 0L, &reply_base);
649 XFlush (display);
651 #ifdef TRACE_SELECTION
653 char *sel = XGetAtomName (display, reply->selection);
654 char *tgt = XGetAtomName (display, reply->target);
655 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
656 sel, tgt, ++x_reply_selection_request_cnt);
657 if (sel) XFree (sel);
658 if (tgt) XFree (tgt);
660 #endif /* TRACE_SELECTION */
662 /* Finish sending the rest of each of the INCR values. This should
663 be improved; there's a chance of deadlock if more than one
664 subtarget in a MULTIPLE selection requires an INCR transfer, and
665 the requestor and Emacs loop waiting on different transfers. */
666 for (cs = converted_selections; cs; cs = cs->next)
667 if (cs->wait_object)
669 int format_bytes = cs->format / 8;
670 int had_errors = x_had_errors_p (display);
671 UNBLOCK_INPUT;
673 bytes_remaining = cs->size * format_bytes;
675 /* Wait for the requester to ack by deleting the property.
676 This can run Lisp code (process handlers) or signal. */
677 if (! had_errors)
679 TRACE1 ("Waiting for ACK (deletion of %s)",
680 XGetAtomName (display, cs->property));
681 wait_for_property_change (cs->wait_object);
683 else
684 unexpect_property_change (cs->wait_object);
686 while (bytes_remaining)
688 int i = ((bytes_remaining < max_bytes)
689 ? bytes_remaining
690 : max_bytes) / format_bytes;
691 BLOCK_INPUT;
693 cs->wait_object
694 = expect_property_change (display, window, cs->property,
695 PropertyDelete);
697 TRACE1 ("Sending increment of %d elements", i);
698 TRACE1 ("Set %s to increment data",
699 XGetAtomName (display, cs->property));
701 /* Append the next chunk of data to the property. */
702 XChangeProperty (display, window, cs->property,
703 cs->type, cs->format, PropModeAppend,
704 cs->data, i);
705 bytes_remaining -= i * format_bytes;
706 cs->data += i * ((cs->format == 32) ? sizeof (long)
707 : format_bytes);
708 XFlush (display);
709 had_errors = x_had_errors_p (display);
710 UNBLOCK_INPUT;
712 if (had_errors) break;
714 /* Wait for the requester to ack this chunk by deleting
715 the property. This can run Lisp code or signal. */
716 TRACE1 ("Waiting for increment ACK (deletion of %s)",
717 XGetAtomName (display, cs->property));
718 wait_for_property_change (cs->wait_object);
721 /* Now write a zero-length chunk to the property to tell the
722 requester that we're done. */
723 BLOCK_INPUT;
724 if (! waiting_for_other_props_on_window (display, window))
725 XSelectInput (display, window, 0L);
727 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
728 XGetAtomName (display, cs->property));
729 XChangeProperty (display, window, cs->property,
730 cs->type, cs->format, PropModeReplace,
731 cs->data, 0);
732 TRACE0 ("Done sending incrementally");
735 /* rms, 2003-01-03: I think I have fixed this bug. */
736 /* The window we're communicating with may have been deleted
737 in the meantime (that's a real situation from a bug report).
738 In this case, there may be events in the event queue still
739 refering to the deleted window, and we'll get a BadWindow error
740 in XTread_socket when processing the events. I don't have
741 an idea how to fix that. gerd, 2001-01-98. */
742 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
743 delivered before uncatch errors. */
744 XSync (display, False);
745 UNBLOCK_INPUT;
747 /* GTK queues events in addition to the queue in Xlib. So we
748 UNBLOCK to enter the event loop and get possible errors delivered,
749 and then BLOCK again because x_uncatch_errors requires it. */
750 BLOCK_INPUT;
751 /* This calls x_uncatch_errors. */
752 unbind_to (count, Qnil);
753 UNBLOCK_INPUT;
756 /* Handle a SelectionRequest event EVENT.
757 This is called from keyboard.c when such an event is found in the queue. */
759 static void
760 x_handle_selection_request (struct input_event *event)
762 struct gcpro gcpro1, gcpro2;
763 Time local_selection_time;
765 Display *display = SELECTION_EVENT_DISPLAY (event);
766 struct x_display_info *dpyinfo = x_display_info_for_display (display);
767 Atom selection = SELECTION_EVENT_SELECTION (event);
768 Lisp_Object selection_symbol = x_atom_to_symbol (display, selection);
769 Atom target = SELECTION_EVENT_TARGET (event);
770 Lisp_Object target_symbol = x_atom_to_symbol (display, target);
771 Atom property = SELECTION_EVENT_PROPERTY (event);
772 Lisp_Object local_selection_data;
773 int success = 0;
774 int count = SPECPDL_INDEX ();
775 GCPRO2 (local_selection_data, target_symbol);
777 if (!dpyinfo) goto DONE;
779 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
781 /* Decline if we don't own any selections. */
782 if (NILP (local_selection_data)) goto DONE;
784 /* Decline requests issued prior to our acquiring the selection. */
785 local_selection_time
786 = (Time) cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
787 if (SELECTION_EVENT_TIME (event) != CurrentTime
788 && local_selection_time > SELECTION_EVENT_TIME (event))
789 goto DONE;
791 x_selection_current_request = event;
792 selection_request_dpyinfo = dpyinfo;
793 record_unwind_protect (x_selection_request_lisp_error, Qnil);
795 /* We might be able to handle nested x_handle_selection_requests,
796 but this is difficult to test, and seems unimportant. */
797 x_start_queuing_selection_requests ();
798 record_unwind_protect (queue_selection_requests_unwind, Qnil);
800 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
801 SDATA (SYMBOL_NAME (selection_symbol)),
802 SDATA (SYMBOL_NAME (target_symbol)));
804 if (EQ (target_symbol, QMULTIPLE))
806 /* For MULTIPLE targets, the event property names a list of atom
807 pairs; the first atom names a target and the second names a
808 non-None property. */
809 Window requestor = SELECTION_EVENT_REQUESTOR (event);
810 Lisp_Object multprop;
811 int j, nselections;
813 if (property == None) goto DONE;
814 multprop
815 = x_get_window_property_as_lisp_data (display, requestor, property,
816 QMULTIPLE, selection);
818 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
819 goto DONE;
821 nselections = ASIZE (multprop) / 2;
822 /* Perform conversions. This can signal. */
823 for (j = 0; j < nselections; j++)
825 Lisp_Object subtarget = AREF (multprop, 2*j);
826 Atom subproperty = symbol_to_x_atom (dpyinfo,
827 AREF (multprop, 2*j+1));
829 if (subproperty != None)
830 x_convert_selection (event, selection_symbol, subtarget,
831 subproperty, 1, dpyinfo);
833 success = 1;
835 else
837 if (property == None)
838 property = SELECTION_EVENT_TARGET (event);
839 success = x_convert_selection (event, selection_symbol,
840 target_symbol, property,
841 0, dpyinfo);
844 DONE:
846 if (success)
847 x_reply_selection_request (event, dpyinfo);
848 else
849 x_decline_selection_request (event);
850 x_selection_current_request = 0;
852 /* Run the `x-sent-selection-functions' abnormal hook. */
853 if (!NILP (Vx_sent_selection_functions)
854 && !EQ (Vx_sent_selection_functions, Qunbound))
856 Lisp_Object args[4];
857 args[0] = Vx_sent_selection_functions;
858 args[1] = selection_symbol;
859 args[2] = target_symbol;
860 args[3] = success ? Qt : Qnil;
861 Frun_hook_with_args (4, args);
864 unbind_to (count, Qnil);
865 UNGCPRO;
868 /* Perform the requested selection conversion, and write the data to
869 the converted_selections linked list, where it can be accessed by
870 x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out
871 the data even if conversion fails, using conversion_fail_tag.
873 Return 0 if the selection failed to convert, 1 otherwise. */
875 static int
876 x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
877 Lisp_Object target_symbol, Atom property,
878 int for_multiple, struct x_display_info *dpyinfo)
880 struct gcpro gcpro1;
881 Lisp_Object lisp_selection;
882 struct selection_data *cs;
883 GCPRO1 (lisp_selection);
885 lisp_selection
886 = x_get_local_selection (selection_symbol, target_symbol,
887 0, dpyinfo);
889 /* A nil return value means we can't perform the conversion. */
890 if (NILP (lisp_selection)
891 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
893 if (for_multiple)
895 cs = xmalloc (sizeof (struct selection_data));
896 cs->data = (unsigned char *) &conversion_fail_tag;
897 cs->size = 1;
898 cs->format = 32;
899 cs->type = XA_ATOM;
900 cs->nofree = 1;
901 cs->property = property;
902 cs->wait_object = NULL;
903 cs->next = converted_selections;
904 converted_selections = cs;
907 UNGCPRO;
908 return 0;
911 /* Otherwise, record the converted selection to binary. */
912 cs = xmalloc (sizeof (struct selection_data));
913 cs->nofree = 1;
914 cs->property = property;
915 cs->wait_object = NULL;
916 cs->next = converted_selections;
917 converted_selections = cs;
918 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
919 lisp_selection,
920 &(cs->data), &(cs->type),
921 &(cs->size), &(cs->format),
922 &(cs->nofree));
923 UNGCPRO;
924 return 1;
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. */
931 static void
932 x_handle_selection_clear (struct input_event *event)
934 Display *display = SELECTION_EVENT_DISPLAY (event);
935 Atom selection = SELECTION_EVENT_SELECTION (event);
936 Time changed_owner_time = SELECTION_EVENT_TIME (event);
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 Lisp_Object Vselection_alist;
943 TRACE0 ("x_handle_selection_clear");
945 if (!dpyinfo) return;
947 selection_symbol = x_atom_to_symbol (display, selection);
948 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
950 /* Well, we already believe that we don't own it, so that's just fine. */
951 if (NILP (local_selection_data)) return;
953 local_selection_time = (Time)
954 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
956 /* We have reasserted the selection since this SelectionClear was
957 generated, so we can disregard it. */
958 if (changed_owner_time != CurrentTime
959 && local_selection_time > changed_owner_time)
960 return;
962 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
963 Vselection_alist = dpyinfo->terminal->Vselection_alist;
964 if (EQ (local_selection_data, CAR (Vselection_alist)))
965 Vselection_alist = XCDR (Vselection_alist);
966 else
968 Lisp_Object rest;
969 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
970 if (EQ (local_selection_data, CAR (XCDR (rest))))
972 XSETCDR (rest, XCDR (XCDR (rest)));
973 break;
976 dpyinfo->terminal->Vselection_alist = Vselection_alist;
978 /* Run the `x-lost-selection-functions' abnormal hook. */
980 Lisp_Object args[2];
981 args[0] = Vx_lost_selection_functions;
982 args[1] = selection_symbol;
983 Frun_hook_with_args (2, args);
986 prepare_menu_bars ();
987 redisplay_preserve_echo_area (20);
990 void
991 x_handle_selection_event (struct input_event *event)
993 TRACE0 ("x_handle_selection_event");
994 if (event->kind != SELECTION_REQUEST_EVENT)
995 x_handle_selection_clear (event);
996 else if (x_queue_selection_requests)
997 x_queue_event (event);
998 else
999 x_handle_selection_request (event);
1003 /* Clear all selections that were made from frame F.
1004 We do this when about to delete a frame. */
1006 void
1007 x_clear_frame_selections (FRAME_PTR f)
1009 Lisp_Object frame;
1010 Lisp_Object rest;
1011 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1012 struct terminal *t = dpyinfo->terminal;
1014 XSETFRAME (frame, f);
1016 /* Delete elements from the beginning of Vselection_alist. */
1017 while (CONSP (t->Vselection_alist)
1018 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
1020 /* Run the `x-lost-selection-functions' abnormal hook. */
1021 Lisp_Object args[2];
1022 args[0] = Vx_lost_selection_functions;
1023 args[1] = Fcar (Fcar (t->Vselection_alist));
1024 Frun_hook_with_args (2, args);
1026 t->Vselection_alist = XCDR (t->Vselection_alist);
1029 /* Delete elements after the beginning of Vselection_alist. */
1030 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
1031 if (CONSP (XCDR (rest))
1032 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
1034 Lisp_Object args[2];
1035 args[0] = Vx_lost_selection_functions;
1036 args[1] = XCAR (XCAR (XCDR (rest)));
1037 Frun_hook_with_args (2, args);
1038 XSETCDR (rest, XCDR (XCDR (rest)));
1039 break;
1043 /* Nonzero if any properties for DISPLAY and WINDOW
1044 are on the list of what we are waiting for. */
1046 static int
1047 waiting_for_other_props_on_window (Display *display, Window window)
1049 struct prop_location *rest = property_change_wait_list;
1050 while (rest)
1051 if (rest->display == display && rest->window == window)
1052 return 1;
1053 else
1054 rest = rest->next;
1055 return 0;
1058 /* Add an entry to the list of property changes we are waiting for.
1059 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1060 The return value is a number that uniquely identifies
1061 this awaited property change. */
1063 static struct prop_location *
1064 expect_property_change (Display *display, Window window, Atom property, int state)
1066 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1067 pl->identifier = ++prop_location_identifier;
1068 pl->display = display;
1069 pl->window = window;
1070 pl->property = property;
1071 pl->desired_state = state;
1072 pl->next = property_change_wait_list;
1073 pl->arrived = 0;
1074 property_change_wait_list = pl;
1075 return pl;
1078 /* Delete an entry from the list of property changes we are waiting for.
1079 IDENTIFIER is the number that uniquely identifies the entry. */
1081 static void
1082 unexpect_property_change (struct prop_location *location)
1084 struct prop_location *prev = 0, *rest = property_change_wait_list;
1085 while (rest)
1087 if (rest == location)
1089 if (prev)
1090 prev->next = rest->next;
1091 else
1092 property_change_wait_list = rest->next;
1093 xfree (rest);
1094 return;
1096 prev = rest;
1097 rest = rest->next;
1101 /* Remove the property change expectation element for IDENTIFIER. */
1103 static Lisp_Object
1104 wait_for_property_change_unwind (Lisp_Object loc)
1106 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1108 unexpect_property_change (location);
1109 if (location == property_change_reply_object)
1110 property_change_reply_object = 0;
1111 return Qnil;
1114 /* Actually wait for a property change.
1115 IDENTIFIER should be the value that expect_property_change returned. */
1117 static void
1118 wait_for_property_change (struct prop_location *location)
1120 int secs, usecs;
1121 int count = SPECPDL_INDEX ();
1123 if (property_change_reply_object)
1124 abort ();
1126 /* Make sure to do unexpect_property_change if we quit or err. */
1127 record_unwind_protect (wait_for_property_change_unwind,
1128 make_save_value (location, 0));
1130 XSETCAR (property_change_reply, Qnil);
1131 property_change_reply_object = location;
1133 /* If the event we are waiting for arrives beyond here, it will set
1134 property_change_reply, because property_change_reply_object says so. */
1135 if (! location->arrived)
1137 secs = x_selection_timeout / 1000;
1138 usecs = (x_selection_timeout % 1000) * 1000;
1139 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1140 wait_reading_process_output (secs, usecs, 0, 0,
1141 property_change_reply, NULL, 0);
1143 if (NILP (XCAR (property_change_reply)))
1145 TRACE0 (" Timed out");
1146 error ("Timed out waiting for property-notify event");
1150 unbind_to (count, Qnil);
1153 /* Called from XTread_socket in response to a PropertyNotify event. */
1155 void
1156 x_handle_property_notify (XPropertyEvent *event)
1158 struct prop_location *rest;
1160 for (rest = property_change_wait_list; rest; rest = rest->next)
1162 if (!rest->arrived
1163 && rest->property == event->atom
1164 && rest->window == event->window
1165 && rest->display == event->display
1166 && rest->desired_state == event->state)
1168 TRACE2 ("Expected %s of property %s",
1169 (event->state == PropertyDelete ? "deletion" : "change"),
1170 XGetAtomName (event->display, event->atom));
1172 rest->arrived = 1;
1174 /* If this is the one wait_for_property_change is waiting for,
1175 tell it to wake up. */
1176 if (rest == property_change_reply_object)
1177 XSETCAR (property_change_reply, Qt);
1179 return;
1186 /* Variables for communication with x_handle_selection_notify. */
1187 static Atom reading_which_selection;
1188 static Lisp_Object reading_selection_reply;
1189 static Window reading_selection_window;
1191 /* Do protocol to read selection-data from the server.
1192 Converts this to Lisp data and returns it.
1193 FRAME is the frame whose X window shall request the selection. */
1195 static Lisp_Object
1196 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1197 Lisp_Object time_stamp, Lisp_Object frame)
1199 struct frame *f = XFRAME (frame);
1200 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1201 Display *display = dpyinfo->display;
1202 Window requestor_window = FRAME_X_WINDOW (f);
1203 Time requestor_time = last_event_timestamp;
1204 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1205 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1206 Atom type_atom = (CONSP (target_type)
1207 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1208 : symbol_to_x_atom (dpyinfo, target_type));
1209 int secs, usecs;
1211 if (!FRAME_LIVE_P (f))
1212 return Qnil;
1214 if (! NILP (time_stamp))
1216 if (CONSP (time_stamp))
1217 requestor_time = (Time) cons_to_long (time_stamp);
1218 else if (INTEGERP (time_stamp))
1219 requestor_time = (Time) XUINT (time_stamp);
1220 else if (FLOATP (time_stamp))
1221 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1222 else
1223 error ("TIME_STAMP must be cons or number");
1226 BLOCK_INPUT;
1227 TRACE2 ("Get selection %s, type %s",
1228 XGetAtomName (display, type_atom),
1229 XGetAtomName (display, target_property));
1231 x_catch_errors (display);
1232 XConvertSelection (display, selection_atom, type_atom, target_property,
1233 requestor_window, requestor_time);
1234 x_check_errors (display, "Can't convert selection: %s");
1235 x_uncatch_errors ();
1237 /* Prepare to block until the reply has been read. */
1238 reading_selection_window = requestor_window;
1239 reading_which_selection = selection_atom;
1240 XSETCAR (reading_selection_reply, Qnil);
1242 /* It should not be necessary to stop handling selection requests
1243 during this time. In fact, the SAVE_TARGETS mechanism requires
1244 us to handle a clipboard manager's requests before it returns
1245 SelectionNotify. */
1246 #if 0
1247 x_start_queuing_selection_requests ();
1248 record_unwind_protect (queue_selection_requests_unwind, Qnil);
1249 #endif
1251 UNBLOCK_INPUT;
1253 /* This allows quits. Also, don't wait forever. */
1254 secs = x_selection_timeout / 1000;
1255 usecs = (x_selection_timeout % 1000) * 1000;
1256 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1257 wait_reading_process_output (secs, usecs, 0, 0,
1258 reading_selection_reply, NULL, 0);
1259 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1261 if (NILP (XCAR (reading_selection_reply)))
1262 error ("Timed out waiting for reply from selection owner");
1263 if (EQ (XCAR (reading_selection_reply), Qlambda))
1264 return Qnil;
1266 /* Otherwise, the selection is waiting for us on the requested property. */
1267 return
1268 x_get_window_property_as_lisp_data (display, requestor_window,
1269 target_property, target_type,
1270 selection_atom);
1273 /* Subroutines of x_get_window_property_as_lisp_data */
1275 /* Use xfree, not XFree, to free the data obtained with this function. */
1277 static void
1278 x_get_window_property (Display *display, Window window, Atom property,
1279 unsigned char **data_ret, int *bytes_ret,
1280 Atom *actual_type_ret, int *actual_format_ret,
1281 unsigned long *actual_size_ret, int delete_p)
1283 int total_size;
1284 unsigned long bytes_remaining;
1285 int offset = 0;
1286 unsigned char *tmp_data = 0;
1287 int result;
1288 int buffer_size = SELECTION_QUANTUM (display);
1290 if (buffer_size > MAX_SELECTION_QUANTUM)
1291 buffer_size = MAX_SELECTION_QUANTUM;
1293 BLOCK_INPUT;
1295 /* First probe the thing to find out how big it is. */
1296 result = XGetWindowProperty (display, window, property,
1297 0L, 0L, False, AnyPropertyType,
1298 actual_type_ret, actual_format_ret,
1299 actual_size_ret,
1300 &bytes_remaining, &tmp_data);
1301 if (result != Success)
1303 UNBLOCK_INPUT;
1304 *data_ret = 0;
1305 *bytes_ret = 0;
1306 return;
1309 /* This was allocated by Xlib, so use XFree. */
1310 XFree ((char *) tmp_data);
1312 if (*actual_type_ret == None || *actual_format_ret == 0)
1314 UNBLOCK_INPUT;
1315 return;
1318 total_size = bytes_remaining + 1;
1319 *data_ret = (unsigned char *) xmalloc (total_size);
1321 /* Now read, until we've gotten it all. */
1322 while (bytes_remaining)
1324 #ifdef TRACE_SELECTION
1325 unsigned long last = bytes_remaining;
1326 #endif
1327 result
1328 = XGetWindowProperty (display, window, property,
1329 (long)offset/4, (long)buffer_size/4,
1330 False,
1331 AnyPropertyType,
1332 actual_type_ret, actual_format_ret,
1333 actual_size_ret, &bytes_remaining, &tmp_data);
1335 TRACE2 ("Read %lu bytes from property %s",
1336 last - bytes_remaining,
1337 XGetAtomName (display, property));
1339 /* If this doesn't return Success at this point, it means that
1340 some clod deleted the selection while we were in the midst of
1341 reading it. Deal with that, I guess.... */
1342 if (result != Success)
1343 break;
1345 /* The man page for XGetWindowProperty says:
1346 "If the returned format is 32, the returned data is represented
1347 as a long array and should be cast to that type to obtain the
1348 elements."
1349 This applies even if long is more than 32 bits, the X library
1350 converts from 32 bit elements received from the X server to long
1351 and passes the long array to us. Thus, for that case memcpy can not
1352 be used. We convert to a 32 bit type here, because so much code
1353 assume on that.
1355 The bytes and offsets passed to XGetWindowProperty refers to the
1356 property and those are indeed in 32 bit quantities if format is 32. */
1358 if (32 < BITS_PER_LONG && *actual_format_ret == 32)
1360 unsigned long i;
1361 int *idata = (int *) ((*data_ret) + offset);
1362 long *ldata = (long *) tmp_data;
1364 for (i = 0; i < *actual_size_ret; ++i)
1366 idata[i]= (int) ldata[i];
1367 offset += 4;
1370 else
1372 *actual_size_ret *= *actual_format_ret / 8;
1373 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1374 offset += *actual_size_ret;
1377 /* This was allocated by Xlib, so use XFree. */
1378 XFree ((char *) tmp_data);
1381 XFlush (display);
1382 UNBLOCK_INPUT;
1383 *bytes_ret = offset;
1386 /* Use xfree, not XFree, to free the data obtained with this function. */
1388 static void
1389 receive_incremental_selection (Display *display, Window window, Atom property,
1390 Lisp_Object target_type,
1391 unsigned int min_size_bytes,
1392 unsigned char **data_ret, int *size_bytes_ret,
1393 Atom *type_ret, int *format_ret,
1394 unsigned long *size_ret)
1396 int offset = 0;
1397 struct prop_location *wait_object;
1398 *size_bytes_ret = min_size_bytes;
1399 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1401 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1403 /* At this point, we have read an INCR property.
1404 Delete the property to ack it.
1405 (But first, prepare to receive the next event in this handshake.)
1407 Now, we must loop, waiting for the sending window to put a value on
1408 that property, then reading the property, then deleting it to ack.
1409 We are done when the sender places a property of length 0.
1411 BLOCK_INPUT;
1412 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1413 TRACE1 (" Delete property %s",
1414 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1415 XDeleteProperty (display, window, property);
1416 TRACE1 (" Expect new value of property %s",
1417 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1418 wait_object = expect_property_change (display, window, property,
1419 PropertyNewValue);
1420 XFlush (display);
1421 UNBLOCK_INPUT;
1423 while (1)
1425 unsigned char *tmp_data;
1426 int tmp_size_bytes;
1428 TRACE0 (" Wait for property change");
1429 wait_for_property_change (wait_object);
1431 /* expect it again immediately, because x_get_window_property may
1432 .. no it won't, I don't get it.
1433 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1434 TRACE0 (" Get property value");
1435 x_get_window_property (display, window, property,
1436 &tmp_data, &tmp_size_bytes,
1437 type_ret, format_ret, size_ret, 1);
1439 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1441 if (tmp_size_bytes == 0) /* we're done */
1443 TRACE0 ("Done reading incrementally");
1445 if (! waiting_for_other_props_on_window (display, window))
1446 XSelectInput (display, window, STANDARD_EVENT_SET);
1447 /* Use xfree, not XFree, because x_get_window_property
1448 calls xmalloc itself. */
1449 xfree (tmp_data);
1450 break;
1453 BLOCK_INPUT;
1454 TRACE1 (" ACK by deleting property %s",
1455 XGetAtomName (display, property));
1456 XDeleteProperty (display, window, property);
1457 wait_object = expect_property_change (display, window, property,
1458 PropertyNewValue);
1459 XFlush (display);
1460 UNBLOCK_INPUT;
1462 if (*size_bytes_ret < offset + tmp_size_bytes)
1464 *size_bytes_ret = offset + tmp_size_bytes;
1465 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1468 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1469 offset += tmp_size_bytes;
1471 /* Use xfree, not XFree, because x_get_window_property
1472 calls xmalloc itself. */
1473 xfree (tmp_data);
1478 /* Fetch a value from property PROPERTY of X window WINDOW on display
1479 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1480 if this fails. */
1482 static Lisp_Object
1483 x_get_window_property_as_lisp_data (Display *display, Window window,
1484 Atom property,
1485 Lisp_Object target_type,
1486 Atom selection_atom)
1488 Atom actual_type;
1489 int actual_format;
1490 unsigned long actual_size;
1491 unsigned char *data = 0;
1492 int bytes = 0;
1493 Lisp_Object val;
1494 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1496 TRACE0 ("Reading selection data");
1498 x_get_window_property (display, window, property, &data, &bytes,
1499 &actual_type, &actual_format, &actual_size, 1);
1500 if (! data)
1502 int there_is_a_selection_owner;
1503 BLOCK_INPUT;
1504 there_is_a_selection_owner
1505 = XGetSelectionOwner (display, selection_atom);
1506 UNBLOCK_INPUT;
1507 if (there_is_a_selection_owner)
1508 signal_error ("Selection owner couldn't convert",
1509 actual_type
1510 ? list2 (target_type,
1511 x_atom_to_symbol (display, actual_type))
1512 : target_type);
1513 else
1514 signal_error ("No selection",
1515 x_atom_to_symbol (display, selection_atom));
1518 if (actual_type == dpyinfo->Xatom_INCR)
1520 /* That wasn't really the data, just the beginning. */
1522 unsigned int min_size_bytes = * ((unsigned int *) data);
1523 BLOCK_INPUT;
1524 /* Use xfree, not XFree, because x_get_window_property
1525 calls xmalloc itself. */
1526 xfree ((char *) data);
1527 UNBLOCK_INPUT;
1528 receive_incremental_selection (display, window, property, target_type,
1529 min_size_bytes, &data, &bytes,
1530 &actual_type, &actual_format,
1531 &actual_size);
1534 BLOCK_INPUT;
1535 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1536 XDeleteProperty (display, window, property);
1537 XFlush (display);
1538 UNBLOCK_INPUT;
1540 /* It's been read. Now convert it to a lisp object in some semi-rational
1541 manner. */
1542 val = selection_data_to_lisp_data (display, data, bytes,
1543 actual_type, actual_format);
1545 /* Use xfree, not XFree, because x_get_window_property
1546 calls xmalloc itself. */
1547 xfree ((char *) data);
1548 return val;
1551 /* These functions convert from the selection data read from the server into
1552 something that we can use from Lisp, and vice versa.
1554 Type: Format: Size: Lisp Type:
1555 ----- ------- ----- -----------
1556 * 8 * String
1557 ATOM 32 1 Symbol
1558 ATOM 32 > 1 Vector of Symbols
1559 * 16 1 Integer
1560 * 16 > 1 Vector of Integers
1561 * 32 1 if <=16 bits: Integer
1562 if > 16 bits: Cons of top16, bot16
1563 * 32 > 1 Vector of the above
1565 When converting a Lisp number to C, it is assumed to be of format 16 if
1566 it is an integer, and of format 32 if it is a cons of two integers.
1568 When converting a vector of numbers from Lisp to C, it is assumed to be
1569 of format 16 if every element in the vector is an integer, and is assumed
1570 to be of format 32 if any element is a cons of two integers.
1572 When converting an object to C, it may be of the form (SYMBOL . <data>)
1573 where SYMBOL is what we should claim that the type is. Format and
1574 representation are as above.
1576 Important: When format is 32, data should contain an array of int,
1577 not an array of long as the X library returns. This makes a difference
1578 when sizeof(long) != sizeof(int). */
1582 static Lisp_Object
1583 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1584 int size, Atom type, int format)
1586 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1588 if (type == dpyinfo->Xatom_NULL)
1589 return QNULL;
1591 /* Convert any 8-bit data to a string, for compactness. */
1592 else if (format == 8)
1594 Lisp_Object str, lispy_type;
1596 str = make_unibyte_string ((char *) data, size);
1597 /* Indicate that this string is from foreign selection by a text
1598 property `foreign-selection' so that the caller of
1599 x-get-selection-internal (usually x-get-selection) can know
1600 that the string must be decode. */
1601 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1602 lispy_type = QCOMPOUND_TEXT;
1603 else if (type == dpyinfo->Xatom_UTF8_STRING)
1604 lispy_type = QUTF8_STRING;
1605 else
1606 lispy_type = QSTRING;
1607 Fput_text_property (make_number (0), make_number (size),
1608 Qforeign_selection, lispy_type, str);
1609 return str;
1611 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1612 a vector of symbols. */
1613 else if (type == XA_ATOM
1614 /* Treat ATOM_PAIR type similar to list of atoms. */
1615 || type == dpyinfo->Xatom_ATOM_PAIR)
1617 int i;
1618 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1619 But the callers of these function has made sure the data for
1620 format == 32 is an array of int. Thus, use int instead
1621 of Atom. */
1622 int *idata = (int *) data;
1624 if (size == sizeof (int))
1625 return x_atom_to_symbol (display, (Atom) idata[0]);
1626 else
1628 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1629 make_number (0));
1630 for (i = 0; i < size / sizeof (int); i++)
1631 Faset (v, make_number (i),
1632 x_atom_to_symbol (display, (Atom) idata[i]));
1633 return v;
1637 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1638 If the number is 32 bits and won't fit in a Lisp_Int,
1639 convert it to a cons of integers, 16 bits in each half.
1641 else if (format == 32 && size == sizeof (int))
1642 return long_to_cons (((unsigned int *) data) [0]);
1643 else if (format == 16 && size == sizeof (short))
1644 return make_number ((int) (((unsigned short *) data) [0]));
1646 /* Convert any other kind of data to a vector of numbers, represented
1647 as above (as an integer, or a cons of two 16 bit integers.)
1649 else if (format == 16)
1651 int i;
1652 Lisp_Object v;
1653 v = Fmake_vector (make_number (size / 2), make_number (0));
1654 for (i = 0; i < size / 2; i++)
1656 int j = (int) ((unsigned short *) data) [i];
1657 Faset (v, make_number (i), make_number (j));
1659 return v;
1661 else
1663 int i;
1664 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1665 for (i = 0; i < size / 4; i++)
1667 unsigned int j = ((unsigned int *) data) [i];
1668 Faset (v, make_number (i), long_to_cons (j));
1670 return v;
1675 /* Use xfree, not XFree, to free the data obtained with this function. */
1677 static void
1678 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1679 unsigned char **data_ret, Atom *type_ret,
1680 unsigned int *size_ret,
1681 int *format_ret, int *nofree_ret)
1683 Lisp_Object type = Qnil;
1684 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1686 *nofree_ret = 0;
1688 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1690 type = XCAR (obj);
1691 obj = XCDR (obj);
1692 if (CONSP (obj) && NILP (XCDR (obj)))
1693 obj = XCAR (obj);
1696 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1697 { /* This is not the same as declining */
1698 *format_ret = 32;
1699 *size_ret = 0;
1700 *data_ret = 0;
1701 type = QNULL;
1703 else if (STRINGP (obj))
1705 if (SCHARS (obj) < SBYTES (obj))
1706 /* OBJ is a multibyte string containing a non-ASCII char. */
1707 signal_error ("Non-ASCII string must be encoded in advance", obj);
1708 if (NILP (type))
1709 type = QSTRING;
1710 *format_ret = 8;
1711 *size_ret = SBYTES (obj);
1712 *data_ret = SDATA (obj);
1713 *nofree_ret = 1;
1715 else if (SYMBOLP (obj))
1717 *format_ret = 32;
1718 *size_ret = 1;
1719 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1720 (*data_ret) [sizeof (Atom)] = 0;
1721 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, obj);
1722 if (NILP (type)) type = QATOM;
1724 else if (INTEGERP (obj)
1725 && XINT (obj) < 0xFFFF
1726 && XINT (obj) > -0xFFFF)
1728 *format_ret = 16;
1729 *size_ret = 1;
1730 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1731 (*data_ret) [sizeof (short)] = 0;
1732 (*(short **) data_ret) [0] = (short) XINT (obj);
1733 if (NILP (type)) type = QINTEGER;
1735 else if (INTEGERP (obj)
1736 || (CONSP (obj) && INTEGERP (XCAR (obj))
1737 && (INTEGERP (XCDR (obj))
1738 || (CONSP (XCDR (obj))
1739 && INTEGERP (XCAR (XCDR (obj)))))))
1741 *format_ret = 32;
1742 *size_ret = 1;
1743 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1744 (*data_ret) [sizeof (long)] = 0;
1745 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1746 if (NILP (type)) type = QINTEGER;
1748 else if (VECTORP (obj))
1750 /* Lisp_Vectors may represent a set of ATOMs;
1751 a set of 16 or 32 bit INTEGERs;
1752 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1754 int i;
1756 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1757 /* This vector is an ATOM set */
1759 if (NILP (type)) type = QATOM;
1760 *size_ret = ASIZE (obj);
1761 *format_ret = 32;
1762 for (i = 0; i < *size_ret; i++)
1763 if (!SYMBOLP (XVECTOR (obj)->contents [i]))
1764 signal_error ("All elements of selection vector must have same type", obj);
1766 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1767 for (i = 0; i < *size_ret; i++)
1768 (*(Atom **) data_ret) [i]
1769 = symbol_to_x_atom (dpyinfo, XVECTOR (obj)->contents [i]);
1771 else
1772 /* This vector is an INTEGER set, or something like it */
1774 int data_size = 2;
1775 *size_ret = ASIZE (obj);
1776 if (NILP (type)) type = QINTEGER;
1777 *format_ret = 16;
1778 for (i = 0; i < *size_ret; i++)
1779 if (CONSP (XVECTOR (obj)->contents [i]))
1780 *format_ret = 32;
1781 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1782 signal_error (/* Qselection_error */
1783 "Elements of selection vector must be integers or conses of integers",
1784 obj);
1786 /* Use sizeof(long) even if it is more than 32 bits. See comment
1787 in x_get_window_property and x_fill_property_data. */
1789 if (*format_ret == 32) data_size = sizeof(long);
1790 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1791 for (i = 0; i < *size_ret; i++)
1792 if (*format_ret == 32)
1793 (*((unsigned long **) data_ret)) [i]
1794 = cons_to_long (XVECTOR (obj)->contents [i]);
1795 else
1796 (*((unsigned short **) data_ret)) [i]
1797 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
1800 else
1801 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1803 *type_ret = symbol_to_x_atom (dpyinfo, type);
1806 static Lisp_Object
1807 clean_local_selection_data (Lisp_Object obj)
1809 if (CONSP (obj)
1810 && INTEGERP (XCAR (obj))
1811 && CONSP (XCDR (obj))
1812 && INTEGERP (XCAR (XCDR (obj)))
1813 && NILP (XCDR (XCDR (obj))))
1814 obj = Fcons (XCAR (obj), XCDR (obj));
1816 if (CONSP (obj)
1817 && INTEGERP (XCAR (obj))
1818 && INTEGERP (XCDR (obj)))
1820 if (XINT (XCAR (obj)) == 0)
1821 return XCDR (obj);
1822 if (XINT (XCAR (obj)) == -1)
1823 return make_number (- XINT (XCDR (obj)));
1825 if (VECTORP (obj))
1827 int i;
1828 int size = ASIZE (obj);
1829 Lisp_Object copy;
1830 if (size == 1)
1831 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1832 copy = Fmake_vector (make_number (size), Qnil);
1833 for (i = 0; i < size; i++)
1834 XVECTOR (copy)->contents [i]
1835 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1836 return copy;
1838 return obj;
1841 /* Called from XTread_socket to handle SelectionNotify events.
1842 If it's the selection we are waiting for, stop waiting
1843 by setting the car of reading_selection_reply to non-nil.
1844 We store t there if the reply is successful, lambda if not. */
1846 void
1847 x_handle_selection_notify (XSelectionEvent *event)
1849 if (event->requestor != reading_selection_window)
1850 return;
1851 if (event->selection != reading_which_selection)
1852 return;
1854 TRACE0 ("Received SelectionNotify");
1855 XSETCAR (reading_selection_reply,
1856 (event->property != 0 ? Qt : Qlambda));
1860 /* From a Lisp_Object, return a suitable frame for selection
1861 operations. OBJECT may be a frame, a terminal object, or nil
1862 (which stands for the selected frame--or, if that is not an X
1863 frame, the first X display on the list). If no suitable frame can
1864 be found, return NULL. */
1866 static struct frame *
1867 frame_for_x_selection (Lisp_Object object)
1869 Lisp_Object tail;
1870 struct frame *f;
1872 if (NILP (object))
1874 f = XFRAME (selected_frame);
1875 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1876 return f;
1878 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1880 f = XFRAME (XCAR (tail));
1881 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1882 return f;
1885 else if (TERMINALP (object))
1887 struct terminal *t = get_terminal (object, 1);
1888 if (t->type == output_x_window)
1890 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1892 f = XFRAME (XCAR (tail));
1893 if (FRAME_LIVE_P (f) && f->terminal == t)
1894 return f;
1898 else if (FRAMEP (object))
1900 f = XFRAME (object);
1901 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1902 return f;
1905 return NULL;
1909 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1910 Sx_own_selection_internal, 2, 3, 0,
1911 doc: /* Assert an X selection of type SELECTION and value VALUE.
1912 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1913 \(Those are literal upper-case symbol names, since that's what X expects.)
1914 VALUE is typically a string, or a cons of two markers, but may be
1915 anything that the functions on `selection-converter-alist' know about.
1917 FRAME should be a frame that should own the selection. If omitted or
1918 nil, it defaults to the selected frame. */)
1919 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1921 if (NILP (frame)) frame = selected_frame;
1922 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1923 error ("X selection unavailable for this frame");
1925 CHECK_SYMBOL (selection);
1926 if (NILP (value)) error ("VALUE may not be nil");
1927 x_own_selection (selection, value, frame);
1928 return value;
1932 /* Request the selection value from the owner. If we are the owner,
1933 simply return our selection value. If we are not the owner, this
1934 will block until all of the data has arrived. */
1936 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1937 Sx_get_selection_internal, 2, 4, 0,
1938 doc: /* Return text selected from some X window.
1939 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1940 \(Those are literal upper-case symbol names, since that's what X expects.)
1941 TYPE is the type of data desired, typically `STRING'.
1942 TIME_STAMP is the time to use in the XConvertSelection call for foreign
1943 selections. If omitted, defaults to the time for the last event.
1945 TERMINAL should be a terminal object or a frame specifying the X
1946 server to query. If omitted or nil, that stands for the selected
1947 frame's display, or the first available X display. */)
1948 (Lisp_Object selection_symbol, Lisp_Object target_type,
1949 Lisp_Object time_stamp, Lisp_Object terminal)
1951 Lisp_Object val = Qnil;
1952 struct gcpro gcpro1, gcpro2;
1953 struct frame *f = frame_for_x_selection (terminal);
1954 GCPRO2 (target_type, val); /* we store newly consed data into these */
1956 CHECK_SYMBOL (selection_symbol);
1957 CHECK_SYMBOL (target_type);
1958 if (EQ (target_type, QMULTIPLE))
1959 error ("Retrieving MULTIPLE selections is currently unimplemented");
1960 if (!f)
1961 error ("X selection unavailable for this frame");
1963 val = x_get_local_selection (selection_symbol, target_type, 1,
1964 FRAME_X_DISPLAY_INFO (f));
1966 if (NILP (val) && FRAME_LIVE_P (f))
1968 Lisp_Object frame;
1969 XSETFRAME (frame, f);
1970 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1971 time_stamp, frame));
1974 if (CONSP (val) && SYMBOLP (XCAR (val)))
1976 val = XCDR (val);
1977 if (CONSP (val) && NILP (XCDR (val)))
1978 val = XCAR (val);
1980 RETURN_UNGCPRO (clean_local_selection_data (val));
1983 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
1984 Sx_disown_selection_internal, 1, 3, 0,
1985 doc: /* If we own the selection SELECTION, disown it.
1986 Disowning it means there is no such selection.
1988 TERMINAL should be a terminal object or a frame specifying the X
1989 server to query. If omitted or nil, that stands for the selected
1990 frame's display, or the first available X display. */)
1991 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
1993 Time timestamp;
1994 Atom selection_atom;
1995 union {
1996 struct selection_input_event sie;
1997 struct input_event ie;
1998 } event;
1999 struct frame *f = frame_for_x_selection (terminal);
2000 struct x_display_info *dpyinfo;
2002 if (!f)
2003 return Qnil;
2005 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2006 CHECK_SYMBOL (selection);
2008 /* Don't disown the selection when we're not the owner. */
2009 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2010 return Qnil;
2012 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2014 BLOCK_INPUT;
2015 timestamp = (NILP (time_object) ? last_event_timestamp
2016 : cons_to_long (time_object));
2017 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2018 UNBLOCK_INPUT;
2020 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2021 generated for a window which owns the selection when that window sets
2022 the selection owner to None. The NCD server does, the MIT Sun4 server
2023 doesn't. So we synthesize one; this means we might get two, but
2024 that's ok, because the second one won't have any effect. */
2025 SELECTION_EVENT_DISPLAY (&event.sie) = dpyinfo->display;
2026 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2027 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2028 x_handle_selection_clear (&event.ie);
2030 return Qt;
2033 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2034 0, 2, 0,
2035 doc: /* Whether the current Emacs process owns the given X Selection.
2036 The arg should be the name of the selection in question, typically one of
2037 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2038 \(Those are literal upper-case symbol names, since that's what X expects.)
2039 For convenience, the symbol nil is the same as `PRIMARY',
2040 and t is the same as `SECONDARY'.
2042 TERMINAL should be a terminal object or a frame specifying the X
2043 server to query. If omitted or nil, that stands for the selected
2044 frame's display, or the first available X display. */)
2045 (Lisp_Object selection, Lisp_Object terminal)
2047 struct frame *f = frame_for_x_selection (terminal);
2049 CHECK_SYMBOL (selection);
2050 if (EQ (selection, Qnil)) selection = QPRIMARY;
2051 if (EQ (selection, Qt)) selection = QSECONDARY;
2053 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f))))
2054 return Qt;
2055 else
2056 return Qnil;
2059 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2060 0, 2, 0,
2061 doc: /* Whether there is an owner for the given X selection.
2062 SELECTION should be the name of the selection in question, typically
2063 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
2064 these literal upper-case names.) The symbol nil is the same as
2065 `PRIMARY', and t is the same as `SECONDARY'.
2067 TERMINAL should be a terminal object or a frame specifying the X
2068 server to query. If omitted or nil, that stands for the selected
2069 frame's display, or the first available X display. */)
2070 (Lisp_Object selection, Lisp_Object terminal)
2072 Window owner;
2073 Atom atom;
2074 struct frame *f = frame_for_x_selection (terminal);
2075 struct x_display_info *dpyinfo;
2077 CHECK_SYMBOL (selection);
2078 if (EQ (selection, Qnil)) selection = QPRIMARY;
2079 if (EQ (selection, Qt)) selection = QSECONDARY;
2081 if (!f)
2082 return Qnil;
2084 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2086 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2087 return Qt;
2089 atom = symbol_to_x_atom (dpyinfo, selection);
2090 if (atom == 0) return Qnil;
2091 BLOCK_INPUT;
2092 owner = XGetSelectionOwner (dpyinfo->display, atom);
2093 UNBLOCK_INPUT;
2094 return (owner ? Qt : Qnil);
2098 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2099 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2101 static Lisp_Object
2102 x_clipboard_manager_save (Lisp_Object frame)
2104 struct frame *f = XFRAME (frame);
2105 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2106 Atom data = dpyinfo->Xatom_UTF8_STRING;
2108 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2109 dpyinfo->Xatom_EMACS_TMP,
2110 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2111 (unsigned char *) &data, 1);
2112 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2113 Qnil, frame);
2114 return Qt;
2117 /* Error handler for x_clipboard_manager_save_frame. */
2119 static Lisp_Object
2120 x_clipboard_manager_error_1 (Lisp_Object err)
2122 Lisp_Object args[2];
2123 args[0] = build_string ("X clipboard manager error: %s\n\
2124 If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2125 args[1] = CAR (CDR (err));
2126 Fmessage (2, args);
2127 return Qnil;
2130 /* Error handler for x_clipboard_manager_save_all. */
2132 static Lisp_Object
2133 x_clipboard_manager_error_2 (Lisp_Object err)
2135 fprintf (stderr, "Error saving to X clipboard manager.\n\
2136 If the problem persists, set `x-select-enable-clipboard-manager' \
2137 to nil.\n");
2138 return Qnil;
2141 /* Called from delete_frame: save any clipboard owned by FRAME to the
2142 clipboard manager. Do nothing if FRAME does not own the clipboard,
2143 or if no clipboard manager is present. */
2145 void
2146 x_clipboard_manager_save_frame (Lisp_Object frame)
2148 struct frame *f;
2150 if (!NILP (Vx_select_enable_clipboard_manager)
2151 && FRAMEP (frame)
2152 && (f = XFRAME (frame), FRAME_X_P (f))
2153 && FRAME_LIVE_P (f))
2155 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2156 Lisp_Object local_selection
2157 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2159 if (!NILP (local_selection)
2160 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2161 && XGetSelectionOwner (dpyinfo->display,
2162 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2163 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2164 x_clipboard_manager_error_1);
2168 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2169 clipboard manager. Do nothing if FRAME does not own the clipboard,
2170 or if no clipboard manager is present. */
2172 void
2173 x_clipboard_manager_save_all (void)
2175 /* Loop through all X displays, saving owned clipboards. */
2176 struct x_display_info *dpyinfo;
2177 Lisp_Object local_selection, local_frame;
2179 if (NILP (Vx_select_enable_clipboard_manager))
2180 return;
2182 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2184 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2185 if (NILP (local_selection)
2186 || !XGetSelectionOwner (dpyinfo->display,
2187 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2188 continue;
2190 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2191 if (FRAME_LIVE_P (XFRAME (local_frame)))
2192 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2193 Qt, x_clipboard_manager_error_2);
2198 /***********************************************************************
2199 Drag and drop support
2200 ***********************************************************************/
2201 /* Check that lisp values are of correct type for x_fill_property_data.
2202 That is, number, string or a cons with two numbers (low and high 16
2203 bit parts of a 32 bit number). Return the number of items in DATA,
2204 or -1 if there is an error. */
2207 x_check_property_data (Lisp_Object data)
2209 Lisp_Object iter;
2210 int size = 0;
2212 for (iter = data; CONSP (iter); iter = XCDR (iter))
2214 Lisp_Object o = XCAR (iter);
2216 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2217 return -1;
2218 else if (CONSP (o) &&
2219 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2220 return -1;
2221 size++;
2224 return size;
2227 /* Convert lisp values to a C array. Values may be a number, a string
2228 which is taken as an X atom name and converted to the atom value, or
2229 a cons containing the two 16 bit parts of a 32 bit number.
2231 DPY is the display use to look up X atoms.
2232 DATA is a Lisp list of values to be converted.
2233 RET is the C array that contains the converted values. It is assumed
2234 it is big enough to hold all values.
2235 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2236 be stored in RET. Note that long is used for 32 even if long is more
2237 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2238 XClientMessageEvent). */
2240 void
2241 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2243 long val;
2244 long *d32 = (long *) ret;
2245 short *d16 = (short *) ret;
2246 char *d08 = (char *) ret;
2247 Lisp_Object iter;
2249 for (iter = data; CONSP (iter); iter = XCDR (iter))
2251 Lisp_Object o = XCAR (iter);
2253 if (INTEGERP (o))
2254 val = (long) XFASTINT (o);
2255 else if (FLOATP (o))
2256 val = (long) XFLOAT_DATA (o);
2257 else if (CONSP (o))
2258 val = (long) cons_to_long (o);
2259 else if (STRINGP (o))
2261 BLOCK_INPUT;
2262 val = (long) XInternAtom (dpy, SSDATA (o), False);
2263 UNBLOCK_INPUT;
2265 else
2266 error ("Wrong type, must be string, number or cons");
2268 if (format == 8)
2269 *d08++ = (char) val;
2270 else if (format == 16)
2271 *d16++ = (short) val;
2272 else
2273 *d32++ = val;
2277 /* Convert an array of C values to a Lisp list.
2278 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2279 DATA is a C array of values to be converted.
2280 TYPE is the type of the data. Only XA_ATOM is special, it converts
2281 each number in DATA to its corresponfing X atom as a symbol.
2282 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2283 be stored in RET.
2284 SIZE is the number of elements in DATA.
2286 Important: When format is 32, data should contain an array of int,
2287 not an array of long as the X library returns. This makes a difference
2288 when sizeof(long) != sizeof(int).
2290 Also see comment for selection_data_to_lisp_data above. */
2292 Lisp_Object
2293 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2294 Atom type, int format, long unsigned int size)
2296 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2297 data, size*format/8, type, format);
2300 /* Get the mouse position in frame relative coordinates. */
2302 static void
2303 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2305 Window root, dummy_window;
2306 int dummy;
2308 BLOCK_INPUT;
2310 XQueryPointer (FRAME_X_DISPLAY (f),
2311 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2313 /* The root window which contains the pointer. */
2314 &root,
2316 /* Window pointer is on, not used */
2317 &dummy_window,
2319 /* The position on that root window. */
2320 x, y,
2322 /* x/y in dummy_window coordinates, not used. */
2323 &dummy, &dummy,
2325 /* Modifier keys and pointer buttons, about which
2326 we don't care. */
2327 (unsigned int *) &dummy);
2330 /* Absolute to relative. */
2331 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2332 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2334 UNBLOCK_INPUT;
2337 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2338 Sx_get_atom_name, 1, 2, 0,
2339 doc: /* Return the X atom name for VALUE as a string.
2340 VALUE may be a number or a cons where the car is the upper 16 bits and
2341 the cdr is the lower 16 bits of a 32 bit value.
2342 Use the display for FRAME or the current frame if FRAME is not given or nil.
2344 If the value is 0 or the atom is not known, return the empty string. */)
2345 (Lisp_Object value, Lisp_Object frame)
2347 struct frame *f = check_x_frame (frame);
2348 char *name = 0;
2349 char empty[] = "";
2350 Lisp_Object ret = Qnil;
2351 Display *dpy = FRAME_X_DISPLAY (f);
2352 Atom atom;
2353 int had_errors;
2355 if (INTEGERP (value))
2356 atom = (Atom) XUINT (value);
2357 else if (FLOATP (value))
2358 atom = (Atom) XFLOAT_DATA (value);
2359 else if (CONSP (value))
2360 atom = (Atom) cons_to_long (value);
2361 else
2362 error ("Wrong type, value must be number or cons");
2364 BLOCK_INPUT;
2365 x_catch_errors (dpy);
2366 name = atom ? XGetAtomName (dpy, atom) : empty;
2367 had_errors = x_had_errors_p (dpy);
2368 x_uncatch_errors ();
2370 if (!had_errors)
2371 ret = make_string (name, strlen (name));
2373 if (atom && name) XFree (name);
2374 if (NILP (ret)) ret = empty_unibyte_string;
2376 UNBLOCK_INPUT;
2378 return ret;
2381 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2382 Sx_register_dnd_atom, 1, 2, 0,
2383 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2384 ATOM can be a symbol or a string. The ATOM is interned on the display that
2385 FRAME is on. If FRAME is nil, the selected frame is used. */)
2386 (Lisp_Object atom, Lisp_Object frame)
2388 Atom x_atom;
2389 struct frame *f = check_x_frame (frame);
2390 size_t i;
2391 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2394 if (SYMBOLP (atom))
2395 x_atom = symbol_to_x_atom (dpyinfo, atom);
2396 else if (STRINGP (atom))
2398 BLOCK_INPUT;
2399 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2400 UNBLOCK_INPUT;
2402 else
2403 error ("ATOM must be a symbol or a string");
2405 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2406 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2407 return Qnil;
2409 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2411 dpyinfo->x_dnd_atoms_size *= 2;
2412 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2413 sizeof (*dpyinfo->x_dnd_atoms)
2414 * dpyinfo->x_dnd_atoms_size);
2417 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2418 return Qnil;
2421 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2424 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2426 Lisp_Object vec;
2427 Lisp_Object frame;
2428 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2429 unsigned long size = 160/event->format;
2430 int x, y;
2431 unsigned char *data = (unsigned char *) event->data.b;
2432 int idata[5];
2433 size_t i;
2435 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2436 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2438 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2440 XSETFRAME (frame, f);
2442 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2443 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2444 function expects them to be of size int (i.e. 32). So to be able to
2445 use that function, put the data in the form it expects if format is 32. */
2447 if (32 < BITS_PER_LONG && event->format == 32)
2449 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2450 idata[i] = (int) event->data.l[i];
2451 data = (unsigned char *) idata;
2454 vec = Fmake_vector (make_number (4), Qnil);
2455 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2456 event->message_type)));
2457 ASET (vec, 1, frame);
2458 ASET (vec, 2, make_number (event->format));
2459 ASET (vec, 3, x_property_data_to_lisp (f,
2460 data,
2461 event->message_type,
2462 event->format,
2463 size));
2465 mouse_position_for_drop (f, &x, &y);
2466 bufp->kind = DRAG_N_DROP_EVENT;
2467 bufp->frame_or_window = frame;
2468 bufp->timestamp = CurrentTime;
2469 bufp->x = make_number (x);
2470 bufp->y = make_number (y);
2471 bufp->arg = vec;
2472 bufp->modifiers = 0;
2474 return 1;
2477 DEFUN ("x-send-client-message", Fx_send_client_event,
2478 Sx_send_client_message, 6, 6, 0,
2479 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2481 For DISPLAY, specify either a frame or a display name (a string).
2482 If DISPLAY is nil, that stands for the selected frame's display.
2483 DEST may be a number, in which case it is a Window id. The value 0 may
2484 be used to send to the root window of the DISPLAY.
2485 If DEST is a cons, it is converted to a 32 bit number
2486 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2487 number is then used as a window id.
2488 If DEST is a frame the event is sent to the outer window of that frame.
2489 A value of nil means the currently selected frame.
2490 If DEST is the string "PointerWindow" the event is sent to the window that
2491 contains the pointer. If DEST is the string "InputFocus" the event is
2492 sent to the window that has the input focus.
2493 FROM is the frame sending the event. Use nil for currently selected frame.
2494 MESSAGE-TYPE is the name of an Atom as a string.
2495 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2496 bits. VALUES is a list of numbers, cons and/or strings containing the values
2497 to send. If a value is a string, it is converted to an Atom and the value of
2498 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2499 with the high 16 bits from the car and the lower 16 bit from the cdr.
2500 If more values than fits into the event is given, the excessive values
2501 are ignored. */)
2502 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2504 struct x_display_info *dpyinfo = check_x_display_info (display);
2506 CHECK_STRING (message_type);
2507 x_send_client_event(display, dest, from,
2508 XInternAtom (dpyinfo->display,
2509 SSDATA (message_type),
2510 False),
2511 format, values);
2513 return Qnil;
2516 void
2517 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2519 struct x_display_info *dpyinfo = check_x_display_info (display);
2520 Window wdest;
2521 XEvent event;
2522 struct frame *f = check_x_frame (from);
2523 int to_root;
2525 CHECK_NUMBER (format);
2526 CHECK_CONS (values);
2528 if (x_check_property_data (values) == -1)
2529 error ("Bad data in VALUES, must be number, cons or string");
2531 event.xclient.type = ClientMessage;
2532 event.xclient.format = XFASTINT (format);
2534 if (event.xclient.format != 8 && event.xclient.format != 16
2535 && event.xclient.format != 32)
2536 error ("FORMAT must be one of 8, 16 or 32");
2538 if (FRAMEP (dest) || NILP (dest))
2540 struct frame *fdest = check_x_frame (dest);
2541 wdest = FRAME_OUTER_WINDOW (fdest);
2543 else if (STRINGP (dest))
2545 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2546 wdest = PointerWindow;
2547 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2548 wdest = InputFocus;
2549 else
2550 error ("DEST as a string must be one of PointerWindow or InputFocus");
2552 else if (INTEGERP (dest))
2553 wdest = (Window) XFASTINT (dest);
2554 else if (FLOATP (dest))
2555 wdest = (Window) XFLOAT_DATA (dest);
2556 else if (CONSP (dest))
2558 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2559 error ("Both car and cdr for DEST must be numbers");
2560 else
2561 wdest = (Window) cons_to_long (dest);
2563 else
2564 error ("DEST must be a frame, nil, string, number or cons");
2566 if (wdest == 0) wdest = dpyinfo->root_window;
2567 to_root = wdest == dpyinfo->root_window;
2569 BLOCK_INPUT;
2571 event.xclient.message_type = message_type;
2572 event.xclient.display = dpyinfo->display;
2574 /* Some clients (metacity for example) expects sending window to be here
2575 when sending to the root window. */
2576 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2579 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2580 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2581 event.xclient.format);
2583 /* If event mask is 0 the event is sent to the client that created
2584 the destination window. But if we are sending to the root window,
2585 there is no such client. Then we set the event mask to 0xffff. The
2586 event then goes to clients selecting for events on the root window. */
2587 x_catch_errors (dpyinfo->display);
2589 int propagate = to_root ? False : True;
2590 unsigned mask = to_root ? 0xffff : 0;
2591 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2592 XFlush (dpyinfo->display);
2594 x_uncatch_errors ();
2595 UNBLOCK_INPUT;
2599 void
2600 syms_of_xselect (void)
2602 defsubr (&Sx_get_selection_internal);
2603 defsubr (&Sx_own_selection_internal);
2604 defsubr (&Sx_disown_selection_internal);
2605 defsubr (&Sx_selection_owner_p);
2606 defsubr (&Sx_selection_exists_p);
2608 defsubr (&Sx_get_atom_name);
2609 defsubr (&Sx_send_client_message);
2610 defsubr (&Sx_register_dnd_atom);
2612 reading_selection_reply = Fcons (Qnil, Qnil);
2613 staticpro (&reading_selection_reply);
2614 reading_selection_window = 0;
2615 reading_which_selection = 0;
2617 property_change_wait_list = 0;
2618 prop_location_identifier = 0;
2619 property_change_reply = Fcons (Qnil, Qnil);
2620 staticpro (&property_change_reply);
2622 converted_selections = NULL;
2623 conversion_fail_tag = None;
2625 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2626 doc: /* An alist associating X Windows selection-types with functions.
2627 These functions are called to convert the selection, with three args:
2628 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2629 a desired type to which the selection should be converted;
2630 and the local selection value (whatever was given to `x-own-selection').
2632 The function should return the value to send to the X server
2633 \(typically a string). A return value of nil
2634 means that the conversion could not be done.
2635 A return value which is the symbol `NULL'
2636 means that a side-effect was executed,
2637 and there is no meaningful selection value. */);
2638 Vselection_converter_alist = Qnil;
2640 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2641 doc: /* A list of functions to be called when Emacs loses an X selection.
2642 \(This happens when some other X client makes its own selection
2643 or when a Lisp program explicitly clears the selection.)
2644 The functions are called with one argument, the selection type
2645 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2646 Vx_lost_selection_functions = Qnil;
2648 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2649 doc: /* A list of functions to be called when Emacs answers a selection request.
2650 The functions are called with three arguments:
2651 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2652 - the selection-type which Emacs was asked to convert the
2653 selection into before sending (for example, `STRING' or `LENGTH');
2654 - a flag indicating success or failure for responding to the request.
2655 We might have failed (and declined the request) for any number of reasons,
2656 including being asked for a selection that we no longer own, or being asked
2657 to convert into a type that we don't know about or that is inappropriate.
2658 This hook doesn't let you change the behavior of Emacs's selection replies,
2659 it merely informs you that they have happened. */);
2660 Vx_sent_selection_functions = Qnil;
2662 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2663 Vx_select_enable_clipboard_manager,
2664 doc: /* Whether to enable X clipboard manager support.
2665 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2666 while owning the X clipboard, the clipboard contents are saved to the
2667 clipboard manager if one is present. */);
2668 Vx_select_enable_clipboard_manager = Qt;
2670 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2671 doc: /* Number of milliseconds to wait for a selection reply.
2672 If the selection owner doesn't reply in this time, we give up.
2673 A value of 0 means wait as long as necessary. This is initialized from the
2674 \"*selectionTimeout\" resource. */);
2675 x_selection_timeout = 0;
2677 /* QPRIMARY is defined in keyboard.c. */
2678 DEFSYM (QSECONDARY, "SECONDARY");
2679 DEFSYM (QSTRING, "STRING");
2680 DEFSYM (QINTEGER, "INTEGER");
2681 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2682 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2683 DEFSYM (QTEXT, "TEXT");
2684 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2685 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2686 DEFSYM (QDELETE, "DELETE");
2687 DEFSYM (QMULTIPLE, "MULTIPLE");
2688 DEFSYM (QINCR, "INCR");
2689 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2690 DEFSYM (QTARGETS, "TARGETS");
2691 DEFSYM (QATOM, "ATOM");
2692 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2693 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2694 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2695 DEFSYM (QNULL, "NULL");
2696 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2697 DEFSYM (Qforeign_selection, "foreign-selection");