Update copyright year to 2015
[emacs.git] / src / xselect.c
blob92460d115dbcdf4e0fa54a4fc2d38d115ccbf44b
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2015 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 <limits.h>
24 #include <stdio.h> /* termhooks.h needs this */
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 "character.h"
38 #include "buffer.h"
39 #include "process.h"
40 #include "termhooks.h"
41 #include "keyboard.h"
43 #include <X11/Xproto.h>
45 struct prop_location;
46 struct selection_data;
48 static void x_decline_selection_request (struct input_event *);
49 static bool x_convert_selection (struct input_event *, Lisp_Object,
50 Lisp_Object, Atom, bool,
51 struct x_display_info *);
52 static bool waiting_for_other_props_on_window (Display *, Window);
53 static struct prop_location *expect_property_change (Display *, Window,
54 Atom, int);
55 static void unexpect_property_change (struct prop_location *);
56 static void wait_for_property_change (struct prop_location *);
57 static Lisp_Object x_get_window_property_as_lisp_data (struct x_display_info *,
58 Window, Atom,
59 Lisp_Object, Atom);
60 static Lisp_Object selection_data_to_lisp_data (struct x_display_info *,
61 const unsigned char *,
62 ptrdiff_t, Atom, int);
63 static void lisp_data_to_selection_data (struct x_display_info *, Lisp_Object,
64 struct selection_data *);
66 /* Printing traces to stderr. */
68 #ifdef TRACE_SELECTION
69 #define TRACE0(fmt) \
70 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid ())
71 #define TRACE1(fmt, a0) \
72 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0)
73 #define TRACE2(fmt, a0, a1) \
74 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1)
75 #define TRACE3(fmt, a0, a1, a2) \
76 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1, a2)
77 #else
78 #define TRACE0(fmt) (void) 0
79 #define TRACE1(fmt, a0) (void) 0
80 #define TRACE2(fmt, a0, a1) (void) 0
81 #endif
84 static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
85 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
86 QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS;
88 static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
89 static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
91 static Lisp_Object Qcompound_text_with_extensions;
93 static Lisp_Object Qforeign_selection;
94 static Lisp_Object Qx_lost_selection_functions, Qx_sent_selection_functions;
96 /* Bytes needed to represent 'long' data. This is as per libX11; it
97 is not necessarily sizeof (long). */
98 #define X_LONG_SIZE 4
100 /* If this is a smaller number than the max-request-size of the display,
101 emacs will use INCR selection transfer when the selection is larger
102 than this. The max-request-size is usually around 64k, so if you want
103 emacs to use incremental selection transfers when the selection is
104 smaller than that, set this. I added this mostly for debugging the
105 incremental transfer stuff, but it might improve server performance.
107 This value cannot exceed INT_MAX / max (X_LONG_SIZE, sizeof (long))
108 because it is multiplied by X_LONG_SIZE and by sizeof (long) in
109 subscript calculations. Similarly for PTRDIFF_MAX - 1 or SIZE_MAX
110 - 1 in place of INT_MAX. */
111 #define MAX_SELECTION_QUANTUM \
112 ((int) min (0xFFFFFF, (min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) - 1) \
113 / max (X_LONG_SIZE, sizeof (long)))))
115 static int
116 selection_quantum (Display *display)
118 long mrs = XMaxRequestSize (display);
119 return (mrs < MAX_SELECTION_QUANTUM / X_LONG_SIZE + 25
120 ? (mrs - 25) * X_LONG_SIZE
121 : MAX_SELECTION_QUANTUM);
124 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
125 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
128 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
129 handling. */
131 struct selection_event_queue
133 struct input_event event;
134 struct selection_event_queue *next;
137 static struct selection_event_queue *selection_queue;
139 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
141 static int x_queue_selection_requests;
143 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
145 static void
146 x_queue_event (struct input_event *event)
148 struct selection_event_queue *queue_tmp;
150 /* Don't queue repeated requests.
151 This only happens for large requests which uses the incremental protocol. */
152 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
154 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
156 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
157 x_decline_selection_request (event);
158 return;
162 queue_tmp = xmalloc (sizeof *queue_tmp);
163 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
164 queue_tmp->event = *event;
165 queue_tmp->next = selection_queue;
166 selection_queue = queue_tmp;
169 /* Start queuing SELECTION_REQUEST_EVENT events. */
171 static void
172 x_start_queuing_selection_requests (void)
174 if (x_queue_selection_requests)
175 emacs_abort ();
177 x_queue_selection_requests++;
178 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
181 /* Stop queuing SELECTION_REQUEST_EVENT events. */
183 static void
184 x_stop_queuing_selection_requests (void)
186 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
187 --x_queue_selection_requests;
189 /* Take all the queued events and put them back
190 so that they get processed afresh. */
192 while (selection_queue != NULL)
194 struct selection_event_queue *queue_tmp = selection_queue;
195 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
196 kbd_buffer_unget_event (&queue_tmp->event);
197 selection_queue = queue_tmp->next;
198 xfree (queue_tmp);
203 /* This converts a Lisp symbol to a server Atom, avoiding a server
204 roundtrip whenever possible. */
206 static Atom
207 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
209 Atom val;
210 if (NILP (sym)) return 0;
211 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
212 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
213 if (EQ (sym, QSTRING)) return XA_STRING;
214 if (EQ (sym, QINTEGER)) return XA_INTEGER;
215 if (EQ (sym, QATOM)) return XA_ATOM;
216 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
217 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
218 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
219 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
220 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
221 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
222 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
223 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
224 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
225 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
226 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
227 if (!SYMBOLP (sym)) emacs_abort ();
229 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
230 block_input ();
231 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
232 unblock_input ();
233 return val;
237 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
238 and calls to intern whenever possible. */
240 static Lisp_Object
241 x_atom_to_symbol (struct x_display_info *dpyinfo, Atom atom)
243 char *str;
244 Lisp_Object val;
246 if (! atom)
247 return Qnil;
249 switch (atom)
251 case XA_PRIMARY:
252 return QPRIMARY;
253 case XA_SECONDARY:
254 return QSECONDARY;
255 case XA_STRING:
256 return QSTRING;
257 case XA_INTEGER:
258 return QINTEGER;
259 case XA_ATOM:
260 return QATOM;
263 if (dpyinfo == NULL)
264 return Qnil;
265 if (atom == dpyinfo->Xatom_CLIPBOARD)
266 return QCLIPBOARD;
267 if (atom == dpyinfo->Xatom_TIMESTAMP)
268 return QTIMESTAMP;
269 if (atom == dpyinfo->Xatom_TEXT)
270 return QTEXT;
271 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
272 return QCOMPOUND_TEXT;
273 if (atom == dpyinfo->Xatom_UTF8_STRING)
274 return QUTF8_STRING;
275 if (atom == dpyinfo->Xatom_DELETE)
276 return QDELETE;
277 if (atom == dpyinfo->Xatom_MULTIPLE)
278 return QMULTIPLE;
279 if (atom == dpyinfo->Xatom_INCR)
280 return QINCR;
281 if (atom == dpyinfo->Xatom_EMACS_TMP)
282 return QEMACS_TMP;
283 if (atom == dpyinfo->Xatom_TARGETS)
284 return QTARGETS;
285 if (atom == dpyinfo->Xatom_NULL)
286 return QNULL;
288 block_input ();
289 str = XGetAtomName (dpyinfo->display, atom);
290 unblock_input ();
291 TRACE1 ("XGetAtomName --> %s", str);
292 if (! str) return Qnil;
293 val = intern (str);
294 block_input ();
295 /* This was allocated by Xlib, so use XFree. */
296 XFree (str);
297 unblock_input ();
298 return val;
301 /* Do protocol to assert ourself as a selection owner.
302 FRAME shall be the owner; it must be a valid X frame.
303 Update the Vselection_alist so that we can reply to later requests for
304 our selection. */
306 static void
307 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
308 Lisp_Object frame)
310 struct frame *f = XFRAME (frame);
311 Window selecting_window = FRAME_X_WINDOW (f);
312 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
313 Display *display = dpyinfo->display;
314 Time timestamp = dpyinfo->last_user_time;
315 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
317 block_input ();
318 x_catch_errors (display);
319 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
320 x_check_errors (display, "Can't set selection: %s");
321 x_uncatch_errors ();
322 unblock_input ();
324 /* Now update the local cache */
326 Lisp_Object selection_data;
327 Lisp_Object prev_value;
329 selection_data = list4 (selection_name, selection_value,
330 INTEGER_TO_CONS (timestamp), frame);
331 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
333 tset_selection_alist
334 (dpyinfo->terminal,
335 Fcons (selection_data, dpyinfo->terminal->Vselection_alist));
337 /* If we already owned the selection, remove the old selection
338 data. Don't use Fdelq as that may QUIT. */
339 if (!NILP (prev_value))
341 /* We know it's not the CAR, so it's easy. */
342 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
343 for (; CONSP (rest); rest = XCDR (rest))
344 if (EQ (prev_value, Fcar (XCDR (rest))))
346 XSETCDR (rest, XCDR (XCDR (rest)));
347 break;
353 /* Given a selection-name and desired type, look up our local copy of
354 the selection value and convert it to the type.
355 Return nil, a string, a vector, a symbol, an integer, or a cons
356 that CONS_TO_INTEGER could plausibly handle.
357 This function is used both for remote requests (LOCAL_REQUEST is zero)
358 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
360 This calls random Lisp code, and may signal or gc. */
362 static Lisp_Object
363 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
364 bool local_request, struct x_display_info *dpyinfo)
366 Lisp_Object local_value;
367 Lisp_Object handler_fn, value, check;
369 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
371 if (NILP (local_value)) return Qnil;
373 /* TIMESTAMP is a special case. */
374 if (EQ (target_type, QTIMESTAMP))
376 handler_fn = Qnil;
377 value = XCAR (XCDR (XCDR (local_value)));
379 else
381 /* Don't allow a quit within the converter.
382 When the user types C-g, he would be surprised
383 if by luck it came during a converter. */
384 ptrdiff_t count = SPECPDL_INDEX ();
385 specbind (Qinhibit_quit, Qt);
387 CHECK_SYMBOL (target_type);
388 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
389 /* gcpro is not needed here since nothing but HANDLER_FN
390 is live, and that ought to be a symbol. */
392 if (!NILP (handler_fn))
393 value = call3 (handler_fn,
394 selection_symbol, (local_request ? Qnil : target_type),
395 XCAR (XCDR (local_value)));
396 else
397 value = Qnil;
398 unbind_to (count, Qnil);
401 /* Make sure this value is of a type that we could transmit
402 to another X client. */
404 check = value;
405 if (CONSP (value)
406 && SYMBOLP (XCAR (value)))
407 check = XCDR (value);
409 if (STRINGP (check)
410 || VECTORP (check)
411 || SYMBOLP (check)
412 || INTEGERP (check)
413 || NILP (value))
414 return value;
415 /* Check for a value that CONS_TO_INTEGER could handle. */
416 else if (CONSP (check)
417 && INTEGERP (XCAR (check))
418 && (INTEGERP (XCDR (check))
420 (CONSP (XCDR (check))
421 && INTEGERP (XCAR (XCDR (check)))
422 && NILP (XCDR (XCDR (check))))))
423 return value;
425 signal_error ("Invalid data returned by selection-conversion function",
426 list2 (handler_fn, value));
429 /* Subroutines of x_reply_selection_request. */
431 /* Send a SelectionNotify event to the requestor with property=None,
432 meaning we were unable to do what they wanted. */
434 static void
435 x_decline_selection_request (struct input_event *event)
437 XEvent reply_base;
438 XSelectionEvent *reply = &(reply_base.xselection);
440 reply->type = SelectionNotify;
441 reply->display = SELECTION_EVENT_DISPLAY (event);
442 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
443 reply->selection = SELECTION_EVENT_SELECTION (event);
444 reply->time = SELECTION_EVENT_TIME (event);
445 reply->target = SELECTION_EVENT_TARGET (event);
446 reply->property = None;
448 /* The reason for the error may be that the receiver has
449 died in the meantime. Handle that case. */
450 block_input ();
451 x_catch_errors (reply->display);
452 XSendEvent (reply->display, reply->requestor, False, 0, &reply_base);
453 XFlush (reply->display);
454 x_uncatch_errors ();
455 unblock_input ();
458 /* This is the selection request currently being processed.
459 It is set to zero when the request is fully processed. */
460 static struct input_event *x_selection_current_request;
462 /* Display info in x_selection_request. */
464 static struct x_display_info *selection_request_dpyinfo;
466 /* Raw selection data, for sending to a requestor window. */
468 struct selection_data
470 unsigned char *data;
471 ptrdiff_t size;
472 int format;
473 Atom type;
474 bool nofree;
475 Atom property;
476 /* This can be set to non-NULL during x_reply_selection_request, if
477 the selection is waiting for an INCR transfer to complete. Don't
478 free these; that's done by unexpect_property_change. */
479 struct prop_location *wait_object;
480 struct selection_data *next;
483 /* Linked list of the above (in support of MULTIPLE targets). */
485 static struct selection_data *converted_selections;
487 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
488 static Atom conversion_fail_tag;
490 /* Used as an unwind-protect clause so that, if a selection-converter signals
491 an error, we tell the requestor that we were unable to do what they wanted
492 before we throw to top-level or go into the debugger or whatever. */
494 static void
495 x_selection_request_lisp_error (void)
497 struct selection_data *cs, *next;
499 for (cs = converted_selections; cs; cs = next)
501 next = cs->next;
502 if (! cs->nofree && cs->data)
503 xfree (cs->data);
504 xfree (cs);
506 converted_selections = NULL;
508 if (x_selection_current_request != 0
509 && selection_request_dpyinfo->display)
510 x_decline_selection_request (x_selection_current_request);
513 static void
514 x_catch_errors_unwind (void)
516 block_input ();
517 x_uncatch_errors ();
518 unblock_input ();
522 /* This stuff is so that INCR selections are reentrant (that is, so we can
523 be servicing multiple INCR selection requests simultaneously.) I haven't
524 actually tested that yet. */
526 /* Keep a list of the property changes that are awaited. */
528 struct prop_location
530 int identifier;
531 Display *display;
532 Window window;
533 Atom property;
534 int desired_state;
535 bool arrived;
536 struct prop_location *next;
539 static int prop_location_identifier;
541 static Lisp_Object property_change_reply;
543 static struct prop_location *property_change_reply_object;
545 static struct prop_location *property_change_wait_list;
548 /* Send the reply to a selection request event EVENT. */
550 #ifdef TRACE_SELECTION
551 static int x_reply_selection_request_cnt;
552 #endif /* TRACE_SELECTION */
554 static void
555 x_reply_selection_request (struct input_event *event,
556 struct x_display_info *dpyinfo)
558 XEvent reply_base;
559 XSelectionEvent *reply = &(reply_base.xselection);
560 Display *display = SELECTION_EVENT_DISPLAY (event);
561 Window window = SELECTION_EVENT_REQUESTOR (event);
562 ptrdiff_t bytes_remaining;
563 int max_bytes = selection_quantum (display);
564 ptrdiff_t count = SPECPDL_INDEX ();
565 struct selection_data *cs;
567 reply->type = SelectionNotify;
568 reply->display = display;
569 reply->requestor = window;
570 reply->selection = SELECTION_EVENT_SELECTION (event);
571 reply->time = SELECTION_EVENT_TIME (event);
572 reply->target = SELECTION_EVENT_TARGET (event);
573 reply->property = SELECTION_EVENT_PROPERTY (event);
574 if (reply->property == None)
575 reply->property = reply->target;
577 block_input ();
578 /* The protected block contains wait_for_property_change, which can
579 run random lisp code (process handlers) or signal. Therefore, we
580 put the x_uncatch_errors call in an unwind. */
581 record_unwind_protect_void (x_catch_errors_unwind);
582 x_catch_errors (display);
584 /* Loop over converted selections, storing them in the requested
585 properties. If data is large, only store the first N bytes
586 (section 2.7.2 of ICCCM). Note that we store the data for a
587 MULTIPLE request in the opposite order; the ICCM says only that
588 the conversion itself must be done in the same order. */
589 for (cs = converted_selections; cs; cs = cs->next)
591 if (cs->property == None)
592 continue;
594 bytes_remaining = cs->size;
595 bytes_remaining *= cs->format >> 3;
596 if (bytes_remaining <= max_bytes)
598 /* Send all the data at once, with minimal handshaking. */
599 TRACE1 ("Sending all %"pD"d bytes", bytes_remaining);
600 XChangeProperty (display, window, cs->property,
601 cs->type, cs->format, PropModeReplace,
602 cs->data, cs->size);
604 else
606 /* Send an INCR tag to initiate incremental transfer. */
607 long value[1];
609 TRACE2 ("Start sending %"pD"d bytes incrementally (%s)",
610 bytes_remaining, XGetAtomName (display, cs->property));
611 cs->wait_object
612 = expect_property_change (display, window, cs->property,
613 PropertyDelete);
615 /* XChangeProperty expects an array of long even if long is
616 more than 32 bits. */
617 value[0] = min (bytes_remaining, X_LONG_MAX);
618 XChangeProperty (display, window, cs->property,
619 dpyinfo->Xatom_INCR, 32, PropModeReplace,
620 (unsigned char *) value, 1);
621 XSelectInput (display, window, PropertyChangeMask);
625 /* Now issue the SelectionNotify event. */
626 XSendEvent (display, window, False, 0, &reply_base);
627 XFlush (display);
629 #ifdef TRACE_SELECTION
631 char *sel = XGetAtomName (display, reply->selection);
632 char *tgt = XGetAtomName (display, reply->target);
633 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
634 sel, tgt, ++x_reply_selection_request_cnt);
635 if (sel) XFree (sel);
636 if (tgt) XFree (tgt);
638 #endif /* TRACE_SELECTION */
640 /* Finish sending the rest of each of the INCR values. This should
641 be improved; there's a chance of deadlock if more than one
642 subtarget in a MULTIPLE selection requires an INCR transfer, and
643 the requestor and Emacs loop waiting on different transfers. */
644 for (cs = converted_selections; cs; cs = cs->next)
645 if (cs->wait_object)
647 int format_bytes = cs->format / 8;
648 bool had_errors_p = x_had_errors_p (display);
649 unblock_input ();
651 bytes_remaining = cs->size;
652 bytes_remaining *= format_bytes;
654 /* Wait for the requestor to ack by deleting the property.
655 This can run Lisp code (process handlers) or signal. */
656 if (! had_errors_p)
658 TRACE1 ("Waiting for ACK (deletion of %s)",
659 XGetAtomName (display, cs->property));
660 wait_for_property_change (cs->wait_object);
662 else
663 unexpect_property_change (cs->wait_object);
665 while (bytes_remaining)
667 int i = ((bytes_remaining < max_bytes)
668 ? bytes_remaining
669 : max_bytes) / format_bytes;
670 block_input ();
672 cs->wait_object
673 = expect_property_change (display, window, cs->property,
674 PropertyDelete);
676 TRACE1 ("Sending increment of %d elements", i);
677 TRACE1 ("Set %s to increment data",
678 XGetAtomName (display, cs->property));
680 /* Append the next chunk of data to the property. */
681 XChangeProperty (display, window, cs->property,
682 cs->type, cs->format, PropModeAppend,
683 cs->data, i);
684 bytes_remaining -= i * format_bytes;
685 cs->data += i * ((cs->format == 32) ? sizeof (long)
686 : format_bytes);
687 XFlush (display);
688 had_errors_p = x_had_errors_p (display);
689 unblock_input ();
691 if (had_errors_p) break;
693 /* Wait for the requestor to ack this chunk by deleting
694 the property. This can run Lisp code or signal. */
695 TRACE1 ("Waiting for increment ACK (deletion of %s)",
696 XGetAtomName (display, cs->property));
697 wait_for_property_change (cs->wait_object);
700 /* Now write a zero-length chunk to the property to tell the
701 requestor that we're done. */
702 block_input ();
703 if (! waiting_for_other_props_on_window (display, window))
704 XSelectInput (display, window, 0);
706 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
707 XGetAtomName (display, cs->property));
708 XChangeProperty (display, window, cs->property,
709 cs->type, cs->format, PropModeReplace,
710 cs->data, 0);
711 TRACE0 ("Done sending incrementally");
714 /* rms, 2003-01-03: I think I have fixed this bug. */
715 /* The window we're communicating with may have been deleted
716 in the meantime (that's a real situation from a bug report).
717 In this case, there may be events in the event queue still
718 referring to the deleted window, and we'll get a BadWindow error
719 in XTread_socket when processing the events. I don't have
720 an idea how to fix that. gerd, 2001-01-98. */
721 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
722 delivered before uncatch errors. */
723 XSync (display, False);
724 unblock_input ();
726 /* GTK queues events in addition to the queue in Xlib. So we
727 UNBLOCK to enter the event loop and get possible errors delivered,
728 and then BLOCK again because x_uncatch_errors requires it. */
729 block_input ();
730 /* This calls x_uncatch_errors. */
731 unbind_to (count, Qnil);
732 unblock_input ();
735 /* Handle a SelectionRequest event EVENT.
736 This is called from keyboard.c when such an event is found in the queue. */
738 static void
739 x_handle_selection_request (struct input_event *event)
741 struct gcpro gcpro1, gcpro2;
742 Time local_selection_time;
744 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
745 Atom selection = SELECTION_EVENT_SELECTION (event);
746 Lisp_Object selection_symbol = x_atom_to_symbol (dpyinfo, selection);
747 Atom target = SELECTION_EVENT_TARGET (event);
748 Lisp_Object target_symbol = x_atom_to_symbol (dpyinfo, target);
749 Atom property = SELECTION_EVENT_PROPERTY (event);
750 Lisp_Object local_selection_data;
751 bool success = false;
752 ptrdiff_t count = SPECPDL_INDEX ();
753 GCPRO2 (local_selection_data, target_symbol);
755 if (!dpyinfo) goto DONE;
757 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
759 /* Decline if we don't own any selections. */
760 if (NILP (local_selection_data)) goto DONE;
762 /* Decline requests issued prior to our acquiring the selection. */
763 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
764 Time, local_selection_time);
765 if (SELECTION_EVENT_TIME (event) != CurrentTime
766 && local_selection_time > SELECTION_EVENT_TIME (event))
767 goto DONE;
769 x_selection_current_request = event;
770 selection_request_dpyinfo = dpyinfo;
771 record_unwind_protect_void (x_selection_request_lisp_error);
773 /* We might be able to handle nested x_handle_selection_requests,
774 but this is difficult to test, and seems unimportant. */
775 x_start_queuing_selection_requests ();
776 record_unwind_protect_void (x_stop_queuing_selection_requests);
778 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
779 SDATA (SYMBOL_NAME (selection_symbol)),
780 SDATA (SYMBOL_NAME (target_symbol)));
782 if (EQ (target_symbol, QMULTIPLE))
784 /* For MULTIPLE targets, the event property names a list of atom
785 pairs; the first atom names a target and the second names a
786 non-None property. */
787 Window requestor = SELECTION_EVENT_REQUESTOR (event);
788 Lisp_Object multprop;
789 ptrdiff_t j, nselections;
791 if (property == None) goto DONE;
792 multprop
793 = x_get_window_property_as_lisp_data (dpyinfo, requestor, property,
794 QMULTIPLE, selection);
796 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
797 goto DONE;
799 nselections = ASIZE (multprop) / 2;
800 /* Perform conversions. This can signal. */
801 for (j = 0; j < nselections; j++)
803 Lisp_Object subtarget = AREF (multprop, 2*j);
804 Atom subproperty = symbol_to_x_atom (dpyinfo,
805 AREF (multprop, 2*j+1));
807 if (subproperty != None)
808 x_convert_selection (event, selection_symbol, subtarget,
809 subproperty, true, dpyinfo);
811 success = true;
813 else
815 if (property == None)
816 property = SELECTION_EVENT_TARGET (event);
817 success = x_convert_selection (event, selection_symbol,
818 target_symbol, property,
819 false, dpyinfo);
822 DONE:
824 if (success)
825 x_reply_selection_request (event, dpyinfo);
826 else
827 x_decline_selection_request (event);
828 x_selection_current_request = 0;
830 /* Run the `x-sent-selection-functions' abnormal hook. */
831 if (!NILP (Vx_sent_selection_functions)
832 && !EQ (Vx_sent_selection_functions, Qunbound))
834 Lisp_Object args[4];
835 args[0] = Qx_sent_selection_functions;
836 args[1] = selection_symbol;
837 args[2] = target_symbol;
838 args[3] = success ? Qt : Qnil;
839 Frun_hook_with_args (4, args);
842 unbind_to (count, Qnil);
843 UNGCPRO;
846 /* Perform the requested selection conversion, and write the data to
847 the converted_selections linked list, where it can be accessed by
848 x_reply_selection_request. If FOR_MULTIPLE, write out
849 the data even if conversion fails, using conversion_fail_tag.
851 Return true iff successful. */
853 static bool
854 x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
855 Lisp_Object target_symbol, Atom property,
856 bool for_multiple, struct x_display_info *dpyinfo)
858 struct gcpro gcpro1;
859 Lisp_Object lisp_selection;
860 struct selection_data *cs;
861 GCPRO1 (lisp_selection);
863 lisp_selection
864 = x_get_local_selection (selection_symbol, target_symbol,
865 false, dpyinfo);
867 /* A nil return value means we can't perform the conversion. */
868 if (NILP (lisp_selection)
869 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
871 if (for_multiple)
873 cs = xmalloc (sizeof *cs);
874 cs->data = (unsigned char *) &conversion_fail_tag;
875 cs->size = 1;
876 cs->format = 32;
877 cs->type = XA_ATOM;
878 cs->nofree = true;
879 cs->property = property;
880 cs->wait_object = NULL;
881 cs->next = converted_selections;
882 converted_selections = cs;
885 UNGCPRO;
886 return false;
889 /* Otherwise, record the converted selection to binary. */
890 cs = xmalloc (sizeof *cs);
891 cs->data = NULL;
892 cs->nofree = true;
893 cs->property = property;
894 cs->wait_object = NULL;
895 cs->next = converted_selections;
896 converted_selections = cs;
897 lisp_data_to_selection_data (dpyinfo, lisp_selection, cs);
898 UNGCPRO;
899 return true;
902 /* Handle a SelectionClear event EVENT, which indicates that some
903 client cleared out our previously asserted selection.
904 This is called from keyboard.c when such an event is found in the queue. */
906 static void
907 x_handle_selection_clear (struct input_event *event)
909 Atom selection = SELECTION_EVENT_SELECTION (event);
910 Time changed_owner_time = SELECTION_EVENT_TIME (event);
912 Lisp_Object selection_symbol, local_selection_data;
913 Time local_selection_time;
914 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
915 Lisp_Object Vselection_alist;
917 TRACE0 ("x_handle_selection_clear");
919 if (!dpyinfo) return;
921 selection_symbol = x_atom_to_symbol (dpyinfo, selection);
922 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
924 /* Well, we already believe that we don't own it, so that's just fine. */
925 if (NILP (local_selection_data)) return;
927 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
928 Time, local_selection_time);
930 /* We have reasserted the selection since this SelectionClear was
931 generated, so we can disregard it. */
932 if (changed_owner_time != CurrentTime
933 && local_selection_time > changed_owner_time)
934 return;
936 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
937 Vselection_alist = dpyinfo->terminal->Vselection_alist;
938 if (EQ (local_selection_data, CAR (Vselection_alist)))
939 Vselection_alist = XCDR (Vselection_alist);
940 else
942 Lisp_Object rest;
943 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
944 if (EQ (local_selection_data, CAR (XCDR (rest))))
946 XSETCDR (rest, XCDR (XCDR (rest)));
947 break;
950 tset_selection_alist (dpyinfo->terminal, Vselection_alist);
952 /* Run the `x-lost-selection-functions' abnormal hook. */
954 Lisp_Object args[2];
955 args[0] = Qx_lost_selection_functions;
956 args[1] = selection_symbol;
957 Frun_hook_with_args (2, args);
960 redisplay_preserve_echo_area (20);
963 void
964 x_handle_selection_event (struct input_event *event)
966 TRACE0 ("x_handle_selection_event");
967 if (event->kind != SELECTION_REQUEST_EVENT)
968 x_handle_selection_clear (event);
969 else if (x_queue_selection_requests)
970 x_queue_event (event);
971 else
972 x_handle_selection_request (event);
976 /* Clear all selections that were made from frame F.
977 We do this when about to delete a frame. */
979 void
980 x_clear_frame_selections (struct frame *f)
982 Lisp_Object frame;
983 Lisp_Object rest;
984 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
985 struct terminal *t = dpyinfo->terminal;
987 XSETFRAME (frame, f);
989 /* Delete elements from the beginning of Vselection_alist. */
990 while (CONSP (t->Vselection_alist)
991 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
993 /* Run the `x-lost-selection-functions' abnormal hook. */
994 Lisp_Object args[2];
995 args[0] = Qx_lost_selection_functions;
996 args[1] = Fcar (Fcar (t->Vselection_alist));
997 Frun_hook_with_args (2, args);
999 tset_selection_alist (t, XCDR (t->Vselection_alist));
1002 /* Delete elements after the beginning of Vselection_alist. */
1003 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
1004 if (CONSP (XCDR (rest))
1005 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
1007 Lisp_Object args[2];
1008 args[0] = Qx_lost_selection_functions;
1009 args[1] = XCAR (XCAR (XCDR (rest)));
1010 Frun_hook_with_args (2, args);
1011 XSETCDR (rest, XCDR (XCDR (rest)));
1012 break;
1016 /* True if any properties for DISPLAY and WINDOW
1017 are on the list of what we are waiting for. */
1019 static bool
1020 waiting_for_other_props_on_window (Display *display, Window window)
1022 for (struct prop_location *p = property_change_wait_list; p; p = p->next)
1023 if (p->display == display && p->window == window)
1024 return true;
1025 return false;
1028 /* Add an entry to the list of property changes we are waiting for.
1029 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1030 The return value is a number that uniquely identifies
1031 this awaited property change. */
1033 static struct prop_location *
1034 expect_property_change (Display *display, Window window,
1035 Atom property, int state)
1037 struct prop_location *pl = xmalloc (sizeof *pl);
1038 pl->identifier = ++prop_location_identifier;
1039 pl->display = display;
1040 pl->window = window;
1041 pl->property = property;
1042 pl->desired_state = state;
1043 pl->next = property_change_wait_list;
1044 pl->arrived = false;
1045 property_change_wait_list = pl;
1046 return pl;
1049 /* Delete an entry from the list of property changes we are waiting for.
1050 IDENTIFIER is the number that uniquely identifies the entry. */
1052 static void
1053 unexpect_property_change (struct prop_location *location)
1055 struct prop_location *prop, **pprev = &property_change_wait_list;
1057 for (prop = property_change_wait_list; prop; prop = *pprev)
1059 if (prop == location)
1061 *pprev = prop->next;
1062 xfree (prop);
1063 break;
1065 else
1066 pprev = &prop->next;
1070 /* Remove the property change expectation element for IDENTIFIER. */
1072 static void
1073 wait_for_property_change_unwind (void *loc)
1075 struct prop_location *location = loc;
1077 unexpect_property_change (location);
1078 if (location == property_change_reply_object)
1079 property_change_reply_object = 0;
1082 /* Actually wait for a property change.
1083 IDENTIFIER should be the value that expect_property_change returned. */
1085 static void
1086 wait_for_property_change (struct prop_location *location)
1088 ptrdiff_t count = SPECPDL_INDEX ();
1090 if (property_change_reply_object)
1091 emacs_abort ();
1093 /* Make sure to do unexpect_property_change if we quit or err. */
1094 record_unwind_protect_ptr (wait_for_property_change_unwind, location);
1096 XSETCAR (property_change_reply, Qnil);
1097 property_change_reply_object = location;
1099 /* If the event we are waiting for arrives beyond here, it will set
1100 property_change_reply, because property_change_reply_object says so. */
1101 if (! location->arrived)
1103 EMACS_INT timeout = max (0, x_selection_timeout);
1104 EMACS_INT secs = timeout / 1000;
1105 int nsecs = (timeout % 1000) * 1000000;
1106 TRACE2 (" Waiting %"pI"d secs, %d nsecs", secs, nsecs);
1107 wait_reading_process_output (secs, nsecs, 0, false,
1108 property_change_reply, NULL, 0);
1110 if (NILP (XCAR (property_change_reply)))
1112 TRACE0 (" Timed out");
1113 error ("Timed out waiting for property-notify event");
1117 unbind_to (count, Qnil);
1120 /* Called from XTread_socket in response to a PropertyNotify event. */
1122 void
1123 x_handle_property_notify (const XPropertyEvent *event)
1125 struct prop_location *rest;
1127 for (rest = property_change_wait_list; rest; rest = rest->next)
1129 if (!rest->arrived
1130 && rest->property == event->atom
1131 && rest->window == event->window
1132 && rest->display == event->display
1133 && rest->desired_state == event->state)
1135 TRACE2 ("Expected %s of property %s",
1136 (event->state == PropertyDelete ? "deletion" : "change"),
1137 XGetAtomName (event->display, event->atom));
1139 rest->arrived = true;
1141 /* If this is the one wait_for_property_change is waiting for,
1142 tell it to wake up. */
1143 if (rest == property_change_reply_object)
1144 XSETCAR (property_change_reply, Qt);
1146 return;
1153 /* Variables for communication with x_handle_selection_notify. */
1154 static Atom reading_which_selection;
1155 static Lisp_Object reading_selection_reply;
1156 static Window reading_selection_window;
1158 /* Do protocol to read selection-data from the server.
1159 Converts this to Lisp data and returns it.
1160 FRAME is the frame whose X window shall request the selection. */
1162 static Lisp_Object
1163 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1164 Lisp_Object time_stamp, Lisp_Object frame)
1166 struct frame *f = XFRAME (frame);
1167 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1168 Display *display = dpyinfo->display;
1169 Window requestor_window = FRAME_X_WINDOW (f);
1170 Time requestor_time = dpyinfo->last_user_time;
1171 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1172 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1173 Atom type_atom = (CONSP (target_type)
1174 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1175 : symbol_to_x_atom (dpyinfo, target_type));
1176 EMACS_INT timeout, secs;
1177 int nsecs;
1179 if (!FRAME_LIVE_P (f))
1180 return Qnil;
1182 if (! NILP (time_stamp))
1183 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1185 block_input ();
1186 TRACE2 ("Get selection %s, type %s",
1187 XGetAtomName (display, type_atom),
1188 XGetAtomName (display, target_property));
1190 x_catch_errors (display);
1191 XConvertSelection (display, selection_atom, type_atom, target_property,
1192 requestor_window, requestor_time);
1193 x_check_errors (display, "Can't convert selection: %s");
1194 x_uncatch_errors ();
1196 /* Prepare to block until the reply has been read. */
1197 reading_selection_window = requestor_window;
1198 reading_which_selection = selection_atom;
1199 XSETCAR (reading_selection_reply, Qnil);
1201 /* It should not be necessary to stop handling selection requests
1202 during this time. In fact, the SAVE_TARGETS mechanism requires
1203 us to handle a clipboard manager's requests before it returns
1204 SelectionNotify. */
1205 #if false
1206 x_start_queuing_selection_requests ();
1207 record_unwind_protect_void (x_stop_queuing_selection_requests);
1208 #endif
1210 unblock_input ();
1212 /* This allows quits. Also, don't wait forever. */
1213 timeout = max (0, x_selection_timeout);
1214 secs = timeout / 1000;
1215 nsecs = (timeout % 1000) * 1000000;
1216 TRACE1 (" Start waiting %"pI"d secs for SelectionNotify", secs);
1217 wait_reading_process_output (secs, nsecs, 0, false,
1218 reading_selection_reply, NULL, 0);
1219 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1221 if (NILP (XCAR (reading_selection_reply)))
1222 error ("Timed out waiting for reply from selection owner");
1223 if (EQ (XCAR (reading_selection_reply), Qlambda))
1224 return Qnil;
1226 /* Otherwise, the selection is waiting for us on the requested property. */
1227 return
1228 x_get_window_property_as_lisp_data (dpyinfo, requestor_window,
1229 target_property, target_type,
1230 selection_atom);
1233 /* Subroutines of x_get_window_property_as_lisp_data */
1235 /* Use xfree, not XFree, to free the data obtained with this function. */
1237 static void
1238 x_get_window_property (Display *display, Window window, Atom property,
1239 unsigned char **data_ret, ptrdiff_t *bytes_ret,
1240 Atom *actual_type_ret, int *actual_format_ret,
1241 unsigned long *actual_size_ret)
1243 ptrdiff_t total_size;
1244 unsigned long bytes_remaining;
1245 ptrdiff_t offset = 0;
1246 unsigned char *data = 0;
1247 unsigned char *tmp_data = 0;
1248 int result;
1249 int buffer_size = selection_quantum (display);
1251 /* Wide enough to avoid overflow in expressions using it. */
1252 ptrdiff_t x_long_size = X_LONG_SIZE;
1254 /* Maximum value for TOTAL_SIZE. It cannot exceed PTRDIFF_MAX - 1
1255 and SIZE_MAX - 1, for an extra byte at the end. And it cannot
1256 exceed LONG_MAX * X_LONG_SIZE, for XGetWindowProperty. */
1257 ptrdiff_t total_size_max =
1258 ((min (PTRDIFF_MAX, SIZE_MAX) - 1) / x_long_size < LONG_MAX
1259 ? min (PTRDIFF_MAX, SIZE_MAX) - 1
1260 : LONG_MAX * x_long_size);
1262 block_input ();
1264 /* First probe the thing to find out how big it is. */
1265 result = XGetWindowProperty (display, window, property,
1266 0, 0, False, AnyPropertyType,
1267 actual_type_ret, actual_format_ret,
1268 actual_size_ret,
1269 &bytes_remaining, &tmp_data);
1270 if (result != Success)
1271 goto done;
1273 /* This was allocated by Xlib, so use XFree. */
1274 XFree (tmp_data);
1276 if (*actual_type_ret == None || *actual_format_ret == 0)
1277 goto done;
1279 if (total_size_max < bytes_remaining)
1280 goto size_overflow;
1281 total_size = bytes_remaining;
1282 data = xmalloc (total_size + 1);
1284 /* Now read, until we've gotten it all. */
1285 while (bytes_remaining)
1287 ptrdiff_t bytes_gotten;
1288 int bytes_per_item;
1289 result
1290 = XGetWindowProperty (display, window, property,
1291 offset / X_LONG_SIZE,
1292 buffer_size / X_LONG_SIZE,
1293 False,
1294 AnyPropertyType,
1295 actual_type_ret, actual_format_ret,
1296 actual_size_ret, &bytes_remaining, &tmp_data);
1298 /* If this doesn't return Success at this point, it means that
1299 some clod deleted the selection while we were in the midst of
1300 reading it. Deal with that, I guess.... */
1301 if (result != Success)
1302 break;
1304 bytes_per_item = *actual_format_ret >> 3;
1305 eassert (*actual_size_ret <= buffer_size / bytes_per_item);
1307 /* The man page for XGetWindowProperty says:
1308 "If the returned format is 32, the returned data is represented
1309 as a long array and should be cast to that type to obtain the
1310 elements."
1311 This applies even if long is more than 32 bits, the X library
1312 converts from 32 bit elements received from the X server to long
1313 and passes the long array to us. Thus, for that case memcpy can not
1314 be used. We convert to a 32 bit type here, because so much code
1315 assume on that.
1317 The bytes and offsets passed to XGetWindowProperty refers to the
1318 property and those are indeed in 32 bit quantities if format is 32. */
1320 bytes_gotten = *actual_size_ret;
1321 bytes_gotten *= bytes_per_item;
1323 TRACE2 ("Read %"pD"d bytes from property %s",
1324 bytes_gotten, XGetAtomName (display, property));
1326 if (total_size - offset < bytes_gotten)
1328 unsigned char *data1;
1329 ptrdiff_t remaining_lim = total_size_max - offset - bytes_gotten;
1330 if (remaining_lim < 0 || remaining_lim < bytes_remaining)
1331 goto size_overflow;
1332 total_size = offset + bytes_gotten + bytes_remaining;
1333 data1 = xrealloc (data, total_size + 1);
1334 data = data1;
1337 if (BITS_PER_LONG > 32 && *actual_format_ret == 32)
1339 unsigned long i;
1340 int *idata = (int *) (data + offset);
1341 long *ldata = (long *) tmp_data;
1343 for (i = 0; i < *actual_size_ret; ++i)
1344 idata[i] = ldata[i];
1346 else
1347 memcpy (data + offset, tmp_data, bytes_gotten);
1349 offset += bytes_gotten;
1351 /* This was allocated by Xlib, so use XFree. */
1352 XFree (tmp_data);
1355 XFlush (display);
1356 data[offset] = '\0';
1358 done:
1359 unblock_input ();
1360 *data_ret = data;
1361 *bytes_ret = offset;
1362 return;
1364 size_overflow:
1365 if (data)
1366 xfree (data);
1367 unblock_input ();
1368 memory_full (SIZE_MAX);
1371 /* Use xfree, not XFree, to free the data obtained with this function. */
1373 static void
1374 receive_incremental_selection (struct x_display_info *dpyinfo,
1375 Window window, Atom property,
1376 Lisp_Object target_type,
1377 unsigned int min_size_bytes,
1378 unsigned char **data_ret,
1379 ptrdiff_t *size_bytes_ret,
1380 Atom *type_ret, int *format_ret,
1381 unsigned long *size_ret)
1383 ptrdiff_t offset = 0;
1384 struct prop_location *wait_object;
1385 Display *display = dpyinfo->display;
1387 if (min (PTRDIFF_MAX, SIZE_MAX) < min_size_bytes)
1388 memory_full (SIZE_MAX);
1389 *data_ret = xmalloc (min_size_bytes);
1390 *size_bytes_ret = min_size_bytes;
1392 TRACE1 ("Read %u bytes incrementally", min_size_bytes);
1394 /* At this point, we have read an INCR property.
1395 Delete the property to ack it.
1396 (But first, prepare to receive the next event in this handshake.)
1398 Now, we must loop, waiting for the sending window to put a value on
1399 that property, then reading the property, then deleting it to ack.
1400 We are done when the sender places a property of length 0.
1402 block_input ();
1403 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1404 TRACE1 (" Delete property %s",
1405 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1406 XDeleteProperty (display, window, property);
1407 TRACE1 (" Expect new value of property %s",
1408 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1409 wait_object = expect_property_change (display, window, property,
1410 PropertyNewValue);
1411 XFlush (display);
1412 unblock_input ();
1414 while (true)
1416 unsigned char *tmp_data;
1417 ptrdiff_t tmp_size_bytes;
1419 TRACE0 (" Wait for property change");
1420 wait_for_property_change (wait_object);
1422 /* expect it again immediately, because x_get_window_property may
1423 .. no it won't, I don't get it.
1424 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1425 TRACE0 (" Get property value");
1426 x_get_window_property (display, window, property,
1427 &tmp_data, &tmp_size_bytes,
1428 type_ret, format_ret, size_ret);
1430 TRACE1 (" Read increment of %"pD"d bytes", tmp_size_bytes);
1432 if (tmp_size_bytes == 0) /* we're done */
1434 TRACE0 ("Done reading incrementally");
1436 if (! waiting_for_other_props_on_window (display, window))
1437 XSelectInput (display, window, STANDARD_EVENT_SET);
1438 /* Use xfree, not XFree, because x_get_window_property
1439 calls xmalloc itself. */
1440 xfree (tmp_data);
1441 break;
1444 block_input ();
1445 TRACE1 (" ACK by deleting property %s",
1446 XGetAtomName (display, property));
1447 XDeleteProperty (display, window, property);
1448 wait_object = expect_property_change (display, window, property,
1449 PropertyNewValue);
1450 XFlush (display);
1451 unblock_input ();
1453 if (*size_bytes_ret - offset < tmp_size_bytes)
1454 *data_ret = xpalloc (*data_ret, size_bytes_ret,
1455 tmp_size_bytes - (*size_bytes_ret - offset),
1456 -1, 1);
1458 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1459 offset += tmp_size_bytes;
1461 /* Use xfree, not XFree, because x_get_window_property
1462 calls xmalloc itself. */
1463 xfree (tmp_data);
1468 /* Fetch a value from property PROPERTY of X window WINDOW on display
1469 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1470 if this fails. */
1472 static Lisp_Object
1473 x_get_window_property_as_lisp_data (struct x_display_info *dpyinfo,
1474 Window window, Atom property,
1475 Lisp_Object target_type,
1476 Atom selection_atom)
1478 Atom actual_type;
1479 int actual_format;
1480 unsigned long actual_size;
1481 unsigned char *data = 0;
1482 ptrdiff_t bytes = 0;
1483 Lisp_Object val;
1484 Display *display = dpyinfo->display;
1486 TRACE0 ("Reading selection data");
1488 x_get_window_property (display, window, property, &data, &bytes,
1489 &actual_type, &actual_format, &actual_size);
1490 if (! data)
1492 block_input ();
1493 bool there_is_a_selection_owner
1494 = XGetSelectionOwner (display, selection_atom) != 0;
1495 unblock_input ();
1496 if (there_is_a_selection_owner)
1497 signal_error ("Selection owner couldn't convert",
1498 actual_type
1499 ? list2 (target_type,
1500 x_atom_to_symbol (dpyinfo, actual_type))
1501 : target_type);
1502 else
1503 signal_error ("No selection",
1504 x_atom_to_symbol (dpyinfo, selection_atom));
1507 if (actual_type == dpyinfo->Xatom_INCR)
1509 /* That wasn't really the data, just the beginning. */
1511 unsigned int min_size_bytes = * ((unsigned int *) data);
1512 block_input ();
1513 /* Use xfree, not XFree, because x_get_window_property
1514 calls xmalloc itself. */
1515 xfree (data);
1516 unblock_input ();
1517 receive_incremental_selection (dpyinfo, window, property, target_type,
1518 min_size_bytes, &data, &bytes,
1519 &actual_type, &actual_format,
1520 &actual_size);
1523 block_input ();
1524 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1525 XDeleteProperty (display, window, property);
1526 XFlush (display);
1527 unblock_input ();
1529 /* It's been read. Now convert it to a lisp object in some semi-rational
1530 manner. */
1531 val = selection_data_to_lisp_data (dpyinfo, data, bytes,
1532 actual_type, actual_format);
1534 /* Use xfree, not XFree, because x_get_window_property
1535 calls xmalloc itself. */
1536 xfree (data);
1537 return val;
1540 /* These functions convert from the selection data read from the server into
1541 something that we can use from Lisp, and vice versa.
1543 Type: Format: Size: Lisp Type:
1544 ----- ------- ----- -----------
1545 * 8 * String
1546 ATOM 32 1 Symbol
1547 ATOM 32 > 1 Vector of Symbols
1548 * 16 1 Integer
1549 * 16 > 1 Vector of Integers
1550 * 32 1 if <=16 bits: Integer
1551 if > 16 bits: Cons of top16, bot16
1552 * 32 > 1 Vector of the above
1554 When converting a Lisp number to C, it is assumed to be of format 16 if
1555 it is an integer, and of format 32 if it is a cons of two integers.
1557 When converting a vector of numbers from Lisp to C, it is assumed to be
1558 of format 16 if every element in the vector is an integer, and is assumed
1559 to be of format 32 if any element is a cons of two integers.
1561 When converting an object to C, it may be of the form (SYMBOL . <data>)
1562 where SYMBOL is what we should claim that the type is. Format and
1563 representation are as above.
1565 Important: When format is 32, data should contain an array of int,
1566 not an array of long as the X library returns. This makes a difference
1567 when sizeof(long) != sizeof(int). */
1571 static Lisp_Object
1572 selection_data_to_lisp_data (struct x_display_info *dpyinfo,
1573 const unsigned char *data,
1574 ptrdiff_t size, Atom type, int format)
1576 if (type == dpyinfo->Xatom_NULL)
1577 return QNULL;
1579 /* Convert any 8-bit data to a string, for compactness. */
1580 else if (format == 8)
1582 Lisp_Object str, lispy_type;
1584 str = make_unibyte_string ((char *) data, size);
1585 /* Indicate that this string is from foreign selection by a text
1586 property `foreign-selection' so that the caller of
1587 x-get-selection-internal (usually x-get-selection) can know
1588 that the string must be decode. */
1589 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1590 lispy_type = QCOMPOUND_TEXT;
1591 else if (type == dpyinfo->Xatom_UTF8_STRING)
1592 lispy_type = QUTF8_STRING;
1593 else
1594 lispy_type = QSTRING;
1595 Fput_text_property (make_number (0), make_number (size),
1596 Qforeign_selection, lispy_type, str);
1597 return str;
1599 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1600 a vector of symbols. */
1601 else if (type == XA_ATOM
1602 /* Treat ATOM_PAIR type similar to list of atoms. */
1603 || type == dpyinfo->Xatom_ATOM_PAIR)
1605 ptrdiff_t i;
1606 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1607 But the callers of these function has made sure the data for
1608 format == 32 is an array of int. Thus, use int instead
1609 of Atom. */
1610 int *idata = (int *) data;
1612 if (size == sizeof (int))
1613 return x_atom_to_symbol (dpyinfo, (Atom) idata[0]);
1614 else
1616 Lisp_Object v = make_uninit_vector (size / sizeof (int));
1618 for (i = 0; i < size / sizeof (int); i++)
1619 ASET (v, i, x_atom_to_symbol (dpyinfo, (Atom) idata[i]));
1620 return v;
1624 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1625 If the number is 32 bits and won't fit in a Lisp_Int,
1626 convert it to a cons of integers, 16 bits in each half.
1628 else if (format == 32 && size == sizeof (int))
1629 return INTEGER_TO_CONS (((int *) data) [0]);
1630 else if (format == 16 && size == sizeof (short))
1631 return make_number (((short *) data) [0]);
1633 /* Convert any other kind of data to a vector of numbers, represented
1634 as above (as an integer, or a cons of two 16 bit integers.)
1636 else if (format == 16)
1638 ptrdiff_t i;
1639 Lisp_Object v = make_uninit_vector (size / 2);
1641 for (i = 0; i < size / 2; i++)
1643 short j = ((short *) data) [i];
1644 ASET (v, i, make_number (j));
1646 return v;
1648 else
1650 ptrdiff_t i;
1651 Lisp_Object v = make_uninit_vector (size / X_LONG_SIZE);
1653 for (i = 0; i < size / X_LONG_SIZE; i++)
1655 int j = ((int *) data) [i];
1656 ASET (v, i, INTEGER_TO_CONS (j));
1658 return v;
1662 /* Convert OBJ to an X long value, and return it as unsigned long.
1663 OBJ should be an integer or a cons representing an integer.
1664 Treat values in the range X_LONG_MAX + 1 .. X_ULONG_MAX as X
1665 unsigned long values: in theory these values are supposed to be
1666 signed but in practice unsigned 32-bit data are communicated via X
1667 selections and we need to support that. */
1668 static unsigned long
1669 cons_to_x_long (Lisp_Object obj)
1671 if (X_ULONG_MAX <= INTMAX_MAX
1672 || XINT (INTEGERP (obj) ? obj : XCAR (obj)) < 0)
1673 return cons_to_signed (obj, X_LONG_MIN, min (X_ULONG_MAX, INTMAX_MAX));
1674 else
1675 return cons_to_unsigned (obj, X_ULONG_MAX);
1678 /* Use xfree, not XFree, to free the data obtained with this function. */
1680 static void
1681 lisp_data_to_selection_data (struct x_display_info *dpyinfo,
1682 Lisp_Object obj, struct selection_data *cs)
1684 Lisp_Object type = Qnil;
1686 eassert (cs != NULL);
1687 cs->nofree = false;
1689 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1691 type = XCAR (obj);
1692 obj = XCDR (obj);
1693 if (CONSP (obj) && NILP (XCDR (obj)))
1694 obj = XCAR (obj);
1697 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1698 { /* This is not the same as declining */
1699 cs->format = 32;
1700 cs->size = 0;
1701 cs->data = NULL;
1702 type = QNULL;
1704 else if (STRINGP (obj))
1706 if (SCHARS (obj) < SBYTES (obj))
1707 /* OBJ is a multibyte string containing a non-ASCII char. */
1708 signal_error ("Non-ASCII string must be encoded in advance", obj);
1709 if (NILP (type))
1710 type = QSTRING;
1711 cs->format = 8;
1712 cs->size = SBYTES (obj);
1713 cs->data = SDATA (obj);
1714 cs->nofree = true;
1716 else if (SYMBOLP (obj))
1718 void *data = xmalloc (sizeof (Atom) + 1);
1719 Atom *x_atom_ptr = data;
1720 cs->data = data;
1721 cs->format = 32;
1722 cs->size = 1;
1723 cs->data[sizeof (Atom)] = 0;
1724 *x_atom_ptr = symbol_to_x_atom (dpyinfo, obj);
1725 if (NILP (type)) type = QATOM;
1727 else if (RANGED_INTEGERP (X_SHRT_MIN, obj, X_SHRT_MAX))
1729 void *data = xmalloc (sizeof (short) + 1);
1730 short *short_ptr = data;
1731 cs->data = data;
1732 cs->format = 16;
1733 cs->size = 1;
1734 cs->data[sizeof (short)] = 0;
1735 *short_ptr = XINT (obj);
1736 if (NILP (type)) type = QINTEGER;
1738 else if (INTEGERP (obj)
1739 || (CONSP (obj) && INTEGERP (XCAR (obj))
1740 && (INTEGERP (XCDR (obj))
1741 || (CONSP (XCDR (obj))
1742 && INTEGERP (XCAR (XCDR (obj)))))))
1744 void *data = xmalloc (sizeof (unsigned long) + 1);
1745 unsigned long *x_long_ptr = data;
1746 cs->data = data;
1747 cs->format = 32;
1748 cs->size = 1;
1749 cs->data[sizeof (unsigned long)] = 0;
1750 *x_long_ptr = cons_to_x_long (obj);
1751 if (NILP (type)) type = QINTEGER;
1753 else if (VECTORP (obj))
1755 /* Lisp_Vectors may represent a set of ATOMs;
1756 a set of 16 or 32 bit INTEGERs;
1757 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1759 ptrdiff_t i;
1760 ptrdiff_t size = ASIZE (obj);
1762 if (SYMBOLP (AREF (obj, 0)))
1763 /* This vector is an ATOM set */
1765 void *data;
1766 Atom *x_atoms;
1767 if (NILP (type)) type = QATOM;
1768 for (i = 0; i < size; i++)
1769 if (!SYMBOLP (AREF (obj, i)))
1770 signal_error ("All elements of selection vector must have same type", obj);
1772 cs->data = data = xnmalloc (size, sizeof *x_atoms);
1773 x_atoms = data;
1774 cs->format = 32;
1775 cs->size = size;
1776 for (i = 0; i < size; i++)
1777 x_atoms[i] = symbol_to_x_atom (dpyinfo, AREF (obj, i));
1779 else
1780 /* This vector is an INTEGER set, or something like it */
1782 int format = 16;
1783 int data_size = sizeof (short);
1784 void *data;
1785 unsigned long *x_atoms;
1786 short *shorts;
1787 if (NILP (type)) type = QINTEGER;
1788 for (i = 0; i < size; i++)
1790 if (! RANGED_INTEGERP (X_SHRT_MIN, AREF (obj, i),
1791 X_SHRT_MAX))
1793 /* Use sizeof (long) even if it is more than 32 bits.
1794 See comment in x_get_window_property and
1795 x_fill_property_data. */
1796 data_size = sizeof (long);
1797 format = 32;
1798 break;
1801 cs->data = data = xnmalloc (size, data_size);
1802 x_atoms = data;
1803 shorts = data;
1804 cs->format = format;
1805 cs->size = size;
1806 for (i = 0; i < size; i++)
1808 if (format == 32)
1809 x_atoms[i] = cons_to_x_long (AREF (obj, i));
1810 else
1811 shorts[i] = XINT (AREF (obj, i));
1815 else
1816 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1818 cs->type = symbol_to_x_atom (dpyinfo, type);
1821 static Lisp_Object
1822 clean_local_selection_data (Lisp_Object obj)
1824 if (CONSP (obj)
1825 && INTEGERP (XCAR (obj))
1826 && CONSP (XCDR (obj))
1827 && INTEGERP (XCAR (XCDR (obj)))
1828 && NILP (XCDR (XCDR (obj))))
1829 obj = Fcons (XCAR (obj), XCDR (obj));
1831 if (CONSP (obj)
1832 && INTEGERP (XCAR (obj))
1833 && INTEGERP (XCDR (obj)))
1835 if (XINT (XCAR (obj)) == 0)
1836 return XCDR (obj);
1837 if (XINT (XCAR (obj)) == -1)
1838 return make_number (- XINT (XCDR (obj)));
1840 if (VECTORP (obj))
1842 ptrdiff_t i;
1843 ptrdiff_t size = ASIZE (obj);
1844 Lisp_Object copy;
1845 if (size == 1)
1846 return clean_local_selection_data (AREF (obj, 0));
1847 copy = make_uninit_vector (size);
1848 for (i = 0; i < size; i++)
1849 ASET (copy, i, clean_local_selection_data (AREF (obj, i)));
1850 return copy;
1852 return obj;
1855 /* Called from XTread_socket to handle SelectionNotify events.
1856 If it's the selection we are waiting for, stop waiting
1857 by setting the car of reading_selection_reply to non-nil.
1858 We store t there if the reply is successful, lambda if not. */
1860 void
1861 x_handle_selection_notify (const XSelectionEvent *event)
1863 if (event->requestor != reading_selection_window)
1864 return;
1865 if (event->selection != reading_which_selection)
1866 return;
1868 TRACE0 ("Received SelectionNotify");
1869 XSETCAR (reading_selection_reply,
1870 (event->property != 0 ? Qt : Qlambda));
1874 /* From a Lisp_Object, return a suitable frame for selection
1875 operations. OBJECT may be a frame, a terminal object, or nil
1876 (which stands for the selected frame--or, if that is not an X
1877 frame, the first X display on the list). If no suitable frame can
1878 be found, return NULL. */
1880 static struct frame *
1881 frame_for_x_selection (Lisp_Object object)
1883 Lisp_Object tail, frame;
1884 struct frame *f;
1886 if (NILP (object))
1888 f = XFRAME (selected_frame);
1889 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1890 return f;
1892 FOR_EACH_FRAME (tail, frame)
1894 f = XFRAME (frame);
1895 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1896 return f;
1899 else if (TERMINALP (object))
1901 struct terminal *t = decode_live_terminal (object);
1903 if (t->type == output_x_window)
1904 FOR_EACH_FRAME (tail, frame)
1906 f = XFRAME (frame);
1907 if (FRAME_LIVE_P (f) && f->terminal == t)
1908 return f;
1911 else if (FRAMEP (object))
1913 f = XFRAME (object);
1914 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1915 return f;
1918 return NULL;
1922 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1923 Sx_own_selection_internal, 2, 3, 0,
1924 doc: /* Assert an X selection of type SELECTION and value VALUE.
1925 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1926 \(Those are literal upper-case symbol names, since that's what X expects.)
1927 VALUE is typically a string, or a cons of two markers, but may be
1928 anything that the functions on `selection-converter-alist' know about.
1930 FRAME should be a frame that should own the selection. If omitted or
1931 nil, it defaults to the selected frame.
1933 On Nextstep, FRAME is unused. */)
1934 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1936 if (NILP (frame)) frame = selected_frame;
1937 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1938 error ("X selection unavailable for this frame");
1940 CHECK_SYMBOL (selection);
1941 if (NILP (value)) error ("VALUE may not be nil");
1942 x_own_selection (selection, value, frame);
1943 return value;
1947 /* Request the selection value from the owner. If we are the owner,
1948 simply return our selection value. If we are not the owner, this
1949 will block until all of the data has arrived. */
1951 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1952 Sx_get_selection_internal, 2, 4, 0,
1953 doc: /* Return text selected from some X window.
1954 SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1955 \(Those are literal upper-case symbol names, since that's what X expects.)
1956 TARGET-TYPE is the type of data desired, typically `STRING'.
1958 TIME-STAMP is the time to use in the XConvertSelection call for foreign
1959 selections. If omitted, defaults to the time for the last event.
1961 TERMINAL should be a terminal object or a frame specifying the X
1962 server to query. If omitted or nil, that stands for the selected
1963 frame's display, or the first available X display.
1965 On Nextstep, TIME-STAMP and TERMINAL are unused. */)
1966 (Lisp_Object selection_symbol, Lisp_Object target_type,
1967 Lisp_Object time_stamp, Lisp_Object terminal)
1969 Lisp_Object val = Qnil;
1970 struct gcpro gcpro1, gcpro2;
1971 struct frame *f = frame_for_x_selection (terminal);
1972 GCPRO2 (target_type, val); /* we store newly consed data into these */
1974 CHECK_SYMBOL (selection_symbol);
1975 CHECK_SYMBOL (target_type);
1976 if (EQ (target_type, QMULTIPLE))
1977 error ("Retrieving MULTIPLE selections is currently unimplemented");
1978 if (!f)
1979 error ("X selection unavailable for this frame");
1981 val = x_get_local_selection (selection_symbol, target_type, true,
1982 FRAME_DISPLAY_INFO (f));
1984 if (NILP (val) && FRAME_LIVE_P (f))
1986 Lisp_Object frame;
1987 XSETFRAME (frame, f);
1988 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1989 time_stamp, frame));
1992 if (CONSP (val) && SYMBOLP (XCAR (val)))
1994 val = XCDR (val);
1995 if (CONSP (val) && NILP (XCDR (val)))
1996 val = XCAR (val);
1998 RETURN_UNGCPRO (clean_local_selection_data (val));
2001 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2002 Sx_disown_selection_internal, 1, 3, 0,
2003 doc: /* If we own the selection SELECTION, disown it.
2004 Disowning it means there is no such selection.
2006 Sets the last-change time for the selection to TIME-OBJECT (by default
2007 the time of the last event).
2009 TERMINAL should be a terminal object or a frame specifying the X
2010 server to query. If omitted or nil, that stands for the selected
2011 frame's display, or the first available X display.
2013 On Nextstep, the TIME-OBJECT and TERMINAL arguments are unused.
2014 On MS-DOS, all this does is return non-nil if we own the selection. */)
2015 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
2017 Time timestamp;
2018 Atom selection_atom;
2019 union {
2020 struct selection_input_event sie;
2021 struct input_event ie;
2022 } event;
2023 struct frame *f = frame_for_x_selection (terminal);
2024 struct x_display_info *dpyinfo;
2026 if (!f)
2027 return Qnil;
2029 dpyinfo = FRAME_DISPLAY_INFO (f);
2030 CHECK_SYMBOL (selection);
2032 /* Don't disown the selection when we're not the owner. */
2033 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2034 return Qnil;
2036 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2038 block_input ();
2039 if (NILP (time_object))
2040 timestamp = dpyinfo->last_user_time;
2041 else
2042 CONS_TO_INTEGER (time_object, Time, timestamp);
2043 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2044 unblock_input ();
2046 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2047 generated for a window which owns the selection when that window sets
2048 the selection owner to None. The NCD server does, the MIT Sun4 server
2049 doesn't. So we synthesize one; this means we might get two, but
2050 that's ok, because the second one won't have any effect. */
2051 SELECTION_EVENT_DPYINFO (&event.sie) = dpyinfo;
2052 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2053 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2054 x_handle_selection_clear (&event.ie);
2056 return Qt;
2059 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2060 0, 2, 0,
2061 doc: /* Whether the current Emacs process owns the given X Selection.
2062 The arg should be the name of the selection in question, typically one of
2063 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2064 \(Those are literal upper-case symbol names, since that's what X expects.)
2065 For convenience, the symbol nil is the same as `PRIMARY',
2066 and t is the same as `SECONDARY'.
2068 TERMINAL should be a terminal object or a frame specifying the X
2069 server to query. If omitted or nil, that stands for the selected
2070 frame's display, or the first available X display.
2072 On Nextstep, TERMINAL is unused. */)
2073 (Lisp_Object selection, Lisp_Object terminal)
2075 struct frame *f = frame_for_x_selection (terminal);
2077 CHECK_SYMBOL (selection);
2078 if (EQ (selection, Qnil)) selection = QPRIMARY;
2079 if (EQ (selection, Qt)) selection = QSECONDARY;
2081 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_DISPLAY_INFO (f))))
2082 return Qt;
2083 else
2084 return Qnil;
2087 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2088 0, 2, 0,
2089 doc: /* Whether there is an owner for the given X selection.
2090 SELECTION should be the name of the selection in question, typically
2091 one of the symbols `PRIMARY', `SECONDARY', `CLIPBOARD', or
2092 `CLIPBOARD_MANAGER' (X expects these literal upper-case names.) The
2093 symbol nil is the same as `PRIMARY', and t is the same as `SECONDARY'.
2095 TERMINAL should be a terminal object or a frame specifying the X
2096 server to query. If omitted or nil, that stands for the selected
2097 frame's display, or the first available X display.
2099 On Nextstep, TERMINAL is unused. */)
2100 (Lisp_Object selection, Lisp_Object terminal)
2102 Window owner;
2103 Atom atom;
2104 struct frame *f = frame_for_x_selection (terminal);
2105 struct x_display_info *dpyinfo;
2107 CHECK_SYMBOL (selection);
2108 if (EQ (selection, Qnil)) selection = QPRIMARY;
2109 if (EQ (selection, Qt)) selection = QSECONDARY;
2111 if (!f)
2112 return Qnil;
2114 dpyinfo = FRAME_DISPLAY_INFO (f);
2116 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2117 return Qt;
2119 atom = symbol_to_x_atom (dpyinfo, selection);
2120 if (atom == 0) return Qnil;
2121 block_input ();
2122 owner = XGetSelectionOwner (dpyinfo->display, atom);
2123 unblock_input ();
2124 return (owner ? Qt : Qnil);
2128 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2129 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2131 static Lisp_Object
2132 x_clipboard_manager_save (Lisp_Object frame)
2134 struct frame *f = XFRAME (frame);
2135 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2136 Atom data = dpyinfo->Xatom_UTF8_STRING;
2138 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2139 dpyinfo->Xatom_EMACS_TMP,
2140 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2141 (unsigned char *) &data, 1);
2142 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2143 Qnil, frame);
2144 return Qt;
2147 /* Error handler for x_clipboard_manager_save_frame. */
2149 static Lisp_Object
2150 x_clipboard_manager_error_1 (Lisp_Object err)
2152 AUTO_STRING (format, "X clipboard manager error: %s\n\
2153 If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2154 Fmessage (2, (Lisp_Object []) {format, CAR (CDR (err))});
2155 return Qnil;
2158 /* Error handler for x_clipboard_manager_save_all. */
2160 static Lisp_Object
2161 x_clipboard_manager_error_2 (Lisp_Object err)
2163 fprintf (stderr, "Error saving to X clipboard manager.\n\
2164 If the problem persists, set `x-select-enable-clipboard-manager' \
2165 to nil.\n");
2166 return Qnil;
2169 /* Called from delete_frame: save any clipboard owned by FRAME to the
2170 clipboard manager. Do nothing if FRAME does not own the clipboard,
2171 or if no clipboard manager is present. */
2173 void
2174 x_clipboard_manager_save_frame (Lisp_Object frame)
2176 struct frame *f;
2178 if (!NILP (Vx_select_enable_clipboard_manager)
2179 && FRAMEP (frame)
2180 && (f = XFRAME (frame), FRAME_X_P (f))
2181 && FRAME_LIVE_P (f))
2183 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2184 Lisp_Object local_selection
2185 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2187 if (!NILP (local_selection)
2188 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2189 && XGetSelectionOwner (dpyinfo->display,
2190 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2191 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2192 x_clipboard_manager_error_1);
2196 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2197 clipboard manager. Do nothing if FRAME does not own the clipboard,
2198 or if no clipboard manager is present. */
2200 void
2201 x_clipboard_manager_save_all (void)
2203 /* Loop through all X displays, saving owned clipboards. */
2204 struct x_display_info *dpyinfo;
2205 Lisp_Object local_selection, local_frame;
2207 if (NILP (Vx_select_enable_clipboard_manager))
2208 return;
2210 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2212 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2213 if (NILP (local_selection)
2214 || !XGetSelectionOwner (dpyinfo->display,
2215 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2216 continue;
2218 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2219 if (FRAME_LIVE_P (XFRAME (local_frame)))
2221 AUTO_STRING (saving, "Saving clipboard to X clipboard manager...");
2222 Fmessage (1, &saving);
2223 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2224 Qt, x_clipboard_manager_error_2);
2230 /***********************************************************************
2231 Drag and drop support
2232 ***********************************************************************/
2233 /* Check that lisp values are of correct type for x_fill_property_data.
2234 That is, number, string or a cons with two numbers (low and high 16
2235 bit parts of a 32 bit number). Return the number of items in DATA,
2236 or -1 if there is an error. */
2239 x_check_property_data (Lisp_Object data)
2241 Lisp_Object iter;
2242 int size = 0;
2244 for (iter = data; CONSP (iter); iter = XCDR (iter))
2246 Lisp_Object o = XCAR (iter);
2248 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2249 return -1;
2250 else if (CONSP (o) &&
2251 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2252 return -1;
2253 if (size == INT_MAX)
2254 return -1;
2255 size++;
2258 return size;
2261 /* Convert lisp values to a C array. Values may be a number, a string
2262 which is taken as an X atom name and converted to the atom value, or
2263 a cons containing the two 16 bit parts of a 32 bit number.
2265 DPY is the display use to look up X atoms.
2266 DATA is a Lisp list of values to be converted.
2267 RET is the C array that contains the converted values. It is assumed
2268 it is big enough to hold all values.
2269 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2270 be stored in RET. Note that long is used for 32 even if long is more
2271 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2272 XClientMessageEvent). */
2274 void
2275 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2277 unsigned long val;
2278 unsigned long *d32 = (unsigned long *) ret;
2279 unsigned short *d16 = (unsigned short *) ret;
2280 unsigned char *d08 = (unsigned char *) ret;
2281 Lisp_Object iter;
2283 for (iter = data; CONSP (iter); iter = XCDR (iter))
2285 Lisp_Object o = XCAR (iter);
2287 if (INTEGERP (o) || FLOATP (o) || CONSP (o))
2289 if (CONSP (o)
2290 && RANGED_INTEGERP (X_LONG_MIN >> 16, XCAR (o), X_LONG_MAX >> 16)
2291 && RANGED_INTEGERP (- (1 << 15), XCDR (o), -1))
2293 /* cons_to_x_long does not handle negative values for v2.
2294 For XDnd, v2 might be y of a window, and can be negative.
2295 The XDnd spec. is not explicit about negative values,
2296 but let's assume negative v2 is sent modulo 2**16. */
2297 unsigned long v1 = XINT (XCAR (o)) & 0xffff;
2298 unsigned long v2 = XINT (XCDR (o)) & 0xffff;
2299 val = (v1 << 16) | v2;
2301 else
2302 val = cons_to_x_long (o);
2304 else if (STRINGP (o))
2306 block_input ();
2307 val = XInternAtom (dpy, SSDATA (o), False);
2308 unblock_input ();
2310 else
2311 error ("Wrong type, must be string, number or cons");
2313 if (format == 8)
2315 if ((1 << 8) < val && val <= X_ULONG_MAX - (1 << 7))
2316 error ("Out of 'char' range");
2317 *d08++ = val;
2319 else if (format == 16)
2321 if ((1 << 16) < val && val <= X_ULONG_MAX - (1 << 15))
2322 error ("Out of 'short' range");
2323 *d16++ = val;
2325 else
2326 *d32++ = val;
2330 /* Convert an array of C values to a Lisp list.
2331 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2332 DATA is a C array of values to be converted.
2333 TYPE is the type of the data. Only XA_ATOM is special, it converts
2334 each number in DATA to its corresponding X atom as a symbol.
2335 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2336 be stored in RET.
2337 SIZE is the number of elements in DATA.
2339 Important: When format is 32, data should contain an array of int,
2340 not an array of long as the X library returns. This makes a difference
2341 when sizeof(long) != sizeof(int).
2343 Also see comment for selection_data_to_lisp_data above. */
2345 Lisp_Object
2346 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2347 Atom type, int format, unsigned long size)
2349 ptrdiff_t format_bytes = format >> 3;
2350 if (PTRDIFF_MAX / format_bytes < size)
2351 memory_full (SIZE_MAX);
2352 return selection_data_to_lisp_data (FRAME_DISPLAY_INFO (f), data,
2353 size * format_bytes, type, format);
2356 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2357 Sx_get_atom_name, 1, 2, 0,
2358 doc: /* Return the X atom name for VALUE as a string.
2359 VALUE may be a number or a cons where the car is the upper 16 bits and
2360 the cdr is the lower 16 bits of a 32 bit value.
2361 Use the display for FRAME or the current frame if FRAME is not given or nil.
2363 If the value is 0 or the atom is not known, return the empty string. */)
2364 (Lisp_Object value, Lisp_Object frame)
2366 struct frame *f = decode_window_system_frame (frame);
2367 char *name = 0;
2368 char empty[] = "";
2369 Lisp_Object ret = Qnil;
2370 Display *dpy = FRAME_X_DISPLAY (f);
2371 Atom atom;
2372 bool had_errors_p;
2374 CONS_TO_INTEGER (value, Atom, atom);
2376 block_input ();
2377 x_catch_errors (dpy);
2378 name = atom ? XGetAtomName (dpy, atom) : empty;
2379 had_errors_p = x_had_errors_p (dpy);
2380 x_uncatch_errors ();
2382 if (!had_errors_p)
2383 ret = build_string (name);
2385 if (atom && name) XFree (name);
2386 if (NILP (ret)) ret = empty_unibyte_string;
2388 unblock_input ();
2390 return ret;
2393 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2394 Sx_register_dnd_atom, 1, 2, 0,
2395 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2396 ATOM can be a symbol or a string. The ATOM is interned on the display that
2397 FRAME is on. If FRAME is nil, the selected frame is used. */)
2398 (Lisp_Object atom, Lisp_Object frame)
2400 Atom x_atom;
2401 struct frame *f = decode_window_system_frame (frame);
2402 ptrdiff_t i;
2403 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2406 if (SYMBOLP (atom))
2407 x_atom = symbol_to_x_atom (dpyinfo, atom);
2408 else if (STRINGP (atom))
2410 block_input ();
2411 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2412 unblock_input ();
2414 else
2415 error ("ATOM must be a symbol or a string");
2417 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2418 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2419 return Qnil;
2421 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2422 dpyinfo->x_dnd_atoms =
2423 xpalloc (dpyinfo->x_dnd_atoms, &dpyinfo->x_dnd_atoms_size,
2424 1, -1, sizeof *dpyinfo->x_dnd_atoms);
2426 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2427 return Qnil;
2430 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2432 bool
2433 x_handle_dnd_message (struct frame *f, const XClientMessageEvent *event,
2434 struct x_display_info *dpyinfo, struct input_event *bufp)
2436 Lisp_Object vec;
2437 Lisp_Object frame;
2438 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2439 unsigned long size = 160/event->format;
2440 int x, y;
2441 unsigned char *data = (unsigned char *) event->data.b;
2442 int idata[5];
2443 ptrdiff_t i;
2445 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2446 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2448 if (i == dpyinfo->x_dnd_atoms_length) return false;
2450 XSETFRAME (frame, f);
2452 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2453 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2454 function expects them to be of size int (i.e. 32). So to be able to
2455 use that function, put the data in the form it expects if format is 32. */
2457 if (BITS_PER_LONG > 32 && event->format == 32)
2459 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2460 idata[i] = event->data.l[i];
2461 data = (unsigned char *) idata;
2464 vec = Fmake_vector (make_number (4), Qnil);
2465 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_DISPLAY_INFO (f),
2466 event->message_type)));
2467 ASET (vec, 1, frame);
2468 ASET (vec, 2, make_number (event->format));
2469 ASET (vec, 3, x_property_data_to_lisp (f,
2470 data,
2471 event->message_type,
2472 event->format,
2473 size));
2475 x_relative_mouse_position (f, &x, &y);
2476 bufp->kind = DRAG_N_DROP_EVENT;
2477 bufp->frame_or_window = frame;
2478 bufp->timestamp = CurrentTime;
2479 bufp->x = make_number (x);
2480 bufp->y = make_number (y);
2481 bufp->arg = vec;
2482 bufp->modifiers = 0;
2484 return true;
2487 DEFUN ("x-send-client-message", Fx_send_client_message,
2488 Sx_send_client_message, 6, 6, 0,
2489 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2491 For DISPLAY, specify either a frame or a display name (a string).
2492 If DISPLAY is nil, that stands for the selected frame's display.
2493 DEST may be a number, in which case it is a Window id. The value 0 may
2494 be used to send to the root window of the DISPLAY.
2495 If DEST is a cons, it is converted to a 32 bit number
2496 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2497 number is then used as a window id.
2498 If DEST is a frame the event is sent to the outer window of that frame.
2499 A value of nil means the currently selected frame.
2500 If DEST is the string "PointerWindow" the event is sent to the window that
2501 contains the pointer. If DEST is the string "InputFocus" the event is
2502 sent to the window that has the input focus.
2503 FROM is the frame sending the event. Use nil for currently selected frame.
2504 MESSAGE-TYPE is the name of an Atom as a string.
2505 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2506 bits. VALUES is a list of numbers, cons and/or strings containing the values
2507 to send. If a value is a string, it is converted to an Atom and the value of
2508 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2509 with the high 16 bits from the car and the lower 16 bit from the cdr.
2510 If more values than fits into the event is given, the excessive values
2511 are ignored. */)
2512 (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2513 Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2515 struct x_display_info *dpyinfo = check_x_display_info (display);
2517 CHECK_STRING (message_type);
2518 x_send_client_event (display, dest, from,
2519 XInternAtom (dpyinfo->display,
2520 SSDATA (message_type),
2521 False),
2522 format, values);
2524 return Qnil;
2527 void
2528 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2529 Atom message_type, Lisp_Object format, Lisp_Object values)
2531 struct x_display_info *dpyinfo = check_x_display_info (display);
2532 Window wdest;
2533 XEvent event;
2534 struct frame *f = decode_window_system_frame (from);
2535 bool to_root;
2537 CHECK_NUMBER (format);
2538 CHECK_CONS (values);
2540 if (x_check_property_data (values) == -1)
2541 error ("Bad data in VALUES, must be number, cons or string");
2543 if (XINT (format) != 8 && XINT (format) != 16 && XINT (format) != 32)
2544 error ("FORMAT must be one of 8, 16 or 32");
2546 event.xclient.type = ClientMessage;
2547 event.xclient.format = XINT (format);
2549 if (FRAMEP (dest) || NILP (dest))
2551 struct frame *fdest = decode_window_system_frame (dest);
2552 wdest = FRAME_OUTER_WINDOW (fdest);
2554 else if (STRINGP (dest))
2556 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2557 wdest = PointerWindow;
2558 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2559 wdest = InputFocus;
2560 else
2561 error ("DEST as a string must be one of PointerWindow or InputFocus");
2563 else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest))
2564 CONS_TO_INTEGER (dest, Window, wdest);
2565 else
2566 error ("DEST must be a frame, nil, string, number or cons");
2568 if (wdest == 0) wdest = dpyinfo->root_window;
2569 to_root = wdest == dpyinfo->root_window;
2571 block_input ();
2573 event.xclient.send_event = True;
2574 event.xclient.serial = 0;
2575 event.xclient.message_type = message_type;
2576 event.xclient.display = dpyinfo->display;
2578 /* Some clients (metacity for example) expects sending window to be here
2579 when sending to the root window. */
2580 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2582 memset (event.xclient.data.l, 0, sizeof (event.xclient.data.l));
2583 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2584 event.xclient.format);
2586 /* If event mask is 0 the event is sent to the client that created
2587 the destination window. But if we are sending to the root window,
2588 there is no such client. Then we set the event mask to 0xffffff. The
2589 event then goes to clients selecting for events on the root window. */
2590 x_catch_errors (dpyinfo->display);
2592 bool propagate = !to_root;
2593 long mask = to_root ? 0xffffff : 0;
2595 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2596 XFlush (dpyinfo->display);
2598 x_uncatch_errors ();
2599 unblock_input ();
2603 void
2604 syms_of_xselect (void)
2606 defsubr (&Sx_get_selection_internal);
2607 defsubr (&Sx_own_selection_internal);
2608 defsubr (&Sx_disown_selection_internal);
2609 defsubr (&Sx_selection_owner_p);
2610 defsubr (&Sx_selection_exists_p);
2612 defsubr (&Sx_get_atom_name);
2613 defsubr (&Sx_send_client_message);
2614 defsubr (&Sx_register_dnd_atom);
2616 reading_selection_reply = Fcons (Qnil, Qnil);
2617 staticpro (&reading_selection_reply);
2618 reading_selection_window = 0;
2619 reading_which_selection = 0;
2621 property_change_wait_list = 0;
2622 prop_location_identifier = 0;
2623 property_change_reply = Fcons (Qnil, Qnil);
2624 staticpro (&property_change_reply);
2626 converted_selections = NULL;
2627 conversion_fail_tag = None;
2629 /* FIXME: Duplicate definition in nsselect.c. */
2630 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2631 doc: /* An alist associating X Windows selection-types with functions.
2632 These functions are called to convert the selection, with three args:
2633 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2634 a desired type to which the selection should be converted;
2635 and the local selection value (whatever was given to
2636 `x-own-selection-internal').
2638 The function should return the value to send to the X server
2639 \(typically a string). A return value of nil
2640 means that the conversion could not be done.
2641 A return value which is the symbol `NULL'
2642 means that a side-effect was executed,
2643 and there is no meaningful selection value. */);
2644 Vselection_converter_alist = Qnil;
2646 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2647 doc: /* A list of functions to be called when Emacs loses an X selection.
2648 \(This happens when some other X client makes its own selection
2649 or when a Lisp program explicitly clears the selection.)
2650 The functions are called with one argument, the selection type
2651 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2652 Vx_lost_selection_functions = Qnil;
2654 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2655 doc: /* A list of functions to be called when Emacs answers a selection request.
2656 The functions are called with three arguments:
2657 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2658 - the selection-type which Emacs was asked to convert the
2659 selection into before sending (for example, `STRING' or `LENGTH');
2660 - a flag indicating success or failure for responding to the request.
2661 We might have failed (and declined the request) for any number of reasons,
2662 including being asked for a selection that we no longer own, or being asked
2663 to convert into a type that we don't know about or that is inappropriate.
2664 This hook doesn't let you change the behavior of Emacs's selection replies,
2665 it merely informs you that they have happened. */);
2666 Vx_sent_selection_functions = Qnil;
2668 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2669 Vx_select_enable_clipboard_manager,
2670 doc: /* Whether to enable X clipboard manager support.
2671 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2672 while owning the X clipboard, the clipboard contents are saved to the
2673 clipboard manager if one is present. */);
2674 Vx_select_enable_clipboard_manager = Qt;
2676 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2677 doc: /* Number of milliseconds to wait for a selection reply.
2678 If the selection owner doesn't reply in this time, we give up.
2679 A value of 0 means wait as long as necessary. This is initialized from the
2680 \"*selectionTimeout\" resource. */);
2681 x_selection_timeout = 0;
2683 /* QPRIMARY is defined in keyboard.c. */
2684 DEFSYM (QSECONDARY, "SECONDARY");
2685 DEFSYM (QSTRING, "STRING");
2686 DEFSYM (QINTEGER, "INTEGER");
2687 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2688 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2689 DEFSYM (QTEXT, "TEXT");
2690 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2691 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2692 DEFSYM (QDELETE, "DELETE");
2693 DEFSYM (QMULTIPLE, "MULTIPLE");
2694 DEFSYM (QINCR, "INCR");
2695 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2696 DEFSYM (QTARGETS, "TARGETS");
2697 DEFSYM (QATOM, "ATOM");
2698 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2699 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2700 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2701 DEFSYM (QNULL, "NULL");
2702 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2703 DEFSYM (Qforeign_selection, "foreign-selection");
2704 DEFSYM (Qx_lost_selection_functions, "x-lost-selection-functions");
2705 DEFSYM (Qx_sent_selection_functions, "x-sent-selection-functions");