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