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