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