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