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