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