1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* Rewritten by jwz */
23 #include <stdio.h> /* termhooks.h needs this */
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
33 #include "xterm.h" /* for all of the X includes */
34 #include "dispextern.h" /* frame.h seems to want this */
35 #include "frame.h" /* Need this to get the X window of selected_frame */
36 #include "blockinput.h"
39 #include "termhooks.h"
42 #include <X11/Xproto.h>
46 static Lisp_Object
x_atom_to_symbol (Display
*dpy
, Atom atom
);
47 static Atom
symbol_to_x_atom (struct x_display_info
*, Display
*,
49 static void x_own_selection (Lisp_Object
, Lisp_Object
);
50 static Lisp_Object
x_get_local_selection (Lisp_Object
, Lisp_Object
, int);
51 static void x_decline_selection_request (struct input_event
*);
52 static Lisp_Object
x_selection_request_lisp_error (Lisp_Object
);
53 static Lisp_Object
queue_selection_requests_unwind (Lisp_Object
);
54 static Lisp_Object
some_frame_on_display (struct x_display_info
*);
55 static Lisp_Object
x_catch_errors_unwind (Lisp_Object
);
56 static void x_reply_selection_request (struct input_event
*, int,
57 unsigned char *, int, Atom
);
58 static int waiting_for_other_props_on_window (Display
*, Window
);
59 static struct prop_location
*expect_property_change (Display
*, Window
,
61 static void unexpect_property_change (struct prop_location
*);
62 static Lisp_Object
wait_for_property_change_unwind (Lisp_Object
);
63 static void wait_for_property_change (struct prop_location
*);
64 static Lisp_Object
x_get_foreign_selection (Lisp_Object
,
67 static void x_get_window_property (Display
*, Window
, Atom
,
68 unsigned char **, int *,
69 Atom
*, int *, unsigned long *, int);
70 static void receive_incremental_selection (Display
*, Window
, Atom
,
71 Lisp_Object
, unsigned,
72 unsigned char **, int *,
73 Atom
*, int *, unsigned long *);
74 static Lisp_Object
x_get_window_property_as_lisp_data (Display
*,
77 static Lisp_Object
selection_data_to_lisp_data (Display
*,
78 const unsigned char *,
80 static void lisp_data_to_selection_data (Display
*, Lisp_Object
,
81 unsigned char **, Atom
*,
82 unsigned *, int *, int *);
83 static Lisp_Object
clean_local_selection_data (Lisp_Object
);
85 /* Printing traces to stderr. */
87 #ifdef TRACE_SELECTION
89 fprintf (stderr, "%d: " fmt "\n", getpid ())
90 #define TRACE1(fmt, a0) \
91 fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
92 #define TRACE2(fmt, a0, a1) \
93 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
94 #define TRACE3(fmt, a0, a1, a2) \
95 fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
97 #define TRACE0(fmt) (void) 0
98 #define TRACE1(fmt, a0) (void) 0
99 #define TRACE2(fmt, a0, a1) (void) 0
100 #define TRACE3(fmt, a0, a1) (void) 0
104 Lisp_Object QSECONDARY
, QSTRING
, QINTEGER
, QCLIPBOARD
, QTIMESTAMP
,
105 QTEXT
, QDELETE
, QMULTIPLE
, QINCR
, QEMACS_TMP
, QTARGETS
, QATOM
, QNULL
,
108 Lisp_Object QCOMPOUND_TEXT
; /* This is a type of selection. */
109 Lisp_Object QUTF8_STRING
; /* This is a type of selection. */
111 Lisp_Object Qcompound_text_with_extensions
;
113 static Lisp_Object Qforeign_selection
;
115 /* If this is a smaller number than the max-request-size of the display,
116 emacs will use INCR selection transfer when the selection is larger
117 than this. The max-request-size is usually around 64k, so if you want
118 emacs to use incremental selection transfers when the selection is
119 smaller than that, set this. I added this mostly for debugging the
120 incremental transfer stuff, but it might improve server performance. */
121 #define MAX_SELECTION_QUANTUM 0xFFFFFF
123 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
125 /* The timestamp of the last input event Emacs received from the X server. */
126 /* Defined in keyboard.c. */
127 extern unsigned long last_event_timestamp
;
129 /* This is an association list whose elements are of the form
130 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
131 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
132 SELECTION-VALUE is the value that emacs owns for that selection.
133 It may be any kind of Lisp object.
134 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
135 as a cons of two 16-bit numbers (making a 32 bit time.)
136 FRAME is the frame for which we made the selection.
137 If there is an entry in this alist, then it can be assumed that Emacs owns
139 The only (eq) parts of this list that are visible from Lisp are the
141 static Lisp_Object Vselection_alist
;
145 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
148 struct selection_event_queue
150 struct input_event event
;
151 struct selection_event_queue
*next
;
154 static struct selection_event_queue
*selection_queue
;
156 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
158 static int x_queue_selection_requests
;
160 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
163 x_queue_event (struct input_event
*event
)
165 struct selection_event_queue
*queue_tmp
;
167 /* Don't queue repeated requests.
168 This only happens for large requests which uses the incremental protocol. */
169 for (queue_tmp
= selection_queue
; queue_tmp
; queue_tmp
= queue_tmp
->next
)
171 if (!memcmp (&queue_tmp
->event
, event
, sizeof (*event
)))
173 TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp
);
174 x_decline_selection_request (event
);
180 = (struct selection_event_queue
*) xmalloc (sizeof (struct selection_event_queue
));
182 if (queue_tmp
!= NULL
)
184 TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp
);
185 queue_tmp
->event
= *event
;
186 queue_tmp
->next
= selection_queue
;
187 selection_queue
= queue_tmp
;
191 /* Start queuing SELECTION_REQUEST_EVENT events. */
194 x_start_queuing_selection_requests (void)
196 if (x_queue_selection_requests
)
199 x_queue_selection_requests
++;
200 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests
);
203 /* Stop queuing SELECTION_REQUEST_EVENT events. */
206 x_stop_queuing_selection_requests (void)
208 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests
);
209 --x_queue_selection_requests
;
211 /* Take all the queued events and put them back
212 so that they get processed afresh. */
214 while (selection_queue
!= NULL
)
216 struct selection_event_queue
*queue_tmp
= selection_queue
;
217 TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp
);
218 kbd_buffer_unget_event (&queue_tmp
->event
);
219 selection_queue
= queue_tmp
->next
;
220 xfree ((char *)queue_tmp
);
225 /* This converts a Lisp symbol to a server Atom, avoiding a server
226 roundtrip whenever possible. */
229 symbol_to_x_atom (struct x_display_info
*dpyinfo
, Display
*display
, Lisp_Object sym
)
232 if (NILP (sym
)) return 0;
233 if (EQ (sym
, QPRIMARY
)) return XA_PRIMARY
;
234 if (EQ (sym
, QSECONDARY
)) return XA_SECONDARY
;
235 if (EQ (sym
, QSTRING
)) return XA_STRING
;
236 if (EQ (sym
, QINTEGER
)) return XA_INTEGER
;
237 if (EQ (sym
, QATOM
)) return XA_ATOM
;
238 if (EQ (sym
, QCLIPBOARD
)) return dpyinfo
->Xatom_CLIPBOARD
;
239 if (EQ (sym
, QTIMESTAMP
)) return dpyinfo
->Xatom_TIMESTAMP
;
240 if (EQ (sym
, QTEXT
)) return dpyinfo
->Xatom_TEXT
;
241 if (EQ (sym
, QCOMPOUND_TEXT
)) return dpyinfo
->Xatom_COMPOUND_TEXT
;
242 if (EQ (sym
, QUTF8_STRING
)) return dpyinfo
->Xatom_UTF8_STRING
;
243 if (EQ (sym
, QDELETE
)) return dpyinfo
->Xatom_DELETE
;
244 if (EQ (sym
, QMULTIPLE
)) return dpyinfo
->Xatom_MULTIPLE
;
245 if (EQ (sym
, QINCR
)) return dpyinfo
->Xatom_INCR
;
246 if (EQ (sym
, QEMACS_TMP
)) return dpyinfo
->Xatom_EMACS_TMP
;
247 if (EQ (sym
, QTARGETS
)) return dpyinfo
->Xatom_TARGETS
;
248 if (EQ (sym
, QNULL
)) return dpyinfo
->Xatom_NULL
;
249 if (!SYMBOLP (sym
)) abort ();
251 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym
)));
253 val
= XInternAtom (display
, SSDATA (SYMBOL_NAME (sym
)), False
);
259 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
260 and calls to intern whenever possible. */
263 x_atom_to_symbol (Display
*dpy
, Atom atom
)
265 struct x_display_info
*dpyinfo
;
286 dpyinfo
= x_display_info_for_display (dpy
);
287 if (atom
== dpyinfo
->Xatom_CLIPBOARD
)
289 if (atom
== dpyinfo
->Xatom_TIMESTAMP
)
291 if (atom
== dpyinfo
->Xatom_TEXT
)
293 if (atom
== dpyinfo
->Xatom_COMPOUND_TEXT
)
294 return QCOMPOUND_TEXT
;
295 if (atom
== dpyinfo
->Xatom_UTF8_STRING
)
297 if (atom
== dpyinfo
->Xatom_DELETE
)
299 if (atom
== dpyinfo
->Xatom_MULTIPLE
)
301 if (atom
== dpyinfo
->Xatom_INCR
)
303 if (atom
== dpyinfo
->Xatom_EMACS_TMP
)
305 if (atom
== dpyinfo
->Xatom_TARGETS
)
307 if (atom
== dpyinfo
->Xatom_NULL
)
311 str
= XGetAtomName (dpy
, atom
);
313 TRACE1 ("XGetAtomName --> %s", str
);
314 if (! str
) return Qnil
;
317 /* This was allocated by Xlib, so use XFree. */
323 /* Do protocol to assert ourself as a selection owner.
324 Update the Vselection_alist so that we can reply to later requests for
328 x_own_selection (Lisp_Object selection_name
, Lisp_Object selection_value
)
330 struct frame
*sf
= SELECTED_FRAME ();
331 Window selecting_window
;
333 Time time
= last_event_timestamp
;
335 struct x_display_info
*dpyinfo
;
337 if (! FRAME_X_P (sf
))
340 selecting_window
= FRAME_X_WINDOW (sf
);
341 display
= FRAME_X_DISPLAY (sf
);
342 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
344 CHECK_SYMBOL (selection_name
);
345 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection_name
);
348 x_catch_errors (display
);
349 XSetSelectionOwner (display
, selection_atom
, selecting_window
, time
);
350 x_check_errors (display
, "Can't set selection: %s");
354 /* Now update the local cache */
356 Lisp_Object selection_time
;
357 Lisp_Object selection_data
;
358 Lisp_Object prev_value
;
360 selection_time
= long_to_cons ((unsigned long) time
);
361 selection_data
= list4 (selection_name
, selection_value
,
362 selection_time
, selected_frame
);
363 prev_value
= assq_no_quit (selection_name
, Vselection_alist
);
365 Vselection_alist
= Fcons (selection_data
, Vselection_alist
);
367 /* If we already owned the selection, remove the old selection data.
368 Perhaps we should destructively modify it instead.
369 Don't use Fdelq as that may QUIT. */
370 if (!NILP (prev_value
))
372 Lisp_Object rest
; /* we know it's not the CAR, so it's easy. */
373 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
374 if (EQ (prev_value
, Fcar (XCDR (rest
))))
376 XSETCDR (rest
, Fcdr (XCDR (rest
)));
383 /* Given a selection-name and desired type, look up our local copy of
384 the selection value and convert it to the type.
385 The value is nil or a string.
386 This function is used both for remote requests (LOCAL_REQUEST is zero)
387 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
389 This calls random Lisp code, and may signal or gc. */
392 x_get_local_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
, int local_request
)
394 Lisp_Object local_value
;
395 Lisp_Object handler_fn
, value
, type
, check
;
398 local_value
= assq_no_quit (selection_symbol
, Vselection_alist
);
400 if (NILP (local_value
)) return Qnil
;
402 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
403 if (EQ (target_type
, QTIMESTAMP
))
406 value
= XCAR (XCDR (XCDR (local_value
)));
409 else if (EQ (target_type
, QDELETE
))
412 Fx_disown_selection_internal
414 XCAR (XCDR (XCDR (local_value
))));
419 #if 0 /* #### MULTIPLE doesn't work yet */
420 else if (CONSP (target_type
)
421 && XCAR (target_type
) == QMULTIPLE
)
426 pairs
= XCDR (target_type
);
427 size
= XVECTOR (pairs
)->size
;
428 /* If the target is MULTIPLE, then target_type looks like
429 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
430 We modify the second element of each pair in the vector and
431 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
433 for (i
= 0; i
< size
; i
++)
436 pair
= XVECTOR (pairs
)->contents
[i
];
437 XVECTOR (pair
)->contents
[1]
438 = x_get_local_selection (XVECTOR (pair
)->contents
[0],
439 XVECTOR (pair
)->contents
[1],
447 /* Don't allow a quit within the converter.
448 When the user types C-g, he would be surprised
449 if by luck it came during a converter. */
450 count
= SPECPDL_INDEX ();
451 specbind (Qinhibit_quit
, Qt
);
453 CHECK_SYMBOL (target_type
);
454 handler_fn
= Fcdr (Fassq (target_type
, Vselection_converter_alist
));
455 /* gcpro is not needed here since nothing but HANDLER_FN
456 is live, and that ought to be a symbol. */
458 if (!NILP (handler_fn
))
459 value
= call3 (handler_fn
,
460 selection_symbol
, (local_request
? Qnil
: target_type
),
461 XCAR (XCDR (local_value
)));
464 unbind_to (count
, Qnil
);
467 /* Make sure this value is of a type that we could transmit
468 to another X client. */
472 && SYMBOLP (XCAR (value
)))
474 check
= XCDR (value
);
482 /* Check for a value that cons_to_long could handle. */
483 else if (CONSP (check
)
484 && INTEGERP (XCAR (check
))
485 && (INTEGERP (XCDR (check
))
487 (CONSP (XCDR (check
))
488 && INTEGERP (XCAR (XCDR (check
)))
489 && NILP (XCDR (XCDR (check
))))))
492 signal_error ("Invalid data returned by selection-conversion function",
493 list2 (handler_fn
, value
));
496 /* Subroutines of x_reply_selection_request. */
498 /* Send a SelectionNotify event to the requestor with property=None,
499 meaning we were unable to do what they wanted. */
502 x_decline_selection_request (struct input_event
*event
)
504 XSelectionEvent reply
;
506 reply
.type
= SelectionNotify
;
507 reply
.display
= SELECTION_EVENT_DISPLAY (event
);
508 reply
.requestor
= SELECTION_EVENT_REQUESTOR (event
);
509 reply
.selection
= SELECTION_EVENT_SELECTION (event
);
510 reply
.time
= SELECTION_EVENT_TIME (event
);
511 reply
.target
= SELECTION_EVENT_TARGET (event
);
512 reply
.property
= None
;
514 /* The reason for the error may be that the receiver has
515 died in the meantime. Handle that case. */
517 x_catch_errors (reply
.display
);
518 XSendEvent (reply
.display
, reply
.requestor
, False
, 0L, (XEvent
*) &reply
);
519 XFlush (reply
.display
);
524 /* This is the selection request currently being processed.
525 It is set to zero when the request is fully processed. */
526 static struct input_event
*x_selection_current_request
;
528 /* Display info in x_selection_request. */
530 static struct x_display_info
*selection_request_dpyinfo
;
532 /* Used as an unwind-protect clause so that, if a selection-converter signals
533 an error, we tell the requester that we were unable to do what they wanted
534 before we throw to top-level or go into the debugger or whatever. */
537 x_selection_request_lisp_error (Lisp_Object ignore
)
539 if (x_selection_current_request
!= 0
540 && selection_request_dpyinfo
->display
)
541 x_decline_selection_request (x_selection_current_request
);
546 x_catch_errors_unwind (Lisp_Object dummy
)
555 /* This stuff is so that INCR selections are reentrant (that is, so we can
556 be servicing multiple INCR selection requests simultaneously.) I haven't
557 actually tested that yet. */
559 /* Keep a list of the property changes that are awaited. */
569 struct prop_location
*next
;
572 static struct prop_location
*expect_property_change (Display
*display
, Window window
, Atom property
, int state
);
573 static void wait_for_property_change (struct prop_location
*location
);
574 static void unexpect_property_change (struct prop_location
*location
);
575 static int waiting_for_other_props_on_window (Display
*display
, Window window
);
577 static int prop_location_identifier
;
579 static Lisp_Object property_change_reply
;
581 static struct prop_location
*property_change_reply_object
;
583 static struct prop_location
*property_change_wait_list
;
586 queue_selection_requests_unwind (Lisp_Object tem
)
588 x_stop_queuing_selection_requests ();
592 /* Return some frame whose display info is DPYINFO.
593 Return nil if there is none. */
596 some_frame_on_display (struct x_display_info
*dpyinfo
)
598 Lisp_Object list
, frame
;
600 FOR_EACH_FRAME (list
, frame
)
602 if (FRAME_X_P (XFRAME (frame
))
603 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
610 /* Send the reply to a selection request event EVENT.
611 TYPE is the type of selection data requested.
612 DATA and SIZE describe the data to send, already converted.
613 FORMAT is the unit-size (in bits) of the data to be transmitted. */
615 #ifdef TRACE_SELECTION
616 static int x_reply_selection_request_cnt
;
617 #endif /* TRACE_SELECTION */
620 x_reply_selection_request (struct input_event
*event
, int format
, unsigned char *data
, int size
, Atom type
)
622 XSelectionEvent reply
;
623 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
624 Window window
= SELECTION_EVENT_REQUESTOR (event
);
626 int format_bytes
= format
/8;
627 int max_bytes
= SELECTION_QUANTUM (display
);
628 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
629 int count
= SPECPDL_INDEX ();
631 if (max_bytes
> MAX_SELECTION_QUANTUM
)
632 max_bytes
= MAX_SELECTION_QUANTUM
;
634 reply
.type
= SelectionNotify
;
635 reply
.display
= display
;
636 reply
.requestor
= window
;
637 reply
.selection
= SELECTION_EVENT_SELECTION (event
);
638 reply
.time
= SELECTION_EVENT_TIME (event
);
639 reply
.target
= SELECTION_EVENT_TARGET (event
);
640 reply
.property
= SELECTION_EVENT_PROPERTY (event
);
641 if (reply
.property
== None
)
642 reply
.property
= reply
.target
;
645 /* The protected block contains wait_for_property_change, which can
646 run random lisp code (process handlers) or signal. Therefore, we
647 put the x_uncatch_errors call in an unwind. */
648 record_unwind_protect (x_catch_errors_unwind
, Qnil
);
649 x_catch_errors (display
);
651 #ifdef TRACE_SELECTION
653 char *sel
= XGetAtomName (display
, reply
.selection
);
654 char *tgt
= XGetAtomName (display
, reply
.target
);
655 TRACE3 ("%s, target %s (%d)", sel
, tgt
, ++x_reply_selection_request_cnt
);
656 if (sel
) XFree (sel
);
657 if (tgt
) XFree (tgt
);
659 #endif /* TRACE_SELECTION */
661 /* Store the data on the requested property.
662 If the selection is large, only store the first N bytes of it.
664 bytes_remaining
= size
* format_bytes
;
665 if (bytes_remaining
<= max_bytes
)
667 /* Send all the data at once, with minimal handshaking. */
668 TRACE1 ("Sending all %d bytes", bytes_remaining
);
669 XChangeProperty (display
, window
, reply
.property
, type
, format
,
670 PropModeReplace
, data
, size
);
671 /* At this point, the selection was successfully stored; ack it. */
672 XSendEvent (display
, window
, False
, 0L, (XEvent
*) &reply
);
676 /* Send an INCR selection. */
677 struct prop_location
*wait_object
;
681 frame
= some_frame_on_display (dpyinfo
);
683 /* If the display no longer has frames, we can't expect
684 to get many more selection requests from it, so don't
685 bother trying to queue them. */
688 x_start_queuing_selection_requests ();
690 record_unwind_protect (queue_selection_requests_unwind
,
694 if (x_window_to_frame (dpyinfo
, window
)) /* #### debug */
695 error ("Attempt to transfer an INCR to ourself!");
697 TRACE2 ("Start sending %d bytes incrementally (%s)",
698 bytes_remaining
, XGetAtomName (display
, reply
.property
));
699 wait_object
= expect_property_change (display
, window
, reply
.property
,
702 TRACE1 ("Set %s to number of bytes to send",
703 XGetAtomName (display
, reply
.property
));
705 /* XChangeProperty expects an array of long even if long is more than
709 value
[0] = bytes_remaining
;
710 XChangeProperty (display
, window
, reply
.property
, dpyinfo
->Xatom_INCR
,
712 (unsigned char *) value
, 1);
715 XSelectInput (display
, window
, PropertyChangeMask
);
717 /* Tell 'em the INCR data is there... */
718 TRACE0 ("Send SelectionNotify event");
719 XSendEvent (display
, window
, False
, 0L, (XEvent
*) &reply
);
722 had_errors
= x_had_errors_p (display
);
725 /* First, wait for the requester to ack by deleting the property.
726 This can run random lisp code (process handlers) or signal. */
729 TRACE1 ("Waiting for ACK (deletion of %s)",
730 XGetAtomName (display
, reply
.property
));
731 wait_for_property_change (wait_object
);
734 unexpect_property_change (wait_object
);
737 while (bytes_remaining
)
739 int i
= ((bytes_remaining
< max_bytes
)
741 : max_bytes
) / format_bytes
;
746 = expect_property_change (display
, window
, reply
.property
,
749 TRACE1 ("Sending increment of %d elements", i
);
750 TRACE1 ("Set %s to increment data",
751 XGetAtomName (display
, reply
.property
));
753 /* Append the next chunk of data to the property. */
754 XChangeProperty (display
, window
, reply
.property
, type
, format
,
755 PropModeAppend
, data
, i
);
756 bytes_remaining
-= i
* format_bytes
;
758 data
+= i
* sizeof (long);
760 data
+= i
* format_bytes
;
762 had_errors
= x_had_errors_p (display
);
768 /* Now wait for the requester to ack this chunk by deleting the
769 property. This can run random lisp code or signal. */
770 TRACE1 ("Waiting for increment ACK (deletion of %s)",
771 XGetAtomName (display
, reply
.property
));
772 wait_for_property_change (wait_object
);
775 /* Now write a zero-length chunk to the property to tell the
776 requester that we're done. */
778 if (! waiting_for_other_props_on_window (display
, window
))
779 XSelectInput (display
, window
, 0L);
781 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
782 XGetAtomName (display
, reply
.property
));
783 XChangeProperty (display
, window
, reply
.property
, type
, format
,
784 PropModeReplace
, data
, 0);
785 TRACE0 ("Done sending incrementally");
788 /* rms, 2003-01-03: I think I have fixed this bug. */
789 /* The window we're communicating with may have been deleted
790 in the meantime (that's a real situation from a bug report).
791 In this case, there may be events in the event queue still
792 refering to the deleted window, and we'll get a BadWindow error
793 in XTread_socket when processing the events. I don't have
794 an idea how to fix that. gerd, 2001-01-98. */
795 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
796 delivered before uncatch errors. */
797 XSync (display
, False
);
800 /* GTK queues events in addition to the queue in Xlib. So we
801 UNBLOCK to enter the event loop and get possible errors delivered,
802 and then BLOCK again because x_uncatch_errors requires it. */
804 /* This calls x_uncatch_errors. */
805 unbind_to (count
, Qnil
);
809 /* Handle a SelectionRequest event EVENT.
810 This is called from keyboard.c when such an event is found in the queue. */
813 x_handle_selection_request (struct input_event
*event
)
815 struct gcpro gcpro1
, gcpro2
, gcpro3
;
816 Lisp_Object local_selection_data
;
817 Lisp_Object selection_symbol
;
818 Lisp_Object target_symbol
;
819 Lisp_Object converted_selection
;
820 Time local_selection_time
;
821 Lisp_Object successful_p
;
823 struct x_display_info
*dpyinfo
824 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event
));
826 TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
827 (unsigned long) SELECTION_EVENT_REQUESTOR (event
),
828 (unsigned long) SELECTION_EVENT_TIME (event
));
830 local_selection_data
= Qnil
;
831 target_symbol
= Qnil
;
832 converted_selection
= Qnil
;
835 GCPRO3 (local_selection_data
, converted_selection
, target_symbol
);
837 selection_symbol
= x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event
),
838 SELECTION_EVENT_SELECTION (event
));
840 local_selection_data
= assq_no_quit (selection_symbol
, Vselection_alist
);
842 if (NILP (local_selection_data
))
844 /* Someone asked for the selection, but we don't have it any more.
846 x_decline_selection_request (event
);
850 local_selection_time
= (Time
)
851 cons_to_long (XCAR (XCDR (XCDR (local_selection_data
))));
853 if (SELECTION_EVENT_TIME (event
) != CurrentTime
854 && local_selection_time
> SELECTION_EVENT_TIME (event
))
856 /* Someone asked for the selection, and we have one, but not the one
859 x_decline_selection_request (event
);
863 x_selection_current_request
= event
;
864 count
= SPECPDL_INDEX ();
865 selection_request_dpyinfo
= dpyinfo
;
866 record_unwind_protect (x_selection_request_lisp_error
, Qnil
);
868 target_symbol
= x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event
),
869 SELECTION_EVENT_TARGET (event
));
871 #if 0 /* #### MULTIPLE doesn't work yet */
872 if (EQ (target_symbol
, QMULTIPLE
))
873 target_symbol
= fetch_multiple_target (event
);
876 /* Convert lisp objects back into binary data */
879 = x_get_local_selection (selection_symbol
, target_symbol
, 0);
881 if (! NILP (converted_selection
))
889 if (CONSP (converted_selection
) && NILP (XCDR (converted_selection
)))
891 x_decline_selection_request (event
);
895 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event
),
897 &data
, &type
, &size
, &format
, &nofree
);
899 x_reply_selection_request (event
, format
, data
, size
, type
);
902 /* Indicate we have successfully processed this event. */
903 x_selection_current_request
= 0;
905 /* Use xfree, not XFree, because lisp_data_to_selection_data
906 calls xmalloc itself. */
912 unbind_to (count
, Qnil
);
916 /* Let random lisp code notice that the selection has been asked for. */
919 rest
= Vx_sent_selection_functions
;
920 if (!EQ (rest
, Qunbound
))
921 for (; CONSP (rest
); rest
= Fcdr (rest
))
922 call3 (Fcar (rest
), selection_symbol
, target_symbol
, successful_p
);
928 /* Handle a SelectionClear event EVENT, which indicates that some
929 client cleared out our previously asserted selection.
930 This is called from keyboard.c when such an event is found in the queue. */
933 x_handle_selection_clear (struct input_event
*event
)
935 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
936 Atom selection
= SELECTION_EVENT_SELECTION (event
);
937 Time changed_owner_time
= SELECTION_EVENT_TIME (event
);
939 Lisp_Object selection_symbol
, local_selection_data
;
940 Time local_selection_time
;
941 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
942 struct x_display_info
*t_dpyinfo
;
944 TRACE0 ("x_handle_selection_clear");
946 /* If the new selection owner is also Emacs,
947 don't clear the new selection. */
949 /* Check each display on the same terminal,
950 to see if this Emacs job now owns the selection
951 through that display. */
952 for (t_dpyinfo
= x_display_list
; t_dpyinfo
; t_dpyinfo
= t_dpyinfo
->next
)
953 if (t_dpyinfo
->terminal
->kboard
== dpyinfo
->terminal
->kboard
)
956 = XGetSelectionOwner (t_dpyinfo
->display
, selection
);
957 if (x_window_to_frame (t_dpyinfo
, owner_window
) != 0)
965 selection_symbol
= x_atom_to_symbol (display
, selection
);
967 local_selection_data
= assq_no_quit (selection_symbol
, Vselection_alist
);
969 /* Well, we already believe that we don't own it, so that's just fine. */
970 if (NILP (local_selection_data
)) return;
972 local_selection_time
= (Time
)
973 cons_to_long (XCAR (XCDR (XCDR (local_selection_data
))));
975 /* This SelectionClear is for a selection that we no longer own, so we can
976 disregard it. (That is, we have reasserted the selection since this
977 request was generated.) */
979 if (changed_owner_time
!= CurrentTime
980 && local_selection_time
> changed_owner_time
)
983 /* Otherwise, we're really honest and truly being told to drop it.
984 Don't use Fdelq as that may QUIT;. */
986 if (EQ (local_selection_data
, Fcar (Vselection_alist
)))
987 Vselection_alist
= Fcdr (Vselection_alist
);
991 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
992 if (EQ (local_selection_data
, Fcar (XCDR (rest
))))
994 XSETCDR (rest
, Fcdr (XCDR (rest
)));
999 /* Let random lisp code notice that the selection has been stolen. */
1003 rest
= Vx_lost_selection_functions
;
1004 if (!EQ (rest
, Qunbound
))
1006 for (; CONSP (rest
); rest
= Fcdr (rest
))
1007 call1 (Fcar (rest
), selection_symbol
);
1008 prepare_menu_bars ();
1009 redisplay_preserve_echo_area (20);
1015 x_handle_selection_event (struct input_event
*event
)
1017 TRACE0 ("x_handle_selection_event");
1019 if (event
->kind
== SELECTION_REQUEST_EVENT
)
1021 if (x_queue_selection_requests
)
1022 x_queue_event (event
);
1024 x_handle_selection_request (event
);
1027 x_handle_selection_clear (event
);
1031 /* Clear all selections that were made from frame F.
1032 We do this when about to delete a frame. */
1035 x_clear_frame_selections (FRAME_PTR f
)
1040 XSETFRAME (frame
, f
);
1042 /* Otherwise, we're really honest and truly being told to drop it.
1043 Don't use Fdelq as that may QUIT;. */
1045 /* Delete elements from the beginning of Vselection_alist. */
1046 while (!NILP (Vselection_alist
)
1047 && EQ (frame
, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist
)))))))
1049 /* Let random Lisp code notice that the selection has been stolen. */
1050 Lisp_Object hooks
, selection_symbol
;
1052 hooks
= Vx_lost_selection_functions
;
1053 selection_symbol
= Fcar (Fcar (Vselection_alist
));
1055 if (!EQ (hooks
, Qunbound
))
1057 for (; CONSP (hooks
); hooks
= Fcdr (hooks
))
1058 call1 (Fcar (hooks
), selection_symbol
);
1059 #if 0 /* This can crash when deleting a frame
1060 from x_connection_closed. Anyway, it seems unnecessary;
1061 something else should cause a redisplay. */
1062 redisplay_preserve_echo_area (21);
1066 Vselection_alist
= Fcdr (Vselection_alist
);
1069 /* Delete elements after the beginning of Vselection_alist. */
1070 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
1071 if (EQ (frame
, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest
))))))))
1073 /* Let random Lisp code notice that the selection has been stolen. */
1074 Lisp_Object hooks
, selection_symbol
;
1076 hooks
= Vx_lost_selection_functions
;
1077 selection_symbol
= Fcar (Fcar (XCDR (rest
)));
1079 if (!EQ (hooks
, Qunbound
))
1081 for (; CONSP (hooks
); hooks
= Fcdr (hooks
))
1082 call1 (Fcar (hooks
), selection_symbol
);
1083 #if 0 /* See above */
1084 redisplay_preserve_echo_area (22);
1087 XSETCDR (rest
, Fcdr (XCDR (rest
)));
1092 /* Nonzero if any properties for DISPLAY and WINDOW
1093 are on the list of what we are waiting for. */
1096 waiting_for_other_props_on_window (Display
*display
, Window window
)
1098 struct prop_location
*rest
= property_change_wait_list
;
1100 if (rest
->display
== display
&& rest
->window
== window
)
1107 /* Add an entry to the list of property changes we are waiting for.
1108 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1109 The return value is a number that uniquely identifies
1110 this awaited property change. */
1112 static struct prop_location
*
1113 expect_property_change (Display
*display
, Window window
, Atom property
, int state
)
1115 struct prop_location
*pl
= (struct prop_location
*) xmalloc (sizeof *pl
);
1116 pl
->identifier
= ++prop_location_identifier
;
1117 pl
->display
= display
;
1118 pl
->window
= window
;
1119 pl
->property
= property
;
1120 pl
->desired_state
= state
;
1121 pl
->next
= property_change_wait_list
;
1123 property_change_wait_list
= pl
;
1127 /* Delete an entry from the list of property changes we are waiting for.
1128 IDENTIFIER is the number that uniquely identifies the entry. */
1131 unexpect_property_change (struct prop_location
*location
)
1133 struct prop_location
*prev
= 0, *rest
= property_change_wait_list
;
1136 if (rest
== location
)
1139 prev
->next
= rest
->next
;
1141 property_change_wait_list
= rest
->next
;
1150 /* Remove the property change expectation element for IDENTIFIER. */
1153 wait_for_property_change_unwind (Lisp_Object loc
)
1155 struct prop_location
*location
= XSAVE_VALUE (loc
)->pointer
;
1157 unexpect_property_change (location
);
1158 if (location
== property_change_reply_object
)
1159 property_change_reply_object
= 0;
1163 /* Actually wait for a property change.
1164 IDENTIFIER should be the value that expect_property_change returned. */
1167 wait_for_property_change (struct prop_location
*location
)
1170 int count
= SPECPDL_INDEX ();
1172 if (property_change_reply_object
)
1175 /* Make sure to do unexpect_property_change if we quit or err. */
1176 record_unwind_protect (wait_for_property_change_unwind
,
1177 make_save_value (location
, 0));
1179 XSETCAR (property_change_reply
, Qnil
);
1180 property_change_reply_object
= location
;
1182 /* If the event we are waiting for arrives beyond here, it will set
1183 property_change_reply, because property_change_reply_object says so. */
1184 if (! location
->arrived
)
1186 secs
= x_selection_timeout
/ 1000;
1187 usecs
= (x_selection_timeout
% 1000) * 1000;
1188 TRACE2 (" Waiting %d secs, %d usecs", secs
, usecs
);
1189 wait_reading_process_output (secs
, usecs
, 0, 0,
1190 property_change_reply
, NULL
, 0);
1192 if (NILP (XCAR (property_change_reply
)))
1194 TRACE0 (" Timed out");
1195 error ("Timed out waiting for property-notify event");
1199 unbind_to (count
, Qnil
);
1202 /* Called from XTread_socket in response to a PropertyNotify event. */
1205 x_handle_property_notify (XPropertyEvent
*event
)
1207 struct prop_location
*prev
= 0, *rest
= property_change_wait_list
;
1212 && rest
->property
== event
->atom
1213 && rest
->window
== event
->window
1214 && rest
->display
== event
->display
1215 && rest
->desired_state
== event
->state
)
1217 TRACE2 ("Expected %s of property %s",
1218 (event
->state
== PropertyDelete
? "deletion" : "change"),
1219 XGetAtomName (event
->display
, event
->atom
));
1223 /* If this is the one wait_for_property_change is waiting for,
1224 tell it to wake up. */
1225 if (rest
== property_change_reply_object
)
1226 XSETCAR (property_change_reply
, Qt
);
1238 #if 0 /* #### MULTIPLE doesn't work yet */
1241 fetch_multiple_target (event
)
1242 XSelectionRequestEvent
*event
;
1244 Display
*display
= event
->display
;
1245 Window window
= event
->requestor
;
1246 Atom target
= event
->target
;
1247 Atom selection_atom
= event
->selection
;
1252 x_get_window_property_as_lisp_data (display
, window
, target
,
1253 QMULTIPLE
, selection_atom
));
1257 copy_multiple_data (obj
)
1264 return Fcons (XCAR (obj
), copy_multiple_data (XCDR (obj
)));
1267 vec
= Fmake_vector (size
= XVECTOR (obj
)->size
, Qnil
);
1268 for (i
= 0; i
< size
; i
++)
1270 Lisp_Object vec2
= XVECTOR (obj
)->contents
[i
];
1271 CHECK_VECTOR (vec2
);
1272 if (XVECTOR (vec2
)->size
!= 2)
1273 /* ??? Confusing error message */
1274 signal_error ("Vectors must be of length 2", vec2
);
1275 XVECTOR (vec
)->contents
[i
] = Fmake_vector (2, Qnil
);
1276 XVECTOR (XVECTOR (vec
)->contents
[i
])->contents
[0]
1277 = XVECTOR (vec2
)->contents
[0];
1278 XVECTOR (XVECTOR (vec
)->contents
[i
])->contents
[1]
1279 = XVECTOR (vec2
)->contents
[1];
1287 /* Variables for communication with x_handle_selection_notify. */
1288 static Atom reading_which_selection
;
1289 static Lisp_Object reading_selection_reply
;
1290 static Window reading_selection_window
;
1292 /* Do protocol to read selection-data from the server.
1293 Converts this to Lisp data and returns it. */
1296 x_get_foreign_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
, Lisp_Object time_stamp
)
1298 struct frame
*sf
= SELECTED_FRAME ();
1299 Window requestor_window
;
1301 struct x_display_info
*dpyinfo
;
1302 Time requestor_time
= last_event_timestamp
;
1303 Atom target_property
;
1304 Atom selection_atom
;
1307 int count
= SPECPDL_INDEX ();
1310 if (! FRAME_X_P (sf
))
1313 requestor_window
= FRAME_X_WINDOW (sf
);
1314 display
= FRAME_X_DISPLAY (sf
);
1315 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
1316 target_property
= dpyinfo
->Xatom_EMACS_TMP
;
1317 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection_symbol
);
1319 if (CONSP (target_type
))
1320 type_atom
= symbol_to_x_atom (dpyinfo
, display
, XCAR (target_type
));
1322 type_atom
= symbol_to_x_atom (dpyinfo
, display
, target_type
);
1324 if (! NILP (time_stamp
))
1326 if (CONSP (time_stamp
))
1327 requestor_time
= (Time
) cons_to_long (time_stamp
);
1328 else if (INTEGERP (time_stamp
))
1329 requestor_time
= (Time
) XUINT (time_stamp
);
1330 else if (FLOATP (time_stamp
))
1331 requestor_time
= (Time
) XFLOAT_DATA (time_stamp
);
1333 error ("TIME_STAMP must be cons or number");
1338 /* The protected block contains wait_reading_process_output, which
1339 can run random lisp code (process handlers) or signal.
1340 Therefore, we put the x_uncatch_errors call in an unwind. */
1341 record_unwind_protect (x_catch_errors_unwind
, Qnil
);
1342 x_catch_errors (display
);
1344 TRACE2 ("Get selection %s, type %s",
1345 XGetAtomName (display
, type_atom
),
1346 XGetAtomName (display
, target_property
));
1348 XConvertSelection (display
, selection_atom
, type_atom
, target_property
,
1349 requestor_window
, requestor_time
);
1352 /* Prepare to block until the reply has been read. */
1353 reading_selection_window
= requestor_window
;
1354 reading_which_selection
= selection_atom
;
1355 XSETCAR (reading_selection_reply
, Qnil
);
1357 frame
= some_frame_on_display (dpyinfo
);
1359 /* If the display no longer has frames, we can't expect
1360 to get many more selection requests from it, so don't
1361 bother trying to queue them. */
1364 x_start_queuing_selection_requests ();
1366 record_unwind_protect (queue_selection_requests_unwind
,
1371 /* This allows quits. Also, don't wait forever. */
1372 secs
= x_selection_timeout
/ 1000;
1373 usecs
= (x_selection_timeout
% 1000) * 1000;
1374 TRACE1 (" Start waiting %d secs for SelectionNotify", secs
);
1375 wait_reading_process_output (secs
, usecs
, 0, 0,
1376 reading_selection_reply
, NULL
, 0);
1377 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply
)));
1380 if (x_had_errors_p (display
))
1381 error ("Cannot get selection");
1382 /* This calls x_uncatch_errors. */
1383 unbind_to (count
, Qnil
);
1386 if (NILP (XCAR (reading_selection_reply
)))
1387 error ("Timed out waiting for reply from selection owner");
1388 if (EQ (XCAR (reading_selection_reply
), Qlambda
))
1391 /* Otherwise, the selection is waiting for us on the requested property. */
1393 x_get_window_property_as_lisp_data (display
, requestor_window
,
1394 target_property
, target_type
,
1398 /* Subroutines of x_get_window_property_as_lisp_data */
1400 /* Use xfree, not XFree, to free the data obtained with this function. */
1403 x_get_window_property (Display
*display
, Window window
, Atom property
,
1404 unsigned char **data_ret
, int *bytes_ret
,
1405 Atom
*actual_type_ret
, int *actual_format_ret
,
1406 unsigned long *actual_size_ret
, int delete_p
)
1409 unsigned long bytes_remaining
;
1411 unsigned char *tmp_data
= 0;
1413 int buffer_size
= SELECTION_QUANTUM (display
);
1415 if (buffer_size
> MAX_SELECTION_QUANTUM
)
1416 buffer_size
= MAX_SELECTION_QUANTUM
;
1420 /* First probe the thing to find out how big it is. */
1421 result
= XGetWindowProperty (display
, window
, property
,
1422 0L, 0L, False
, AnyPropertyType
,
1423 actual_type_ret
, actual_format_ret
,
1425 &bytes_remaining
, &tmp_data
);
1426 if (result
!= Success
)
1434 /* This was allocated by Xlib, so use XFree. */
1435 XFree ((char *) tmp_data
);
1437 if (*actual_type_ret
== None
|| *actual_format_ret
== 0)
1443 total_size
= bytes_remaining
+ 1;
1444 *data_ret
= (unsigned char *) xmalloc (total_size
);
1446 /* Now read, until we've gotten it all. */
1447 while (bytes_remaining
)
1449 #ifdef TRACE_SELECTION
1450 int last
= bytes_remaining
;
1453 = XGetWindowProperty (display
, window
, property
,
1454 (long)offset
/4, (long)buffer_size
/4,
1457 actual_type_ret
, actual_format_ret
,
1458 actual_size_ret
, &bytes_remaining
, &tmp_data
);
1460 TRACE2 ("Read %ld bytes from property %s",
1461 last
- bytes_remaining
,
1462 XGetAtomName (display
, property
));
1464 /* If this doesn't return Success at this point, it means that
1465 some clod deleted the selection while we were in the midst of
1466 reading it. Deal with that, I guess.... */
1467 if (result
!= Success
)
1470 /* The man page for XGetWindowProperty says:
1471 "If the returned format is 32, the returned data is represented
1472 as a long array and should be cast to that type to obtain the
1474 This applies even if long is more than 32 bits, the X library
1475 converts from 32 bit elements received from the X server to long
1476 and passes the long array to us. Thus, for that case memcpy can not
1477 be used. We convert to a 32 bit type here, because so much code
1480 The bytes and offsets passed to XGetWindowProperty refers to the
1481 property and those are indeed in 32 bit quantities if format is 32. */
1483 if (*actual_format_ret
== 32 && *actual_format_ret
< BITS_PER_LONG
)
1486 int *idata
= (int *) ((*data_ret
) + offset
);
1487 long *ldata
= (long *) tmp_data
;
1489 for (i
= 0; i
< *actual_size_ret
; ++i
)
1491 idata
[i
]= (int) ldata
[i
];
1497 *actual_size_ret
*= *actual_format_ret
/ 8;
1498 memcpy ((*data_ret
) + offset
, tmp_data
, *actual_size_ret
);
1499 offset
+= *actual_size_ret
;
1502 /* This was allocated by Xlib, so use XFree. */
1503 XFree ((char *) tmp_data
);
1508 *bytes_ret
= offset
;
1511 /* Use xfree, not XFree, to free the data obtained with this function. */
1514 receive_incremental_selection (Display
*display
, Window window
, Atom property
,
1515 Lisp_Object target_type
,
1516 unsigned int min_size_bytes
,
1517 unsigned char **data_ret
, int *size_bytes_ret
,
1518 Atom
*type_ret
, int *format_ret
,
1519 unsigned long *size_ret
)
1522 struct prop_location
*wait_object
;
1523 *size_bytes_ret
= min_size_bytes
;
1524 *data_ret
= (unsigned char *) xmalloc (*size_bytes_ret
);
1526 TRACE1 ("Read %d bytes incrementally", min_size_bytes
);
1528 /* At this point, we have read an INCR property.
1529 Delete the property to ack it.
1530 (But first, prepare to receive the next event in this handshake.)
1532 Now, we must loop, waiting for the sending window to put a value on
1533 that property, then reading the property, then deleting it to ack.
1534 We are done when the sender places a property of length 0.
1537 XSelectInput (display
, window
, STANDARD_EVENT_SET
| PropertyChangeMask
);
1538 TRACE1 (" Delete property %s",
1539 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1540 XDeleteProperty (display
, window
, property
);
1541 TRACE1 (" Expect new value of property %s",
1542 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1543 wait_object
= expect_property_change (display
, window
, property
,
1550 unsigned char *tmp_data
;
1553 TRACE0 (" Wait for property change");
1554 wait_for_property_change (wait_object
);
1556 /* expect it again immediately, because x_get_window_property may
1557 .. no it won't, I don't get it.
1558 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1559 TRACE0 (" Get property value");
1560 x_get_window_property (display
, window
, property
,
1561 &tmp_data
, &tmp_size_bytes
,
1562 type_ret
, format_ret
, size_ret
, 1);
1564 TRACE1 (" Read increment of %d bytes", tmp_size_bytes
);
1566 if (tmp_size_bytes
== 0) /* we're done */
1568 TRACE0 ("Done reading incrementally");
1570 if (! waiting_for_other_props_on_window (display
, window
))
1571 XSelectInput (display
, window
, STANDARD_EVENT_SET
);
1572 /* Use xfree, not XFree, because x_get_window_property
1573 calls xmalloc itself. */
1579 TRACE1 (" ACK by deleting property %s",
1580 XGetAtomName (display
, property
));
1581 XDeleteProperty (display
, window
, property
);
1582 wait_object
= expect_property_change (display
, window
, property
,
1587 if (*size_bytes_ret
< offset
+ tmp_size_bytes
)
1589 *size_bytes_ret
= offset
+ tmp_size_bytes
;
1590 *data_ret
= (unsigned char *) xrealloc (*data_ret
, *size_bytes_ret
);
1593 memcpy ((*data_ret
) + offset
, tmp_data
, tmp_size_bytes
);
1594 offset
+= tmp_size_bytes
;
1596 /* Use xfree, not XFree, because x_get_window_property
1597 calls xmalloc itself. */
1603 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1604 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1605 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1608 x_get_window_property_as_lisp_data (Display
*display
, Window window
,
1610 Lisp_Object target_type
,
1611 Atom selection_atom
)
1615 unsigned long actual_size
;
1616 unsigned char *data
= 0;
1619 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1621 TRACE0 ("Reading selection data");
1623 x_get_window_property (display
, window
, property
, &data
, &bytes
,
1624 &actual_type
, &actual_format
, &actual_size
, 1);
1627 int there_is_a_selection_owner
;
1629 there_is_a_selection_owner
1630 = XGetSelectionOwner (display
, selection_atom
);
1632 if (there_is_a_selection_owner
)
1633 signal_error ("Selection owner couldn't convert",
1635 ? list2 (target_type
,
1636 x_atom_to_symbol (display
, actual_type
))
1639 signal_error ("No selection",
1640 x_atom_to_symbol (display
, selection_atom
));
1643 if (actual_type
== dpyinfo
->Xatom_INCR
)
1645 /* That wasn't really the data, just the beginning. */
1647 unsigned int min_size_bytes
= * ((unsigned int *) data
);
1649 /* Use xfree, not XFree, because x_get_window_property
1650 calls xmalloc itself. */
1651 xfree ((char *) data
);
1653 receive_incremental_selection (display
, window
, property
, target_type
,
1654 min_size_bytes
, &data
, &bytes
,
1655 &actual_type
, &actual_format
,
1660 TRACE1 (" Delete property %s", XGetAtomName (display
, property
));
1661 XDeleteProperty (display
, window
, property
);
1665 /* It's been read. Now convert it to a lisp object in some semi-rational
1667 val
= selection_data_to_lisp_data (display
, data
, bytes
,
1668 actual_type
, actual_format
);
1670 /* Use xfree, not XFree, because x_get_window_property
1671 calls xmalloc itself. */
1672 xfree ((char *) data
);
1676 /* These functions convert from the selection data read from the server into
1677 something that we can use from Lisp, and vice versa.
1679 Type: Format: Size: Lisp Type:
1680 ----- ------- ----- -----------
1683 ATOM 32 > 1 Vector of Symbols
1685 * 16 > 1 Vector of Integers
1686 * 32 1 if <=16 bits: Integer
1687 if > 16 bits: Cons of top16, bot16
1688 * 32 > 1 Vector of the above
1690 When converting a Lisp number to C, it is assumed to be of format 16 if
1691 it is an integer, and of format 32 if it is a cons of two integers.
1693 When converting a vector of numbers from Lisp to C, it is assumed to be
1694 of format 16 if every element in the vector is an integer, and is assumed
1695 to be of format 32 if any element is a cons of two integers.
1697 When converting an object to C, it may be of the form (SYMBOL . <data>)
1698 where SYMBOL is what we should claim that the type is. Format and
1699 representation are as above.
1701 Important: When format is 32, data should contain an array of int,
1702 not an array of long as the X library returns. This makes a difference
1703 when sizeof(long) != sizeof(int). */
1708 selection_data_to_lisp_data (Display
*display
, const unsigned char *data
,
1709 int size
, Atom type
, int format
)
1711 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1713 if (type
== dpyinfo
->Xatom_NULL
)
1716 /* Convert any 8-bit data to a string, for compactness. */
1717 else if (format
== 8)
1719 Lisp_Object str
, lispy_type
;
1721 str
= make_unibyte_string ((char *) data
, size
);
1722 /* Indicate that this string is from foreign selection by a text
1723 property `foreign-selection' so that the caller of
1724 x-get-selection-internal (usually x-get-selection) can know
1725 that the string must be decode. */
1726 if (type
== dpyinfo
->Xatom_COMPOUND_TEXT
)
1727 lispy_type
= QCOMPOUND_TEXT
;
1728 else if (type
== dpyinfo
->Xatom_UTF8_STRING
)
1729 lispy_type
= QUTF8_STRING
;
1731 lispy_type
= QSTRING
;
1732 Fput_text_property (make_number (0), make_number (size
),
1733 Qforeign_selection
, lispy_type
, str
);
1736 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1737 a vector of symbols.
1739 else if (type
== XA_ATOM
)
1742 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1743 But the callers of these function has made sure the data for
1744 format == 32 is an array of int. Thus, use int instead
1746 int *idata
= (int *) data
;
1748 if (size
== sizeof (int))
1749 return x_atom_to_symbol (display
, (Atom
) idata
[0]);
1752 Lisp_Object v
= Fmake_vector (make_number (size
/ sizeof (int)),
1754 for (i
= 0; i
< size
/ sizeof (int); i
++)
1755 Faset (v
, make_number (i
),
1756 x_atom_to_symbol (display
, (Atom
) idata
[i
]));
1761 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1762 If the number is 32 bits and won't fit in a Lisp_Int,
1763 convert it to a cons of integers, 16 bits in each half.
1765 else if (format
== 32 && size
== sizeof (int))
1766 return long_to_cons (((unsigned int *) data
) [0]);
1767 else if (format
== 16 && size
== sizeof (short))
1768 return make_number ((int) (((unsigned short *) data
) [0]));
1770 /* Convert any other kind of data to a vector of numbers, represented
1771 as above (as an integer, or a cons of two 16 bit integers.)
1773 else if (format
== 16)
1777 v
= Fmake_vector (make_number (size
/ 2), make_number (0));
1778 for (i
= 0; i
< size
/ 2; i
++)
1780 int j
= (int) ((unsigned short *) data
) [i
];
1781 Faset (v
, make_number (i
), make_number (j
));
1788 Lisp_Object v
= Fmake_vector (make_number (size
/ 4), make_number (0));
1789 for (i
= 0; i
< size
/ 4; i
++)
1791 unsigned int j
= ((unsigned int *) data
) [i
];
1792 Faset (v
, make_number (i
), long_to_cons (j
));
1799 /* Use xfree, not XFree, to free the data obtained with this function. */
1802 lisp_data_to_selection_data (Display
*display
, Lisp_Object obj
,
1803 unsigned char **data_ret
, Atom
*type_ret
,
1804 unsigned int *size_ret
,
1805 int *format_ret
, int *nofree_ret
)
1807 Lisp_Object type
= Qnil
;
1808 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1812 if (CONSP (obj
) && SYMBOLP (XCAR (obj
)))
1816 if (CONSP (obj
) && NILP (XCDR (obj
)))
1820 if (EQ (obj
, QNULL
) || (EQ (type
, QNULL
)))
1821 { /* This is not the same as declining */
1827 else if (STRINGP (obj
))
1829 if (SCHARS (obj
) < SBYTES (obj
))
1830 /* OBJ is a multibyte string containing a non-ASCII char. */
1831 signal_error ("Non-ASCII string must be encoded in advance", obj
);
1835 *size_ret
= SBYTES (obj
);
1836 *data_ret
= SDATA (obj
);
1839 else if (SYMBOLP (obj
))
1843 *data_ret
= (unsigned char *) xmalloc (sizeof (Atom
) + 1);
1844 (*data_ret
) [sizeof (Atom
)] = 0;
1845 (*(Atom
**) data_ret
) [0] = symbol_to_x_atom (dpyinfo
, display
, obj
);
1846 if (NILP (type
)) type
= QATOM
;
1848 else if (INTEGERP (obj
)
1849 && XINT (obj
) < 0xFFFF
1850 && XINT (obj
) > -0xFFFF)
1854 *data_ret
= (unsigned char *) xmalloc (sizeof (short) + 1);
1855 (*data_ret
) [sizeof (short)] = 0;
1856 (*(short **) data_ret
) [0] = (short) XINT (obj
);
1857 if (NILP (type
)) type
= QINTEGER
;
1859 else if (INTEGERP (obj
)
1860 || (CONSP (obj
) && INTEGERP (XCAR (obj
))
1861 && (INTEGERP (XCDR (obj
))
1862 || (CONSP (XCDR (obj
))
1863 && INTEGERP (XCAR (XCDR (obj
)))))))
1867 *data_ret
= (unsigned char *) xmalloc (sizeof (long) + 1);
1868 (*data_ret
) [sizeof (long)] = 0;
1869 (*(unsigned long **) data_ret
) [0] = cons_to_long (obj
);
1870 if (NILP (type
)) type
= QINTEGER
;
1872 else if (VECTORP (obj
))
1874 /* Lisp_Vectors may represent a set of ATOMs;
1875 a set of 16 or 32 bit INTEGERs;
1876 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1880 if (SYMBOLP (XVECTOR (obj
)->contents
[0]))
1881 /* This vector is an ATOM set */
1883 if (NILP (type
)) type
= QATOM
;
1884 *size_ret
= XVECTOR (obj
)->size
;
1886 *data_ret
= (unsigned char *) xmalloc ((*size_ret
) * sizeof (Atom
));
1887 for (i
= 0; i
< *size_ret
; i
++)
1888 if (SYMBOLP (XVECTOR (obj
)->contents
[i
]))
1889 (*(Atom
**) data_ret
) [i
]
1890 = symbol_to_x_atom (dpyinfo
, display
, XVECTOR (obj
)->contents
[i
]);
1892 signal_error ("All elements of selection vector must have same type", obj
);
1894 #if 0 /* #### MULTIPLE doesn't work yet */
1895 else if (VECTORP (XVECTOR (obj
)->contents
[0]))
1896 /* This vector is an ATOM_PAIR set */
1898 if (NILP (type
)) type
= QATOM_PAIR
;
1899 *size_ret
= XVECTOR (obj
)->size
;
1901 *data_ret
= (unsigned char *)
1902 xmalloc ((*size_ret
) * sizeof (Atom
) * 2);
1903 for (i
= 0; i
< *size_ret
; i
++)
1904 if (VECTORP (XVECTOR (obj
)->contents
[i
]))
1906 Lisp_Object pair
= XVECTOR (obj
)->contents
[i
];
1907 if (XVECTOR (pair
)->size
!= 2)
1909 "Elements of the vector must be vectors of exactly two elements",
1912 (*(Atom
**) data_ret
) [i
* 2]
1913 = symbol_to_x_atom (dpyinfo
, display
,
1914 XVECTOR (pair
)->contents
[0]);
1915 (*(Atom
**) data_ret
) [(i
* 2) + 1]
1916 = symbol_to_x_atom (dpyinfo
, display
,
1917 XVECTOR (pair
)->contents
[1]);
1920 signal_error ("All elements of the vector must be of the same type",
1926 /* This vector is an INTEGER set, or something like it */
1929 *size_ret
= XVECTOR (obj
)->size
;
1930 if (NILP (type
)) type
= QINTEGER
;
1932 for (i
= 0; i
< *size_ret
; i
++)
1933 if (CONSP (XVECTOR (obj
)->contents
[i
]))
1935 else if (!INTEGERP (XVECTOR (obj
)->contents
[i
]))
1936 signal_error (/* Qselection_error */
1937 "Elements of selection vector must be integers or conses of integers",
1940 /* Use sizeof(long) even if it is more than 32 bits. See comment
1941 in x_get_window_property and x_fill_property_data. */
1943 if (*format_ret
== 32) data_size
= sizeof(long);
1944 *data_ret
= (unsigned char *) xmalloc (*size_ret
* data_size
);
1945 for (i
= 0; i
< *size_ret
; i
++)
1946 if (*format_ret
== 32)
1947 (*((unsigned long **) data_ret
)) [i
]
1948 = cons_to_long (XVECTOR (obj
)->contents
[i
]);
1950 (*((unsigned short **) data_ret
)) [i
]
1951 = (unsigned short) cons_to_long (XVECTOR (obj
)->contents
[i
]);
1955 signal_error (/* Qselection_error */ "Unrecognized selection data", obj
);
1957 *type_ret
= symbol_to_x_atom (dpyinfo
, display
, type
);
1961 clean_local_selection_data (Lisp_Object obj
)
1964 && INTEGERP (XCAR (obj
))
1965 && CONSP (XCDR (obj
))
1966 && INTEGERP (XCAR (XCDR (obj
)))
1967 && NILP (XCDR (XCDR (obj
))))
1968 obj
= Fcons (XCAR (obj
), XCDR (obj
));
1971 && INTEGERP (XCAR (obj
))
1972 && INTEGERP (XCDR (obj
)))
1974 if (XINT (XCAR (obj
)) == 0)
1976 if (XINT (XCAR (obj
)) == -1)
1977 return make_number (- XINT (XCDR (obj
)));
1982 int size
= XVECTOR (obj
)->size
;
1985 return clean_local_selection_data (XVECTOR (obj
)->contents
[0]);
1986 copy
= Fmake_vector (make_number (size
), Qnil
);
1987 for (i
= 0; i
< size
; i
++)
1988 XVECTOR (copy
)->contents
[i
]
1989 = clean_local_selection_data (XVECTOR (obj
)->contents
[i
]);
1995 /* Called from XTread_socket to handle SelectionNotify events.
1996 If it's the selection we are waiting for, stop waiting
1997 by setting the car of reading_selection_reply to non-nil.
1998 We store t there if the reply is successful, lambda if not. */
2001 x_handle_selection_notify (XSelectionEvent
*event
)
2003 if (event
->requestor
!= reading_selection_window
)
2005 if (event
->selection
!= reading_which_selection
)
2008 TRACE0 ("Received SelectionNotify");
2009 XSETCAR (reading_selection_reply
,
2010 (event
->property
!= 0 ? Qt
: Qlambda
));
2014 DEFUN ("x-own-selection-internal", Fx_own_selection_internal
,
2015 Sx_own_selection_internal
, 2, 2, 0,
2016 doc
: /* Assert an X selection of the given TYPE with the given VALUE.
2017 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2018 \(Those are literal upper-case symbol names, since that's what X expects.)
2019 VALUE is typically a string, or a cons of two markers, but may be
2020 anything that the functions on `selection-converter-alist' know about. */)
2021 (Lisp_Object selection_name
, Lisp_Object selection_value
)
2024 CHECK_SYMBOL (selection_name
);
2025 if (NILP (selection_value
)) error ("SELECTION-VALUE may not be nil");
2026 x_own_selection (selection_name
, selection_value
);
2027 return selection_value
;
2031 /* Request the selection value from the owner. If we are the owner,
2032 simply return our selection value. If we are not the owner, this
2033 will block until all of the data has arrived. */
2035 DEFUN ("x-get-selection-internal", Fx_get_selection_internal
,
2036 Sx_get_selection_internal
, 2, 3, 0,
2037 doc
: /* Return text selected from some X window.
2038 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2039 \(Those are literal upper-case symbol names, since that's what X expects.)
2040 TYPE is the type of data desired, typically `STRING'.
2041 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2042 selections. If omitted, defaults to the time for the last event. */)
2043 (Lisp_Object selection_symbol
, Lisp_Object target_type
, Lisp_Object time_stamp
)
2045 Lisp_Object val
= Qnil
;
2046 struct gcpro gcpro1
, gcpro2
;
2047 GCPRO2 (target_type
, val
); /* we store newly consed data into these */
2049 CHECK_SYMBOL (selection_symbol
);
2051 #if 0 /* #### MULTIPLE doesn't work yet */
2052 if (CONSP (target_type
)
2053 && XCAR (target_type
) == QMULTIPLE
)
2055 CHECK_VECTOR (XCDR (target_type
));
2056 /* So we don't destructively modify this... */
2057 target_type
= copy_multiple_data (target_type
);
2061 CHECK_SYMBOL (target_type
);
2063 val
= x_get_local_selection (selection_symbol
, target_type
, 1);
2067 val
= x_get_foreign_selection (selection_symbol
, target_type
, time_stamp
);
2072 && SYMBOLP (XCAR (val
)))
2075 if (CONSP (val
) && NILP (XCDR (val
)))
2078 val
= clean_local_selection_data (val
);
2084 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal
,
2085 Sx_disown_selection_internal
, 1, 2, 0,
2086 doc
: /* If we own the selection SELECTION, disown it.
2087 Disowning it means there is no such selection. */)
2088 (Lisp_Object selection
, Lisp_Object time
)
2091 Atom selection_atom
;
2093 struct selection_input_event sie
;
2094 struct input_event ie
;
2097 struct x_display_info
*dpyinfo
;
2098 struct frame
*sf
= SELECTED_FRAME ();
2101 if (! FRAME_X_P (sf
))
2104 display
= FRAME_X_DISPLAY (sf
);
2105 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
2106 CHECK_SYMBOL (selection
);
2108 timestamp
= last_event_timestamp
;
2110 timestamp
= cons_to_long (time
);
2112 if (NILP (assq_no_quit (selection
, Vselection_alist
)))
2113 return Qnil
; /* Don't disown the selection when we're not the owner. */
2115 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection
);
2118 XSetSelectionOwner (display
, selection_atom
, None
, timestamp
);
2121 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2122 generated for a window which owns the selection when that window sets
2123 the selection owner to None. The NCD server does, the MIT Sun4 server
2124 doesn't. So we synthesize one; this means we might get two, but
2125 that's ok, because the second one won't have any effect. */
2126 SELECTION_EVENT_DISPLAY (&event
.sie
) = display
;
2127 SELECTION_EVENT_SELECTION (&event
.sie
) = selection_atom
;
2128 SELECTION_EVENT_TIME (&event
.sie
) = timestamp
;
2129 x_handle_selection_clear (&event
.ie
);
2134 /* Get rid of all the selections in buffer BUFFER.
2135 This is used when we kill a buffer. */
2138 x_disown_buffer_selections (Lisp_Object buffer
)
2141 struct buffer
*buf
= XBUFFER (buffer
);
2143 for (tail
= Vselection_alist
; CONSP (tail
); tail
= XCDR (tail
))
2145 Lisp_Object elt
, value
;
2148 if (CONSP (value
) && MARKERP (XCAR (value
))
2149 && XMARKER (XCAR (value
))->buffer
== buf
)
2150 Fx_disown_selection_internal (XCAR (elt
), Qnil
);
2154 DEFUN ("x-selection-owner-p", Fx_selection_owner_p
, Sx_selection_owner_p
,
2156 doc
: /* Whether the current Emacs process owns the given X Selection.
2157 The arg should be the name of the selection in question, typically one of
2158 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2159 \(Those are literal upper-case symbol names, since that's what X expects.)
2160 For convenience, the symbol nil is the same as `PRIMARY',
2161 and t is the same as `SECONDARY'. */)
2162 (Lisp_Object selection
)
2165 CHECK_SYMBOL (selection
);
2166 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2167 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2169 if (NILP (Fassq (selection
, Vselection_alist
)))
2174 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
2176 doc
: /* Whether there is an owner for the given X Selection.
2177 The arg should be the name of the selection in question, typically one of
2178 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2179 \(Those are literal upper-case symbol names, since that's what X expects.)
2180 For convenience, the symbol nil is the same as `PRIMARY',
2181 and t is the same as `SECONDARY'. */)
2182 (Lisp_Object selection
)
2187 struct frame
*sf
= SELECTED_FRAME ();
2189 /* It should be safe to call this before we have an X frame. */
2190 if (! FRAME_X_P (sf
))
2193 dpy
= FRAME_X_DISPLAY (sf
);
2194 CHECK_SYMBOL (selection
);
2195 if (!NILP (Fx_selection_owner_p (selection
)))
2197 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2198 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2199 atom
= symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf
), dpy
, selection
);
2203 owner
= XGetSelectionOwner (dpy
, atom
);
2205 return (owner
? Qt
: Qnil
);
2209 /***********************************************************************
2210 Drag and drop support
2211 ***********************************************************************/
2212 /* Check that lisp values are of correct type for x_fill_property_data.
2213 That is, number, string or a cons with two numbers (low and high 16
2214 bit parts of a 32 bit number). */
2217 x_check_property_data (Lisp_Object data
)
2222 for (iter
= data
; CONSP (iter
) && size
!= -1; iter
= XCDR (iter
), ++size
)
2224 Lisp_Object o
= XCAR (iter
);
2226 if (! NUMBERP (o
) && ! STRINGP (o
) && ! CONSP (o
))
2228 else if (CONSP (o
) &&
2229 (! NUMBERP (XCAR (o
)) || ! NUMBERP (XCDR (o
))))
2236 /* Convert lisp values to a C array. Values may be a number, a string
2237 which is taken as an X atom name and converted to the atom value, or
2238 a cons containing the two 16 bit parts of a 32 bit number.
2240 DPY is the display use to look up X atoms.
2241 DATA is a Lisp list of values to be converted.
2242 RET is the C array that contains the converted values. It is assumed
2243 it is big enough to hold all values.
2244 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2245 be stored in RET. Note that long is used for 32 even if long is more
2246 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2247 XClientMessageEvent). */
2250 x_fill_property_data (Display
*dpy
, Lisp_Object data
, void *ret
, int format
)
2253 long *d32
= (long *) ret
;
2254 short *d16
= (short *) ret
;
2255 char *d08
= (char *) ret
;
2258 for (iter
= data
; CONSP (iter
); iter
= XCDR (iter
))
2260 Lisp_Object o
= XCAR (iter
);
2263 val
= (long) XFASTINT (o
);
2264 else if (FLOATP (o
))
2265 val
= (long) XFLOAT_DATA (o
);
2267 val
= (long) cons_to_long (o
);
2268 else if (STRINGP (o
))
2271 val
= (long) XInternAtom (dpy
, SSDATA (o
), False
);
2275 error ("Wrong type, must be string, number or cons");
2278 *d08
++ = (char) val
;
2279 else if (format
== 16)
2280 *d16
++ = (short) val
;
2286 /* Convert an array of C values to a Lisp list.
2287 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2288 DATA is a C array of values to be converted.
2289 TYPE is the type of the data. Only XA_ATOM is special, it converts
2290 each number in DATA to its corresponfing X atom as a symbol.
2291 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2293 SIZE is the number of elements in DATA.
2295 Important: When format is 32, data should contain an array of int,
2296 not an array of long as the X library returns. This makes a difference
2297 when sizeof(long) != sizeof(int).
2299 Also see comment for selection_data_to_lisp_data above. */
2302 x_property_data_to_lisp (struct frame
*f
, const unsigned char *data
,
2303 Atom type
, int format
, long unsigned int size
)
2305 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f
),
2306 data
, size
*format
/8, type
, format
);
2309 /* Get the mouse position in frame relative coordinates. */
2312 mouse_position_for_drop (FRAME_PTR f
, int *x
, int *y
)
2314 Window root
, dummy_window
;
2319 XQueryPointer (FRAME_X_DISPLAY (f
),
2320 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
2322 /* The root window which contains the pointer. */
2325 /* Window pointer is on, not used */
2328 /* The position on that root window. */
2331 /* x/y in dummy_window coordinates, not used. */
2334 /* Modifier keys and pointer buttons, about which
2336 (unsigned int *) &dummy
);
2339 /* Absolute to relative. */
2340 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2341 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2346 DEFUN ("x-get-atom-name", Fx_get_atom_name
,
2347 Sx_get_atom_name
, 1, 2, 0,
2348 doc
: /* Return the X atom name for VALUE as a string.
2349 VALUE may be a number or a cons where the car is the upper 16 bits and
2350 the cdr is the lower 16 bits of a 32 bit value.
2351 Use the display for FRAME or the current frame if FRAME is not given or nil.
2353 If the value is 0 or the atom is not known, return the empty string. */)
2354 (Lisp_Object value
, Lisp_Object frame
)
2356 struct frame
*f
= check_x_frame (frame
);
2359 Lisp_Object ret
= Qnil
;
2360 Display
*dpy
= FRAME_X_DISPLAY (f
);
2364 if (INTEGERP (value
))
2365 atom
= (Atom
) XUINT (value
);
2366 else if (FLOATP (value
))
2367 atom
= (Atom
) XFLOAT_DATA (value
);
2368 else if (CONSP (value
))
2369 atom
= (Atom
) cons_to_long (value
);
2371 error ("Wrong type, value must be number or cons");
2374 x_catch_errors (dpy
);
2375 name
= atom
? XGetAtomName (dpy
, atom
) : empty
;
2376 had_errors
= x_had_errors_p (dpy
);
2377 x_uncatch_errors ();
2380 ret
= make_string (name
, strlen (name
));
2382 if (atom
&& name
) XFree (name
);
2383 if (NILP (ret
)) ret
= empty_unibyte_string
;
2390 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom
,
2391 Sx_register_dnd_atom
, 1, 2, 0,
2392 doc
: /* Request that dnd events are made for ClientMessages with ATOM.
2393 ATOM can be a symbol or a string. The ATOM is interned on the display that
2394 FRAME is on. If FRAME is nil, the selected frame is used. */)
2395 (Lisp_Object atom
, Lisp_Object frame
)
2398 struct frame
*f
= check_x_frame (frame
);
2400 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2404 x_atom
= symbol_to_x_atom (dpyinfo
, FRAME_X_DISPLAY (f
), atom
);
2405 else if (STRINGP (atom
))
2408 x_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SSDATA (atom
), False
);
2412 error ("ATOM must be a symbol or a string");
2414 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2415 if (dpyinfo
->x_dnd_atoms
[i
] == x_atom
)
2418 if (dpyinfo
->x_dnd_atoms_length
== dpyinfo
->x_dnd_atoms_size
)
2420 dpyinfo
->x_dnd_atoms_size
*= 2;
2421 dpyinfo
->x_dnd_atoms
= xrealloc (dpyinfo
->x_dnd_atoms
,
2422 sizeof (*dpyinfo
->x_dnd_atoms
)
2423 * dpyinfo
->x_dnd_atoms_size
);
2426 dpyinfo
->x_dnd_atoms
[dpyinfo
->x_dnd_atoms_length
++] = x_atom
;
2430 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2433 x_handle_dnd_message (struct frame
*f
, XClientMessageEvent
*event
, struct x_display_info
*dpyinfo
, struct input_event
*bufp
)
2437 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2438 unsigned long size
= 160/event
->format
;
2440 unsigned char *data
= (unsigned char *) event
->data
.b
;
2444 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2445 if (dpyinfo
->x_dnd_atoms
[i
] == event
->message_type
) break;
2447 if (i
== dpyinfo
->x_dnd_atoms_length
) return 0;
2449 XSETFRAME (frame
, f
);
2451 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2452 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2453 function expects them to be of size int (i.e. 32). So to be able to
2454 use that function, put the data in the form it expects if format is 32. */
2456 if (event
->format
== 32 && event
->format
< BITS_PER_LONG
)
2459 for (i
= 0; i
< 5; ++i
) /* There are only 5 longs in a ClientMessage. */
2460 idata
[i
] = (int) event
->data
.l
[i
];
2461 data
= (unsigned char *) idata
;
2464 vec
= Fmake_vector (make_number (4), Qnil
);
2465 ASET (vec
, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f
),
2466 event
->message_type
)));
2467 ASET (vec
, 1, frame
);
2468 ASET (vec
, 2, make_number (event
->format
));
2469 ASET (vec
, 3, x_property_data_to_lisp (f
,
2471 event
->message_type
,
2475 mouse_position_for_drop (f
, &x
, &y
);
2476 bufp
->kind
= DRAG_N_DROP_EVENT
;
2477 bufp
->frame_or_window
= frame
;
2478 bufp
->timestamp
= CurrentTime
;
2479 bufp
->x
= make_number (x
);
2480 bufp
->y
= make_number (y
);
2482 bufp
->modifiers
= 0;
2487 DEFUN ("x-send-client-message", Fx_send_client_event
,
2488 Sx_send_client_message
, 6, 6, 0,
2489 doc
: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2491 For DISPLAY, specify either a frame or a display name (a string).
2492 If DISPLAY is nil, that stands for the selected frame's display.
2493 DEST may be a number, in which case it is a Window id. The value 0 may
2494 be used to send to the root window of the DISPLAY.
2495 If DEST is a cons, it is converted to a 32 bit number
2496 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2497 number is then used as a window id.
2498 If DEST is a frame the event is sent to the outer window of that frame.
2499 A value of nil means the currently selected frame.
2500 If DEST is the string "PointerWindow" the event is sent to the window that
2501 contains the pointer. If DEST is the string "InputFocus" the event is
2502 sent to the window that has the input focus.
2503 FROM is the frame sending the event. Use nil for currently selected frame.
2504 MESSAGE-TYPE is the name of an Atom as a string.
2505 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2506 bits. VALUES is a list of numbers, cons and/or strings containing the values
2507 to send. If a value is a string, it is converted to an Atom and the value of
2508 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2509 with the high 16 bits from the car and the lower 16 bit from the cdr.
2510 If more values than fits into the event is given, the excessive values
2512 (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
, Lisp_Object message_type
, Lisp_Object format
, Lisp_Object values
)
2514 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2516 CHECK_STRING (message_type
);
2517 x_send_client_event(display
, dest
, from
,
2518 XInternAtom (dpyinfo
->display
,
2519 SSDATA (message_type
),
2527 x_send_client_event (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
, Atom message_type
, Lisp_Object format
, Lisp_Object values
)
2529 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2532 struct frame
*f
= check_x_frame (from
);
2535 CHECK_NUMBER (format
);
2536 CHECK_CONS (values
);
2538 if (x_check_property_data (values
) == -1)
2539 error ("Bad data in VALUES, must be number, cons or string");
2541 event
.xclient
.type
= ClientMessage
;
2542 event
.xclient
.format
= XFASTINT (format
);
2544 if (event
.xclient
.format
!= 8 && event
.xclient
.format
!= 16
2545 && event
.xclient
.format
!= 32)
2546 error ("FORMAT must be one of 8, 16 or 32");
2548 if (FRAMEP (dest
) || NILP (dest
))
2550 struct frame
*fdest
= check_x_frame (dest
);
2551 wdest
= FRAME_OUTER_WINDOW (fdest
);
2553 else if (STRINGP (dest
))
2555 if (strcmp (SSDATA (dest
), "PointerWindow") == 0)
2556 wdest
= PointerWindow
;
2557 else if (strcmp (SSDATA (dest
), "InputFocus") == 0)
2560 error ("DEST as a string must be one of PointerWindow or InputFocus");
2562 else if (INTEGERP (dest
))
2563 wdest
= (Window
) XFASTINT (dest
);
2564 else if (FLOATP (dest
))
2565 wdest
= (Window
) XFLOAT_DATA (dest
);
2566 else if (CONSP (dest
))
2568 if (! NUMBERP (XCAR (dest
)) || ! NUMBERP (XCDR (dest
)))
2569 error ("Both car and cdr for DEST must be numbers");
2571 wdest
= (Window
) cons_to_long (dest
);
2574 error ("DEST must be a frame, nil, string, number or cons");
2576 if (wdest
== 0) wdest
= dpyinfo
->root_window
;
2577 to_root
= wdest
== dpyinfo
->root_window
;
2581 event
.xclient
.message_type
= message_type
;
2582 event
.xclient
.display
= dpyinfo
->display
;
2584 /* Some clients (metacity for example) expects sending window to be here
2585 when sending to the root window. */
2586 event
.xclient
.window
= to_root
? FRAME_OUTER_WINDOW (f
) : wdest
;
2589 memset (event
.xclient
.data
.b
, 0, sizeof (event
.xclient
.data
.b
));
2590 x_fill_property_data (dpyinfo
->display
, values
, event
.xclient
.data
.b
,
2591 event
.xclient
.format
);
2593 /* If event mask is 0 the event is sent to the client that created
2594 the destination window. But if we are sending to the root window,
2595 there is no such client. Then we set the event mask to 0xffff. The
2596 event then goes to clients selecting for events on the root window. */
2597 x_catch_errors (dpyinfo
->display
);
2599 int propagate
= to_root
? False
: True
;
2600 unsigned mask
= to_root
? 0xffff : 0;
2601 XSendEvent (dpyinfo
->display
, wdest
, propagate
, mask
, &event
);
2602 XFlush (dpyinfo
->display
);
2604 x_uncatch_errors ();
2610 syms_of_xselect (void)
2612 defsubr (&Sx_get_selection_internal
);
2613 defsubr (&Sx_own_selection_internal
);
2614 defsubr (&Sx_disown_selection_internal
);
2615 defsubr (&Sx_selection_owner_p
);
2616 defsubr (&Sx_selection_exists_p
);
2618 defsubr (&Sx_get_atom_name
);
2619 defsubr (&Sx_send_client_message
);
2620 defsubr (&Sx_register_dnd_atom
);
2622 reading_selection_reply
= Fcons (Qnil
, Qnil
);
2623 staticpro (&reading_selection_reply
);
2624 reading_selection_window
= 0;
2625 reading_which_selection
= 0;
2627 property_change_wait_list
= 0;
2628 prop_location_identifier
= 0;
2629 property_change_reply
= Fcons (Qnil
, Qnil
);
2630 staticpro (&property_change_reply
);
2632 Vselection_alist
= Qnil
;
2633 staticpro (&Vselection_alist
);
2635 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist
,
2636 doc
: /* An alist associating X Windows selection-types with functions.
2637 These functions are called to convert the selection, with three args:
2638 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2639 a desired type to which the selection should be converted;
2640 and the local selection value (whatever was given to `x-own-selection').
2642 The function should return the value to send to the X server
2643 \(typically a string). A return value of nil
2644 means that the conversion could not be done.
2645 A return value which is the symbol `NULL'
2646 means that a side-effect was executed,
2647 and there is no meaningful selection value. */);
2648 Vselection_converter_alist
= Qnil
;
2650 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions
,
2651 doc
: /* A list of functions to be called when Emacs loses an X selection.
2652 \(This happens when some other X client makes its own selection
2653 or when a Lisp program explicitly clears the selection.)
2654 The functions are called with one argument, the selection type
2655 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2656 Vx_lost_selection_functions
= Qnil
;
2658 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions
,
2659 doc
: /* A list of functions to be called when Emacs answers a selection request.
2660 The functions are called with four arguments:
2661 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2662 - the selection-type which Emacs was asked to convert the
2663 selection into before sending (for example, `STRING' or `LENGTH');
2664 - a flag indicating success or failure for responding to the request.
2665 We might have failed (and declined the request) for any number of reasons,
2666 including being asked for a selection that we no longer own, or being asked
2667 to convert into a type that we don't know about or that is inappropriate.
2668 This hook doesn't let you change the behavior of Emacs's selection replies,
2669 it merely informs you that they have happened. */);
2670 Vx_sent_selection_functions
= Qnil
;
2672 DEFVAR_INT ("x-selection-timeout", x_selection_timeout
,
2673 doc
: /* Number of milliseconds to wait for a selection reply.
2674 If the selection owner doesn't reply in this time, we give up.
2675 A value of 0 means wait as long as necessary. This is initialized from the
2676 \"*selectionTimeout\" resource. */);
2677 x_selection_timeout
= 0;
2679 /* QPRIMARY is defined in keyboard.c. */
2680 QSECONDARY
= intern_c_string ("SECONDARY"); staticpro (&QSECONDARY
);
2681 QSTRING
= intern_c_string ("STRING"); staticpro (&QSTRING
);
2682 QINTEGER
= intern_c_string ("INTEGER"); staticpro (&QINTEGER
);
2683 QCLIPBOARD
= intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD
);
2684 QTIMESTAMP
= intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP
);
2685 QTEXT
= intern_c_string ("TEXT"); staticpro (&QTEXT
);
2686 QCOMPOUND_TEXT
= intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT
);
2687 QUTF8_STRING
= intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING
);
2688 QDELETE
= intern_c_string ("DELETE"); staticpro (&QDELETE
);
2689 QMULTIPLE
= intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE
);
2690 QINCR
= intern_c_string ("INCR"); staticpro (&QINCR
);
2691 QEMACS_TMP
= intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP
);
2692 QTARGETS
= intern_c_string ("TARGETS"); staticpro (&QTARGETS
);
2693 QATOM
= intern_c_string ("ATOM"); staticpro (&QATOM
);
2694 QATOM_PAIR
= intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR
);
2695 QNULL
= intern_c_string ("NULL"); staticpro (&QNULL
);
2696 Qcompound_text_with_extensions
= intern_c_string ("compound-text-with-extensions");
2697 staticpro (&Qcompound_text_with_extensions
);
2699 Qforeign_selection
= intern_c_string ("foreign-selection");
2700 staticpro (&Qforeign_selection
);