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