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