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