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