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
103 static Lisp_Object QSECONDARY
, QSTRING
, QINTEGER
, QCLIPBOARD
, QTIMESTAMP
,
104 QTEXT
, QDELETE
, QMULTIPLE
, QINCR
, QEMACS_TMP
, QTARGETS
, QATOM
, QNULL
,
107 static Lisp_Object QCOMPOUND_TEXT
; /* This is a type of selection. */
108 static Lisp_Object QUTF8_STRING
; /* This is a type of selection. */
110 static Lisp_Object Qcompound_text_with_extensions
;
112 static Lisp_Object Qforeign_selection
;
114 /* If this is a smaller number than the max-request-size of the display,
115 emacs will use INCR selection transfer when the selection is larger
116 than this. The max-request-size is usually around 64k, so if you want
117 emacs to use incremental selection transfers when the selection is
118 smaller than that, set this. I added this mostly for debugging the
119 incremental transfer stuff, but it might improve server performance. */
120 #define MAX_SELECTION_QUANTUM 0xFFFFFF
122 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
124 /* This is an association list whose elements are of the form
125 ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
126 SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
127 SELECTION-VALUE is the value that emacs owns for that selection.
128 It may be any kind of Lisp object.
129 SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
130 as a cons of two 16-bit numbers (making a 32 bit time.)
131 FRAME is the frame for which we made the selection.
132 If there is an entry in this alist, then it can be assumed that Emacs owns
134 The only (eq) parts of this list that are visible from Lisp are the
136 static Lisp_Object Vselection_alist
;
140 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
143 struct selection_event_queue
145 struct input_event event
;
146 struct selection_event_queue
*next
;
149 static struct selection_event_queue
*selection_queue
;
151 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
153 static int x_queue_selection_requests
;
155 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
158 x_queue_event (struct input_event
*event
)
160 struct selection_event_queue
*queue_tmp
;
162 /* Don't queue repeated requests.
163 This only happens for large requests which uses the incremental protocol. */
164 for (queue_tmp
= selection_queue
; queue_tmp
; queue_tmp
= queue_tmp
->next
)
166 if (!memcmp (&queue_tmp
->event
, event
, sizeof (*event
)))
168 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp
);
169 x_decline_selection_request (event
);
175 = (struct selection_event_queue
*) xmalloc (sizeof (struct selection_event_queue
));
177 if (queue_tmp
!= NULL
)
179 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp
);
180 queue_tmp
->event
= *event
;
181 queue_tmp
->next
= selection_queue
;
182 selection_queue
= queue_tmp
;
186 /* Start queuing SELECTION_REQUEST_EVENT events. */
189 x_start_queuing_selection_requests (void)
191 if (x_queue_selection_requests
)
194 x_queue_selection_requests
++;
195 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests
);
198 /* Stop queuing SELECTION_REQUEST_EVENT events. */
201 x_stop_queuing_selection_requests (void)
203 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests
);
204 --x_queue_selection_requests
;
206 /* Take all the queued events and put them back
207 so that they get processed afresh. */
209 while (selection_queue
!= NULL
)
211 struct selection_event_queue
*queue_tmp
= selection_queue
;
212 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp
);
213 kbd_buffer_unget_event (&queue_tmp
->event
);
214 selection_queue
= queue_tmp
->next
;
215 xfree ((char *)queue_tmp
);
220 /* This converts a Lisp symbol to a server Atom, avoiding a server
221 roundtrip whenever possible. */
224 symbol_to_x_atom (struct x_display_info
*dpyinfo
, Display
*display
, Lisp_Object sym
)
227 if (NILP (sym
)) return 0;
228 if (EQ (sym
, QPRIMARY
)) return XA_PRIMARY
;
229 if (EQ (sym
, QSECONDARY
)) return XA_SECONDARY
;
230 if (EQ (sym
, QSTRING
)) return XA_STRING
;
231 if (EQ (sym
, QINTEGER
)) return XA_INTEGER
;
232 if (EQ (sym
, QATOM
)) return XA_ATOM
;
233 if (EQ (sym
, QCLIPBOARD
)) return dpyinfo
->Xatom_CLIPBOARD
;
234 if (EQ (sym
, QTIMESTAMP
)) return dpyinfo
->Xatom_TIMESTAMP
;
235 if (EQ (sym
, QTEXT
)) return dpyinfo
->Xatom_TEXT
;
236 if (EQ (sym
, QCOMPOUND_TEXT
)) return dpyinfo
->Xatom_COMPOUND_TEXT
;
237 if (EQ (sym
, QUTF8_STRING
)) return dpyinfo
->Xatom_UTF8_STRING
;
238 if (EQ (sym
, QDELETE
)) return dpyinfo
->Xatom_DELETE
;
239 if (EQ (sym
, QMULTIPLE
)) return dpyinfo
->Xatom_MULTIPLE
;
240 if (EQ (sym
, QINCR
)) return dpyinfo
->Xatom_INCR
;
241 if (EQ (sym
, QEMACS_TMP
)) return dpyinfo
->Xatom_EMACS_TMP
;
242 if (EQ (sym
, QTARGETS
)) return dpyinfo
->Xatom_TARGETS
;
243 if (EQ (sym
, QNULL
)) return dpyinfo
->Xatom_NULL
;
244 if (!SYMBOLP (sym
)) abort ();
246 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym
)));
248 val
= XInternAtom (display
, SSDATA (SYMBOL_NAME (sym
)), False
);
254 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
255 and calls to intern whenever possible. */
258 x_atom_to_symbol (Display
*dpy
, Atom atom
)
260 struct x_display_info
*dpyinfo
;
281 dpyinfo
= x_display_info_for_display (dpy
);
282 if (atom
== dpyinfo
->Xatom_CLIPBOARD
)
284 if (atom
== dpyinfo
->Xatom_TIMESTAMP
)
286 if (atom
== dpyinfo
->Xatom_TEXT
)
288 if (atom
== dpyinfo
->Xatom_COMPOUND_TEXT
)
289 return QCOMPOUND_TEXT
;
290 if (atom
== dpyinfo
->Xatom_UTF8_STRING
)
292 if (atom
== dpyinfo
->Xatom_DELETE
)
294 if (atom
== dpyinfo
->Xatom_MULTIPLE
)
296 if (atom
== dpyinfo
->Xatom_INCR
)
298 if (atom
== dpyinfo
->Xatom_EMACS_TMP
)
300 if (atom
== dpyinfo
->Xatom_TARGETS
)
302 if (atom
== dpyinfo
->Xatom_NULL
)
306 str
= XGetAtomName (dpy
, atom
);
308 TRACE1 ("XGetAtomName --> %s", str
);
309 if (! str
) return Qnil
;
312 /* This was allocated by Xlib, so use XFree. */
318 /* Do protocol to assert ourself as a selection owner.
319 Update the Vselection_alist so that we can reply to later requests for
323 x_own_selection (Lisp_Object selection_name
, Lisp_Object selection_value
)
325 struct frame
*sf
= SELECTED_FRAME ();
326 Window selecting_window
;
328 Time timestamp
= last_event_timestamp
;
330 struct x_display_info
*dpyinfo
;
332 if (! FRAME_X_P (sf
))
335 selecting_window
= FRAME_X_WINDOW (sf
);
336 display
= FRAME_X_DISPLAY (sf
);
337 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
339 CHECK_SYMBOL (selection_name
);
340 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection_name
);
343 x_catch_errors (display
);
344 XSetSelectionOwner (display
, selection_atom
, selecting_window
, timestamp
);
345 x_check_errors (display
, "Can't set selection: %s");
349 /* Now update the local cache */
351 Lisp_Object selection_time
;
352 Lisp_Object selection_data
;
353 Lisp_Object prev_value
;
355 selection_time
= long_to_cons (timestamp
);
356 selection_data
= list4 (selection_name
, selection_value
,
357 selection_time
, selected_frame
);
358 prev_value
= assq_no_quit (selection_name
, Vselection_alist
);
360 Vselection_alist
= Fcons (selection_data
, Vselection_alist
);
362 /* If we already owned the selection, remove the old selection data.
363 Perhaps we should destructively modify it instead.
364 Don't use Fdelq as that may QUIT. */
365 if (!NILP (prev_value
))
367 Lisp_Object rest
; /* we know it's not the CAR, so it's easy. */
368 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
369 if (EQ (prev_value
, Fcar (XCDR (rest
))))
371 XSETCDR (rest
, Fcdr (XCDR (rest
)));
378 /* Given a selection-name and desired type, look up our local copy of
379 the selection value and convert it to the type.
380 The value is nil or a string.
381 This function is used both for remote requests (LOCAL_REQUEST is zero)
382 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
384 This calls random Lisp code, and may signal or gc. */
387 x_get_local_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
, int local_request
)
389 Lisp_Object local_value
;
390 Lisp_Object handler_fn
, value
, check
;
393 local_value
= assq_no_quit (selection_symbol
, Vselection_alist
);
395 if (NILP (local_value
)) return Qnil
;
397 /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */
398 if (EQ (target_type
, QTIMESTAMP
))
401 value
= XCAR (XCDR (XCDR (local_value
)));
404 else if (EQ (target_type
, QDELETE
))
407 Fx_disown_selection_internal
409 XCAR (XCDR (XCDR (local_value
))));
414 #if 0 /* #### MULTIPLE doesn't work yet */
415 else if (CONSP (target_type
)
416 && XCAR (target_type
) == QMULTIPLE
)
421 pairs
= XCDR (target_type
);
422 size
= ASIZE (pairs
);
423 /* If the target is MULTIPLE, then target_type looks like
424 (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
425 We modify the second element of each pair in the vector and
426 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
428 for (i
= 0; i
< size
; i
++)
431 pair
= XVECTOR (pairs
)->contents
[i
];
432 XVECTOR (pair
)->contents
[1]
433 = x_get_local_selection (XVECTOR (pair
)->contents
[0],
434 XVECTOR (pair
)->contents
[1],
442 /* Don't allow a quit within the converter.
443 When the user types C-g, he would be surprised
444 if by luck it came during a converter. */
445 count
= SPECPDL_INDEX ();
446 specbind (Qinhibit_quit
, Qt
);
448 CHECK_SYMBOL (target_type
);
449 handler_fn
= Fcdr (Fassq (target_type
, Vselection_converter_alist
));
450 /* gcpro is not needed here since nothing but HANDLER_FN
451 is live, and that ought to be a symbol. */
453 if (!NILP (handler_fn
))
454 value
= call3 (handler_fn
,
455 selection_symbol
, (local_request
? Qnil
: target_type
),
456 XCAR (XCDR (local_value
)));
459 unbind_to (count
, Qnil
);
462 /* Make sure this value is of a type that we could transmit
463 to another X client. */
467 && SYMBOLP (XCAR (value
)))
468 check
= XCDR (value
);
476 /* Check for a value that cons_to_long could handle. */
477 else if (CONSP (check
)
478 && INTEGERP (XCAR (check
))
479 && (INTEGERP (XCDR (check
))
481 (CONSP (XCDR (check
))
482 && INTEGERP (XCAR (XCDR (check
)))
483 && NILP (XCDR (XCDR (check
))))))
486 signal_error ("Invalid data returned by selection-conversion function",
487 list2 (handler_fn
, value
));
490 /* Subroutines of x_reply_selection_request. */
492 /* Send a SelectionNotify event to the requestor with property=None,
493 meaning we were unable to do what they wanted. */
496 x_decline_selection_request (struct input_event
*event
)
499 XSelectionEvent
*reply
= &(reply_base
.xselection
);
501 reply
->type
= SelectionNotify
;
502 reply
->display
= SELECTION_EVENT_DISPLAY (event
);
503 reply
->requestor
= SELECTION_EVENT_REQUESTOR (event
);
504 reply
->selection
= SELECTION_EVENT_SELECTION (event
);
505 reply
->time
= SELECTION_EVENT_TIME (event
);
506 reply
->target
= SELECTION_EVENT_TARGET (event
);
507 reply
->property
= None
;
509 /* The reason for the error may be that the receiver has
510 died in the meantime. Handle that case. */
512 x_catch_errors (reply
->display
);
513 XSendEvent (reply
->display
, reply
->requestor
, False
, 0L, &reply_base
);
514 XFlush (reply
->display
);
519 /* This is the selection request currently being processed.
520 It is set to zero when the request is fully processed. */
521 static struct input_event
*x_selection_current_request
;
523 /* Display info in x_selection_request. */
525 static struct x_display_info
*selection_request_dpyinfo
;
527 /* Used as an unwind-protect clause so that, if a selection-converter signals
528 an error, we tell the requester that we were unable to do what they wanted
529 before we throw to top-level or go into the debugger or whatever. */
532 x_selection_request_lisp_error (Lisp_Object ignore
)
534 if (x_selection_current_request
!= 0
535 && selection_request_dpyinfo
->display
)
536 x_decline_selection_request (x_selection_current_request
);
541 x_catch_errors_unwind (Lisp_Object dummy
)
550 /* This stuff is so that INCR selections are reentrant (that is, so we can
551 be servicing multiple INCR selection requests simultaneously.) I haven't
552 actually tested that yet. */
554 /* Keep a list of the property changes that are awaited. */
564 struct prop_location
*next
;
567 static struct prop_location
*expect_property_change (Display
*display
, Window window
, Atom property
, int state
);
568 static void wait_for_property_change (struct prop_location
*location
);
569 static void unexpect_property_change (struct prop_location
*location
);
570 static int waiting_for_other_props_on_window (Display
*display
, Window window
);
572 static int prop_location_identifier
;
574 static Lisp_Object property_change_reply
;
576 static struct prop_location
*property_change_reply_object
;
578 static struct prop_location
*property_change_wait_list
;
581 queue_selection_requests_unwind (Lisp_Object tem
)
583 x_stop_queuing_selection_requests ();
587 /* Return some frame whose display info is DPYINFO.
588 Return nil if there is none. */
591 some_frame_on_display (struct x_display_info
*dpyinfo
)
593 Lisp_Object list
, frame
;
595 FOR_EACH_FRAME (list
, frame
)
597 if (FRAME_X_P (XFRAME (frame
))
598 && FRAME_X_DISPLAY_INFO (XFRAME (frame
)) == dpyinfo
)
605 /* Send the reply to a selection request event EVENT.
606 TYPE is the type of selection data requested.
607 DATA and SIZE describe the data to send, already converted.
608 FORMAT is the unit-size (in bits) of the data to be transmitted. */
610 #ifdef TRACE_SELECTION
611 static int x_reply_selection_request_cnt
;
612 #endif /* TRACE_SELECTION */
615 x_reply_selection_request (struct input_event
*event
, int format
, unsigned char *data
, int size
, Atom type
)
618 XSelectionEvent
*reply
= &(reply_base
.xselection
);
619 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
620 Window window
= SELECTION_EVENT_REQUESTOR (event
);
622 int format_bytes
= format
/8;
623 int max_bytes
= SELECTION_QUANTUM (display
);
624 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
625 int count
= SPECPDL_INDEX ();
627 if (max_bytes
> MAX_SELECTION_QUANTUM
)
628 max_bytes
= MAX_SELECTION_QUANTUM
;
630 reply
->type
= SelectionNotify
;
631 reply
->display
= display
;
632 reply
->requestor
= window
;
633 reply
->selection
= SELECTION_EVENT_SELECTION (event
);
634 reply
->time
= SELECTION_EVENT_TIME (event
);
635 reply
->target
= SELECTION_EVENT_TARGET (event
);
636 reply
->property
= SELECTION_EVENT_PROPERTY (event
);
637 if (reply
->property
== None
)
638 reply
->property
= reply
->target
;
641 /* The protected block contains wait_for_property_change, which can
642 run random lisp code (process handlers) or signal. Therefore, we
643 put the x_uncatch_errors call in an unwind. */
644 record_unwind_protect (x_catch_errors_unwind
, Qnil
);
645 x_catch_errors (display
);
647 #ifdef TRACE_SELECTION
649 char *sel
= XGetAtomName (display
, reply
->selection
);
650 char *tgt
= XGetAtomName (display
, reply
->target
);
651 TRACE3 ("%s, target %s (%d)", sel
, tgt
, ++x_reply_selection_request_cnt
);
652 if (sel
) XFree (sel
);
653 if (tgt
) XFree (tgt
);
655 #endif /* TRACE_SELECTION */
657 /* Store the data on the requested property.
658 If the selection is large, only store the first N bytes of it.
660 bytes_remaining
= size
* format_bytes
;
661 if (bytes_remaining
<= max_bytes
)
663 /* Send all the data at once, with minimal handshaking. */
664 TRACE1 ("Sending all %d bytes", bytes_remaining
);
665 XChangeProperty (display
, window
, reply
->property
, type
, format
,
666 PropModeReplace
, data
, size
);
667 /* At this point, the selection was successfully stored; ack it. */
668 XSendEvent (display
, window
, False
, 0L, &reply_base
);
672 /* Send an INCR selection. */
673 struct prop_location
*wait_object
;
677 frame
= some_frame_on_display (dpyinfo
);
679 /* If the display no longer has frames, we can't expect
680 to get many more selection requests from it, so don't
681 bother trying to queue them. */
684 x_start_queuing_selection_requests ();
686 record_unwind_protect (queue_selection_requests_unwind
,
690 if (x_window_to_frame (dpyinfo
, window
)) /* #### debug */
691 error ("Attempt to transfer an INCR to ourself!");
693 TRACE2 ("Start sending %d bytes incrementally (%s)",
694 bytes_remaining
, XGetAtomName (display
, reply
->property
));
695 wait_object
= expect_property_change (display
, window
, reply
->property
,
698 TRACE1 ("Set %s to number of bytes to send",
699 XGetAtomName (display
, reply
->property
));
701 /* XChangeProperty expects an array of long even if long is more than
705 value
[0] = bytes_remaining
;
706 XChangeProperty (display
, window
, reply
->property
, dpyinfo
->Xatom_INCR
,
708 (unsigned char *) value
, 1);
711 XSelectInput (display
, window
, PropertyChangeMask
);
713 /* Tell 'em the INCR data is there... */
714 TRACE0 ("Send SelectionNotify event");
715 XSendEvent (display
, window
, False
, 0L, &reply_base
);
718 had_errors
= x_had_errors_p (display
);
721 /* First, wait for the requester to ack by deleting the property.
722 This can run random lisp code (process handlers) or signal. */
725 TRACE1 ("Waiting for ACK (deletion of %s)",
726 XGetAtomName (display
, reply
->property
));
727 wait_for_property_change (wait_object
);
730 unexpect_property_change (wait_object
);
733 while (bytes_remaining
)
735 int i
= ((bytes_remaining
< max_bytes
)
737 : max_bytes
) / format_bytes
;
742 = expect_property_change (display
, window
, reply
->property
,
745 TRACE1 ("Sending increment of %d elements", i
);
746 TRACE1 ("Set %s to increment data",
747 XGetAtomName (display
, reply
->property
));
749 /* Append the next chunk of data to the property. */
750 XChangeProperty (display
, window
, reply
->property
, type
, format
,
751 PropModeAppend
, data
, i
);
752 bytes_remaining
-= i
* format_bytes
;
754 data
+= i
* sizeof (long);
756 data
+= i
* format_bytes
;
758 had_errors
= x_had_errors_p (display
);
764 /* Now wait for the requester to ack this chunk by deleting the
765 property. This can run random lisp code or signal. */
766 TRACE1 ("Waiting for increment ACK (deletion of %s)",
767 XGetAtomName (display
, reply
->property
));
768 wait_for_property_change (wait_object
);
771 /* Now write a zero-length chunk to the property to tell the
772 requester that we're done. */
774 if (! waiting_for_other_props_on_window (display
, window
))
775 XSelectInput (display
, window
, 0L);
777 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
778 XGetAtomName (display
, reply
->property
));
779 XChangeProperty (display
, window
, reply
->property
, type
, format
,
780 PropModeReplace
, data
, 0);
781 TRACE0 ("Done sending incrementally");
784 /* rms, 2003-01-03: I think I have fixed this bug. */
785 /* The window we're communicating with may have been deleted
786 in the meantime (that's a real situation from a bug report).
787 In this case, there may be events in the event queue still
788 refering to the deleted window, and we'll get a BadWindow error
789 in XTread_socket when processing the events. I don't have
790 an idea how to fix that. gerd, 2001-01-98. */
791 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
792 delivered before uncatch errors. */
793 XSync (display
, False
);
796 /* GTK queues events in addition to the queue in Xlib. So we
797 UNBLOCK to enter the event loop and get possible errors delivered,
798 and then BLOCK again because x_uncatch_errors requires it. */
800 /* This calls x_uncatch_errors. */
801 unbind_to (count
, Qnil
);
805 /* Handle a SelectionRequest event EVENT.
806 This is called from keyboard.c when such an event is found in the queue. */
809 x_handle_selection_request (struct input_event
*event
)
811 struct gcpro gcpro1
, gcpro2
, gcpro3
;
812 Lisp_Object local_selection_data
;
813 Lisp_Object selection_symbol
;
814 Lisp_Object target_symbol
;
815 Lisp_Object converted_selection
;
816 Time local_selection_time
;
817 Lisp_Object successful_p
;
819 struct x_display_info
*dpyinfo
820 = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event
));
822 TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
823 (unsigned long) SELECTION_EVENT_REQUESTOR (event
),
824 (unsigned long) SELECTION_EVENT_TIME (event
));
826 local_selection_data
= Qnil
;
827 target_symbol
= Qnil
;
828 converted_selection
= Qnil
;
831 GCPRO3 (local_selection_data
, converted_selection
, target_symbol
);
833 selection_symbol
= x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event
),
834 SELECTION_EVENT_SELECTION (event
));
836 local_selection_data
= assq_no_quit (selection_symbol
, Vselection_alist
);
838 if (NILP (local_selection_data
))
840 /* Someone asked for the selection, but we don't have it any more.
842 x_decline_selection_request (event
);
846 local_selection_time
= (Time
)
847 cons_to_long (XCAR (XCDR (XCDR (local_selection_data
))));
849 if (SELECTION_EVENT_TIME (event
) != CurrentTime
850 && local_selection_time
> SELECTION_EVENT_TIME (event
))
852 /* Someone asked for the selection, and we have one, but not the one
855 x_decline_selection_request (event
);
859 x_selection_current_request
= event
;
860 count
= SPECPDL_INDEX ();
861 selection_request_dpyinfo
= dpyinfo
;
862 record_unwind_protect (x_selection_request_lisp_error
, Qnil
);
864 target_symbol
= x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event
),
865 SELECTION_EVENT_TARGET (event
));
867 #if 0 /* #### MULTIPLE doesn't work yet */
868 if (EQ (target_symbol
, QMULTIPLE
))
869 target_symbol
= fetch_multiple_target (event
);
872 /* Convert lisp objects back into binary data */
875 = x_get_local_selection (selection_symbol
, target_symbol
, 0);
877 if (! NILP (converted_selection
))
885 if (CONSP (converted_selection
) && NILP (XCDR (converted_selection
)))
887 x_decline_selection_request (event
);
891 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event
),
893 &data
, &type
, &size
, &format
, &nofree
);
895 x_reply_selection_request (event
, format
, data
, size
, type
);
898 /* Indicate we have successfully processed this event. */
899 x_selection_current_request
= 0;
901 /* Use xfree, not XFree, because lisp_data_to_selection_data
902 calls xmalloc itself. */
908 unbind_to (count
, Qnil
);
912 /* Let random lisp code notice that the selection has been asked for. */
915 rest
= Vx_sent_selection_functions
;
916 if (!EQ (rest
, Qunbound
))
917 for (; CONSP (rest
); rest
= Fcdr (rest
))
918 call3 (Fcar (rest
), selection_symbol
, target_symbol
, successful_p
);
924 /* Handle a SelectionClear event EVENT, which indicates that some
925 client cleared out our previously asserted selection.
926 This is called from keyboard.c when such an event is found in the queue. */
929 x_handle_selection_clear (struct input_event
*event
)
931 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
932 Atom selection
= SELECTION_EVENT_SELECTION (event
);
933 Time changed_owner_time
= SELECTION_EVENT_TIME (event
);
935 Lisp_Object selection_symbol
, local_selection_data
;
936 Time local_selection_time
;
937 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
938 struct x_display_info
*t_dpyinfo
;
940 TRACE0 ("x_handle_selection_clear");
942 /* If the new selection owner is also Emacs,
943 don't clear the new selection. */
945 /* Check each display on the same terminal,
946 to see if this Emacs job now owns the selection
947 through that display. */
948 for (t_dpyinfo
= x_display_list
; t_dpyinfo
; t_dpyinfo
= t_dpyinfo
->next
)
949 if (t_dpyinfo
->terminal
->kboard
== dpyinfo
->terminal
->kboard
)
952 = XGetSelectionOwner (t_dpyinfo
->display
, selection
);
953 if (x_window_to_frame (t_dpyinfo
, owner_window
) != 0)
961 selection_symbol
= x_atom_to_symbol (display
, selection
);
963 local_selection_data
= assq_no_quit (selection_symbol
, Vselection_alist
);
965 /* Well, we already believe that we don't own it, so that's just fine. */
966 if (NILP (local_selection_data
)) return;
968 local_selection_time
= (Time
)
969 cons_to_long (XCAR (XCDR (XCDR (local_selection_data
))));
971 /* This SelectionClear is for a selection that we no longer own, so we can
972 disregard it. (That is, we have reasserted the selection since this
973 request was generated.) */
975 if (changed_owner_time
!= CurrentTime
976 && local_selection_time
> changed_owner_time
)
979 /* Otherwise, we're really honest and truly being told to drop it.
980 Don't use Fdelq as that may QUIT;. */
982 if (EQ (local_selection_data
, Fcar (Vselection_alist
)))
983 Vselection_alist
= Fcdr (Vselection_alist
);
987 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
988 if (EQ (local_selection_data
, Fcar (XCDR (rest
))))
990 XSETCDR (rest
, Fcdr (XCDR (rest
)));
995 /* Let random lisp code notice that the selection has been stolen. */
999 rest
= Vx_lost_selection_functions
;
1000 if (!EQ (rest
, Qunbound
))
1002 for (; CONSP (rest
); rest
= Fcdr (rest
))
1003 call1 (Fcar (rest
), selection_symbol
);
1004 prepare_menu_bars ();
1005 redisplay_preserve_echo_area (20);
1011 x_handle_selection_event (struct input_event
*event
)
1013 TRACE0 ("x_handle_selection_event");
1015 if (event
->kind
== SELECTION_REQUEST_EVENT
)
1017 if (x_queue_selection_requests
)
1018 x_queue_event (event
);
1020 x_handle_selection_request (event
);
1023 x_handle_selection_clear (event
);
1027 /* Clear all selections that were made from frame F.
1028 We do this when about to delete a frame. */
1031 x_clear_frame_selections (FRAME_PTR f
)
1036 XSETFRAME (frame
, f
);
1038 /* Otherwise, we're really honest and truly being told to drop it.
1039 Don't use Fdelq as that may QUIT;. */
1041 /* Delete elements from the beginning of Vselection_alist. */
1042 while (!NILP (Vselection_alist
)
1043 && EQ (frame
, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist
)))))))
1045 /* Let random Lisp code notice that the selection has been stolen. */
1046 Lisp_Object hooks
, selection_symbol
;
1048 hooks
= Vx_lost_selection_functions
;
1049 selection_symbol
= Fcar (Fcar (Vselection_alist
));
1051 if (!EQ (hooks
, Qunbound
))
1053 for (; CONSP (hooks
); hooks
= Fcdr (hooks
))
1054 call1 (Fcar (hooks
), selection_symbol
);
1055 #if 0 /* This can crash when deleting a frame
1056 from x_connection_closed. Anyway, it seems unnecessary;
1057 something else should cause a redisplay. */
1058 redisplay_preserve_echo_area (21);
1062 Vselection_alist
= Fcdr (Vselection_alist
);
1065 /* Delete elements after the beginning of Vselection_alist. */
1066 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
1067 if (EQ (frame
, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest
))))))))
1069 /* Let random Lisp code notice that the selection has been stolen. */
1070 Lisp_Object hooks
, selection_symbol
;
1072 hooks
= Vx_lost_selection_functions
;
1073 selection_symbol
= Fcar (Fcar (XCDR (rest
)));
1075 if (!EQ (hooks
, Qunbound
))
1077 for (; CONSP (hooks
); hooks
= Fcdr (hooks
))
1078 call1 (Fcar (hooks
), selection_symbol
);
1079 #if 0 /* See above */
1080 redisplay_preserve_echo_area (22);
1083 XSETCDR (rest
, Fcdr (XCDR (rest
)));
1088 /* Nonzero if any properties for DISPLAY and WINDOW
1089 are on the list of what we are waiting for. */
1092 waiting_for_other_props_on_window (Display
*display
, Window window
)
1094 struct prop_location
*rest
= property_change_wait_list
;
1096 if (rest
->display
== display
&& rest
->window
== window
)
1103 /* Add an entry to the list of property changes we are waiting for.
1104 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1105 The return value is a number that uniquely identifies
1106 this awaited property change. */
1108 static struct prop_location
*
1109 expect_property_change (Display
*display
, Window window
, Atom property
, int state
)
1111 struct prop_location
*pl
= (struct prop_location
*) xmalloc (sizeof *pl
);
1112 pl
->identifier
= ++prop_location_identifier
;
1113 pl
->display
= display
;
1114 pl
->window
= window
;
1115 pl
->property
= property
;
1116 pl
->desired_state
= state
;
1117 pl
->next
= property_change_wait_list
;
1119 property_change_wait_list
= pl
;
1123 /* Delete an entry from the list of property changes we are waiting for.
1124 IDENTIFIER is the number that uniquely identifies the entry. */
1127 unexpect_property_change (struct prop_location
*location
)
1129 struct prop_location
*prev
= 0, *rest
= property_change_wait_list
;
1132 if (rest
== location
)
1135 prev
->next
= rest
->next
;
1137 property_change_wait_list
= rest
->next
;
1146 /* Remove the property change expectation element for IDENTIFIER. */
1149 wait_for_property_change_unwind (Lisp_Object loc
)
1151 struct prop_location
*location
= XSAVE_VALUE (loc
)->pointer
;
1153 unexpect_property_change (location
);
1154 if (location
== property_change_reply_object
)
1155 property_change_reply_object
= 0;
1159 /* Actually wait for a property change.
1160 IDENTIFIER should be the value that expect_property_change returned. */
1163 wait_for_property_change (struct prop_location
*location
)
1166 int count
= SPECPDL_INDEX ();
1168 if (property_change_reply_object
)
1171 /* Make sure to do unexpect_property_change if we quit or err. */
1172 record_unwind_protect (wait_for_property_change_unwind
,
1173 make_save_value (location
, 0));
1175 XSETCAR (property_change_reply
, Qnil
);
1176 property_change_reply_object
= location
;
1178 /* If the event we are waiting for arrives beyond here, it will set
1179 property_change_reply, because property_change_reply_object says so. */
1180 if (! location
->arrived
)
1182 secs
= x_selection_timeout
/ 1000;
1183 usecs
= (x_selection_timeout
% 1000) * 1000;
1184 TRACE2 (" Waiting %d secs, %d usecs", secs
, usecs
);
1185 wait_reading_process_output (secs
, usecs
, 0, 0,
1186 property_change_reply
, NULL
, 0);
1188 if (NILP (XCAR (property_change_reply
)))
1190 TRACE0 (" Timed out");
1191 error ("Timed out waiting for property-notify event");
1195 unbind_to (count
, Qnil
);
1198 /* Called from XTread_socket in response to a PropertyNotify event. */
1201 x_handle_property_notify (XPropertyEvent
*event
)
1203 struct prop_location
*rest
;
1205 for (rest
= property_change_wait_list
; rest
; rest
= rest
->next
)
1208 && rest
->property
== event
->atom
1209 && rest
->window
== event
->window
1210 && rest
->display
== event
->display
1211 && rest
->desired_state
== event
->state
)
1213 TRACE2 ("Expected %s of property %s",
1214 (event
->state
== PropertyDelete
? "deletion" : "change"),
1215 XGetAtomName (event
->display
, event
->atom
));
1219 /* If this is the one wait_for_property_change is waiting for,
1220 tell it to wake up. */
1221 if (rest
== property_change_reply_object
)
1222 XSETCAR (property_change_reply
, Qt
);
1231 #if 0 /* #### MULTIPLE doesn't work yet */
1234 fetch_multiple_target (event
)
1235 XSelectionRequestEvent
*event
;
1237 Display
*display
= event
->display
;
1238 Window window
= event
->requestor
;
1239 Atom target
= event
->target
;
1240 Atom selection_atom
= event
->selection
;
1245 x_get_window_property_as_lisp_data (display
, window
, target
,
1246 QMULTIPLE
, selection_atom
));
1250 copy_multiple_data (obj
)
1257 return Fcons (XCAR (obj
), copy_multiple_data (XCDR (obj
)));
1260 vec
= Fmake_vector (size
= ASIZE (obj
), Qnil
);
1261 for (i
= 0; i
< size
; i
++)
1263 Lisp_Object vec2
= XVECTOR (obj
)->contents
[i
];
1264 CHECK_VECTOR (vec2
);
1265 if (ASIZE (vec2
) != 2)
1266 /* ??? Confusing error message */
1267 signal_error ("Vectors must be of length 2", vec2
);
1268 XVECTOR (vec
)->contents
[i
] = Fmake_vector (2, Qnil
);
1269 XVECTOR (XVECTOR (vec
)->contents
[i
])->contents
[0]
1270 = XVECTOR (vec2
)->contents
[0];
1271 XVECTOR (XVECTOR (vec
)->contents
[i
])->contents
[1]
1272 = XVECTOR (vec2
)->contents
[1];
1280 /* Variables for communication with x_handle_selection_notify. */
1281 static Atom reading_which_selection
;
1282 static Lisp_Object reading_selection_reply
;
1283 static Window reading_selection_window
;
1285 /* Do protocol to read selection-data from the server.
1286 Converts this to Lisp data and returns it. */
1289 x_get_foreign_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
, Lisp_Object time_stamp
)
1291 struct frame
*sf
= SELECTED_FRAME ();
1292 Window requestor_window
;
1294 struct x_display_info
*dpyinfo
;
1295 Time requestor_time
= last_event_timestamp
;
1296 Atom target_property
;
1297 Atom selection_atom
;
1300 int count
= SPECPDL_INDEX ();
1303 if (! FRAME_X_P (sf
))
1306 requestor_window
= FRAME_X_WINDOW (sf
);
1307 display
= FRAME_X_DISPLAY (sf
);
1308 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
1309 target_property
= dpyinfo
->Xatom_EMACS_TMP
;
1310 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection_symbol
);
1312 if (CONSP (target_type
))
1313 type_atom
= symbol_to_x_atom (dpyinfo
, display
, XCAR (target_type
));
1315 type_atom
= symbol_to_x_atom (dpyinfo
, display
, target_type
);
1317 if (! NILP (time_stamp
))
1319 if (CONSP (time_stamp
))
1320 requestor_time
= (Time
) cons_to_long (time_stamp
);
1321 else if (INTEGERP (time_stamp
))
1322 requestor_time
= (Time
) XUINT (time_stamp
);
1323 else if (FLOATP (time_stamp
))
1324 requestor_time
= (Time
) XFLOAT_DATA (time_stamp
);
1326 error ("TIME_STAMP must be cons or number");
1331 /* The protected block contains wait_reading_process_output, which
1332 can run random lisp code (process handlers) or signal.
1333 Therefore, we put the x_uncatch_errors call in an unwind. */
1334 record_unwind_protect (x_catch_errors_unwind
, Qnil
);
1335 x_catch_errors (display
);
1337 TRACE2 ("Get selection %s, type %s",
1338 XGetAtomName (display
, type_atom
),
1339 XGetAtomName (display
, target_property
));
1341 XConvertSelection (display
, selection_atom
, type_atom
, target_property
,
1342 requestor_window
, requestor_time
);
1345 /* Prepare to block until the reply has been read. */
1346 reading_selection_window
= requestor_window
;
1347 reading_which_selection
= selection_atom
;
1348 XSETCAR (reading_selection_reply
, Qnil
);
1350 frame
= some_frame_on_display (dpyinfo
);
1352 /* If the display no longer has frames, we can't expect
1353 to get many more selection requests from it, so don't
1354 bother trying to queue them. */
1357 x_start_queuing_selection_requests ();
1359 record_unwind_protect (queue_selection_requests_unwind
,
1364 /* This allows quits. Also, don't wait forever. */
1365 secs
= x_selection_timeout
/ 1000;
1366 usecs
= (x_selection_timeout
% 1000) * 1000;
1367 TRACE1 (" Start waiting %d secs for SelectionNotify", secs
);
1368 wait_reading_process_output (secs
, usecs
, 0, 0,
1369 reading_selection_reply
, NULL
, 0);
1370 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply
)));
1373 if (x_had_errors_p (display
))
1374 error ("Cannot get selection");
1375 /* This calls x_uncatch_errors. */
1376 unbind_to (count
, Qnil
);
1379 if (NILP (XCAR (reading_selection_reply
)))
1380 error ("Timed out waiting for reply from selection owner");
1381 if (EQ (XCAR (reading_selection_reply
), Qlambda
))
1384 /* Otherwise, the selection is waiting for us on the requested property. */
1386 x_get_window_property_as_lisp_data (display
, requestor_window
,
1387 target_property
, target_type
,
1391 /* Subroutines of x_get_window_property_as_lisp_data */
1393 /* Use xfree, not XFree, to free the data obtained with this function. */
1396 x_get_window_property (Display
*display
, Window window
, Atom property
,
1397 unsigned char **data_ret
, int *bytes_ret
,
1398 Atom
*actual_type_ret
, int *actual_format_ret
,
1399 unsigned long *actual_size_ret
, int delete_p
)
1402 unsigned long bytes_remaining
;
1404 unsigned char *tmp_data
= 0;
1406 int buffer_size
= SELECTION_QUANTUM (display
);
1408 if (buffer_size
> MAX_SELECTION_QUANTUM
)
1409 buffer_size
= MAX_SELECTION_QUANTUM
;
1413 /* First probe the thing to find out how big it is. */
1414 result
= XGetWindowProperty (display
, window
, property
,
1415 0L, 0L, False
, AnyPropertyType
,
1416 actual_type_ret
, actual_format_ret
,
1418 &bytes_remaining
, &tmp_data
);
1419 if (result
!= Success
)
1427 /* This was allocated by Xlib, so use XFree. */
1428 XFree ((char *) tmp_data
);
1430 if (*actual_type_ret
== None
|| *actual_format_ret
== 0)
1436 total_size
= bytes_remaining
+ 1;
1437 *data_ret
= (unsigned char *) xmalloc (total_size
);
1439 /* Now read, until we've gotten it all. */
1440 while (bytes_remaining
)
1442 #ifdef TRACE_SELECTION
1443 unsigned long last
= bytes_remaining
;
1446 = XGetWindowProperty (display
, window
, property
,
1447 (long)offset
/4, (long)buffer_size
/4,
1450 actual_type_ret
, actual_format_ret
,
1451 actual_size_ret
, &bytes_remaining
, &tmp_data
);
1453 TRACE2 ("Read %lu bytes from property %s",
1454 last
- bytes_remaining
,
1455 XGetAtomName (display
, property
));
1457 /* If this doesn't return Success at this point, it means that
1458 some clod deleted the selection while we were in the midst of
1459 reading it. Deal with that, I guess.... */
1460 if (result
!= Success
)
1463 /* The man page for XGetWindowProperty says:
1464 "If the returned format is 32, the returned data is represented
1465 as a long array and should be cast to that type to obtain the
1467 This applies even if long is more than 32 bits, the X library
1468 converts from 32 bit elements received from the X server to long
1469 and passes the long array to us. Thus, for that case memcpy can not
1470 be used. We convert to a 32 bit type here, because so much code
1473 The bytes and offsets passed to XGetWindowProperty refers to the
1474 property and those are indeed in 32 bit quantities if format is 32. */
1476 if (32 < BITS_PER_LONG
&& *actual_format_ret
== 32)
1479 int *idata
= (int *) ((*data_ret
) + offset
);
1480 long *ldata
= (long *) tmp_data
;
1482 for (i
= 0; i
< *actual_size_ret
; ++i
)
1484 idata
[i
]= (int) ldata
[i
];
1490 *actual_size_ret
*= *actual_format_ret
/ 8;
1491 memcpy ((*data_ret
) + offset
, tmp_data
, *actual_size_ret
);
1492 offset
+= *actual_size_ret
;
1495 /* This was allocated by Xlib, so use XFree. */
1496 XFree ((char *) tmp_data
);
1501 *bytes_ret
= offset
;
1504 /* Use xfree, not XFree, to free the data obtained with this function. */
1507 receive_incremental_selection (Display
*display
, Window window
, Atom property
,
1508 Lisp_Object target_type
,
1509 unsigned int min_size_bytes
,
1510 unsigned char **data_ret
, int *size_bytes_ret
,
1511 Atom
*type_ret
, int *format_ret
,
1512 unsigned long *size_ret
)
1515 struct prop_location
*wait_object
;
1516 *size_bytes_ret
= min_size_bytes
;
1517 *data_ret
= (unsigned char *) xmalloc (*size_bytes_ret
);
1519 TRACE1 ("Read %d bytes incrementally", min_size_bytes
);
1521 /* At this point, we have read an INCR property.
1522 Delete the property to ack it.
1523 (But first, prepare to receive the next event in this handshake.)
1525 Now, we must loop, waiting for the sending window to put a value on
1526 that property, then reading the property, then deleting it to ack.
1527 We are done when the sender places a property of length 0.
1530 XSelectInput (display
, window
, STANDARD_EVENT_SET
| PropertyChangeMask
);
1531 TRACE1 (" Delete property %s",
1532 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1533 XDeleteProperty (display
, window
, property
);
1534 TRACE1 (" Expect new value of property %s",
1535 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1536 wait_object
= expect_property_change (display
, window
, property
,
1543 unsigned char *tmp_data
;
1546 TRACE0 (" Wait for property change");
1547 wait_for_property_change (wait_object
);
1549 /* expect it again immediately, because x_get_window_property may
1550 .. no it won't, I don't get it.
1551 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1552 TRACE0 (" Get property value");
1553 x_get_window_property (display
, window
, property
,
1554 &tmp_data
, &tmp_size_bytes
,
1555 type_ret
, format_ret
, size_ret
, 1);
1557 TRACE1 (" Read increment of %d bytes", tmp_size_bytes
);
1559 if (tmp_size_bytes
== 0) /* we're done */
1561 TRACE0 ("Done reading incrementally");
1563 if (! waiting_for_other_props_on_window (display
, window
))
1564 XSelectInput (display
, window
, STANDARD_EVENT_SET
);
1565 /* Use xfree, not XFree, because x_get_window_property
1566 calls xmalloc itself. */
1572 TRACE1 (" ACK by deleting property %s",
1573 XGetAtomName (display
, property
));
1574 XDeleteProperty (display
, window
, property
);
1575 wait_object
= expect_property_change (display
, window
, property
,
1580 if (*size_bytes_ret
< offset
+ tmp_size_bytes
)
1582 *size_bytes_ret
= offset
+ tmp_size_bytes
;
1583 *data_ret
= (unsigned char *) xrealloc (*data_ret
, *size_bytes_ret
);
1586 memcpy ((*data_ret
) + offset
, tmp_data
, tmp_size_bytes
);
1587 offset
+= tmp_size_bytes
;
1589 /* Use xfree, not XFree, because x_get_window_property
1590 calls xmalloc itself. */
1596 /* Once a requested selection is "ready" (we got a SelectionNotify event),
1597 fetch value from property PROPERTY of X window WINDOW on display DISPLAY.
1598 TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */
1601 x_get_window_property_as_lisp_data (Display
*display
, Window window
,
1603 Lisp_Object target_type
,
1604 Atom selection_atom
)
1608 unsigned long actual_size
;
1609 unsigned char *data
= 0;
1612 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1614 TRACE0 ("Reading selection data");
1616 x_get_window_property (display
, window
, property
, &data
, &bytes
,
1617 &actual_type
, &actual_format
, &actual_size
, 1);
1620 int there_is_a_selection_owner
;
1622 there_is_a_selection_owner
1623 = XGetSelectionOwner (display
, selection_atom
);
1625 if (there_is_a_selection_owner
)
1626 signal_error ("Selection owner couldn't convert",
1628 ? list2 (target_type
,
1629 x_atom_to_symbol (display
, actual_type
))
1632 signal_error ("No selection",
1633 x_atom_to_symbol (display
, selection_atom
));
1636 if (actual_type
== dpyinfo
->Xatom_INCR
)
1638 /* That wasn't really the data, just the beginning. */
1640 unsigned int min_size_bytes
= * ((unsigned int *) data
);
1642 /* Use xfree, not XFree, because x_get_window_property
1643 calls xmalloc itself. */
1644 xfree ((char *) data
);
1646 receive_incremental_selection (display
, window
, property
, target_type
,
1647 min_size_bytes
, &data
, &bytes
,
1648 &actual_type
, &actual_format
,
1653 TRACE1 (" Delete property %s", XGetAtomName (display
, property
));
1654 XDeleteProperty (display
, window
, property
);
1658 /* It's been read. Now convert it to a lisp object in some semi-rational
1660 val
= selection_data_to_lisp_data (display
, data
, bytes
,
1661 actual_type
, actual_format
);
1663 /* Use xfree, not XFree, because x_get_window_property
1664 calls xmalloc itself. */
1665 xfree ((char *) data
);
1669 /* These functions convert from the selection data read from the server into
1670 something that we can use from Lisp, and vice versa.
1672 Type: Format: Size: Lisp Type:
1673 ----- ------- ----- -----------
1676 ATOM 32 > 1 Vector of Symbols
1678 * 16 > 1 Vector of Integers
1679 * 32 1 if <=16 bits: Integer
1680 if > 16 bits: Cons of top16, bot16
1681 * 32 > 1 Vector of the above
1683 When converting a Lisp number to C, it is assumed to be of format 16 if
1684 it is an integer, and of format 32 if it is a cons of two integers.
1686 When converting a vector of numbers from Lisp to C, it is assumed to be
1687 of format 16 if every element in the vector is an integer, and is assumed
1688 to be of format 32 if any element is a cons of two integers.
1690 When converting an object to C, it may be of the form (SYMBOL . <data>)
1691 where SYMBOL is what we should claim that the type is. Format and
1692 representation are as above.
1694 Important: When format is 32, data should contain an array of int,
1695 not an array of long as the X library returns. This makes a difference
1696 when sizeof(long) != sizeof(int). */
1701 selection_data_to_lisp_data (Display
*display
, const unsigned char *data
,
1702 int size
, Atom type
, int format
)
1704 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1706 if (type
== dpyinfo
->Xatom_NULL
)
1709 /* Convert any 8-bit data to a string, for compactness. */
1710 else if (format
== 8)
1712 Lisp_Object str
, lispy_type
;
1714 str
= make_unibyte_string ((char *) data
, size
);
1715 /* Indicate that this string is from foreign selection by a text
1716 property `foreign-selection' so that the caller of
1717 x-get-selection-internal (usually x-get-selection) can know
1718 that the string must be decode. */
1719 if (type
== dpyinfo
->Xatom_COMPOUND_TEXT
)
1720 lispy_type
= QCOMPOUND_TEXT
;
1721 else if (type
== dpyinfo
->Xatom_UTF8_STRING
)
1722 lispy_type
= QUTF8_STRING
;
1724 lispy_type
= QSTRING
;
1725 Fput_text_property (make_number (0), make_number (size
),
1726 Qforeign_selection
, lispy_type
, str
);
1729 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1730 a vector of symbols.
1732 else if (type
== XA_ATOM
)
1735 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1736 But the callers of these function has made sure the data for
1737 format == 32 is an array of int. Thus, use int instead
1739 int *idata
= (int *) data
;
1741 if (size
== sizeof (int))
1742 return x_atom_to_symbol (display
, (Atom
) idata
[0]);
1745 Lisp_Object v
= Fmake_vector (make_number (size
/ sizeof (int)),
1747 for (i
= 0; i
< size
/ sizeof (int); i
++)
1748 Faset (v
, make_number (i
),
1749 x_atom_to_symbol (display
, (Atom
) idata
[i
]));
1754 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1755 If the number is 32 bits and won't fit in a Lisp_Int,
1756 convert it to a cons of integers, 16 bits in each half.
1758 else if (format
== 32 && size
== sizeof (int))
1759 return long_to_cons (((unsigned int *) data
) [0]);
1760 else if (format
== 16 && size
== sizeof (short))
1761 return make_number ((int) (((unsigned short *) data
) [0]));
1763 /* Convert any other kind of data to a vector of numbers, represented
1764 as above (as an integer, or a cons of two 16 bit integers.)
1766 else if (format
== 16)
1770 v
= Fmake_vector (make_number (size
/ 2), make_number (0));
1771 for (i
= 0; i
< size
/ 2; i
++)
1773 int j
= (int) ((unsigned short *) data
) [i
];
1774 Faset (v
, make_number (i
), make_number (j
));
1781 Lisp_Object v
= Fmake_vector (make_number (size
/ 4), make_number (0));
1782 for (i
= 0; i
< size
/ 4; i
++)
1784 unsigned int j
= ((unsigned int *) data
) [i
];
1785 Faset (v
, make_number (i
), long_to_cons (j
));
1792 /* Use xfree, not XFree, to free the data obtained with this function. */
1795 lisp_data_to_selection_data (Display
*display
, Lisp_Object obj
,
1796 unsigned char **data_ret
, Atom
*type_ret
,
1797 unsigned int *size_ret
,
1798 int *format_ret
, int *nofree_ret
)
1800 Lisp_Object type
= Qnil
;
1801 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1805 if (CONSP (obj
) && SYMBOLP (XCAR (obj
)))
1809 if (CONSP (obj
) && NILP (XCDR (obj
)))
1813 if (EQ (obj
, QNULL
) || (EQ (type
, QNULL
)))
1814 { /* This is not the same as declining */
1820 else if (STRINGP (obj
))
1822 if (SCHARS (obj
) < SBYTES (obj
))
1823 /* OBJ is a multibyte string containing a non-ASCII char. */
1824 signal_error ("Non-ASCII string must be encoded in advance", obj
);
1828 *size_ret
= SBYTES (obj
);
1829 *data_ret
= SDATA (obj
);
1832 else if (SYMBOLP (obj
))
1836 *data_ret
= (unsigned char *) xmalloc (sizeof (Atom
) + 1);
1837 (*data_ret
) [sizeof (Atom
)] = 0;
1838 (*(Atom
**) data_ret
) [0] = symbol_to_x_atom (dpyinfo
, display
, obj
);
1839 if (NILP (type
)) type
= QATOM
;
1841 else if (INTEGERP (obj
)
1842 && XINT (obj
) < 0xFFFF
1843 && XINT (obj
) > -0xFFFF)
1847 *data_ret
= (unsigned char *) xmalloc (sizeof (short) + 1);
1848 (*data_ret
) [sizeof (short)] = 0;
1849 (*(short **) data_ret
) [0] = (short) XINT (obj
);
1850 if (NILP (type
)) type
= QINTEGER
;
1852 else if (INTEGERP (obj
)
1853 || (CONSP (obj
) && INTEGERP (XCAR (obj
))
1854 && (INTEGERP (XCDR (obj
))
1855 || (CONSP (XCDR (obj
))
1856 && INTEGERP (XCAR (XCDR (obj
)))))))
1860 *data_ret
= (unsigned char *) xmalloc (sizeof (long) + 1);
1861 (*data_ret
) [sizeof (long)] = 0;
1862 (*(unsigned long **) data_ret
) [0] = cons_to_long (obj
);
1863 if (NILP (type
)) type
= QINTEGER
;
1865 else if (VECTORP (obj
))
1867 /* Lisp_Vectors may represent a set of ATOMs;
1868 a set of 16 or 32 bit INTEGERs;
1869 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1873 if (SYMBOLP (XVECTOR (obj
)->contents
[0]))
1874 /* This vector is an ATOM set */
1876 if (NILP (type
)) type
= QATOM
;
1877 *size_ret
= ASIZE (obj
);
1879 *data_ret
= (unsigned char *) xmalloc ((*size_ret
) * sizeof (Atom
));
1880 for (i
= 0; i
< *size_ret
; i
++)
1881 if (SYMBOLP (XVECTOR (obj
)->contents
[i
]))
1882 (*(Atom
**) data_ret
) [i
]
1883 = symbol_to_x_atom (dpyinfo
, display
, XVECTOR (obj
)->contents
[i
]);
1885 signal_error ("All elements of selection vector must have same type", obj
);
1887 #if 0 /* #### MULTIPLE doesn't work yet */
1888 else if (VECTORP (XVECTOR (obj
)->contents
[0]))
1889 /* This vector is an ATOM_PAIR set */
1891 if (NILP (type
)) type
= QATOM_PAIR
;
1892 *size_ret
= ASIZE (obj
);
1894 *data_ret
= (unsigned char *)
1895 xmalloc ((*size_ret
) * sizeof (Atom
) * 2);
1896 for (i
= 0; i
< *size_ret
; i
++)
1897 if (VECTORP (XVECTOR (obj
)->contents
[i
]))
1899 Lisp_Object pair
= XVECTOR (obj
)->contents
[i
];
1900 if (ASIZE (pair
) != 2)
1902 "Elements of the vector must be vectors of exactly two elements",
1905 (*(Atom
**) data_ret
) [i
* 2]
1906 = symbol_to_x_atom (dpyinfo
, display
,
1907 XVECTOR (pair
)->contents
[0]);
1908 (*(Atom
**) data_ret
) [(i
* 2) + 1]
1909 = symbol_to_x_atom (dpyinfo
, display
,
1910 XVECTOR (pair
)->contents
[1]);
1913 signal_error ("All elements of the vector must be of the same type",
1919 /* This vector is an INTEGER set, or something like it */
1922 *size_ret
= ASIZE (obj
);
1923 if (NILP (type
)) type
= QINTEGER
;
1925 for (i
= 0; i
< *size_ret
; i
++)
1926 if (CONSP (XVECTOR (obj
)->contents
[i
]))
1928 else if (!INTEGERP (XVECTOR (obj
)->contents
[i
]))
1929 signal_error (/* Qselection_error */
1930 "Elements of selection vector must be integers or conses of integers",
1933 /* Use sizeof(long) even if it is more than 32 bits. See comment
1934 in x_get_window_property and x_fill_property_data. */
1936 if (*format_ret
== 32) data_size
= sizeof(long);
1937 *data_ret
= (unsigned char *) xmalloc (*size_ret
* data_size
);
1938 for (i
= 0; i
< *size_ret
; i
++)
1939 if (*format_ret
== 32)
1940 (*((unsigned long **) data_ret
)) [i
]
1941 = cons_to_long (XVECTOR (obj
)->contents
[i
]);
1943 (*((unsigned short **) data_ret
)) [i
]
1944 = (unsigned short) cons_to_long (XVECTOR (obj
)->contents
[i
]);
1948 signal_error (/* Qselection_error */ "Unrecognized selection data", obj
);
1950 *type_ret
= symbol_to_x_atom (dpyinfo
, display
, type
);
1954 clean_local_selection_data (Lisp_Object obj
)
1957 && INTEGERP (XCAR (obj
))
1958 && CONSP (XCDR (obj
))
1959 && INTEGERP (XCAR (XCDR (obj
)))
1960 && NILP (XCDR (XCDR (obj
))))
1961 obj
= Fcons (XCAR (obj
), XCDR (obj
));
1964 && INTEGERP (XCAR (obj
))
1965 && INTEGERP (XCDR (obj
)))
1967 if (XINT (XCAR (obj
)) == 0)
1969 if (XINT (XCAR (obj
)) == -1)
1970 return make_number (- XINT (XCDR (obj
)));
1975 int size
= ASIZE (obj
);
1978 return clean_local_selection_data (XVECTOR (obj
)->contents
[0]);
1979 copy
= Fmake_vector (make_number (size
), Qnil
);
1980 for (i
= 0; i
< size
; i
++)
1981 XVECTOR (copy
)->contents
[i
]
1982 = clean_local_selection_data (XVECTOR (obj
)->contents
[i
]);
1988 /* Called from XTread_socket to handle SelectionNotify events.
1989 If it's the selection we are waiting for, stop waiting
1990 by setting the car of reading_selection_reply to non-nil.
1991 We store t there if the reply is successful, lambda if not. */
1994 x_handle_selection_notify (XSelectionEvent
*event
)
1996 if (event
->requestor
!= reading_selection_window
)
1998 if (event
->selection
!= reading_which_selection
)
2001 TRACE0 ("Received SelectionNotify");
2002 XSETCAR (reading_selection_reply
,
2003 (event
->property
!= 0 ? Qt
: Qlambda
));
2007 DEFUN ("x-own-selection-internal", Fx_own_selection_internal
,
2008 Sx_own_selection_internal
, 2, 2, 0,
2009 doc
: /* Assert an X selection of the given TYPE with the given VALUE.
2010 TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2011 \(Those are literal upper-case symbol names, since that's what X expects.)
2012 VALUE is typically a string, or a cons of two markers, but may be
2013 anything that the functions on `selection-converter-alist' know about. */)
2014 (Lisp_Object selection_name
, Lisp_Object selection_value
)
2017 CHECK_SYMBOL (selection_name
);
2018 if (NILP (selection_value
)) error ("SELECTION-VALUE may not be nil");
2019 x_own_selection (selection_name
, selection_value
);
2020 return selection_value
;
2024 /* Request the selection value from the owner. If we are the owner,
2025 simply return our selection value. If we are not the owner, this
2026 will block until all of the data has arrived. */
2028 DEFUN ("x-get-selection-internal", Fx_get_selection_internal
,
2029 Sx_get_selection_internal
, 2, 3, 0,
2030 doc
: /* Return text selected from some X window.
2031 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2032 \(Those are literal upper-case symbol names, since that's what X expects.)
2033 TYPE is the type of data desired, typically `STRING'.
2034 TIME_STAMP is the time to use in the XConvertSelection call for foreign
2035 selections. If omitted, defaults to the time for the last event. */)
2036 (Lisp_Object selection_symbol
, Lisp_Object target_type
, Lisp_Object time_stamp
)
2038 Lisp_Object val
= Qnil
;
2039 struct gcpro gcpro1
, gcpro2
;
2040 GCPRO2 (target_type
, val
); /* we store newly consed data into these */
2042 CHECK_SYMBOL (selection_symbol
);
2044 #if 0 /* #### MULTIPLE doesn't work yet */
2045 if (CONSP (target_type
)
2046 && XCAR (target_type
) == QMULTIPLE
)
2048 CHECK_VECTOR (XCDR (target_type
));
2049 /* So we don't destructively modify this... */
2050 target_type
= copy_multiple_data (target_type
);
2054 CHECK_SYMBOL (target_type
);
2056 val
= x_get_local_selection (selection_symbol
, target_type
, 1);
2060 val
= x_get_foreign_selection (selection_symbol
, target_type
, time_stamp
);
2065 && SYMBOLP (XCAR (val
)))
2068 if (CONSP (val
) && NILP (XCDR (val
)))
2071 val
= clean_local_selection_data (val
);
2077 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal
,
2078 Sx_disown_selection_internal
, 1, 2, 0,
2079 doc
: /* If we own the selection SELECTION, disown it.
2080 Disowning it means there is no such selection. */)
2081 (Lisp_Object selection
, Lisp_Object time_object
)
2084 Atom selection_atom
;
2086 struct selection_input_event sie
;
2087 struct input_event ie
;
2090 struct x_display_info
*dpyinfo
;
2091 struct frame
*sf
= SELECTED_FRAME ();
2094 if (! FRAME_X_P (sf
))
2097 display
= FRAME_X_DISPLAY (sf
);
2098 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
2099 CHECK_SYMBOL (selection
);
2100 if (NILP (time_object
))
2101 timestamp
= last_event_timestamp
;
2103 timestamp
= cons_to_long (time_object
);
2105 if (NILP (assq_no_quit (selection
, Vselection_alist
)))
2106 return Qnil
; /* Don't disown the selection when we're not the owner. */
2108 selection_atom
= symbol_to_x_atom (dpyinfo
, display
, selection
);
2111 XSetSelectionOwner (display
, selection_atom
, None
, timestamp
);
2114 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2115 generated for a window which owns the selection when that window sets
2116 the selection owner to None. The NCD server does, the MIT Sun4 server
2117 doesn't. So we synthesize one; this means we might get two, but
2118 that's ok, because the second one won't have any effect. */
2119 SELECTION_EVENT_DISPLAY (&event
.sie
) = display
;
2120 SELECTION_EVENT_SELECTION (&event
.sie
) = selection_atom
;
2121 SELECTION_EVENT_TIME (&event
.sie
) = timestamp
;
2122 x_handle_selection_clear (&event
.ie
);
2127 DEFUN ("x-selection-owner-p", Fx_selection_owner_p
, Sx_selection_owner_p
,
2129 doc
: /* Whether the current Emacs process owns the given X Selection.
2130 The arg should be the name of the selection in question, typically one of
2131 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2132 \(Those are literal upper-case symbol names, since that's what X expects.)
2133 For convenience, the symbol nil is the same as `PRIMARY',
2134 and t is the same as `SECONDARY'. */)
2135 (Lisp_Object selection
)
2138 CHECK_SYMBOL (selection
);
2139 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2140 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2142 if (NILP (Fassq (selection
, Vselection_alist
)))
2147 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
2149 doc
: /* Whether there is an owner for the given X Selection.
2150 The arg should be the name of the selection in question, typically one of
2151 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2152 \(Those are literal upper-case symbol names, since that's what X expects.)
2153 For convenience, the symbol nil is the same as `PRIMARY',
2154 and t is the same as `SECONDARY'. */)
2155 (Lisp_Object selection
)
2160 struct frame
*sf
= SELECTED_FRAME ();
2162 /* It should be safe to call this before we have an X frame. */
2163 if (! FRAME_X_P (sf
))
2166 dpy
= FRAME_X_DISPLAY (sf
);
2167 CHECK_SYMBOL (selection
);
2168 if (!NILP (Fx_selection_owner_p (selection
)))
2170 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2171 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2172 atom
= symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf
), dpy
, selection
);
2176 owner
= XGetSelectionOwner (dpy
, atom
);
2178 return (owner
? Qt
: Qnil
);
2182 /***********************************************************************
2183 Drag and drop support
2184 ***********************************************************************/
2185 /* Check that lisp values are of correct type for x_fill_property_data.
2186 That is, number, string or a cons with two numbers (low and high 16
2187 bit parts of a 32 bit number). Return the number of items in DATA,
2188 or -1 if there is an error. */
2191 x_check_property_data (Lisp_Object data
)
2196 for (iter
= data
; CONSP (iter
); iter
= XCDR (iter
))
2198 Lisp_Object o
= XCAR (iter
);
2200 if (! NUMBERP (o
) && ! STRINGP (o
) && ! CONSP (o
))
2202 else if (CONSP (o
) &&
2203 (! NUMBERP (XCAR (o
)) || ! NUMBERP (XCDR (o
))))
2211 /* Convert lisp values to a C array. Values may be a number, a string
2212 which is taken as an X atom name and converted to the atom value, or
2213 a cons containing the two 16 bit parts of a 32 bit number.
2215 DPY is the display use to look up X atoms.
2216 DATA is a Lisp list of values to be converted.
2217 RET is the C array that contains the converted values. It is assumed
2218 it is big enough to hold all values.
2219 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2220 be stored in RET. Note that long is used for 32 even if long is more
2221 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2222 XClientMessageEvent). */
2225 x_fill_property_data (Display
*dpy
, Lisp_Object data
, void *ret
, int format
)
2228 long *d32
= (long *) ret
;
2229 short *d16
= (short *) ret
;
2230 char *d08
= (char *) ret
;
2233 for (iter
= data
; CONSP (iter
); iter
= XCDR (iter
))
2235 Lisp_Object o
= XCAR (iter
);
2238 val
= (long) XFASTINT (o
);
2239 else if (FLOATP (o
))
2240 val
= (long) XFLOAT_DATA (o
);
2242 val
= (long) cons_to_long (o
);
2243 else if (STRINGP (o
))
2246 val
= (long) XInternAtom (dpy
, SSDATA (o
), False
);
2250 error ("Wrong type, must be string, number or cons");
2253 *d08
++ = (char) val
;
2254 else if (format
== 16)
2255 *d16
++ = (short) val
;
2261 /* Convert an array of C values to a Lisp list.
2262 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2263 DATA is a C array of values to be converted.
2264 TYPE is the type of the data. Only XA_ATOM is special, it converts
2265 each number in DATA to its corresponfing X atom as a symbol.
2266 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2268 SIZE is the number of elements in DATA.
2270 Important: When format is 32, data should contain an array of int,
2271 not an array of long as the X library returns. This makes a difference
2272 when sizeof(long) != sizeof(int).
2274 Also see comment for selection_data_to_lisp_data above. */
2277 x_property_data_to_lisp (struct frame
*f
, const unsigned char *data
,
2278 Atom type
, int format
, long unsigned int size
)
2280 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f
),
2281 data
, size
*format
/8, type
, format
);
2284 /* Get the mouse position in frame relative coordinates. */
2287 mouse_position_for_drop (FRAME_PTR f
, int *x
, int *y
)
2289 Window root
, dummy_window
;
2294 XQueryPointer (FRAME_X_DISPLAY (f
),
2295 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
2297 /* The root window which contains the pointer. */
2300 /* Window pointer is on, not used */
2303 /* The position on that root window. */
2306 /* x/y in dummy_window coordinates, not used. */
2309 /* Modifier keys and pointer buttons, about which
2311 (unsigned int *) &dummy
);
2314 /* Absolute to relative. */
2315 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2316 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2321 DEFUN ("x-get-atom-name", Fx_get_atom_name
,
2322 Sx_get_atom_name
, 1, 2, 0,
2323 doc
: /* Return the X atom name for VALUE as a string.
2324 VALUE may be a number or a cons where the car is the upper 16 bits and
2325 the cdr is the lower 16 bits of a 32 bit value.
2326 Use the display for FRAME or the current frame if FRAME is not given or nil.
2328 If the value is 0 or the atom is not known, return the empty string. */)
2329 (Lisp_Object value
, Lisp_Object frame
)
2331 struct frame
*f
= check_x_frame (frame
);
2334 Lisp_Object ret
= Qnil
;
2335 Display
*dpy
= FRAME_X_DISPLAY (f
);
2339 if (INTEGERP (value
))
2340 atom
= (Atom
) XUINT (value
);
2341 else if (FLOATP (value
))
2342 atom
= (Atom
) XFLOAT_DATA (value
);
2343 else if (CONSP (value
))
2344 atom
= (Atom
) cons_to_long (value
);
2346 error ("Wrong type, value must be number or cons");
2349 x_catch_errors (dpy
);
2350 name
= atom
? XGetAtomName (dpy
, atom
) : empty
;
2351 had_errors
= x_had_errors_p (dpy
);
2352 x_uncatch_errors ();
2355 ret
= make_string (name
, strlen (name
));
2357 if (atom
&& name
) XFree (name
);
2358 if (NILP (ret
)) ret
= empty_unibyte_string
;
2365 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom
,
2366 Sx_register_dnd_atom
, 1, 2, 0,
2367 doc
: /* Request that dnd events are made for ClientMessages with ATOM.
2368 ATOM can be a symbol or a string. The ATOM is interned on the display that
2369 FRAME is on. If FRAME is nil, the selected frame is used. */)
2370 (Lisp_Object atom
, Lisp_Object frame
)
2373 struct frame
*f
= check_x_frame (frame
);
2375 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2379 x_atom
= symbol_to_x_atom (dpyinfo
, FRAME_X_DISPLAY (f
), atom
);
2380 else if (STRINGP (atom
))
2383 x_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SSDATA (atom
), False
);
2387 error ("ATOM must be a symbol or a string");
2389 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2390 if (dpyinfo
->x_dnd_atoms
[i
] == x_atom
)
2393 if (dpyinfo
->x_dnd_atoms_length
== dpyinfo
->x_dnd_atoms_size
)
2395 dpyinfo
->x_dnd_atoms_size
*= 2;
2396 dpyinfo
->x_dnd_atoms
= xrealloc (dpyinfo
->x_dnd_atoms
,
2397 sizeof (*dpyinfo
->x_dnd_atoms
)
2398 * dpyinfo
->x_dnd_atoms_size
);
2401 dpyinfo
->x_dnd_atoms
[dpyinfo
->x_dnd_atoms_length
++] = x_atom
;
2405 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2408 x_handle_dnd_message (struct frame
*f
, XClientMessageEvent
*event
, struct x_display_info
*dpyinfo
, struct input_event
*bufp
)
2412 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2413 unsigned long size
= 160/event
->format
;
2415 unsigned char *data
= (unsigned char *) event
->data
.b
;
2419 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2420 if (dpyinfo
->x_dnd_atoms
[i
] == event
->message_type
) break;
2422 if (i
== dpyinfo
->x_dnd_atoms_length
) return 0;
2424 XSETFRAME (frame
, f
);
2426 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2427 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2428 function expects them to be of size int (i.e. 32). So to be able to
2429 use that function, put the data in the form it expects if format is 32. */
2431 if (32 < BITS_PER_LONG
&& event
->format
== 32)
2433 for (i
= 0; i
< 5; ++i
) /* There are only 5 longs in a ClientMessage. */
2434 idata
[i
] = (int) event
->data
.l
[i
];
2435 data
= (unsigned char *) idata
;
2438 vec
= Fmake_vector (make_number (4), Qnil
);
2439 ASET (vec
, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f
),
2440 event
->message_type
)));
2441 ASET (vec
, 1, frame
);
2442 ASET (vec
, 2, make_number (event
->format
));
2443 ASET (vec
, 3, x_property_data_to_lisp (f
,
2445 event
->message_type
,
2449 mouse_position_for_drop (f
, &x
, &y
);
2450 bufp
->kind
= DRAG_N_DROP_EVENT
;
2451 bufp
->frame_or_window
= frame
;
2452 bufp
->timestamp
= CurrentTime
;
2453 bufp
->x
= make_number (x
);
2454 bufp
->y
= make_number (y
);
2456 bufp
->modifiers
= 0;
2461 DEFUN ("x-send-client-message", Fx_send_client_event
,
2462 Sx_send_client_message
, 6, 6, 0,
2463 doc
: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2465 For DISPLAY, specify either a frame or a display name (a string).
2466 If DISPLAY is nil, that stands for the selected frame's display.
2467 DEST may be a number, in which case it is a Window id. The value 0 may
2468 be used to send to the root window of the DISPLAY.
2469 If DEST is a cons, it is converted to a 32 bit number
2470 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2471 number is then used as a window id.
2472 If DEST is a frame the event is sent to the outer window of that frame.
2473 A value of nil means the currently selected frame.
2474 If DEST is the string "PointerWindow" the event is sent to the window that
2475 contains the pointer. If DEST is the string "InputFocus" the event is
2476 sent to the window that has the input focus.
2477 FROM is the frame sending the event. Use nil for currently selected frame.
2478 MESSAGE-TYPE is the name of an Atom as a string.
2479 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2480 bits. VALUES is a list of numbers, cons and/or strings containing the values
2481 to send. If a value is a string, it is converted to an Atom and the value of
2482 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2483 with the high 16 bits from the car and the lower 16 bit from the cdr.
2484 If more values than fits into the event is given, the excessive values
2486 (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
, Lisp_Object message_type
, Lisp_Object format
, Lisp_Object values
)
2488 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2490 CHECK_STRING (message_type
);
2491 x_send_client_event(display
, dest
, from
,
2492 XInternAtom (dpyinfo
->display
,
2493 SSDATA (message_type
),
2501 x_send_client_event (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
, Atom message_type
, Lisp_Object format
, Lisp_Object values
)
2503 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2506 struct frame
*f
= check_x_frame (from
);
2509 CHECK_NUMBER (format
);
2510 CHECK_CONS (values
);
2512 if (x_check_property_data (values
) == -1)
2513 error ("Bad data in VALUES, must be number, cons or string");
2515 event
.xclient
.type
= ClientMessage
;
2516 event
.xclient
.format
= XFASTINT (format
);
2518 if (event
.xclient
.format
!= 8 && event
.xclient
.format
!= 16
2519 && event
.xclient
.format
!= 32)
2520 error ("FORMAT must be one of 8, 16 or 32");
2522 if (FRAMEP (dest
) || NILP (dest
))
2524 struct frame
*fdest
= check_x_frame (dest
);
2525 wdest
= FRAME_OUTER_WINDOW (fdest
);
2527 else if (STRINGP (dest
))
2529 if (strcmp (SSDATA (dest
), "PointerWindow") == 0)
2530 wdest
= PointerWindow
;
2531 else if (strcmp (SSDATA (dest
), "InputFocus") == 0)
2534 error ("DEST as a string must be one of PointerWindow or InputFocus");
2536 else if (INTEGERP (dest
))
2537 wdest
= (Window
) XFASTINT (dest
);
2538 else if (FLOATP (dest
))
2539 wdest
= (Window
) XFLOAT_DATA (dest
);
2540 else if (CONSP (dest
))
2542 if (! NUMBERP (XCAR (dest
)) || ! NUMBERP (XCDR (dest
)))
2543 error ("Both car and cdr for DEST must be numbers");
2545 wdest
= (Window
) cons_to_long (dest
);
2548 error ("DEST must be a frame, nil, string, number or cons");
2550 if (wdest
== 0) wdest
= dpyinfo
->root_window
;
2551 to_root
= wdest
== dpyinfo
->root_window
;
2555 event
.xclient
.message_type
= message_type
;
2556 event
.xclient
.display
= dpyinfo
->display
;
2558 /* Some clients (metacity for example) expects sending window to be here
2559 when sending to the root window. */
2560 event
.xclient
.window
= to_root
? FRAME_OUTER_WINDOW (f
) : wdest
;
2563 memset (event
.xclient
.data
.b
, 0, sizeof (event
.xclient
.data
.b
));
2564 x_fill_property_data (dpyinfo
->display
, values
, event
.xclient
.data
.b
,
2565 event
.xclient
.format
);
2567 /* If event mask is 0 the event is sent to the client that created
2568 the destination window. But if we are sending to the root window,
2569 there is no such client. Then we set the event mask to 0xffff. The
2570 event then goes to clients selecting for events on the root window. */
2571 x_catch_errors (dpyinfo
->display
);
2573 int propagate
= to_root
? False
: True
;
2574 unsigned mask
= to_root
? 0xffff : 0;
2575 XSendEvent (dpyinfo
->display
, wdest
, propagate
, mask
, &event
);
2576 XFlush (dpyinfo
->display
);
2578 x_uncatch_errors ();
2584 syms_of_xselect (void)
2586 defsubr (&Sx_get_selection_internal
);
2587 defsubr (&Sx_own_selection_internal
);
2588 defsubr (&Sx_disown_selection_internal
);
2589 defsubr (&Sx_selection_owner_p
);
2590 defsubr (&Sx_selection_exists_p
);
2592 defsubr (&Sx_get_atom_name
);
2593 defsubr (&Sx_send_client_message
);
2594 defsubr (&Sx_register_dnd_atom
);
2596 reading_selection_reply
= Fcons (Qnil
, Qnil
);
2597 staticpro (&reading_selection_reply
);
2598 reading_selection_window
= 0;
2599 reading_which_selection
= 0;
2601 property_change_wait_list
= 0;
2602 prop_location_identifier
= 0;
2603 property_change_reply
= Fcons (Qnil
, Qnil
);
2604 staticpro (&property_change_reply
);
2606 Vselection_alist
= Qnil
;
2607 staticpro (&Vselection_alist
);
2609 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist
,
2610 doc
: /* An alist associating X Windows selection-types with functions.
2611 These functions are called to convert the selection, with three args:
2612 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2613 a desired type to which the selection should be converted;
2614 and the local selection value (whatever was given to `x-own-selection').
2616 The function should return the value to send to the X server
2617 \(typically a string). A return value of nil
2618 means that the conversion could not be done.
2619 A return value which is the symbol `NULL'
2620 means that a side-effect was executed,
2621 and there is no meaningful selection value. */);
2622 Vselection_converter_alist
= Qnil
;
2624 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions
,
2625 doc
: /* A list of functions to be called when Emacs loses an X selection.
2626 \(This happens when some other X client makes its own selection
2627 or when a Lisp program explicitly clears the selection.)
2628 The functions are called with one argument, the selection type
2629 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2630 Vx_lost_selection_functions
= Qnil
;
2632 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions
,
2633 doc
: /* A list of functions to be called when Emacs answers a selection request.
2634 The functions are called with four arguments:
2635 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2636 - the selection-type which Emacs was asked to convert the
2637 selection into before sending (for example, `STRING' or `LENGTH');
2638 - a flag indicating success or failure for responding to the request.
2639 We might have failed (and declined the request) for any number of reasons,
2640 including being asked for a selection that we no longer own, or being asked
2641 to convert into a type that we don't know about or that is inappropriate.
2642 This hook doesn't let you change the behavior of Emacs's selection replies,
2643 it merely informs you that they have happened. */);
2644 Vx_sent_selection_functions
= Qnil
;
2646 DEFVAR_INT ("x-selection-timeout", x_selection_timeout
,
2647 doc
: /* Number of milliseconds to wait for a selection reply.
2648 If the selection owner doesn't reply in this time, we give up.
2649 A value of 0 means wait as long as necessary. This is initialized from the
2650 \"*selectionTimeout\" resource. */);
2651 x_selection_timeout
= 0;
2653 /* QPRIMARY is defined in keyboard.c. */
2654 QSECONDARY
= intern_c_string ("SECONDARY"); staticpro (&QSECONDARY
);
2655 QSTRING
= intern_c_string ("STRING"); staticpro (&QSTRING
);
2656 QINTEGER
= intern_c_string ("INTEGER"); staticpro (&QINTEGER
);
2657 QCLIPBOARD
= intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD
);
2658 QTIMESTAMP
= intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP
);
2659 QTEXT
= intern_c_string ("TEXT"); staticpro (&QTEXT
);
2660 QCOMPOUND_TEXT
= intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT
);
2661 QUTF8_STRING
= intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING
);
2662 QDELETE
= intern_c_string ("DELETE"); staticpro (&QDELETE
);
2663 QMULTIPLE
= intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE
);
2664 QINCR
= intern_c_string ("INCR"); staticpro (&QINCR
);
2665 QEMACS_TMP
= intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP
);
2666 QTARGETS
= intern_c_string ("TARGETS"); staticpro (&QTARGETS
);
2667 QATOM
= intern_c_string ("ATOM"); staticpro (&QATOM
);
2668 QATOM_PAIR
= intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR
);
2669 QNULL
= intern_c_string ("NULL"); staticpro (&QNULL
);
2670 Qcompound_text_with_extensions
= intern_c_string ("compound-text-with-extensions");
2671 staticpro (&Qcompound_text_with_extensions
);
2673 Qforeign_selection
= intern_c_string ("foreign-selection");
2674 staticpro (&Qforeign_selection
);