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