(frame-inherited-parameters): New var.
[emacs.git] / src / xselect.c
blobd71ac42aa7c6b5d15d10232aaee77d10e6145807
1 /* X Selection processing for Emacs.
2 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007 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;
402 Display *display;
403 Time time = last_event_timestamp;
404 Atom selection_atom;
405 struct x_display_info *dpyinfo;
407 if (! FRAME_X_P (sf))
408 return;
410 selecting_window = FRAME_X_WINDOW (sf);
411 display = FRAME_X_DISPLAY (sf);
412 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
414 CHECK_SYMBOL (selection_name);
415 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
417 BLOCK_INPUT;
418 x_catch_errors (display);
419 XSetSelectionOwner (display, selection_atom, selecting_window, time);
420 x_check_errors (display, "Can't set selection: %s");
421 x_uncatch_errors ();
422 UNBLOCK_INPUT;
424 /* Now update the local cache */
426 Lisp_Object selection_time;
427 Lisp_Object selection_data;
428 Lisp_Object prev_value;
430 selection_time = long_to_cons ((unsigned long) time);
431 selection_data = Fcons (selection_name,
432 Fcons (selection_value,
433 Fcons (selection_time,
434 Fcons (selected_frame, Qnil))));
435 prev_value = assq_no_quit (selection_name, Vselection_alist);
437 Vselection_alist = Fcons (selection_data, Vselection_alist);
439 /* If we already owned the selection, remove the old selection data.
440 Perhaps we should destructively modify it instead.
441 Don't use Fdelq as that may QUIT. */
442 if (!NILP (prev_value))
444 Lisp_Object rest; /* we know it's not the CAR, so it's easy. */
445 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
446 if (EQ (prev_value, Fcar (XCDR (rest))))
448 XSETCDR (rest, Fcdr (XCDR (rest)));
449 break;
455 /* Given a selection-name and desired type, look up our local copy of
456 the selection value and convert it to the type.
457 The value is nil or a string.
458 This function is used both for remote requests (LOCAL_REQUEST is zero)
459 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
461 This calls random Lisp code, and may signal or gc. */
463 static Lisp_Object
464 x_get_local_selection (selection_symbol, target_type, local_request)
465 Lisp_Object selection_symbol, target_type;
466 int local_request;
468 Lisp_Object local_value;
469 Lisp_Object handler_fn, value, type, check;
470 int count;
472 local_value = assq_no_quit (selection_symbol, Vselection_alist);
474 if (NILP (local_value)) return Qnil;
476 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
477 if (EQ (target_type, QTIMESTAMP))
479 handler_fn = Qnil;
480 value = XCAR (XCDR (XCDR (local_value)));
482 #if 0
483 else if (EQ (target_type, QDELETE))
485 handler_fn = Qnil;
486 Fx_disown_selection_internal
487 (selection_symbol,
488 XCAR (XCDR (XCDR (local_value))));
489 value = QNULL;
491 #endif
493 #if 0 /* #### MULTIPLE doesn't work yet */
494 else if (CONSP (target_type)
495 && XCAR (target_type) == QMULTIPLE)
497 Lisp_Object pairs;
498 int size;
499 int i;
500 pairs = XCDR (target_type);
501 size = XVECTOR (pairs)->size;
502 /* If the target is MULTIPLE, then target_type looks like
503 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
504 We modify the second element of each pair in the vector and
505 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
507 for (i = 0; i < size; i++)
509 Lisp_Object pair;
510 pair = XVECTOR (pairs)->contents [i];
511 XVECTOR (pair)->contents [1]
512 = x_get_local_selection (XVECTOR (pair)->contents [0],
513 XVECTOR (pair)->contents [1],
514 local_request);
516 return pairs;
518 #endif
519 else
521 /* Don't allow a quit within the converter.
522 When the user types C-g, he would be surprised
523 if by luck it came during a converter. */
524 count = SPECPDL_INDEX ();
525 specbind (Qinhibit_quit, Qt);
527 CHECK_SYMBOL (target_type);
528 handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
529 /* gcpro is not needed here since nothing but HANDLER_FN
530 is live, and that ought to be a symbol. */
532 if (!NILP (handler_fn))
533 value = call3 (handler_fn,
534 selection_symbol, (local_request ? Qnil : target_type),
535 XCAR (XCDR (local_value)));
536 else
537 value = Qnil;
538 unbind_to (count, Qnil);
541 /* Make sure this value is of a type that we could transmit
542 to another X client. */
544 check = value;
545 if (CONSP (value)
546 && SYMBOLP (XCAR (value)))
547 type = XCAR (value),
548 check = XCDR (value);
550 if (STRINGP (check)
551 || VECTORP (check)
552 || SYMBOLP (check)
553 || INTEGERP (check)
554 || NILP (value))
555 return value;
556 /* Check for a value that cons_to_long could handle. */
557 else if (CONSP (check)
558 && INTEGERP (XCAR (check))
559 && (INTEGERP (XCDR (check))
561 (CONSP (XCDR (check))
562 && INTEGERP (XCAR (XCDR (check)))
563 && NILP (XCDR (XCDR (check))))))
564 return value;
566 signal_error ("Invalid data returned by selection-conversion function",
567 list2 (handler_fn, value));
570 /* Subroutines of x_reply_selection_request. */
572 /* Send a SelectionNotify event to the requestor with property=None,
573 meaning we were unable to do what they wanted. */
575 static void
576 x_decline_selection_request (event)
577 struct input_event *event;
579 XSelectionEvent reply;
581 reply.type = SelectionNotify;
582 reply.display = SELECTION_EVENT_DISPLAY (event);
583 reply.requestor = SELECTION_EVENT_REQUESTOR (event);
584 reply.selection = SELECTION_EVENT_SELECTION (event);
585 reply.time = SELECTION_EVENT_TIME (event);
586 reply.target = SELECTION_EVENT_TARGET (event);
587 reply.property = None;
589 /* The reason for the error may be that the receiver has
590 died in the meantime. Handle that case. */
591 BLOCK_INPUT;
592 x_catch_errors (reply.display);
593 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
594 XFlush (reply.display);
595 x_uncatch_errors ();
596 UNBLOCK_INPUT;
599 /* This is the selection request currently being processed.
600 It is set to zero when the request is fully processed. */
601 static struct input_event *x_selection_current_request;
603 /* Display info in x_selection_request. */
605 static struct x_display_info *selection_request_dpyinfo;
607 /* Used as an unwind-protect clause so that, if a selection-converter signals
608 an error, we tell the requester that we were unable to do what they wanted
609 before we throw to top-level or go into the debugger or whatever. */
611 static Lisp_Object
612 x_selection_request_lisp_error (ignore)
613 Lisp_Object ignore;
615 if (x_selection_current_request != 0
616 && selection_request_dpyinfo->display)
617 x_decline_selection_request (x_selection_current_request);
618 return Qnil;
621 static Lisp_Object
622 x_catch_errors_unwind (dummy)
623 Lisp_Object dummy;
625 BLOCK_INPUT;
626 x_uncatch_errors ();
627 UNBLOCK_INPUT;
628 return Qnil;
632 /* This stuff is so that INCR selections are reentrant (that is, so we can
633 be servicing multiple INCR selection requests simultaneously.) I haven't
634 actually tested that yet. */
636 /* Keep a list of the property changes that are awaited. */
638 struct prop_location
640 int identifier;
641 Display *display;
642 Window window;
643 Atom property;
644 int desired_state;
645 int arrived;
646 struct prop_location *next;
649 static struct prop_location *expect_property_change ();
650 static void wait_for_property_change ();
651 static void unexpect_property_change ();
652 static int waiting_for_other_props_on_window ();
654 static int prop_location_identifier;
656 static Lisp_Object property_change_reply;
658 static struct prop_location *property_change_reply_object;
660 static struct prop_location *property_change_wait_list;
662 static Lisp_Object
663 queue_selection_requests_unwind (tem)
664 Lisp_Object tem;
666 x_stop_queuing_selection_requests ();
667 return Qnil;
670 /* Return some frame whose display info is DPYINFO.
671 Return nil if there is none. */
673 static Lisp_Object
674 some_frame_on_display (dpyinfo)
675 struct x_display_info *dpyinfo;
677 Lisp_Object list, frame;
679 FOR_EACH_FRAME (list, frame)
681 if (FRAME_X_P (XFRAME (frame))
682 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
683 return frame;
686 return Qnil;
689 /* Send the reply to a selection request event EVENT.
690 TYPE is the type of selection data requested.
691 DATA and SIZE describe the data to send, already converted.
692 FORMAT is the unit-size (in bits) of the data to be transmitted. */
694 #ifdef TRACE_SELECTION
695 static int x_reply_selection_request_cnt;
696 #endif /* TRACE_SELECTION */
698 static void
699 x_reply_selection_request (event, format, data, size, type)
700 struct input_event *event;
701 int format, size;
702 unsigned char *data;
703 Atom type;
705 XSelectionEvent reply;
706 Display *display = SELECTION_EVENT_DISPLAY (event);
707 Window window = SELECTION_EVENT_REQUESTOR (event);
708 int bytes_remaining;
709 int format_bytes = format/8;
710 int max_bytes = SELECTION_QUANTUM (display);
711 struct x_display_info *dpyinfo = x_display_info_for_display (display);
712 int count = SPECPDL_INDEX ();
714 if (max_bytes > MAX_SELECTION_QUANTUM)
715 max_bytes = MAX_SELECTION_QUANTUM;
717 reply.type = SelectionNotify;
718 reply.display = display;
719 reply.requestor = window;
720 reply.selection = SELECTION_EVENT_SELECTION (event);
721 reply.time = SELECTION_EVENT_TIME (event);
722 reply.target = SELECTION_EVENT_TARGET (event);
723 reply.property = SELECTION_EVENT_PROPERTY (event);
724 if (reply.property == None)
725 reply.property = reply.target;
727 BLOCK_INPUT;
728 /* The protected block contains wait_for_property_change, which can
729 run random lisp code (process handlers) or signal. Therefore, we
730 put the x_uncatch_errors call in an unwind. */
731 record_unwind_protect (x_catch_errors_unwind, Qnil);
732 x_catch_errors (display);
734 #ifdef TRACE_SELECTION
736 char *sel = XGetAtomName (display, reply.selection);
737 char *tgt = XGetAtomName (display, reply.target);
738 TRACE3 ("%s, target %s (%d)", sel, tgt, ++x_reply_selection_request_cnt);
739 if (sel) XFree (sel);
740 if (tgt) XFree (tgt);
742 #endif /* TRACE_SELECTION */
744 /* Store the data on the requested property.
745 If the selection is large, only store the first N bytes of it.
747 bytes_remaining = size * format_bytes;
748 if (bytes_remaining <= max_bytes)
750 /* Send all the data at once, with minimal handshaking. */
751 TRACE1 ("Sending all %d bytes", bytes_remaining);
752 XChangeProperty (display, window, reply.property, type, format,
753 PropModeReplace, data, size);
754 /* At this point, the selection was successfully stored; ack it. */
755 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
757 else
759 /* Send an INCR selection. */
760 struct prop_location *wait_object;
761 int had_errors;
762 Lisp_Object frame;
764 frame = some_frame_on_display (dpyinfo);
766 /* If the display no longer has frames, we can't expect
767 to get many more selection requests from it, so don't
768 bother trying to queue them. */
769 if (!NILP (frame))
771 x_start_queuing_selection_requests ();
773 record_unwind_protect (queue_selection_requests_unwind,
774 Qnil);
777 if (x_window_to_frame (dpyinfo, window)) /* #### debug */
778 error ("Attempt to transfer an INCR to ourself!");
780 TRACE2 ("Start sending %d bytes incrementally (%s)",
781 bytes_remaining, XGetAtomName (display, reply.property));
782 wait_object = expect_property_change (display, window, reply.property,
783 PropertyDelete);
785 TRACE1 ("Set %s to number of bytes to send",
786 XGetAtomName (display, reply.property));
788 /* XChangeProperty expects an array of long even if long is more than
789 32 bits. */
790 long value[1];
792 value[0] = bytes_remaining;
793 XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
794 32, PropModeReplace,
795 (unsigned char *) value, 1);
798 XSelectInput (display, window, PropertyChangeMask);
800 /* Tell 'em the INCR data is there... */
801 TRACE0 ("Send SelectionNotify event");
802 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
803 XFlush (display);
805 had_errors = x_had_errors_p (display);
806 UNBLOCK_INPUT;
808 /* First, wait for the requester to ack by deleting the property.
809 This can run random lisp code (process handlers) or signal. */
810 if (! had_errors)
812 TRACE1 ("Waiting for ACK (deletion of %s)",
813 XGetAtomName (display, reply.property));
814 wait_for_property_change (wait_object);
816 else
817 unexpect_property_change (wait_object);
819 TRACE0 ("Got ACK");
820 while (bytes_remaining)
822 int i = ((bytes_remaining < max_bytes)
823 ? bytes_remaining
824 : max_bytes);
826 BLOCK_INPUT;
828 wait_object
829 = expect_property_change (display, window, reply.property,
830 PropertyDelete);
832 TRACE1 ("Sending increment of %d bytes", i);
833 TRACE1 ("Set %s to increment data",
834 XGetAtomName (display, reply.property));
836 /* Append the next chunk of data to the property. */
837 XChangeProperty (display, window, reply.property, type, format,
838 PropModeAppend, data, i / format_bytes);
839 bytes_remaining -= i;
840 data += i;
841 XFlush (display);
842 had_errors = x_had_errors_p (display);
843 UNBLOCK_INPUT;
845 if (had_errors)
846 break;
848 /* Now wait for the requester to ack this chunk by deleting the
849 property. This can run random lisp code or signal. */
850 TRACE1 ("Waiting for increment ACK (deletion of %s)",
851 XGetAtomName (display, reply.property));
852 wait_for_property_change (wait_object);
855 /* Now write a zero-length chunk to the property to tell the
856 requester that we're done. */
857 BLOCK_INPUT;
858 if (! waiting_for_other_props_on_window (display, window))
859 XSelectInput (display, window, 0L);
861 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
862 XGetAtomName (display, reply.property));
863 XChangeProperty (display, window, reply.property, type, format,
864 PropModeReplace, data, 0);
865 TRACE0 ("Done sending incrementally");
868 /* rms, 2003-01-03: I think I have fixed this bug. */
869 /* The window we're communicating with may have been deleted
870 in the meantime (that's a real situation from a bug report).
871 In this case, there may be events in the event queue still
872 refering to the deleted window, and we'll get a BadWindow error
873 in XTread_socket when processing the events. I don't have
874 an idea how to fix that. gerd, 2001-01-98. */
875 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
876 delivered before uncatch errors. */
877 XSync (display, False);
878 UNBLOCK_INPUT;
880 /* GTK queues events in addition to the queue in Xlib. So we
881 UNBLOCK to enter the event loop and get possible errors delivered,
882 and then BLOCK again because x_uncatch_errors requires it. */
883 BLOCK_INPUT;
884 /* This calls x_uncatch_errors. */
885 unbind_to (count, Qnil);
886 UNBLOCK_INPUT;
889 /* Handle a SelectionRequest event EVENT.
890 This is called from keyboard.c when such an event is found in the queue. */
892 static void
893 x_handle_selection_request (event)
894 struct input_event *event;
896 struct gcpro gcpro1, gcpro2, gcpro3;
897 Lisp_Object local_selection_data;
898 Lisp_Object selection_symbol;
899 Lisp_Object target_symbol;
900 Lisp_Object converted_selection;
901 Time local_selection_time;
902 Lisp_Object successful_p;
903 int count;
904 struct x_display_info *dpyinfo
905 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
907 TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
908 (unsigned long) SELECTION_EVENT_REQUESTOR (event),
909 (unsigned long) SELECTION_EVENT_TIME (event));
911 local_selection_data = Qnil;
912 target_symbol = Qnil;
913 converted_selection = Qnil;
914 successful_p = Qnil;
916 GCPRO3 (local_selection_data, converted_selection, target_symbol);
918 selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
919 SELECTION_EVENT_SELECTION (event));
921 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
923 if (NILP (local_selection_data))
925 /* Someone asked for the selection, but we don't have it any more.
927 x_decline_selection_request (event);
928 goto DONE;
931 local_selection_time = (Time)
932 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
934 if (SELECTION_EVENT_TIME (event) != CurrentTime
935 && local_selection_time > SELECTION_EVENT_TIME (event))
937 /* Someone asked for the selection, and we have one, but not the one
938 they're looking for.
940 x_decline_selection_request (event);
941 goto DONE;
944 x_selection_current_request = event;
945 count = SPECPDL_INDEX ();
946 selection_request_dpyinfo = dpyinfo;
947 record_unwind_protect (x_selection_request_lisp_error, Qnil);
949 target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
950 SELECTION_EVENT_TARGET (event));
952 #if 0 /* #### MULTIPLE doesn't work yet */
953 if (EQ (target_symbol, QMULTIPLE))
954 target_symbol = fetch_multiple_target (event);
955 #endif
957 /* Convert lisp objects back into binary data */
959 converted_selection
960 = x_get_local_selection (selection_symbol, target_symbol, 0);
962 if (! NILP (converted_selection))
964 unsigned char *data;
965 unsigned int size;
966 int format;
967 Atom type;
968 int nofree;
970 if (CONSP (converted_selection) && NILP (XCDR (converted_selection)))
972 x_decline_selection_request (event);
973 goto DONE2;
976 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
977 converted_selection,
978 &data, &type, &size, &format, &nofree);
980 x_reply_selection_request (event, format, data, size, type);
981 successful_p = Qt;
983 /* Indicate we have successfully processed this event. */
984 x_selection_current_request = 0;
986 /* Use xfree, not XFree, because lisp_data_to_selection_data
987 calls xmalloc itself. */
988 if (!nofree)
989 xfree (data);
992 DONE2:
993 unbind_to (count, Qnil);
995 DONE:
997 /* Let random lisp code notice that the selection has been asked for. */
999 Lisp_Object rest;
1000 rest = Vx_sent_selection_functions;
1001 if (!EQ (rest, Qunbound))
1002 for (; CONSP (rest); rest = Fcdr (rest))
1003 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
1006 UNGCPRO;
1009 /* Handle a SelectionClear event EVENT, which indicates that some
1010 client cleared out our previously asserted selection.
1011 This is called from keyboard.c when such an event is found in the queue. */
1013 static void
1014 x_handle_selection_clear (event)
1015 struct input_event *event;
1017 Display *display = SELECTION_EVENT_DISPLAY (event);
1018 Atom selection = SELECTION_EVENT_SELECTION (event);
1019 Time changed_owner_time = SELECTION_EVENT_TIME (event);
1021 Lisp_Object selection_symbol, local_selection_data;
1022 Time local_selection_time;
1023 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1024 struct x_display_info *t_dpyinfo;
1026 TRACE0 ("x_handle_selection_clear");
1028 #ifdef MULTI_KBOARD
1029 /* If the new selection owner is also Emacs,
1030 don't clear the new selection. */
1031 BLOCK_INPUT;
1032 /* Check each display on the same terminal,
1033 to see if this Emacs job now owns the selection
1034 through that display. */
1035 for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next)
1036 if (t_dpyinfo->terminal->kboard == dpyinfo->terminal->kboard)
1038 Window owner_window
1039 = XGetSelectionOwner (t_dpyinfo->display, selection);
1040 if (x_window_to_frame (t_dpyinfo, owner_window) != 0)
1042 UNBLOCK_INPUT;
1043 return;
1046 UNBLOCK_INPUT;
1047 #endif
1049 selection_symbol = x_atom_to_symbol (display, selection);
1051 local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);
1053 /* Well, we already believe that we don't own it, so that's just fine. */
1054 if (NILP (local_selection_data)) return;
1056 local_selection_time = (Time)
1057 cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
1059 /* This SelectionClear is for a selection that we no longer own, so we can
1060 disregard it. (That is, we have reasserted the selection since this
1061 request was generated.) */
1063 if (changed_owner_time != CurrentTime
1064 && local_selection_time > changed_owner_time)
1065 return;
1067 /* Otherwise, we're really honest and truly being told to drop it.
1068 Don't use Fdelq as that may QUIT;. */
1070 if (EQ (local_selection_data, Fcar (Vselection_alist)))
1071 Vselection_alist = Fcdr (Vselection_alist);
1072 else
1074 Lisp_Object rest;
1075 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
1076 if (EQ (local_selection_data, Fcar (XCDR (rest))))
1078 XSETCDR (rest, Fcdr (XCDR (rest)));
1079 break;
1083 /* Let random lisp code notice that the selection has been stolen. */
1086 Lisp_Object rest;
1087 rest = Vx_lost_selection_functions;
1088 if (!EQ (rest, Qunbound))
1090 for (; CONSP (rest); rest = Fcdr (rest))
1091 call1 (Fcar (rest), selection_symbol);
1092 prepare_menu_bars ();
1093 redisplay_preserve_echo_area (20);
1098 void
1099 x_handle_selection_event (event)
1100 struct input_event *event;
1102 TRACE0 ("x_handle_selection_event");
1104 if (event->kind == SELECTION_REQUEST_EVENT)
1106 if (x_queue_selection_requests)
1107 x_queue_event (event);
1108 else
1109 x_handle_selection_request (event);
1111 else
1112 x_handle_selection_clear (event);
1116 /* Clear all selections that were made from frame F.
1117 We do this when about to delete a frame. */
1119 void
1120 x_clear_frame_selections (f)
1121 FRAME_PTR f;
1123 Lisp_Object frame;
1124 Lisp_Object rest;
1126 XSETFRAME (frame, f);
1128 /* Otherwise, we're really honest and truly being told to drop it.
1129 Don't use Fdelq as that may QUIT;. */
1131 /* Delete elements from the beginning of Vselection_alist. */
1132 while (!NILP (Vselection_alist)
1133 && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist)))))))
1135 /* Let random Lisp code notice that the selection has been stolen. */
1136 Lisp_Object hooks, selection_symbol;
1138 hooks = Vx_lost_selection_functions;
1139 selection_symbol = Fcar (Fcar (Vselection_alist));
1141 if (!EQ (hooks, Qunbound))
1143 for (; CONSP (hooks); hooks = Fcdr (hooks))
1144 call1 (Fcar (hooks), selection_symbol);
1145 #if 0 /* This can crash when deleting a frame
1146 from x_connection_closed. Anyway, it seems unnecessary;
1147 something else should cause a redisplay. */
1148 redisplay_preserve_echo_area (21);
1149 #endif
1152 Vselection_alist = Fcdr (Vselection_alist);
1155 /* Delete elements after the beginning of Vselection_alist. */
1156 for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
1157 if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest))))))))
1159 /* Let random Lisp code notice that the selection has been stolen. */
1160 Lisp_Object hooks, selection_symbol;
1162 hooks = Vx_lost_selection_functions;
1163 selection_symbol = Fcar (Fcar (XCDR (rest)));
1165 if (!EQ (hooks, Qunbound))
1167 for (; CONSP (hooks); hooks = Fcdr (hooks))
1168 call1 (Fcar (hooks), selection_symbol);
1169 #if 0 /* See above */
1170 redisplay_preserve_echo_area (22);
1171 #endif
1173 XSETCDR (rest, Fcdr (XCDR (rest)));
1174 break;
1178 /* Nonzero if any properties for DISPLAY and WINDOW
1179 are on the list of what we are waiting for. */
1181 static int
1182 waiting_for_other_props_on_window (display, window)
1183 Display *display;
1184 Window window;
1186 struct prop_location *rest = property_change_wait_list;
1187 while (rest)
1188 if (rest->display == display && rest->window == window)
1189 return 1;
1190 else
1191 rest = rest->next;
1192 return 0;
1195 /* Add an entry to the list of property changes we are waiting for.
1196 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1197 The return value is a number that uniquely identifies
1198 this awaited property change. */
1200 static struct prop_location *
1201 expect_property_change (display, window, property, state)
1202 Display *display;
1203 Window window;
1204 Atom property;
1205 int state;
1207 struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl);
1208 pl->identifier = ++prop_location_identifier;
1209 pl->display = display;
1210 pl->window = window;
1211 pl->property = property;
1212 pl->desired_state = state;
1213 pl->next = property_change_wait_list;
1214 pl->arrived = 0;
1215 property_change_wait_list = pl;
1216 return pl;
1219 /* Delete an entry from the list of property changes we are waiting for.
1220 IDENTIFIER is the number that uniquely identifies the entry. */
1222 static void
1223 unexpect_property_change (location)
1224 struct prop_location *location;
1226 struct prop_location *prev = 0, *rest = property_change_wait_list;
1227 while (rest)
1229 if (rest == location)
1231 if (prev)
1232 prev->next = rest->next;
1233 else
1234 property_change_wait_list = rest->next;
1235 xfree (rest);
1236 return;
1238 prev = rest;
1239 rest = rest->next;
1243 /* Remove the property change expectation element for IDENTIFIER. */
1245 static Lisp_Object
1246 wait_for_property_change_unwind (loc)
1247 Lisp_Object loc;
1249 struct prop_location *location = XSAVE_VALUE (loc)->pointer;
1251 unexpect_property_change (location);
1252 if (location == property_change_reply_object)
1253 property_change_reply_object = 0;
1254 return Qnil;
1257 /* Actually wait for a property change.
1258 IDENTIFIER should be the value that expect_property_change returned. */
1260 static void
1261 wait_for_property_change (location)
1262 struct prop_location *location;
1264 int secs, usecs;
1265 int count = SPECPDL_INDEX ();
1267 if (property_change_reply_object)
1268 abort ();
1270 /* Make sure to do unexpect_property_change if we quit or err. */
1271 record_unwind_protect (wait_for_property_change_unwind,
1272 make_save_value (location, 0));
1274 XSETCAR (property_change_reply, Qnil);
1275 property_change_reply_object = location;
1277 /* If the event we are waiting for arrives beyond here, it will set
1278 property_change_reply, because property_change_reply_object says so. */
1279 if (! location->arrived)
1281 secs = x_selection_timeout / 1000;
1282 usecs = (x_selection_timeout % 1000) * 1000;
1283 TRACE2 (" Waiting %d secs, %d usecs", secs, usecs);
1284 wait_reading_process_output (secs, usecs, 0, 0,
1285 property_change_reply, NULL, 0);
1287 if (NILP (XCAR (property_change_reply)))
1289 TRACE0 (" Timed out");
1290 error ("Timed out waiting for property-notify event");
1294 unbind_to (count, Qnil);
1297 /* Called from XTread_socket in response to a PropertyNotify event. */
1299 void
1300 x_handle_property_notify (event)
1301 XPropertyEvent *event;
1303 struct prop_location *prev = 0, *rest = property_change_wait_list;
1305 while (rest)
1307 if (!rest->arrived
1308 && rest->property == event->atom
1309 && rest->window == event->window
1310 && rest->display == event->display
1311 && rest->desired_state == event->state)
1313 TRACE2 ("Expected %s of property %s",
1314 (event->state == PropertyDelete ? "deletion" : "change"),
1315 XGetAtomName (event->display, event->atom));
1317 rest->arrived = 1;
1319 /* If this is the one wait_for_property_change is waiting for,
1320 tell it to wake up. */
1321 if (rest == property_change_reply_object)
1322 XSETCAR (property_change_reply, Qt);
1324 return;
1327 prev = rest;
1328 rest = rest->next;
1334 #if 0 /* #### MULTIPLE doesn't work yet */
1336 static Lisp_Object
1337 fetch_multiple_target (event)
1338 XSelectionRequestEvent *event;
1340 Display *display = event->display;
1341 Window window = event->requestor;
1342 Atom target = event->target;
1343 Atom selection_atom = event->selection;
1344 int result;
1346 return
1347 Fcons (QMULTIPLE,
1348 x_get_window_property_as_lisp_data (display, window, target,
1349 QMULTIPLE, selection_atom));
1352 static Lisp_Object
1353 copy_multiple_data (obj)
1354 Lisp_Object obj;
1356 Lisp_Object vec;
1357 int i;
1358 int size;
1359 if (CONSP (obj))
1360 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
1362 CHECK_VECTOR (obj);
1363 vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil);
1364 for (i = 0; i < size; i++)
1366 Lisp_Object vec2 = XVECTOR (obj)->contents [i];
1367 CHECK_VECTOR (vec2);
1368 if (XVECTOR (vec2)->size != 2)
1369 /* ??? Confusing error message */
1370 signal_error ("Vectors must be of length 2", vec2);
1371 XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil);
1372 XVECTOR (XVECTOR (vec)->contents [i])->contents [0]
1373 = XVECTOR (vec2)->contents [0];
1374 XVECTOR (XVECTOR (vec)->contents [i])->contents [1]
1375 = XVECTOR (vec2)->contents [1];
1377 return vec;
1380 #endif
1383 /* Variables for communication with x_handle_selection_notify. */
1384 static Atom reading_which_selection;
1385 static Lisp_Object reading_selection_reply;
1386 static Window reading_selection_window;
1388 /* Do protocol to read selection-data from the server.
1389 Converts this to Lisp data and returns it. */
1391 static Lisp_Object
1392 x_get_foreign_selection (selection_symbol, target_type, time_stamp)
1393 Lisp_Object selection_symbol, target_type, time_stamp;
1395 struct frame *sf = SELECTED_FRAME ();
1396 Window requestor_window;
1397 Display *display;
1398 struct x_display_info *dpyinfo;
1399 Time requestor_time = last_event_timestamp;
1400 Atom target_property;
1401 Atom selection_atom;
1402 Atom type_atom;
1403 int secs, usecs;
1404 int count = SPECPDL_INDEX ();
1405 Lisp_Object frame;
1407 if (! FRAME_X_P (sf))
1408 return Qnil;
1410 requestor_window = FRAME_X_WINDOW (sf);
1411 display = FRAME_X_DISPLAY (sf);
1412 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
1413 target_property = dpyinfo->Xatom_EMACS_TMP;
1414 selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol);
1416 if (CONSP (target_type))
1417 type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type));
1418 else
1419 type_atom = symbol_to_x_atom (dpyinfo, display, target_type);
1421 if (! NILP (time_stamp))
1423 if (CONSP (time_stamp))
1424 requestor_time = (Time) cons_to_long (time_stamp);
1425 else if (INTEGERP (time_stamp))
1426 requestor_time = (Time) XUINT (time_stamp);
1427 else if (FLOATP (time_stamp))
1428 requestor_time = (Time) XFLOAT_DATA (time_stamp);
1429 else
1430 error ("TIME_STAMP must be cons or number");
1433 BLOCK_INPUT;
1435 /* The protected block contains wait_reading_process_output, which
1436 can run random lisp code (process handlers) or signal.
1437 Therefore, we put the x_uncatch_errors call in an unwind. */
1438 record_unwind_protect (x_catch_errors_unwind, Qnil);
1439 x_catch_errors (display);
1441 TRACE2 ("Get selection %s, type %s",
1442 XGetAtomName (display, type_atom),
1443 XGetAtomName (display, target_property));
1445 XConvertSelection (display, selection_atom, type_atom, target_property,
1446 requestor_window, requestor_time);
1447 XFlush (display);
1449 /* Prepare to block until the reply has been read. */
1450 reading_selection_window = requestor_window;
1451 reading_which_selection = selection_atom;
1452 XSETCAR (reading_selection_reply, Qnil);
1454 frame = some_frame_on_display (dpyinfo);
1456 /* If the display no longer has frames, we can't expect
1457 to get many more selection requests from it, so don't
1458 bother trying to queue them. */
1459 if (!NILP (frame))
1461 x_start_queuing_selection_requests ();
1463 record_unwind_protect (queue_selection_requests_unwind,
1464 Qnil);
1466 UNBLOCK_INPUT;
1468 /* This allows quits. Also, don't wait forever. */
1469 secs = x_selection_timeout / 1000;
1470 usecs = (x_selection_timeout % 1000) * 1000;
1471 TRACE1 (" Start waiting %d secs for SelectionNotify", secs);
1472 wait_reading_process_output (secs, usecs, 0, 0,
1473 reading_selection_reply, NULL, 0);
1474 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply)));
1476 BLOCK_INPUT;
1477 if (x_had_errors_p (display))
1478 error ("Cannot get selection");
1479 /* This calls x_uncatch_errors. */
1480 unbind_to (count, Qnil);
1481 UNBLOCK_INPUT;
1483 if (NILP (XCAR (reading_selection_reply)))
1484 error ("Timed out waiting for reply from selection owner");
1485 if (EQ (XCAR (reading_selection_reply), Qlambda))
1486 error ("No `%s' selection", SDATA (SYMBOL_NAME (selection_symbol)));
1488 /* Otherwise, the selection is waiting for us on the requested property. */
1489 return
1490 x_get_window_property_as_lisp_data (display, requestor_window,
1491 target_property, target_type,
1492 selection_atom);
1495 /* Subroutines of x_get_window_property_as_lisp_data */
1497 /* Use xfree, not XFree, to free the data obtained with this function. */
1499 static void
1500 x_get_window_property (display, window, property, data_ret, bytes_ret,
1501 actual_type_ret, actual_format_ret, actual_size_ret,
1502 delete_p)
1503 Display *display;
1504 Window window;
1505 Atom property;
1506 unsigned char **data_ret;
1507 int *bytes_ret;
1508 Atom *actual_type_ret;
1509 int *actual_format_ret;
1510 unsigned long *actual_size_ret;
1511 int delete_p;
1513 int total_size;
1514 unsigned long bytes_remaining;
1515 int offset = 0;
1516 unsigned char *tmp_data = 0;
1517 int result;
1518 int buffer_size = SELECTION_QUANTUM (display);
1520 if (buffer_size > MAX_SELECTION_QUANTUM)
1521 buffer_size = MAX_SELECTION_QUANTUM;
1523 BLOCK_INPUT;
1525 /* First probe the thing to find out how big it is. */
1526 result = XGetWindowProperty (display, window, property,
1527 0L, 0L, False, AnyPropertyType,
1528 actual_type_ret, actual_format_ret,
1529 actual_size_ret,
1530 &bytes_remaining, &tmp_data);
1531 if (result != Success)
1533 UNBLOCK_INPUT;
1534 *data_ret = 0;
1535 *bytes_ret = 0;
1536 return;
1539 /* This was allocated by Xlib, so use XFree. */
1540 XFree ((char *) tmp_data);
1542 if (*actual_type_ret == None || *actual_format_ret == 0)
1544 UNBLOCK_INPUT;
1545 return;
1548 total_size = bytes_remaining + 1;
1549 *data_ret = (unsigned char *) xmalloc (total_size);
1551 /* Now read, until we've gotten it all. */
1552 while (bytes_remaining)
1554 #ifdef TRACE_SELECTION
1555 int last = bytes_remaining;
1556 #endif
1557 result
1558 = XGetWindowProperty (display, window, property,
1559 (long)offset/4, (long)buffer_size/4,
1560 False,
1561 AnyPropertyType,
1562 actual_type_ret, actual_format_ret,
1563 actual_size_ret, &bytes_remaining, &tmp_data);
1565 TRACE2 ("Read %ld bytes from property %s",
1566 last - bytes_remaining,
1567 XGetAtomName (display, property));
1569 /* If this doesn't return Success at this point, it means that
1570 some clod deleted the selection while we were in the midst of
1571 reading it. Deal with that, I guess.... */
1572 if (result != Success)
1573 break;
1575 /* The man page for XGetWindowProperty says:
1576 "If the returned format is 32, the returned data is represented
1577 as a long array and should be cast to that type to obtain the
1578 elements."
1579 This applies even if long is more than 32 bits, the X library
1580 converts from 32 bit elements received from the X server to long
1581 and passes the long array to us. Thus, for that case bcopy can not
1582 be used. We convert to a 32 bit type here, because so much code
1583 assume on that.
1585 The bytes and offsets passed to XGetWindowProperty refers to the
1586 property and those are indeed in 32 bit quantities if format is 32. */
1588 if (*actual_format_ret == 32 && *actual_format_ret < BITS_PER_LONG)
1590 unsigned long i;
1591 int *idata = (int *) ((*data_ret) + offset);
1592 long *ldata = (long *) tmp_data;
1594 for (i = 0; i < *actual_size_ret; ++i)
1596 idata[i]= (int) ldata[i];
1597 offset += 4;
1600 else
1602 *actual_size_ret *= *actual_format_ret / 8;
1603 bcopy (tmp_data, (*data_ret) + offset, *actual_size_ret);
1604 offset += *actual_size_ret;
1607 /* This was allocated by Xlib, so use XFree. */
1608 XFree ((char *) tmp_data);
1611 XFlush (display);
1612 UNBLOCK_INPUT;
1613 *bytes_ret = offset;
1616 /* Use xfree, not XFree, to free the data obtained with this function. */
1618 static void
1619 receive_incremental_selection (display, window, property, target_type,
1620 min_size_bytes, data_ret, size_bytes_ret,
1621 type_ret, format_ret, size_ret)
1622 Display *display;
1623 Window window;
1624 Atom property;
1625 Lisp_Object target_type; /* for error messages only */
1626 unsigned int min_size_bytes;
1627 unsigned char **data_ret;
1628 int *size_bytes_ret;
1629 Atom *type_ret;
1630 unsigned long *size_ret;
1631 int *format_ret;
1633 int offset = 0;
1634 struct prop_location *wait_object;
1635 *size_bytes_ret = min_size_bytes;
1636 *data_ret = (unsigned char *) xmalloc (*size_bytes_ret);
1638 TRACE1 ("Read %d bytes incrementally", min_size_bytes);
1640 /* At this point, we have read an INCR property.
1641 Delete the property to ack it.
1642 (But first, prepare to receive the next event in this handshake.)
1644 Now, we must loop, waiting for the sending window to put a value on
1645 that property, then reading the property, then deleting it to ack.
1646 We are done when the sender places a property of length 0.
1648 BLOCK_INPUT;
1649 XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask);
1650 TRACE1 (" Delete property %s",
1651 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1652 XDeleteProperty (display, window, property);
1653 TRACE1 (" Expect new value of property %s",
1654 SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property))));
1655 wait_object = expect_property_change (display, window, property,
1656 PropertyNewValue);
1657 XFlush (display);
1658 UNBLOCK_INPUT;
1660 while (1)
1662 unsigned char *tmp_data;
1663 int tmp_size_bytes;
1665 TRACE0 (" Wait for property change");
1666 wait_for_property_change (wait_object);
1668 /* expect it again immediately, because x_get_window_property may
1669 .. no it won't, I don't get it.
1670 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1671 TRACE0 (" Get property value");
1672 x_get_window_property (display, window, property,
1673 &tmp_data, &tmp_size_bytes,
1674 type_ret, format_ret, size_ret, 1);
1676 TRACE1 (" Read increment of %d bytes", tmp_size_bytes);
1678 if (tmp_size_bytes == 0) /* we're done */
1680 TRACE0 ("Done reading incrementally");
1682 if (! waiting_for_other_props_on_window (display, window))
1683 XSelectInput (display, window, STANDARD_EVENT_SET);
1684 /* Use xfree, not XFree, because x_get_window_property
1685 calls xmalloc itself. */
1686 if (tmp_data) xfree (tmp_data);
1687 break;
1690 BLOCK_INPUT;
1691 TRACE1 (" ACK by deleting property %s",
1692 XGetAtomName (display, property));
1693 XDeleteProperty (display, window, property);
1694 wait_object = expect_property_change (display, window, property,
1695 PropertyNewValue);
1696 XFlush (display);
1697 UNBLOCK_INPUT;
1699 if (*size_bytes_ret < offset + tmp_size_bytes)
1701 *size_bytes_ret = offset + tmp_size_bytes;
1702 *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret);
1705 bcopy (tmp_data, (*data_ret) + offset, tmp_size_bytes);
1706 offset += tmp_size_bytes;
1708 /* Use xfree, not XFree, because x_get_window_property
1709 calls xmalloc itself. */
1710 xfree (tmp_data);
1715 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1716 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1717 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1719 static Lisp_Object
1720 x_get_window_property_as_lisp_data (display, window, property, target_type,
1721 selection_atom)
1722 Display *display;
1723 Window window;
1724 Atom property;
1725 Lisp_Object target_type; /* for error messages only */
1726 Atom selection_atom; /* for error messages only */
1728 Atom actual_type;
1729 int actual_format;
1730 unsigned long actual_size;
1731 unsigned char *data = 0;
1732 int bytes = 0;
1733 Lisp_Object val;
1734 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1736 TRACE0 ("Reading selection data");
1738 x_get_window_property (display, window, property, &data, &bytes,
1739 &actual_type, &actual_format, &actual_size, 1);
1740 if (! data)
1742 int there_is_a_selection_owner;
1743 BLOCK_INPUT;
1744 there_is_a_selection_owner
1745 = XGetSelectionOwner (display, selection_atom);
1746 UNBLOCK_INPUT;
1747 if (there_is_a_selection_owner)
1748 signal_error ("Selection owner couldn't convert",
1749 actual_type
1750 ? list2 (target_type,
1751 x_atom_to_symbol (display, actual_type))
1752 : target_type);
1753 else
1754 signal_error ("No selection",
1755 x_atom_to_symbol (display, selection_atom));
1758 if (actual_type == dpyinfo->Xatom_INCR)
1760 /* That wasn't really the data, just the beginning. */
1762 unsigned int min_size_bytes = * ((unsigned int *) data);
1763 BLOCK_INPUT;
1764 /* Use xfree, not XFree, because x_get_window_property
1765 calls xmalloc itself. */
1766 xfree ((char *) data);
1767 UNBLOCK_INPUT;
1768 receive_incremental_selection (display, window, property, target_type,
1769 min_size_bytes, &data, &bytes,
1770 &actual_type, &actual_format,
1771 &actual_size);
1774 BLOCK_INPUT;
1775 TRACE1 (" Delete property %s", XGetAtomName (display, property));
1776 XDeleteProperty (display, window, property);
1777 XFlush (display);
1778 UNBLOCK_INPUT;
1780 /* It's been read. Now convert it to a lisp object in some semi-rational
1781 manner. */
1782 val = selection_data_to_lisp_data (display, data, bytes,
1783 actual_type, actual_format);
1785 /* Use xfree, not XFree, because x_get_window_property
1786 calls xmalloc itself. */
1787 xfree ((char *) data);
1788 return val;
1791 /* These functions convert from the selection data read from the server into
1792 something that we can use from Lisp, and vice versa.
1794 Type: Format: Size: Lisp Type:
1795 ----- ------- ----- -----------
1796 * 8 * String
1797 ATOM 32 1 Symbol
1798 ATOM 32 > 1 Vector of Symbols
1799 * 16 1 Integer
1800 * 16 > 1 Vector of Integers
1801 * 32 1 if <=16 bits: Integer
1802 if > 16 bits: Cons of top16, bot16
1803 * 32 > 1 Vector of the above
1805 When converting a Lisp number to C, it is assumed to be of format 16 if
1806 it is an integer, and of format 32 if it is a cons of two integers.
1808 When converting a vector of numbers from Lisp to C, it is assumed to be
1809 of format 16 if every element in the vector is an integer, and is assumed
1810 to be of format 32 if any element is a cons of two integers.
1812 When converting an object to C, it may be of the form (SYMBOL . <data>)
1813 where SYMBOL is what we should claim that the type is. Format and
1814 representation are as above.
1816 Important: When format is 32, data should contain an array of int,
1817 not an array of long as the X library returns. This makes a difference
1818 when sizeof(long) != sizeof(int). */
1822 static Lisp_Object
1823 selection_data_to_lisp_data (display, data, size, type, format)
1824 Display *display;
1825 unsigned char *data;
1826 Atom type;
1827 int size, format;
1829 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1831 if (type == dpyinfo->Xatom_NULL)
1832 return QNULL;
1834 /* Convert any 8-bit data to a string, for compactness. */
1835 else if (format == 8)
1837 Lisp_Object str, lispy_type;
1839 str = make_unibyte_string ((char *) data, size);
1840 /* Indicate that this string is from foreign selection by a text
1841 property `foreign-selection' so that the caller of
1842 x-get-selection-internal (usually x-get-selection) can know
1843 that the string must be decode. */
1844 if (type == dpyinfo->Xatom_COMPOUND_TEXT)
1845 lispy_type = QCOMPOUND_TEXT;
1846 else if (type == dpyinfo->Xatom_UTF8_STRING)
1847 lispy_type = QUTF8_STRING;
1848 else
1849 lispy_type = QSTRING;
1850 Fput_text_property (make_number (0), make_number (size),
1851 Qforeign_selection, lispy_type, str);
1852 return str;
1854 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1855 a vector of symbols.
1857 else if (type == XA_ATOM)
1859 int i;
1860 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1861 But the callers of these function has made sure the data for
1862 format == 32 is an array of int. Thus, use int instead
1863 of Atom. */
1864 int *idata = (int *) data;
1866 if (size == sizeof (int))
1867 return x_atom_to_symbol (display, (Atom) idata[0]);
1868 else
1870 Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)),
1871 make_number (0));
1872 for (i = 0; i < size / sizeof (int); i++)
1873 Faset (v, make_number (i),
1874 x_atom_to_symbol (display, (Atom) idata[i]));
1875 return v;
1879 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1880 If the number is 32 bits and won't fit in a Lisp_Int,
1881 convert it to a cons of integers, 16 bits in each half.
1883 else if (format == 32 && size == sizeof (int))
1884 return long_to_cons (((unsigned int *) data) [0]);
1885 else if (format == 16 && size == sizeof (short))
1886 return make_number ((int) (((unsigned short *) data) [0]));
1888 /* Convert any other kind of data to a vector of numbers, represented
1889 as above (as an integer, or a cons of two 16 bit integers.)
1891 else if (format == 16)
1893 int i;
1894 Lisp_Object v;
1895 v = Fmake_vector (make_number (size / 2), make_number (0));
1896 for (i = 0; i < size / 2; i++)
1898 int j = (int) ((unsigned short *) data) [i];
1899 Faset (v, make_number (i), make_number (j));
1901 return v;
1903 else
1905 int i;
1906 Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0));
1907 for (i = 0; i < size / 4; i++)
1909 unsigned int j = ((unsigned int *) data) [i];
1910 Faset (v, make_number (i), long_to_cons (j));
1912 return v;
1917 /* Use xfree, not XFree, to free the data obtained with this function. */
1919 static void
1920 lisp_data_to_selection_data (display, obj,
1921 data_ret, type_ret, size_ret,
1922 format_ret, nofree_ret)
1923 Display *display;
1924 Lisp_Object obj;
1925 unsigned char **data_ret;
1926 Atom *type_ret;
1927 unsigned int *size_ret;
1928 int *format_ret;
1929 int *nofree_ret;
1931 Lisp_Object type = Qnil;
1932 struct x_display_info *dpyinfo = x_display_info_for_display (display);
1934 *nofree_ret = 0;
1936 if (CONSP (obj) && SYMBOLP (XCAR (obj)))
1938 type = XCAR (obj);
1939 obj = XCDR (obj);
1940 if (CONSP (obj) && NILP (XCDR (obj)))
1941 obj = XCAR (obj);
1944 if (EQ (obj, QNULL) || (EQ (type, QNULL)))
1945 { /* This is not the same as declining */
1946 *format_ret = 32;
1947 *size_ret = 0;
1948 *data_ret = 0;
1949 type = QNULL;
1951 else if (STRINGP (obj))
1953 if (SCHARS (obj) < SBYTES (obj))
1954 /* OBJ is a multibyte string containing a non-ASCII char. */
1955 signal_error ("Non-ASCII string must be encoded in advance", obj);
1956 if (NILP (type))
1957 type = QSTRING;
1958 *format_ret = 8;
1959 *size_ret = SBYTES (obj);
1960 *data_ret = SDATA (obj);
1961 *nofree_ret = 1;
1963 else if (SYMBOLP (obj))
1965 *format_ret = 32;
1966 *size_ret = 1;
1967 *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1);
1968 (*data_ret) [sizeof (Atom)] = 0;
1969 (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj);
1970 if (NILP (type)) type = QATOM;
1972 else if (INTEGERP (obj)
1973 && XINT (obj) < 0xFFFF
1974 && XINT (obj) > -0xFFFF)
1976 *format_ret = 16;
1977 *size_ret = 1;
1978 *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1);
1979 (*data_ret) [sizeof (short)] = 0;
1980 (*(short **) data_ret) [0] = (short) XINT (obj);
1981 if (NILP (type)) type = QINTEGER;
1983 else if (INTEGERP (obj)
1984 || (CONSP (obj) && INTEGERP (XCAR (obj))
1985 && (INTEGERP (XCDR (obj))
1986 || (CONSP (XCDR (obj))
1987 && INTEGERP (XCAR (XCDR (obj)))))))
1989 *format_ret = 32;
1990 *size_ret = 1;
1991 *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1);
1992 (*data_ret) [sizeof (long)] = 0;
1993 (*(unsigned long **) data_ret) [0] = cons_to_long (obj);
1994 if (NILP (type)) type = QINTEGER;
1996 else if (VECTORP (obj))
1998 /* Lisp_Vectors may represent a set of ATOMs;
1999 a set of 16 or 32 bit INTEGERs;
2000 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
2002 int i;
2004 if (SYMBOLP (XVECTOR (obj)->contents [0]))
2005 /* This vector is an ATOM set */
2007 if (NILP (type)) type = QATOM;
2008 *size_ret = XVECTOR (obj)->size;
2009 *format_ret = 32;
2010 *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom));
2011 for (i = 0; i < *size_ret; i++)
2012 if (SYMBOLP (XVECTOR (obj)->contents [i]))
2013 (*(Atom **) data_ret) [i]
2014 = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]);
2015 else
2016 signal_error ("All elements of selection vector must have same type", obj);
2018 #if 0 /* #### MULTIPLE doesn't work yet */
2019 else if (VECTORP (XVECTOR (obj)->contents [0]))
2020 /* This vector is an ATOM_PAIR set */
2022 if (NILP (type)) type = QATOM_PAIR;
2023 *size_ret = XVECTOR (obj)->size;
2024 *format_ret = 32;
2025 *data_ret = (unsigned char *)
2026 xmalloc ((*size_ret) * sizeof (Atom) * 2);
2027 for (i = 0; i < *size_ret; i++)
2028 if (VECTORP (XVECTOR (obj)->contents [i]))
2030 Lisp_Object pair = XVECTOR (obj)->contents [i];
2031 if (XVECTOR (pair)->size != 2)
2032 signal_error (
2033 "Elements of the vector must be vectors of exactly two elements",
2034 pair);
2036 (*(Atom **) data_ret) [i * 2]
2037 = symbol_to_x_atom (dpyinfo, display,
2038 XVECTOR (pair)->contents [0]);
2039 (*(Atom **) data_ret) [(i * 2) + 1]
2040 = symbol_to_x_atom (dpyinfo, display,
2041 XVECTOR (pair)->contents [1]);
2043 else
2044 signal_error ("All elements of the vector must be of the same type",
2045 obj);
2048 #endif
2049 else
2050 /* This vector is an INTEGER set, or something like it */
2052 int data_size = 2;
2053 *size_ret = XVECTOR (obj)->size;
2054 if (NILP (type)) type = QINTEGER;
2055 *format_ret = 16;
2056 for (i = 0; i < *size_ret; i++)
2057 if (CONSP (XVECTOR (obj)->contents [i]))
2058 *format_ret = 32;
2059 else if (!INTEGERP (XVECTOR (obj)->contents [i]))
2060 signal_error (/* Qselection_error */
2061 "Elements of selection vector must be integers or conses of integers",
2062 obj);
2064 /* Use sizeof(long) even if it is more than 32 bits. See comment
2065 in x_get_window_property and x_fill_property_data. */
2067 if (*format_ret == 32) data_size = sizeof(long);
2068 *data_ret = (unsigned char *) xmalloc (*size_ret * data_size);
2069 for (i = 0; i < *size_ret; i++)
2070 if (*format_ret == 32)
2071 (*((unsigned long **) data_ret)) [i]
2072 = cons_to_long (XVECTOR (obj)->contents [i]);
2073 else
2074 (*((unsigned short **) data_ret)) [i]
2075 = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]);
2078 else
2079 signal_error (/* Qselection_error */ "Unrecognized selection data", obj);
2081 *type_ret = symbol_to_x_atom (dpyinfo, display, type);
2084 static Lisp_Object
2085 clean_local_selection_data (obj)
2086 Lisp_Object obj;
2088 if (CONSP (obj)
2089 && INTEGERP (XCAR (obj))
2090 && CONSP (XCDR (obj))
2091 && INTEGERP (XCAR (XCDR (obj)))
2092 && NILP (XCDR (XCDR (obj))))
2093 obj = Fcons (XCAR (obj), XCDR (obj));
2095 if (CONSP (obj)
2096 && INTEGERP (XCAR (obj))
2097 && INTEGERP (XCDR (obj)))
2099 if (XINT (XCAR (obj)) == 0)
2100 return XCDR (obj);
2101 if (XINT (XCAR (obj)) == -1)
2102 return make_number (- XINT (XCDR (obj)));
2104 if (VECTORP (obj))
2106 int i;
2107 int size = XVECTOR (obj)->size;
2108 Lisp_Object copy;
2109 if (size == 1)
2110 return clean_local_selection_data (XVECTOR (obj)->contents [0]);
2111 copy = Fmake_vector (make_number (size), Qnil);
2112 for (i = 0; i < size; i++)
2113 XVECTOR (copy)->contents [i]
2114 = clean_local_selection_data (XVECTOR (obj)->contents [i]);
2115 return copy;
2117 return obj;
2120 /* Called from XTread_socket to handle SelectionNotify events.
2121 If it's the selection we are waiting for, stop waiting
2122 by setting the car of reading_selection_reply to non-nil.
2123 We store t there if the reply is successful, lambda if not. */
2125 void
2126 x_handle_selection_notify (event)
2127 XSelectionEvent *event;
2129 if (event->requestor != reading_selection_window)
2130 return;
2131 if (event->selection != reading_which_selection)
2132 return;
2134 TRACE0 ("Received SelectionNotify");
2135 XSETCAR (reading_selection_reply,
2136 (event->property != 0 ? Qt : Qlambda));
2140 DEFUN ("x-own-selection-internal", Fx_own_selection_internal,
2141 Sx_own_selection_internal, 2, 2, 0,
2142 doc: /* Assert an X selection of the given TYPE with the given VALUE.
2143 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2144 \(Those are literal upper-case symbol names, since that's what X expects.)
2145 VALUE is typically a string, or a cons of two markers, but may be
2146 anything that the functions on `selection-converter-alist' know about. */)
2147 (selection_name, selection_value)
2148 Lisp_Object selection_name, selection_value;
2150 check_x ();
2151 CHECK_SYMBOL (selection_name);
2152 if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil");
2153 x_own_selection (selection_name, selection_value);
2154 return selection_value;
2158 /* Request the selection value from the owner. If we are the owner,
2159 simply return our selection value. If we are not the owner, this
2160 will block until all of the data has arrived. */
2162 DEFUN ("x-get-selection-internal", Fx_get_selection_internal,
2163 Sx_get_selection_internal, 2, 3, 0,
2164 doc: /* Return text selected from some X window.
2165 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2166 \(Those are literal upper-case symbol names, since that's what X expects.)
2167 TYPE is the type of data desired, typically `STRING'.
2168 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2169 selections. If omitted, defaults to the time for the last event. */)
2170 (selection_symbol, target_type, time_stamp)
2171 Lisp_Object selection_symbol, target_type, time_stamp;
2173 Lisp_Object val = Qnil;
2174 struct gcpro gcpro1, gcpro2;
2175 GCPRO2 (target_type, val); /* we store newly consed data into these */
2176 check_x ();
2177 CHECK_SYMBOL (selection_symbol);
2179 #if 0 /* #### MULTIPLE doesn't work yet */
2180 if (CONSP (target_type)
2181 && XCAR (target_type) == QMULTIPLE)
2183 CHECK_VECTOR (XCDR (target_type));
2184 /* So we don't destructively modify this... */
2185 target_type = copy_multiple_data (target_type);
2187 else
2188 #endif
2189 CHECK_SYMBOL (target_type);
2191 val = x_get_local_selection (selection_symbol, target_type, 1);
2193 if (NILP (val))
2195 val = x_get_foreign_selection (selection_symbol, target_type, time_stamp);
2196 goto DONE;
2199 if (CONSP (val)
2200 && SYMBOLP (XCAR (val)))
2202 val = XCDR (val);
2203 if (CONSP (val) && NILP (XCDR (val)))
2204 val = XCAR (val);
2206 val = clean_local_selection_data (val);
2207 DONE:
2208 UNGCPRO;
2209 return val;
2212 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal,
2213 Sx_disown_selection_internal, 1, 2, 0,
2214 doc: /* If we own the selection SELECTION, disown it.
2215 Disowning it means there is no such selection. */)
2216 (selection, time)
2217 Lisp_Object selection;
2218 Lisp_Object time;
2220 Time timestamp;
2221 Atom selection_atom;
2222 union {
2223 struct selection_input_event sie;
2224 struct input_event ie;
2225 } event;
2226 Display *display;
2227 struct x_display_info *dpyinfo;
2228 struct frame *sf = SELECTED_FRAME ();
2230 check_x ();
2231 if (! FRAME_X_P (sf))
2232 return Qnil;
2234 display = FRAME_X_DISPLAY (sf);
2235 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2236 CHECK_SYMBOL (selection);
2237 if (NILP (time))
2238 timestamp = last_event_timestamp;
2239 else
2240 timestamp = cons_to_long (time);
2242 if (NILP (assq_no_quit (selection, Vselection_alist)))
2243 return Qnil; /* Don't disown the selection when we're not the owner. */
2245 selection_atom = symbol_to_x_atom (dpyinfo, display, selection);
2247 BLOCK_INPUT;
2248 XSetSelectionOwner (display, selection_atom, None, timestamp);
2249 UNBLOCK_INPUT;
2251 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2252 generated for a window which owns the selection when that window sets
2253 the selection owner to None. The NCD server does, the MIT Sun4 server
2254 doesn't. So we synthesize one; this means we might get two, but
2255 that's ok, because the second one won't have any effect. */
2256 SELECTION_EVENT_DISPLAY (&event.sie) = display;
2257 SELECTION_EVENT_SELECTION (&event.sie) = selection_atom;
2258 SELECTION_EVENT_TIME (&event.sie) = timestamp;
2259 x_handle_selection_clear (&event.ie);
2261 return Qt;
2264 /* Get rid of all the selections in buffer BUFFER.
2265 This is used when we kill a buffer. */
2267 void
2268 x_disown_buffer_selections (buffer)
2269 Lisp_Object buffer;
2271 Lisp_Object tail;
2272 struct buffer *buf = XBUFFER (buffer);
2274 for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail))
2276 Lisp_Object elt, value;
2277 elt = XCAR (tail);
2278 value = XCDR (elt);
2279 if (CONSP (value) && MARKERP (XCAR (value))
2280 && XMARKER (XCAR (value))->buffer == buf)
2281 Fx_disown_selection_internal (XCAR (elt), Qnil);
2285 DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p,
2286 0, 1, 0,
2287 doc: /* Whether the current Emacs process owns the given X Selection.
2288 The arg should be the name of the selection in question, typically one of
2289 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2290 \(Those are literal upper-case symbol names, since that's what X expects.)
2291 For convenience, the symbol nil is the same as `PRIMARY',
2292 and t is the same as `SECONDARY'. */)
2293 (selection)
2294 Lisp_Object selection;
2296 check_x ();
2297 CHECK_SYMBOL (selection);
2298 if (EQ (selection, Qnil)) selection = QPRIMARY;
2299 if (EQ (selection, Qt)) selection = QSECONDARY;
2301 if (NILP (Fassq (selection, Vselection_alist)))
2302 return Qnil;
2303 return Qt;
2306 DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p,
2307 0, 1, 0,
2308 doc: /* Whether there is an owner for the given X Selection.
2309 The arg should be the name of the selection in question, typically one of
2310 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2311 \(Those are literal upper-case symbol names, since that's what X expects.)
2312 For convenience, the symbol nil is the same as `PRIMARY',
2313 and t is the same as `SECONDARY'. */)
2314 (selection)
2315 Lisp_Object selection;
2317 Window owner;
2318 Atom atom;
2319 Display *dpy;
2320 struct frame *sf = SELECTED_FRAME ();
2322 /* It should be safe to call this before we have an X frame. */
2323 if (! FRAME_X_P (sf))
2324 return Qnil;
2326 dpy = FRAME_X_DISPLAY (sf);
2327 CHECK_SYMBOL (selection);
2328 if (!NILP (Fx_selection_owner_p (selection)))
2329 return Qt;
2330 if (EQ (selection, Qnil)) selection = QPRIMARY;
2331 if (EQ (selection, Qt)) selection = QSECONDARY;
2332 atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection);
2333 if (atom == 0)
2334 return Qnil;
2335 BLOCK_INPUT;
2336 owner = XGetSelectionOwner (dpy, atom);
2337 UNBLOCK_INPUT;
2338 return (owner ? Qt : Qnil);
2342 #ifdef CUT_BUFFER_SUPPORT
2344 /* Ensure that all 8 cut buffers exist. ICCCM says we gotta... */
2345 static void
2346 initialize_cut_buffers (display, window)
2347 Display *display;
2348 Window window;
2350 unsigned char *data = (unsigned char *) "";
2351 BLOCK_INPUT;
2352 #define FROB(atom) XChangeProperty (display, window, atom, XA_STRING, 8, \
2353 PropModeAppend, data, 0)
2354 FROB (XA_CUT_BUFFER0);
2355 FROB (XA_CUT_BUFFER1);
2356 FROB (XA_CUT_BUFFER2);
2357 FROB (XA_CUT_BUFFER3);
2358 FROB (XA_CUT_BUFFER4);
2359 FROB (XA_CUT_BUFFER5);
2360 FROB (XA_CUT_BUFFER6);
2361 FROB (XA_CUT_BUFFER7);
2362 #undef FROB
2363 UNBLOCK_INPUT;
2367 #define CHECK_CUT_BUFFER(symbol) \
2368 do { CHECK_SYMBOL ((symbol)); \
2369 if (!EQ((symbol), QCUT_BUFFER0) && !EQ((symbol), QCUT_BUFFER1) \
2370 && !EQ((symbol), QCUT_BUFFER2) && !EQ((symbol), QCUT_BUFFER3) \
2371 && !EQ((symbol), QCUT_BUFFER4) && !EQ((symbol), QCUT_BUFFER5) \
2372 && !EQ((symbol), QCUT_BUFFER6) && !EQ((symbol), QCUT_BUFFER7)) \
2373 signal_error ("Doesn't name a cut buffer", (symbol)); \
2374 } while (0)
2376 DEFUN ("x-get-cut-buffer-internal", Fx_get_cut_buffer_internal,
2377 Sx_get_cut_buffer_internal, 1, 1, 0,
2378 doc: /* Returns the value of the named cut buffer (typically CUT_BUFFER0). */)
2379 (buffer)
2380 Lisp_Object buffer;
2382 Window window;
2383 Atom buffer_atom;
2384 unsigned char *data;
2385 int bytes;
2386 Atom type;
2387 int format;
2388 unsigned long size;
2389 Lisp_Object ret;
2390 Display *display;
2391 struct x_display_info *dpyinfo;
2392 struct frame *sf = SELECTED_FRAME ();
2394 check_x ();
2396 if (! FRAME_X_P (sf))
2397 return Qnil;
2399 display = FRAME_X_DISPLAY (sf);
2400 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
2401 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2402 CHECK_CUT_BUFFER (buffer);
2403 buffer_atom = symbol_to_x_atom (dpyinfo, display, buffer);
2405 x_get_window_property (display, window, buffer_atom, &data, &bytes,
2406 &type, &format, &size, 0);
2407 if (!data || !format)
2408 return Qnil;
2410 if (format != 8 || type != XA_STRING)
2411 signal_error ("Cut buffer doesn't contain 8-bit data",
2412 list2 (x_atom_to_symbol (display, type),
2413 make_number (format)));
2415 ret = (bytes ? make_unibyte_string ((char *) data, bytes) : Qnil);
2416 /* Use xfree, not XFree, because x_get_window_property
2417 calls xmalloc itself. */
2418 xfree (data);
2419 return ret;
2423 DEFUN ("x-store-cut-buffer-internal", Fx_store_cut_buffer_internal,
2424 Sx_store_cut_buffer_internal, 2, 2, 0,
2425 doc: /* Sets the value of the named cut buffer (typically CUT_BUFFER0). */)
2426 (buffer, string)
2427 Lisp_Object buffer, string;
2429 Window window;
2430 Atom buffer_atom;
2431 unsigned char *data;
2432 int bytes;
2433 int bytes_remaining;
2434 int max_bytes;
2435 Display *display;
2436 struct frame *sf = SELECTED_FRAME ();
2438 check_x ();
2440 if (! FRAME_X_P (sf))
2441 return Qnil;
2443 display = FRAME_X_DISPLAY (sf);
2444 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2446 max_bytes = SELECTION_QUANTUM (display);
2447 if (max_bytes > MAX_SELECTION_QUANTUM)
2448 max_bytes = MAX_SELECTION_QUANTUM;
2450 CHECK_CUT_BUFFER (buffer);
2451 CHECK_STRING (string);
2452 buffer_atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf),
2453 display, buffer);
2454 data = (unsigned char *) SDATA (string);
2455 bytes = SBYTES (string);
2456 bytes_remaining = bytes;
2458 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
2460 initialize_cut_buffers (display, window);
2461 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
2464 BLOCK_INPUT;
2466 /* Don't mess up with an empty value. */
2467 if (!bytes_remaining)
2468 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2469 PropModeReplace, data, 0);
2471 while (bytes_remaining)
2473 int chunk = (bytes_remaining < max_bytes
2474 ? bytes_remaining : max_bytes);
2475 XChangeProperty (display, window, buffer_atom, XA_STRING, 8,
2476 (bytes_remaining == bytes
2477 ? PropModeReplace
2478 : PropModeAppend),
2479 data, chunk);
2480 data += chunk;
2481 bytes_remaining -= chunk;
2483 UNBLOCK_INPUT;
2484 return string;
2488 DEFUN ("x-rotate-cut-buffers-internal", Fx_rotate_cut_buffers_internal,
2489 Sx_rotate_cut_buffers_internal, 1, 1, 0,
2490 doc: /* Rotate the values of the cut buffers by the given number of step.
2491 Positive means shift the values forward, negative means backward. */)
2493 Lisp_Object n;
2495 Window window;
2496 Atom props[8];
2497 Display *display;
2498 struct frame *sf = SELECTED_FRAME ();
2500 check_x ();
2502 if (! FRAME_X_P (sf))
2503 return Qnil;
2505 display = FRAME_X_DISPLAY (sf);
2506 window = RootWindow (display, 0); /* Cut buffers are on screen 0 */
2507 CHECK_NUMBER (n);
2508 if (XINT (n) == 0)
2509 return n;
2510 if (! FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized)
2512 initialize_cut_buffers (display, window);
2513 FRAME_X_DISPLAY_INFO (sf)->cut_buffers_initialized = 1;
2516 props[0] = XA_CUT_BUFFER0;
2517 props[1] = XA_CUT_BUFFER1;
2518 props[2] = XA_CUT_BUFFER2;
2519 props[3] = XA_CUT_BUFFER3;
2520 props[4] = XA_CUT_BUFFER4;
2521 props[5] = XA_CUT_BUFFER5;
2522 props[6] = XA_CUT_BUFFER6;
2523 props[7] = XA_CUT_BUFFER7;
2524 BLOCK_INPUT;
2525 XRotateWindowProperties (display, window, props, 8, XINT (n));
2526 UNBLOCK_INPUT;
2527 return n;
2530 #endif
2532 /***********************************************************************
2533 Drag and drop support
2534 ***********************************************************************/
2535 /* Check that lisp values are of correct type for x_fill_property_data.
2536 That is, number, string or a cons with two numbers (low and high 16
2537 bit parts of a 32 bit number). */
2540 x_check_property_data (data)
2541 Lisp_Object data;
2543 Lisp_Object iter;
2544 int size = 0;
2546 for (iter = data; CONSP (iter) && size != -1; iter = XCDR (iter), ++size)
2548 Lisp_Object o = XCAR (iter);
2550 if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o))
2551 size = -1;
2552 else if (CONSP (o) &&
2553 (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o))))
2554 size = -1;
2557 return size;
2560 /* Convert lisp values to a C array. Values may be a number, a string
2561 which is taken as an X atom name and converted to the atom value, or
2562 a cons containing the two 16 bit parts of a 32 bit number.
2564 DPY is the display use to look up X atoms.
2565 DATA is a Lisp list of values to be converted.
2566 RET is the C array that contains the converted values. It is assumed
2567 it is big enough to hold all values.
2568 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2569 be stored in RET. Note that long is used for 32 even if long is more
2570 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2571 XClientMessageEvent). */
2573 void
2574 x_fill_property_data (dpy, data, ret, format)
2575 Display *dpy;
2576 Lisp_Object data;
2577 void *ret;
2578 int format;
2580 long val;
2581 long *d32 = (long *) ret;
2582 short *d16 = (short *) ret;
2583 char *d08 = (char *) ret;
2584 Lisp_Object iter;
2586 for (iter = data; CONSP (iter); iter = XCDR (iter))
2588 Lisp_Object o = XCAR (iter);
2590 if (INTEGERP (o))
2591 val = (long) XFASTINT (o);
2592 else if (FLOATP (o))
2593 val = (long) XFLOAT_DATA (o);
2594 else if (CONSP (o))
2595 val = (long) cons_to_long (o);
2596 else if (STRINGP (o))
2598 BLOCK_INPUT;
2599 val = (long) XInternAtom (dpy, (char *) SDATA (o), False);
2600 UNBLOCK_INPUT;
2602 else
2603 error ("Wrong type, must be string, number or cons");
2605 if (format == 8)
2606 *d08++ = (char) val;
2607 else if (format == 16)
2608 *d16++ = (short) val;
2609 else
2610 *d32++ = val;
2614 /* Convert an array of C values to a Lisp list.
2615 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2616 DATA is a C array of values to be converted.
2617 TYPE is the type of the data. Only XA_ATOM is special, it converts
2618 each number in DATA to its corresponfing X atom as a symbol.
2619 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2620 be stored in RET.
2621 SIZE is the number of elements in DATA.
2623 Important: When format is 32, data should contain an array of int,
2624 not an array of long as the X library returns. This makes a difference
2625 when sizeof(long) != sizeof(int).
2627 Also see comment for selection_data_to_lisp_data above. */
2629 Lisp_Object
2630 x_property_data_to_lisp (f, data, type, format, size)
2631 struct frame *f;
2632 unsigned char *data;
2633 Atom type;
2634 int format;
2635 unsigned long size;
2637 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f),
2638 data, size*format/8, type, format);
2641 /* Get the mouse position in frame relative coordinates. */
2643 static void
2644 mouse_position_for_drop (f, x, y)
2645 FRAME_PTR f;
2646 int *x;
2647 int *y;
2649 Window root, dummy_window;
2650 int dummy;
2652 BLOCK_INPUT;
2654 XQueryPointer (FRAME_X_DISPLAY (f),
2655 DefaultRootWindow (FRAME_X_DISPLAY (f)),
2657 /* The root window which contains the pointer. */
2658 &root,
2660 /* Window pointer is on, not used */
2661 &dummy_window,
2663 /* The position on that root window. */
2664 x, y,
2666 /* x/y in dummy_window coordinates, not used. */
2667 &dummy, &dummy,
2669 /* Modifier keys and pointer buttons, about which
2670 we don't care. */
2671 (unsigned int *) &dummy);
2674 /* Absolute to relative. */
2675 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
2676 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
2678 UNBLOCK_INPUT;
2681 DEFUN ("x-get-atom-name", Fx_get_atom_name,
2682 Sx_get_atom_name, 1, 2, 0,
2683 doc: /* Return the X atom name for VALUE as a string.
2684 VALUE may be a number or a cons where the car is the upper 16 bits and
2685 the cdr is the lower 16 bits of a 32 bit value.
2686 Use the display for FRAME or the current frame if FRAME is not given or nil.
2688 If the value is 0 or the atom is not known, return the empty string. */)
2689 (value, frame)
2690 Lisp_Object value, frame;
2692 struct frame *f = check_x_frame (frame);
2693 char *name = 0;
2694 Lisp_Object ret = Qnil;
2695 Display *dpy = FRAME_X_DISPLAY (f);
2696 Atom atom;
2697 int had_errors;
2699 if (INTEGERP (value))
2700 atom = (Atom) XUINT (value);
2701 else if (FLOATP (value))
2702 atom = (Atom) XFLOAT_DATA (value);
2703 else if (CONSP (value))
2704 atom = (Atom) cons_to_long (value);
2705 else
2706 error ("Wrong type, value must be number or cons");
2708 BLOCK_INPUT;
2709 x_catch_errors (dpy);
2710 name = atom ? XGetAtomName (dpy, atom) : "";
2711 had_errors = x_had_errors_p (dpy);
2712 x_uncatch_errors ();
2714 if (!had_errors)
2715 ret = make_string (name, strlen (name));
2717 if (atom && name) XFree (name);
2718 if (NILP (ret)) ret = empty_unibyte_string;
2720 UNBLOCK_INPUT;
2722 return ret;
2725 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom,
2726 Sx_register_dnd_atom, 1, 2, 0,
2727 doc: /* Request that dnd events are made for ClientMessages with ATOM.
2728 ATOM can be a symbol or a string. The ATOM is interned on the display that
2729 FRAME is on. If FRAME is nil, the selected frame is used. */)
2730 (atom, frame)
2731 Lisp_Object atom, frame;
2733 Atom x_atom;
2734 struct frame *f = check_x_frame (frame);
2735 size_t i;
2736 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2739 if (SYMBOLP (atom))
2740 x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom);
2741 else if (STRINGP (atom))
2743 BLOCK_INPUT;
2744 x_atom = XInternAtom (FRAME_X_DISPLAY (f), (char *) SDATA (atom), False);
2745 UNBLOCK_INPUT;
2747 else
2748 error ("ATOM must be a symbol or a string");
2750 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2751 if (dpyinfo->x_dnd_atoms[i] == x_atom)
2752 return Qnil;
2754 if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size)
2756 dpyinfo->x_dnd_atoms_size *= 2;
2757 dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms,
2758 sizeof (*dpyinfo->x_dnd_atoms)
2759 * dpyinfo->x_dnd_atoms_size);
2762 dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom;
2763 return Qnil;
2766 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2769 x_handle_dnd_message (f, event, dpyinfo, bufp)
2770 struct frame *f;
2771 XClientMessageEvent *event;
2772 struct x_display_info *dpyinfo;
2773 struct input_event *bufp;
2775 Lisp_Object vec;
2776 Lisp_Object frame;
2777 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2778 unsigned long size = 160/event->format;
2779 int x, y;
2780 unsigned char *data = (unsigned char *) event->data.b;
2781 int idata[5];
2782 size_t i;
2784 for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i)
2785 if (dpyinfo->x_dnd_atoms[i] == event->message_type) break;
2787 if (i == dpyinfo->x_dnd_atoms_length) return 0;
2789 XSETFRAME (frame, f);
2791 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2792 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2793 function expects them to be of size int (i.e. 32). So to be able to
2794 use that function, put the data in the form it expects if format is 32. */
2796 if (event->format == 32 && event->format < BITS_PER_LONG)
2798 int i;
2799 for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */
2800 idata[i] = (int) event->data.l[i];
2801 data = (unsigned char *) idata;
2804 vec = Fmake_vector (make_number (4), Qnil);
2805 AREF (vec, 0) = SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f),
2806 event->message_type));
2807 AREF (vec, 1) = frame;
2808 AREF (vec, 2) = make_number (event->format);
2809 AREF (vec, 3) = x_property_data_to_lisp (f,
2810 data,
2811 event->message_type,
2812 event->format,
2813 size);
2815 mouse_position_for_drop (f, &x, &y);
2816 bufp->kind = DRAG_N_DROP_EVENT;
2817 bufp->frame_or_window = frame;
2818 bufp->timestamp = CurrentTime;
2819 bufp->x = make_number (x);
2820 bufp->y = make_number (y);
2821 bufp->arg = vec;
2822 bufp->modifiers = 0;
2824 return 1;
2827 DEFUN ("x-send-client-message", Fx_send_client_event,
2828 Sx_send_client_message, 6, 6, 0,
2829 doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2831 For DISPLAY, specify either a frame or a display name (a string).
2832 If DISPLAY is nil, that stands for the selected frame's display.
2833 DEST may be a number, in which case it is a Window id. The value 0 may
2834 be used to send to the root window of the DISPLAY.
2835 If DEST is a cons, it is converted to a 32 bit number
2836 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2837 number is then used as a window id.
2838 If DEST is a frame the event is sent to the outer window of that frame.
2839 Nil means the currently selected frame.
2840 If DEST is the string "PointerWindow" the event is sent to the window that
2841 contains the pointer. If DEST is the string "InputFocus" the event is
2842 sent to the window that has the input focus.
2843 FROM is the frame sending the event. Use nil for currently selected frame.
2844 MESSAGE-TYPE is the name of an Atom as a string.
2845 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2846 bits. VALUES is a list of numbers, cons and/or strings containing the values
2847 to send. If a value is a string, it is converted to an Atom and the value of
2848 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2849 with the high 16 bits from the car and the lower 16 bit from the cdr.
2850 If more values than fits into the event is given, the excessive values
2851 are ignored. */)
2852 (display, dest, from, message_type, format, values)
2853 Lisp_Object display, dest, from, message_type, format, values;
2855 struct x_display_info *dpyinfo = check_x_display_info (display);
2856 Window wdest;
2857 XEvent event;
2858 Lisp_Object cons;
2859 int size;
2860 struct frame *f = check_x_frame (from);
2861 int to_root;
2863 CHECK_STRING (message_type);
2864 CHECK_NUMBER (format);
2865 CHECK_CONS (values);
2867 if (x_check_property_data (values) == -1)
2868 error ("Bad data in VALUES, must be number, cons or string");
2870 event.xclient.type = ClientMessage;
2871 event.xclient.format = XFASTINT (format);
2873 if (event.xclient.format != 8 && event.xclient.format != 16
2874 && event.xclient.format != 32)
2875 error ("FORMAT must be one of 8, 16 or 32");
2877 if (FRAMEP (dest) || NILP (dest))
2879 struct frame *fdest = check_x_frame (dest);
2880 wdest = FRAME_OUTER_WINDOW (fdest);
2882 else if (STRINGP (dest))
2884 if (strcmp (SDATA (dest), "PointerWindow") == 0)
2885 wdest = PointerWindow;
2886 else if (strcmp (SDATA (dest), "InputFocus") == 0)
2887 wdest = InputFocus;
2888 else
2889 error ("DEST as a string must be one of PointerWindow or InputFocus");
2891 else if (INTEGERP (dest))
2892 wdest = (Window) XFASTINT (dest);
2893 else if (FLOATP (dest))
2894 wdest = (Window) XFLOAT_DATA (dest);
2895 else if (CONSP (dest))
2897 if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest)))
2898 error ("Both car and cdr for DEST must be numbers");
2899 else
2900 wdest = (Window) cons_to_long (dest);
2902 else
2903 error ("DEST must be a frame, nil, string, number or cons");
2905 if (wdest == 0) wdest = dpyinfo->root_window;
2906 to_root = wdest == dpyinfo->root_window;
2908 for (cons = values, size = 0; CONSP (cons); cons = XCDR (cons), ++size)
2911 BLOCK_INPUT;
2913 event.xclient.message_type
2914 = XInternAtom (dpyinfo->display, SDATA (message_type), False);
2915 event.xclient.display = dpyinfo->display;
2917 /* Some clients (metacity for example) expects sending window to be here
2918 when sending to the root window. */
2919 event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest;
2922 memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b));
2923 x_fill_property_data (dpyinfo->display, values, event.xclient.data.b,
2924 event.xclient.format);
2926 /* If event mask is 0 the event is sent to the client that created
2927 the destination window. But if we are sending to the root window,
2928 there is no such client. Then we set the event mask to 0xffff. The
2929 event then goes to clients selecting for events on the root window. */
2930 x_catch_errors (dpyinfo->display);
2932 int propagate = to_root ? False : True;
2933 unsigned mask = to_root ? 0xffff : 0;
2934 XSendEvent (dpyinfo->display, wdest, propagate, mask, &event);
2935 XFlush (dpyinfo->display);
2937 x_uncatch_errors ();
2938 UNBLOCK_INPUT;
2940 return Qnil;
2944 void
2945 syms_of_xselect ()
2947 defsubr (&Sx_get_selection_internal);
2948 defsubr (&Sx_own_selection_internal);
2949 defsubr (&Sx_disown_selection_internal);
2950 defsubr (&Sx_selection_owner_p);
2951 defsubr (&Sx_selection_exists_p);
2953 #ifdef CUT_BUFFER_SUPPORT
2954 defsubr (&Sx_get_cut_buffer_internal);
2955 defsubr (&Sx_store_cut_buffer_internal);
2956 defsubr (&Sx_rotate_cut_buffers_internal);
2957 #endif
2959 defsubr (&Sx_get_atom_name);
2960 defsubr (&Sx_send_client_message);
2961 defsubr (&Sx_register_dnd_atom);
2963 reading_selection_reply = Fcons (Qnil, Qnil);
2964 staticpro (&reading_selection_reply);
2965 reading_selection_window = 0;
2966 reading_which_selection = 0;
2968 property_change_wait_list = 0;
2969 prop_location_identifier = 0;
2970 property_change_reply = Fcons (Qnil, Qnil);
2971 staticpro (&property_change_reply);
2973 Vselection_alist = Qnil;
2974 staticpro (&Vselection_alist);
2976 DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist,
2977 doc: /* An alist associating X Windows selection-types with functions.
2978 These functions are called to convert the selection, with three args:
2979 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2980 a desired type to which the selection should be converted;
2981 and the local selection value (whatever was given to `x-own-selection').
2983 The function should return the value to send to the X server
2984 \(typically a string). A return value of nil
2985 means that the conversion could not be done.
2986 A return value which is the symbol `NULL'
2987 means that a side-effect was executed,
2988 and there is no meaningful selection value. */);
2989 Vselection_converter_alist = Qnil;
2991 DEFVAR_LISP ("x-lost-selection-functions", &Vx_lost_selection_functions,
2992 doc: /* A list of functions to be called when Emacs loses an X selection.
2993 \(This happens when some other X client makes its own selection
2994 or when a Lisp program explicitly clears the selection.)
2995 The functions are called with one argument, the selection type
2996 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2997 Vx_lost_selection_functions = Qnil;
2999 DEFVAR_LISP ("x-sent-selection-functions", &Vx_sent_selection_functions,
3000 doc: /* A list of functions to be called when Emacs answers a selection request.
3001 The functions are called with four arguments:
3002 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
3003 - the selection-type which Emacs was asked to convert the
3004 selection into before sending (for example, `STRING' or `LENGTH');
3005 - a flag indicating success or failure for responding to the request.
3006 We might have failed (and declined the request) for any number of reasons,
3007 including being asked for a selection that we no longer own, or being asked
3008 to convert into a type that we don't know about or that is inappropriate.
3009 This hook doesn't let you change the behavior of Emacs's selection replies,
3010 it merely informs you that they have happened. */);
3011 Vx_sent_selection_functions = Qnil;
3013 DEFVAR_LISP ("selection-coding-system", &Vselection_coding_system,
3014 doc: /* Coding system for communicating with other X clients.
3016 When sending text via selection and clipboard, if the requested
3017 data-type is not "UTF8_STRING", the text is encoded by this coding
3018 system.
3020 When receiving text, if the data-type of the received text is not
3021 "UTF8_STRING", it is decoded by this coding system.
3023 See also the documentation of the variable `x-select-request-type' how
3024 to control which data-type to request for receiving text.
3026 The default value is `compound-text-with-extensions'. */);
3027 Vselection_coding_system = intern ("compound-text-with-extensions");
3029 DEFVAR_LISP ("next-selection-coding-system", &Vnext_selection_coding_system,
3030 doc: /* Coding system for the next communication with other X clients.
3031 Usually, `selection-coding-system' is used for communicating with
3032 other X clients. But, if this variable is set, it is used for the
3033 next communication only. After the communication, this variable is
3034 set to nil. */);
3035 Vnext_selection_coding_system = Qnil;
3037 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout,
3038 doc: /* Number of milliseconds to wait for a selection reply.
3039 If the selection owner doesn't reply in this time, we give up.
3040 A value of 0 means wait as long as necessary. This is initialized from the
3041 \"*selectionTimeout\" resource. */);
3042 x_selection_timeout = 0;
3044 QPRIMARY = intern ("PRIMARY"); staticpro (&QPRIMARY);
3045 QSECONDARY = intern ("SECONDARY"); staticpro (&QSECONDARY);
3046 QSTRING = intern ("STRING"); staticpro (&QSTRING);
3047 QINTEGER = intern ("INTEGER"); staticpro (&QINTEGER);
3048 QCLIPBOARD = intern ("CLIPBOARD"); staticpro (&QCLIPBOARD);
3049 QTIMESTAMP = intern ("TIMESTAMP"); staticpro (&QTIMESTAMP);
3050 QTEXT = intern ("TEXT"); staticpro (&QTEXT);
3051 QCOMPOUND_TEXT = intern ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT);
3052 QUTF8_STRING = intern ("UTF8_STRING"); staticpro (&QUTF8_STRING);
3053 QDELETE = intern ("DELETE"); staticpro (&QDELETE);
3054 QMULTIPLE = intern ("MULTIPLE"); staticpro (&QMULTIPLE);
3055 QINCR = intern ("INCR"); staticpro (&QINCR);
3056 QEMACS_TMP = intern ("_EMACS_TMP_"); staticpro (&QEMACS_TMP);
3057 QTARGETS = intern ("TARGETS"); staticpro (&QTARGETS);
3058 QATOM = intern ("ATOM"); staticpro (&QATOM);
3059 QATOM_PAIR = intern ("ATOM_PAIR"); staticpro (&QATOM_PAIR);
3060 QNULL = intern ("NULL"); staticpro (&QNULL);
3061 Qcompound_text_with_extensions = intern ("compound-text-with-extensions");
3062 staticpro (&Qcompound_text_with_extensions);
3064 #ifdef CUT_BUFFER_SUPPORT
3065 QCUT_BUFFER0 = intern ("CUT_BUFFER0"); staticpro (&QCUT_BUFFER0);
3066 QCUT_BUFFER1 = intern ("CUT_BUFFER1"); staticpro (&QCUT_BUFFER1);
3067 QCUT_BUFFER2 = intern ("CUT_BUFFER2"); staticpro (&QCUT_BUFFER2);
3068 QCUT_BUFFER3 = intern ("CUT_BUFFER3"); staticpro (&QCUT_BUFFER3);
3069 QCUT_BUFFER4 = intern ("CUT_BUFFER4"); staticpro (&QCUT_BUFFER4);
3070 QCUT_BUFFER5 = intern ("CUT_BUFFER5"); staticpro (&QCUT_BUFFER5);
3071 QCUT_BUFFER6 = intern ("CUT_BUFFER6"); staticpro (&QCUT_BUFFER6);
3072 QCUT_BUFFER7 = intern ("CUT_BUFFER7"); staticpro (&QCUT_BUFFER7);
3073 #endif
3075 Qforeign_selection = intern ("foreign-selection");
3076 staticpro (&Qforeign_selection);
3079 /* arch-tag: 7c293b0f-9918-4f69-8ac7-03e142307236
3080 (do not change this comment) */