upstream
[emacs.git] / src / xselect.c
blobf63977a73debc92b8537fe164d3e7a8dec3f3558
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Rewritten by jwz */
22 #include <config.h>
23 #include <limits.h>
24 #include <stdio.h> /* termhooks.h needs this */
25 #include <setjmp.h>
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
31 #include <unistd.h>
33 #include "lisp.h"
34 #include "xterm.h" /* for all of the X includes */
35 #include "dispextern.h" /* frame.h seems to want this */
36 #include "frame.h" /* Need this to get the X window of selected_frame */
37 #include "blockinput.h"
38 #include "buffer.h"
39 #include "process.h"
40 #include "termhooks.h"
41 #include "keyboard.h"
42 #include "character.h"
44 #include <X11/Xproto.h>
46 struct prop_location;
47 struct selection_data;
49 static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom);
50 static Atom symbol_to_x_atom (struct x_display_info *, Lisp_Object);
51 static void x_own_selection (Lisp_Object, Lisp_Object, Lisp_Object);
52 static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int,
53 struct x_display_info *);
54 static void x_decline_selection_request (struct input_event *);
55 static Lisp_Object x_selection_request_lisp_error (Lisp_Object);
56 static Lisp_Object queue_selection_requests_unwind (Lisp_Object);
57 static Lisp_Object x_catch_errors_unwind (Lisp_Object);
58 static void x_reply_selection_request (struct input_event *, struct x_display_info *);
59 static int x_convert_selection (struct input_event *, Lisp_Object, Lisp_Object,
60 Atom, int, struct x_display_info *);
61 static int waiting_for_other_props_on_window (Display *, Window);
62 static struct prop_location *expect_property_change (Display *, Window,
63 Atom, int);
64 static void unexpect_property_change (struct prop_location *);
65 static Lisp_Object wait_for_property_change_unwind (Lisp_Object);
66 static void wait_for_property_change (struct prop_location *);
67 static Lisp_Object x_get_foreign_selection (Lisp_Object, Lisp_Object,
68 Lisp_Object, Lisp_Object);
69 static void x_get_window_property (Display *, Window, Atom,
70 unsigned char **, int *,
71 Atom *, int *, unsigned long *, int);
72 static void receive_incremental_selection (Display *, Window, Atom,
73 Lisp_Object, unsigned,
74 unsigned char **, int *,
75 Atom *, int *, unsigned long *);
76 static Lisp_Object x_get_window_property_as_lisp_data (Display *,
77 Window, Atom,
78 Lisp_Object, Atom);
79 static Lisp_Object selection_data_to_lisp_data (Display *,
80 const unsigned char *,
81 int, Atom, int);
82 static void lisp_data_to_selection_data (Display *, Lisp_Object,
83 unsigned char **, Atom *,
84 unsigned *, int *, int *);
85 static Lisp_Object clean_local_selection_data (Lisp_Object);
87 /* Printing traces to stderr. */
89 #ifdef TRACE_SELECTION
90 #define TRACE0(fmt) \
91 fprintf (stderr, "%d: " fmt "\n", getpid ())
92 #define TRACE1(fmt, a0) \
93 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
94 #define TRACE2(fmt, a0, a1) \
95 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
96 #define TRACE3(fmt, a0, a1, a2) \
97 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
98 #else
99 #define TRACE0(fmt) (void) 0
100 #define TRACE1(fmt, a0) (void) 0
101 #define TRACE2(fmt, a0, a1) (void) 0
102 #endif
105 static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
106 QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
107 QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS;
109 static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */
110 static Lisp_Object QUTF8_STRING; /* This is a type of selection. */
112 static Lisp_Object Qcompound_text_with_extensions;
114 static Lisp_Object Qforeign_selection;
115 static Lisp_Object Qx_lost_selection_functions, Qx_sent_selection_functions;
117 /* If this is a smaller number than the max-request-size of the display,
118 emacs will use INCR selection transfer when the selection is larger
119 than this. The max-request-size is usually around 64k, so if you want
120 emacs to use incremental selection transfers when the selection is
121 smaller than that, set this. I added this mostly for debugging the
122 incremental transfer stuff, but it might improve server performance. */
123 #define MAX_SELECTION_QUANTUM 0xFFFFFF
125 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
127 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
128 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
131 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
132 handling. */
134 struct selection_event_queue
136 struct input_event event;
137 struct selection_event_queue *next;
140 static struct selection_event_queue *selection_queue;
142 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
144 static int x_queue_selection_requests;
146 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
148 static void
149 x_queue_event (struct input_event *event)
151 struct selection_event_queue *queue_tmp;
153 /* Don't queue repeated requests.
154 This only happens for large requests which uses the incremental protocol. */
155 for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
157 if (!memcmp (&queue_tmp->event, event, sizeof (*event)))
159 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp);
160 x_decline_selection_request (event);
161 return;
165 queue_tmp
166 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
168 if (queue_tmp != NULL)
170 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp);
171 queue_tmp->event = *event;
172 queue_tmp->next = selection_queue;
173 selection_queue = queue_tmp;
177 /* Start queuing SELECTION_REQUEST_EVENT events. */
179 static void
180 x_start_queuing_selection_requests (void)
182 if (x_queue_selection_requests)
183 abort ();
185 x_queue_selection_requests++;
186 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
189 /* Stop queuing SELECTION_REQUEST_EVENT events. */
191 static void
192 x_stop_queuing_selection_requests (void)
194 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
195 --x_queue_selection_requests;
197 /* Take all the queued events and put them back
198 so that they get processed afresh. */
200 while (selection_queue != NULL)
202 struct selection_event_queue *queue_tmp = selection_queue;
203 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp);
204 kbd_buffer_unget_event (&queue_tmp->event);
205 selection_queue = queue_tmp->next;
206 xfree ((char *)queue_tmp);
211 /* This converts a Lisp symbol to a server Atom, avoiding a server
212 roundtrip whenever possible. */
214 static Atom
215 symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym)
217 Atom val;
218 if (NILP (sym)) return 0;
219 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
220 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
221 if (EQ (sym, QSTRING)) return XA_STRING;
222 if (EQ (sym, QINTEGER)) return XA_INTEGER;
223 if (EQ (sym, QATOM)) return XA_ATOM;
224 if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
225 if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
226 if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT;
227 if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
228 if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
229 if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE;
230 if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE;
231 if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR;
232 if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
233 if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS;
234 if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL;
235 if (!SYMBOLP (sym)) abort ();
237 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym)));
238 BLOCK_INPUT;
239 val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False);
240 UNBLOCK_INPUT;
241 return val;
245 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
246 and calls to intern whenever possible. */
248 static Lisp_Object
249 x_atom_to_symbol (Display *dpy, Atom atom)
251 struct x_display_info *dpyinfo;
252 char *str;
253 Lisp_Object val;
255 if (! atom)
256 return Qnil;
258 switch (atom)
260 case XA_PRIMARY:
261 return QPRIMARY;
262 case XA_SECONDARY:
263 return QSECONDARY;
264 case XA_STRING:
265 return QSTRING;
266 case XA_INTEGER:
267 return QINTEGER;
268 case XA_ATOM:
269 return QATOM;
272 dpyinfo = x_display_info_for_display (dpy);
273 if (dpyinfo == NULL)
274 return Qnil;
275 if (atom == dpyinfo->Xatom_CLIPBOARD)
276 return QCLIPBOARD;
277 if (atom == dpyinfo->Xatom_TIMESTAMP)
278 return QTIMESTAMP;
279 if (atom == dpyinfo->Xatom_TEXT)
280 return QTEXT;
281 if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
282 return QCOMPOUND_TEXT;
283 if (atom == dpyinfo->Xatom_UTF8_STRING)
284 return QUTF8_STRING;
285 if (atom == dpyinfo->Xatom_DELETE)
286 return QDELETE;
287 if (atom == dpyinfo->Xatom_MULTIPLE)
288 return QMULTIPLE;
289 if (atom == dpyinfo->Xatom_INCR)
290 return QINCR;
291 if (atom == dpyinfo->Xatom_EMACS_TMP)
292 return QEMACS_TMP;
293 if (atom == dpyinfo->Xatom_TARGETS)
294 return QTARGETS;
295 if (atom == dpyinfo->Xatom_NULL)
296 return QNULL;
298 BLOCK_INPUT;
299 str = XGetAtomName (dpy, atom);
300 UNBLOCK_INPUT;
301 TRACE1 ("XGetAtomName --> %s", str);
302 if (! str) return Qnil;
303 val = intern (str);
304 BLOCK_INPUT;
305 /* This was allocated by Xlib, so use XFree. */
306 XFree (str);
307 UNBLOCK_INPUT;
308 return val;
311 /* Do protocol to assert ourself as a selection owner.
312 FRAME shall be the owner; it must be a valid X frame.
313 Update the Vselection_alist so that we can reply to later requests for
314 our selection. */
316 static void
317 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
318 Lisp_Object frame)
320 struct frame *f = XFRAME (frame);
321 Window selecting_window = FRAME_X_WINDOW (f);
322 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
323 Display *display = dpyinfo->display;
324 Time timestamp = last_event_timestamp;
325 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name);
327 BLOCK_INPUT;
328 x_catch_errors (display);
329 XSetSelectionOwner (display, selection_atom, selecting_window, timestamp);
330 x_check_errors (display, "Can't set selection: %s");
331 x_uncatch_errors ();
332 UNBLOCK_INPUT;
334 /* Now update the local cache */
336 Lisp_Object selection_data;
337 Lisp_Object prev_value;
339 selection_data = list4 (selection_name, selection_value,
340 INTEGER_TO_CONS (timestamp), frame);
341 prev_value = LOCAL_SELECTION (selection_name, dpyinfo);
343 dpyinfo->terminal->Vselection_alist
344 = Fcons (selection_data, dpyinfo->terminal->Vselection_alist);
346 /* If we already owned the selection, remove the old selection
347 data. Don't use Fdelq as that may QUIT. */
348 if (!NILP (prev_value))
350 /* We know it's not the CAR, so it's easy. */
351 Lisp_Object rest = dpyinfo->terminal->Vselection_alist;
352 for (; CONSP (rest); rest = XCDR (rest))
353 if (EQ (prev_value, Fcar (XCDR (rest))))
355 XSETCDR (rest, XCDR (XCDR (rest)));
356 break;
362 /* Given a selection-name and desired type, look up our local copy of
363 the selection value and convert it to the type.
364 The value is nil or a string.
365 This function is used both for remote requests (LOCAL_REQUEST is zero)
366 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
368 This calls random Lisp code, and may signal or gc. */
370 static Lisp_Object
371 x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
372 int local_request, struct x_display_info *dpyinfo)
374 Lisp_Object local_value;
375 Lisp_Object handler_fn, value, check;
376 int count;
378 local_value = LOCAL_SELECTION (selection_symbol, dpyinfo);
380 if (NILP (local_value)) return Qnil;
382 /* TIMESTAMP is a special case. */
383 if (EQ (target_type, QTIMESTAMP))
385 handler_fn = Qnil;
386 value = XCAR (XCDR (XCDR (local_value)));
388 else
390 /* Don't allow a quit within the converter.
391 When the user types C-g, he would be surprised
392 if by luck it came during a converter. */
393 count = SPECPDL_INDEX ();
394 specbind (Qinhibit_quit, Qt);
396 CHECK_SYMBOL (target_type);
397 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
398 /* gcpro is not needed here since nothing but HANDLER_FN
399 is live, and that ought to be a symbol. */
401 if (!NILP (handler_fn))
402 value = call3 (handler_fn,
403 selection_symbol, (local_request ? Qnil : target_type),
404 XCAR (XCDR (local_value)));
405 else
406 value = Qnil;
407 unbind_to (count, Qnil);
410 /* Make sure this value is of a type that we could transmit
411 to another X client. */
413 check = value;
414 if (CONSP (value)
415 && SYMBOLP (XCAR (value)))
416 check = XCDR (value);
418 if (STRINGP (check)
419 || VECTORP (check)
420 || SYMBOLP (check)
421 || INTEGERP (check)
422 || NILP (value))
423 return value;
424 /* Check for a value that CONS_TO_INTEGER could handle. */
425 else if (CONSP (check)
426 && INTEGERP (XCAR (check))
427 && (INTEGERP (XCDR (check))
429 (CONSP (XCDR (check))
430 && INTEGERP (XCAR (XCDR (check)))
431 && NILP (XCDR (XCDR (check))))))
432 return value;
434 signal_error ("Invalid data returned by selection-conversion function",
435 list2 (handler_fn, value));
438 /* Subroutines of x_reply_selection_request. */
440 /* Send a SelectionNotify event to the requestor with property=None,
441 meaning we were unable to do what they wanted. */
443 static void
444 x_decline_selection_request (struct input_event *event)
446 XEvent reply_base;
447 XSelectionEvent *reply = &(reply_base.xselection);
449 reply->type = SelectionNotify;
450 reply->display = SELECTION_EVENT_DISPLAY (event);
451 reply->requestor = SELECTION_EVENT_REQUESTOR (event);
452 reply->selection = SELECTION_EVENT_SELECTION (event);
453 reply->time = SELECTION_EVENT_TIME (event);
454 reply->target = SELECTION_EVENT_TARGET (event);
455 reply->property = None;
457 /* The reason for the error may be that the receiver has
458 died in the meantime. Handle that case. */
459 BLOCK_INPUT;
460 x_catch_errors (reply->display);
461 XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base);
462 XFlush (reply->display);
463 x_uncatch_errors ();
464 UNBLOCK_INPUT;
467 /* This is the selection request currently being processed.
468 It is set to zero when the request is fully processed. */
469 static struct input_event *x_selection_current_request;
471 /* Display info in x_selection_request. */
473 static struct x_display_info *selection_request_dpyinfo;
475 /* Raw selection data, for sending to a requestor window. */
477 struct selection_data
479 unsigned char *data;
480 unsigned int size;
481 int format;
482 Atom type;
483 int nofree;
484 Atom property;
485 /* This can be set to non-NULL during x_reply_selection_request, if
486 the selection is waiting for an INCR transfer to complete. Don't
487 free these; that's done by unexpect_property_change. */
488 struct prop_location *wait_object;
489 struct selection_data *next;
492 /* Linked list of the above (in support of MULTIPLE targets). */
494 static struct selection_data *converted_selections;
496 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
497 static Atom conversion_fail_tag;
499 /* Used as an unwind-protect clause so that, if a selection-converter signals
500 an error, we tell the requester that we were unable to do what they wanted
501 before we throw to top-level or go into the debugger or whatever. */
503 static Lisp_Object
504 x_selection_request_lisp_error (Lisp_Object ignore)
506 struct selection_data *cs, *next;
508 for (cs = converted_selections; cs; cs = next)
510 next = cs->next;
511 if (cs->nofree == 0 && cs->data)
512 xfree (cs->data);
513 xfree (cs);
515 converted_selections = NULL;
517 if (x_selection_current_request != 0
518 && selection_request_dpyinfo->display)
519 x_decline_selection_request (x_selection_current_request);
520 return Qnil;
523 static Lisp_Object
524 x_catch_errors_unwind (Lisp_Object dummy)
526 BLOCK_INPUT;
527 x_uncatch_errors ();
528 UNBLOCK_INPUT;
529 return Qnil;
533 /* This stuff is so that INCR selections are reentrant (that is, so we can
534 be servicing multiple INCR selection requests simultaneously.) I haven't
535 actually tested that yet. */
537 /* Keep a list of the property changes that are awaited. */
539 struct prop_location
541 int identifier;
542 Display *display;
543 Window window;
544 Atom property;
545 int desired_state;
546 int arrived;
547 struct prop_location *next;
550 static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state);
551 static void wait_for_property_change (struct prop_location *location);
552 static void unexpect_property_change (struct prop_location *location);
553 static int waiting_for_other_props_on_window (Display *display, Window window);
555 static int prop_location_identifier;
557 static Lisp_Object property_change_reply;
559 static struct prop_location *property_change_reply_object;
561 static struct prop_location *property_change_wait_list;
563 static Lisp_Object
564 queue_selection_requests_unwind (Lisp_Object tem)
566 x_stop_queuing_selection_requests ();
567 return Qnil;
571 /* Send the reply to a selection request event EVENT. */
573 #ifdef TRACE_SELECTION
574 static int x_reply_selection_request_cnt;
575 #endif /* TRACE_SELECTION */
577 static void
578 x_reply_selection_request (struct input_event *event, struct x_display_info *dpyinfo)
580 XEvent reply_base;
581 XSelectionEvent *reply = &(reply_base.xselection);
582 Display *display = SELECTION_EVENT_DISPLAY (event);
583 Window window = SELECTION_EVENT_REQUESTOR (event);
584 int bytes_remaining;
585 int max_bytes = SELECTION_QUANTUM (display);
586 int count = SPECPDL_INDEX ();
587 struct selection_data *cs;
589 if (max_bytes > MAX_SELECTION_QUANTUM)
590 max_bytes = MAX_SELECTION_QUANTUM;
592 reply->type = SelectionNotify;
593 reply->display = display;
594 reply->requestor = window;
595 reply->selection = SELECTION_EVENT_SELECTION (event);
596 reply->time = SELECTION_EVENT_TIME (event);
597 reply->target = SELECTION_EVENT_TARGET (event);
598 reply->property = SELECTION_EVENT_PROPERTY (event);
599 if (reply->property == None)
600 reply->property = reply->target;
602 BLOCK_INPUT;
603 /* The protected block contains wait_for_property_change, which can
604 run random lisp code (process handlers) or signal. Therefore, we
605 put the x_uncatch_errors call in an unwind. */
606 record_unwind_protect (x_catch_errors_unwind, Qnil);
607 x_catch_errors (display);
609 /* Loop over converted selections, storing them in the requested
610 properties. If data is large, only store the first N bytes
611 (section 2.7.2 of ICCCM). Note that we store the data for a
612 MULTIPLE request in the opposite order; the ICCM says only that
613 the conversion itself must be done in the same order. */
614 for (cs = converted_selections; cs; cs = cs->next)
616 if (cs->property == None)
617 continue;
619 bytes_remaining = cs->size * (cs->format / 8);
620 if (bytes_remaining <= max_bytes)
622 /* Send all the data at once, with minimal handshaking. */
623 TRACE1 ("Sending all %d bytes", bytes_remaining);
624 XChangeProperty (display, window, cs->property,
625 cs->type, cs->format, PropModeReplace,
626 cs->data, cs->size);
628 else
630 /* Send an INCR tag to initiate incremental transfer. */
631 long value[1];
633 TRACE2 ("Start sending %d bytes incrementally (%s)",
634 bytes_remaining, XGetAtomName (display, cs->property));
635 cs->wait_object
636 = expect_property_change (display, window, cs->property,
637 PropertyDelete);
639 /* XChangeProperty expects an array of long even if long is
640 more than 32 bits. */
641 value[0] = bytes_remaining;
642 XChangeProperty (display, window, cs->property,
643 dpyinfo->Xatom_INCR, 32, PropModeReplace,
644 (unsigned char *) value, 1);
645 XSelectInput (display, window, PropertyChangeMask);
649 /* Now issue the SelectionNotify event. */
650 XSendEvent (display, window, False, 0L, &reply_base);
651 XFlush (display);
653 #ifdef TRACE_SELECTION
655 char *sel = XGetAtomName (display, reply->selection);
656 char *tgt = XGetAtomName (display, reply->target);
657 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
658 sel, tgt, ++x_reply_selection_request_cnt);
659 if (sel) XFree (sel);
660 if (tgt) XFree (tgt);
662 #endif /* TRACE_SELECTION */
664 /* Finish sending the rest of each of the INCR values. This should
665 be improved; there's a chance of deadlock if more than one
666 subtarget in a MULTIPLE selection requires an INCR transfer, and
667 the requestor and Emacs loop waiting on different transfers. */
668 for (cs = converted_selections; cs; cs = cs->next)
669 if (cs->wait_object)
671 int format_bytes = cs->format / 8;
672 int had_errors = x_had_errors_p (display);
673 UNBLOCK_INPUT;
675 bytes_remaining = cs->size * format_bytes;
677 /* Wait for the requester to ack by deleting the property.
678 This can run Lisp code (process handlers) or signal. */
679 if (! had_errors)
681 TRACE1 ("Waiting for ACK (deletion of %s)",
682 XGetAtomName (display, cs->property));
683 wait_for_property_change (cs->wait_object);
685 else
686 unexpect_property_change (cs->wait_object);
688 while (bytes_remaining)
690 int i = ((bytes_remaining < max_bytes)
691 ? bytes_remaining
692 : max_bytes) / format_bytes;
693 BLOCK_INPUT;
695 cs->wait_object
696 = expect_property_change (display, window, cs->property,
697 PropertyDelete);
699 TRACE1 ("Sending increment of %d elements", i);
700 TRACE1 ("Set %s to increment data",
701 XGetAtomName (display, cs->property));
703 /* Append the next chunk of data to the property. */
704 XChangeProperty (display, window, cs->property,
705 cs->type, cs->format, PropModeAppend,
706 cs->data, i);
707 bytes_remaining -= i * format_bytes;
708 cs->data += i * ((cs->format == 32) ? sizeof (long)
709 : format_bytes);
710 XFlush (display);
711 had_errors = x_had_errors_p (display);
712 UNBLOCK_INPUT;
714 if (had_errors) break;
716 /* Wait for the requester to ack this chunk by deleting
717 the property. This can run Lisp code or signal. */
718 TRACE1 ("Waiting for increment ACK (deletion of %s)",
719 XGetAtomName (display, cs->property));
720 wait_for_property_change (cs->wait_object);
723 /* Now write a zero-length chunk to the property to tell the
724 requester that we're done. */
725 BLOCK_INPUT;
726 if (! waiting_for_other_props_on_window (display, window))
727 XSelectInput (display, window, 0L);
729 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
730 XGetAtomName (display, cs->property));
731 XChangeProperty (display, window, cs->property,
732 cs->type, cs->format, PropModeReplace,
733 cs->data, 0);
734 TRACE0 ("Done sending incrementally");
737 /* rms, 2003-01-03: I think I have fixed this bug. */
738 /* The window we're communicating with may have been deleted
739 in the meantime (that's a real situation from a bug report).
740 In this case, there may be events in the event queue still
741 refering to the deleted window, and we'll get a BadWindow error
742 in XTread_socket when processing the events. I don't have
743 an idea how to fix that. gerd, 2001-01-98. */
744 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
745 delivered before uncatch errors. */
746 XSync (display, False);
747 UNBLOCK_INPUT;
749 /* GTK queues events in addition to the queue in Xlib. So we
750 UNBLOCK to enter the event loop and get possible errors delivered,
751 and then BLOCK again because x_uncatch_errors requires it. */
752 BLOCK_INPUT;
753 /* This calls x_uncatch_errors. */
754 unbind_to (count, Qnil);
755 UNBLOCK_INPUT;
758 /* Handle a SelectionRequest event EVENT.
759 This is called from keyboard.c when such an event is found in the queue. */
761 static void
762 x_handle_selection_request (struct input_event *event)
764 struct gcpro gcpro1, gcpro2;
765 Time local_selection_time;
767 Display *display = SELECTION_EVENT_DISPLAY (event);
768 struct x_display_info *dpyinfo = x_display_info_for_display (display);
769 Atom selection = SELECTION_EVENT_SELECTION (event);
770 Lisp_Object selection_symbol = x_atom_to_symbol (display, selection);
771 Atom target = SELECTION_EVENT_TARGET (event);
772 Lisp_Object target_symbol = x_atom_to_symbol (display, target);
773 Atom property = SELECTION_EVENT_PROPERTY (event);
774 Lisp_Object local_selection_data;
775 int success = 0;
776 int count = SPECPDL_INDEX ();
777 GCPRO2 (local_selection_data, target_symbol);
779 if (!dpyinfo) goto DONE;
781 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
783 /* Decline if we don't own any selections. */
784 if (NILP (local_selection_data)) goto DONE;
786 /* Decline requests issued prior to our acquiring the selection. */
787 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
788 Time, local_selection_time);
789 if (SELECTION_EVENT_TIME (event) != CurrentTime
790 && local_selection_time > SELECTION_EVENT_TIME (event))
791 goto DONE;
793 x_selection_current_request = event;
794 selection_request_dpyinfo = dpyinfo;
795 record_unwind_protect (x_selection_request_lisp_error, Qnil);
797 /* We might be able to handle nested x_handle_selection_requests,
798 but this is difficult to test, and seems unimportant. */
799 x_start_queuing_selection_requests ();
800 record_unwind_protect (queue_selection_requests_unwind, Qnil);
802 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
803 SDATA (SYMBOL_NAME (selection_symbol)),
804 SDATA (SYMBOL_NAME (target_symbol)));
806 if (EQ (target_symbol, QMULTIPLE))
808 /* For MULTIPLE targets, the event property names a list of atom
809 pairs; the first atom names a target and the second names a
810 non-None property. */
811 Window requestor = SELECTION_EVENT_REQUESTOR (event);
812 Lisp_Object multprop;
813 int j, nselections;
815 if (property == None) goto DONE;
816 multprop
817 = x_get_window_property_as_lisp_data (display, requestor, property,
818 QMULTIPLE, selection);
820 if (!VECTORP (multprop) || ASIZE (multprop) % 2)
821 goto DONE;
823 nselections = ASIZE (multprop) / 2;
824 /* Perform conversions. This can signal. */
825 for (j = 0; j < nselections; j++)
827 Lisp_Object subtarget = AREF (multprop, 2*j);
828 Atom subproperty = symbol_to_x_atom (dpyinfo,
829 AREF (multprop, 2*j+1));
831 if (subproperty != None)
832 x_convert_selection (event, selection_symbol, subtarget,
833 subproperty, 1, dpyinfo);
835 success = 1;
837 else
839 if (property == None)
840 property = SELECTION_EVENT_TARGET (event);
841 success = x_convert_selection (event, selection_symbol,
842 target_symbol, property,
843 0, dpyinfo);
846 DONE:
848 if (success)
849 x_reply_selection_request (event, dpyinfo);
850 else
851 x_decline_selection_request (event);
852 x_selection_current_request = 0;
854 /* Run the `x-sent-selection-functions' abnormal hook. */
855 if (!NILP (Vx_sent_selection_functions)
856 && !EQ (Vx_sent_selection_functions, Qunbound))
858 Lisp_Object args[4];
859 args[0] = Qx_sent_selection_functions;
860 args[1] = selection_symbol;
861 args[2] = target_symbol;
862 args[3] = success ? Qt : Qnil;
863 Frun_hook_with_args (4, args);
866 unbind_to (count, Qnil);
867 UNGCPRO;
870 /* Perform the requested selection conversion, and write the data to
871 the converted_selections linked list, where it can be accessed by
872 x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out
873 the data even if conversion fails, using conversion_fail_tag.
875 Return 0 if the selection failed to convert, 1 otherwise. */
877 static int
878 x_convert_selection (struct input_event *event, Lisp_Object selection_symbol,
879 Lisp_Object target_symbol, Atom property,
880 int for_multiple, struct x_display_info *dpyinfo)
882 struct gcpro gcpro1;
883 Lisp_Object lisp_selection;
884 struct selection_data *cs;
885 GCPRO1 (lisp_selection);
887 lisp_selection
888 = x_get_local_selection (selection_symbol, target_symbol,
889 0, dpyinfo);
891 /* A nil return value means we can't perform the conversion. */
892 if (NILP (lisp_selection)
893 || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection))))
895 if (for_multiple)
897 cs = xmalloc (sizeof (struct selection_data));
898 cs->data = (unsigned char *) &conversion_fail_tag;
899 cs->size = 1;
900 cs->format = 32;
901 cs->type = XA_ATOM;
902 cs->nofree = 1;
903 cs->property = property;
904 cs->wait_object = NULL;
905 cs->next = converted_selections;
906 converted_selections = cs;
909 UNGCPRO;
910 return 0;
913 /* Otherwise, record the converted selection to binary. */
914 cs = xmalloc (sizeof (struct selection_data));
915 cs->nofree = 1;
916 cs->property = property;
917 cs->wait_object = NULL;
918 cs->next = converted_selections;
919 converted_selections = cs;
920 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
921 lisp_selection,
922 &(cs->data), &(cs->type),
923 &(cs->size), &(cs->format),
924 &(cs->nofree));
925 UNGCPRO;
926 return 1;
929 /* Handle a SelectionClear event EVENT, which indicates that some
930 client cleared out our previously asserted selection.
931 This is called from keyboard.c when such an event is found in the queue. */
933 static void
934 x_handle_selection_clear (struct input_event *event)
936 Display *display = SELECTION_EVENT_DISPLAY (event);
937 Atom selection = SELECTION_EVENT_SELECTION (event);
938 Time changed_owner_time = SELECTION_EVENT_TIME (event);
940 Lisp_Object selection_symbol, local_selection_data;
941 Time local_selection_time;
942 struct x_display_info *dpyinfo = x_display_info_for_display (display);
943 Lisp_Object Vselection_alist;
945 TRACE0 ("x_handle_selection_clear");
947 if (!dpyinfo) return;
949 selection_symbol = x_atom_to_symbol (display, selection);
950 local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo);
952 /* Well, we already believe that we don't own it, so that's just fine. */
953 if (NILP (local_selection_data)) return;
955 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))),
956 Time, local_selection_time);
958 /* We have reasserted the selection since this SelectionClear was
959 generated, so we can disregard it. */
960 if (changed_owner_time != CurrentTime
961 && local_selection_time > changed_owner_time)
962 return;
964 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
965 Vselection_alist = dpyinfo->terminal->Vselection_alist;
966 if (EQ (local_selection_data, CAR (Vselection_alist)))
967 Vselection_alist = XCDR (Vselection_alist);
968 else
970 Lisp_Object rest;
971 for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest))
972 if (EQ (local_selection_data, CAR (XCDR (rest))))
974 XSETCDR (rest, XCDR (XCDR (rest)));
975 break;
978 dpyinfo->terminal->Vselection_alist = Vselection_alist;
980 /* Run the `x-lost-selection-functions' abnormal hook. */
982 Lisp_Object args[2];
983 args[0] = Qx_lost_selection_functions;
984 args[1] = selection_symbol;
985 Frun_hook_with_args (2, args);
988 prepare_menu_bars ();
989 redisplay_preserve_echo_area (20);
992 void
993 x_handle_selection_event (struct input_event *event)
995 TRACE0 ("x_handle_selection_event");
996 if (event->kind != SELECTION_REQUEST_EVENT)
997 x_handle_selection_clear (event);
998 else if (x_queue_selection_requests)
999 x_queue_event (event);
1000 else
1001 x_handle_selection_request (event);
1005 /* Clear all selections that were made from frame F.
1006 We do this when about to delete a frame. */
1008 void
1009 x_clear_frame_selections (FRAME_PTR f)
1011 Lisp_Object frame;
1012 Lisp_Object rest;
1013 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1014 struct terminal *t = dpyinfo->terminal;
1016 XSETFRAME (frame, f);
1018 /* Delete elements from the beginning of Vselection_alist. */
1019 while (CONSP (t->Vselection_alist)
1020 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist)))))))
1022 /* Run the `x-lost-selection-functions' abnormal hook. */
1023 Lisp_Object args[2];
1024 args[0] = Qx_lost_selection_functions;
1025 args[1] = Fcar (Fcar (t->Vselection_alist));
1026 Frun_hook_with_args (2, args);
1028 t->Vselection_alist = XCDR (t->Vselection_alist);
1031 /* Delete elements after the beginning of Vselection_alist. */
1032 for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest))
1033 if (CONSP (XCDR (rest))
1034 && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest))))))))
1036 Lisp_Object args[2];
1037 args[0] = Qx_lost_selection_functions;
1038 args[1] = XCAR (XCAR (XCDR (rest)));
1039 Frun_hook_with_args (2, args);
1040 XSETCDR (rest, XCDR (XCDR (rest)));
1041 break;
1045 /* Nonzero if any properties for DISPLAY and WINDOW
1046 are on the list of what we are waiting for. */
1048 static int
1049 waiting_for_other_props_on_window (Display *display, Window window)
1051 struct prop_location *rest = property_change_wait_list;
1052 while (rest)
1053 if (rest->display == display && rest->window == window)
1054 return 1;
1055 else
1056 rest = rest->next;
1057 return 0;
1060 /* Add an entry to the list of property changes we are waiting for.
1061 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1062 The return value is a number that uniquely identifies
1063 this awaited property change. */
1065 static struct prop_location *
1066 expect_property_change (Display *display, Window window, Atom property, int state)
1068 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1069 pl->identifier = ++prop_location_identifier;
1070 pl->display = display;
1071 pl->window = window;
1072 pl->property = property;
1073 pl->desired_state = state;
1074 pl->next = property_change_wait_list;
1075 pl->arrived = 0;
1076 property_change_wait_list = pl;
1077 return pl;
1080 /* Delete an entry from the list of property changes we are waiting for.
1081 IDENTIFIER is the number that uniquely identifies the entry. */
1083 static void
1084 unexpect_property_change (struct prop_location *location)
1086 struct prop_location *prev = 0, *rest = property_change_wait_list;
1087 while (rest)
1089 if (rest == location)
1091 if (prev)
1092 prev->next = rest->next;
1093 else
1094 property_change_wait_list = rest->next;
1095 xfree (rest);
1096 return;
1098 prev = rest;
1099 rest = rest->next;
1103 /* Remove the property change expectation element for IDENTIFIER. */
1105 static Lisp_Object
1106 wait_for_property_change_unwind (Lisp_Object loc)
1108 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1110 unexpect_property_change (location);
1111 if (location == property_change_reply_object)
1112 property_change_reply_object = 0;
1113 return Qnil;
1116 /* Actually wait for a property change.
1117 IDENTIFIER should be the value that expect_property_change returned. */
1119 static void
1120 wait_for_property_change (struct prop_location *location)
1122 int secs, usecs;
1123 int count = SPECPDL_INDEX ();
1125 if (property_change_reply_object)
1126 abort ();
1128 /* Make sure to do unexpect_property_change if we quit or err. */
1129 record_unwind_protect (wait_for_property_change_unwind,
1130 make_save_value (location, 0));
1132 XSETCAR (property_change_reply, Qnil);
1133 property_change_reply_object = location;
1135 /* If the event we are waiting for arrives beyond here, it will set
1136 property_change_reply, because property_change_reply_object says so. */
1137 if (! location->arrived)
1139 secs = x_selection_timeout / 1000;
1140 usecs = (x_selection_timeout % 1000) * 1000;
1141 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1142 wait_reading_process_output (secs, usecs, 0, 0,
1143 property_change_reply, NULL, 0);
1145 if (NILP (XCAR (property_change_reply)))
1147 TRACE0 (" Timed out");
1148 error ("Timed out waiting for property-notify event");
1152 unbind_to (count, Qnil);
1155 /* Called from XTread_socket in response to a PropertyNotify event. */
1157 void
1158 x_handle_property_notify (XPropertyEvent *event)
1160 struct prop_location *rest;
1162 for (rest = property_change_wait_list; rest; rest = rest->next)
1164 if (!rest->arrived
1165 && rest->property == event->atom
1166 && rest->window == event->window
1167 && rest->display == event->display
1168 && rest->desired_state == event->state)
1170 TRACE2 ("Expected %s of property %s",
1171 (event->state == PropertyDelete ? "deletion" : "change"),
1172 XGetAtomName (event->display, event->atom));
1174 rest->arrived = 1;
1176 /* If this is the one wait_for_property_change is waiting for,
1177 tell it to wake up. */
1178 if (rest == property_change_reply_object)
1179 XSETCAR (property_change_reply, Qt);
1181 return;
1188 /* Variables for communication with x_handle_selection_notify. */
1189 static Atom reading_which_selection;
1190 static Lisp_Object reading_selection_reply;
1191 static Window reading_selection_window;
1193 /* Do protocol to read selection-data from the server.
1194 Converts this to Lisp data and returns it.
1195 FRAME is the frame whose X window shall request the selection. */
1197 static Lisp_Object
1198 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type,
1199 Lisp_Object time_stamp, Lisp_Object frame)
1201 struct frame *f = XFRAME (frame);
1202 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1203 Display *display = dpyinfo->display;
1204 Window requestor_window = FRAME_X_WINDOW (f);
1205 Time requestor_time = last_event_timestamp;
1206 Atom target_property = dpyinfo->Xatom_EMACS_TMP;
1207 Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol);
1208 Atom type_atom = (CONSP (target_type)
1209 ? symbol_to_x_atom (dpyinfo, XCAR (target_type))
1210 : symbol_to_x_atom (dpyinfo, target_type));
1211 int secs, usecs;
1213 if (!FRAME_LIVE_P (f))
1214 return Qnil;
1216 if (! NILP (time_stamp))
1217 CONS_TO_INTEGER (time_stamp, Time, requestor_time);
1219 BLOCK_INPUT;
1220 TRACE2 ("Get selection %s, type %s",
1221 XGetAtomName (display, type_atom),
1222 XGetAtomName (display, target_property));
1224 x_catch_errors (display);
1225 XConvertSelection (display, selection_atom, type_atom, target_property,
1226 requestor_window, requestor_time);
1227 x_check_errors (display, "Can't convert selection: %s");
1228 x_uncatch_errors ();
1230 /* Prepare to block until the reply has been read. */
1231 reading_selection_window = requestor_window;
1232 reading_which_selection = selection_atom;
1233 XSETCAR (reading_selection_reply, Qnil);
1235 /* It should not be necessary to stop handling selection requests
1236 during this time. In fact, the SAVE_TARGETS mechanism requires
1237 us to handle a clipboard manager's requests before it returns
1238 SelectionNotify. */
1239 #if 0
1240 x_start_queuing_selection_requests ();
1241 record_unwind_protect (queue_selection_requests_unwind, Qnil);
1242 #endif
1244 UNBLOCK_INPUT;
1246 /* This allows quits. Also, don't wait forever. */
1247 secs = x_selection_timeout / 1000;
1248 usecs = (x_selection_timeout % 1000) * 1000;
1249 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1250 wait_reading_process_output (secs, usecs, 0, 0,
1251 reading_selection_reply, NULL, 0);
1252 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1254 if (NILP (XCAR (reading_selection_reply)))
1255 error ("Timed out waiting for reply from selection owner");
1256 if (EQ (XCAR (reading_selection_reply), Qlambda))
1257 return Qnil;
1259 /* Otherwise, the selection is waiting for us on the requested property. */
1260 return
1261 x_get_window_property_as_lisp_data (display, requestor_window,
1262 target_property, target_type,
1263 selection_atom);
1266 /* Subroutines of x_get_window_property_as_lisp_data */
1268 /* Use xfree, not XFree, to free the data obtained with this function. */
1270 static void
1271 x_get_window_property (Display *display, Window window, Atom property,
1272 unsigned char **data_ret, int *bytes_ret,
1273 Atom *actual_type_ret, int *actual_format_ret,
1274 unsigned long *actual_size_ret, int delete_p)
1276 int total_size;
1277 unsigned long bytes_remaining;
1278 int offset = 0;
1279 unsigned char *tmp_data = 0;
1280 int result;
1281 int buffer_size = SELECTION_QUANTUM (display);
1283 if (buffer_size > MAX_SELECTION_QUANTUM)
1284 buffer_size = MAX_SELECTION_QUANTUM;
1286 BLOCK_INPUT;
1288 /* First probe the thing to find out how big it is. */
1289 result = XGetWindowProperty (display, window, property,
1290 0L, 0L, False, AnyPropertyType,
1291 actual_type_ret, actual_format_ret,
1292 actual_size_ret,
1293 &bytes_remaining, &tmp_data);
1294 if (result != Success)
1296 UNBLOCK_INPUT;
1297 *data_ret = 0;
1298 *bytes_ret = 0;
1299 return;
1302 /* This was allocated by Xlib, so use XFree. */
1303 XFree ((char *) tmp_data);
1305 if (*actual_type_ret == None || *actual_format_ret == 0)
1307 UNBLOCK_INPUT;
1308 return;
1311 total_size = bytes_remaining + 1;
1312 *data_ret = (unsigned char *) xmalloc (total_size);
1314 /* Now read, until we've gotten it all. */
1315 while (bytes_remaining)
1317 #ifdef TRACE_SELECTION
1318 unsigned long last = bytes_remaining;
1319 #endif
1320 result
1321 = XGetWindowProperty (display, window, property,
1322 (long)offset/4, (long)buffer_size/4,
1323 False,
1324 AnyPropertyType,
1325 actual_type_ret, actual_format_ret,
1326 actual_size_ret, &bytes_remaining, &tmp_data);
1328 TRACE2 ("Read %lu bytes from property %s",
1329 last - bytes_remaining,
1330 XGetAtomName (display, property));
1332 /* If this doesn't return Success at this point, it means that
1333 some clod deleted the selection while we were in the midst of
1334 reading it. Deal with that, I guess.... */
1335 if (result != Success)
1336 break;
1338 /* The man page for XGetWindowProperty says:
1339 "If the returned format is 32, the returned data is represented
1340 as a long array and should be cast to that type to obtain the
1341 elements."
1342 This applies even if long is more than 32 bits, the X library
1343 converts from 32 bit elements received from the X server to long
1344 and passes the long array to us. Thus, for that case memcpy can not
1345 be used. We convert to a 32 bit type here, because so much code
1346 assume on that.
1348 The bytes and offsets passed to XGetWindowProperty refers to the
1349 property and those are indeed in 32 bit quantities if format is 32. */
1351 if (32 < BITS_PER_LONG && *actual_format_ret == 32)
1353 unsigned long i;
1354 int *idata = (int *) ((*data_ret) + offset);
1355 long *ldata = (long *) tmp_data;
1357 for (i = 0; i < *actual_size_ret; ++i)
1359 idata[i]= (int) ldata[i];
1360 offset += 4;
1363 else
1365 *actual_size_ret *= *actual_format_ret / 8;
1366 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
1367 offset += *actual_size_ret;
1370 /* This was allocated by Xlib, so use XFree. */
1371 XFree ((char *) tmp_data);
1374 XFlush (display);
1375 UNBLOCK_INPUT;
1376 *bytes_ret = offset;
1379 /* Use xfree, not XFree, to free the data obtained with this function. */
1381 static void
1382 receive_incremental_selection (Display *display, Window window, Atom property,
1383 Lisp_Object target_type,
1384 unsigned int min_size_bytes,
1385 unsigned char **data_ret, int *size_bytes_ret,
1386 Atom *type_ret, int *format_ret,
1387 unsigned long *size_ret)
1389 int offset = 0;
1390 struct prop_location *wait_object;
1391 *size_bytes_ret = min_size_bytes;
1392 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1394 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1396 /* At this point, we have read an INCR property.
1397 Delete the property to ack it.
1398 (But first, prepare to receive the next event in this handshake.)
1400 Now, we must loop, waiting for the sending window to put a value on
1401 that property, then reading the property, then deleting it to ack.
1402 We are done when the sender places a property of length 0.
1404 BLOCK_INPUT;
1405 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1406 TRACE1 (" Delete property %s",
1407 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1408 XDeleteProperty (display, window, property);
1409 TRACE1 (" Expect new value of property %s",
1410 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1411 wait_object = expect_property_change (display, window, property,
1412 PropertyNewValue);
1413 XFlush (display);
1414 UNBLOCK_INPUT;
1416 while (1)
1418 unsigned char *tmp_data;
1419 int tmp_size_bytes;
1421 TRACE0 (" Wait for property change");
1422 wait_for_property_change (wait_object);
1424 /* expect it again immediately, because x_get_window_property may
1425 .. no it won't, I don't get it.
1426 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1427 TRACE0 (" Get property value");
1428 x_get_window_property (display, window, property,
1429 &tmp_data, &tmp_size_bytes,
1430 type_ret, format_ret, size_ret, 1);
1432 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1434 if (tmp_size_bytes == 0) /* we're done */
1436 TRACE0 ("Done reading incrementally");
1438 if (! waiting_for_other_props_on_window (display, window))
1439 XSelectInput (display, window, STANDARD_EVENT_SET);
1440 /* Use xfree, not XFree, because x_get_window_property
1441 calls xmalloc itself. */
1442 xfree (tmp_data);
1443 break;
1446 BLOCK_INPUT;
1447 TRACE1 (" ACK by deleting property %s",
1448 XGetAtomName (display, property));
1449 XDeleteProperty (display, window, property);
1450 wait_object = expect_property_change (display, window, property,
1451 PropertyNewValue);
1452 XFlush (display);
1453 UNBLOCK_INPUT;
1455 if (*size_bytes_ret < offset + tmp_size_bytes)
1457 *size_bytes_ret = offset + tmp_size_bytes;
1458 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1461 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
1462 offset += tmp_size_bytes;
1464 /* Use xfree, not XFree, because x_get_window_property
1465 calls xmalloc itself. */
1466 xfree (tmp_data);
1471 /* Fetch a value from property PROPERTY of X window WINDOW on display
1472 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1473 if this fails. */
1475 static Lisp_Object
1476 x_get_window_property_as_lisp_data (Display *display, Window window,
1477 Atom property,
1478 Lisp_Object target_type,
1479 Atom selection_atom)
1481 Atom actual_type;
1482 int actual_format;
1483 unsigned long actual_size;
1484 unsigned char *data = 0;
1485 int bytes = 0;
1486 Lisp_Object val;
1487 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1489 TRACE0 ("Reading selection data");
1491 x_get_window_property (display, window, property, &data, &bytes,
1492 &actual_type, &actual_format, &actual_size, 1);
1493 if (! data)
1495 int there_is_a_selection_owner;
1496 BLOCK_INPUT;
1497 there_is_a_selection_owner
1498 = XGetSelectionOwner (display, selection_atom);
1499 UNBLOCK_INPUT;
1500 if (there_is_a_selection_owner)
1501 signal_error ("Selection owner couldn't convert",
1502 actual_type
1503 ? list2 (target_type,
1504 x_atom_to_symbol (display, actual_type))
1505 : target_type);
1506 else
1507 signal_error ("No selection",
1508 x_atom_to_symbol (display, selection_atom));
1511 if (actual_type == dpyinfo->Xatom_INCR)
1513 /* That wasn't really the data, just the beginning. */
1515 unsigned int min_size_bytes = * ((unsigned int *) data);
1516 BLOCK_INPUT;
1517 /* Use xfree, not XFree, because x_get_window_property
1518 calls xmalloc itself. */
1519 xfree ((char *) data);
1520 UNBLOCK_INPUT;
1521 receive_incremental_selection (display, window, property, target_type,
1522 min_size_bytes, &data, &bytes,
1523 &actual_type, &actual_format,
1524 &actual_size);
1527 BLOCK_INPUT;
1528 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1529 XDeleteProperty (display, window, property);
1530 XFlush (display);
1531 UNBLOCK_INPUT;
1533 /* It's been read. Now convert it to a lisp object in some semi-rational
1534 manner. */
1535 val = selection_data_to_lisp_data (display, data, bytes,
1536 actual_type, actual_format);
1538 /* Use xfree, not XFree, because x_get_window_property
1539 calls xmalloc itself. */
1540 xfree ((char *) data);
1541 return val;
1544 /* These functions convert from the selection data read from the server into
1545 something that we can use from Lisp, and vice versa.
1547 Type: Format: Size: Lisp Type:
1548 ----- ------- ----- -----------
1549 * 8 * String
1550 ATOM 32 1 Symbol
1551 ATOM 32 > 1 Vector of Symbols
1552 * 16 1 Integer
1553 * 16 > 1 Vector of Integers
1554 * 32 1 if <=16 bits: Integer
1555 if > 16 bits: Cons of top16, bot16
1556 * 32 > 1 Vector of the above
1558 When converting a Lisp number to C, it is assumed to be of format 16 if
1559 it is an integer, and of format 32 if it is a cons of two integers.
1561 When converting a vector of numbers from Lisp to C, it is assumed to be
1562 of format 16 if every element in the vector is an integer, and is assumed
1563 to be of format 32 if any element is a cons of two integers.
1565 When converting an object to C, it may be of the form (SYMBOL . <data>)
1566 where SYMBOL is what we should claim that the type is. Format and
1567 representation are as above.
1569 Important: When format is 32, data should contain an array of int,
1570 not an array of long as the X library returns. This makes a difference
1571 when sizeof(long) != sizeof(int). */
1575 static Lisp_Object
1576 selection_data_to_lisp_data (Display *display, const unsigned char *data,
1577 int size, Atom type, int format)
1579 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1581 if (type == dpyinfo->Xatom_NULL)
1582 return QNULL;
1584 /* Convert any 8-bit data to a string, for compactness. */
1585 else if (format == 8)
1587 Lisp_Object str, lispy_type;
1589 str = make_unibyte_string ((char *) data, size);
1590 /* Indicate that this string is from foreign selection by a text
1591 property `foreign-selection' so that the caller of
1592 x-get-selection-internal (usually x-get-selection) can know
1593 that the string must be decode. */
1594 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1595 lispy_type = QCOMPOUND_TEXT;
1596 else if (type == dpyinfo->Xatom_UTF8_STRING)
1597 lispy_type = QUTF8_STRING;
1598 else
1599 lispy_type = QSTRING;
1600 Fput_text_property (make_number (0), make_number (size),
1601 Qforeign_selection, lispy_type, str);
1602 return str;
1604 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1605 a vector of symbols. */
1606 else if (type == XA_ATOM
1607 /* Treat ATOM_PAIR type similar to list of atoms. */
1608 || type == dpyinfo->Xatom_ATOM_PAIR)
1610 int i;
1611 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1612 But the callers of these function has made sure the data for
1613 format == 32 is an array of int. Thus, use int instead
1614 of Atom. */
1615 int *idata = (int *) data;
1617 if (size == sizeof (int))
1618 return x_atom_to_symbol (display, (Atom) idata[0]);
1619 else
1621 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1622 make_number (0));
1623 for (i = 0; i < size / sizeof (int); i++)
1624 Faset (v, make_number (i),
1625 x_atom_to_symbol (display, (Atom) idata[i]));
1626 return v;
1630 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1631 If the number is 32 bits and won't fit in a Lisp_Int,
1632 convert it to a cons of integers, 16 bits in each half.
1634 else if (format == 32 && size == sizeof (int))
1635 return INTEGER_TO_CONS (((unsigned int *) data) [0]);
1636 else if (format == 16 && size == sizeof (short))
1637 return make_number ((int) (((unsigned short *) data) [0]));
1639 /* Convert any other kind of data to a vector of numbers, represented
1640 as above (as an integer, or a cons of two 16 bit integers.)
1642 else if (format == 16)
1644 int i;
1645 Lisp_Object v;
1646 v = Fmake_vector (make_number (size / 2), make_number (0));
1647 for (i = 0; i < size / 2; i++)
1649 int j = (int) ((unsigned short *) data) [i];
1650 Faset (v, make_number (i), make_number (j));
1652 return v;
1654 else
1656 int i;
1657 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1658 for (i = 0; i < size / 4; i++)
1660 unsigned int j = ((unsigned int *) data) [i];
1661 Faset (v, make_number (i), INTEGER_TO_CONS (j));
1663 return v;
1668 /* Use xfree, not XFree, to free the data obtained with this function. */
1670 static void
1671 lisp_data_to_selection_data (Display *display, Lisp_Object obj,
1672 unsigned char **data_ret, Atom *type_ret,
1673 unsigned int *size_ret,
1674 int *format_ret, int *nofree_ret)
1676 Lisp_Object type = Qnil;
1677 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1679 *nofree_ret = 0;
1681 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1683 type = XCAR (obj);
1684 obj = XCDR (obj);
1685 if (CONSP (obj) && NILP (XCDR (obj)))
1686 obj = XCAR (obj);
1689 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1690 { /* This is not the same as declining */
1691 *format_ret = 32;
1692 *size_ret = 0;
1693 *data_ret = 0;
1694 type = QNULL;
1696 else if (STRINGP (obj))
1698 if (SCHARS (obj) < SBYTES (obj))
1699 /* OBJ is a multibyte string containing a non-ASCII char. */
1700 signal_error ("Non-ASCII string must be encoded in advance", obj);
1701 if (NILP (type))
1702 type = QSTRING;
1703 *format_ret = 8;
1704 *size_ret = SBYTES (obj);
1705 *data_ret = SDATA (obj);
1706 *nofree_ret = 1;
1708 else if (SYMBOLP (obj))
1710 *format_ret = 32;
1711 *size_ret = 1;
1712 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1713 (*data_ret) [sizeof (Atom)] = 0;
1714 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, obj);
1715 if (NILP (type)) type = QATOM;
1717 else if (INTEGERP (obj)
1718 && XINT (obj) < 0xFFFF
1719 && XINT (obj) > -0xFFFF)
1721 *format_ret = 16;
1722 *size_ret = 1;
1723 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1724 (*data_ret) [sizeof (short)] = 0;
1725 (*(short **) data_ret) [0] = (short) XINT (obj);
1726 if (NILP (type)) type = QINTEGER;
1728 else if (INTEGERP (obj)
1729 || (CONSP (obj) && INTEGERP (XCAR (obj))
1730 && (INTEGERP (XCDR (obj))
1731 || (CONSP (XCDR (obj))
1732 && INTEGERP (XCAR (XCDR (obj)))))))
1734 *format_ret = 32;
1735 *size_ret = 1;
1736 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1737 (*data_ret) [sizeof (long)] = 0;
1738 (*(unsigned long **) data_ret) [0] = cons_to_unsigned (obj, ULONG_MAX);
1739 if (NILP (type)) type = QINTEGER;
1741 else if (VECTORP (obj))
1743 /* Lisp_Vectors may represent a set of ATOMs;
1744 a set of 16 or 32 bit INTEGERs;
1745 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1747 int i;
1749 if (SYMBOLP (XVECTOR (obj)->contents [0]))
1750 /* This vector is an ATOM set */
1752 if (NILP (type)) type = QATOM;
1753 *size_ret = ASIZE (obj);
1754 *format_ret = 32;
1755 for (i = 0; i < *size_ret; i++)
1756 if (!SYMBOLP (XVECTOR (obj)->contents [i]))
1757 signal_error ("All elements of selection vector must have same type", obj);
1759 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
1760 for (i = 0; i < *size_ret; i++)
1761 (*(Atom **) data_ret) [i]
1762 = symbol_to_x_atom (dpyinfo, XVECTOR (obj)->contents [i]);
1764 else
1765 /* This vector is an INTEGER set, or something like it */
1767 int data_size = 2;
1768 *size_ret = ASIZE (obj);
1769 if (NILP (type)) type = QINTEGER;
1770 *format_ret = 16;
1771 for (i = 0; i < *size_ret; i++)
1772 if (CONSP (XVECTOR (obj)->contents [i]))
1773 *format_ret = 32;
1774 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
1775 signal_error (/* Qselection_error */
1776 "Elements of selection vector must be integers or conses of integers",
1777 obj);
1779 /* Use sizeof(long) even if it is more than 32 bits. See comment
1780 in x_get_window_property and x_fill_property_data. */
1782 if (*format_ret == 32) data_size = sizeof(long);
1783 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
1784 for (i = 0; i < *size_ret; i++)
1785 if (*format_ret == 32)
1786 (*((unsigned long **) data_ret)) [i] =
1787 cons_to_unsigned (XVECTOR (obj)->contents [i], ULONG_MAX);
1788 else
1789 (*((unsigned short **) data_ret)) [i] =
1790 cons_to_unsigned (XVECTOR (obj)->contents [i], USHRT_MAX);
1793 else
1794 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
1796 *type_ret = symbol_to_x_atom (dpyinfo, type);
1799 static Lisp_Object
1800 clean_local_selection_data (Lisp_Object obj)
1802 if (CONSP (obj)
1803 && INTEGERP (XCAR (obj))
1804 && CONSP (XCDR (obj))
1805 && INTEGERP (XCAR (XCDR (obj)))
1806 && NILP (XCDR (XCDR (obj))))
1807 obj = Fcons (XCAR (obj), XCDR (obj));
1809 if (CONSP (obj)
1810 && INTEGERP (XCAR (obj))
1811 && INTEGERP (XCDR (obj)))
1813 if (XINT (XCAR (obj)) == 0)
1814 return XCDR (obj);
1815 if (XINT (XCAR (obj)) == -1)
1816 return make_number (- XINT (XCDR (obj)));
1818 if (VECTORP (obj))
1820 int i;
1821 int size = ASIZE (obj);
1822 Lisp_Object copy;
1823 if (size == 1)
1824 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
1825 copy = Fmake_vector (make_number (size), Qnil);
1826 for (i = 0; i < size; i++)
1827 XVECTOR (copy)->contents [i]
1828 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
1829 return copy;
1831 return obj;
1834 /* Called from XTread_socket to handle SelectionNotify events.
1835 If it's the selection we are waiting for, stop waiting
1836 by setting the car of reading_selection_reply to non-nil.
1837 We store t there if the reply is successful, lambda if not. */
1839 void
1840 x_handle_selection_notify (XSelectionEvent *event)
1842 if (event->requestor != reading_selection_window)
1843 return;
1844 if (event->selection != reading_which_selection)
1845 return;
1847 TRACE0 ("Received SelectionNotify");
1848 XSETCAR (reading_selection_reply,
1849 (event->property != 0 ? Qt : Qlambda));
1853 /* From a Lisp_Object, return a suitable frame for selection
1854 operations. OBJECT may be a frame, a terminal object, or nil
1855 (which stands for the selected frame--or, if that is not an X
1856 frame, the first X display on the list). If no suitable frame can
1857 be found, return NULL. */
1859 static struct frame *
1860 frame_for_x_selection (Lisp_Object object)
1862 Lisp_Object tail;
1863 struct frame *f;
1865 if (NILP (object))
1867 f = XFRAME (selected_frame);
1868 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1869 return f;
1871 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1873 f = XFRAME (XCAR (tail));
1874 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1875 return f;
1878 else if (TERMINALP (object))
1880 struct terminal *t = get_terminal (object, 1);
1881 if (t->type == output_x_window)
1883 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1885 f = XFRAME (XCAR (tail));
1886 if (FRAME_LIVE_P (f) && f->terminal == t)
1887 return f;
1891 else if (FRAMEP (object))
1893 f = XFRAME (object);
1894 if (FRAME_X_P (f) && FRAME_LIVE_P (f))
1895 return f;
1898 return NULL;
1902 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
1903 Sx_own_selection_internal, 2, 3, 0,
1904 doc: /* Assert an X selection of type SELECTION and value VALUE.
1905 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1906 \(Those are literal upper-case symbol names, since that's what X expects.)
1907 VALUE is typically a string, or a cons of two markers, but may be
1908 anything that the functions on `selection-converter-alist' know about.
1910 FRAME should be a frame that should own the selection. If omitted or
1911 nil, it defaults to the selected frame. */)
1912 (Lisp_Object selection, Lisp_Object value, Lisp_Object frame)
1914 if (NILP (frame)) frame = selected_frame;
1915 if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame)))
1916 error ("X selection unavailable for this frame");
1918 CHECK_SYMBOL (selection);
1919 if (NILP (value)) error ("VALUE may not be nil");
1920 x_own_selection (selection, value, frame);
1921 return value;
1925 /* Request the selection value from the owner. If we are the owner,
1926 simply return our selection value. If we are not the owner, this
1927 will block until all of the data has arrived. */
1929 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
1930 Sx_get_selection_internal, 2, 4, 0,
1931 doc: /* Return text selected from some X window.
1932 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1933 \(Those are literal upper-case symbol names, since that's what X expects.)
1934 TYPE is the type of data desired, typically `STRING'.
1935 TIME_STAMP is the time to use in the XConvertSelection call for foreign
1936 selections. If omitted, defaults to the time for the last event.
1938 TERMINAL should be a terminal object or a frame specifying the X
1939 server to query. If omitted or nil, that stands for the selected
1940 frame's display, or the first available X display. */)
1941 (Lisp_Object selection_symbol, Lisp_Object target_type,
1942 Lisp_Object time_stamp, Lisp_Object terminal)
1944 Lisp_Object val = Qnil;
1945 struct gcpro gcpro1, gcpro2;
1946 struct frame *f = frame_for_x_selection (terminal);
1947 GCPRO2 (target_type, val); /* we store newly consed data into these */
1949 CHECK_SYMBOL (selection_symbol);
1950 CHECK_SYMBOL (target_type);
1951 if (EQ (target_type, QMULTIPLE))
1952 error ("Retrieving MULTIPLE selections is currently unimplemented");
1953 if (!f)
1954 error ("X selection unavailable for this frame");
1956 val = x_get_local_selection (selection_symbol, target_type, 1,
1957 FRAME_X_DISPLAY_INFO (f));
1959 if (NILP (val) && FRAME_LIVE_P (f))
1961 Lisp_Object frame;
1962 XSETFRAME (frame, f);
1963 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type,
1964 time_stamp, frame));
1967 if (CONSP (val) && SYMBOLP (XCAR (val)))
1969 val = XCDR (val);
1970 if (CONSP (val) && NILP (XCDR (val)))
1971 val = XCAR (val);
1973 RETURN_UNGCPRO (clean_local_selection_data (val));
1976 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
1977 Sx_disown_selection_internal, 1, 3, 0,
1978 doc: /* If we own the selection SELECTION, disown it.
1979 Disowning it means there is no such selection.
1981 TERMINAL should be a terminal object or a frame specifying the X
1982 server to query. If omitted or nil, that stands for the selected
1983 frame's display, or the first available X display. */)
1984 (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal)
1986 Time timestamp;
1987 Atom selection_atom;
1988 union {
1989 struct selection_input_event sie;
1990 struct input_event ie;
1991 } event;
1992 struct frame *f = frame_for_x_selection (terminal);
1993 struct x_display_info *dpyinfo;
1995 if (!f)
1996 return Qnil;
1998 dpyinfo = FRAME_X_DISPLAY_INFO (f);
1999 CHECK_SYMBOL (selection);
2001 /* Don't disown the selection when we're not the owner. */
2002 if (NILP (LOCAL_SELECTION (selection, dpyinfo)))
2003 return Qnil;
2005 selection_atom = symbol_to_x_atom (dpyinfo, selection);
2007 BLOCK_INPUT;
2008 if (NILP (time_object))
2009 timestamp = last_event_timestamp;
2010 else
2011 CONS_TO_INTEGER (time_object, Time, timestamp);
2012 XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp);
2013 UNBLOCK_INPUT;
2015 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2016 generated for a window which owns the selection when that window sets
2017 the selection owner to None. The NCD server does, the MIT Sun4 server
2018 doesn't. So we synthesize one; this means we might get two, but
2019 that's ok, because the second one won't have any effect. */
2020 SELECTION_EVENT_DISPLAY (&event.sie) = dpyinfo->display;
2021 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2022 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2023 x_handle_selection_clear (&event.ie);
2025 return Qt;
2028 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2029 0, 2, 0,
2030 doc: /* Whether the current Emacs process owns the given X Selection.
2031 The arg should be the name of the selection in question, typically one of
2032 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2033 \(Those are literal upper-case symbol names, since that's what X expects.)
2034 For convenience, the symbol nil is the same as `PRIMARY',
2035 and t is the same as `SECONDARY'.
2037 TERMINAL should be a terminal object or a frame specifying the X
2038 server to query. If omitted or nil, that stands for the selected
2039 frame's display, or the first available X display. */)
2040 (Lisp_Object selection, Lisp_Object terminal)
2042 struct frame *f = frame_for_x_selection (terminal);
2044 CHECK_SYMBOL (selection);
2045 if (EQ (selection, Qnil)) selection = QPRIMARY;
2046 if (EQ (selection, Qt)) selection = QSECONDARY;
2048 if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f))))
2049 return Qt;
2050 else
2051 return Qnil;
2054 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2055 0, 2, 0,
2056 doc: /* Whether there is an owner for the given X selection.
2057 SELECTION should be the name of the selection in question, typically
2058 one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects
2059 these literal upper-case names.) The symbol nil is the same as
2060 `PRIMARY', and t is the same as `SECONDARY'.
2062 TERMINAL should be a terminal object or a frame specifying the X
2063 server to query. If omitted or nil, that stands for the selected
2064 frame's display, or the first available X display. */)
2065 (Lisp_Object selection, Lisp_Object terminal)
2067 Window owner;
2068 Atom atom;
2069 struct frame *f = frame_for_x_selection (terminal);
2070 struct x_display_info *dpyinfo;
2072 CHECK_SYMBOL (selection);
2073 if (EQ (selection, Qnil)) selection = QPRIMARY;
2074 if (EQ (selection, Qt)) selection = QSECONDARY;
2076 if (!f)
2077 return Qnil;
2079 dpyinfo = FRAME_X_DISPLAY_INFO (f);
2081 if (!NILP (LOCAL_SELECTION (selection, dpyinfo)))
2082 return Qt;
2084 atom = symbol_to_x_atom (dpyinfo, selection);
2085 if (atom == 0) return Qnil;
2086 BLOCK_INPUT;
2087 owner = XGetSelectionOwner (dpyinfo->display, atom);
2088 UNBLOCK_INPUT;
2089 return (owner ? Qt : Qnil);
2093 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2094 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2096 static Lisp_Object
2097 x_clipboard_manager_save (Lisp_Object frame)
2099 struct frame *f = XFRAME (frame);
2100 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2101 Atom data = dpyinfo->Xatom_UTF8_STRING;
2103 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2104 dpyinfo->Xatom_EMACS_TMP,
2105 dpyinfo->Xatom_ATOM, 32, PropModeReplace,
2106 (unsigned char *) &data, 1);
2107 x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS,
2108 Qnil, frame);
2109 return Qt;
2112 /* Error handler for x_clipboard_manager_save_frame. */
2114 static Lisp_Object
2115 x_clipboard_manager_error_1 (Lisp_Object err)
2117 Lisp_Object args[2];
2118 args[0] = build_string ("X clipboard manager error: %s\n\
2119 If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2120 args[1] = CAR (CDR (err));
2121 Fmessage (2, args);
2122 return Qnil;
2125 /* Error handler for x_clipboard_manager_save_all. */
2127 static Lisp_Object
2128 x_clipboard_manager_error_2 (Lisp_Object err)
2130 fprintf (stderr, "Error saving to X clipboard manager.\n\
2131 If the problem persists, set `x-select-enable-clipboard-manager' \
2132 to nil.\n");
2133 return Qnil;
2136 /* Called from delete_frame: save any clipboard owned by FRAME to the
2137 clipboard manager. Do nothing if FRAME does not own the clipboard,
2138 or if no clipboard manager is present. */
2140 void
2141 x_clipboard_manager_save_frame (Lisp_Object frame)
2143 struct frame *f;
2145 if (!NILP (Vx_select_enable_clipboard_manager)
2146 && FRAMEP (frame)
2147 && (f = XFRAME (frame), FRAME_X_P (f))
2148 && FRAME_LIVE_P (f))
2150 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2151 Lisp_Object local_selection
2152 = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2154 if (!NILP (local_selection)
2155 && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection)))))
2156 && XGetSelectionOwner (dpyinfo->display,
2157 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2158 internal_condition_case_1 (x_clipboard_manager_save, frame, Qt,
2159 x_clipboard_manager_error_1);
2163 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2164 clipboard manager. Do nothing if FRAME does not own the clipboard,
2165 or if no clipboard manager is present. */
2167 void
2168 x_clipboard_manager_save_all (void)
2170 /* Loop through all X displays, saving owned clipboards. */
2171 struct x_display_info *dpyinfo;
2172 Lisp_Object local_selection, local_frame;
2174 if (NILP (Vx_select_enable_clipboard_manager))
2175 return;
2177 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
2179 local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo);
2180 if (NILP (local_selection)
2181 || !XGetSelectionOwner (dpyinfo->display,
2182 dpyinfo->Xatom_CLIPBOARD_MANAGER))
2183 continue;
2185 local_frame = XCAR (XCDR (XCDR (XCDR (local_selection))));
2186 if (FRAME_LIVE_P (XFRAME (local_frame)))
2187 internal_condition_case_1 (x_clipboard_manager_save, local_frame,
2188 Qt, x_clipboard_manager_error_2);
2193 /***********************************************************************
2194 Drag and drop support
2195 ***********************************************************************/
2196 /* Check that lisp values are of correct type for x_fill_property_data.
2197 That is, number, string or a cons with two numbers (low and high 16
2198 bit parts of a 32 bit number). Return the number of items in DATA,
2199 or -1 if there is an error. */
2202 x_check_property_data (Lisp_Object data)
2204 Lisp_Object iter;
2205 int size = 0;
2207 for (iter = data; CONSP (iter); iter = XCDR (iter))
2209 Lisp_Object o = XCAR (iter);
2211 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2212 return -1;
2213 else if (CONSP (o) &&
2214 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2215 return -1;
2216 size++;
2219 return size;
2222 /* Convert lisp values to a C array. Values may be a number, a string
2223 which is taken as an X atom name and converted to the atom value, or
2224 a cons containing the two 16 bit parts of a 32 bit number.
2226 DPY is the display use to look up X atoms.
2227 DATA is a Lisp list of values to be converted.
2228 RET is the C array that contains the converted values. It is assumed
2229 it is big enough to hold all values.
2230 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2231 be stored in RET. Note that long is used for 32 even if long is more
2232 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2233 XClientMessageEvent). */
2235 void
2236 x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format)
2238 long val;
2239 long *d32 = (long *) ret;
2240 short *d16 = (short *) ret;
2241 char *d08 = (char *) ret;
2242 Lisp_Object iter;
2244 for (iter = data; CONSP (iter); iter = XCDR (iter))
2246 Lisp_Object o = XCAR (iter);
2248 if (INTEGERP (o) || FLOATP (o) || CONSP (o))
2249 val = cons_to_signed (o, LONG_MIN, LONG_MAX);
2250 else if (STRINGP (o))
2252 BLOCK_INPUT;
2253 val = (long) XInternAtom (dpy, SSDATA (o), False);
2254 UNBLOCK_INPUT;
2256 else
2257 error ("Wrong type, must be string, number or cons");
2259 if (format == 8)
2261 if (CHAR_MIN <= val && val <= CHAR_MAX)
2262 *d08++ = val;
2263 else
2264 error ("Out of 'char' range");
2266 else if (format == 16)
2268 if (SHRT_MIN <= val && val <= SHRT_MAX)
2269 *d16++ = val;
2270 else
2271 error ("Out of 'short' range");
2273 else
2274 *d32++ = val;
2278 /* Convert an array of C values to a Lisp list.
2279 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2280 DATA is a C array of values to be converted.
2281 TYPE is the type of the data. Only XA_ATOM is special, it converts
2282 each number in DATA to its corresponfing X atom as a symbol.
2283 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2284 be stored in RET.
2285 SIZE is the number of elements in DATA.
2287 Important: When format is 32, data should contain an array of int,
2288 not an array of long as the X library returns. This makes a difference
2289 when sizeof(long) != sizeof(int).
2291 Also see comment for selection_data_to_lisp_data above. */
2293 Lisp_Object
2294 x_property_data_to_lisp (struct frame *f, const unsigned char *data,
2295 Atom type, int format, long unsigned int size)
2297 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2298 data, size*format/8, type, format);
2301 /* Get the mouse position in frame relative coordinates. */
2303 static void
2304 mouse_position_for_drop (FRAME_PTR f, int *x, int *y)
2306 Window root, dummy_window;
2307 int dummy;
2309 BLOCK_INPUT;
2311 XQueryPointer (FRAME_X_DISPLAY (f),
2312 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2314 /* The root window which contains the pointer. */
2315 &root,
2317 /* Window pointer is on, not used */
2318 &dummy_window,
2320 /* The position on that root window. */
2321 x, y,
2323 /* x/y in dummy_window coordinates, not used. */
2324 &dummy, &dummy,
2326 /* Modifier keys and pointer buttons, about which
2327 we don't care. */
2328 (unsigned int *) &dummy);
2331 /* Absolute to relative. */
2332 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2333 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2335 UNBLOCK_INPUT;
2338 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2339 Sx_get_atom_name, 1, 2, 0,
2340 doc: /* Return the X atom name for VALUE as a string.
2341 VALUE may be a number or a cons where the car is the upper 16 bits and
2342 the cdr is the lower 16 bits of a 32 bit value.
2343 Use the display for FRAME or the current frame if FRAME is not given or nil.
2345 If the value is 0 or the atom is not known, return the empty string. */)
2346 (Lisp_Object value, Lisp_Object frame)
2348 struct frame *f = check_x_frame (frame);
2349 char *name = 0;
2350 char empty[] = "";
2351 Lisp_Object ret = Qnil;
2352 Display *dpy = FRAME_X_DISPLAY (f);
2353 Atom atom;
2354 int had_errors;
2356 CONS_TO_INTEGER (value, Atom, atom);
2358 BLOCK_INPUT;
2359 x_catch_errors (dpy);
2360 name = atom ? XGetAtomName (dpy, atom) : empty;
2361 had_errors = x_had_errors_p (dpy);
2362 x_uncatch_errors ();
2364 if (!had_errors)
2365 ret = build_string (name);
2367 if (atom && name) XFree (name);
2368 if (NILP (ret)) ret = empty_unibyte_string;
2370 UNBLOCK_INPUT;
2372 return ret;
2375 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2376 Sx_register_dnd_atom, 1, 2, 0,
2377 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2378 ATOM can be a symbol or a string. The ATOM is interned on the display that
2379 FRAME is on. If FRAME is nil, the selected frame is used. */)
2380 (Lisp_Object atom, Lisp_Object frame)
2382 Atom x_atom;
2383 struct frame *f = check_x_frame (frame);
2384 ptrdiff_t i;
2385 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2388 if (SYMBOLP (atom))
2389 x_atom = symbol_to_x_atom (dpyinfo, atom);
2390 else if (STRINGP (atom))
2392 BLOCK_INPUT;
2393 x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False);
2394 UNBLOCK_INPUT;
2396 else
2397 error ("ATOM must be a symbol or a string");
2399 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2400 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2401 return Qnil;
2403 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2405 if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof *dpyinfo->x_dnd_atoms / 2
2406 < dpyinfo->x_dnd_atoms_size)
2407 memory_full (SIZE_MAX);
2408 dpyinfo->x_dnd_atoms_size *= 2;
2409 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2410 sizeof (*dpyinfo->x_dnd_atoms)
2411 * dpyinfo->x_dnd_atoms_size);
2414 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2415 return Qnil;
2418 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2421 x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp)
2423 Lisp_Object vec;
2424 Lisp_Object frame;
2425 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2426 unsigned long size = 160/event->format;
2427 int x, y;
2428 unsigned char *data = (unsigned char *) event->data.b;
2429 int idata[5];
2430 ptrdiff_t i;
2432 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2433 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2435 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2437 XSETFRAME (frame, f);
2439 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2440 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2441 function expects them to be of size int (i.e. 32). So to be able to
2442 use that function, put the data in the form it expects if format is 32. */
2444 if (32 < BITS_PER_LONG && event->format == 32)
2446 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2447 idata[i] = (int) event->data.l[i];
2448 data = (unsigned char *) idata;
2451 vec = Fmake_vector (make_number (4), Qnil);
2452 ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2453 event->message_type)));
2454 ASET (vec, 1, frame);
2455 ASET (vec, 2, make_number (event->format));
2456 ASET (vec, 3, x_property_data_to_lisp (f,
2457 data,
2458 event->message_type,
2459 event->format,
2460 size));
2462 mouse_position_for_drop (f, &x, &y);
2463 bufp->kind = DRAG_N_DROP_EVENT;
2464 bufp->frame_or_window = frame;
2465 bufp->timestamp = CurrentTime;
2466 bufp->x = make_number (x);
2467 bufp->y = make_number (y);
2468 bufp->arg = vec;
2469 bufp->modifiers = 0;
2471 return 1;
2474 DEFUN ("x-send-client-message", Fx_send_client_event,
2475 Sx_send_client_message, 6, 6, 0,
2476 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2478 For DISPLAY, specify either a frame or a display name (a string).
2479 If DISPLAY is nil, that stands for the selected frame's display.
2480 DEST may be a number, in which case it is a Window id. The value 0 may
2481 be used to send to the root window of the DISPLAY.
2482 If DEST is a cons, it is converted to a 32 bit number
2483 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2484 number is then used as a window id.
2485 If DEST is a frame the event is sent to the outer window of that frame.
2486 A value of nil means the currently selected frame.
2487 If DEST is the string "PointerWindow" the event is sent to the window that
2488 contains the pointer. If DEST is the string "InputFocus" the event is
2489 sent to the window that has the input focus.
2490 FROM is the frame sending the event. Use nil for currently selected frame.
2491 MESSAGE-TYPE is the name of an Atom as a string.
2492 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2493 bits. VALUES is a list of numbers, cons and/or strings containing the values
2494 to send. If a value is a string, it is converted to an Atom and the value of
2495 the Atom is sent. If a value 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.
2497 If more values than fits into the event is given, the excessive values
2498 are ignored. */)
2499 (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values)
2501 struct x_display_info *dpyinfo = check_x_display_info (display);
2503 CHECK_STRING (message_type);
2504 x_send_client_event(display, dest, from,
2505 XInternAtom (dpyinfo->display,
2506 SSDATA (message_type),
2507 False),
2508 format, values);
2510 return Qnil;
2513 void
2514 x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values)
2516 struct x_display_info *dpyinfo = check_x_display_info (display);
2517 Window wdest;
2518 XEvent event;
2519 struct frame *f = check_x_frame (from);
2520 int to_root;
2522 CHECK_NUMBER (format);
2523 CHECK_CONS (values);
2525 if (x_check_property_data (values) == -1)
2526 error ("Bad data in VALUES, must be number, cons or string");
2528 event.xclient.type = ClientMessage;
2529 event.xclient.format = XFASTINT (format);
2531 if (event.xclient.format != 8 && event.xclient.format != 16
2532 && event.xclient.format != 32)
2533 error ("FORMAT must be one of 8, 16 or 32");
2535 if (FRAMEP (dest) || NILP (dest))
2537 struct frame *fdest = check_x_frame (dest);
2538 wdest = FRAME_OUTER_WINDOW (fdest);
2540 else if (STRINGP (dest))
2542 if (strcmp (SSDATA (dest), "PointerWindow") == 0)
2543 wdest = PointerWindow;
2544 else if (strcmp (SSDATA (dest), "InputFocus") == 0)
2545 wdest = InputFocus;
2546 else
2547 error ("DEST as a string must be one of PointerWindow or InputFocus");
2549 else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest))
2550 CONS_TO_INTEGER (dest, Window, wdest);
2551 else
2552 error ("DEST must be a frame, nil, string, number or cons");
2554 if (wdest == 0) wdest = dpyinfo->root_window;
2555 to_root = wdest == dpyinfo->root_window;
2557 BLOCK_INPUT;
2559 event.xclient.message_type = message_type;
2560 event.xclient.display = dpyinfo->display;
2562 /* Some clients (metacity for example) expects sending window to be here
2563 when sending to the root window. */
2564 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2567 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2568 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2569 event.xclient.format);
2571 /* If event mask is 0 the event is sent to the client that created
2572 the destination window. But if we are sending to the root window,
2573 there is no such client. Then we set the event mask to 0xffff. The
2574 event then goes to clients selecting for events on the root window. */
2575 x_catch_errors (dpyinfo->display);
2577 int propagate = to_root ? False : True;
2578 unsigned mask = to_root ? 0xffff : 0;
2579 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2580 XFlush (dpyinfo->display);
2582 x_uncatch_errors ();
2583 UNBLOCK_INPUT;
2587 void
2588 syms_of_xselect (void)
2590 defsubr (&Sx_get_selection_internal);
2591 defsubr (&Sx_own_selection_internal);
2592 defsubr (&Sx_disown_selection_internal);
2593 defsubr (&Sx_selection_owner_p);
2594 defsubr (&Sx_selection_exists_p);
2596 defsubr (&Sx_get_atom_name);
2597 defsubr (&Sx_send_client_message);
2598 defsubr (&Sx_register_dnd_atom);
2600 reading_selection_reply = Fcons (Qnil, Qnil);
2601 staticpro (&reading_selection_reply);
2602 reading_selection_window = 0;
2603 reading_which_selection = 0;
2605 property_change_wait_list = 0;
2606 prop_location_identifier = 0;
2607 property_change_reply = Fcons (Qnil, Qnil);
2608 staticpro (&property_change_reply);
2610 converted_selections = NULL;
2611 conversion_fail_tag = None;
2613 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist,
2614 doc: /* An alist associating X Windows selection-types with functions.
2615 These functions are called to convert the selection, with three args:
2616 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2617 a desired type to which the selection should be converted;
2618 and the local selection value (whatever was given to `x-own-selection').
2620 The function should return the value to send to the X server
2621 \(typically a string). A return value of nil
2622 means that the conversion could not be done.
2623 A return value which is the symbol `NULL'
2624 means that a side-effect was executed,
2625 and there is no meaningful selection value. */);
2626 Vselection_converter_alist = Qnil;
2628 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions,
2629 doc: /* A list of functions to be called when Emacs loses an X selection.
2630 \(This happens when some other X client makes its own selection
2631 or when a Lisp program explicitly clears the selection.)
2632 The functions are called with one argument, the selection type
2633 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2634 Vx_lost_selection_functions = Qnil;
2636 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions,
2637 doc: /* A list of functions to be called when Emacs answers a selection request.
2638 The functions are called with three arguments:
2639 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2640 - the selection-type which Emacs was asked to convert the
2641 selection into before sending (for example, `STRING' or `LENGTH');
2642 - a flag indicating success or failure for responding to the request.
2643 We might have failed (and declined the request) for any number of reasons,
2644 including being asked for a selection that we no longer own, or being asked
2645 to convert into a type that we don't know about or that is inappropriate.
2646 This hook doesn't let you change the behavior of Emacs's selection replies,
2647 it merely informs you that they have happened. */);
2648 Vx_sent_selection_functions = Qnil;
2650 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2651 Vx_select_enable_clipboard_manager,
2652 doc: /* Whether to enable X clipboard manager support.
2653 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2654 while owning the X clipboard, the clipboard contents are saved to the
2655 clipboard manager if one is present. */);
2656 Vx_select_enable_clipboard_manager = Qt;
2658 DEFVAR_INT ("x-selection-timeout", x_selection_timeout,
2659 doc: /* Number of milliseconds to wait for a selection reply.
2660 If the selection owner doesn't reply in this time, we give up.
2661 A value of 0 means wait as long as necessary. This is initialized from the
2662 \"*selectionTimeout\" resource. */);
2663 x_selection_timeout = 0;
2665 /* QPRIMARY is defined in keyboard.c. */
2666 DEFSYM (QSECONDARY, "SECONDARY");
2667 DEFSYM (QSTRING, "STRING");
2668 DEFSYM (QINTEGER, "INTEGER");
2669 DEFSYM (QCLIPBOARD, "CLIPBOARD");
2670 DEFSYM (QTIMESTAMP, "TIMESTAMP");
2671 DEFSYM (QTEXT, "TEXT");
2672 DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT");
2673 DEFSYM (QUTF8_STRING, "UTF8_STRING");
2674 DEFSYM (QDELETE, "DELETE");
2675 DEFSYM (QMULTIPLE, "MULTIPLE");
2676 DEFSYM (QINCR, "INCR");
2677 DEFSYM (QEMACS_TMP, "_EMACS_TMP_");
2678 DEFSYM (QTARGETS, "TARGETS");
2679 DEFSYM (QATOM, "ATOM");
2680 DEFSYM (QATOM_PAIR, "ATOM_PAIR");
2681 DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER");
2682 DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS");
2683 DEFSYM (QNULL, "NULL");
2684 DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions");
2685 DEFSYM (Qforeign_selection, "foreign-selection");
2686 DEFSYM (Qx_lost_selection_functions, "x-lost-selection-functions");
2687 DEFSYM (Qx_sent_selection_functions, "x-sent-selection-functions");