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