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