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