Fix minor problems with loaddefs autogeneration
[emacs.git] / src / xselect.c
blob2249828fb4e6e2e67864c0b9e713cbbbba763a27
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2017 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 (at
9 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 "frame.h" /* Need this to get the X window of selected_frame */
35 #include "blockinput.h"
36 #include "termhooks.h"
37 #include "keyboard.h"
39 #include <X11/Xproto.h>
41 struct prop_location;
42 struct selection_data;
44 static void x_decline_selection_request (struct selection_input_event *);
45 static bool x_convert_selection (Lisp_Object, Lisp_Object, Atom, bool,
46 struct x_display_info *);
47 static bool waiting_for_other_props_on_window (Display *, Window);
48 static struct prop_location *expect_property_change (Display *, Window,
49 Atom, int);
50 static void unexpect_property_change (struct prop_location *);
51 static void wait_for_property_change (struct prop_location *);
52 static Lisp_Object x_get_window_property_as_lisp_data (struct x_display_info *,
53 Window, Atom,
54 Lisp_Object, Atom);
55 static Lisp_Object selection_data_to_lisp_data (struct x_display_info *,
56 const unsigned char *,
57 ptrdiff_t, Atom, int);
58 static void lisp_data_to_selection_data (struct x_display_info *, Lisp_Object,
59 struct selection_data *);
61 /* Printing traces to stderr. */
63 #ifdef TRACE_SELECTION
64 #define TRACE0(fmt) \
65 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid ())
66 #define TRACE1(fmt, a0) \
67 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0)
68 #define TRACE2(fmt, a0, a1) \
69 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1)
70 #define TRACE3(fmt, a0, a1, a2) \
71 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1, a2)
72 #else
73 #define TRACE0(fmt) (void) 0
74 #define TRACE1(fmt, a0) (void) 0
75 #define TRACE2(fmt, a0, a1) (void) 0
76 #endif
78 /* Bytes needed to represent 'long' data. This is as per libX11; it
79 is not necessarily sizeof (long). */
80 #define X_LONG_SIZE 4
82 /* If this is a smaller number than the max-request-size of the display,
83 emacs will use INCR selection transfer when the selection is larger
84 than this. The max-request-size is usually around 64k, so if you want
85 emacs to use incremental selection transfers when the selection is
86 smaller than that, set this. I added this mostly for debugging the
87 incremental transfer stuff, but it might improve server performance.
89 This value cannot exceed INT_MAX / max (X_LONG_SIZE, sizeof (long))
90 because it is multiplied by X_LONG_SIZE and by sizeof (long) in
91 subscript calculations. Similarly for PTRDIFF_MAX - 1 or SIZE_MAX
92 - 1 in place of INT_MAX. */
93 #define MAX_SELECTION_QUANTUM \
94 ((int) min (0xFFFFFF, (min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) - 1) \
95 / max (X_LONG_SIZE, sizeof (long)))))
97 static int
98 selection_quantum (Display *display)
100 long mrs = XMaxRequestSize (display);
101 return (mrs < MAX_SELECTION_QUANTUM / X_LONG_SIZE + 25
102 ? (mrs - 25) * X_LONG_SIZE
103 : MAX_SELECTION_QUANTUM);
106 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
107 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
110 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
111 handling. */
113 struct selection_event_queue
115 struct selection_input_event event;
116 struct selection_event_queue *next;
119 static struct selection_event_queue *selection_queue;
121 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
123 static int x_queue_selection_requests;
125 /* True if the input events are duplicates. */
127 static bool
128 selection_input_event_equal (struct selection_input_event *a,
129 struct selection_input_event *b)
131 return (a->kind == b->kind && a->dpyinfo == b->dpyinfo
132 && a->requestor == b->requestor && a->selection == b->selection
133 && a->target == b->target && a->property == b->property
134 && a->time == b->time);
137 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
139 static void
140 x_queue_event (struct selection_input_event *event)
142 struct selection_event_queue *queue_tmp;
144 /* Don't queue repeated requests.
145 This only happens for large requests which uses the incremental protocol. */
146 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
148 if (selection_input_event_equal (event, &queue_tmp->event))
150 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
151 x_decline_selection_request (event);
152 return;
156 queue_tmp = xmalloc (sizeof *queue_tmp);
157 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
158 queue_tmp->event = *event;
159 queue_tmp->next = selection_queue;
160 selection_queue = queue_tmp;
163 /* Start queuing SELECTION_REQUEST_EVENT events. */
165 static void
166 x_start_queuing_selection_requests (void)
168 if (x_queue_selection_requests)
169 emacs_abort ();
171 x_queue_selection_requests++;
172 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
175 /* Stop queuing SELECTION_REQUEST_EVENT events. */
177 static void
178 x_stop_queuing_selection_requests (void)
180 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
181 --x_queue_selection_requests;
183 /* Take all the queued events and put them back
184 so that they get processed afresh. */
186 while (selection_queue != NULL)
188 struct selection_event_queue *queue_tmp = selection_queue;
189 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
190 kbd_buffer_unget_event (&queue_tmp->event);
191 selection_queue = queue_tmp->next;
192 xfree (queue_tmp);
197 /* This converts a Lisp symbol to a server Atom, avoiding a server
198 roundtrip whenever possible. */
200 static Atom
201 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
203 Atom val;
204 if (NILP (sym)) return 0;
205 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
206 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
207 if (EQ (sym, QSTRING)) return XA_STRING;
208 if (EQ (sym, QINTEGER)) return XA_INTEGER;
209 if (EQ (sym, QATOM)) return XA_ATOM;
210 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
211 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
212 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
213 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
214 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
215 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
216 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
217 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
218 if (EQ (sym, Q_EMACS_TMP_)) return dpyinfo->Xatom_EMACS_TMP;
219 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
220 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
221 if (!SYMBOLP (sym)) emacs_abort ();
223 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
224 block_input ();
225 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
226 unblock_input ();
227 return val;
231 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
232 and calls to intern whenever possible. */
234 static Lisp_Object
235 x_atom_to_symbol (struct x_display_info *dpyinfo, Atom atom)
237 char *str;
238 Lisp_Object val;
240 if (! atom)
241 return Qnil;
243 switch (atom)
245 case XA_PRIMARY:
246 return QPRIMARY;
247 case XA_SECONDARY:
248 return QSECONDARY;
249 case XA_STRING:
250 return QSTRING;
251 case XA_INTEGER:
252 return QINTEGER;
253 case XA_ATOM:
254 return QATOM;
257 if (dpyinfo == NULL)
258 return Qnil;
259 if (atom == dpyinfo->Xatom_CLIPBOARD)
260 return QCLIPBOARD;
261 if (atom == dpyinfo->Xatom_TIMESTAMP)
262 return QTIMESTAMP;
263 if (atom == dpyinfo->Xatom_TEXT)
264 return QTEXT;
265 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
266 return QCOMPOUND_TEXT;
267 if (atom == dpyinfo->Xatom_UTF8_STRING)
268 return QUTF8_STRING;
269 if (atom == dpyinfo->Xatom_DELETE)
270 return QDELETE;
271 if (atom == dpyinfo->Xatom_MULTIPLE)
272 return QMULTIPLE;
273 if (atom == dpyinfo->Xatom_INCR)
274 return QINCR;
275 if (atom == dpyinfo->Xatom_EMACS_TMP)
276 return Q_EMACS_TMP_;
277 if (atom == dpyinfo->Xatom_TARGETS)
278 return QTARGETS;
279 if (atom == dpyinfo->Xatom_NULL)
280 return QNULL;
282 block_input ();
283 str = XGetAtomName (dpyinfo->display, atom);
284 unblock_input ();
285 TRACE1 ("XGetAtomName --> %s", str);
286 if (! str) return Qnil;
287 val = intern (str);
288 block_input ();
289 /* This was allocated by Xlib, so use XFree. */
290 XFree (str);
291 unblock_input ();
292 return val;
295 /* Do protocol to assert ourself as a selection owner.
296 FRAME shall be the owner; it must be a valid X frame.
297 Update the Vselection_alist so that we can reply to later requests for
298 our selection. */
300 static void
301 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
302 Lisp_Object frame)
304 struct frame *f = XFRAME (frame);
305 Window selecting_window = FRAME_X_WINDOW (f);
306 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
307 Display *display = dpyinfo->display;
308 Time timestamp = dpyinfo->last_user_time;
309 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
311 block_input ();
312 x_catch_errors (display);
313 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
314 x_check_errors (display, "Can't set selection: %s");
315 x_uncatch_errors_after_check ();
316 unblock_input ();
318 /* Now update the local cache */
320 Lisp_Object selection_data;
321 Lisp_Object prev_value;
323 selection_data = list4 (selection_name, selection_value,
324 INTEGER_TO_CONS (timestamp), frame);
325 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
327 tset_selection_alist
328 (dpyinfo->terminal,
329 Fcons (selection_data, dpyinfo->terminal->Vselection_alist));
331 /* If we already owned the selection, remove the old selection
332 data. Don't use Fdelq as that may quit. */
333 if (!NILP (prev_value))
335 /* We know it's not the CAR, so it's easy. */
336 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
337 for (; CONSP (rest); rest = XCDR (rest))
338 if (EQ (prev_value, Fcar (XCDR (rest))))
340 XSETCDR (rest, XCDR (XCDR (rest)));
341 break;
347 /* Given a selection-name and desired type, look up our local copy of
348 the selection value and convert it to the type.
349 Return nil, a string, a vector, a symbol, an integer, or a cons
350 that CONS_TO_INTEGER could plausibly handle.
351 This function is used both for remote requests (LOCAL_REQUEST is zero)
352 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
354 This calls random Lisp code, and may signal or gc. */
356 static Lisp_Object
357 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
358 bool local_request, struct x_display_info *dpyinfo)
360 Lisp_Object local_value;
361 Lisp_Object handler_fn, value, check;
363 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
365 if (NILP (local_value)) return Qnil;
367 /* TIMESTAMP is a special case. */
368 if (EQ (target_type, QTIMESTAMP))
370 handler_fn = Qnil;
371 value = XCAR (XCDR (XCDR (local_value)));
373 else
375 /* Don't allow a quit within the converter.
376 When the user types C-g, he would be surprised
377 if by luck it came during a converter. */
378 ptrdiff_t count = SPECPDL_INDEX ();
379 specbind (Qinhibit_quit, Qt);
381 CHECK_SYMBOL (target_type);
382 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
384 if (!NILP (handler_fn))
385 value = call3 (handler_fn,
386 selection_symbol, (local_request ? Qnil : target_type),
387 XCAR (XCDR (local_value)));
388 else
389 value = Qnil;
390 unbind_to (count, Qnil);
393 /* Make sure this value is of a type that we could transmit
394 to another X client. */
396 check = value;
397 if (CONSP (value)
398 && SYMBOLP (XCAR (value)))
399 check = XCDR (value);
401 if (STRINGP (check)
402 || VECTORP (check)
403 || SYMBOLP (check)
404 || INTEGERP (check)
405 || NILP (value))
406 return value;
407 /* Check for a value that CONS_TO_INTEGER could handle. */
408 else if (CONSP (check)
409 && INTEGERP (XCAR (check))
410 && (INTEGERP (XCDR (check))
412 (CONSP (XCDR (check))
413 && INTEGERP (XCAR (XCDR (check)))
414 && NILP (XCDR (XCDR (check))))))
415 return value;
417 signal_error ("Invalid data returned by selection-conversion function",
418 list2 (handler_fn, value));
421 /* Subroutines of x_reply_selection_request. */
423 /* Send a SelectionNotify event to the requestor with property=None,
424 meaning we were unable to do what they wanted. */
426 static void
427 x_decline_selection_request (struct selection_input_event *event)
429 XEvent reply_base;
430 XSelectionEvent *reply = &(reply_base.xselection);
432 reply->type = SelectionNotify;
433 reply->display = SELECTION_EVENT_DISPLAY (event);
434 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
435 reply->selection = SELECTION_EVENT_SELECTION (event);
436 reply->time = SELECTION_EVENT_TIME (event);
437 reply->target = SELECTION_EVENT_TARGET (event);
438 reply->property = None;
440 /* The reason for the error may be that the receiver has
441 died in the meantime. Handle that case. */
442 block_input ();
443 x_catch_errors (reply->display);
444 XSendEvent (reply->display, reply->requestor, False, 0, &reply_base);
445 XFlush (reply->display);
446 x_uncatch_errors ();
447 unblock_input ();
450 /* This is the selection request currently being processed.
451 It is set to zero when the request is fully processed. */
452 static struct selection_input_event *x_selection_current_request;
454 /* Display info in x_selection_request. */
456 static struct x_display_info *selection_request_dpyinfo;
458 /* Raw selection data, for sending to a requestor window. */
460 struct selection_data
462 unsigned char *data;
463 ptrdiff_t size;
464 int format;
465 Atom type;
466 bool nofree;
467 Atom property;
468 /* This can be set to non-NULL during x_reply_selection_request, if
469 the selection is waiting for an INCR transfer to complete. Don't
470 free these; that's done by unexpect_property_change. */
471 struct prop_location *wait_object;
472 struct selection_data *next;
475 /* Linked list of the above (in support of MULTIPLE targets). */
477 static struct selection_data *converted_selections;
479 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
480 static Atom conversion_fail_tag;
482 /* Used as an unwind-protect clause so that, if a selection-converter signals
483 an error, we tell the requestor that we were unable to do what they wanted
484 before we throw to top-level or go into the debugger or whatever. */
486 static void
487 x_selection_request_lisp_error (void)
489 struct selection_data *cs, *next;
491 for (cs = converted_selections; cs; cs = next)
493 next = cs->next;
494 if (! cs->nofree && cs->data)
495 xfree (cs->data);
496 xfree (cs);
498 converted_selections = NULL;
500 if (x_selection_current_request != 0
501 && selection_request_dpyinfo->display)
502 x_decline_selection_request (x_selection_current_request);
505 static void
506 x_catch_errors_unwind (void)
508 block_input ();
509 x_uncatch_errors ();
510 unblock_input ();
514 /* This stuff is so that INCR selections are reentrant (that is, so we can
515 be servicing multiple INCR selection requests simultaneously.) I haven't
516 actually tested that yet. */
518 /* Keep a list of the property changes that are awaited. */
520 struct prop_location
522 int identifier;
523 Display *display;
524 Window window;
525 Atom property;
526 int desired_state;
527 bool arrived;
528 struct prop_location *next;
531 static int prop_location_identifier;
533 static Lisp_Object property_change_reply;
535 static struct prop_location *property_change_reply_object;
537 static struct prop_location *property_change_wait_list;
539 static void
540 set_property_change_object (struct prop_location *location)
542 /* Input must be blocked so we don't get the event before we set these. */
543 if (! input_blocked_p ())
544 emacs_abort ();
545 XSETCAR (property_change_reply, Qnil);
546 property_change_reply_object = location;
550 /* Send the reply to a selection request event EVENT. */
552 #ifdef TRACE_SELECTION
553 static int x_reply_selection_request_cnt;
554 #endif /* TRACE_SELECTION */
556 static void
557 x_reply_selection_request (struct selection_input_event *event,
558 struct x_display_info *dpyinfo)
560 XEvent reply_base;
561 XSelectionEvent *reply = &(reply_base.xselection);
562 Display *display = SELECTION_EVENT_DISPLAY (event);
563 Window window = SELECTION_EVENT_REQUESTOR (event);
564 ptrdiff_t bytes_remaining;
565 int max_bytes = selection_quantum (display);
566 ptrdiff_t count = SPECPDL_INDEX ();
567 struct selection_data *cs;
569 reply->type = SelectionNotify;
570 reply->display = display;
571 reply->requestor = window;
572 reply->selection = SELECTION_EVENT_SELECTION (event);
573 reply->time = SELECTION_EVENT_TIME (event);
574 reply->target = SELECTION_EVENT_TARGET (event);
575 reply->property = SELECTION_EVENT_PROPERTY (event);
576 if (reply->property == None)
577 reply->property = reply->target;
579 block_input ();
580 /* The protected block contains wait_for_property_change, which can
581 run random lisp code (process handlers) or signal. Therefore, we
582 put the x_uncatch_errors call in an unwind. */
583 record_unwind_protect_void (x_catch_errors_unwind);
584 x_catch_errors (display);
586 /* Loop over converted selections, storing them in the requested
587 properties. If data is large, only store the first N bytes
588 (section 2.7.2 of ICCCM). Note that we store the data for a
589 MULTIPLE request in the opposite order; the ICCM says only that
590 the conversion itself must be done in the same order. */
591 for (cs = converted_selections; cs; cs = cs->next)
593 if (cs->property == None)
594 continue;
596 bytes_remaining = cs->size;
597 bytes_remaining *= cs->format >> 3;
598 if (bytes_remaining <= max_bytes)
600 /* Send all the data at once, with minimal handshaking. */
601 TRACE1 ("Sending all %"pD"d bytes", bytes_remaining);
602 XChangeProperty (display, window, cs->property,
603 cs->type, cs->format, PropModeReplace,
604 cs->data, cs->size);
606 else
608 /* Send an INCR tag to initiate incremental transfer. */
609 long value[1];
611 TRACE2 ("Start sending %"pD"d bytes incrementally (%s)",
612 bytes_remaining, XGetAtomName (display, cs->property));
613 cs->wait_object
614 = expect_property_change (display, window, cs->property,
615 PropertyDelete);
617 /* XChangeProperty expects an array of long even if long is
618 more than 32 bits. */
619 value[0] = min (bytes_remaining, X_LONG_MAX);
620 XChangeProperty (display, window, cs->property,
621 dpyinfo->Xatom_INCR, 32, PropModeReplace,
622 (unsigned char *) value, 1);
623 XSelectInput (display, window, PropertyChangeMask);
627 /* Now issue the SelectionNotify event. */
628 XSendEvent (display, window, False, 0, &reply_base);
629 XFlush (display);
631 #ifdef TRACE_SELECTION
633 char *sel = XGetAtomName (display, reply->selection);
634 char *tgt = XGetAtomName (display, reply->target);
635 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
636 sel, tgt, ++x_reply_selection_request_cnt);
637 if (sel) XFree (sel);
638 if (tgt) XFree (tgt);
640 #endif /* TRACE_SELECTION */
642 /* Finish sending the rest of each of the INCR values. This should
643 be improved; there's a chance of deadlock if more than one
644 subtarget in a MULTIPLE selection requires an INCR transfer, and
645 the requestor and Emacs loop waiting on different transfers. */
646 for (cs = converted_selections; cs; cs = cs->next)
647 if (cs->wait_object)
649 int format_bytes = cs->format / 8;
650 bool had_errors_p = x_had_errors_p (display);
652 /* Must set this inside block_input (). unblock_input may read
653 events and setting property_change_reply in
654 wait_for_property_change is then too late. */
655 set_property_change_object (cs->wait_object);
656 unblock_input ();
658 bytes_remaining = cs->size;
659 bytes_remaining *= format_bytes;
661 /* Wait for the requestor to ack by deleting the property.
662 This can run Lisp code (process handlers) or signal. */
663 if (! had_errors_p)
665 TRACE1 ("Waiting for ACK (deletion of %s)",
666 XGetAtomName (display, cs->property));
667 wait_for_property_change (cs->wait_object);
669 else
670 unexpect_property_change (cs->wait_object);
672 while (bytes_remaining)
674 int i = ((bytes_remaining < max_bytes)
675 ? bytes_remaining
676 : max_bytes) / format_bytes;
677 block_input ();
679 cs->wait_object
680 = expect_property_change (display, window, cs->property,
681 PropertyDelete);
683 TRACE1 ("Sending increment of %d elements", i);
684 TRACE1 ("Set %s to increment data",
685 XGetAtomName (display, cs->property));
687 /* Append the next chunk of data to the property. */
688 XChangeProperty (display, window, cs->property,
689 cs->type, cs->format, PropModeAppend,
690 cs->data, i);
691 bytes_remaining -= i * format_bytes;
692 cs->data += i * ((cs->format == 32) ? sizeof (long)
693 : format_bytes);
694 XFlush (display);
695 had_errors_p = x_had_errors_p (display);
696 /* See comment above about property_change_reply. */
697 set_property_change_object (cs->wait_object);
698 unblock_input ();
700 if (had_errors_p) break;
702 /* Wait for the requestor to ack this chunk by deleting
703 the property. This can run Lisp code or signal. */
704 TRACE1 ("Waiting for increment ACK (deletion of %s)",
705 XGetAtomName (display, cs->property));
706 wait_for_property_change (cs->wait_object);
709 /* Now write a zero-length chunk to the property to tell the
710 requestor that we're done. */
711 block_input ();
712 if (! waiting_for_other_props_on_window (display, window))
713 XSelectInput (display, window, 0);
715 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
716 XGetAtomName (display, cs->property));
717 XChangeProperty (display, window, cs->property,
718 cs->type, cs->format, PropModeReplace,
719 cs->data, 0);
720 TRACE0 ("Done sending incrementally");
723 /* rms, 2003-01-03: I think I have fixed this bug. */
724 /* The window we're communicating with may have been deleted
725 in the meantime (that's a real situation from a bug report).
726 In this case, there may be events in the event queue still
727 referring to the deleted window, and we'll get a BadWindow error
728 in XTread_socket when processing the events. I don't have
729 an idea how to fix that. gerd, 2001-01-98. */
730 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
731 delivered before uncatch errors. */
732 XSync (display, False);
733 unblock_input ();
735 /* GTK queues events in addition to the queue in Xlib. So we
736 UNBLOCK to enter the event loop and get possible errors delivered,
737 and then BLOCK again because x_uncatch_errors requires it. */
738 block_input ();
739 /* This calls x_uncatch_errors. */
740 unbind_to (count, Qnil);
741 unblock_input ();
744 /* Handle a SelectionRequest event EVENT.
745 This is called from keyboard.c when such an event is found in the queue. */
747 static void
748 x_handle_selection_request (struct selection_input_event *event)
750 Time local_selection_time;
752 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
753 Atom selection = SELECTION_EVENT_SELECTION (event);
754 Lisp_Object selection_symbol = x_atom_to_symbol (dpyinfo, selection);
755 Atom target = SELECTION_EVENT_TARGET (event);
756 Lisp_Object target_symbol = x_atom_to_symbol (dpyinfo, target);
757 Atom property = SELECTION_EVENT_PROPERTY (event);
758 Lisp_Object local_selection_data;
759 bool success = false;
760 ptrdiff_t count = SPECPDL_INDEX ();
762 if (!dpyinfo) goto DONE;
764 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
766 /* Decline if we don't own any selections. */
767 if (NILP (local_selection_data)) goto DONE;
769 /* Decline requests issued prior to our acquiring the selection. */
770 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
771 Time, local_selection_time);
772 if (SELECTION_EVENT_TIME (event) != CurrentTime
773 && local_selection_time > SELECTION_EVENT_TIME (event))
774 goto DONE;
776 x_selection_current_request = event;
777 selection_request_dpyinfo = dpyinfo;
778 record_unwind_protect_void (x_selection_request_lisp_error);
780 /* We might be able to handle nested x_handle_selection_requests,
781 but this is difficult to test, and seems unimportant. */
782 x_start_queuing_selection_requests ();
783 record_unwind_protect_void (x_stop_queuing_selection_requests);
785 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
786 SDATA (SYMBOL_NAME (selection_symbol)),
787 SDATA (SYMBOL_NAME (target_symbol)));
789 if (EQ (target_symbol, QMULTIPLE))
791 /* For MULTIPLE targets, the event property names a list of atom
792 pairs; the first atom names a target and the second names a
793 non-None property. */
794 Window requestor = SELECTION_EVENT_REQUESTOR (event);
795 Lisp_Object multprop;
796 ptrdiff_t j, nselections;
798 if (property == None) goto DONE;
799 multprop
800 = x_get_window_property_as_lisp_data (dpyinfo, requestor, property,
801 QMULTIPLE, selection);
803 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
804 goto DONE;
806 nselections = ASIZE (multprop) / 2;
807 /* Perform conversions. This can signal. */
808 for (j = 0; j < nselections; j++)
810 Lisp_Object subtarget = AREF (multprop, 2*j);
811 Atom subproperty = symbol_to_x_atom (dpyinfo,
812 AREF (multprop, 2*j+1));
814 if (subproperty != None)
815 x_convert_selection (selection_symbol, subtarget,
816 subproperty, true, dpyinfo);
818 success = true;
820 else
822 if (property == None)
823 property = SELECTION_EVENT_TARGET (event);
824 success = x_convert_selection (selection_symbol,
825 target_symbol, property,
826 false, dpyinfo);
829 DONE:
831 if (success)
832 x_reply_selection_request (event, dpyinfo);
833 else
834 x_decline_selection_request (event);
835 x_selection_current_request = 0;
837 /* Run the `x-sent-selection-functions' abnormal hook. */
838 if (!NILP (Vx_sent_selection_functions)
839 && !EQ (Vx_sent_selection_functions, Qunbound))
840 CALLN (Frun_hook_with_args, Qx_sent_selection_functions,
841 selection_symbol, target_symbol, success ? Qt : Qnil);
843 unbind_to (count, Qnil);
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 (Lisp_Object selection_symbol,
855 Lisp_Object target_symbol, Atom property,
856 bool for_multiple, struct x_display_info *dpyinfo)
858 Lisp_Object lisp_selection;
859 struct selection_data *cs;
861 lisp_selection
862 = x_get_local_selection (selection_symbol, target_symbol,
863 false, dpyinfo);
865 /* A nil return value means we can't perform the conversion. */
866 if (NILP (lisp_selection)
867 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
869 if (for_multiple)
871 cs = xmalloc (sizeof *cs);
872 cs->data = (unsigned char *) &conversion_fail_tag;
873 cs->size = 1;
874 cs->format = 32;
875 cs->type = XA_ATOM;
876 cs->nofree = true;
877 cs->property = property;
878 cs->wait_object = NULL;
879 cs->next = converted_selections;
880 converted_selections = cs;
883 return false;
886 /* Otherwise, record the converted selection to binary. */
887 cs = xmalloc (sizeof *cs);
888 cs->data = NULL;
889 cs->nofree = true;
890 cs->property = property;
891 cs->wait_object = NULL;
892 cs->next = converted_selections;
893 converted_selections = cs;
894 lisp_data_to_selection_data (dpyinfo, lisp_selection, cs);
895 return true;
898 /* Handle a SelectionClear event EVENT, which indicates that some
899 client cleared out our previously asserted selection.
900 This is called from keyboard.c when such an event is found in the queue. */
902 static void
903 x_handle_selection_clear (struct selection_input_event *event)
905 Atom selection = SELECTION_EVENT_SELECTION (event);
906 Time changed_owner_time = SELECTION_EVENT_TIME (event);
908 Lisp_Object selection_symbol, local_selection_data;
909 Time local_selection_time;
910 struct x_display_info *dpyinfo = SELECTION_EVENT_DPYINFO (event);
911 Lisp_Object Vselection_alist;
913 TRACE0 ("x_handle_selection_clear");
915 if (!dpyinfo) return;
917 selection_symbol = x_atom_to_symbol (dpyinfo, selection);
918 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
920 /* Well, we already believe that we don't own it, so that's just fine. */
921 if (NILP (local_selection_data)) return;
923 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
924 Time, local_selection_time);
926 /* We have reasserted the selection since this SelectionClear was
927 generated, so we can disregard it. */
928 if (changed_owner_time != CurrentTime
929 && local_selection_time > changed_owner_time)
930 return;
932 /* Otherwise, really clear. Don't use Fdelq as that may quit. */
933 Vselection_alist = dpyinfo->terminal->Vselection_alist;
934 if (EQ (local_selection_data, CAR (Vselection_alist)))
935 Vselection_alist = XCDR (Vselection_alist);
936 else
938 Lisp_Object rest;
939 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
940 if (EQ (local_selection_data, CAR (XCDR (rest))))
942 XSETCDR (rest, XCDR (XCDR (rest)));
943 break;
946 tset_selection_alist (dpyinfo->terminal, Vselection_alist);
948 /* Run the `x-lost-selection-functions' abnormal hook. */
949 CALLN (Frun_hook_with_args, Qx_lost_selection_functions, selection_symbol);
951 redisplay_preserve_echo_area (20);
954 void
955 x_handle_selection_event (struct selection_input_event *event)
957 TRACE0 ("x_handle_selection_event");
958 if (event->kind != SELECTION_REQUEST_EVENT)
959 x_handle_selection_clear (event);
960 else if (x_queue_selection_requests)
961 x_queue_event (event);
962 else
963 x_handle_selection_request (event);
967 /* Clear all selections that were made from frame F.
968 We do this when about to delete a frame. */
970 void
971 x_clear_frame_selections (struct frame *f)
973 Lisp_Object frame;
974 Lisp_Object rest;
975 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
976 struct terminal *t = dpyinfo->terminal;
978 XSETFRAME (frame, f);
980 /* Delete elements from the beginning of Vselection_alist. */
981 while (CONSP (t->Vselection_alist)
982 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
984 /* Run the `x-lost-selection-functions' abnormal hook. */
985 CALLN (Frun_hook_with_args, Qx_lost_selection_functions,
986 Fcar (Fcar (t->Vselection_alist)));
988 tset_selection_alist (t, XCDR (t->Vselection_alist));
991 /* Delete elements after the beginning of Vselection_alist. */
992 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
993 if (CONSP (XCDR (rest))
994 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
996 CALLN (Frun_hook_with_args, Qx_lost_selection_functions,
997 XCAR (XCAR (XCDR (rest))));
998 XSETCDR (rest, XCDR (XCDR (rest)));
999 break;
1003 /* True if any properties for DISPLAY and WINDOW
1004 are on the list of what we are waiting for. */
1006 static bool
1007 waiting_for_other_props_on_window (Display *display, Window window)
1009 for (struct prop_location *p = property_change_wait_list; p; p = p->next)
1010 if (p->display == display && p->window == window)
1011 return true;
1012 return false;
1015 /* Add an entry to the list of property changes we are waiting for.
1016 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1017 The return value is a number that uniquely identifies
1018 this awaited property change. */
1020 static struct prop_location *
1021 expect_property_change (Display *display, Window window,
1022 Atom property, int state)
1024 struct prop_location *pl = xmalloc (sizeof *pl);
1025 pl->identifier = ++prop_location_identifier;
1026 pl->display = display;
1027 pl->window = window;
1028 pl->property = property;
1029 pl->desired_state = state;
1030 pl->next = property_change_wait_list;
1031 pl->arrived = false;
1032 property_change_wait_list = pl;
1033 return pl;
1036 /* Delete an entry from the list of property changes we are waiting for.
1037 IDENTIFIER is the number that uniquely identifies the entry. */
1039 static void
1040 unexpect_property_change (struct prop_location *location)
1042 struct prop_location *prop, **pprev = &property_change_wait_list;
1044 for (prop = property_change_wait_list; prop; prop = *pprev)
1046 if (prop == location)
1048 *pprev = prop->next;
1049 xfree (prop);
1050 break;
1052 else
1053 pprev = &prop->next;
1057 /* Remove the property change expectation element for IDENTIFIER. */
1059 static void
1060 wait_for_property_change_unwind (void *loc)
1062 struct prop_location *location = loc;
1064 unexpect_property_change (location);
1065 if (location == property_change_reply_object)
1066 property_change_reply_object = 0;
1069 /* Actually wait for a property change.
1070 IDENTIFIER should be the value that expect_property_change returned. */
1072 static void
1073 wait_for_property_change (struct prop_location *location)
1075 ptrdiff_t count = SPECPDL_INDEX ();
1077 /* Make sure to do unexpect_property_change if we quit or err. */
1078 record_unwind_protect_ptr (wait_for_property_change_unwind, location);
1080 /* See comment in x_reply_selection_request about setting
1081 property_change_reply. Do not do it here. */
1083 /* If the event we are waiting for arrives beyond here, it will set
1084 property_change_reply, because property_change_reply_object says so. */
1085 if (! location->arrived)
1087 EMACS_INT timeout = max (0, x_selection_timeout);
1088 EMACS_INT secs = timeout / 1000;
1089 int nsecs = (timeout % 1000) * 1000000;
1090 TRACE2 (" Waiting %"pI"d secs, %d nsecs", secs, nsecs);
1091 wait_reading_process_output (secs, nsecs, 0, false,
1092 property_change_reply, NULL, 0);
1094 if (NILP (XCAR (property_change_reply)))
1096 TRACE0 (" Timed out");
1097 error ("Timed out waiting for property-notify event");
1101 unbind_to (count, Qnil);
1104 /* Called from XTread_socket in response to a PropertyNotify event. */
1106 void
1107 x_handle_property_notify (const XPropertyEvent *event)
1109 struct prop_location *rest;
1111 for (rest = property_change_wait_list; rest; rest = rest->next)
1113 if (!rest->arrived
1114 && rest->property == event->atom
1115 && rest->window == event->window
1116 && rest->display == event->display
1117 && rest->desired_state == event->state)
1119 TRACE2 ("Expected %s of property %s",
1120 (event->state == PropertyDelete ? "deletion" : "change"),
1121 XGetAtomName (event->display, event->atom));
1123 rest->arrived = true;
1125 /* If this is the one wait_for_property_change is waiting for,
1126 tell it to wake up. */
1127 if (rest == property_change_reply_object)
1128 XSETCAR (property_change_reply, Qt);
1130 return;
1137 /* Variables for communication with x_handle_selection_notify. */
1138 static Atom reading_which_selection;
1139 static Lisp_Object reading_selection_reply;
1140 static Window reading_selection_window;
1142 /* Do protocol to read selection-data from the server.
1143 Converts this to Lisp data and returns it.
1144 FRAME is the frame whose X window shall request the selection. */
1146 static Lisp_Object
1147 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1148 Lisp_Object time_stamp, Lisp_Object frame)
1150 struct frame *f = XFRAME (frame);
1151 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1152 Display *display = dpyinfo->display;
1153 Window requestor_window = FRAME_X_WINDOW (f);
1154 Time requestor_time = dpyinfo->last_user_time;
1155 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1156 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1157 Atom type_atom = (CONSP (target_type)
1158 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1159 : symbol_to_x_atom (dpyinfo, target_type));
1160 EMACS_INT timeout, secs;
1161 int nsecs;
1163 if (!FRAME_LIVE_P (f))
1164 return Qnil;
1166 if (! NILP (time_stamp))
1167 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1169 block_input ();
1170 TRACE2 ("Get selection %s, type %s",
1171 XGetAtomName (display, type_atom),
1172 XGetAtomName (display, target_property));
1174 x_catch_errors (display);
1175 XConvertSelection (display, selection_atom, type_atom, target_property,
1176 requestor_window, requestor_time);
1177 x_check_errors (display, "Can't convert selection: %s");
1178 x_uncatch_errors_after_check ();
1180 /* Prepare to block until the reply has been read. */
1181 reading_selection_window = requestor_window;
1182 reading_which_selection = selection_atom;
1183 XSETCAR (reading_selection_reply, Qnil);
1185 /* It should not be necessary to stop handling selection requests
1186 during this time. In fact, the SAVE_TARGETS mechanism requires
1187 us to handle a clipboard manager's requests before it returns
1188 SelectionNotify. */
1189 #if false
1190 x_start_queuing_selection_requests ();
1191 record_unwind_protect_void (x_stop_queuing_selection_requests);
1192 #endif
1194 unblock_input ();
1196 /* This allows quits. Also, don't wait forever. */
1197 timeout = max (0, x_selection_timeout);
1198 secs = timeout / 1000;
1199 nsecs = (timeout % 1000) * 1000000;
1200 TRACE1 (" Start waiting %"pI"d secs for SelectionNotify", secs);
1201 wait_reading_process_output (secs, nsecs, 0, false,
1202 reading_selection_reply, NULL, 0);
1203 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1205 if (NILP (XCAR (reading_selection_reply)))
1206 error ("Timed out waiting for reply from selection owner");
1207 if (EQ (XCAR (reading_selection_reply), Qlambda))
1208 return Qnil;
1210 /* Otherwise, the selection is waiting for us on the requested property. */
1211 return
1212 x_get_window_property_as_lisp_data (dpyinfo, requestor_window,
1213 target_property, target_type,
1214 selection_atom);
1217 /* Subroutines of x_get_window_property_as_lisp_data */
1219 /* Use xfree, not XFree, to free the data obtained with this function. */
1221 static void
1222 x_get_window_property (Display *display, Window window, Atom property,
1223 unsigned char **data_ret, ptrdiff_t *bytes_ret,
1224 Atom *actual_type_ret, int *actual_format_ret,
1225 unsigned long *actual_size_ret)
1227 ptrdiff_t total_size;
1228 unsigned long bytes_remaining;
1229 ptrdiff_t offset = 0;
1230 unsigned char *data = 0;
1231 unsigned char *tmp_data = 0;
1232 int result;
1233 int buffer_size = selection_quantum (display);
1235 /* Wide enough to avoid overflow in expressions using it. */
1236 ptrdiff_t x_long_size = X_LONG_SIZE;
1238 /* Maximum value for TOTAL_SIZE. It cannot exceed PTRDIFF_MAX - 1
1239 and SIZE_MAX - 1, for an extra byte at the end. And it cannot
1240 exceed LONG_MAX * X_LONG_SIZE, for XGetWindowProperty. */
1241 ptrdiff_t total_size_max =
1242 ((min (PTRDIFF_MAX, SIZE_MAX) - 1) / x_long_size < LONG_MAX
1243 ? min (PTRDIFF_MAX, SIZE_MAX) - 1
1244 : LONG_MAX * x_long_size);
1246 block_input ();
1248 /* First probe the thing to find out how big it is. */
1249 result = XGetWindowProperty (display, window, property,
1250 0, 0, False, AnyPropertyType,
1251 actual_type_ret, actual_format_ret,
1252 actual_size_ret,
1253 &bytes_remaining, &tmp_data);
1254 if (result != Success)
1255 goto done;
1257 /* This was allocated by Xlib, so use XFree. */
1258 XFree (tmp_data);
1260 if (*actual_type_ret == None || *actual_format_ret == 0)
1261 goto done;
1263 if (total_size_max < bytes_remaining)
1264 goto size_overflow;
1265 total_size = bytes_remaining;
1266 data = xmalloc (total_size + 1);
1268 /* Now read, until we've gotten it all. */
1269 while (bytes_remaining)
1271 ptrdiff_t bytes_gotten;
1272 int bytes_per_item;
1273 result
1274 = XGetWindowProperty (display, window, property,
1275 offset / X_LONG_SIZE,
1276 buffer_size / X_LONG_SIZE,
1277 False,
1278 AnyPropertyType,
1279 actual_type_ret, actual_format_ret,
1280 actual_size_ret, &bytes_remaining, &tmp_data);
1282 /* If this doesn't return Success at this point, it means that
1283 some clod deleted the selection while we were in the midst of
1284 reading it. Deal with that, I guess.... */
1285 if (result != Success)
1286 break;
1288 bytes_per_item = *actual_format_ret >> 3;
1289 eassert (*actual_size_ret <= buffer_size / bytes_per_item);
1291 /* The man page for XGetWindowProperty says:
1292 "If the returned format is 32, the returned data is represented
1293 as a long array and should be cast to that type to obtain the
1294 elements."
1295 This applies even if long is more than 32 bits, the X library
1296 converts from 32 bit elements received from the X server to long
1297 and passes the long array to us. Thus, for that case memcpy can not
1298 be used. We convert to a 32 bit type here, because so much code
1299 assume on that.
1301 The bytes and offsets passed to XGetWindowProperty refers to the
1302 property and those are indeed in 32 bit quantities if format is 32. */
1304 bytes_gotten = *actual_size_ret;
1305 bytes_gotten *= bytes_per_item;
1307 TRACE2 ("Read %"pD"d bytes from property %s",
1308 bytes_gotten, XGetAtomName (display, property));
1310 if (total_size - offset < bytes_gotten)
1312 unsigned char *data1;
1313 ptrdiff_t remaining_lim = total_size_max - offset - bytes_gotten;
1314 if (remaining_lim < 0 || remaining_lim < bytes_remaining)
1315 goto size_overflow;
1316 total_size = offset + bytes_gotten + bytes_remaining;
1317 data1 = xrealloc (data, total_size + 1);
1318 data = data1;
1321 if (LONG_WIDTH > 32 && *actual_format_ret == 32)
1323 unsigned long i;
1324 int *idata = (int *) (data + offset);
1325 long *ldata = (long *) tmp_data;
1327 for (i = 0; i < *actual_size_ret; ++i)
1328 idata[i] = ldata[i];
1330 else
1331 memcpy (data + offset, tmp_data, bytes_gotten);
1333 offset += bytes_gotten;
1335 /* This was allocated by Xlib, so use XFree. */
1336 XFree (tmp_data);
1339 XFlush (display);
1340 data[offset] = '\0';
1342 done:
1343 unblock_input ();
1344 *data_ret = data;
1345 *bytes_ret = offset;
1346 return;
1348 size_overflow:
1349 if (data)
1350 xfree (data);
1351 unblock_input ();
1352 memory_full (SIZE_MAX);
1355 /* Use xfree, not XFree, to free the data obtained with this function. */
1357 static void
1358 receive_incremental_selection (struct x_display_info *dpyinfo,
1359 Window window, Atom property,
1360 Lisp_Object target_type,
1361 unsigned int min_size_bytes,
1362 unsigned char **data_ret,
1363 ptrdiff_t *size_bytes_ret,
1364 Atom *type_ret, int *format_ret,
1365 unsigned long *size_ret)
1367 ptrdiff_t offset = 0;
1368 struct prop_location *wait_object;
1369 Display *display = dpyinfo->display;
1371 if (min (PTRDIFF_MAX, SIZE_MAX) < min_size_bytes)
1372 memory_full (SIZE_MAX);
1373 *data_ret = xmalloc (min_size_bytes);
1374 *size_bytes_ret = min_size_bytes;
1376 TRACE1 ("Read %u bytes incrementally", min_size_bytes);
1378 /* At this point, we have read an INCR property.
1379 Delete the property to ack it.
1380 (But first, prepare to receive the next event in this handshake.)
1382 Now, we must loop, waiting for the sending window to put a value on
1383 that property, then reading the property, then deleting it to ack.
1384 We are done when the sender places a property of length 0.
1386 block_input ();
1387 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1388 TRACE1 (" Delete property %s",
1389 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1390 XDeleteProperty (display, window, property);
1391 TRACE1 (" Expect new value of property %s",
1392 SDATA (SYMBOL_NAME (x_atom_to_symbol (dpyinfo, property))));
1393 wait_object = expect_property_change (display, window, property,
1394 PropertyNewValue);
1395 XFlush (display);
1396 /* See comment in x_reply_selection_request about property_change_reply. */
1397 set_property_change_object (wait_object);
1398 unblock_input ();
1400 while (true)
1402 unsigned char *tmp_data;
1403 ptrdiff_t tmp_size_bytes;
1405 TRACE0 (" Wait for property change");
1406 wait_for_property_change (wait_object);
1408 /* expect it again immediately, because x_get_window_property may
1409 .. no it won't, I don't get it.
1410 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1411 TRACE0 (" Get property value");
1412 x_get_window_property (display, window, property,
1413 &tmp_data, &tmp_size_bytes,
1414 type_ret, format_ret, size_ret);
1416 TRACE1 (" Read increment of %"pD"d bytes", tmp_size_bytes);
1418 if (tmp_size_bytes == 0) /* we're done */
1420 TRACE0 ("Done reading incrementally");
1422 if (! waiting_for_other_props_on_window (display, window))
1423 XSelectInput (display, window, STANDARD_EVENT_SET);
1424 /* Use xfree, not XFree, because x_get_window_property
1425 calls xmalloc itself. */
1426 xfree (tmp_data);
1427 break;
1430 block_input ();
1431 TRACE1 (" ACK by deleting property %s",
1432 XGetAtomName (display, property));
1433 XDeleteProperty (display, window, property);
1434 wait_object = expect_property_change (display, window, property,
1435 PropertyNewValue);
1436 /* See comment in x_reply_selection_request about
1437 property_change_reply. */
1438 set_property_change_object (wait_object);
1439 XFlush (display);
1440 unblock_input ();
1442 if (*size_bytes_ret - offset < tmp_size_bytes)
1443 *data_ret = xpalloc (*data_ret, size_bytes_ret,
1444 tmp_size_bytes - (*size_bytes_ret - offset),
1445 -1, 1);
1447 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1448 offset += tmp_size_bytes;
1450 /* Use xfree, not XFree, because x_get_window_property
1451 calls xmalloc itself. */
1452 xfree (tmp_data);
1457 /* Fetch a value from property PROPERTY of X window WINDOW on display
1458 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1459 if this fails. */
1461 static Lisp_Object
1462 x_get_window_property_as_lisp_data (struct x_display_info *dpyinfo,
1463 Window window, Atom property,
1464 Lisp_Object target_type,
1465 Atom selection_atom)
1467 Atom actual_type;
1468 int actual_format;
1469 unsigned long actual_size;
1470 unsigned char *data = 0;
1471 ptrdiff_t bytes = 0;
1472 Lisp_Object val;
1473 Display *display = dpyinfo->display;
1475 TRACE0 ("Reading selection data");
1477 x_get_window_property (display, window, property, &data, &bytes,
1478 &actual_type, &actual_format, &actual_size);
1479 if (! data)
1481 block_input ();
1482 bool there_is_a_selection_owner
1483 = XGetSelectionOwner (display, selection_atom) != 0;
1484 unblock_input ();
1485 if (there_is_a_selection_owner)
1486 signal_error ("Selection owner couldn't convert",
1487 actual_type
1488 ? list2 (target_type,
1489 x_atom_to_symbol (dpyinfo, actual_type))
1490 : target_type);
1491 else
1492 signal_error ("No selection",
1493 x_atom_to_symbol (dpyinfo, selection_atom));
1496 if (actual_type == dpyinfo->Xatom_INCR)
1498 /* That wasn't really the data, just the beginning. */
1500 unsigned int min_size_bytes = * ((unsigned int *) data);
1501 block_input ();
1502 /* Use xfree, not XFree, because x_get_window_property
1503 calls xmalloc itself. */
1504 xfree (data);
1505 unblock_input ();
1506 receive_incremental_selection (dpyinfo, window, property, target_type,
1507 min_size_bytes, &data, &bytes,
1508 &actual_type, &actual_format,
1509 &actual_size);
1512 block_input ();
1513 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1514 XDeleteProperty (display, window, property);
1515 XFlush (display);
1516 unblock_input ();
1518 /* It's been read. Now convert it to a lisp object in some semi-rational
1519 manner. */
1520 val = selection_data_to_lisp_data (dpyinfo, data, bytes,
1521 actual_type, actual_format);
1523 /* Use xfree, not XFree, because x_get_window_property
1524 calls xmalloc itself. */
1525 xfree (data);
1526 return val;
1529 /* These functions convert from the selection data read from the server into
1530 something that we can use from Lisp, and vice versa.
1532 Type: Format: Size: Lisp Type:
1533 ----- ------- ----- -----------
1534 * 8 * String
1535 ATOM 32 1 Symbol
1536 ATOM 32 > 1 Vector of Symbols
1537 * 16 1 Integer
1538 * 16 > 1 Vector of Integers
1539 * 32 1 if <=16 bits: Integer
1540 if > 16 bits: Cons of top16, bot16
1541 * 32 > 1 Vector of the above
1543 When converting a Lisp number to C, it is assumed to be of format 16 if
1544 it is an integer, and of format 32 if it is a cons of two integers.
1546 When converting a vector of numbers from Lisp to C, it is assumed to be
1547 of format 16 if every element in the vector is an integer, and is assumed
1548 to be of format 32 if any element is a cons of two integers.
1550 When converting an object to C, it may be of the form (SYMBOL . <data>)
1551 where SYMBOL is what we should claim that the type is. Format and
1552 representation are as above.
1554 Important: When format is 32, data should contain an array of int,
1555 not an array of long as the X library returns. This makes a difference
1556 when sizeof(long) != sizeof(int). */
1560 static Lisp_Object
1561 selection_data_to_lisp_data (struct x_display_info *dpyinfo,
1562 const unsigned char *data,
1563 ptrdiff_t size, Atom type, int format)
1565 if (type == dpyinfo->Xatom_NULL)
1566 return QNULL;
1568 /* Convert any 8-bit data to a string, for compactness. */
1569 else if (format == 8)
1571 Lisp_Object str, lispy_type;
1573 str = make_unibyte_string ((char *) data, size);
1574 /* Indicate that this string is from foreign selection by a text
1575 property `foreign-selection' so that the caller of
1576 x-get-selection-internal (usually x-get-selection) can know
1577 that the string must be decode. */
1578 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1579 lispy_type = QCOMPOUND_TEXT;
1580 else if (type == dpyinfo->Xatom_UTF8_STRING)
1581 lispy_type = QUTF8_STRING;
1582 else
1583 lispy_type = QSTRING;
1584 Fput_text_property (make_number (0), make_number (size),
1585 Qforeign_selection, lispy_type, str);
1586 return str;
1588 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1589 a vector of symbols. */
1590 else if (type == XA_ATOM
1591 /* Treat ATOM_PAIR type similar to list of atoms. */
1592 || type == dpyinfo->Xatom_ATOM_PAIR)
1594 ptrdiff_t i;
1595 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1596 But the callers of these function has made sure the data for
1597 format == 32 is an array of int. Thus, use int instead
1598 of Atom. */
1599 int *idata = (int *) data;
1601 if (size == sizeof (int))
1602 return x_atom_to_symbol (dpyinfo, (Atom) idata[0]);
1603 else
1605 Lisp_Object v = make_uninit_vector (size / sizeof (int));
1607 for (i = 0; i < size / sizeof (int); i++)
1608 ASET (v, i, x_atom_to_symbol (dpyinfo, (Atom) idata[i]));
1609 return v;
1613 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1614 If the number is 32 bits and won't fit in a Lisp_Int,
1615 convert it to a cons of integers, 16 bits in each half.
1617 INTEGER is a signed type, CARDINAL is unsigned.
1618 Assume any other types are unsigned as well.
1620 else if (format == 32 && size == sizeof (int))
1622 if (type == XA_INTEGER)
1623 return INTEGER_TO_CONS (((int *) data) [0]);
1624 else
1625 return INTEGER_TO_CONS (((unsigned int *) data) [0]);
1627 else if (format == 16 && size == sizeof (short))
1629 if (type == XA_INTEGER)
1630 return make_number (((short *) data) [0]);
1631 else
1632 return make_number (((unsigned short *) data) [0]);
1635 /* Convert any other kind of data to a vector of numbers, represented
1636 as above (as an integer, or a cons of two 16 bit integers.)
1638 else if (format == 16)
1640 ptrdiff_t i;
1641 Lisp_Object v = make_uninit_vector (size / 2);
1643 if (type == XA_INTEGER)
1645 for (i = 0; i < size / 2; i++)
1647 short j = ((short *) data) [i];
1648 ASET (v, i, make_number (j));
1651 else
1653 for (i = 0; i < size / 2; i++)
1655 unsigned short j = ((unsigned short *) data) [i];
1656 ASET (v, i, make_number (j));
1659 return v;
1661 else
1663 ptrdiff_t i;
1664 Lisp_Object v = make_uninit_vector (size / X_LONG_SIZE);
1666 if (type == XA_INTEGER)
1668 for (i = 0; i < size / X_LONG_SIZE; i++)
1670 int j = ((int *) data) [i];
1671 ASET (v, i, INTEGER_TO_CONS (j));
1674 else
1676 for (i = 0; i < size / X_LONG_SIZE; i++)
1678 unsigned int j = ((unsigned int *) data) [i];
1679 ASET (v, i, INTEGER_TO_CONS (j));
1682 return v;
1686 /* Convert OBJ to an X long value, and return it as unsigned long.
1687 OBJ should be an integer or a cons representing an integer.
1688 Treat values in the range X_LONG_MAX + 1 .. X_ULONG_MAX as X
1689 unsigned long values: in theory these values are supposed to be
1690 signed but in practice unsigned 32-bit data are communicated via X
1691 selections and we need to support that. */
1692 static unsigned long
1693 cons_to_x_long (Lisp_Object obj)
1695 if (X_ULONG_MAX <= INTMAX_MAX
1696 || XINT (INTEGERP (obj) ? obj : XCAR (obj)) < 0)
1697 return cons_to_signed (obj, X_LONG_MIN, min (X_ULONG_MAX, INTMAX_MAX));
1698 else
1699 return cons_to_unsigned (obj, X_ULONG_MAX);
1702 /* Use xfree, not XFree, to free the data obtained with this function. */
1704 static void
1705 lisp_data_to_selection_data (struct x_display_info *dpyinfo,
1706 Lisp_Object obj, struct selection_data *cs)
1708 Lisp_Object type = Qnil;
1710 eassert (cs != NULL);
1711 cs->nofree = false;
1713 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1715 type = XCAR (obj);
1716 obj = XCDR (obj);
1717 if (CONSP (obj) && NILP (XCDR (obj)))
1718 obj = XCAR (obj);
1721 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1722 { /* This is not the same as declining */
1723 cs->format = 32;
1724 cs->size = 0;
1725 cs->data = NULL;
1726 type = QNULL;
1728 else if (STRINGP (obj))
1730 if (SCHARS (obj) < SBYTES (obj))
1731 /* OBJ is a multibyte string containing a non-ASCII char. */
1732 signal_error ("Non-ASCII string must be encoded in advance", obj);
1733 if (NILP (type))
1734 type = QSTRING;
1735 cs->format = 8;
1736 cs->size = SBYTES (obj);
1737 cs->data = SDATA (obj);
1738 cs->nofree = true;
1740 else if (SYMBOLP (obj))
1742 void *data = xmalloc (sizeof (Atom) + 1);
1743 Atom *x_atom_ptr = data;
1744 cs->data = data;
1745 cs->format = 32;
1746 cs->size = 1;
1747 cs->data[sizeof (Atom)] = 0;
1748 *x_atom_ptr = symbol_to_x_atom (dpyinfo, obj);
1749 if (NILP (type)) type = QATOM;
1751 else if (RANGED_INTEGERP (X_SHRT_MIN, obj, X_SHRT_MAX))
1753 void *data = xmalloc (sizeof (short) + 1);
1754 short *short_ptr = data;
1755 cs->data = data;
1756 cs->format = 16;
1757 cs->size = 1;
1758 cs->data[sizeof (short)] = 0;
1759 *short_ptr = XINT (obj);
1760 if (NILP (type)) type = QINTEGER;
1762 else if (INTEGERP (obj)
1763 || (CONSP (obj) && INTEGERP (XCAR (obj))
1764 && (INTEGERP (XCDR (obj))
1765 || (CONSP (XCDR (obj))
1766 && INTEGERP (XCAR (XCDR (obj)))))))
1768 void *data = xmalloc (sizeof (unsigned long) + 1);
1769 unsigned long *x_long_ptr = data;
1770 cs->data = data;
1771 cs->format = 32;
1772 cs->size = 1;
1773 cs->data[sizeof (unsigned long)] = 0;
1774 *x_long_ptr = cons_to_x_long (obj);
1775 if (NILP (type)) type = QINTEGER;
1777 else if (VECTORP (obj))
1779 /* Lisp_Vectors may represent a set of ATOMs;
1780 a set of 16 or 32 bit INTEGERs;
1781 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1783 ptrdiff_t i;
1784 ptrdiff_t size = ASIZE (obj);
1786 if (SYMBOLP (AREF (obj, 0)))
1787 /* This vector is an ATOM set */
1789 void *data;
1790 Atom *x_atoms;
1791 if (NILP (type)) type = QATOM;
1792 for (i = 0; i < size; i++)
1793 if (!SYMBOLP (AREF (obj, i)))
1794 signal_error ("All elements of selection vector must have same type", obj);
1796 cs->data = data = xnmalloc (size, sizeof *x_atoms);
1797 x_atoms = data;
1798 cs->format = 32;
1799 cs->size = size;
1800 for (i = 0; i < size; i++)
1801 x_atoms[i] = symbol_to_x_atom (dpyinfo, AREF (obj, i));
1803 else
1804 /* This vector is an INTEGER set, or something like it */
1806 int format = 16;
1807 int data_size = sizeof (short);
1808 void *data;
1809 unsigned long *x_atoms;
1810 short *shorts;
1811 if (NILP (type)) type = QINTEGER;
1812 for (i = 0; i < size; i++)
1814 if (! RANGED_INTEGERP (X_SHRT_MIN, AREF (obj, i),
1815 X_SHRT_MAX))
1817 /* Use sizeof (long) even if it is more than 32 bits.
1818 See comment in x_get_window_property and
1819 x_fill_property_data. */
1820 data_size = sizeof (long);
1821 format = 32;
1822 break;
1825 cs->data = data = xnmalloc (size, data_size);
1826 x_atoms = data;
1827 shorts = data;
1828 cs->format = format;
1829 cs->size = size;
1830 for (i = 0; i < size; i++)
1832 if (format == 32)
1833 x_atoms[i] = cons_to_x_long (AREF (obj, i));
1834 else
1835 shorts[i] = XINT (AREF (obj, i));
1839 else
1840 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1842 cs->type = symbol_to_x_atom (dpyinfo, type);
1845 static Lisp_Object
1846 clean_local_selection_data (Lisp_Object obj)
1848 if (CONSP (obj)
1849 && INTEGERP (XCAR (obj))
1850 && CONSP (XCDR (obj))
1851 && INTEGERP (XCAR (XCDR (obj)))
1852 && NILP (XCDR (XCDR (obj))))
1853 obj = Fcons (XCAR (obj), XCDR (obj));
1855 if (CONSP (obj)
1856 && INTEGERP (XCAR (obj))
1857 && INTEGERP (XCDR (obj)))
1859 if (XINT (XCAR (obj)) == 0)
1860 return XCDR (obj);
1861 if (XINT (XCAR (obj)) == -1)
1862 return make_number (- XINT (XCDR (obj)));
1864 if (VECTORP (obj))
1866 ptrdiff_t i;
1867 ptrdiff_t size = ASIZE (obj);
1868 Lisp_Object copy;
1869 if (size == 1)
1870 return clean_local_selection_data (AREF (obj, 0));
1871 copy = make_uninit_vector (size);
1872 for (i = 0; i < size; i++)
1873 ASET (copy, i, clean_local_selection_data (AREF (obj, i)));
1874 return copy;
1876 return obj;
1879 /* Called from XTread_socket to handle SelectionNotify events.
1880 If it's the selection we are waiting for, stop waiting
1881 by setting the car of reading_selection_reply to non-nil.
1882 We store t there if the reply is successful, lambda if not. */
1884 void
1885 x_handle_selection_notify (const XSelectionEvent *event)
1887 if (event->requestor != reading_selection_window)
1888 return;
1889 if (event->selection != reading_which_selection)
1890 return;
1892 TRACE0 ("Received SelectionNotify");
1893 XSETCAR (reading_selection_reply,
1894 (event->property != 0 ? Qt : Qlambda));
1898 /* From a Lisp_Object, return a suitable frame for selection
1899 operations. OBJECT may be a frame, a terminal object, or nil
1900 (which stands for the selected frame--or, if that is not an X
1901 frame, the first X display on the list). If no suitable frame can
1902 be found, return NULL. */
1904 static struct frame *
1905 frame_for_x_selection (Lisp_Object object)
1907 Lisp_Object tail, frame;
1908 struct frame *f;
1910 if (NILP (object))
1912 f = XFRAME (selected_frame);
1913 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1914 return f;
1916 FOR_EACH_FRAME (tail, frame)
1918 f = XFRAME (frame);
1919 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1920 return f;
1923 else if (TERMINALP (object))
1925 struct terminal *t = decode_live_terminal (object);
1927 if (t->type == output_x_window)
1928 FOR_EACH_FRAME (tail, frame)
1930 f = XFRAME (frame);
1931 if (FRAME_LIVE_P (f) && f->terminal == t)
1932 return f;
1935 else if (FRAMEP (object))
1937 f = XFRAME (object);
1938 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1939 return f;
1942 return NULL;
1946 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1947 Sx_own_selection_internal, 2, 3, 0,
1948 doc: /* Assert an X selection of type SELECTION and value VALUE.
1949 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1950 \(Those are literal upper-case symbol names, since that's what X expects.)
1951 VALUE is typically a string, or a cons of two markers, but may be
1952 anything that the functions on `selection-converter-alist' know about.
1954 FRAME should be a frame that should own the selection. If omitted or
1955 nil, it defaults to the selected frame.
1957 On Nextstep, FRAME is unused. */)
1958 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1960 if (NILP (frame)) frame = selected_frame;
1961 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1962 error ("X selection unavailable for this frame");
1964 CHECK_SYMBOL (selection);
1965 if (NILP (value)) error ("VALUE may not be nil");
1966 x_own_selection (selection, value, frame);
1967 return value;
1971 /* Request the selection value from the owner. If we are the owner,
1972 simply return our selection value. If we are not the owner, this
1973 will block until all of the data has arrived. */
1975 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1976 Sx_get_selection_internal, 2, 4, 0,
1977 doc: /* Return text selected from some X window.
1978 SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1979 \(Those are literal upper-case symbol names, since that's what X expects.)
1980 TARGET-TYPE is the type of data desired, typically `STRING'.
1982 TIME-STAMP is the time to use in the XConvertSelection call for foreign
1983 selections. If omitted, defaults to the time for the last event.
1985 TERMINAL should be a terminal object or a frame specifying the X
1986 server to query. If omitted or nil, that stands for the selected
1987 frame's display, or the first available X display.
1989 On Nextstep, TIME-STAMP and TERMINAL are unused. */)
1990 (Lisp_Object selection_symbol, Lisp_Object target_type,
1991 Lisp_Object time_stamp, Lisp_Object terminal)
1993 Lisp_Object val = Qnil;
1994 struct frame *f = frame_for_x_selection (terminal);
1996 CHECK_SYMBOL (selection_symbol);
1997 CHECK_SYMBOL (target_type);
1998 if (EQ (target_type, QMULTIPLE))
1999 error ("Retrieving MULTIPLE selections is currently unimplemented");
2000 if (!f)
2001 error ("X selection unavailable for this frame");
2003 val = x_get_local_selection (selection_symbol, target_type, true,
2004 FRAME_DISPLAY_INFO (f));
2006 if (NILP (val) && FRAME_LIVE_P (f))
2008 Lisp_Object frame;
2009 XSETFRAME (frame, f);
2010 return x_get_foreign_selection (selection_symbol, target_type,
2011 time_stamp, frame);
2014 if (CONSP (val) && SYMBOLP (XCAR (val)))
2016 val = XCDR (val);
2017 if (CONSP (val) && NILP (XCDR (val)))
2018 val = XCAR (val);
2020 return clean_local_selection_data (val);
2023 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2024 Sx_disown_selection_internal, 1, 3, 0,
2025 doc: /* If we own the selection SELECTION, disown it.
2026 Disowning it means there is no such selection.
2028 Sets the last-change time for the selection to TIME-OBJECT (by default
2029 the time of the last event).
2031 TERMINAL should be a terminal object or a frame specifying the X
2032 server to query. If omitted or nil, that stands for the selected
2033 frame's display, or the first available X display.
2035 On Nextstep, the TIME-OBJECT and TERMINAL arguments are unused.
2036 On MS-DOS, all this does is return non-nil if we own the selection. */)
2037 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
2039 Time timestamp;
2040 Atom selection_atom;
2041 struct selection_input_event event;
2042 struct frame *f = frame_for_x_selection (terminal);
2043 struct x_display_info *dpyinfo;
2045 if (!f)
2046 return Qnil;
2048 dpyinfo = FRAME_DISPLAY_INFO (f);
2049 CHECK_SYMBOL (selection);
2051 /* Don't disown the selection when we're not the owner. */
2052 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2053 return Qnil;
2055 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2057 block_input ();
2058 if (NILP (time_object))
2059 timestamp = dpyinfo->last_user_time;
2060 else
2061 CONS_TO_INTEGER (time_object, Time, timestamp);
2062 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2063 unblock_input ();
2065 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2066 generated for a window which owns the selection when that window sets
2067 the selection owner to None. The NCD server does, the MIT Sun4 server
2068 doesn't. So we synthesize one; this means we might get two, but
2069 that's ok, because the second one won't have any effect. */
2070 SELECTION_EVENT_DPYINFO (&event) = dpyinfo;
2071 SELECTION_EVENT_SELECTION (&event) = selection_atom;
2072 SELECTION_EVENT_TIME (&event) = timestamp;
2073 x_handle_selection_clear (&event);
2075 return Qt;
2078 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2079 0, 2, 0,
2080 doc: /* Whether the current Emacs process owns the given X Selection.
2081 The arg should be the name of the selection in question, typically one of
2082 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2083 \(Those are literal upper-case symbol names, since that's what X expects.)
2084 For convenience, the symbol nil is the same as `PRIMARY',
2085 and t is the same as `SECONDARY'.
2087 TERMINAL should be a terminal object or a frame specifying the X
2088 server to query. If omitted or nil, that stands for the selected
2089 frame's display, or the first available X display.
2091 On Nextstep, TERMINAL is unused. */)
2092 (Lisp_Object selection, Lisp_Object terminal)
2094 struct frame *f = frame_for_x_selection (terminal);
2096 CHECK_SYMBOL (selection);
2097 if (EQ (selection, Qnil)) selection = QPRIMARY;
2098 if (EQ (selection, Qt)) selection = QSECONDARY;
2100 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_DISPLAY_INFO (f))))
2101 return Qt;
2102 else
2103 return Qnil;
2106 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2107 0, 2, 0,
2108 doc: /* Whether there is an owner for the given X selection.
2109 SELECTION should be the name of the selection in question, typically
2110 one of the symbols `PRIMARY', `SECONDARY', `CLIPBOARD', or
2111 `CLIPBOARD_MANAGER' (X expects these literal upper-case names.) The
2112 symbol nil is the same as `PRIMARY', and t is the same as `SECONDARY'.
2114 TERMINAL should be a terminal object or a frame specifying the X
2115 server to query. If omitted or nil, that stands for the selected
2116 frame's display, or the first available X display.
2118 On Nextstep, TERMINAL is unused. */)
2119 (Lisp_Object selection, Lisp_Object terminal)
2121 Window owner;
2122 Atom atom;
2123 struct frame *f = frame_for_x_selection (terminal);
2124 struct x_display_info *dpyinfo;
2126 CHECK_SYMBOL (selection);
2127 if (EQ (selection, Qnil)) selection = QPRIMARY;
2128 if (EQ (selection, Qt)) selection = QSECONDARY;
2130 if (!f)
2131 return Qnil;
2133 dpyinfo = FRAME_DISPLAY_INFO (f);
2135 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2136 return Qt;
2138 atom = symbol_to_x_atom (dpyinfo, selection);
2139 if (atom == 0) return Qnil;
2140 block_input ();
2141 owner = XGetSelectionOwner (dpyinfo->display, atom);
2142 unblock_input ();
2143 return (owner ? Qt : Qnil);
2147 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2148 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2150 static Lisp_Object
2151 x_clipboard_manager_save (Lisp_Object frame)
2153 struct frame *f = XFRAME (frame);
2154 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2155 Atom data = dpyinfo->Xatom_UTF8_STRING;
2157 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2158 dpyinfo->Xatom_EMACS_TMP,
2159 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2160 (unsigned char *) &data, 1);
2161 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2162 Qnil, frame);
2163 return Qt;
2166 /* Error handler for x_clipboard_manager_save_frame. */
2168 static Lisp_Object
2169 x_clipboard_manager_error_1 (Lisp_Object err)
2171 AUTO_STRING (format, "X clipboard manager error: %s\n\
2172 If the problem persists, set `%s' to nil.");
2173 AUTO_STRING (varname, "x-select-enable-clipboard-manager");
2174 CALLN (Fmessage, format, CAR (CDR (err)), varname);
2175 return Qnil;
2178 /* Error handler for x_clipboard_manager_save_all. */
2180 static Lisp_Object
2181 x_clipboard_manager_error_2 (Lisp_Object err)
2183 fprintf (stderr, "Error saving to X clipboard manager.\n\
2184 If the problem persists, set '%s' \
2185 to nil.\n", "x-select-enable-clipboard-manager");
2186 return Qnil;
2189 /* Called from delete_frame: save any clipboard owned by FRAME to the
2190 clipboard manager. Do nothing if FRAME does not own the clipboard,
2191 or if no clipboard manager is present. */
2193 void
2194 x_clipboard_manager_save_frame (Lisp_Object frame)
2196 struct frame *f;
2198 if (!NILP (Vx_select_enable_clipboard_manager)
2199 && FRAMEP (frame)
2200 && (f = XFRAME (frame), FRAME_X_P (f))
2201 && FRAME_LIVE_P (f))
2203 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2204 Lisp_Object local_selection
2205 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2207 if (!NILP (local_selection)
2208 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2209 && XGetSelectionOwner (dpyinfo->display,
2210 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2211 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2212 x_clipboard_manager_error_1);
2216 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2217 clipboard manager. Do nothing if FRAME does not own the clipboard,
2218 or if no clipboard manager is present. */
2220 void
2221 x_clipboard_manager_save_all (void)
2223 /* Loop through all X displays, saving owned clipboards. */
2224 struct x_display_info *dpyinfo;
2225 Lisp_Object local_selection, local_frame;
2227 if (NILP (Vx_select_enable_clipboard_manager))
2228 return;
2230 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2232 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2233 if (NILP (local_selection)
2234 || !XGetSelectionOwner (dpyinfo->display,
2235 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2236 continue;
2238 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2239 if (FRAME_LIVE_P (XFRAME (local_frame)))
2241 message ("Saving clipboard to X clipboard manager...");
2242 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2243 Qt, x_clipboard_manager_error_2);
2249 /***********************************************************************
2250 Drag and drop support
2251 ***********************************************************************/
2252 /* Check that lisp values are of correct type for x_fill_property_data.
2253 That is, number, string or a cons with two numbers (low and high 16
2254 bit parts of a 32 bit number). Return the number of items in DATA,
2255 or -1 if there is an error. */
2258 x_check_property_data (Lisp_Object data)
2260 Lisp_Object iter;
2261 int size = 0;
2263 for (iter = data; CONSP (iter); iter = XCDR (iter))
2265 Lisp_Object o = XCAR (iter);
2267 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2268 return -1;
2269 else if (CONSP (o) &&
2270 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2271 return -1;
2272 if (size == INT_MAX)
2273 return -1;
2274 size++;
2277 return size;
2280 /* Convert lisp values to a C array. Values may be a number, a string
2281 which is taken as an X atom name and converted to the atom value, or
2282 a cons containing the two 16 bit parts of a 32 bit number.
2284 DPY is the display use to look up X atoms.
2285 DATA is a Lisp list of values to be converted.
2286 RET is the C array that contains the converted values. It is assumed
2287 it is big enough to hold all values.
2288 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2289 be stored in RET. Note that long is used for 32 even if long is more
2290 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2291 XClientMessageEvent). */
2293 void
2294 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2296 unsigned long val;
2297 unsigned long *d32 = (unsigned long *) ret;
2298 unsigned short *d16 = (unsigned short *) ret;
2299 unsigned char *d08 = (unsigned char *) ret;
2300 Lisp_Object iter;
2302 for (iter = data; CONSP (iter); iter = XCDR (iter))
2304 Lisp_Object o = XCAR (iter);
2306 if (NUMBERP (o) || CONSP (o))
2308 if (CONSP (o)
2309 && RANGED_INTEGERP (X_LONG_MIN >> 16, XCAR (o), X_LONG_MAX >> 16)
2310 && RANGED_INTEGERP (- (1 << 15), XCDR (o), -1))
2312 /* cons_to_x_long does not handle negative values for v2.
2313 For XDnd, v2 might be y of a window, and can be negative.
2314 The XDnd spec. is not explicit about negative values,
2315 but let's assume negative v2 is sent modulo 2**16. */
2316 unsigned long v1 = XINT (XCAR (o)) & 0xffff;
2317 unsigned long v2 = XINT (XCDR (o)) & 0xffff;
2318 val = (v1 << 16) | v2;
2320 else
2321 val = cons_to_x_long (o);
2323 else if (STRINGP (o))
2325 block_input ();
2326 val = XInternAtom (dpy, SSDATA (o), False);
2327 unblock_input ();
2329 else
2330 error ("Wrong type, must be string, number or cons");
2332 if (format == 8)
2334 if ((1 << 8) < val && val <= X_ULONG_MAX - (1 << 7))
2335 error ("Out of `char' range");
2336 *d08++ = val;
2338 else if (format == 16)
2340 if ((1 << 16) < val && val <= X_ULONG_MAX - (1 << 15))
2341 error ("Out of `short' range");
2342 *d16++ = val;
2344 else
2345 *d32++ = val;
2349 /* Convert an array of C values to a Lisp list.
2350 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2351 DATA is a C array of values to be converted.
2352 TYPE is the type of the data. Only XA_ATOM is special, it converts
2353 each number in DATA to its corresponding X atom as a symbol.
2354 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2355 be stored in RET.
2356 SIZE is the number of elements in DATA.
2358 Important: When format is 32, data should contain an array of int,
2359 not an array of long as the X library returns. This makes a difference
2360 when sizeof(long) != sizeof(int).
2362 Also see comment for selection_data_to_lisp_data above. */
2364 Lisp_Object
2365 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2366 Atom type, int format, unsigned long size)
2368 ptrdiff_t format_bytes = format >> 3;
2369 ptrdiff_t data_bytes;
2370 if (INT_MULTIPLY_WRAPV (size, format_bytes, &data_bytes))
2371 memory_full (SIZE_MAX);
2372 return selection_data_to_lisp_data (FRAME_DISPLAY_INFO (f), data,
2373 data_bytes, type, format);
2376 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2377 Sx_get_atom_name, 1, 2, 0,
2378 doc: /* Return the X atom name for VALUE as a string.
2379 VALUE may be a number or a cons where the car is the upper 16 bits and
2380 the cdr is the lower 16 bits of a 32 bit value.
2381 Use the display for FRAME or the current frame if FRAME is not given or nil.
2383 If the value is 0 or the atom is not known, return the empty string. */)
2384 (Lisp_Object value, Lisp_Object frame)
2386 struct frame *f = decode_window_system_frame (frame);
2387 char *name = 0;
2388 char empty[] = "";
2389 Lisp_Object ret = Qnil;
2390 Display *dpy = FRAME_X_DISPLAY (f);
2391 Atom atom;
2392 bool had_errors_p;
2394 CONS_TO_INTEGER (value, Atom, atom);
2396 block_input ();
2397 x_catch_errors (dpy);
2398 name = atom ? XGetAtomName (dpy, atom) : empty;
2399 had_errors_p = x_had_errors_p (dpy);
2400 x_uncatch_errors_after_check ();
2402 if (!had_errors_p)
2403 ret = build_string (name);
2405 if (atom && name) XFree (name);
2406 if (NILP (ret)) ret = empty_unibyte_string;
2408 unblock_input ();
2410 return ret;
2413 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2414 Sx_register_dnd_atom, 1, 2, 0,
2415 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2416 ATOM can be a symbol or a string. The ATOM is interned on the display that
2417 FRAME is on. If FRAME is nil, the selected frame is used. */)
2418 (Lisp_Object atom, Lisp_Object frame)
2420 Atom x_atom;
2421 struct frame *f = decode_window_system_frame (frame);
2422 ptrdiff_t i;
2423 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2426 if (SYMBOLP (atom))
2427 x_atom = symbol_to_x_atom (dpyinfo, atom);
2428 else if (STRINGP (atom))
2430 block_input ();
2431 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2432 unblock_input ();
2434 else
2435 error ("ATOM must be a symbol or a string");
2437 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2438 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2439 return Qnil;
2441 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2442 dpyinfo->x_dnd_atoms =
2443 xpalloc (dpyinfo->x_dnd_atoms, &dpyinfo->x_dnd_atoms_size,
2444 1, -1, sizeof *dpyinfo->x_dnd_atoms);
2446 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2447 return Qnil;
2450 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2452 bool
2453 x_handle_dnd_message (struct frame *f, const XClientMessageEvent *event,
2454 struct x_display_info *dpyinfo, struct input_event *bufp)
2456 Lisp_Object vec;
2457 Lisp_Object frame;
2458 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2459 unsigned long size = 160/event->format;
2460 int x, y;
2461 unsigned char *data = (unsigned char *) event->data.b;
2462 int idata[5];
2463 ptrdiff_t i;
2465 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2466 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2468 if (i == dpyinfo->x_dnd_atoms_length) return false;
2470 XSETFRAME (frame, f);
2472 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2473 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2474 function expects them to be of size int (i.e. 32). So to be able to
2475 use that function, put the data in the form it expects if format is 32. */
2477 if (LONG_WIDTH > 32 && event->format == 32)
2479 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2480 idata[i] = event->data.l[i];
2481 data = (unsigned char *) idata;
2484 vec = Fmake_vector (make_number (4), Qnil);
2485 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_DISPLAY_INFO (f),
2486 event->message_type)));
2487 ASET (vec, 1, frame);
2488 ASET (vec, 2, make_number (event->format));
2489 ASET (vec, 3, x_property_data_to_lisp (f,
2490 data,
2491 event->message_type,
2492 event->format,
2493 size));
2495 x_relative_mouse_position (f, &x, &y);
2496 bufp->kind = DRAG_N_DROP_EVENT;
2497 bufp->frame_or_window = frame;
2498 bufp->timestamp = CurrentTime;
2499 bufp->x = make_number (x);
2500 bufp->y = make_number (y);
2501 bufp->arg = vec;
2502 bufp->modifiers = 0;
2504 return true;
2507 DEFUN ("x-send-client-message", Fx_send_client_message,
2508 Sx_send_client_message, 6, 6, 0,
2509 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2511 For DISPLAY, specify either a frame or a display name (a string).
2512 If DISPLAY is nil, that stands for the selected frame's display.
2513 DEST may be a number, in which case it is a Window id. The value 0 may
2514 be used to send to the root window of the DISPLAY.
2515 If DEST is a cons, it is converted to a 32 bit number
2516 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2517 number is then used as a window id.
2518 If DEST is a frame the event is sent to the outer window of that frame.
2519 A value of nil means the currently selected frame.
2520 If DEST is the string "PointerWindow" the event is sent to the window that
2521 contains the pointer. If DEST is the string "InputFocus" the event is
2522 sent to the window that has the input focus.
2523 FROM is the frame sending the event. Use nil for currently selected frame.
2524 MESSAGE-TYPE is the name of an Atom as a string.
2525 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2526 bits. VALUES is a list of numbers, cons and/or strings containing the values
2527 to send. If a value is a string, it is converted to an Atom and the value of
2528 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2529 with the high 16 bits from the car and the lower 16 bit from the cdr.
2530 If more values than fits into the event is given, the excessive values
2531 are ignored. */)
2532 (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2533 Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2535 struct x_display_info *dpyinfo = check_x_display_info (display);
2537 CHECK_STRING (message_type);
2538 x_send_client_event (display, dest, from,
2539 XInternAtom (dpyinfo->display,
2540 SSDATA (message_type),
2541 False),
2542 format, values);
2544 return Qnil;
2547 void
2548 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from,
2549 Atom message_type, Lisp_Object format, Lisp_Object values)
2551 struct x_display_info *dpyinfo = check_x_display_info (display);
2552 Window wdest;
2553 XEvent event;
2554 struct frame *f = decode_window_system_frame (from);
2555 bool to_root;
2557 CHECK_NUMBER (format);
2558 CHECK_CONS (values);
2560 if (x_check_property_data (values) == -1)
2561 error ("Bad data in VALUES, must be number, cons or string");
2563 if (XINT (format) != 8 && XINT (format) != 16 && XINT (format) != 32)
2564 error ("FORMAT must be one of 8, 16 or 32");
2566 event.xclient.type = ClientMessage;
2567 event.xclient.format = XINT (format);
2569 if (FRAMEP (dest) || NILP (dest))
2571 struct frame *fdest = decode_window_system_frame (dest);
2572 wdest = FRAME_OUTER_WINDOW (fdest);
2574 else if (STRINGP (dest))
2576 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2577 wdest = PointerWindow;
2578 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2579 wdest = InputFocus;
2580 else
2581 error ("DEST as a string must be one of PointerWindow or InputFocus");
2583 else if (NUMBERP (dest) || CONSP (dest))
2584 CONS_TO_INTEGER (dest, Window, wdest);
2585 else
2586 error ("DEST must be a frame, nil, string, number or cons");
2588 if (wdest == 0) wdest = dpyinfo->root_window;
2589 to_root = wdest == dpyinfo->root_window;
2591 block_input ();
2593 event.xclient.send_event = True;
2594 event.xclient.serial = 0;
2595 event.xclient.message_type = message_type;
2596 event.xclient.display = dpyinfo->display;
2598 /* Some clients (metacity for example) expects sending window to be here
2599 when sending to the root window. */
2600 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2602 memset (event.xclient.data.l, 0, sizeof (event.xclient.data.l));
2603 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2604 event.xclient.format);
2606 /* If event mask is 0 the event is sent to the client that created
2607 the destination window. But if we are sending to the root window,
2608 there is no such client. Then we set the event mask to 0xffffff. The
2609 event then goes to clients selecting for events on the root window. */
2610 x_catch_errors (dpyinfo->display);
2612 bool propagate = !to_root;
2613 long mask = to_root ? 0xffffff : 0;
2615 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2616 XFlush (dpyinfo->display);
2618 x_uncatch_errors ();
2619 unblock_input ();
2623 void
2624 syms_of_xselect (void)
2626 defsubr (&Sx_get_selection_internal);
2627 defsubr (&Sx_own_selection_internal);
2628 defsubr (&Sx_disown_selection_internal);
2629 defsubr (&Sx_selection_owner_p);
2630 defsubr (&Sx_selection_exists_p);
2632 defsubr (&Sx_get_atom_name);
2633 defsubr (&Sx_send_client_message);
2634 defsubr (&Sx_register_dnd_atom);
2636 reading_selection_reply = Fcons (Qnil, Qnil);
2637 staticpro (&reading_selection_reply);
2638 reading_selection_window = 0;
2639 reading_which_selection = 0;
2641 property_change_wait_list = 0;
2642 prop_location_identifier = 0;
2643 property_change_reply = Fcons (Qnil, Qnil);
2644 staticpro (&property_change_reply);
2646 converted_selections = NULL;
2647 conversion_fail_tag = None;
2649 /* FIXME: Duplicate definition in nsselect.c. */
2650 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2651 doc: /* An alist associating X Windows selection-types with functions.
2652 These functions are called to convert the selection, with three args:
2653 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2654 a desired type to which the selection should be converted;
2655 and the local selection value (whatever was given to
2656 `x-own-selection-internal').
2658 The function should return the value to send to the X server
2659 \(typically a string). A return value of nil
2660 means that the conversion could not be done.
2661 A return value which is the symbol `NULL'
2662 means that a side-effect was executed,
2663 and there is no meaningful selection value. */);
2664 Vselection_converter_alist = Qnil;
2666 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2667 doc: /* A list of functions to be called when Emacs loses an X selection.
2668 \(This happens when some other X client makes its own selection
2669 or when a Lisp program explicitly clears the selection.)
2670 The functions are called with one argument, the selection type
2671 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2672 Vx_lost_selection_functions = Qnil;
2674 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2675 doc: /* A list of functions to be called when Emacs answers a selection request.
2676 The functions are called with three arguments:
2677 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2678 - the selection-type which Emacs was asked to convert the
2679 selection into before sending (for example, `STRING' or `LENGTH');
2680 - a flag indicating success or failure for responding to the request.
2681 We might have failed (and declined the request) for any number of reasons,
2682 including being asked for a selection that we no longer own, or being asked
2683 to convert into a type that we don't know about or that is inappropriate.
2684 This hook doesn't let you change the behavior of Emacs's selection replies,
2685 it merely informs you that they have happened. */);
2686 Vx_sent_selection_functions = Qnil;
2688 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2689 Vx_select_enable_clipboard_manager,
2690 doc: /* Whether to enable X clipboard manager support.
2691 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2692 while owning the X clipboard, the clipboard contents are saved to the
2693 clipboard manager if one is present. */);
2694 Vx_select_enable_clipboard_manager = Qt;
2696 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2697 doc: /* Number of milliseconds to wait for a selection reply.
2698 If the selection owner doesn't reply in this time, we give up.
2699 A value of 0 means wait as long as necessary. This is initialized from the
2700 \"*selectionTimeout\" resource. */);
2701 x_selection_timeout = 0;
2703 /* QPRIMARY is defined in keyboard.c. */
2704 DEFSYM (QSECONDARY, "SECONDARY");
2705 DEFSYM (QSTRING, "STRING");
2706 DEFSYM (QINTEGER, "INTEGER");
2707 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2708 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2709 DEFSYM (QTEXT, "TEXT");
2711 /* These are types of selection. */
2712 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2713 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2715 DEFSYM (QDELETE, "DELETE");
2716 DEFSYM (QMULTIPLE, "MULTIPLE");
2717 DEFSYM (QINCR, "INCR");
2718 DEFSYM (Q_EMACS_TMP_, "_EMACS_TMP_");
2719 DEFSYM (QTARGETS, "TARGETS");
2720 DEFSYM (QATOM, "ATOM");
2721 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2722 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2723 DEFSYM (QNULL, "NULL");
2724 DEFSYM (Qforeign_selection, "foreign-selection");
2725 DEFSYM (Qx_lost_selection_functions, "x-lost-selection-functions");
2726 DEFSYM (Qx_sent_selection_functions, "x-sent-selection-functions");