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