1 /* X Selection processing for Emacs.
2 Copyright (C) 1993-1997, 2000-2013 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 */
24 #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"
37 #include "character.h"
40 #include "termhooks.h"
43 #include <X11/Xproto.h>
46 struct selection_data
;
48 static Lisp_Object
x_atom_to_symbol (Display
*dpy
, Atom atom
);
49 static Atom
symbol_to_x_atom (struct x_display_info
*, Lisp_Object
);
50 static void x_own_selection (Lisp_Object
, Lisp_Object
, Lisp_Object
);
51 static Lisp_Object
x_get_local_selection (Lisp_Object
, Lisp_Object
, int,
52 struct x_display_info
*);
53 static void x_decline_selection_request (struct input_event
*);
54 static Lisp_Object
x_selection_request_lisp_error (Lisp_Object
);
55 static Lisp_Object
queue_selection_requests_unwind (Lisp_Object
);
56 static Lisp_Object
x_catch_errors_unwind (Lisp_Object
);
57 static void x_reply_selection_request (struct input_event
*, struct x_display_info
*);
58 static int x_convert_selection (struct input_event
*, Lisp_Object
, Lisp_Object
,
59 Atom
, int, struct x_display_info
*);
60 static int waiting_for_other_props_on_window (Display
*, Window
);
61 static struct prop_location
*expect_property_change (Display
*, Window
,
63 static void unexpect_property_change (struct prop_location
*);
64 static Lisp_Object
wait_for_property_change_unwind (Lisp_Object
);
65 static void wait_for_property_change (struct prop_location
*);
66 static Lisp_Object
x_get_foreign_selection (Lisp_Object
, Lisp_Object
,
67 Lisp_Object
, Lisp_Object
);
68 static Lisp_Object
x_get_window_property_as_lisp_data (Display
*,
71 static Lisp_Object
selection_data_to_lisp_data (Display
*,
72 const unsigned char *,
73 ptrdiff_t, Atom
, int);
74 static void lisp_data_to_selection_data (Display
*, Lisp_Object
,
75 unsigned char **, Atom
*,
76 ptrdiff_t *, int *, int *);
77 static Lisp_Object
clean_local_selection_data (Lisp_Object
);
79 /* Printing traces to stderr. */
81 #ifdef TRACE_SELECTION
83 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid ())
84 #define TRACE1(fmt, a0) \
85 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0)
86 #define TRACE2(fmt, a0, a1) \
87 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1)
88 #define TRACE3(fmt, a0, a1, a2) \
89 fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1, a2)
91 #define TRACE0(fmt) (void) 0
92 #define TRACE1(fmt, a0) (void) 0
93 #define TRACE2(fmt, a0, a1) (void) 0
97 static Lisp_Object QSECONDARY
, QSTRING
, QINTEGER
, QCLIPBOARD
, QTIMESTAMP
,
98 QTEXT
, QDELETE
, QMULTIPLE
, QINCR
, QEMACS_TMP
, QTARGETS
, QATOM
, QNULL
,
99 QATOM_PAIR
, QCLIPBOARD_MANAGER
, QSAVE_TARGETS
;
101 static Lisp_Object QCOMPOUND_TEXT
; /* This is a type of selection. */
102 static Lisp_Object QUTF8_STRING
; /* This is a type of selection. */
104 static Lisp_Object Qcompound_text_with_extensions
;
106 static Lisp_Object Qforeign_selection
;
107 static Lisp_Object Qx_lost_selection_functions
, Qx_sent_selection_functions
;
109 /* Bytes needed to represent 'long' data. This is as per libX11; it
110 is not necessarily sizeof (long). */
111 #define X_LONG_SIZE 4
113 /* Extreme 'short' and 'long' values suitable for libX11. */
114 #define X_SHRT_MAX 0x7fff
115 #define X_SHRT_MIN (-1 - X_SHRT_MAX)
116 #define X_LONG_MAX 0x7fffffff
117 #define X_LONG_MIN (-1 - X_LONG_MAX)
118 #define X_ULONG_MAX 0xffffffffUL
120 /* If this is a smaller number than the max-request-size of the display,
121 emacs will use INCR selection transfer when the selection is larger
122 than this. The max-request-size is usually around 64k, so if you want
123 emacs to use incremental selection transfers when the selection is
124 smaller than that, set this. I added this mostly for debugging the
125 incremental transfer stuff, but it might improve server performance.
127 This value cannot exceed INT_MAX / max (X_LONG_SIZE, sizeof (long))
128 because it is multiplied by X_LONG_SIZE and by sizeof (long) in
129 subscript calculations. Similarly for PTRDIFF_MAX - 1 or SIZE_MAX
130 - 1 in place of INT_MAX. */
131 #define MAX_SELECTION_QUANTUM \
132 ((int) min (0xFFFFFF, (min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) - 1) \
133 / max (X_LONG_SIZE, sizeof (long)))))
136 selection_quantum (Display
*display
)
138 long mrs
= XMaxRequestSize (display
);
139 return (mrs
< MAX_SELECTION_QUANTUM
/ X_LONG_SIZE
+ 25
140 ? (mrs
- 25) * X_LONG_SIZE
141 : MAX_SELECTION_QUANTUM
);
144 #define LOCAL_SELECTION(selection_symbol,dpyinfo) \
145 assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist)
148 /* Define a queue to save up SELECTION_REQUEST_EVENT events for later
151 struct selection_event_queue
153 struct input_event event
;
154 struct selection_event_queue
*next
;
157 static struct selection_event_queue
*selection_queue
;
159 /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */
161 static int x_queue_selection_requests
;
163 /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */
166 x_queue_event (struct input_event
*event
)
168 struct selection_event_queue
*queue_tmp
;
170 /* Don't queue repeated requests.
171 This only happens for large requests which uses the incremental protocol. */
172 for (queue_tmp
= selection_queue
; queue_tmp
; queue_tmp
= queue_tmp
->next
)
174 if (!memcmp (&queue_tmp
->event
, event
, sizeof (*event
)))
176 TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp
);
177 x_decline_selection_request (event
);
182 queue_tmp
= xmalloc (sizeof *queue_tmp
);
183 TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp
);
184 queue_tmp
->event
= *event
;
185 queue_tmp
->next
= selection_queue
;
186 selection_queue
= queue_tmp
;
189 /* Start queuing SELECTION_REQUEST_EVENT events. */
192 x_start_queuing_selection_requests (void)
194 if (x_queue_selection_requests
)
197 x_queue_selection_requests
++;
198 TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests
);
201 /* Stop queuing SELECTION_REQUEST_EVENT events. */
204 x_stop_queuing_selection_requests (void)
206 TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests
);
207 --x_queue_selection_requests
;
209 /* Take all the queued events and put them back
210 so that they get processed afresh. */
212 while (selection_queue
!= NULL
)
214 struct selection_event_queue
*queue_tmp
= selection_queue
;
215 TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp
);
216 kbd_buffer_unget_event (&queue_tmp
->event
);
217 selection_queue
= queue_tmp
->next
;
223 /* This converts a Lisp symbol to a server Atom, avoiding a server
224 roundtrip whenever possible. */
227 symbol_to_x_atom (struct x_display_info
*dpyinfo
, Lisp_Object sym
)
230 if (NILP (sym
)) return 0;
231 if (EQ (sym
, QPRIMARY
)) return XA_PRIMARY
;
232 if (EQ (sym
, QSECONDARY
)) return XA_SECONDARY
;
233 if (EQ (sym
, QSTRING
)) return XA_STRING
;
234 if (EQ (sym
, QINTEGER
)) return XA_INTEGER
;
235 if (EQ (sym
, QATOM
)) return XA_ATOM
;
236 if (EQ (sym
, QCLIPBOARD
)) return dpyinfo
->Xatom_CLIPBOARD
;
237 if (EQ (sym
, QTIMESTAMP
)) return dpyinfo
->Xatom_TIMESTAMP
;
238 if (EQ (sym
, QTEXT
)) return dpyinfo
->Xatom_TEXT
;
239 if (EQ (sym
, QCOMPOUND_TEXT
)) return dpyinfo
->Xatom_COMPOUND_TEXT
;
240 if (EQ (sym
, QUTF8_STRING
)) return dpyinfo
->Xatom_UTF8_STRING
;
241 if (EQ (sym
, QDELETE
)) return dpyinfo
->Xatom_DELETE
;
242 if (EQ (sym
, QMULTIPLE
)) return dpyinfo
->Xatom_MULTIPLE
;
243 if (EQ (sym
, QINCR
)) return dpyinfo
->Xatom_INCR
;
244 if (EQ (sym
, QEMACS_TMP
)) return dpyinfo
->Xatom_EMACS_TMP
;
245 if (EQ (sym
, QTARGETS
)) return dpyinfo
->Xatom_TARGETS
;
246 if (EQ (sym
, QNULL
)) return dpyinfo
->Xatom_NULL
;
247 if (!SYMBOLP (sym
)) emacs_abort ();
249 TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym
)));
251 val
= XInternAtom (dpyinfo
->display
, SSDATA (SYMBOL_NAME (sym
)), False
);
257 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
258 and calls to intern whenever possible. */
261 x_atom_to_symbol (Display
*dpy
, Atom atom
)
263 struct x_display_info
*dpyinfo
;
284 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 FRAME shall be the owner; it must be a valid X frame.
325 Update the Vselection_alist so that we can reply to later requests for
329 x_own_selection (Lisp_Object selection_name
, Lisp_Object selection_value
,
332 struct frame
*f
= XFRAME (frame
);
333 Window selecting_window
= FRAME_X_WINDOW (f
);
334 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
335 Display
*display
= dpyinfo
->display
;
336 Time timestamp
= last_event_timestamp
;
337 Atom selection_atom
= symbol_to_x_atom (dpyinfo
, selection_name
);
340 x_catch_errors (display
);
341 XSetSelectionOwner (display
, selection_atom
, selecting_window
, timestamp
);
342 x_check_errors (display
, "Can't set selection: %s");
346 /* Now update the local cache */
348 Lisp_Object selection_data
;
349 Lisp_Object prev_value
;
351 selection_data
= list4 (selection_name
, selection_value
,
352 INTEGER_TO_CONS (timestamp
), frame
);
353 prev_value
= LOCAL_SELECTION (selection_name
, dpyinfo
);
357 Fcons (selection_data
, dpyinfo
->terminal
->Vselection_alist
));
359 /* If we already owned the selection, remove the old selection
360 data. Don't use Fdelq as that may QUIT. */
361 if (!NILP (prev_value
))
363 /* We know it's not the CAR, so it's easy. */
364 Lisp_Object rest
= dpyinfo
->terminal
->Vselection_alist
;
365 for (; CONSP (rest
); rest
= XCDR (rest
))
366 if (EQ (prev_value
, Fcar (XCDR (rest
))))
368 XSETCDR (rest
, XCDR (XCDR (rest
)));
375 /* Given a selection-name and desired type, look up our local copy of
376 the selection value and convert it to the type.
377 Return nil, a string, a vector, a symbol, an integer, or a cons
378 that CONS_TO_INTEGER could plausibly handle.
379 This function is used both for remote requests (LOCAL_REQUEST is zero)
380 and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
382 This calls random Lisp code, and may signal or gc. */
385 x_get_local_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
,
386 int local_request
, struct x_display_info
*dpyinfo
)
388 Lisp_Object local_value
;
389 Lisp_Object handler_fn
, value
, check
;
391 local_value
= LOCAL_SELECTION (selection_symbol
, dpyinfo
);
393 if (NILP (local_value
)) return Qnil
;
395 /* TIMESTAMP is a special case. */
396 if (EQ (target_type
, QTIMESTAMP
))
399 value
= XCAR (XCDR (XCDR (local_value
)));
403 /* Don't allow a quit within the converter.
404 When the user types C-g, he would be surprised
405 if by luck it came during a converter. */
406 ptrdiff_t count
= SPECPDL_INDEX ();
407 specbind (Qinhibit_quit
, Qt
);
409 CHECK_SYMBOL (target_type
);
410 handler_fn
= Fcdr (Fassq (target_type
, Vselection_converter_alist
));
411 /* gcpro is not needed here since nothing but HANDLER_FN
412 is live, and that ought to be a symbol. */
414 if (!NILP (handler_fn
))
415 value
= call3 (handler_fn
,
416 selection_symbol
, (local_request
? Qnil
: target_type
),
417 XCAR (XCDR (local_value
)));
420 unbind_to (count
, Qnil
);
423 /* Make sure this value is of a type that we could transmit
424 to another X client. */
428 && SYMBOLP (XCAR (value
)))
429 check
= XCDR (value
);
437 /* Check for a value that CONS_TO_INTEGER could handle. */
438 else if (CONSP (check
)
439 && INTEGERP (XCAR (check
))
440 && (INTEGERP (XCDR (check
))
442 (CONSP (XCDR (check
))
443 && INTEGERP (XCAR (XCDR (check
)))
444 && NILP (XCDR (XCDR (check
))))))
447 signal_error ("Invalid data returned by selection-conversion function",
448 list2 (handler_fn
, value
));
451 /* Subroutines of x_reply_selection_request. */
453 /* Send a SelectionNotify event to the requestor with property=None,
454 meaning we were unable to do what they wanted. */
457 x_decline_selection_request (struct input_event
*event
)
460 XSelectionEvent
*reply
= &(reply_base
.xselection
);
462 reply
->type
= SelectionNotify
;
463 reply
->display
= SELECTION_EVENT_DISPLAY (event
);
464 reply
->requestor
= SELECTION_EVENT_REQUESTOR (event
);
465 reply
->selection
= SELECTION_EVENT_SELECTION (event
);
466 reply
->time
= SELECTION_EVENT_TIME (event
);
467 reply
->target
= SELECTION_EVENT_TARGET (event
);
468 reply
->property
= None
;
470 /* The reason for the error may be that the receiver has
471 died in the meantime. Handle that case. */
473 x_catch_errors (reply
->display
);
474 XSendEvent (reply
->display
, reply
->requestor
, False
, 0L, &reply_base
);
475 XFlush (reply
->display
);
480 /* This is the selection request currently being processed.
481 It is set to zero when the request is fully processed. */
482 static struct input_event
*x_selection_current_request
;
484 /* Display info in x_selection_request. */
486 static struct x_display_info
*selection_request_dpyinfo
;
488 /* Raw selection data, for sending to a requestor window. */
490 struct selection_data
498 /* This can be set to non-NULL during x_reply_selection_request, if
499 the selection is waiting for an INCR transfer to complete. Don't
500 free these; that's done by unexpect_property_change. */
501 struct prop_location
*wait_object
;
502 struct selection_data
*next
;
505 /* Linked list of the above (in support of MULTIPLE targets). */
507 static struct selection_data
*converted_selections
;
509 /* "Data" to send a requestor for a failed MULTIPLE subtarget. */
510 static Atom conversion_fail_tag
;
512 /* Used as an unwind-protect clause so that, if a selection-converter signals
513 an error, we tell the requestor that we were unable to do what they wanted
514 before we throw to top-level or go into the debugger or whatever. */
517 x_selection_request_lisp_error (Lisp_Object ignore
)
519 struct selection_data
*cs
, *next
;
521 for (cs
= converted_selections
; cs
; cs
= next
)
524 if (cs
->nofree
== 0 && cs
->data
)
528 converted_selections
= NULL
;
530 if (x_selection_current_request
!= 0
531 && selection_request_dpyinfo
->display
)
532 x_decline_selection_request (x_selection_current_request
);
537 x_catch_errors_unwind (Lisp_Object dummy
)
546 /* This stuff is so that INCR selections are reentrant (that is, so we can
547 be servicing multiple INCR selection requests simultaneously.) I haven't
548 actually tested that yet. */
550 /* Keep a list of the property changes that are awaited. */
560 struct prop_location
*next
;
563 static struct prop_location
*expect_property_change (Display
*display
, Window window
, Atom property
, int state
);
564 static void wait_for_property_change (struct prop_location
*location
);
565 static void unexpect_property_change (struct prop_location
*location
);
566 static int waiting_for_other_props_on_window (Display
*display
, Window window
);
568 static int prop_location_identifier
;
570 static Lisp_Object property_change_reply
;
572 static struct prop_location
*property_change_reply_object
;
574 static struct prop_location
*property_change_wait_list
;
577 queue_selection_requests_unwind (Lisp_Object tem
)
579 x_stop_queuing_selection_requests ();
584 /* Send the reply to a selection request event EVENT. */
586 #ifdef TRACE_SELECTION
587 static int x_reply_selection_request_cnt
;
588 #endif /* TRACE_SELECTION */
591 x_reply_selection_request (struct input_event
*event
,
592 struct x_display_info
*dpyinfo
)
595 XSelectionEvent
*reply
= &(reply_base
.xselection
);
596 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
597 Window window
= SELECTION_EVENT_REQUESTOR (event
);
598 ptrdiff_t bytes_remaining
;
599 int max_bytes
= selection_quantum (display
);
600 ptrdiff_t count
= SPECPDL_INDEX ();
601 struct selection_data
*cs
;
603 reply
->type
= SelectionNotify
;
604 reply
->display
= display
;
605 reply
->requestor
= window
;
606 reply
->selection
= SELECTION_EVENT_SELECTION (event
);
607 reply
->time
= SELECTION_EVENT_TIME (event
);
608 reply
->target
= SELECTION_EVENT_TARGET (event
);
609 reply
->property
= SELECTION_EVENT_PROPERTY (event
);
610 if (reply
->property
== None
)
611 reply
->property
= reply
->target
;
614 /* The protected block contains wait_for_property_change, which can
615 run random lisp code (process handlers) or signal. Therefore, we
616 put the x_uncatch_errors call in an unwind. */
617 record_unwind_protect (x_catch_errors_unwind
, Qnil
);
618 x_catch_errors (display
);
620 /* Loop over converted selections, storing them in the requested
621 properties. If data is large, only store the first N bytes
622 (section 2.7.2 of ICCCM). Note that we store the data for a
623 MULTIPLE request in the opposite order; the ICCM says only that
624 the conversion itself must be done in the same order. */
625 for (cs
= converted_selections
; cs
; cs
= cs
->next
)
627 if (cs
->property
== None
)
630 bytes_remaining
= cs
->size
;
631 bytes_remaining
*= cs
->format
>> 3;
632 if (bytes_remaining
<= max_bytes
)
634 /* Send all the data at once, with minimal handshaking. */
635 TRACE1 ("Sending all %"pD
"d bytes", bytes_remaining
);
636 XChangeProperty (display
, window
, cs
->property
,
637 cs
->type
, cs
->format
, PropModeReplace
,
642 /* Send an INCR tag to initiate incremental transfer. */
645 TRACE2 ("Start sending %"pD
"d bytes incrementally (%s)",
646 bytes_remaining
, XGetAtomName (display
, cs
->property
));
648 = expect_property_change (display
, window
, cs
->property
,
651 /* XChangeProperty expects an array of long even if long is
652 more than 32 bits. */
653 value
[0] = min (bytes_remaining
, X_LONG_MAX
);
654 XChangeProperty (display
, window
, cs
->property
,
655 dpyinfo
->Xatom_INCR
, 32, PropModeReplace
,
656 (unsigned char *) value
, 1);
657 XSelectInput (display
, window
, PropertyChangeMask
);
661 /* Now issue the SelectionNotify event. */
662 XSendEvent (display
, window
, False
, 0L, &reply_base
);
665 #ifdef TRACE_SELECTION
667 char *sel
= XGetAtomName (display
, reply
->selection
);
668 char *tgt
= XGetAtomName (display
, reply
->target
);
669 TRACE3 ("Sent SelectionNotify: %s, target %s (%d)",
670 sel
, tgt
, ++x_reply_selection_request_cnt
);
671 if (sel
) XFree (sel
);
672 if (tgt
) XFree (tgt
);
674 #endif /* TRACE_SELECTION */
676 /* Finish sending the rest of each of the INCR values. This should
677 be improved; there's a chance of deadlock if more than one
678 subtarget in a MULTIPLE selection requires an INCR transfer, and
679 the requestor and Emacs loop waiting on different transfers. */
680 for (cs
= converted_selections
; cs
; cs
= cs
->next
)
683 int format_bytes
= cs
->format
/ 8;
684 int had_errors
= x_had_errors_p (display
);
687 bytes_remaining
= cs
->size
;
688 bytes_remaining
*= format_bytes
;
690 /* Wait for the requestor to ack by deleting the property.
691 This can run Lisp code (process handlers) or signal. */
694 TRACE1 ("Waiting for ACK (deletion of %s)",
695 XGetAtomName (display
, cs
->property
));
696 wait_for_property_change (cs
->wait_object
);
699 unexpect_property_change (cs
->wait_object
);
701 while (bytes_remaining
)
703 int i
= ((bytes_remaining
< max_bytes
)
705 : max_bytes
) / format_bytes
;
709 = expect_property_change (display
, window
, cs
->property
,
712 TRACE1 ("Sending increment of %d elements", i
);
713 TRACE1 ("Set %s to increment data",
714 XGetAtomName (display
, cs
->property
));
716 /* Append the next chunk of data to the property. */
717 XChangeProperty (display
, window
, cs
->property
,
718 cs
->type
, cs
->format
, PropModeAppend
,
720 bytes_remaining
-= i
* format_bytes
;
721 cs
->data
+= i
* ((cs
->format
== 32) ? sizeof (long)
724 had_errors
= x_had_errors_p (display
);
727 if (had_errors
) break;
729 /* Wait for the requestor to ack this chunk by deleting
730 the property. This can run Lisp code or signal. */
731 TRACE1 ("Waiting for increment ACK (deletion of %s)",
732 XGetAtomName (display
, cs
->property
));
733 wait_for_property_change (cs
->wait_object
);
736 /* Now write a zero-length chunk to the property to tell the
737 requestor that we're done. */
739 if (! waiting_for_other_props_on_window (display
, window
))
740 XSelectInput (display
, window
, 0L);
742 TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
743 XGetAtomName (display
, cs
->property
));
744 XChangeProperty (display
, window
, cs
->property
,
745 cs
->type
, cs
->format
, PropModeReplace
,
747 TRACE0 ("Done sending incrementally");
750 /* rms, 2003-01-03: I think I have fixed this bug. */
751 /* The window we're communicating with may have been deleted
752 in the meantime (that's a real situation from a bug report).
753 In this case, there may be events in the event queue still
754 referring to the deleted window, and we'll get a BadWindow error
755 in XTread_socket when processing the events. I don't have
756 an idea how to fix that. gerd, 2001-01-98. */
757 /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
758 delivered before uncatch errors. */
759 XSync (display
, False
);
762 /* GTK queues events in addition to the queue in Xlib. So we
763 UNBLOCK to enter the event loop and get possible errors delivered,
764 and then BLOCK again because x_uncatch_errors requires it. */
766 /* This calls x_uncatch_errors. */
767 unbind_to (count
, Qnil
);
771 /* Handle a SelectionRequest event EVENT.
772 This is called from keyboard.c when such an event is found in the queue. */
775 x_handle_selection_request (struct input_event
*event
)
777 struct gcpro gcpro1
, gcpro2
;
778 Time local_selection_time
;
780 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
781 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
782 Atom selection
= SELECTION_EVENT_SELECTION (event
);
783 Lisp_Object selection_symbol
= x_atom_to_symbol (display
, selection
);
784 Atom target
= SELECTION_EVENT_TARGET (event
);
785 Lisp_Object target_symbol
= x_atom_to_symbol (display
, target
);
786 Atom property
= SELECTION_EVENT_PROPERTY (event
);
787 Lisp_Object local_selection_data
;
789 ptrdiff_t count
= SPECPDL_INDEX ();
790 GCPRO2 (local_selection_data
, target_symbol
);
792 if (!dpyinfo
) goto DONE
;
794 local_selection_data
= LOCAL_SELECTION (selection_symbol
, dpyinfo
);
796 /* Decline if we don't own any selections. */
797 if (NILP (local_selection_data
)) goto DONE
;
799 /* Decline requests issued prior to our acquiring the selection. */
800 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data
))),
801 Time
, local_selection_time
);
802 if (SELECTION_EVENT_TIME (event
) != CurrentTime
803 && local_selection_time
> SELECTION_EVENT_TIME (event
))
806 x_selection_current_request
= event
;
807 selection_request_dpyinfo
= dpyinfo
;
808 record_unwind_protect (x_selection_request_lisp_error
, Qnil
);
810 /* We might be able to handle nested x_handle_selection_requests,
811 but this is difficult to test, and seems unimportant. */
812 x_start_queuing_selection_requests ();
813 record_unwind_protect (queue_selection_requests_unwind
, Qnil
);
815 TRACE2 ("x_handle_selection_request: selection=%s, target=%s",
816 SDATA (SYMBOL_NAME (selection_symbol
)),
817 SDATA (SYMBOL_NAME (target_symbol
)));
819 if (EQ (target_symbol
, QMULTIPLE
))
821 /* For MULTIPLE targets, the event property names a list of atom
822 pairs; the first atom names a target and the second names a
823 non-None property. */
824 Window requestor
= SELECTION_EVENT_REQUESTOR (event
);
825 Lisp_Object multprop
;
826 ptrdiff_t j
, nselections
;
828 if (property
== None
) goto DONE
;
830 = x_get_window_property_as_lisp_data (display
, requestor
, property
,
831 QMULTIPLE
, selection
);
833 if (!VECTORP (multprop
) || ASIZE (multprop
) % 2)
836 nselections
= ASIZE (multprop
) / 2;
837 /* Perform conversions. This can signal. */
838 for (j
= 0; j
< nselections
; j
++)
840 Lisp_Object subtarget
= AREF (multprop
, 2*j
);
841 Atom subproperty
= symbol_to_x_atom (dpyinfo
,
842 AREF (multprop
, 2*j
+1));
844 if (subproperty
!= None
)
845 x_convert_selection (event
, selection_symbol
, subtarget
,
846 subproperty
, 1, dpyinfo
);
852 if (property
== None
)
853 property
= SELECTION_EVENT_TARGET (event
);
854 success
= x_convert_selection (event
, selection_symbol
,
855 target_symbol
, property
,
862 x_reply_selection_request (event
, dpyinfo
);
864 x_decline_selection_request (event
);
865 x_selection_current_request
= 0;
867 /* Run the `x-sent-selection-functions' abnormal hook. */
868 if (!NILP (Vx_sent_selection_functions
)
869 && !EQ (Vx_sent_selection_functions
, Qunbound
))
872 args
[0] = Qx_sent_selection_functions
;
873 args
[1] = selection_symbol
;
874 args
[2] = target_symbol
;
875 args
[3] = success
? Qt
: Qnil
;
876 Frun_hook_with_args (4, args
);
879 unbind_to (count
, Qnil
);
883 /* Perform the requested selection conversion, and write the data to
884 the converted_selections linked list, where it can be accessed by
885 x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out
886 the data even if conversion fails, using conversion_fail_tag.
888 Return 0 if the selection failed to convert, 1 otherwise. */
891 x_convert_selection (struct input_event
*event
, Lisp_Object selection_symbol
,
892 Lisp_Object target_symbol
, Atom property
,
893 int for_multiple
, struct x_display_info
*dpyinfo
)
896 Lisp_Object lisp_selection
;
897 struct selection_data
*cs
;
898 GCPRO1 (lisp_selection
);
901 = x_get_local_selection (selection_symbol
, target_symbol
,
904 /* A nil return value means we can't perform the conversion. */
905 if (NILP (lisp_selection
)
906 || (CONSP (lisp_selection
) && NILP (XCDR (lisp_selection
))))
910 cs
= xmalloc (sizeof *cs
);
911 cs
->data
= (unsigned char *) &conversion_fail_tag
;
916 cs
->property
= property
;
917 cs
->wait_object
= NULL
;
918 cs
->next
= converted_selections
;
919 converted_selections
= cs
;
926 /* Otherwise, record the converted selection to binary. */
927 cs
= xmalloc (sizeof *cs
);
930 cs
->property
= property
;
931 cs
->wait_object
= NULL
;
932 cs
->next
= converted_selections
;
933 converted_selections
= cs
;
934 lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event
),
936 &(cs
->data
), &(cs
->type
),
937 &(cs
->size
), &(cs
->format
),
943 /* Handle a SelectionClear event EVENT, which indicates that some
944 client cleared out our previously asserted selection.
945 This is called from keyboard.c when such an event is found in the queue. */
948 x_handle_selection_clear (struct input_event
*event
)
950 Display
*display
= SELECTION_EVENT_DISPLAY (event
);
951 Atom selection
= SELECTION_EVENT_SELECTION (event
);
952 Time changed_owner_time
= SELECTION_EVENT_TIME (event
);
954 Lisp_Object selection_symbol
, local_selection_data
;
955 Time local_selection_time
;
956 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
957 Lisp_Object Vselection_alist
;
959 TRACE0 ("x_handle_selection_clear");
961 if (!dpyinfo
) return;
963 selection_symbol
= x_atom_to_symbol (display
, selection
);
964 local_selection_data
= LOCAL_SELECTION (selection_symbol
, dpyinfo
);
966 /* Well, we already believe that we don't own it, so that's just fine. */
967 if (NILP (local_selection_data
)) return;
969 CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data
))),
970 Time
, local_selection_time
);
972 /* We have reasserted the selection since this SelectionClear was
973 generated, so we can disregard it. */
974 if (changed_owner_time
!= CurrentTime
975 && local_selection_time
> changed_owner_time
)
978 /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */
979 Vselection_alist
= dpyinfo
->terminal
->Vselection_alist
;
980 if (EQ (local_selection_data
, CAR (Vselection_alist
)))
981 Vselection_alist
= XCDR (Vselection_alist
);
985 for (rest
= Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
986 if (EQ (local_selection_data
, CAR (XCDR (rest
))))
988 XSETCDR (rest
, XCDR (XCDR (rest
)));
992 tset_selection_alist (dpyinfo
->terminal
, Vselection_alist
);
994 /* Run the `x-lost-selection-functions' abnormal hook. */
997 args
[0] = Qx_lost_selection_functions
;
998 args
[1] = selection_symbol
;
999 Frun_hook_with_args (2, args
);
1002 prepare_menu_bars ();
1003 redisplay_preserve_echo_area (20);
1007 x_handle_selection_event (struct input_event
*event
)
1009 TRACE0 ("x_handle_selection_event");
1010 if (event
->kind
!= SELECTION_REQUEST_EVENT
)
1011 x_handle_selection_clear (event
);
1012 else if (x_queue_selection_requests
)
1013 x_queue_event (event
);
1015 x_handle_selection_request (event
);
1019 /* Clear all selections that were made from frame F.
1020 We do this when about to delete a frame. */
1023 x_clear_frame_selections (FRAME_PTR f
)
1027 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
1028 struct terminal
*t
= dpyinfo
->terminal
;
1030 XSETFRAME (frame
, f
);
1032 /* Delete elements from the beginning of Vselection_alist. */
1033 while (CONSP (t
->Vselection_alist
)
1034 && EQ (frame
, XCAR (XCDR (XCDR (XCDR (XCAR (t
->Vselection_alist
)))))))
1036 /* Run the `x-lost-selection-functions' abnormal hook. */
1037 Lisp_Object args
[2];
1038 args
[0] = Qx_lost_selection_functions
;
1039 args
[1] = Fcar (Fcar (t
->Vselection_alist
));
1040 Frun_hook_with_args (2, args
);
1042 tset_selection_alist (t
, XCDR (t
->Vselection_alist
));
1045 /* Delete elements after the beginning of Vselection_alist. */
1046 for (rest
= t
->Vselection_alist
; CONSP (rest
); rest
= XCDR (rest
))
1047 if (CONSP (XCDR (rest
))
1048 && EQ (frame
, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest
))))))))
1050 Lisp_Object args
[2];
1051 args
[0] = Qx_lost_selection_functions
;
1052 args
[1] = XCAR (XCAR (XCDR (rest
)));
1053 Frun_hook_with_args (2, args
);
1054 XSETCDR (rest
, XCDR (XCDR (rest
)));
1059 /* Nonzero if any properties for DISPLAY and WINDOW
1060 are on the list of what we are waiting for. */
1063 waiting_for_other_props_on_window (Display
*display
, Window window
)
1065 struct prop_location
*rest
= property_change_wait_list
;
1067 if (rest
->display
== display
&& rest
->window
== window
)
1074 /* Add an entry to the list of property changes we are waiting for.
1075 DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for.
1076 The return value is a number that uniquely identifies
1077 this awaited property change. */
1079 static struct prop_location
*
1080 expect_property_change (Display
*display
, Window window
,
1081 Atom property
, int state
)
1083 struct prop_location
*pl
= xmalloc (sizeof *pl
);
1084 pl
->identifier
= ++prop_location_identifier
;
1085 pl
->display
= display
;
1086 pl
->window
= window
;
1087 pl
->property
= property
;
1088 pl
->desired_state
= state
;
1089 pl
->next
= property_change_wait_list
;
1091 property_change_wait_list
= pl
;
1095 /* Delete an entry from the list of property changes we are waiting for.
1096 IDENTIFIER is the number that uniquely identifies the entry. */
1099 unexpect_property_change (struct prop_location
*location
)
1101 struct prop_location
*prev
= 0, *rest
= property_change_wait_list
;
1104 if (rest
== location
)
1107 prev
->next
= rest
->next
;
1109 property_change_wait_list
= rest
->next
;
1118 /* Remove the property change expectation element for IDENTIFIER. */
1121 wait_for_property_change_unwind (Lisp_Object loc
)
1123 struct prop_location
*location
= XSAVE_POINTER (loc
, 0);
1125 unexpect_property_change (location
);
1126 if (location
== property_change_reply_object
)
1127 property_change_reply_object
= 0;
1131 /* Actually wait for a property change.
1132 IDENTIFIER should be the value that expect_property_change returned. */
1135 wait_for_property_change (struct prop_location
*location
)
1137 ptrdiff_t count
= SPECPDL_INDEX ();
1139 if (property_change_reply_object
)
1142 /* Make sure to do unexpect_property_change if we quit or err. */
1143 record_unwind_protect (wait_for_property_change_unwind
,
1144 make_save_pointer (location
));
1146 XSETCAR (property_change_reply
, Qnil
);
1147 property_change_reply_object
= location
;
1149 /* If the event we are waiting for arrives beyond here, it will set
1150 property_change_reply, because property_change_reply_object says so. */
1151 if (! location
->arrived
)
1153 EMACS_INT timeout
= max (0, x_selection_timeout
);
1154 EMACS_INT secs
= timeout
/ 1000;
1155 int nsecs
= (timeout
% 1000) * 1000000;
1156 TRACE2 (" Waiting %"pI
"d secs, %d nsecs", secs
, nsecs
);
1157 wait_reading_process_output (secs
, nsecs
, 0, 0,
1158 property_change_reply
, NULL
, 0);
1160 if (NILP (XCAR (property_change_reply
)))
1162 TRACE0 (" Timed out");
1163 error ("Timed out waiting for property-notify event");
1167 unbind_to (count
, Qnil
);
1170 /* Called from XTread_socket in response to a PropertyNotify event. */
1173 x_handle_property_notify (XPropertyEvent
*event
)
1175 struct prop_location
*rest
;
1177 for (rest
= property_change_wait_list
; rest
; rest
= rest
->next
)
1180 && rest
->property
== event
->atom
1181 && rest
->window
== event
->window
1182 && rest
->display
== event
->display
1183 && rest
->desired_state
== event
->state
)
1185 TRACE2 ("Expected %s of property %s",
1186 (event
->state
== PropertyDelete
? "deletion" : "change"),
1187 XGetAtomName (event
->display
, event
->atom
));
1191 /* If this is the one wait_for_property_change is waiting for,
1192 tell it to wake up. */
1193 if (rest
== property_change_reply_object
)
1194 XSETCAR (property_change_reply
, Qt
);
1203 /* Variables for communication with x_handle_selection_notify. */
1204 static Atom reading_which_selection
;
1205 static Lisp_Object reading_selection_reply
;
1206 static Window reading_selection_window
;
1208 /* Do protocol to read selection-data from the server.
1209 Converts this to Lisp data and returns it.
1210 FRAME is the frame whose X window shall request the selection. */
1213 x_get_foreign_selection (Lisp_Object selection_symbol
, Lisp_Object target_type
,
1214 Lisp_Object time_stamp
, Lisp_Object frame
)
1216 struct frame
*f
= XFRAME (frame
);
1217 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
1218 Display
*display
= dpyinfo
->display
;
1219 Window requestor_window
= FRAME_X_WINDOW (f
);
1220 Time requestor_time
= last_event_timestamp
;
1221 Atom target_property
= dpyinfo
->Xatom_EMACS_TMP
;
1222 Atom selection_atom
= symbol_to_x_atom (dpyinfo
, selection_symbol
);
1223 Atom type_atom
= (CONSP (target_type
)
1224 ? symbol_to_x_atom (dpyinfo
, XCAR (target_type
))
1225 : symbol_to_x_atom (dpyinfo
, target_type
));
1226 EMACS_INT timeout
, secs
;
1229 if (!FRAME_LIVE_P (f
))
1232 if (! NILP (time_stamp
))
1233 CONS_TO_INTEGER (time_stamp
, Time
, requestor_time
);
1236 TRACE2 ("Get selection %s, type %s",
1237 XGetAtomName (display
, type_atom
),
1238 XGetAtomName (display
, target_property
));
1240 x_catch_errors (display
);
1241 XConvertSelection (display
, selection_atom
, type_atom
, target_property
,
1242 requestor_window
, requestor_time
);
1243 x_check_errors (display
, "Can't convert selection: %s");
1244 x_uncatch_errors ();
1246 /* Prepare to block until the reply has been read. */
1247 reading_selection_window
= requestor_window
;
1248 reading_which_selection
= selection_atom
;
1249 XSETCAR (reading_selection_reply
, Qnil
);
1251 /* It should not be necessary to stop handling selection requests
1252 during this time. In fact, the SAVE_TARGETS mechanism requires
1253 us to handle a clipboard manager's requests before it returns
1256 x_start_queuing_selection_requests ();
1257 record_unwind_protect (queue_selection_requests_unwind
, Qnil
);
1262 /* This allows quits. Also, don't wait forever. */
1263 timeout
= max (0, x_selection_timeout
);
1264 secs
= timeout
/ 1000;
1265 nsecs
= (timeout
% 1000) * 1000000;
1266 TRACE1 (" Start waiting %"pI
"d secs for SelectionNotify", secs
);
1267 wait_reading_process_output (secs
, nsecs
, 0, 0,
1268 reading_selection_reply
, NULL
, 0);
1269 TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply
)));
1271 if (NILP (XCAR (reading_selection_reply
)))
1272 error ("Timed out waiting for reply from selection owner");
1273 if (EQ (XCAR (reading_selection_reply
), Qlambda
))
1276 /* Otherwise, the selection is waiting for us on the requested property. */
1278 x_get_window_property_as_lisp_data (display
, requestor_window
,
1279 target_property
, target_type
,
1283 /* Subroutines of x_get_window_property_as_lisp_data */
1285 /* Use xfree, not XFree, to free the data obtained with this function. */
1288 x_get_window_property (Display
*display
, Window window
, Atom property
,
1289 unsigned char **data_ret
, ptrdiff_t *bytes_ret
,
1290 Atom
*actual_type_ret
, int *actual_format_ret
,
1291 unsigned long *actual_size_ret
, int delete_p
)
1293 ptrdiff_t total_size
;
1294 unsigned long bytes_remaining
;
1295 ptrdiff_t offset
= 0;
1296 unsigned char *data
= 0;
1297 unsigned char *tmp_data
= 0;
1299 int buffer_size
= selection_quantum (display
);
1301 /* Wide enough to avoid overflow in expressions using it. */
1302 ptrdiff_t x_long_size
= X_LONG_SIZE
;
1304 /* Maximum value for TOTAL_SIZE. It cannot exceed PTRDIFF_MAX - 1
1305 and SIZE_MAX - 1, for an extra byte at the end. And it cannot
1306 exceed LONG_MAX * X_LONG_SIZE, for XGetWindowProperty. */
1307 ptrdiff_t total_size_max
=
1308 ((min (PTRDIFF_MAX
, SIZE_MAX
) - 1) / x_long_size
< LONG_MAX
1309 ? min (PTRDIFF_MAX
, SIZE_MAX
) - 1
1310 : LONG_MAX
* x_long_size
);
1314 /* First probe the thing to find out how big it is. */
1315 result
= XGetWindowProperty (display
, window
, property
,
1316 0L, 0L, False
, AnyPropertyType
,
1317 actual_type_ret
, actual_format_ret
,
1319 &bytes_remaining
, &tmp_data
);
1320 if (result
!= Success
)
1323 /* This was allocated by Xlib, so use XFree. */
1326 if (*actual_type_ret
== None
|| *actual_format_ret
== 0)
1329 if (total_size_max
< bytes_remaining
)
1331 total_size
= bytes_remaining
;
1332 data
= malloc (total_size
+ 1);
1334 goto memory_exhausted
;
1336 /* Now read, until we've gotten it all. */
1337 while (bytes_remaining
)
1339 ptrdiff_t bytes_gotten
;
1342 = XGetWindowProperty (display
, window
, property
,
1343 offset
/ X_LONG_SIZE
,
1344 buffer_size
/ X_LONG_SIZE
,
1347 actual_type_ret
, actual_format_ret
,
1348 actual_size_ret
, &bytes_remaining
, &tmp_data
);
1350 /* If this doesn't return Success at this point, it means that
1351 some clod deleted the selection while we were in the midst of
1352 reading it. Deal with that, I guess.... */
1353 if (result
!= Success
)
1356 bytes_per_item
= *actual_format_ret
>> 3;
1357 eassert (*actual_size_ret
<= buffer_size
/ bytes_per_item
);
1359 /* The man page for XGetWindowProperty says:
1360 "If the returned format is 32, the returned data is represented
1361 as a long array and should be cast to that type to obtain the
1363 This applies even if long is more than 32 bits, the X library
1364 converts from 32 bit elements received from the X server to long
1365 and passes the long array to us. Thus, for that case memcpy can not
1366 be used. We convert to a 32 bit type here, because so much code
1369 The bytes and offsets passed to XGetWindowProperty refers to the
1370 property and those are indeed in 32 bit quantities if format is 32. */
1372 bytes_gotten
= *actual_size_ret
;
1373 bytes_gotten
*= bytes_per_item
;
1375 TRACE2 ("Read %"pD
"d bytes from property %s",
1376 bytes_gotten
, XGetAtomName (display
, property
));
1378 if (total_size
- offset
< bytes_gotten
)
1380 unsigned char *data1
;
1381 ptrdiff_t remaining_lim
= total_size_max
- offset
- bytes_gotten
;
1382 if (remaining_lim
< 0 || remaining_lim
< bytes_remaining
)
1384 total_size
= offset
+ bytes_gotten
+ bytes_remaining
;
1385 data1
= realloc (data
, total_size
+ 1);
1387 goto memory_exhausted
;
1391 if (BITS_PER_LONG
> 32 && *actual_format_ret
== 32)
1394 int *idata
= (int *) (data
+ offset
);
1395 long *ldata
= (long *) tmp_data
;
1397 for (i
= 0; i
< *actual_size_ret
; ++i
)
1398 idata
[i
] = ldata
[i
];
1401 memcpy (data
+ offset
, tmp_data
, bytes_gotten
);
1403 offset
+= bytes_gotten
;
1405 /* This was allocated by Xlib, so use XFree. */
1410 data
[offset
] = '\0';
1415 *bytes_ret
= offset
;
1421 memory_full (SIZE_MAX
);
1426 memory_full (total_size
+ 1);
1429 /* Use xfree, not XFree, to free the data obtained with this function. */
1432 receive_incremental_selection (Display
*display
, Window window
, Atom property
,
1433 Lisp_Object target_type
,
1434 unsigned int min_size_bytes
,
1435 unsigned char **data_ret
,
1436 ptrdiff_t *size_bytes_ret
,
1437 Atom
*type_ret
, int *format_ret
,
1438 unsigned long *size_ret
)
1440 ptrdiff_t offset
= 0;
1441 struct prop_location
*wait_object
;
1442 if (min (PTRDIFF_MAX
, SIZE_MAX
) < min_size_bytes
)
1443 memory_full (SIZE_MAX
);
1444 *data_ret
= xmalloc (min_size_bytes
);
1445 *size_bytes_ret
= min_size_bytes
;
1447 TRACE1 ("Read %u bytes incrementally", min_size_bytes
);
1449 /* At this point, we have read an INCR property.
1450 Delete the property to ack it.
1451 (But first, prepare to receive the next event in this handshake.)
1453 Now, we must loop, waiting for the sending window to put a value on
1454 that property, then reading the property, then deleting it to ack.
1455 We are done when the sender places a property of length 0.
1458 XSelectInput (display
, window
, STANDARD_EVENT_SET
| PropertyChangeMask
);
1459 TRACE1 (" Delete property %s",
1460 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1461 XDeleteProperty (display
, window
, property
);
1462 TRACE1 (" Expect new value of property %s",
1463 SDATA (SYMBOL_NAME (x_atom_to_symbol (display
, property
))));
1464 wait_object
= expect_property_change (display
, window
, property
,
1471 unsigned char *tmp_data
;
1472 ptrdiff_t tmp_size_bytes
;
1474 TRACE0 (" Wait for property change");
1475 wait_for_property_change (wait_object
);
1477 /* expect it again immediately, because x_get_window_property may
1478 .. no it won't, I don't get it.
1479 .. Ok, I get it now, the Xt code that implements INCR is broken. */
1480 TRACE0 (" Get property value");
1481 x_get_window_property (display
, window
, property
,
1482 &tmp_data
, &tmp_size_bytes
,
1483 type_ret
, format_ret
, size_ret
, 1);
1485 TRACE1 (" Read increment of %"pD
"d bytes", tmp_size_bytes
);
1487 if (tmp_size_bytes
== 0) /* we're done */
1489 TRACE0 ("Done reading incrementally");
1491 if (! waiting_for_other_props_on_window (display
, window
))
1492 XSelectInput (display
, window
, STANDARD_EVENT_SET
);
1493 /* Use xfree, not XFree, because x_get_window_property
1494 calls xmalloc itself. */
1500 TRACE1 (" ACK by deleting property %s",
1501 XGetAtomName (display
, property
));
1502 XDeleteProperty (display
, window
, property
);
1503 wait_object
= expect_property_change (display
, window
, property
,
1508 if (*size_bytes_ret
- offset
< tmp_size_bytes
)
1509 *data_ret
= xpalloc (*data_ret
, size_bytes_ret
,
1510 tmp_size_bytes
- (*size_bytes_ret
- offset
),
1513 memcpy ((*data_ret
) + offset
, tmp_data
, tmp_size_bytes
);
1514 offset
+= tmp_size_bytes
;
1516 /* Use xfree, not XFree, because x_get_window_property
1517 calls xmalloc itself. */
1523 /* Fetch a value from property PROPERTY of X window WINDOW on display
1524 DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message
1528 x_get_window_property_as_lisp_data (Display
*display
, Window window
,
1530 Lisp_Object target_type
,
1531 Atom selection_atom
)
1535 unsigned long actual_size
;
1536 unsigned char *data
= 0;
1537 ptrdiff_t bytes
= 0;
1539 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1541 TRACE0 ("Reading selection data");
1543 x_get_window_property (display
, window
, property
, &data
, &bytes
,
1544 &actual_type
, &actual_format
, &actual_size
, 1);
1547 int there_is_a_selection_owner
;
1549 there_is_a_selection_owner
1550 = XGetSelectionOwner (display
, selection_atom
);
1552 if (there_is_a_selection_owner
)
1553 signal_error ("Selection owner couldn't convert",
1555 ? list2 (target_type
,
1556 x_atom_to_symbol (display
, actual_type
))
1559 signal_error ("No selection",
1560 x_atom_to_symbol (display
, selection_atom
));
1563 if (actual_type
== dpyinfo
->Xatom_INCR
)
1565 /* That wasn't really the data, just the beginning. */
1567 unsigned int min_size_bytes
= * ((unsigned int *) data
);
1569 /* Use xfree, not XFree, because x_get_window_property
1570 calls xmalloc itself. */
1573 receive_incremental_selection (display
, window
, property
, target_type
,
1574 min_size_bytes
, &data
, &bytes
,
1575 &actual_type
, &actual_format
,
1580 TRACE1 (" Delete property %s", XGetAtomName (display
, property
));
1581 XDeleteProperty (display
, window
, property
);
1585 /* It's been read. Now convert it to a lisp object in some semi-rational
1587 val
= selection_data_to_lisp_data (display
, data
, bytes
,
1588 actual_type
, actual_format
);
1590 /* Use xfree, not XFree, because x_get_window_property
1591 calls xmalloc itself. */
1596 /* These functions convert from the selection data read from the server into
1597 something that we can use from Lisp, and vice versa.
1599 Type: Format: Size: Lisp Type:
1600 ----- ------- ----- -----------
1603 ATOM 32 > 1 Vector of Symbols
1605 * 16 > 1 Vector of Integers
1606 * 32 1 if <=16 bits: Integer
1607 if > 16 bits: Cons of top16, bot16
1608 * 32 > 1 Vector of the above
1610 When converting a Lisp number to C, it is assumed to be of format 16 if
1611 it is an integer, and of format 32 if it is a cons of two integers.
1613 When converting a vector of numbers from Lisp to C, it is assumed to be
1614 of format 16 if every element in the vector is an integer, and is assumed
1615 to be of format 32 if any element is a cons of two integers.
1617 When converting an object to C, it may be of the form (SYMBOL . <data>)
1618 where SYMBOL is what we should claim that the type is. Format and
1619 representation are as above.
1621 Important: When format is 32, data should contain an array of int,
1622 not an array of long as the X library returns. This makes a difference
1623 when sizeof(long) != sizeof(int). */
1628 selection_data_to_lisp_data (Display
*display
, const unsigned char *data
,
1629 ptrdiff_t size
, Atom type
, int format
)
1631 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1633 if (type
== dpyinfo
->Xatom_NULL
)
1636 /* Convert any 8-bit data to a string, for compactness. */
1637 else if (format
== 8)
1639 Lisp_Object str
, lispy_type
;
1641 str
= make_unibyte_string ((char *) data
, size
);
1642 /* Indicate that this string is from foreign selection by a text
1643 property `foreign-selection' so that the caller of
1644 x-get-selection-internal (usually x-get-selection) can know
1645 that the string must be decode. */
1646 if (type
== dpyinfo
->Xatom_COMPOUND_TEXT
)
1647 lispy_type
= QCOMPOUND_TEXT
;
1648 else if (type
== dpyinfo
->Xatom_UTF8_STRING
)
1649 lispy_type
= QUTF8_STRING
;
1651 lispy_type
= QSTRING
;
1652 Fput_text_property (make_number (0), make_number (size
),
1653 Qforeign_selection
, lispy_type
, str
);
1656 /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to
1657 a vector of symbols. */
1658 else if (type
== XA_ATOM
1659 /* Treat ATOM_PAIR type similar to list of atoms. */
1660 || type
== dpyinfo
->Xatom_ATOM_PAIR
)
1663 /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8.
1664 But the callers of these function has made sure the data for
1665 format == 32 is an array of int. Thus, use int instead
1667 int *idata
= (int *) data
;
1669 if (size
== sizeof (int))
1670 return x_atom_to_symbol (display
, (Atom
) idata
[0]);
1673 Lisp_Object v
= make_uninit_vector (size
/ sizeof (int));
1675 for (i
= 0; i
< size
/ sizeof (int); i
++)
1676 ASET (v
, i
, x_atom_to_symbol (display
, (Atom
) idata
[i
]));
1681 /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int.
1682 If the number is 32 bits and won't fit in a Lisp_Int,
1683 convert it to a cons of integers, 16 bits in each half.
1685 else if (format
== 32 && size
== sizeof (int))
1686 return INTEGER_TO_CONS (((int *) data
) [0]);
1687 else if (format
== 16 && size
== sizeof (short))
1688 return make_number (((short *) data
) [0]);
1690 /* Convert any other kind of data to a vector of numbers, represented
1691 as above (as an integer, or a cons of two 16 bit integers.)
1693 else if (format
== 16)
1696 Lisp_Object v
= make_uninit_vector (size
/ 2);
1698 for (i
= 0; i
< size
/ 2; i
++)
1700 short j
= ((short *) data
) [i
];
1701 ASET (v
, i
, make_number (j
));
1708 Lisp_Object v
= make_uninit_vector (size
/ X_LONG_SIZE
);
1710 for (i
= 0; i
< size
/ X_LONG_SIZE
; i
++)
1712 int j
= ((int *) data
) [i
];
1713 ASET (v
, i
, INTEGER_TO_CONS (j
));
1719 /* Convert OBJ to an X long value, and return it as unsigned long.
1720 OBJ should be an integer or a cons representing an integer.
1721 Treat values in the range X_LONG_MAX + 1 .. X_ULONG_MAX as X
1722 unsigned long values: in theory these values are supposed to be
1723 signed but in practice unsigned 32-bit data are communicated via X
1724 selections and we need to support that. */
1725 static unsigned long
1726 cons_to_x_long (Lisp_Object obj
)
1728 if (X_ULONG_MAX
<= INTMAX_MAX
1729 || XINT (INTEGERP (obj
) ? obj
: XCAR (obj
)) < 0)
1730 return cons_to_signed (obj
, X_LONG_MIN
, min (X_ULONG_MAX
, INTMAX_MAX
));
1732 return cons_to_unsigned (obj
, X_ULONG_MAX
);
1735 /* Use xfree, not XFree, to free the data obtained with this function. */
1738 lisp_data_to_selection_data (Display
*display
, Lisp_Object obj
,
1739 unsigned char **data_ret
, Atom
*type_ret
,
1740 ptrdiff_t *size_ret
,
1741 int *format_ret
, int *nofree_ret
)
1743 Lisp_Object type
= Qnil
;
1744 struct x_display_info
*dpyinfo
= x_display_info_for_display (display
);
1748 if (CONSP (obj
) && SYMBOLP (XCAR (obj
)))
1752 if (CONSP (obj
) && NILP (XCDR (obj
)))
1756 if (EQ (obj
, QNULL
) || (EQ (type
, QNULL
)))
1757 { /* This is not the same as declining */
1763 else if (STRINGP (obj
))
1765 if (SCHARS (obj
) < SBYTES (obj
))
1766 /* OBJ is a multibyte string containing a non-ASCII char. */
1767 signal_error ("Non-ASCII string must be encoded in advance", obj
);
1771 *size_ret
= SBYTES (obj
);
1772 *data_ret
= SDATA (obj
);
1775 else if (SYMBOLP (obj
))
1777 void *data
= xmalloc (sizeof (Atom
) + 1);
1778 Atom
*x_atom_ptr
= data
;
1782 (*data_ret
) [sizeof (Atom
)] = 0;
1783 *x_atom_ptr
= symbol_to_x_atom (dpyinfo
, obj
);
1784 if (NILP (type
)) type
= QATOM
;
1786 else if (RANGED_INTEGERP (X_SHRT_MIN
, obj
, X_SHRT_MAX
))
1788 void *data
= xmalloc (sizeof (short) + 1);
1789 short *short_ptr
= data
;
1793 (*data_ret
) [sizeof (short)] = 0;
1794 *short_ptr
= XINT (obj
);
1795 if (NILP (type
)) type
= QINTEGER
;
1797 else if (INTEGERP (obj
)
1798 || (CONSP (obj
) && INTEGERP (XCAR (obj
))
1799 && (INTEGERP (XCDR (obj
))
1800 || (CONSP (XCDR (obj
))
1801 && INTEGERP (XCAR (XCDR (obj
)))))))
1803 void *data
= xmalloc (sizeof (unsigned long) + 1);
1804 unsigned long *x_long_ptr
= data
;
1808 (*data_ret
) [sizeof (unsigned long)] = 0;
1809 *x_long_ptr
= cons_to_x_long (obj
);
1810 if (NILP (type
)) type
= QINTEGER
;
1812 else if (VECTORP (obj
))
1814 /* Lisp_Vectors may represent a set of ATOMs;
1815 a set of 16 or 32 bit INTEGERs;
1816 or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...]
1819 ptrdiff_t size
= ASIZE (obj
);
1821 if (SYMBOLP (AREF (obj
, 0)))
1822 /* This vector is an ATOM set */
1826 if (NILP (type
)) type
= QATOM
;
1827 for (i
= 0; i
< size
; i
++)
1828 if (!SYMBOLP (AREF (obj
, i
)))
1829 signal_error ("All elements of selection vector must have same type", obj
);
1831 *data_ret
= data
= xnmalloc (size
, sizeof *x_atoms
);
1835 for (i
= 0; i
< size
; i
++)
1836 x_atoms
[i
] = symbol_to_x_atom (dpyinfo
, AREF (obj
, i
));
1839 /* This vector is an INTEGER set, or something like it */
1842 int data_size
= sizeof (short);
1844 unsigned long *x_atoms
;
1846 if (NILP (type
)) type
= QINTEGER
;
1847 for (i
= 0; i
< size
; i
++)
1849 if (! RANGED_INTEGERP (X_SHRT_MIN
, AREF (obj
, i
),
1852 /* Use sizeof (long) even if it is more than 32 bits.
1853 See comment in x_get_window_property and
1854 x_fill_property_data. */
1855 data_size
= sizeof (long);
1860 *data_ret
= data
= xnmalloc (size
, data_size
);
1863 *format_ret
= format
;
1865 for (i
= 0; i
< size
; i
++)
1868 x_atoms
[i
] = cons_to_x_long (AREF (obj
, i
));
1870 shorts
[i
] = XINT (AREF (obj
, i
));
1875 signal_error (/* Qselection_error */ "Unrecognized selection data", obj
);
1877 *type_ret
= symbol_to_x_atom (dpyinfo
, type
);
1881 clean_local_selection_data (Lisp_Object obj
)
1884 && INTEGERP (XCAR (obj
))
1885 && CONSP (XCDR (obj
))
1886 && INTEGERP (XCAR (XCDR (obj
)))
1887 && NILP (XCDR (XCDR (obj
))))
1888 obj
= Fcons (XCAR (obj
), XCDR (obj
));
1891 && INTEGERP (XCAR (obj
))
1892 && INTEGERP (XCDR (obj
)))
1894 if (XINT (XCAR (obj
)) == 0)
1896 if (XINT (XCAR (obj
)) == -1)
1897 return make_number (- XINT (XCDR (obj
)));
1902 ptrdiff_t size
= ASIZE (obj
);
1905 return clean_local_selection_data (AREF (obj
, 0));
1906 copy
= make_uninit_vector (size
);
1907 for (i
= 0; i
< size
; i
++)
1908 ASET (copy
, i
, clean_local_selection_data (AREF (obj
, i
)));
1914 /* Called from XTread_socket to handle SelectionNotify events.
1915 If it's the selection we are waiting for, stop waiting
1916 by setting the car of reading_selection_reply to non-nil.
1917 We store t there if the reply is successful, lambda if not. */
1920 x_handle_selection_notify (XSelectionEvent
*event
)
1922 if (event
->requestor
!= reading_selection_window
)
1924 if (event
->selection
!= reading_which_selection
)
1927 TRACE0 ("Received SelectionNotify");
1928 XSETCAR (reading_selection_reply
,
1929 (event
->property
!= 0 ? Qt
: Qlambda
));
1933 /* From a Lisp_Object, return a suitable frame for selection
1934 operations. OBJECT may be a frame, a terminal object, or nil
1935 (which stands for the selected frame--or, if that is not an X
1936 frame, the first X display on the list). If no suitable frame can
1937 be found, return NULL. */
1939 static struct frame
*
1940 frame_for_x_selection (Lisp_Object object
)
1942 Lisp_Object tail
, frame
;
1947 f
= XFRAME (selected_frame
);
1948 if (FRAME_X_P (f
) && FRAME_LIVE_P (f
))
1951 FOR_EACH_FRAME (tail
, frame
)
1954 if (FRAME_X_P (f
) && FRAME_LIVE_P (f
))
1958 else if (TERMINALP (object
))
1960 struct terminal
*t
= get_terminal (object
, 1);
1962 if (t
->type
== output_x_window
)
1963 FOR_EACH_FRAME (tail
, frame
)
1966 if (FRAME_LIVE_P (f
) && f
->terminal
== t
)
1970 else if (FRAMEP (object
))
1972 f
= XFRAME (object
);
1973 if (FRAME_X_P (f
) && FRAME_LIVE_P (f
))
1981 DEFUN ("x-own-selection-internal", Fx_own_selection_internal
,
1982 Sx_own_selection_internal
, 2, 3, 0,
1983 doc
: /* Assert an X selection of type SELECTION and value VALUE.
1984 SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
1985 \(Those are literal upper-case symbol names, since that's what X expects.)
1986 VALUE is typically a string, or a cons of two markers, but may be
1987 anything that the functions on `selection-converter-alist' know about.
1989 FRAME should be a frame that should own the selection. If omitted or
1990 nil, it defaults to the selected frame.
1992 On Nextstep, FRAME is unused. */)
1993 (Lisp_Object selection
, Lisp_Object value
, Lisp_Object frame
)
1995 if (NILP (frame
)) frame
= selected_frame
;
1996 if (!FRAME_LIVE_P (XFRAME (frame
)) || !FRAME_X_P (XFRAME (frame
)))
1997 error ("X selection unavailable for this frame");
1999 CHECK_SYMBOL (selection
);
2000 if (NILP (value
)) error ("VALUE may not be nil");
2001 x_own_selection (selection
, value
, frame
);
2006 /* Request the selection value from the owner. If we are the owner,
2007 simply return our selection value. If we are not the owner, this
2008 will block until all of the data has arrived. */
2010 DEFUN ("x-get-selection-internal", Fx_get_selection_internal
,
2011 Sx_get_selection_internal
, 2, 4, 0,
2012 doc
: /* Return text selected from some X window.
2013 SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2014 \(Those are literal upper-case symbol names, since that's what X expects.)
2015 TARGET-TYPE is the type of data desired, typically `STRING'.
2017 TIME-STAMP is the time to use in the XConvertSelection call for foreign
2018 selections. If omitted, defaults to the time for the last event.
2020 TERMINAL should be a terminal object or a frame specifying the X
2021 server to query. If omitted or nil, that stands for the selected
2022 frame's display, or the first available X display.
2024 On Nextstep, TIME-STAMP and TERMINAL are unused. */)
2025 (Lisp_Object selection_symbol
, Lisp_Object target_type
,
2026 Lisp_Object time_stamp
, Lisp_Object terminal
)
2028 Lisp_Object val
= Qnil
;
2029 struct gcpro gcpro1
, gcpro2
;
2030 struct frame
*f
= frame_for_x_selection (terminal
);
2031 GCPRO2 (target_type
, val
); /* we store newly consed data into these */
2033 CHECK_SYMBOL (selection_symbol
);
2034 CHECK_SYMBOL (target_type
);
2035 if (EQ (target_type
, QMULTIPLE
))
2036 error ("Retrieving MULTIPLE selections is currently unimplemented");
2038 error ("X selection unavailable for this frame");
2040 val
= x_get_local_selection (selection_symbol
, target_type
, 1,
2041 FRAME_X_DISPLAY_INFO (f
));
2043 if (NILP (val
) && FRAME_LIVE_P (f
))
2046 XSETFRAME (frame
, f
);
2047 RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol
, target_type
,
2048 time_stamp
, frame
));
2051 if (CONSP (val
) && SYMBOLP (XCAR (val
)))
2054 if (CONSP (val
) && NILP (XCDR (val
)))
2057 RETURN_UNGCPRO (clean_local_selection_data (val
));
2060 DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal
,
2061 Sx_disown_selection_internal
, 1, 3, 0,
2062 doc
: /* If we own the selection SELECTION, disown it.
2063 Disowning it means there is no such selection.
2065 Sets the last-change time for the selection to TIME-OBJECT (by default
2066 the time of the last event).
2068 TERMINAL should be a terminal object or a frame specifying the X
2069 server to query. If omitted or nil, that stands for the selected
2070 frame's display, or the first available X display.
2072 On Nextstep, the TIME-OBJECT and TERMINAL arguments are unused.
2073 On MS-DOS, all this does is return non-nil if we own the selection. */)
2074 (Lisp_Object selection
, Lisp_Object time_object
, Lisp_Object terminal
)
2077 Atom selection_atom
;
2079 struct selection_input_event sie
;
2080 struct input_event ie
;
2082 struct frame
*f
= frame_for_x_selection (terminal
);
2083 struct x_display_info
*dpyinfo
;
2088 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2089 CHECK_SYMBOL (selection
);
2091 /* Don't disown the selection when we're not the owner. */
2092 if (NILP (LOCAL_SELECTION (selection
, dpyinfo
)))
2095 selection_atom
= symbol_to_x_atom (dpyinfo
, selection
);
2098 if (NILP (time_object
))
2099 timestamp
= last_event_timestamp
;
2101 CONS_TO_INTEGER (time_object
, Time
, timestamp
);
2102 XSetSelectionOwner (dpyinfo
->display
, selection_atom
, None
, timestamp
);
2105 /* It doesn't seem to be guaranteed that a SelectionClear event will be
2106 generated for a window which owns the selection when that window sets
2107 the selection owner to None. The NCD server does, the MIT Sun4 server
2108 doesn't. So we synthesize one; this means we might get two, but
2109 that's ok, because the second one won't have any effect. */
2110 SELECTION_EVENT_DISPLAY (&event
.sie
) = dpyinfo
->display
;
2111 SELECTION_EVENT_SELECTION (&event
.sie
) = selection_atom
;
2112 SELECTION_EVENT_TIME (&event
.sie
) = timestamp
;
2113 x_handle_selection_clear (&event
.ie
);
2118 DEFUN ("x-selection-owner-p", Fx_selection_owner_p
, Sx_selection_owner_p
,
2120 doc
: /* Whether the current Emacs process owns the given X Selection.
2121 The arg should be the name of the selection in question, typically one of
2122 the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
2123 \(Those are literal upper-case symbol names, since that's what X expects.)
2124 For convenience, the symbol nil is the same as `PRIMARY',
2125 and t is the same as `SECONDARY'.
2127 TERMINAL should be a terminal object or a frame specifying the X
2128 server to query. If omitted or nil, that stands for the selected
2129 frame's display, or the first available X display.
2131 On Nextstep, TERMINAL is unused. */)
2132 (Lisp_Object selection
, Lisp_Object terminal
)
2134 struct frame
*f
= frame_for_x_selection (terminal
);
2136 CHECK_SYMBOL (selection
);
2137 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2138 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2140 if (f
&& !NILP (LOCAL_SELECTION (selection
, FRAME_X_DISPLAY_INFO (f
))))
2146 DEFUN ("x-selection-exists-p", Fx_selection_exists_p
, Sx_selection_exists_p
,
2148 doc
: /* Whether there is an owner for the given X selection.
2149 SELECTION should be the name of the selection in question, typically
2150 one of the symbols `PRIMARY', `SECONDARY', `CLIPBOARD', or
2151 `CLIPBOARD_MANAGER' (X expects these literal upper-case names.) The
2152 symbol nil is the same as `PRIMARY', and t is the same as `SECONDARY'.
2154 TERMINAL should be a terminal object or a frame specifying the X
2155 server to query. If omitted or nil, that stands for the selected
2156 frame's display, or the first available X display.
2158 On Nextstep, TERMINAL is unused. */)
2159 (Lisp_Object selection
, Lisp_Object terminal
)
2163 struct frame
*f
= frame_for_x_selection (terminal
);
2164 struct x_display_info
*dpyinfo
;
2166 CHECK_SYMBOL (selection
);
2167 if (EQ (selection
, Qnil
)) selection
= QPRIMARY
;
2168 if (EQ (selection
, Qt
)) selection
= QSECONDARY
;
2173 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2175 if (!NILP (LOCAL_SELECTION (selection
, dpyinfo
)))
2178 atom
= symbol_to_x_atom (dpyinfo
, selection
);
2179 if (atom
== 0) return Qnil
;
2181 owner
= XGetSelectionOwner (dpyinfo
->display
, atom
);
2183 return (owner
? Qt
: Qnil
);
2187 /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING
2188 property (http://www.freedesktop.org/wiki/ClipboardManager). */
2191 x_clipboard_manager_save (Lisp_Object frame
)
2193 struct frame
*f
= XFRAME (frame
);
2194 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2195 Atom data
= dpyinfo
->Xatom_UTF8_STRING
;
2197 XChangeProperty (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2198 dpyinfo
->Xatom_EMACS_TMP
,
2199 dpyinfo
->Xatom_ATOM
, 32, PropModeReplace
,
2200 (unsigned char *) &data
, 1);
2201 x_get_foreign_selection (QCLIPBOARD_MANAGER
, QSAVE_TARGETS
,
2206 /* Error handler for x_clipboard_manager_save_frame. */
2209 x_clipboard_manager_error_1 (Lisp_Object err
)
2211 Lisp_Object args
[2];
2212 args
[0] = build_string ("X clipboard manager error: %s\n\
2213 If the problem persists, set `x-select-enable-clipboard-manager' to nil.");
2214 args
[1] = CAR (CDR (err
));
2219 /* Error handler for x_clipboard_manager_save_all. */
2222 x_clipboard_manager_error_2 (Lisp_Object err
)
2224 fprintf (stderr
, "Error saving to X clipboard manager.\n\
2225 If the problem persists, set `x-select-enable-clipboard-manager' \
2230 /* Called from delete_frame: save any clipboard owned by FRAME to the
2231 clipboard manager. Do nothing if FRAME does not own the clipboard,
2232 or if no clipboard manager is present. */
2235 x_clipboard_manager_save_frame (Lisp_Object frame
)
2239 if (!NILP (Vx_select_enable_clipboard_manager
)
2241 && (f
= XFRAME (frame
), FRAME_X_P (f
))
2242 && FRAME_LIVE_P (f
))
2244 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2245 Lisp_Object local_selection
2246 = LOCAL_SELECTION (QCLIPBOARD
, dpyinfo
);
2248 if (!NILP (local_selection
)
2249 && EQ (frame
, XCAR (XCDR (XCDR (XCDR (local_selection
)))))
2250 && XGetSelectionOwner (dpyinfo
->display
,
2251 dpyinfo
->Xatom_CLIPBOARD_MANAGER
))
2252 internal_condition_case_1 (x_clipboard_manager_save
, frame
, Qt
,
2253 x_clipboard_manager_error_1
);
2257 /* Called from Fkill_emacs: save any clipboard owned by FRAME to the
2258 clipboard manager. Do nothing if FRAME does not own the clipboard,
2259 or if no clipboard manager is present. */
2262 x_clipboard_manager_save_all (void)
2264 /* Loop through all X displays, saving owned clipboards. */
2265 struct x_display_info
*dpyinfo
;
2266 Lisp_Object local_selection
, local_frame
;
2268 if (NILP (Vx_select_enable_clipboard_manager
))
2271 for (dpyinfo
= x_display_list
; dpyinfo
; dpyinfo
= dpyinfo
->next
)
2273 local_selection
= LOCAL_SELECTION (QCLIPBOARD
, dpyinfo
);
2274 if (NILP (local_selection
)
2275 || !XGetSelectionOwner (dpyinfo
->display
,
2276 dpyinfo
->Xatom_CLIPBOARD_MANAGER
))
2279 local_frame
= XCAR (XCDR (XCDR (XCDR (local_selection
))));
2280 if (FRAME_LIVE_P (XFRAME (local_frame
)))
2282 Lisp_Object args
[1];
2283 args
[0] = build_string ("Saving clipboard to X clipboard manager...");
2286 internal_condition_case_1 (x_clipboard_manager_save
, local_frame
,
2287 Qt
, x_clipboard_manager_error_2
);
2293 /***********************************************************************
2294 Drag and drop support
2295 ***********************************************************************/
2296 /* Check that lisp values are of correct type for x_fill_property_data.
2297 That is, number, string or a cons with two numbers (low and high 16
2298 bit parts of a 32 bit number). Return the number of items in DATA,
2299 or -1 if there is an error. */
2302 x_check_property_data (Lisp_Object data
)
2307 for (iter
= data
; CONSP (iter
); iter
= XCDR (iter
))
2309 Lisp_Object o
= XCAR (iter
);
2311 if (! NUMBERP (o
) && ! STRINGP (o
) && ! CONSP (o
))
2313 else if (CONSP (o
) &&
2314 (! NUMBERP (XCAR (o
)) || ! NUMBERP (XCDR (o
))))
2316 if (size
== INT_MAX
)
2324 /* Convert lisp values to a C array. Values may be a number, a string
2325 which is taken as an X atom name and converted to the atom value, or
2326 a cons containing the two 16 bit parts of a 32 bit number.
2328 DPY is the display use to look up X atoms.
2329 DATA is a Lisp list of values to be converted.
2330 RET is the C array that contains the converted values. It is assumed
2331 it is big enough to hold all values.
2332 FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to
2333 be stored in RET. Note that long is used for 32 even if long is more
2334 than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and
2335 XClientMessageEvent). */
2338 x_fill_property_data (Display
*dpy
, Lisp_Object data
, void *ret
, int format
)
2341 long *d32
= (long *) ret
;
2342 short *d16
= (short *) ret
;
2343 char *d08
= (char *) ret
;
2346 for (iter
= data
; CONSP (iter
); iter
= XCDR (iter
))
2348 Lisp_Object o
= XCAR (iter
);
2350 if (INTEGERP (o
) || FLOATP (o
) || CONSP (o
))
2351 val
= cons_to_signed (o
, LONG_MIN
, LONG_MAX
);
2352 else if (STRINGP (o
))
2355 val
= (long) XInternAtom (dpy
, SSDATA (o
), False
);
2359 error ("Wrong type, must be string, number or cons");
2363 if (CHAR_MIN
<= val
&& val
<= CHAR_MAX
)
2366 error ("Out of 'char' range");
2368 else if (format
== 16)
2370 if (SHRT_MIN
<= val
&& val
<= SHRT_MAX
)
2373 error ("Out of 'short' range");
2380 /* Convert an array of C values to a Lisp list.
2381 F is the frame to be used to look up X atoms if the TYPE is XA_ATOM.
2382 DATA is a C array of values to be converted.
2383 TYPE is the type of the data. Only XA_ATOM is special, it converts
2384 each number in DATA to its corresponding X atom as a symbol.
2385 FORMAT is 8, 16 or 32 and gives the size in bits for each C value to
2387 SIZE is the number of elements in DATA.
2389 Important: When format is 32, data should contain an array of int,
2390 not an array of long as the X library returns. This makes a difference
2391 when sizeof(long) != sizeof(int).
2393 Also see comment for selection_data_to_lisp_data above. */
2396 x_property_data_to_lisp (struct frame
*f
, const unsigned char *data
,
2397 Atom type
, int format
, long unsigned int size
)
2399 ptrdiff_t format_bytes
= format
>> 3;
2400 if (PTRDIFF_MAX
/ format_bytes
< size
)
2401 memory_full (SIZE_MAX
);
2402 return selection_data_to_lisp_data (FRAME_X_DISPLAY (f
),
2403 data
, size
* format_bytes
, type
, format
);
2406 /* Get the mouse position in frame relative coordinates. */
2409 mouse_position_for_drop (FRAME_PTR f
, int *x
, int *y
)
2411 Window root
, dummy_window
;
2416 XQueryPointer (FRAME_X_DISPLAY (f
),
2417 DefaultRootWindow (FRAME_X_DISPLAY (f
)),
2419 /* The root window which contains the pointer. */
2422 /* Window pointer is on, not used */
2425 /* The position on that root window. */
2428 /* x/y in dummy_window coordinates, not used. */
2431 /* Modifier keys and pointer buttons, about which
2433 (unsigned int *) &dummy
);
2436 /* Absolute to relative. */
2437 *x
-= f
->left_pos
+ FRAME_OUTER_TO_INNER_DIFF_X (f
);
2438 *y
-= f
->top_pos
+ FRAME_OUTER_TO_INNER_DIFF_Y (f
);
2443 DEFUN ("x-get-atom-name", Fx_get_atom_name
,
2444 Sx_get_atom_name
, 1, 2, 0,
2445 doc
: /* Return the X atom name for VALUE as a string.
2446 VALUE may be a number or a cons where the car is the upper 16 bits and
2447 the cdr is the lower 16 bits of a 32 bit value.
2448 Use the display for FRAME or the current frame if FRAME is not given or nil.
2450 If the value is 0 or the atom is not known, return the empty string. */)
2451 (Lisp_Object value
, Lisp_Object frame
)
2453 struct frame
*f
= decode_window_system_frame (frame
);
2456 Lisp_Object ret
= Qnil
;
2457 Display
*dpy
= FRAME_X_DISPLAY (f
);
2461 CONS_TO_INTEGER (value
, Atom
, atom
);
2464 x_catch_errors (dpy
);
2465 name
= atom
? XGetAtomName (dpy
, atom
) : empty
;
2466 had_errors
= x_had_errors_p (dpy
);
2467 x_uncatch_errors ();
2470 ret
= build_string (name
);
2472 if (atom
&& name
) XFree (name
);
2473 if (NILP (ret
)) ret
= empty_unibyte_string
;
2480 DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom
,
2481 Sx_register_dnd_atom
, 1, 2, 0,
2482 doc
: /* Request that dnd events are made for ClientMessages with ATOM.
2483 ATOM can be a symbol or a string. The ATOM is interned on the display that
2484 FRAME is on. If FRAME is nil, the selected frame is used. */)
2485 (Lisp_Object atom
, Lisp_Object frame
)
2488 struct frame
*f
= decode_window_system_frame (frame
);
2490 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2494 x_atom
= symbol_to_x_atom (dpyinfo
, atom
);
2495 else if (STRINGP (atom
))
2498 x_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SSDATA (atom
), False
);
2502 error ("ATOM must be a symbol or a string");
2504 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2505 if (dpyinfo
->x_dnd_atoms
[i
] == x_atom
)
2508 if (dpyinfo
->x_dnd_atoms_length
== dpyinfo
->x_dnd_atoms_size
)
2509 dpyinfo
->x_dnd_atoms
=
2510 xpalloc (dpyinfo
->x_dnd_atoms
, &dpyinfo
->x_dnd_atoms_size
,
2511 1, -1, sizeof *dpyinfo
->x_dnd_atoms
);
2513 dpyinfo
->x_dnd_atoms
[dpyinfo
->x_dnd_atoms_length
++] = x_atom
;
2517 /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */
2520 x_handle_dnd_message (struct frame
*f
, XClientMessageEvent
*event
,
2521 struct x_display_info
*dpyinfo
, struct input_event
*bufp
)
2525 /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */
2526 unsigned long size
= 160/event
->format
;
2528 unsigned char *data
= (unsigned char *) event
->data
.b
;
2532 for (i
= 0; i
< dpyinfo
->x_dnd_atoms_length
; ++i
)
2533 if (dpyinfo
->x_dnd_atoms
[i
] == event
->message_type
) break;
2535 if (i
== dpyinfo
->x_dnd_atoms_length
) return 0;
2537 XSETFRAME (frame
, f
);
2539 /* On a 64 bit machine, the event->data.l array members are 64 bits (long),
2540 but the x_property_data_to_lisp (or rather selection_data_to_lisp_data)
2541 function expects them to be of size int (i.e. 32). So to be able to
2542 use that function, put the data in the form it expects if format is 32. */
2544 if (BITS_PER_LONG
> 32 && event
->format
== 32)
2546 for (i
= 0; i
< 5; ++i
) /* There are only 5 longs in a ClientMessage. */
2547 idata
[i
] = event
->data
.l
[i
];
2548 data
= (unsigned char *) idata
;
2551 vec
= Fmake_vector (make_number (4), Qnil
);
2552 ASET (vec
, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f
),
2553 event
->message_type
)));
2554 ASET (vec
, 1, frame
);
2555 ASET (vec
, 2, make_number (event
->format
));
2556 ASET (vec
, 3, x_property_data_to_lisp (f
,
2558 event
->message_type
,
2562 mouse_position_for_drop (f
, &x
, &y
);
2563 bufp
->kind
= DRAG_N_DROP_EVENT
;
2564 bufp
->frame_or_window
= frame
;
2565 bufp
->timestamp
= CurrentTime
;
2566 bufp
->x
= make_number (x
);
2567 bufp
->y
= make_number (y
);
2569 bufp
->modifiers
= 0;
2574 DEFUN ("x-send-client-message", Fx_send_client_message
,
2575 Sx_send_client_message
, 6, 6, 0,
2576 doc
: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
2578 For DISPLAY, specify either a frame or a display name (a string).
2579 If DISPLAY is nil, that stands for the selected frame's display.
2580 DEST may be a number, in which case it is a Window id. The value 0 may
2581 be used to send to the root window of the DISPLAY.
2582 If DEST is a cons, it is converted to a 32 bit number
2583 with the high 16 bits from the car and the lower 16 bit from the cdr. That
2584 number is then used as a window id.
2585 If DEST is a frame the event is sent to the outer window of that frame.
2586 A value of nil means the currently selected frame.
2587 If DEST is the string "PointerWindow" the event is sent to the window that
2588 contains the pointer. If DEST is the string "InputFocus" the event is
2589 sent to the window that has the input focus.
2590 FROM is the frame sending the event. Use nil for currently selected frame.
2591 MESSAGE-TYPE is the name of an Atom as a string.
2592 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
2593 bits. VALUES is a list of numbers, cons and/or strings containing the values
2594 to send. If a value is a string, it is converted to an Atom and the value of
2595 the Atom is sent. If a value is a cons, it is converted to a 32 bit number
2596 with the high 16 bits from the car and the lower 16 bit from the cdr.
2597 If more values than fits into the event is given, the excessive values
2599 (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
,
2600 Lisp_Object message_type
, Lisp_Object format
, Lisp_Object values
)
2602 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2604 CHECK_STRING (message_type
);
2605 x_send_client_event (display
, dest
, from
,
2606 XInternAtom (dpyinfo
->display
,
2607 SSDATA (message_type
),
2615 x_send_client_event (Lisp_Object display
, Lisp_Object dest
, Lisp_Object from
,
2616 Atom message_type
, Lisp_Object format
, Lisp_Object values
)
2618 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
2621 struct frame
*f
= decode_window_system_frame (from
);
2624 CHECK_NUMBER (format
);
2625 CHECK_CONS (values
);
2627 if (x_check_property_data (values
) == -1)
2628 error ("Bad data in VALUES, must be number, cons or string");
2630 if (XINT (format
) != 8 && XINT (format
) != 16 && XINT (format
) != 32)
2631 error ("FORMAT must be one of 8, 16 or 32");
2633 event
.xclient
.type
= ClientMessage
;
2634 event
.xclient
.format
= XINT (format
);
2636 if (FRAMEP (dest
) || NILP (dest
))
2638 struct frame
*fdest
= decode_window_system_frame (dest
);
2639 wdest
= FRAME_OUTER_WINDOW (fdest
);
2641 else if (STRINGP (dest
))
2643 if (strcmp (SSDATA (dest
), "PointerWindow") == 0)
2644 wdest
= PointerWindow
;
2645 else if (strcmp (SSDATA (dest
), "InputFocus") == 0)
2648 error ("DEST as a string must be one of PointerWindow or InputFocus");
2650 else if (INTEGERP (dest
) || FLOATP (dest
) || CONSP (dest
))
2651 CONS_TO_INTEGER (dest
, Window
, wdest
);
2653 error ("DEST must be a frame, nil, string, number or cons");
2655 if (wdest
== 0) wdest
= dpyinfo
->root_window
;
2656 to_root
= wdest
== dpyinfo
->root_window
;
2660 event
.xclient
.message_type
= message_type
;
2661 event
.xclient
.display
= dpyinfo
->display
;
2663 /* Some clients (metacity for example) expects sending window to be here
2664 when sending to the root window. */
2665 event
.xclient
.window
= to_root
? FRAME_OUTER_WINDOW (f
) : wdest
;
2668 memset (event
.xclient
.data
.b
, 0, sizeof (event
.xclient
.data
.b
));
2669 x_fill_property_data (dpyinfo
->display
, values
, event
.xclient
.data
.b
,
2670 event
.xclient
.format
);
2672 /* If event mask is 0 the event is sent to the client that created
2673 the destination window. But if we are sending to the root window,
2674 there is no such client. Then we set the event mask to 0xffff. The
2675 event then goes to clients selecting for events on the root window. */
2676 x_catch_errors (dpyinfo
->display
);
2678 int propagate
= to_root
? False
: True
;
2679 unsigned mask
= to_root
? 0xffff : 0;
2680 XSendEvent (dpyinfo
->display
, wdest
, propagate
, mask
, &event
);
2681 XFlush (dpyinfo
->display
);
2683 x_uncatch_errors ();
2689 syms_of_xselect (void)
2691 defsubr (&Sx_get_selection_internal
);
2692 defsubr (&Sx_own_selection_internal
);
2693 defsubr (&Sx_disown_selection_internal
);
2694 defsubr (&Sx_selection_owner_p
);
2695 defsubr (&Sx_selection_exists_p
);
2697 defsubr (&Sx_get_atom_name
);
2698 defsubr (&Sx_send_client_message
);
2699 defsubr (&Sx_register_dnd_atom
);
2701 reading_selection_reply
= Fcons (Qnil
, Qnil
);
2702 staticpro (&reading_selection_reply
);
2703 reading_selection_window
= 0;
2704 reading_which_selection
= 0;
2706 property_change_wait_list
= 0;
2707 prop_location_identifier
= 0;
2708 property_change_reply
= Fcons (Qnil
, Qnil
);
2709 staticpro (&property_change_reply
);
2711 converted_selections
= NULL
;
2712 conversion_fail_tag
= None
;
2714 DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist
,
2715 doc
: /* An alist associating X Windows selection-types with functions.
2716 These functions are called to convert the selection, with three args:
2717 the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2718 a desired type to which the selection should be converted;
2719 and the local selection value (whatever was given to `x-own-selection').
2721 The function should return the value to send to the X server
2722 \(typically a string). A return value of nil
2723 means that the conversion could not be done.
2724 A return value which is the symbol `NULL'
2725 means that a side-effect was executed,
2726 and there is no meaningful selection value. */);
2727 Vselection_converter_alist
= Qnil
;
2729 DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions
,
2730 doc
: /* A list of functions to be called when Emacs loses an X selection.
2731 \(This happens when some other X client makes its own selection
2732 or when a Lisp program explicitly clears the selection.)
2733 The functions are called with one argument, the selection type
2734 \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */);
2735 Vx_lost_selection_functions
= Qnil
;
2737 DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions
,
2738 doc
: /* A list of functions to be called when Emacs answers a selection request.
2739 The functions are called with three arguments:
2740 - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD');
2741 - the selection-type which Emacs was asked to convert the
2742 selection into before sending (for example, `STRING' or `LENGTH');
2743 - a flag indicating success or failure for responding to the request.
2744 We might have failed (and declined the request) for any number of reasons,
2745 including being asked for a selection that we no longer own, or being asked
2746 to convert into a type that we don't know about or that is inappropriate.
2747 This hook doesn't let you change the behavior of Emacs's selection replies,
2748 it merely informs you that they have happened. */);
2749 Vx_sent_selection_functions
= Qnil
;
2751 DEFVAR_LISP ("x-select-enable-clipboard-manager",
2752 Vx_select_enable_clipboard_manager
,
2753 doc
: /* Whether to enable X clipboard manager support.
2754 If non-nil, then whenever Emacs is killed or an Emacs frame is deleted
2755 while owning the X clipboard, the clipboard contents are saved to the
2756 clipboard manager if one is present. */);
2757 Vx_select_enable_clipboard_manager
= Qt
;
2759 DEFVAR_INT ("x-selection-timeout", x_selection_timeout
,
2760 doc
: /* Number of milliseconds to wait for a selection reply.
2761 If the selection owner doesn't reply in this time, we give up.
2762 A value of 0 means wait as long as necessary. This is initialized from the
2763 \"*selectionTimeout\" resource. */);
2764 x_selection_timeout
= 0;
2766 /* QPRIMARY is defined in keyboard.c. */
2767 DEFSYM (QSECONDARY
, "SECONDARY");
2768 DEFSYM (QSTRING
, "STRING");
2769 DEFSYM (QINTEGER
, "INTEGER");
2770 DEFSYM (QCLIPBOARD
, "CLIPBOARD");
2771 DEFSYM (QTIMESTAMP
, "TIMESTAMP");
2772 DEFSYM (QTEXT
, "TEXT");
2773 DEFSYM (QCOMPOUND_TEXT
, "COMPOUND_TEXT");
2774 DEFSYM (QUTF8_STRING
, "UTF8_STRING");
2775 DEFSYM (QDELETE
, "DELETE");
2776 DEFSYM (QMULTIPLE
, "MULTIPLE");
2777 DEFSYM (QINCR
, "INCR");
2778 DEFSYM (QEMACS_TMP
, "_EMACS_TMP_");
2779 DEFSYM (QTARGETS
, "TARGETS");
2780 DEFSYM (QATOM
, "ATOM");
2781 DEFSYM (QATOM_PAIR
, "ATOM_PAIR");
2782 DEFSYM (QCLIPBOARD_MANAGER
, "CLIPBOARD_MANAGER");
2783 DEFSYM (QSAVE_TARGETS
, "SAVE_TARGETS");
2784 DEFSYM (QNULL
, "NULL");
2785 DEFSYM (Qcompound_text_with_extensions
, "compound-text-with-extensions");
2786 DEFSYM (Qforeign_selection
, "foreign-selection");
2787 DEFSYM (Qx_lost_selection_functions
, "x-lost-selection-functions");
2788 DEFSYM (Qx_sent_selection_functions
, "x-sent-selection-functions");