Commit | Line | Data |
---|---|---|
2408b3a1 | 1 | /* X Selection processing for Emacs. |
0b5538bd | 2 | Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003, |
5df4f04c | 3 | 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
ede4db72 RS |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
ede4db72 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
ede4db72 RS |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
ede4db72 | 19 | |
c6c5df7f | 20 | |
ede4db72 RS |
21 | /* Rewritten by jwz */ |
22 | ||
18160b98 | 23 | #include <config.h> |
5b698285 | 24 | #include <stdio.h> /* termhooks.h needs this */ |
d7306fe6 | 25 | #include <setjmp.h> |
678b3958 KS |
26 | |
27 | #ifdef HAVE_SYS_TYPES_H | |
28 | #include <sys/types.h> | |
29 | #endif | |
30 | #ifdef HAVE_UNISTD_H | |
31 | #include <unistd.h> | |
32 | #endif | |
33 | ||
ede4db72 RS |
34 | #include "lisp.h" |
35 | #include "xterm.h" /* for all of the X includes */ | |
7da64e5c RS |
36 | #include "dispextern.h" /* frame.h seems to want this */ |
37 | #include "frame.h" /* Need this to get the X window of selected_frame */ | |
9ac0d9e0 | 38 | #include "blockinput.h" |
5faa9b45 | 39 | #include "buffer.h" |
dfcf069d | 40 | #include "process.h" |
1fb3821b | 41 | #include "termhooks.h" |
dd0fe424 | 42 | #include "keyboard.h" |
1fb3821b JD |
43 | |
44 | #include <X11/Xproto.h> | |
7da64e5c | 45 | |
d9c0d4a3 GM |
46 | struct prop_location; |
47 | ||
f57e2426 J |
48 | static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom); |
49 | static Atom symbol_to_x_atom (struct x_display_info *, Display *, | |
50 | Lisp_Object); | |
51 | static void x_own_selection (Lisp_Object, Lisp_Object); | |
52 | static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int); | |
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 some_frame_on_display (struct x_display_info *); | |
57 | static Lisp_Object x_catch_errors_unwind (Lisp_Object); | |
58 | static void x_reply_selection_request (struct input_event *, int, | |
59 | unsigned char *, int, Atom); | |
60 | static int waiting_for_other_props_on_window (Display *, Window); | |
61 | static struct prop_location *expect_property_change (Display *, Window, | |
62 | Atom, int); | |
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, | |
67 | Lisp_Object, | |
68 | Lisp_Object); | |
69 | static void x_get_window_property (Display *, Window, Atom, | |
70 | unsigned char **, int *, | |
71 | Atom *, int *, unsigned long *, int); | |
72 | static void receive_incremental_selection (Display *, Window, Atom, | |
73 | Lisp_Object, unsigned, | |
74 | unsigned char **, int *, | |
75 | Atom *, int *, unsigned long *); | |
76 | static Lisp_Object x_get_window_property_as_lisp_data (Display *, | |
77 | Window, Atom, | |
78 | Lisp_Object, Atom); | |
eec47d6b DN |
79 | static Lisp_Object selection_data_to_lisp_data (Display *, |
80 | const unsigned char *, | |
f57e2426 J |
81 | int, Atom, int); |
82 | static void lisp_data_to_selection_data (Display *, Lisp_Object, | |
83 | unsigned char **, Atom *, | |
84 | unsigned *, int *, int *); | |
85 | static Lisp_Object clean_local_selection_data (Lisp_Object); | |
d9c0d4a3 GM |
86 | |
87 | /* Printing traces to stderr. */ | |
88 | ||
89 | #ifdef TRACE_SELECTION | |
90 | #define TRACE0(fmt) \ | |
91 | fprintf (stderr, "%d: " fmt "\n", getpid ()) | |
92 | #define TRACE1(fmt, a0) \ | |
93 | fprintf (stderr, "%d: " fmt "\n", getpid (), a0) | |
94 | #define TRACE2(fmt, a0, a1) \ | |
95 | fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1) | |
9c3ad9e1 JD |
96 | #define TRACE3(fmt, a0, a1, a2) \ |
97 | fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2) | |
d9c0d4a3 GM |
98 | #else |
99 | #define TRACE0(fmt) (void) 0 | |
100 | #define TRACE1(fmt, a0) (void) 0 | |
101 | #define TRACE2(fmt, a0, a1) (void) 0 | |
9c3ad9e1 | 102 | #define TRACE3(fmt, a0, a1) (void) 0 |
d9c0d4a3 GM |
103 | #endif |
104 | ||
105 | ||
e45a249b | 106 | Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP, |
ede4db72 RS |
107 | QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL, |
108 | QATOM_PAIR; | |
109 | ||
e6c7c988 | 110 | Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */ |
5109c8dd | 111 | Lisp_Object QUTF8_STRING; /* This is a type of selection. */ |
e6c7c988 | 112 | |
5a79ea57 | 113 | Lisp_Object Qcompound_text_with_extensions; |
fbbe0ace | 114 | |
c917a8de SM |
115 | static Lisp_Object Vx_lost_selection_functions; |
116 | static Lisp_Object Vx_sent_selection_functions; | |
e57ad4d8 KH |
117 | static Lisp_Object Qforeign_selection; |
118 | ||
ede4db72 RS |
119 | /* If this is a smaller number than the max-request-size of the display, |
120 | emacs will use INCR selection transfer when the selection is larger | |
121 | than this. The max-request-size is usually around 64k, so if you want | |
1b65481e | 122 | emacs to use incremental selection transfers when the selection is |
ede4db72 | 123 | smaller than that, set this. I added this mostly for debugging the |
8f4f023f | 124 | incremental transfer stuff, but it might improve server performance. */ |
ede4db72 RS |
125 | #define MAX_SELECTION_QUANTUM 0xFFFFFF |
126 | ||
c3498e64 | 127 | #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100) |
ede4db72 | 128 | |
7da64e5c | 129 | /* The timestamp of the last input event Emacs received from the X server. */ |
29674445 KH |
130 | /* Defined in keyboard.c. */ |
131 | extern unsigned long last_event_timestamp; | |
ede4db72 RS |
132 | |
133 | /* This is an association list whose elements are of the form | |
118bd841 RS |
134 | ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME) |
135 | SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom. | |
136 | SELECTION-VALUE is the value that emacs owns for that selection. | |
ede4db72 | 137 | It may be any kind of Lisp object. |
118bd841 | 138 | SELECTION-TIMESTAMP is the time at which emacs began owning this selection, |
ede4db72 | 139 | as a cons of two 16-bit numbers (making a 32 bit time.) |
118bd841 RS |
140 | FRAME is the frame for which we made the selection. |
141 | If there is an entry in this alist, then it can be assumed that Emacs owns | |
ede4db72 RS |
142 | that selection. |
143 | The only (eq) parts of this list that are visible from Lisp are the | |
8f4f023f RS |
144 | selection-values. */ |
145 | static Lisp_Object Vselection_alist; | |
ede4db72 RS |
146 | |
147 | /* This is an alist whose CARs are selection-types (whose names are the same | |
148 | as the names of X Atoms) and whose CDRs are the names of Lisp functions to | |
1b65481e | 149 | call to convert the given Emacs selection value to a string representing |
ede4db72 | 150 | the given selection type. This is for Lisp-level extension of the emacs |
8f4f023f RS |
151 | selection handling. */ |
152 | static Lisp_Object Vselection_converter_alist; | |
ede4db72 RS |
153 | |
154 | /* If the selection owner takes too long to reply to a selection request, | |
8f4f023f | 155 | we give up on it. This is in milliseconds (0 = no timeout.) */ |
31ade731 | 156 | static EMACS_INT x_selection_timeout; |
ede4db72 | 157 | |
dd0fe424 KS |
158 | |
159 | \f | |
678b3958 KS |
160 | /* Define a queue to save up SELECTION_REQUEST_EVENT events for later |
161 | handling. */ | |
dd0fe424 KS |
162 | |
163 | struct selection_event_queue | |
164 | { | |
165 | struct input_event event; | |
166 | struct selection_event_queue *next; | |
167 | }; | |
168 | ||
169 | static struct selection_event_queue *selection_queue; | |
170 | ||
678b3958 | 171 | /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */ |
dd0fe424 KS |
172 | |
173 | static int x_queue_selection_requests; | |
174 | ||
678b3958 | 175 | /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */ |
dd0fe424 KS |
176 | |
177 | static void | |
971de7fb | 178 | x_queue_event (struct input_event *event) |
dd0fe424 KS |
179 | { |
180 | struct selection_event_queue *queue_tmp; | |
181 | ||
678b3958 KS |
182 | /* Don't queue repeated requests. |
183 | This only happens for large requests which uses the incremental protocol. */ | |
dd0fe424 KS |
184 | for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next) |
185 | { | |
72af86bd | 186 | if (!memcmp (&queue_tmp->event, event, sizeof (*event))) |
dd0fe424 | 187 | { |
678b3958 KS |
188 | TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp); |
189 | x_decline_selection_request (event); | |
dd0fe424 KS |
190 | return; |
191 | } | |
192 | } | |
193 | ||
194 | queue_tmp | |
195 | = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue)); | |
196 | ||
197 | if (queue_tmp != NULL) | |
198 | { | |
678b3958 | 199 | TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp); |
dd0fe424 KS |
200 | queue_tmp->event = *event; |
201 | queue_tmp->next = selection_queue; | |
202 | selection_queue = queue_tmp; | |
203 | } | |
204 | } | |
205 | ||
678b3958 | 206 | /* Start queuing SELECTION_REQUEST_EVENT events. */ |
dd0fe424 KS |
207 | |
208 | static void | |
971de7fb | 209 | x_start_queuing_selection_requests (void) |
dd0fe424 KS |
210 | { |
211 | if (x_queue_selection_requests) | |
212 | abort (); | |
213 | ||
214 | x_queue_selection_requests++; | |
215 | TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests); | |
216 | } | |
217 | ||
678b3958 | 218 | /* Stop queuing SELECTION_REQUEST_EVENT events. */ |
dd0fe424 KS |
219 | |
220 | static void | |
971de7fb | 221 | x_stop_queuing_selection_requests (void) |
dd0fe424 KS |
222 | { |
223 | TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests); | |
224 | --x_queue_selection_requests; | |
225 | ||
226 | /* Take all the queued events and put them back | |
227 | so that they get processed afresh. */ | |
228 | ||
229 | while (selection_queue != NULL) | |
230 | { | |
231 | struct selection_event_queue *queue_tmp = selection_queue; | |
678b3958 | 232 | TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp); |
dd0fe424 KS |
233 | kbd_buffer_unget_event (&queue_tmp->event); |
234 | selection_queue = queue_tmp->next; | |
235 | xfree ((char *)queue_tmp); | |
236 | } | |
237 | } | |
238 | \f | |
239 | ||
1b65481e | 240 | /* This converts a Lisp symbol to a server Atom, avoiding a server |
ede4db72 RS |
241 | roundtrip whenever possible. */ |
242 | ||
243 | static Atom | |
971de7fb | 244 | symbol_to_x_atom (struct x_display_info *dpyinfo, Display *display, Lisp_Object sym) |
ede4db72 RS |
245 | { |
246 | Atom val; | |
247 | if (NILP (sym)) return 0; | |
248 | if (EQ (sym, QPRIMARY)) return XA_PRIMARY; | |
249 | if (EQ (sym, QSECONDARY)) return XA_SECONDARY; | |
250 | if (EQ (sym, QSTRING)) return XA_STRING; | |
251 | if (EQ (sym, QINTEGER)) return XA_INTEGER; | |
252 | if (EQ (sym, QATOM)) return XA_ATOM; | |
5c3a351a RS |
253 | if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD; |
254 | if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP; | |
255 | if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT; | |
e6c7c988 | 256 | if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT; |
5109c8dd | 257 | if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING; |
5c3a351a RS |
258 | if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE; |
259 | if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE; | |
260 | if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR; | |
261 | if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP; | |
262 | if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS; | |
263 | if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL; | |
ede4db72 RS |
264 | if (!SYMBOLP (sym)) abort (); |
265 | ||
d5db4077 | 266 | TRACE1 (" XInternAtom %s", (char *) SDATA (SYMBOL_NAME (sym))); |
ede4db72 | 267 | BLOCK_INPUT; |
d5db4077 | 268 | val = XInternAtom (display, (char *) SDATA (SYMBOL_NAME (sym)), False); |
ede4db72 RS |
269 | UNBLOCK_INPUT; |
270 | return val; | |
271 | } | |
272 | ||
273 | ||
274 | /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips | |
275 | and calls to intern whenever possible. */ | |
276 | ||
277 | static Lisp_Object | |
971de7fb | 278 | x_atom_to_symbol (Display *dpy, Atom atom) |
ede4db72 | 279 | { |
d9c0d4a3 | 280 | struct x_display_info *dpyinfo; |
ede4db72 RS |
281 | char *str; |
282 | Lisp_Object val; | |
1b65481e | 283 | |
d9c0d4a3 GM |
284 | if (! atom) |
285 | return Qnil; | |
1b65481e | 286 | |
7da64e5c RS |
287 | switch (atom) |
288 | { | |
289 | case XA_PRIMARY: | |
290 | return QPRIMARY; | |
291 | case XA_SECONDARY: | |
292 | return QSECONDARY; | |
293 | case XA_STRING: | |
294 | return QSTRING; | |
295 | case XA_INTEGER: | |
296 | return QINTEGER; | |
297 | case XA_ATOM: | |
298 | return QATOM; | |
7da64e5c RS |
299 | } |
300 | ||
d9c0d4a3 | 301 | dpyinfo = x_display_info_for_display (dpy); |
5c3a351a | 302 | if (atom == dpyinfo->Xatom_CLIPBOARD) |
7da64e5c | 303 | return QCLIPBOARD; |
5c3a351a | 304 | if (atom == dpyinfo->Xatom_TIMESTAMP) |
7da64e5c | 305 | return QTIMESTAMP; |
5c3a351a | 306 | if (atom == dpyinfo->Xatom_TEXT) |
7da64e5c | 307 | return QTEXT; |
e6c7c988 KH |
308 | if (atom == dpyinfo->Xatom_COMPOUND_TEXT) |
309 | return QCOMPOUND_TEXT; | |
5109c8dd KH |
310 | if (atom == dpyinfo->Xatom_UTF8_STRING) |
311 | return QUTF8_STRING; | |
5c3a351a | 312 | if (atom == dpyinfo->Xatom_DELETE) |
7da64e5c | 313 | return QDELETE; |
5c3a351a | 314 | if (atom == dpyinfo->Xatom_MULTIPLE) |
7da64e5c | 315 | return QMULTIPLE; |
5c3a351a | 316 | if (atom == dpyinfo->Xatom_INCR) |
7da64e5c | 317 | return QINCR; |
5c3a351a | 318 | if (atom == dpyinfo->Xatom_EMACS_TMP) |
7da64e5c | 319 | return QEMACS_TMP; |
5c3a351a | 320 | if (atom == dpyinfo->Xatom_TARGETS) |
7da64e5c | 321 | return QTARGETS; |
5c3a351a | 322 | if (atom == dpyinfo->Xatom_NULL) |
7da64e5c | 323 | return QNULL; |
ede4db72 RS |
324 | |
325 | BLOCK_INPUT; | |
d9c0d4a3 | 326 | str = XGetAtomName (dpy, atom); |
ede4db72 | 327 | UNBLOCK_INPUT; |
d9c0d4a3 | 328 | TRACE1 ("XGetAtomName --> %s", str); |
ede4db72 RS |
329 | if (! str) return Qnil; |
330 | val = intern (str); | |
331 | BLOCK_INPUT; | |
0158abbc | 332 | /* This was allocated by Xlib, so use XFree. */ |
ede4db72 RS |
333 | XFree (str); |
334 | UNBLOCK_INPUT; | |
335 | return val; | |
336 | } | |
8a89415e | 337 | \f |
ede4db72 | 338 | /* Do protocol to assert ourself as a selection owner. |
1b65481e | 339 | Update the Vselection_alist so that we can reply to later requests for |
ede4db72 RS |
340 | our selection. */ |
341 | ||
342 | static void | |
971de7fb | 343 | x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value) |
ede4db72 | 344 | { |
378c33ca | 345 | struct frame *sf = SELECTED_FRAME (); |
428a555e KL |
346 | Window selecting_window; |
347 | Display *display; | |
7da64e5c | 348 | Time time = last_event_timestamp; |
ede4db72 | 349 | Atom selection_atom; |
428a555e | 350 | struct x_display_info *dpyinfo; |
ede4db72 | 351 | |
428a555e KL |
352 | if (! FRAME_X_P (sf)) |
353 | return; | |
354 | ||
355 | selecting_window = FRAME_X_WINDOW (sf); | |
356 | display = FRAME_X_DISPLAY (sf); | |
357 | dpyinfo = FRAME_X_DISPLAY_INFO (sf); | |
16041401 | 358 | |
b7826503 | 359 | CHECK_SYMBOL (selection_name); |
5c3a351a | 360 | selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name); |
ede4db72 RS |
361 | |
362 | BLOCK_INPUT; | |
9ba8e10d | 363 | x_catch_errors (display); |
ede4db72 | 364 | XSetSelectionOwner (display, selection_atom, selecting_window, time); |
a7b24d46 | 365 | x_check_errors (display, "Can't set selection: %s"); |
4545fa20 | 366 | x_uncatch_errors (); |
ede4db72 RS |
367 | UNBLOCK_INPUT; |
368 | ||
369 | /* Now update the local cache */ | |
370 | { | |
371 | Lisp_Object selection_time; | |
372 | Lisp_Object selection_data; | |
373 | Lisp_Object prev_value; | |
374 | ||
375 | selection_time = long_to_cons ((unsigned long) time); | |
16041401 CY |
376 | selection_data = list4 (selection_name, selection_value, |
377 | selection_time, selected_frame); | |
ede4db72 RS |
378 | prev_value = assq_no_quit (selection_name, Vselection_alist); |
379 | ||
380 | Vselection_alist = Fcons (selection_data, Vselection_alist); | |
381 | ||
382 | /* If we already owned the selection, remove the old selection data. | |
383 | Perhaps we should destructively modify it instead. | |
384 | Don't use Fdelq as that may QUIT. */ | |
385 | if (!NILP (prev_value)) | |
386 | { | |
387 | Lisp_Object rest; /* we know it's not the CAR, so it's easy. */ | |
99784d63 | 388 | for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) |
8e713be6 | 389 | if (EQ (prev_value, Fcar (XCDR (rest)))) |
ede4db72 | 390 | { |
f3fbd155 | 391 | XSETCDR (rest, Fcdr (XCDR (rest))); |
ede4db72 RS |
392 | break; |
393 | } | |
394 | } | |
395 | } | |
396 | } | |
397 | \f | |
398 | /* Given a selection-name and desired type, look up our local copy of | |
399 | the selection value and convert it to the type. | |
400 | The value is nil or a string. | |
5109c8dd KH |
401 | This function is used both for remote requests (LOCAL_REQUEST is zero) |
402 | and for local x-get-selection-internal (LOCAL_REQUEST is nonzero). | |
ede4db72 RS |
403 | |
404 | This calls random Lisp code, and may signal or gc. */ | |
405 | ||
406 | static Lisp_Object | |
971de7fb | 407 | x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, int local_request) |
ede4db72 RS |
408 | { |
409 | Lisp_Object local_value; | |
410 | Lisp_Object handler_fn, value, type, check; | |
411 | int count; | |
412 | ||
413 | local_value = assq_no_quit (selection_symbol, Vselection_alist); | |
414 | ||
415 | if (NILP (local_value)) return Qnil; | |
416 | ||
417 | /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest. */ | |
418 | if (EQ (target_type, QTIMESTAMP)) | |
419 | { | |
420 | handler_fn = Qnil; | |
8e713be6 | 421 | value = XCAR (XCDR (XCDR (local_value))); |
ede4db72 RS |
422 | } |
423 | #if 0 | |
424 | else if (EQ (target_type, QDELETE)) | |
425 | { | |
426 | handler_fn = Qnil; | |
427 | Fx_disown_selection_internal | |
428 | (selection_symbol, | |
8e713be6 | 429 | XCAR (XCDR (XCDR (local_value)))); |
ede4db72 RS |
430 | value = QNULL; |
431 | } | |
432 | #endif | |
433 | ||
434 | #if 0 /* #### MULTIPLE doesn't work yet */ | |
435 | else if (CONSP (target_type) | |
8e713be6 | 436 | && XCAR (target_type) == QMULTIPLE) |
ede4db72 | 437 | { |
9d2d1dd8 KH |
438 | Lisp_Object pairs; |
439 | int size; | |
ede4db72 | 440 | int i; |
8e713be6 | 441 | pairs = XCDR (target_type); |
9d2d1dd8 | 442 | size = XVECTOR (pairs)->size; |
ede4db72 RS |
443 | /* If the target is MULTIPLE, then target_type looks like |
444 | (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ]) | |
445 | We modify the second element of each pair in the vector and | |
446 | return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ] | |
447 | */ | |
448 | for (i = 0; i < size; i++) | |
449 | { | |
9d2d1dd8 KH |
450 | Lisp_Object pair; |
451 | pair = XVECTOR (pairs)->contents [i]; | |
ede4db72 RS |
452 | XVECTOR (pair)->contents [1] |
453 | = x_get_local_selection (XVECTOR (pair)->contents [0], | |
5109c8dd KH |
454 | XVECTOR (pair)->contents [1], |
455 | local_request); | |
ede4db72 RS |
456 | } |
457 | return pairs; | |
458 | } | |
459 | #endif | |
460 | else | |
461 | { | |
462 | /* Don't allow a quit within the converter. | |
463 | When the user types C-g, he would be surprised | |
464 | if by luck it came during a converter. */ | |
aed13378 | 465 | count = SPECPDL_INDEX (); |
ede4db72 RS |
466 | specbind (Qinhibit_quit, Qt); |
467 | ||
b7826503 | 468 | CHECK_SYMBOL (target_type); |
ede4db72 | 469 | handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist)); |
4f06187f RS |
470 | /* gcpro is not needed here since nothing but HANDLER_FN |
471 | is live, and that ought to be a symbol. */ | |
472 | ||
1eb4d468 RS |
473 | if (!NILP (handler_fn)) |
474 | value = call3 (handler_fn, | |
5109c8dd | 475 | selection_symbol, (local_request ? Qnil : target_type), |
8e713be6 | 476 | XCAR (XCDR (local_value))); |
1eb4d468 RS |
477 | else |
478 | value = Qnil; | |
ede4db72 RS |
479 | unbind_to (count, Qnil); |
480 | } | |
481 | ||
482 | /* Make sure this value is of a type that we could transmit | |
483 | to another X client. */ | |
a87ed99c | 484 | |
ede4db72 RS |
485 | check = value; |
486 | if (CONSP (value) | |
8e713be6 KR |
487 | && SYMBOLP (XCAR (value))) |
488 | type = XCAR (value), | |
489 | check = XCDR (value); | |
1b65481e | 490 | |
ede4db72 RS |
491 | if (STRINGP (check) |
492 | || VECTORP (check) | |
493 | || SYMBOLP (check) | |
7da64e5c | 494 | || INTEGERP (check) |
ede4db72 RS |
495 | || NILP (value)) |
496 | return value; | |
a87ed99c | 497 | /* Check for a value that cons_to_long could handle. */ |
ede4db72 | 498 | else if (CONSP (check) |
8e713be6 KR |
499 | && INTEGERP (XCAR (check)) |
500 | && (INTEGERP (XCDR (check)) | |
ede4db72 | 501 | || |
8e713be6 KR |
502 | (CONSP (XCDR (check)) |
503 | && INTEGERP (XCAR (XCDR (check))) | |
504 | && NILP (XCDR (XCDR (check)))))) | |
ede4db72 | 505 | return value; |
4d30ce50 KS |
506 | |
507 | signal_error ("Invalid data returned by selection-conversion function", | |
508 | list2 (handler_fn, value)); | |
ede4db72 RS |
509 | } |
510 | \f | |
511 | /* Subroutines of x_reply_selection_request. */ | |
512 | ||
5d0ba25b | 513 | /* Send a SelectionNotify event to the requestor with property=None, |
ede4db72 RS |
514 | meaning we were unable to do what they wanted. */ |
515 | ||
516 | static void | |
971de7fb | 517 | x_decline_selection_request (struct input_event *event) |
ede4db72 RS |
518 | { |
519 | XSelectionEvent reply; | |
1b65481e | 520 | |
ede4db72 RS |
521 | reply.type = SelectionNotify; |
522 | reply.display = SELECTION_EVENT_DISPLAY (event); | |
5d0ba25b | 523 | reply.requestor = SELECTION_EVENT_REQUESTOR (event); |
ede4db72 RS |
524 | reply.selection = SELECTION_EVENT_SELECTION (event); |
525 | reply.time = SELECTION_EVENT_TIME (event); | |
526 | reply.target = SELECTION_EVENT_TARGET (event); | |
527 | reply.property = None; | |
528 | ||
d9c0d4a3 GM |
529 | /* The reason for the error may be that the receiver has |
530 | died in the meantime. Handle that case. */ | |
ede4db72 | 531 | BLOCK_INPUT; |
9ba8e10d | 532 | x_catch_errors (reply.display); |
d9c0d4a3 | 533 | XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply); |
5c3a351a | 534 | XFlush (reply.display); |
4545fa20 | 535 | x_uncatch_errors (); |
ede4db72 RS |
536 | UNBLOCK_INPUT; |
537 | } | |
538 | ||
539 | /* This is the selection request currently being processed. | |
540 | It is set to zero when the request is fully processed. */ | |
541 | static struct input_event *x_selection_current_request; | |
542 | ||
ca29f2b8 GM |
543 | /* Display info in x_selection_request. */ |
544 | ||
545 | static struct x_display_info *selection_request_dpyinfo; | |
546 | ||
ede4db72 | 547 | /* Used as an unwind-protect clause so that, if a selection-converter signals |
2a1a4c9d | 548 | an error, we tell the requester that we were unable to do what they wanted |
ede4db72 RS |
549 | before we throw to top-level or go into the debugger or whatever. */ |
550 | ||
551 | static Lisp_Object | |
971de7fb | 552 | x_selection_request_lisp_error (Lisp_Object ignore) |
ede4db72 | 553 | { |
ca29f2b8 GM |
554 | if (x_selection_current_request != 0 |
555 | && selection_request_dpyinfo->display) | |
ede4db72 RS |
556 | x_decline_selection_request (x_selection_current_request); |
557 | return Qnil; | |
558 | } | |
c525d842 CY |
559 | |
560 | static Lisp_Object | |
971de7fb | 561 | x_catch_errors_unwind (Lisp_Object dummy) |
c525d842 CY |
562 | { |
563 | BLOCK_INPUT; | |
564 | x_uncatch_errors (); | |
565 | UNBLOCK_INPUT; | |
6f10509c | 566 | return Qnil; |
c525d842 | 567 | } |
ede4db72 | 568 | \f |
d1f21a66 RS |
569 | |
570 | /* This stuff is so that INCR selections are reentrant (that is, so we can | |
571 | be servicing multiple INCR selection requests simultaneously.) I haven't | |
572 | actually tested that yet. */ | |
573 | ||
574 | /* Keep a list of the property changes that are awaited. */ | |
575 | ||
576 | struct prop_location | |
577 | { | |
578 | int identifier; | |
579 | Display *display; | |
580 | Window window; | |
581 | Atom property; | |
582 | int desired_state; | |
583 | int arrived; | |
584 | struct prop_location *next; | |
585 | }; | |
586 | ||
971de7fb DN |
587 | static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state); |
588 | static void wait_for_property_change (struct prop_location *location); | |
589 | static void unexpect_property_change (struct prop_location *location); | |
590 | static int waiting_for_other_props_on_window (Display *display, Window window); | |
d1f21a66 RS |
591 | |
592 | static int prop_location_identifier; | |
593 | ||
594 | static Lisp_Object property_change_reply; | |
595 | ||
596 | static struct prop_location *property_change_reply_object; | |
597 | ||
598 | static struct prop_location *property_change_wait_list; | |
55b2d45d RS |
599 | |
600 | static Lisp_Object | |
971de7fb | 601 | queue_selection_requests_unwind (Lisp_Object tem) |
55b2d45d | 602 | { |
dd0fe424 | 603 | x_stop_queuing_selection_requests (); |
ab552306 | 604 | return Qnil; |
55b2d45d RS |
605 | } |
606 | ||
607 | /* Return some frame whose display info is DPYINFO. | |
608 | Return nil if there is none. */ | |
609 | ||
610 | static Lisp_Object | |
971de7fb | 611 | some_frame_on_display (struct x_display_info *dpyinfo) |
55b2d45d RS |
612 | { |
613 | Lisp_Object list, frame; | |
614 | ||
615 | FOR_EACH_FRAME (list, frame) | |
616 | { | |
428a555e KL |
617 | if (FRAME_X_P (XFRAME (frame)) |
618 | && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo) | |
55b2d45d RS |
619 | return frame; |
620 | } | |
621 | ||
622 | return Qnil; | |
623 | } | |
d1f21a66 | 624 | \f |
ede4db72 RS |
625 | /* Send the reply to a selection request event EVENT. |
626 | TYPE is the type of selection data requested. | |
627 | DATA and SIZE describe the data to send, already converted. | |
628 | FORMAT is the unit-size (in bits) of the data to be transmitted. */ | |
629 | ||
46d47e41 | 630 | #ifdef TRACE_SELECTION |
ca7af97a | 631 | static int x_reply_selection_request_cnt; |
46d47e41 GM |
632 | #endif /* TRACE_SELECTION */ |
633 | ||
ede4db72 | 634 | static void |
971de7fb | 635 | x_reply_selection_request (struct input_event *event, int format, unsigned char *data, int size, Atom type) |
ede4db72 RS |
636 | { |
637 | XSelectionEvent reply; | |
638 | Display *display = SELECTION_EVENT_DISPLAY (event); | |
5d0ba25b | 639 | Window window = SELECTION_EVENT_REQUESTOR (event); |
ede4db72 RS |
640 | int bytes_remaining; |
641 | int format_bytes = format/8; | |
642 | int max_bytes = SELECTION_QUANTUM (display); | |
5c3a351a | 643 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
9ba8e10d | 644 | int count = SPECPDL_INDEX (); |
ede4db72 RS |
645 | |
646 | if (max_bytes > MAX_SELECTION_QUANTUM) | |
647 | max_bytes = MAX_SELECTION_QUANTUM; | |
648 | ||
649 | reply.type = SelectionNotify; | |
650 | reply.display = display; | |
5d0ba25b | 651 | reply.requestor = window; |
ede4db72 RS |
652 | reply.selection = SELECTION_EVENT_SELECTION (event); |
653 | reply.time = SELECTION_EVENT_TIME (event); | |
654 | reply.target = SELECTION_EVENT_TARGET (event); | |
655 | reply.property = SELECTION_EVENT_PROPERTY (event); | |
656 | if (reply.property == None) | |
657 | reply.property = reply.target; | |
658 | ||
afe1529d | 659 | BLOCK_INPUT; |
c525d842 CY |
660 | /* The protected block contains wait_for_property_change, which can |
661 | run random lisp code (process handlers) or signal. Therefore, we | |
662 | put the x_uncatch_errors call in an unwind. */ | |
663 | record_unwind_protect (x_catch_errors_unwind, Qnil); | |
9ba8e10d | 664 | x_catch_errors (display); |
ede4db72 | 665 | |
9c3ad9e1 JD |
666 | #ifdef TRACE_SELECTION |
667 | { | |
9c3ad9e1 JD |
668 | char *sel = XGetAtomName (display, reply.selection); |
669 | char *tgt = XGetAtomName (display, reply.target); | |
ca7af97a | 670 | TRACE3 ("%s, target %s (%d)", sel, tgt, ++x_reply_selection_request_cnt); |
9c3ad9e1 JD |
671 | if (sel) XFree (sel); |
672 | if (tgt) XFree (tgt); | |
673 | } | |
674 | #endif /* TRACE_SELECTION */ | |
675 | ||
ede4db72 RS |
676 | /* Store the data on the requested property. |
677 | If the selection is large, only store the first N bytes of it. | |
678 | */ | |
679 | bytes_remaining = size * format_bytes; | |
680 | if (bytes_remaining <= max_bytes) | |
681 | { | |
682 | /* Send all the data at once, with minimal handshaking. */ | |
d9c0d4a3 | 683 | TRACE1 ("Sending all %d bytes", bytes_remaining); |
ede4db72 RS |
684 | XChangeProperty (display, window, reply.property, type, format, |
685 | PropModeReplace, data, size); | |
686 | /* At this point, the selection was successfully stored; ack it. */ | |
2f65feb6 | 687 | XSendEvent (display, window, False, 0L, (XEvent *) &reply); |
ede4db72 RS |
688 | } |
689 | else | |
690 | { | |
691 | /* Send an INCR selection. */ | |
d1f21a66 | 692 | struct prop_location *wait_object; |
afe1529d | 693 | int had_errors; |
55b2d45d | 694 | Lisp_Object frame; |
ede4db72 | 695 | |
55b2d45d RS |
696 | frame = some_frame_on_display (dpyinfo); |
697 | ||
698 | /* If the display no longer has frames, we can't expect | |
699 | to get many more selection requests from it, so don't | |
700 | bother trying to queue them. */ | |
701 | if (!NILP (frame)) | |
702 | { | |
dd0fe424 | 703 | x_start_queuing_selection_requests (); |
55b2d45d RS |
704 | |
705 | record_unwind_protect (queue_selection_requests_unwind, | |
dd0fe424 | 706 | Qnil); |
55b2d45d | 707 | } |
2f65feb6 | 708 | |
19126e11 | 709 | if (x_window_to_frame (dpyinfo, window)) /* #### debug */ |
606140dd | 710 | error ("Attempt to transfer an INCR to ourself!"); |
1b65481e | 711 | |
d9c0d4a3 GM |
712 | TRACE2 ("Start sending %d bytes incrementally (%s)", |
713 | bytes_remaining, XGetAtomName (display, reply.property)); | |
d1f21a66 RS |
714 | wait_object = expect_property_change (display, window, reply.property, |
715 | PropertyDelete); | |
ede4db72 | 716 | |
d9c0d4a3 GM |
717 | TRACE1 ("Set %s to number of bytes to send", |
718 | XGetAtomName (display, reply.property)); | |
e22cf39c JD |
719 | { |
720 | /* XChangeProperty expects an array of long even if long is more than | |
721 | 32 bits. */ | |
722 | long value[1]; | |
723 | ||
724 | value[0] = bytes_remaining; | |
725 | XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR, | |
726 | 32, PropModeReplace, | |
727 | (unsigned char *) value, 1); | |
728 | } | |
729 | ||
ede4db72 | 730 | XSelectInput (display, window, PropertyChangeMask); |
1b65481e | 731 | |
ede4db72 | 732 | /* Tell 'em the INCR data is there... */ |
d9c0d4a3 | 733 | TRACE0 ("Send SelectionNotify event"); |
55b2d45d | 734 | XSendEvent (display, window, False, 0L, (XEvent *) &reply); |
5c3a351a | 735 | XFlush (display); |
afe1529d RS |
736 | |
737 | had_errors = x_had_errors_p (display); | |
2f65feb6 | 738 | UNBLOCK_INPUT; |
ede4db72 | 739 | |
2a1a4c9d | 740 | /* First, wait for the requester to ack by deleting the property. |
ede4db72 | 741 | This can run random lisp code (process handlers) or signal. */ |
afe1529d | 742 | if (! had_errors) |
d9c0d4a3 GM |
743 | { |
744 | TRACE1 ("Waiting for ACK (deletion of %s)", | |
745 | XGetAtomName (display, reply.property)); | |
746 | wait_for_property_change (wait_object); | |
747 | } | |
dd0fe424 KS |
748 | else |
749 | unexpect_property_change (wait_object); | |
ede4db72 | 750 | |
d9c0d4a3 | 751 | TRACE0 ("Got ACK"); |
ede4db72 RS |
752 | while (bytes_remaining) |
753 | { | |
e22cf39c JD |
754 | int i = ((bytes_remaining < max_bytes) |
755 | ? bytes_remaining | |
5a9c35e5 | 756 | : max_bytes) / format_bytes; |
2f65feb6 RS |
757 | |
758 | BLOCK_INPUT; | |
759 | ||
d1f21a66 RS |
760 | wait_object |
761 | = expect_property_change (display, window, reply.property, | |
762 | PropertyDelete); | |
d9c0d4a3 | 763 | |
5a9c35e5 | 764 | TRACE1 ("Sending increment of %d elements", i); |
d9c0d4a3 GM |
765 | TRACE1 ("Set %s to increment data", |
766 | XGetAtomName (display, reply.property)); | |
1b65481e | 767 | |
ede4db72 RS |
768 | /* Append the next chunk of data to the property. */ |
769 | XChangeProperty (display, window, reply.property, type, format, | |
5a9c35e5 AS |
770 | PropModeAppend, data, i); |
771 | bytes_remaining -= i * format_bytes; | |
772 | if (format == 32) | |
773 | data += i * sizeof (long); | |
774 | else | |
775 | data += i * format_bytes; | |
5c3a351a | 776 | XFlush (display); |
afe1529d | 777 | had_errors = x_had_errors_p (display); |
2f65feb6 | 778 | UNBLOCK_INPUT; |
ede4db72 | 779 | |
afe1529d RS |
780 | if (had_errors) |
781 | break; | |
782 | ||
2a1a4c9d | 783 | /* Now wait for the requester to ack this chunk by deleting the |
db9cd97a | 784 | property. This can run random lisp code or signal. */ |
d9c0d4a3 GM |
785 | TRACE1 ("Waiting for increment ACK (deletion of %s)", |
786 | XGetAtomName (display, reply.property)); | |
d1f21a66 | 787 | wait_for_property_change (wait_object); |
ede4db72 | 788 | } |
1b65481e | 789 | |
d9c0d4a3 GM |
790 | /* Now write a zero-length chunk to the property to tell the |
791 | requester that we're done. */ | |
2f65feb6 | 792 | BLOCK_INPUT; |
ede4db72 RS |
793 | if (! waiting_for_other_props_on_window (display, window)) |
794 | XSelectInput (display, window, 0L); | |
795 | ||
d9c0d4a3 GM |
796 | TRACE1 ("Set %s to a 0-length chunk to indicate EOF", |
797 | XGetAtomName (display, reply.property)); | |
ede4db72 RS |
798 | XChangeProperty (display, window, reply.property, type, format, |
799 | PropModeReplace, data, 0); | |
d9c0d4a3 | 800 | TRACE0 ("Done sending incrementally"); |
ede4db72 | 801 | } |
afe1529d | 802 | |
ceabd272 | 803 | /* rms, 2003-01-03: I think I have fixed this bug. */ |
47a6ac17 GM |
804 | /* The window we're communicating with may have been deleted |
805 | in the meantime (that's a real situation from a bug report). | |
806 | In this case, there may be events in the event queue still | |
807 | refering to the deleted window, and we'll get a BadWindow error | |
808 | in XTread_socket when processing the events. I don't have | |
809 | an idea how to fix that. gerd, 2001-01-98. */ | |
844fc085 JD |
810 | /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are |
811 | delivered before uncatch errors. */ | |
812 | XSync (display, False); | |
afe1529d | 813 | UNBLOCK_INPUT; |
0608b1a0 JD |
814 | |
815 | /* GTK queues events in addition to the queue in Xlib. So we | |
816 | UNBLOCK to enter the event loop and get possible errors delivered, | |
817 | and then BLOCK again because x_uncatch_errors requires it. */ | |
818 | BLOCK_INPUT; | |
c525d842 | 819 | /* This calls x_uncatch_errors. */ |
9ba8e10d | 820 | unbind_to (count, Qnil); |
afe1529d | 821 | UNBLOCK_INPUT; |
ede4db72 RS |
822 | } |
823 | \f | |
824 | /* Handle a SelectionRequest event EVENT. | |
825 | This is called from keyboard.c when such an event is found in the queue. */ | |
826 | ||
dd0fe424 | 827 | static void |
971de7fb | 828 | x_handle_selection_request (struct input_event *event) |
ede4db72 RS |
829 | { |
830 | struct gcpro gcpro1, gcpro2, gcpro3; | |
9d2d1dd8 | 831 | Lisp_Object local_selection_data; |
ede4db72 | 832 | Lisp_Object selection_symbol; |
9d2d1dd8 KH |
833 | Lisp_Object target_symbol; |
834 | Lisp_Object converted_selection; | |
ede4db72 | 835 | Time local_selection_time; |
9d2d1dd8 | 836 | Lisp_Object successful_p; |
ede4db72 | 837 | int count; |
5c3a351a RS |
838 | struct x_display_info *dpyinfo |
839 | = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event)); | |
ede4db72 | 840 | |
678b3958 KS |
841 | TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu", |
842 | (unsigned long) SELECTION_EVENT_REQUESTOR (event), | |
843 | (unsigned long) SELECTION_EVENT_TIME (event)); | |
dd0fe424 | 844 | |
9d2d1dd8 KH |
845 | local_selection_data = Qnil; |
846 | target_symbol = Qnil; | |
847 | converted_selection = Qnil; | |
848 | successful_p = Qnil; | |
849 | ||
ede4db72 RS |
850 | GCPRO3 (local_selection_data, converted_selection, target_symbol); |
851 | ||
d9c0d4a3 | 852 | selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event), |
ede4db72 RS |
853 | SELECTION_EVENT_SELECTION (event)); |
854 | ||
855 | local_selection_data = assq_no_quit (selection_symbol, Vselection_alist); | |
ede4db72 RS |
856 | |
857 | if (NILP (local_selection_data)) | |
858 | { | |
859 | /* Someone asked for the selection, but we don't have it any more. | |
860 | */ | |
861 | x_decline_selection_request (event); | |
862 | goto DONE; | |
863 | } | |
864 | ||
865 | local_selection_time = (Time) | |
8e713be6 | 866 | cons_to_long (XCAR (XCDR (XCDR (local_selection_data)))); |
ede4db72 RS |
867 | |
868 | if (SELECTION_EVENT_TIME (event) != CurrentTime | |
7da64e5c | 869 | && local_selection_time > SELECTION_EVENT_TIME (event)) |
ede4db72 RS |
870 | { |
871 | /* Someone asked for the selection, and we have one, but not the one | |
872 | they're looking for. | |
873 | */ | |
874 | x_decline_selection_request (event); | |
875 | goto DONE; | |
876 | } | |
877 | ||
ede4db72 | 878 | x_selection_current_request = event; |
331379bf | 879 | count = SPECPDL_INDEX (); |
ca29f2b8 | 880 | selection_request_dpyinfo = dpyinfo; |
ede4db72 RS |
881 | record_unwind_protect (x_selection_request_lisp_error, Qnil); |
882 | ||
d9c0d4a3 | 883 | target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event), |
ede4db72 RS |
884 | SELECTION_EVENT_TARGET (event)); |
885 | ||
886 | #if 0 /* #### MULTIPLE doesn't work yet */ | |
887 | if (EQ (target_symbol, QMULTIPLE)) | |
888 | target_symbol = fetch_multiple_target (event); | |
889 | #endif | |
1b65481e | 890 | |
ede4db72 | 891 | /* Convert lisp objects back into binary data */ |
1b65481e | 892 | |
ede4db72 | 893 | converted_selection |
5109c8dd | 894 | = x_get_local_selection (selection_symbol, target_symbol, 0); |
1b65481e | 895 | |
ede4db72 RS |
896 | if (! NILP (converted_selection)) |
897 | { | |
898 | unsigned char *data; | |
899 | unsigned int size; | |
900 | int format; | |
901 | Atom type; | |
aca39f42 RS |
902 | int nofree; |
903 | ||
866f8518 JD |
904 | if (CONSP (converted_selection) && NILP (XCDR (converted_selection))) |
905 | { | |
906 | x_decline_selection_request (event); | |
907 | goto DONE2; | |
908 | } | |
909 | ||
2f65feb6 RS |
910 | lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event), |
911 | converted_selection, | |
aca39f42 | 912 | &data, &type, &size, &format, &nofree); |
1b65481e | 913 | |
ede4db72 RS |
914 | x_reply_selection_request (event, format, data, size, type); |
915 | successful_p = Qt; | |
916 | ||
917 | /* Indicate we have successfully processed this event. */ | |
7da64e5c | 918 | x_selection_current_request = 0; |
ede4db72 | 919 | |
4feb31b2 | 920 | /* Use xfree, not XFree, because lisp_data_to_selection_data |
0158abbc | 921 | calls xmalloc itself. */ |
aca39f42 | 922 | if (!nofree) |
4feb31b2 | 923 | xfree (data); |
ede4db72 | 924 | } |
866f8518 JD |
925 | |
926 | DONE2: | |
ede4db72 RS |
927 | unbind_to (count, Qnil); |
928 | ||
929 | DONE: | |
930 | ||
ede4db72 RS |
931 | /* Let random lisp code notice that the selection has been asked for. */ |
932 | { | |
9d2d1dd8 | 933 | Lisp_Object rest; |
c917a8de | 934 | rest = Vx_sent_selection_functions; |
ede4db72 RS |
935 | if (!EQ (rest, Qunbound)) |
936 | for (; CONSP (rest); rest = Fcdr (rest)) | |
937 | call3 (Fcar (rest), selection_symbol, target_symbol, successful_p); | |
938 | } | |
4f06187f RS |
939 | |
940 | UNGCPRO; | |
ede4db72 RS |
941 | } |
942 | \f | |
e18e6130 | 943 | /* Handle a SelectionClear event EVENT, which indicates that some |
ede4db72 RS |
944 | client cleared out our previously asserted selection. |
945 | This is called from keyboard.c when such an event is found in the queue. */ | |
946 | ||
dd0fe424 | 947 | static void |
971de7fb | 948 | x_handle_selection_clear (struct input_event *event) |
ede4db72 RS |
949 | { |
950 | Display *display = SELECTION_EVENT_DISPLAY (event); | |
951 | Atom selection = SELECTION_EVENT_SELECTION (event); | |
952 | Time changed_owner_time = SELECTION_EVENT_TIME (event); | |
1b65481e | 953 | |
ede4db72 RS |
954 | Lisp_Object selection_symbol, local_selection_data; |
955 | Time local_selection_time; | |
5c3a351a | 956 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
e18e6130 RS |
957 | struct x_display_info *t_dpyinfo; |
958 | ||
dd0fe424 KS |
959 | TRACE0 ("x_handle_selection_clear"); |
960 | ||
e18e6130 RS |
961 | /* If the new selection owner is also Emacs, |
962 | don't clear the new selection. */ | |
963 | BLOCK_INPUT; | |
964 | /* Check each display on the same terminal, | |
965 | to see if this Emacs job now owns the selection | |
966 | through that display. */ | |
967 | for (t_dpyinfo = x_display_list; t_dpyinfo; t_dpyinfo = t_dpyinfo->next) | |
6ed8eeff | 968 | if (t_dpyinfo->terminal->kboard == dpyinfo->terminal->kboard) |
e18e6130 RS |
969 | { |
970 | Window owner_window | |
971 | = XGetSelectionOwner (t_dpyinfo->display, selection); | |
972 | if (x_window_to_frame (t_dpyinfo, owner_window) != 0) | |
973 | { | |
974 | UNBLOCK_INPUT; | |
975 | return; | |
976 | } | |
977 | } | |
978 | UNBLOCK_INPUT; | |
16041401 | 979 | |
d9c0d4a3 | 980 | selection_symbol = x_atom_to_symbol (display, selection); |
ede4db72 RS |
981 | |
982 | local_selection_data = assq_no_quit (selection_symbol, Vselection_alist); | |
983 | ||
984 | /* Well, we already believe that we don't own it, so that's just fine. */ | |
985 | if (NILP (local_selection_data)) return; | |
986 | ||
987 | local_selection_time = (Time) | |
8e713be6 | 988 | cons_to_long (XCAR (XCDR (XCDR (local_selection_data)))); |
ede4db72 RS |
989 | |
990 | /* This SelectionClear is for a selection that we no longer own, so we can | |
991 | disregard it. (That is, we have reasserted the selection since this | |
992 | request was generated.) */ | |
993 | ||
994 | if (changed_owner_time != CurrentTime | |
995 | && local_selection_time > changed_owner_time) | |
996 | return; | |
997 | ||
998 | /* Otherwise, we're really honest and truly being told to drop it. | |
999 | Don't use Fdelq as that may QUIT;. */ | |
1000 | ||
1001 | if (EQ (local_selection_data, Fcar (Vselection_alist))) | |
1002 | Vselection_alist = Fcdr (Vselection_alist); | |
1003 | else | |
1004 | { | |
1005 | Lisp_Object rest; | |
99784d63 | 1006 | for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) |
8e713be6 | 1007 | if (EQ (local_selection_data, Fcar (XCDR (rest)))) |
ede4db72 | 1008 | { |
f3fbd155 | 1009 | XSETCDR (rest, Fcdr (XCDR (rest))); |
ede4db72 RS |
1010 | break; |
1011 | } | |
1012 | } | |
1013 | ||
1014 | /* Let random lisp code notice that the selection has been stolen. */ | |
1015 | ||
1016 | { | |
d1f21a66 | 1017 | Lisp_Object rest; |
c917a8de | 1018 | rest = Vx_lost_selection_functions; |
ede4db72 | 1019 | if (!EQ (rest, Qunbound)) |
d1f21a66 RS |
1020 | { |
1021 | for (; CONSP (rest); rest = Fcdr (rest)) | |
1022 | call1 (Fcar (rest), selection_symbol); | |
7c6b2ea4 | 1023 | prepare_menu_bars (); |
3007ebfb | 1024 | redisplay_preserve_echo_area (20); |
d1f21a66 | 1025 | } |
ede4db72 RS |
1026 | } |
1027 | } | |
1028 | ||
dd0fe424 | 1029 | void |
971de7fb | 1030 | x_handle_selection_event (struct input_event *event) |
dd0fe424 KS |
1031 | { |
1032 | TRACE0 ("x_handle_selection_event"); | |
1033 | ||
1034 | if (event->kind == SELECTION_REQUEST_EVENT) | |
1035 | { | |
1036 | if (x_queue_selection_requests) | |
1037 | x_queue_event (event); | |
1038 | else | |
1039 | x_handle_selection_request (event); | |
1040 | } | |
1041 | else | |
1042 | x_handle_selection_clear (event); | |
1043 | } | |
1044 | ||
1045 | ||
118bd841 RS |
1046 | /* Clear all selections that were made from frame F. |
1047 | We do this when about to delete a frame. */ | |
1048 | ||
1049 | void | |
971de7fb | 1050 | x_clear_frame_selections (FRAME_PTR f) |
118bd841 RS |
1051 | { |
1052 | Lisp_Object frame; | |
1053 | Lisp_Object rest; | |
1054 | ||
90851bbe | 1055 | XSETFRAME (frame, f); |
118bd841 RS |
1056 | |
1057 | /* Otherwise, we're really honest and truly being told to drop it. | |
1058 | Don't use Fdelq as that may QUIT;. */ | |
1059 | ||
0d199f9c RS |
1060 | /* Delete elements from the beginning of Vselection_alist. */ |
1061 | while (!NILP (Vselection_alist) | |
1062 | && EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (Vselection_alist))))))) | |
1063 | { | |
1064 | /* Let random Lisp code notice that the selection has been stolen. */ | |
1065 | Lisp_Object hooks, selection_symbol; | |
1066 | ||
c917a8de | 1067 | hooks = Vx_lost_selection_functions; |
0d199f9c RS |
1068 | selection_symbol = Fcar (Fcar (Vselection_alist)); |
1069 | ||
1070 | if (!EQ (hooks, Qunbound)) | |
1071 | { | |
1072 | for (; CONSP (hooks); hooks = Fcdr (hooks)) | |
1073 | call1 (Fcar (hooks), selection_symbol); | |
996b804d MB |
1074 | #if 0 /* This can crash when deleting a frame |
1075 | from x_connection_closed. Anyway, it seems unnecessary; | |
1076 | something else should cause a redisplay. */ | |
3007ebfb | 1077 | redisplay_preserve_echo_area (21); |
996b804d | 1078 | #endif |
0d199f9c RS |
1079 | } |
1080 | ||
1081 | Vselection_alist = Fcdr (Vselection_alist); | |
1082 | } | |
1083 | ||
1084 | /* Delete elements after the beginning of Vselection_alist. */ | |
99784d63 | 1085 | for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) |
8e713be6 | 1086 | if (EQ (frame, Fcar (Fcdr (Fcdr (Fcdr (Fcar (XCDR (rest)))))))) |
118bd841 RS |
1087 | { |
1088 | /* Let random Lisp code notice that the selection has been stolen. */ | |
1089 | Lisp_Object hooks, selection_symbol; | |
1090 | ||
c917a8de | 1091 | hooks = Vx_lost_selection_functions; |
8e713be6 | 1092 | selection_symbol = Fcar (Fcar (XCDR (rest))); |
118bd841 RS |
1093 | |
1094 | if (!EQ (hooks, Qunbound)) | |
1095 | { | |
1096 | for (; CONSP (hooks); hooks = Fcdr (hooks)) | |
1097 | call1 (Fcar (hooks), selection_symbol); | |
996b804d | 1098 | #if 0 /* See above */ |
3007ebfb | 1099 | redisplay_preserve_echo_area (22); |
996b804d | 1100 | #endif |
118bd841 | 1101 | } |
f3fbd155 | 1102 | XSETCDR (rest, Fcdr (XCDR (rest))); |
118bd841 RS |
1103 | break; |
1104 | } | |
1105 | } | |
ede4db72 | 1106 | \f |
ede4db72 RS |
1107 | /* Nonzero if any properties for DISPLAY and WINDOW |
1108 | are on the list of what we are waiting for. */ | |
1109 | ||
1110 | static int | |
971de7fb | 1111 | waiting_for_other_props_on_window (Display *display, Window window) |
ede4db72 RS |
1112 | { |
1113 | struct prop_location *rest = property_change_wait_list; | |
1114 | while (rest) | |
1115 | if (rest->display == display && rest->window == window) | |
1116 | return 1; | |
1117 | else | |
1118 | rest = rest->next; | |
1119 | return 0; | |
1120 | } | |
1121 | ||
1122 | /* Add an entry to the list of property changes we are waiting for. | |
1123 | DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for. | |
1124 | The return value is a number that uniquely identifies | |
1125 | this awaited property change. */ | |
1126 | ||
d1f21a66 | 1127 | static struct prop_location * |
971de7fb | 1128 | expect_property_change (Display *display, Window window, Atom property, int state) |
ede4db72 | 1129 | { |
d9c0d4a3 | 1130 | struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl); |
2f65feb6 | 1131 | pl->identifier = ++prop_location_identifier; |
ede4db72 RS |
1132 | pl->display = display; |
1133 | pl->window = window; | |
1134 | pl->property = property; | |
1135 | pl->desired_state = state; | |
1136 | pl->next = property_change_wait_list; | |
d1f21a66 | 1137 | pl->arrived = 0; |
ede4db72 | 1138 | property_change_wait_list = pl; |
d1f21a66 | 1139 | return pl; |
ede4db72 RS |
1140 | } |
1141 | ||
1142 | /* Delete an entry from the list of property changes we are waiting for. | |
2f65feb6 | 1143 | IDENTIFIER is the number that uniquely identifies the entry. */ |
ede4db72 RS |
1144 | |
1145 | static void | |
971de7fb | 1146 | unexpect_property_change (struct prop_location *location) |
ede4db72 RS |
1147 | { |
1148 | struct prop_location *prev = 0, *rest = property_change_wait_list; | |
1149 | while (rest) | |
1150 | { | |
d1f21a66 | 1151 | if (rest == location) |
ede4db72 RS |
1152 | { |
1153 | if (prev) | |
1154 | prev->next = rest->next; | |
1155 | else | |
1156 | property_change_wait_list = rest->next; | |
4feb31b2 | 1157 | xfree (rest); |
ede4db72 RS |
1158 | return; |
1159 | } | |
1160 | prev = rest; | |
1161 | rest = rest->next; | |
1162 | } | |
1163 | } | |
1164 | ||
2f65feb6 RS |
1165 | /* Remove the property change expectation element for IDENTIFIER. */ |
1166 | ||
1167 | static Lisp_Object | |
971de7fb | 1168 | wait_for_property_change_unwind (Lisp_Object loc) |
2f65feb6 | 1169 | { |
dd0fe424 KS |
1170 | struct prop_location *location = XSAVE_VALUE (loc)->pointer; |
1171 | ||
1172 | unexpect_property_change (location); | |
1173 | if (location == property_change_reply_object) | |
1174 | property_change_reply_object = 0; | |
ab552306 | 1175 | return Qnil; |
2f65feb6 RS |
1176 | } |
1177 | ||
ede4db72 | 1178 | /* Actually wait for a property change. |
2f65feb6 | 1179 | IDENTIFIER should be the value that expect_property_change returned. */ |
ede4db72 RS |
1180 | |
1181 | static void | |
971de7fb | 1182 | wait_for_property_change (struct prop_location *location) |
ede4db72 | 1183 | { |
2f65feb6 | 1184 | int secs, usecs; |
aed13378 | 1185 | int count = SPECPDL_INDEX (); |
d1f21a66 | 1186 | |
dd0fe424 KS |
1187 | if (property_change_reply_object) |
1188 | abort (); | |
2f65feb6 RS |
1189 | |
1190 | /* Make sure to do unexpect_property_change if we quit or err. */ | |
dd0fe424 KS |
1191 | record_unwind_protect (wait_for_property_change_unwind, |
1192 | make_save_value (location, 0)); | |
2f65feb6 | 1193 | |
f3fbd155 | 1194 | XSETCAR (property_change_reply, Qnil); |
afe1529d | 1195 | property_change_reply_object = location; |
dd0fe424 | 1196 | |
afe1529d RS |
1197 | /* If the event we are waiting for arrives beyond here, it will set |
1198 | property_change_reply, because property_change_reply_object says so. */ | |
d1f21a66 RS |
1199 | if (! location->arrived) |
1200 | { | |
d1f21a66 RS |
1201 | secs = x_selection_timeout / 1000; |
1202 | usecs = (x_selection_timeout % 1000) * 1000; | |
d9c0d4a3 | 1203 | TRACE2 (" Waiting %d secs, %d usecs", secs, usecs); |
d64b707c KS |
1204 | wait_reading_process_output (secs, usecs, 0, 0, |
1205 | property_change_reply, NULL, 0); | |
d1f21a66 | 1206 | |
8e713be6 | 1207 | if (NILP (XCAR (property_change_reply))) |
d9c0d4a3 GM |
1208 | { |
1209 | TRACE0 (" Timed out"); | |
1210 | error ("Timed out waiting for property-notify event"); | |
1211 | } | |
d1f21a66 | 1212 | } |
2f65feb6 RS |
1213 | |
1214 | unbind_to (count, Qnil); | |
ede4db72 RS |
1215 | } |
1216 | ||
1217 | /* Called from XTread_socket in response to a PropertyNotify event. */ | |
1218 | ||
1219 | void | |
971de7fb | 1220 | x_handle_property_notify (XPropertyEvent *event) |
ede4db72 RS |
1221 | { |
1222 | struct prop_location *prev = 0, *rest = property_change_wait_list; | |
d9c0d4a3 | 1223 | |
ede4db72 RS |
1224 | while (rest) |
1225 | { | |
dd0fe424 KS |
1226 | if (!rest->arrived |
1227 | && rest->property == event->atom | |
ede4db72 RS |
1228 | && rest->window == event->window |
1229 | && rest->display == event->display | |
1230 | && rest->desired_state == event->state) | |
1231 | { | |
d9c0d4a3 GM |
1232 | TRACE2 ("Expected %s of property %s", |
1233 | (event->state == PropertyDelete ? "deletion" : "change"), | |
1234 | XGetAtomName (event->display, event->atom)); | |
ede4db72 | 1235 | |
d1f21a66 RS |
1236 | rest->arrived = 1; |
1237 | ||
ede4db72 RS |
1238 | /* If this is the one wait_for_property_change is waiting for, |
1239 | tell it to wake up. */ | |
d1f21a66 | 1240 | if (rest == property_change_reply_object) |
f3fbd155 | 1241 | XSETCAR (property_change_reply, Qt); |
ede4db72 | 1242 | |
ede4db72 RS |
1243 | return; |
1244 | } | |
1b65481e | 1245 | |
ede4db72 RS |
1246 | prev = rest; |
1247 | rest = rest->next; | |
1248 | } | |
ede4db72 RS |
1249 | } |
1250 | ||
1251 | ||
1252 | \f | |
1253 | #if 0 /* #### MULTIPLE doesn't work yet */ | |
1254 | ||
1255 | static Lisp_Object | |
1256 | fetch_multiple_target (event) | |
1257 | XSelectionRequestEvent *event; | |
1258 | { | |
1259 | Display *display = event->display; | |
5d0ba25b | 1260 | Window window = event->requestor; |
ede4db72 RS |
1261 | Atom target = event->target; |
1262 | Atom selection_atom = event->selection; | |
1263 | int result; | |
1264 | ||
1265 | return | |
1266 | Fcons (QMULTIPLE, | |
1267 | x_get_window_property_as_lisp_data (display, window, target, | |
1268 | QMULTIPLE, selection_atom)); | |
1269 | } | |
1270 | ||
1271 | static Lisp_Object | |
1272 | copy_multiple_data (obj) | |
1273 | Lisp_Object obj; | |
1274 | { | |
1275 | Lisp_Object vec; | |
1276 | int i; | |
1277 | int size; | |
1278 | if (CONSP (obj)) | |
8e713be6 | 1279 | return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj))); |
1b65481e | 1280 | |
b7826503 | 1281 | CHECK_VECTOR (obj); |
ede4db72 RS |
1282 | vec = Fmake_vector (size = XVECTOR (obj)->size, Qnil); |
1283 | for (i = 0; i < size; i++) | |
1284 | { | |
1285 | Lisp_Object vec2 = XVECTOR (obj)->contents [i]; | |
b7826503 | 1286 | CHECK_VECTOR (vec2); |
ede4db72 RS |
1287 | if (XVECTOR (vec2)->size != 2) |
1288 | /* ??? Confusing error message */ | |
4d30ce50 | 1289 | signal_error ("Vectors must be of length 2", vec2); |
ede4db72 RS |
1290 | XVECTOR (vec)->contents [i] = Fmake_vector (2, Qnil); |
1291 | XVECTOR (XVECTOR (vec)->contents [i])->contents [0] | |
1292 | = XVECTOR (vec2)->contents [0]; | |
1293 | XVECTOR (XVECTOR (vec)->contents [i])->contents [1] | |
1294 | = XVECTOR (vec2)->contents [1]; | |
1295 | } | |
1296 | return vec; | |
1297 | } | |
1298 | ||
1299 | #endif | |
1300 | ||
1301 | \f | |
1302 | /* Variables for communication with x_handle_selection_notify. */ | |
1303 | static Atom reading_which_selection; | |
1304 | static Lisp_Object reading_selection_reply; | |
1305 | static Window reading_selection_window; | |
1306 | ||
1307 | /* Do protocol to read selection-data from the server. | |
1308 | Converts this to Lisp data and returns it. */ | |
1309 | ||
1310 | static Lisp_Object | |
971de7fb | 1311 | x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp) |
ede4db72 | 1312 | { |
378c33ca | 1313 | struct frame *sf = SELECTED_FRAME (); |
428a555e KL |
1314 | Window requestor_window; |
1315 | Display *display; | |
1316 | struct x_display_info *dpyinfo; | |
5d0ba25b | 1317 | Time requestor_time = last_event_timestamp; |
428a555e KL |
1318 | Atom target_property; |
1319 | Atom selection_atom; | |
ede4db72 | 1320 | Atom type_atom; |
80da0190 | 1321 | int secs, usecs; |
c525d842 | 1322 | int count = SPECPDL_INDEX (); |
55b2d45d | 1323 | Lisp_Object frame; |
ede4db72 | 1324 | |
428a555e KL |
1325 | if (! FRAME_X_P (sf)) |
1326 | return Qnil; | |
1327 | ||
1328 | requestor_window = FRAME_X_WINDOW (sf); | |
1329 | display = FRAME_X_DISPLAY (sf); | |
1330 | dpyinfo = FRAME_X_DISPLAY_INFO (sf); | |
1331 | target_property = dpyinfo->Xatom_EMACS_TMP; | |
1332 | selection_atom = symbol_to_x_atom (dpyinfo, display, selection_symbol); | |
1333 | ||
ede4db72 | 1334 | if (CONSP (target_type)) |
8e713be6 | 1335 | type_atom = symbol_to_x_atom (dpyinfo, display, XCAR (target_type)); |
ede4db72 | 1336 | else |
5c3a351a | 1337 | type_atom = symbol_to_x_atom (dpyinfo, display, target_type); |
ede4db72 | 1338 | |
3a42401d JD |
1339 | if (! NILP (time_stamp)) |
1340 | { | |
1341 | if (CONSP (time_stamp)) | |
1342 | requestor_time = (Time) cons_to_long (time_stamp); | |
1343 | else if (INTEGERP (time_stamp)) | |
1344 | requestor_time = (Time) XUINT (time_stamp); | |
1345 | else if (FLOATP (time_stamp)) | |
ff59904a | 1346 | requestor_time = (Time) XFLOAT_DATA (time_stamp); |
3a42401d JD |
1347 | else |
1348 | error ("TIME_STAMP must be cons or number"); | |
1349 | } | |
1350 | ||
ede4db72 | 1351 | BLOCK_INPUT; |
1b65481e | 1352 | |
c525d842 CY |
1353 | /* The protected block contains wait_reading_process_output, which |
1354 | can run random lisp code (process handlers) or signal. | |
1355 | Therefore, we put the x_uncatch_errors call in an unwind. */ | |
1356 | record_unwind_protect (x_catch_errors_unwind, Qnil); | |
9ba8e10d | 1357 | x_catch_errors (display); |
1b65481e | 1358 | |
d9c0d4a3 GM |
1359 | TRACE2 ("Get selection %s, type %s", |
1360 | XGetAtomName (display, type_atom), | |
1361 | XGetAtomName (display, target_property)); | |
1362 | ||
ede4db72 | 1363 | XConvertSelection (display, selection_atom, type_atom, target_property, |
5d0ba25b | 1364 | requestor_window, requestor_time); |
5c3a351a | 1365 | XFlush (display); |
ede4db72 RS |
1366 | |
1367 | /* Prepare to block until the reply has been read. */ | |
5d0ba25b | 1368 | reading_selection_window = requestor_window; |
ede4db72 | 1369 | reading_which_selection = selection_atom; |
f3fbd155 | 1370 | XSETCAR (reading_selection_reply, Qnil); |
55b2d45d RS |
1371 | |
1372 | frame = some_frame_on_display (dpyinfo); | |
1373 | ||
1374 | /* If the display no longer has frames, we can't expect | |
1375 | to get many more selection requests from it, so don't | |
1376 | bother trying to queue them. */ | |
1377 | if (!NILP (frame)) | |
1378 | { | |
dd0fe424 | 1379 | x_start_queuing_selection_requests (); |
55b2d45d RS |
1380 | |
1381 | record_unwind_protect (queue_selection_requests_unwind, | |
dd0fe424 | 1382 | Qnil); |
55b2d45d | 1383 | } |
ede4db72 RS |
1384 | UNBLOCK_INPUT; |
1385 | ||
80da0190 RS |
1386 | /* This allows quits. Also, don't wait forever. */ |
1387 | secs = x_selection_timeout / 1000; | |
1388 | usecs = (x_selection_timeout % 1000) * 1000; | |
d9c0d4a3 | 1389 | TRACE1 (" Start waiting %d secs for SelectionNotify", secs); |
d64b707c KS |
1390 | wait_reading_process_output (secs, usecs, 0, 0, |
1391 | reading_selection_reply, NULL, 0); | |
d9c0d4a3 | 1392 | TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply))); |
ede4db72 | 1393 | |
7c6b2ea4 | 1394 | BLOCK_INPUT; |
c525d842 CY |
1395 | if (x_had_errors_p (display)) |
1396 | error ("Cannot get selection"); | |
1397 | /* This calls x_uncatch_errors. */ | |
9ba8e10d | 1398 | unbind_to (count, Qnil); |
7c6b2ea4 RS |
1399 | UNBLOCK_INPUT; |
1400 | ||
8e713be6 | 1401 | if (NILP (XCAR (reading_selection_reply))) |
606140dd | 1402 | error ("Timed out waiting for reply from selection owner"); |
8e713be6 | 1403 | if (EQ (XCAR (reading_selection_reply), Qlambda)) |
12061f06 | 1404 | return Qnil; |
ede4db72 RS |
1405 | |
1406 | /* Otherwise, the selection is waiting for us on the requested property. */ | |
1407 | return | |
5d0ba25b | 1408 | x_get_window_property_as_lisp_data (display, requestor_window, |
ede4db72 RS |
1409 | target_property, target_type, |
1410 | selection_atom); | |
1411 | } | |
1412 | \f | |
1413 | /* Subroutines of x_get_window_property_as_lisp_data */ | |
1414 | ||
4feb31b2 | 1415 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
0158abbc | 1416 | |
ede4db72 | 1417 | static void |
d5a3eaaf AS |
1418 | x_get_window_property (Display *display, Window window, Atom property, |
1419 | unsigned char **data_ret, int *bytes_ret, | |
1420 | Atom *actual_type_ret, int *actual_format_ret, | |
1421 | unsigned long *actual_size_ret, int delete_p) | |
ede4db72 RS |
1422 | { |
1423 | int total_size; | |
1424 | unsigned long bytes_remaining; | |
1425 | int offset = 0; | |
1426 | unsigned char *tmp_data = 0; | |
1427 | int result; | |
1428 | int buffer_size = SELECTION_QUANTUM (display); | |
1b65481e | 1429 | |
d9c0d4a3 GM |
1430 | if (buffer_size > MAX_SELECTION_QUANTUM) |
1431 | buffer_size = MAX_SELECTION_QUANTUM; | |
1b65481e | 1432 | |
ede4db72 | 1433 | BLOCK_INPUT; |
1b65481e | 1434 | |
ede4db72 RS |
1435 | /* First probe the thing to find out how big it is. */ |
1436 | result = XGetWindowProperty (display, window, property, | |
137edb72 | 1437 | 0L, 0L, False, AnyPropertyType, |
ede4db72 RS |
1438 | actual_type_ret, actual_format_ret, |
1439 | actual_size_ret, | |
1440 | &bytes_remaining, &tmp_data); | |
ede4db72 RS |
1441 | if (result != Success) |
1442 | { | |
2f65feb6 | 1443 | UNBLOCK_INPUT; |
ede4db72 RS |
1444 | *data_ret = 0; |
1445 | *bytes_ret = 0; | |
1446 | return; | |
1447 | } | |
1b65481e | 1448 | |
0158abbc RS |
1449 | /* This was allocated by Xlib, so use XFree. */ |
1450 | XFree ((char *) tmp_data); | |
1b65481e | 1451 | |
ede4db72 RS |
1452 | if (*actual_type_ret == None || *actual_format_ret == 0) |
1453 | { | |
2f65feb6 | 1454 | UNBLOCK_INPUT; |
ede4db72 RS |
1455 | return; |
1456 | } | |
1457 | ||
1458 | total_size = bytes_remaining + 1; | |
1459 | *data_ret = (unsigned char *) xmalloc (total_size); | |
1b65481e | 1460 | |
2a1a4c9d | 1461 | /* Now read, until we've gotten it all. */ |
ede4db72 RS |
1462 | while (bytes_remaining) |
1463 | { | |
d9c0d4a3 | 1464 | #ifdef TRACE_SELECTION |
ede4db72 RS |
1465 | int last = bytes_remaining; |
1466 | #endif | |
1467 | result | |
1468 | = XGetWindowProperty (display, window, property, | |
137edb72 | 1469 | (long)offset/4, (long)buffer_size/4, |
2f65feb6 | 1470 | False, |
ede4db72 RS |
1471 | AnyPropertyType, |
1472 | actual_type_ret, actual_format_ret, | |
1473 | actual_size_ret, &bytes_remaining, &tmp_data); | |
d9c0d4a3 GM |
1474 | |
1475 | TRACE2 ("Read %ld bytes from property %s", | |
1476 | last - bytes_remaining, | |
1477 | XGetAtomName (display, property)); | |
1478 | ||
ede4db72 RS |
1479 | /* If this doesn't return Success at this point, it means that |
1480 | some clod deleted the selection while we were in the midst of | |
d9c0d4a3 GM |
1481 | reading it. Deal with that, I guess.... */ |
1482 | if (result != Success) | |
1483 | break; | |
e22cf39c JD |
1484 | |
1485 | /* The man page for XGetWindowProperty says: | |
1486 | "If the returned format is 32, the returned data is represented | |
1487 | as a long array and should be cast to that type to obtain the | |
1488 | elements." | |
1489 | This applies even if long is more than 32 bits, the X library | |
1490 | converts from 32 bit elements received from the X server to long | |
72af86bd | 1491 | and passes the long array to us. Thus, for that case memcpy can not |
e22cf39c JD |
1492 | be used. We convert to a 32 bit type here, because so much code |
1493 | assume on that. | |
1494 | ||
1495 | The bytes and offsets passed to XGetWindowProperty refers to the | |
1496 | property and those are indeed in 32 bit quantities if format is 32. */ | |
1497 | ||
1498 | if (*actual_format_ret == 32 && *actual_format_ret < BITS_PER_LONG) | |
1499 | { | |
1500 | unsigned long i; | |
1501 | int *idata = (int *) ((*data_ret) + offset); | |
1502 | long *ldata = (long *) tmp_data; | |
1503 | ||
1504 | for (i = 0; i < *actual_size_ret; ++i) | |
1505 | { | |
1506 | idata[i]= (int) ldata[i]; | |
1507 | offset += 4; | |
1508 | } | |
1509 | } | |
1510 | else | |
1511 | { | |
1512 | *actual_size_ret *= *actual_format_ret / 8; | |
72af86bd | 1513 | memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret); |
e22cf39c JD |
1514 | offset += *actual_size_ret; |
1515 | } | |
1b65481e | 1516 | |
0158abbc RS |
1517 | /* This was allocated by Xlib, so use XFree. */ |
1518 | XFree ((char *) tmp_data); | |
ede4db72 | 1519 | } |
2f65feb6 | 1520 | |
5c3a351a | 1521 | XFlush (display); |
ede4db72 RS |
1522 | UNBLOCK_INPUT; |
1523 | *bytes_ret = offset; | |
1524 | } | |
1525 | \f | |
4feb31b2 | 1526 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
0158abbc | 1527 | |
ede4db72 | 1528 | static void |
d5a3eaaf AS |
1529 | receive_incremental_selection (Display *display, Window window, Atom property, |
1530 | Lisp_Object target_type, | |
1531 | unsigned int min_size_bytes, | |
1532 | unsigned char **data_ret, int *size_bytes_ret, | |
1533 | Atom *type_ret, int *format_ret, | |
1534 | unsigned long *size_ret) | |
ede4db72 RS |
1535 | { |
1536 | int offset = 0; | |
d1f21a66 | 1537 | struct prop_location *wait_object; |
ede4db72 RS |
1538 | *size_bytes_ret = min_size_bytes; |
1539 | *data_ret = (unsigned char *) xmalloc (*size_bytes_ret); | |
d9c0d4a3 GM |
1540 | |
1541 | TRACE1 ("Read %d bytes incrementally", min_size_bytes); | |
2f65feb6 RS |
1542 | |
1543 | /* At this point, we have read an INCR property. | |
1544 | Delete the property to ack it. | |
1545 | (But first, prepare to receive the next event in this handshake.) | |
ede4db72 RS |
1546 | |
1547 | Now, we must loop, waiting for the sending window to put a value on | |
1548 | that property, then reading the property, then deleting it to ack. | |
1549 | We are done when the sender places a property of length 0. | |
1550 | */ | |
2f65feb6 RS |
1551 | BLOCK_INPUT; |
1552 | XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask); | |
d9c0d4a3 | 1553 | TRACE1 (" Delete property %s", |
dd0fe424 | 1554 | SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property)))); |
2f65feb6 | 1555 | XDeleteProperty (display, window, property); |
d9c0d4a3 | 1556 | TRACE1 (" Expect new value of property %s", |
dd0fe424 | 1557 | SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property)))); |
d1f21a66 RS |
1558 | wait_object = expect_property_change (display, window, property, |
1559 | PropertyNewValue); | |
5c3a351a | 1560 | XFlush (display); |
2f65feb6 RS |
1561 | UNBLOCK_INPUT; |
1562 | ||
ede4db72 RS |
1563 | while (1) |
1564 | { | |
1565 | unsigned char *tmp_data; | |
1566 | int tmp_size_bytes; | |
d9c0d4a3 GM |
1567 | |
1568 | TRACE0 (" Wait for property change"); | |
d1f21a66 | 1569 | wait_for_property_change (wait_object); |
1b65481e | 1570 | |
ede4db72 | 1571 | /* expect it again immediately, because x_get_window_property may |
2a1a4c9d | 1572 | .. no it won't, I don't get it. |
d9c0d4a3 GM |
1573 | .. Ok, I get it now, the Xt code that implements INCR is broken. */ |
1574 | TRACE0 (" Get property value"); | |
ede4db72 RS |
1575 | x_get_window_property (display, window, property, |
1576 | &tmp_data, &tmp_size_bytes, | |
1577 | type_ret, format_ret, size_ret, 1); | |
1578 | ||
d9c0d4a3 GM |
1579 | TRACE1 (" Read increment of %d bytes", tmp_size_bytes); |
1580 | ||
ede4db72 RS |
1581 | if (tmp_size_bytes == 0) /* we're done */ |
1582 | { | |
d9c0d4a3 GM |
1583 | TRACE0 ("Done reading incrementally"); |
1584 | ||
2f65feb6 RS |
1585 | if (! waiting_for_other_props_on_window (display, window)) |
1586 | XSelectInput (display, window, STANDARD_EVENT_SET); | |
4feb31b2 | 1587 | /* Use xfree, not XFree, because x_get_window_property |
0158abbc | 1588 | calls xmalloc itself. */ |
70fdbb46 | 1589 | xfree (tmp_data); |
ede4db72 RS |
1590 | break; |
1591 | } | |
2f65feb6 RS |
1592 | |
1593 | BLOCK_INPUT; | |
d9c0d4a3 GM |
1594 | TRACE1 (" ACK by deleting property %s", |
1595 | XGetAtomName (display, property)); | |
2f65feb6 | 1596 | XDeleteProperty (display, window, property); |
d1f21a66 RS |
1597 | wait_object = expect_property_change (display, window, property, |
1598 | PropertyNewValue); | |
5c3a351a | 1599 | XFlush (display); |
2f65feb6 RS |
1600 | UNBLOCK_INPUT; |
1601 | ||
ede4db72 RS |
1602 | if (*size_bytes_ret < offset + tmp_size_bytes) |
1603 | { | |
ede4db72 RS |
1604 | *size_bytes_ret = offset + tmp_size_bytes; |
1605 | *data_ret = (unsigned char *) xrealloc (*data_ret, *size_bytes_ret); | |
1606 | } | |
1b65481e | 1607 | |
72af86bd | 1608 | memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes); |
ede4db72 | 1609 | offset += tmp_size_bytes; |
1b65481e | 1610 | |
4feb31b2 | 1611 | /* Use xfree, not XFree, because x_get_window_property |
0158abbc | 1612 | calls xmalloc itself. */ |
4feb31b2 | 1613 | xfree (tmp_data); |
ede4db72 RS |
1614 | } |
1615 | } | |
d9c0d4a3 | 1616 | |
ede4db72 RS |
1617 | \f |
1618 | /* Once a requested selection is "ready" (we got a SelectionNotify event), | |
1619 | fetch value from property PROPERTY of X window WINDOW on display DISPLAY. | |
1620 | TARGET_TYPE and SELECTION_ATOM are used in error message if this fails. */ | |
1621 | ||
1622 | static Lisp_Object | |
d5a3eaaf AS |
1623 | x_get_window_property_as_lisp_data (Display *display, Window window, |
1624 | Atom property, | |
1625 | Lisp_Object target_type, | |
1626 | Atom selection_atom) | |
ede4db72 RS |
1627 | { |
1628 | Atom actual_type; | |
1629 | int actual_format; | |
1630 | unsigned long actual_size; | |
1631 | unsigned char *data = 0; | |
1632 | int bytes = 0; | |
1633 | Lisp_Object val; | |
5c3a351a | 1634 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
ede4db72 | 1635 | |
d9c0d4a3 GM |
1636 | TRACE0 ("Reading selection data"); |
1637 | ||
ede4db72 RS |
1638 | x_get_window_property (display, window, property, &data, &bytes, |
1639 | &actual_type, &actual_format, &actual_size, 1); | |
1640 | if (! data) | |
1641 | { | |
1642 | int there_is_a_selection_owner; | |
1643 | BLOCK_INPUT; | |
1644 | there_is_a_selection_owner | |
1645 | = XGetSelectionOwner (display, selection_atom); | |
1646 | UNBLOCK_INPUT; | |
4d30ce50 KS |
1647 | if (there_is_a_selection_owner) |
1648 | signal_error ("Selection owner couldn't convert", | |
1649 | actual_type | |
1650 | ? list2 (target_type, | |
1651 | x_atom_to_symbol (display, actual_type)) | |
1652 | : target_type); | |
1653 | else | |
1654 | signal_error ("No selection", | |
1655 | x_atom_to_symbol (display, selection_atom)); | |
ede4db72 | 1656 | } |
1b65481e | 1657 | |
5c3a351a | 1658 | if (actual_type == dpyinfo->Xatom_INCR) |
ede4db72 RS |
1659 | { |
1660 | /* That wasn't really the data, just the beginning. */ | |
1661 | ||
1662 | unsigned int min_size_bytes = * ((unsigned int *) data); | |
1663 | BLOCK_INPUT; | |
4feb31b2 | 1664 | /* Use xfree, not XFree, because x_get_window_property |
0158abbc | 1665 | calls xmalloc itself. */ |
4feb31b2 | 1666 | xfree ((char *) data); |
ede4db72 RS |
1667 | UNBLOCK_INPUT; |
1668 | receive_incremental_selection (display, window, property, target_type, | |
1669 | min_size_bytes, &data, &bytes, | |
1670 | &actual_type, &actual_format, | |
1671 | &actual_size); | |
1672 | } | |
1673 | ||
2f65feb6 | 1674 | BLOCK_INPUT; |
d9c0d4a3 | 1675 | TRACE1 (" Delete property %s", XGetAtomName (display, property)); |
2f65feb6 | 1676 | XDeleteProperty (display, window, property); |
5c3a351a | 1677 | XFlush (display); |
2f65feb6 RS |
1678 | UNBLOCK_INPUT; |
1679 | ||
ede4db72 RS |
1680 | /* It's been read. Now convert it to a lisp object in some semi-rational |
1681 | manner. */ | |
1682 | val = selection_data_to_lisp_data (display, data, bytes, | |
1683 | actual_type, actual_format); | |
1b65481e | 1684 | |
4feb31b2 | 1685 | /* Use xfree, not XFree, because x_get_window_property |
0158abbc | 1686 | calls xmalloc itself. */ |
4feb31b2 | 1687 | xfree ((char *) data); |
ede4db72 RS |
1688 | return val; |
1689 | } | |
1690 | \f | |
1691 | /* These functions convert from the selection data read from the server into | |
1692 | something that we can use from Lisp, and vice versa. | |
1693 | ||
1694 | Type: Format: Size: Lisp Type: | |
1695 | ----- ------- ----- ----------- | |
1696 | * 8 * String | |
1697 | ATOM 32 1 Symbol | |
1698 | ATOM 32 > 1 Vector of Symbols | |
1699 | * 16 1 Integer | |
1700 | * 16 > 1 Vector of Integers | |
1701 | * 32 1 if <=16 bits: Integer | |
1702 | if > 16 bits: Cons of top16, bot16 | |
1703 | * 32 > 1 Vector of the above | |
1704 | ||
1705 | When converting a Lisp number to C, it is assumed to be of format 16 if | |
1706 | it is an integer, and of format 32 if it is a cons of two integers. | |
1707 | ||
1708 | When converting a vector of numbers from Lisp to C, it is assumed to be | |
1709 | of format 16 if every element in the vector is an integer, and is assumed | |
1710 | to be of format 32 if any element is a cons of two integers. | |
1711 | ||
1712 | When converting an object to C, it may be of the form (SYMBOL . <data>) | |
1713 | where SYMBOL is what we should claim that the type is. Format and | |
b8d6f4af JD |
1714 | representation are as above. |
1715 | ||
1716 | Important: When format is 32, data should contain an array of int, | |
1717 | not an array of long as the X library returns. This makes a difference | |
1718 | when sizeof(long) != sizeof(int). */ | |
ede4db72 RS |
1719 | |
1720 | ||
1721 | ||
1722 | static Lisp_Object | |
eec47d6b DN |
1723 | selection_data_to_lisp_data (Display *display, const unsigned char *data, |
1724 | int size, Atom type, int format) | |
ede4db72 | 1725 | { |
5c3a351a | 1726 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
ede4db72 | 1727 | |
5c3a351a | 1728 | if (type == dpyinfo->Xatom_NULL) |
ede4db72 RS |
1729 | return QNULL; |
1730 | ||
1731 | /* Convert any 8-bit data to a string, for compactness. */ | |
1732 | else if (format == 8) | |
e6c7c988 | 1733 | { |
e57ad4d8 KH |
1734 | Lisp_Object str, lispy_type; |
1735 | ||
1736 | str = make_unibyte_string ((char *) data, size); | |
1737 | /* Indicate that this string is from foreign selection by a text | |
1738 | property `foreign-selection' so that the caller of | |
1739 | x-get-selection-internal (usually x-get-selection) can know | |
1740 | that the string must be decode. */ | |
1741 | if (type == dpyinfo->Xatom_COMPOUND_TEXT) | |
1742 | lispy_type = QCOMPOUND_TEXT; | |
1743 | else if (type == dpyinfo->Xatom_UTF8_STRING) | |
1744 | lispy_type = QUTF8_STRING; | |
e6c7c988 | 1745 | else |
e57ad4d8 KH |
1746 | lispy_type = QSTRING; |
1747 | Fput_text_property (make_number (0), make_number (size), | |
1748 | Qforeign_selection, lispy_type, str); | |
e6c7c988 KH |
1749 | return str; |
1750 | } | |
ede4db72 RS |
1751 | /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to |
1752 | a vector of symbols. | |
1753 | */ | |
1754 | else if (type == XA_ATOM) | |
1755 | { | |
1756 | int i; | |
b8d6f4af JD |
1757 | /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8. |
1758 | But the callers of these function has made sure the data for | |
1759 | format == 32 is an array of int. Thus, use int instead | |
1760 | of Atom. */ | |
1761 | int *idata = (int *) data; | |
1762 | ||
1763 | if (size == sizeof (int)) | |
1764 | return x_atom_to_symbol (display, (Atom) idata[0]); | |
ede4db72 RS |
1765 | else |
1766 | { | |
b8d6f4af | 1767 | Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)), |
e607a484 | 1768 | make_number (0)); |
b8d6f4af | 1769 | for (i = 0; i < size / sizeof (int); i++) |
e607a484 | 1770 | Faset (v, make_number (i), |
b8d6f4af | 1771 | x_atom_to_symbol (display, (Atom) idata[i])); |
ede4db72 RS |
1772 | return v; |
1773 | } | |
1774 | } | |
1775 | ||
1ad08acd RS |
1776 | /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int. |
1777 | If the number is 32 bits and won't fit in a Lisp_Int, | |
1778 | convert it to a cons of integers, 16 bits in each half. | |
ede4db72 | 1779 | */ |
2f51feb8 AS |
1780 | else if (format == 32 && size == sizeof (int)) |
1781 | return long_to_cons (((unsigned int *) data) [0]); | |
ede4db72 RS |
1782 | else if (format == 16 && size == sizeof (short)) |
1783 | return make_number ((int) (((unsigned short *) data) [0])); | |
1784 | ||
1785 | /* Convert any other kind of data to a vector of numbers, represented | |
1786 | as above (as an integer, or a cons of two 16 bit integers.) | |
1787 | */ | |
1788 | else if (format == 16) | |
1789 | { | |
1790 | int i; | |
937a3875 RS |
1791 | Lisp_Object v; |
1792 | v = Fmake_vector (make_number (size / 2), make_number (0)); | |
1793 | for (i = 0; i < size / 2; i++) | |
ede4db72 RS |
1794 | { |
1795 | int j = (int) ((unsigned short *) data) [i]; | |
e607a484 | 1796 | Faset (v, make_number (i), make_number (j)); |
ede4db72 RS |
1797 | } |
1798 | return v; | |
1799 | } | |
1800 | else | |
1801 | { | |
1802 | int i; | |
e607a484 | 1803 | Lisp_Object v = Fmake_vector (make_number (size / 4), make_number (0)); |
ede4db72 RS |
1804 | for (i = 0; i < size / 4; i++) |
1805 | { | |
2f51feb8 | 1806 | unsigned int j = ((unsigned int *) data) [i]; |
e607a484 | 1807 | Faset (v, make_number (i), long_to_cons (j)); |
ede4db72 RS |
1808 | } |
1809 | return v; | |
1810 | } | |
1811 | } | |
1812 | ||
1813 | ||
4feb31b2 | 1814 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
0158abbc | 1815 | |
ede4db72 | 1816 | static void |
d5a3eaaf AS |
1817 | lisp_data_to_selection_data (Display *display, Lisp_Object obj, |
1818 | unsigned char **data_ret, Atom *type_ret, | |
1819 | unsigned int *size_ret, | |
1820 | int *format_ret, int *nofree_ret) | |
ede4db72 RS |
1821 | { |
1822 | Lisp_Object type = Qnil; | |
5c3a351a | 1823 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
aca39f42 RS |
1824 | |
1825 | *nofree_ret = 0; | |
1826 | ||
8e713be6 | 1827 | if (CONSP (obj) && SYMBOLP (XCAR (obj))) |
ede4db72 | 1828 | { |
8e713be6 KR |
1829 | type = XCAR (obj); |
1830 | obj = XCDR (obj); | |
1831 | if (CONSP (obj) && NILP (XCDR (obj))) | |
1832 | obj = XCAR (obj); | |
ede4db72 RS |
1833 | } |
1834 | ||
1835 | if (EQ (obj, QNULL) || (EQ (type, QNULL))) | |
1836 | { /* This is not the same as declining */ | |
1837 | *format_ret = 32; | |
1838 | *size_ret = 0; | |
1839 | *data_ret = 0; | |
1840 | type = QNULL; | |
1841 | } | |
1842 | else if (STRINGP (obj)) | |
1843 | { | |
1bd70c6e KH |
1844 | if (SCHARS (obj) < SBYTES (obj)) |
1845 | /* OBJ is a multibyte string containing a non-ASCII char. */ | |
4d30ce50 | 1846 | signal_error ("Non-ASCII string must be encoded in advance", obj); |
7b9ae523 | 1847 | if (NILP (type)) |
5109c8dd KH |
1848 | type = QSTRING; |
1849 | *format_ret = 8; | |
1850 | *size_ret = SBYTES (obj); | |
1851 | *data_ret = SDATA (obj); | |
1852 | *nofree_ret = 1; | |
ede4db72 RS |
1853 | } |
1854 | else if (SYMBOLP (obj)) | |
1855 | { | |
1856 | *format_ret = 32; | |
1857 | *size_ret = 1; | |
1858 | *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1); | |
1859 | (*data_ret) [sizeof (Atom)] = 0; | |
5c3a351a | 1860 | (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, display, obj); |
ede4db72 RS |
1861 | if (NILP (type)) type = QATOM; |
1862 | } | |
7da64e5c | 1863 | else if (INTEGERP (obj) |
ede4db72 RS |
1864 | && XINT (obj) < 0xFFFF |
1865 | && XINT (obj) > -0xFFFF) | |
1866 | { | |
1867 | *format_ret = 16; | |
1868 | *size_ret = 1; | |
1869 | *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1); | |
1870 | (*data_ret) [sizeof (short)] = 0; | |
1871 | (*(short **) data_ret) [0] = (short) XINT (obj); | |
1872 | if (NILP (type)) type = QINTEGER; | |
1873 | } | |
a87ed99c | 1874 | else if (INTEGERP (obj) |
8e713be6 KR |
1875 | || (CONSP (obj) && INTEGERP (XCAR (obj)) |
1876 | && (INTEGERP (XCDR (obj)) | |
1877 | || (CONSP (XCDR (obj)) | |
1878 | && INTEGERP (XCAR (XCDR (obj))))))) | |
ede4db72 RS |
1879 | { |
1880 | *format_ret = 32; | |
1881 | *size_ret = 1; | |
1882 | *data_ret = (unsigned char *) xmalloc (sizeof (long) + 1); | |
1883 | (*data_ret) [sizeof (long)] = 0; | |
1884 | (*(unsigned long **) data_ret) [0] = cons_to_long (obj); | |
1885 | if (NILP (type)) type = QINTEGER; | |
1886 | } | |
1887 | else if (VECTORP (obj)) | |
1888 | { | |
1889 | /* Lisp_Vectors may represent a set of ATOMs; | |
1890 | a set of 16 or 32 bit INTEGERs; | |
1891 | or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...] | |
1892 | */ | |
1893 | int i; | |
1894 | ||
1895 | if (SYMBOLP (XVECTOR (obj)->contents [0])) | |
1896 | /* This vector is an ATOM set */ | |
1897 | { | |
1898 | if (NILP (type)) type = QATOM; | |
1899 | *size_ret = XVECTOR (obj)->size; | |
1900 | *format_ret = 32; | |
1901 | *data_ret = (unsigned char *) xmalloc ((*size_ret) * sizeof (Atom)); | |
1902 | for (i = 0; i < *size_ret; i++) | |
1903 | if (SYMBOLP (XVECTOR (obj)->contents [i])) | |
1904 | (*(Atom **) data_ret) [i] | |
5c3a351a | 1905 | = symbol_to_x_atom (dpyinfo, display, XVECTOR (obj)->contents [i]); |
ede4db72 | 1906 | else |
4d30ce50 | 1907 | signal_error ("All elements of selection vector must have same type", obj); |
ede4db72 RS |
1908 | } |
1909 | #if 0 /* #### MULTIPLE doesn't work yet */ | |
1910 | else if (VECTORP (XVECTOR (obj)->contents [0])) | |
1911 | /* This vector is an ATOM_PAIR set */ | |
1912 | { | |
1913 | if (NILP (type)) type = QATOM_PAIR; | |
1914 | *size_ret = XVECTOR (obj)->size; | |
1915 | *format_ret = 32; | |
1916 | *data_ret = (unsigned char *) | |
1917 | xmalloc ((*size_ret) * sizeof (Atom) * 2); | |
1918 | for (i = 0; i < *size_ret; i++) | |
1919 | if (VECTORP (XVECTOR (obj)->contents [i])) | |
1920 | { | |
1921 | Lisp_Object pair = XVECTOR (obj)->contents [i]; | |
1922 | if (XVECTOR (pair)->size != 2) | |
4d30ce50 KS |
1923 | signal_error ( |
1924 | "Elements of the vector must be vectors of exactly two elements", | |
1925 | pair); | |
1b65481e | 1926 | |
ede4db72 | 1927 | (*(Atom **) data_ret) [i * 2] |
5c3a351a RS |
1928 | = symbol_to_x_atom (dpyinfo, display, |
1929 | XVECTOR (pair)->contents [0]); | |
ede4db72 | 1930 | (*(Atom **) data_ret) [(i * 2) + 1] |
5c3a351a RS |
1931 | = symbol_to_x_atom (dpyinfo, display, |
1932 | XVECTOR (pair)->contents [1]); | |
ede4db72 RS |
1933 | } |
1934 | else | |
4d30ce50 KS |
1935 | signal_error ("All elements of the vector must be of the same type", |
1936 | obj); | |
1b65481e | 1937 | |
ede4db72 RS |
1938 | } |
1939 | #endif | |
1940 | else | |
1941 | /* This vector is an INTEGER set, or something like it */ | |
1942 | { | |
e22cf39c | 1943 | int data_size = 2; |
ede4db72 RS |
1944 | *size_ret = XVECTOR (obj)->size; |
1945 | if (NILP (type)) type = QINTEGER; | |
1946 | *format_ret = 16; | |
1947 | for (i = 0; i < *size_ret; i++) | |
1948 | if (CONSP (XVECTOR (obj)->contents [i])) | |
1949 | *format_ret = 32; | |
7da64e5c | 1950 | else if (!INTEGERP (XVECTOR (obj)->contents [i])) |
4d30ce50 KS |
1951 | signal_error (/* Qselection_error */ |
1952 | "Elements of selection vector must be integers or conses of integers", | |
1953 | obj); | |
ede4db72 | 1954 | |
e22cf39c JD |
1955 | /* Use sizeof(long) even if it is more than 32 bits. See comment |
1956 | in x_get_window_property and x_fill_property_data. */ | |
6e816df5 | 1957 | |
e22cf39c JD |
1958 | if (*format_ret == 32) data_size = sizeof(long); |
1959 | *data_ret = (unsigned char *) xmalloc (*size_ret * data_size); | |
ede4db72 RS |
1960 | for (i = 0; i < *size_ret; i++) |
1961 | if (*format_ret == 32) | |
1962 | (*((unsigned long **) data_ret)) [i] | |
1963 | = cons_to_long (XVECTOR (obj)->contents [i]); | |
1964 | else | |
1965 | (*((unsigned short **) data_ret)) [i] | |
1966 | = (unsigned short) cons_to_long (XVECTOR (obj)->contents [i]); | |
1967 | } | |
1968 | } | |
1969 | else | |
4d30ce50 | 1970 | signal_error (/* Qselection_error */ "Unrecognized selection data", obj); |
ede4db72 | 1971 | |
5c3a351a | 1972 | *type_ret = symbol_to_x_atom (dpyinfo, display, type); |
ede4db72 RS |
1973 | } |
1974 | ||
1975 | static Lisp_Object | |
971de7fb | 1976 | clean_local_selection_data (Lisp_Object obj) |
ede4db72 RS |
1977 | { |
1978 | if (CONSP (obj) | |
8e713be6 KR |
1979 | && INTEGERP (XCAR (obj)) |
1980 | && CONSP (XCDR (obj)) | |
1981 | && INTEGERP (XCAR (XCDR (obj))) | |
1982 | && NILP (XCDR (XCDR (obj)))) | |
1983 | obj = Fcons (XCAR (obj), XCDR (obj)); | |
ede4db72 RS |
1984 | |
1985 | if (CONSP (obj) | |
8e713be6 KR |
1986 | && INTEGERP (XCAR (obj)) |
1987 | && INTEGERP (XCDR (obj))) | |
ede4db72 | 1988 | { |
8e713be6 KR |
1989 | if (XINT (XCAR (obj)) == 0) |
1990 | return XCDR (obj); | |
1991 | if (XINT (XCAR (obj)) == -1) | |
1992 | return make_number (- XINT (XCDR (obj))); | |
ede4db72 RS |
1993 | } |
1994 | if (VECTORP (obj)) | |
1995 | { | |
1996 | int i; | |
1997 | int size = XVECTOR (obj)->size; | |
1998 | Lisp_Object copy; | |
1999 | if (size == 1) | |
2000 | return clean_local_selection_data (XVECTOR (obj)->contents [0]); | |
e607a484 | 2001 | copy = Fmake_vector (make_number (size), Qnil); |
ede4db72 RS |
2002 | for (i = 0; i < size; i++) |
2003 | XVECTOR (copy)->contents [i] | |
2004 | = clean_local_selection_data (XVECTOR (obj)->contents [i]); | |
2005 | return copy; | |
2006 | } | |
2007 | return obj; | |
2008 | } | |
2009 | \f | |
2010 | /* Called from XTread_socket to handle SelectionNotify events. | |
606140dd KH |
2011 | If it's the selection we are waiting for, stop waiting |
2012 | by setting the car of reading_selection_reply to non-nil. | |
2013 | We store t there if the reply is successful, lambda if not. */ | |
ede4db72 RS |
2014 | |
2015 | void | |
971de7fb | 2016 | x_handle_selection_notify (XSelectionEvent *event) |
ede4db72 | 2017 | { |
5d0ba25b | 2018 | if (event->requestor != reading_selection_window) |
ede4db72 RS |
2019 | return; |
2020 | if (event->selection != reading_which_selection) | |
2021 | return; | |
2022 | ||
d9c0d4a3 | 2023 | TRACE0 ("Received SelectionNotify"); |
f3fbd155 KR |
2024 | XSETCAR (reading_selection_reply, |
2025 | (event->property != 0 ? Qt : Qlambda)); | |
ede4db72 RS |
2026 | } |
2027 | ||
2028 | \f | |
a0d76c27 | 2029 | DEFUN ("x-own-selection-internal", Fx_own_selection_internal, |
8c1a1077 PJ |
2030 | Sx_own_selection_internal, 2, 2, 0, |
2031 | doc: /* Assert an X selection of the given TYPE with the given VALUE. | |
2032 | TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. | |
2033 | \(Those are literal upper-case symbol names, since that's what X expects.) | |
2034 | VALUE is typically a string, or a cons of two markers, but may be | |
2035 | anything that the functions on `selection-converter-alist' know about. */) | |
5842a27b | 2036 | (Lisp_Object selection_name, Lisp_Object selection_value) |
ede4db72 | 2037 | { |
703e0710 | 2038 | check_x (); |
b7826503 | 2039 | CHECK_SYMBOL (selection_name); |
253b2298 | 2040 | if (NILP (selection_value)) error ("SELECTION-VALUE may not be nil"); |
ede4db72 RS |
2041 | x_own_selection (selection_name, selection_value); |
2042 | return selection_value; | |
2043 | } | |
2044 | ||
2045 | ||
2046 | /* Request the selection value from the owner. If we are the owner, | |
2047 | simply return our selection value. If we are not the owner, this | |
2048 | will block until all of the data has arrived. */ | |
2049 | ||
a0d76c27 | 2050 | DEFUN ("x-get-selection-internal", Fx_get_selection_internal, |
3a42401d | 2051 | Sx_get_selection_internal, 2, 3, 0, |
8c1a1077 PJ |
2052 | doc: /* Return text selected from some X window. |
2053 | SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. | |
2054 | \(Those are literal upper-case symbol names, since that's what X expects.) | |
3a42401d JD |
2055 | TYPE is the type of data desired, typically `STRING'. |
2056 | TIME_STAMP is the time to use in the XConvertSelection call for foreign | |
2057 | selections. If omitted, defaults to the time for the last event. */) | |
5842a27b | 2058 | (Lisp_Object selection_symbol, Lisp_Object target_type, Lisp_Object time_stamp) |
ede4db72 RS |
2059 | { |
2060 | Lisp_Object val = Qnil; | |
2061 | struct gcpro gcpro1, gcpro2; | |
2062 | GCPRO2 (target_type, val); /* we store newly consed data into these */ | |
703e0710 | 2063 | check_x (); |
b7826503 | 2064 | CHECK_SYMBOL (selection_symbol); |
ede4db72 RS |
2065 | |
2066 | #if 0 /* #### MULTIPLE doesn't work yet */ | |
2067 | if (CONSP (target_type) | |
8e713be6 | 2068 | && XCAR (target_type) == QMULTIPLE) |
ede4db72 | 2069 | { |
b7826503 | 2070 | CHECK_VECTOR (XCDR (target_type)); |
ede4db72 RS |
2071 | /* So we don't destructively modify this... */ |
2072 | target_type = copy_multiple_data (target_type); | |
2073 | } | |
2074 | else | |
2075 | #endif | |
b7826503 | 2076 | CHECK_SYMBOL (target_type); |
ede4db72 | 2077 | |
5109c8dd | 2078 | val = x_get_local_selection (selection_symbol, target_type, 1); |
ede4db72 RS |
2079 | |
2080 | if (NILP (val)) | |
2081 | { | |
3a42401d | 2082 | val = x_get_foreign_selection (selection_symbol, target_type, time_stamp); |
ede4db72 RS |
2083 | goto DONE; |
2084 | } | |
2085 | ||
2086 | if (CONSP (val) | |
8e713be6 | 2087 | && SYMBOLP (XCAR (val))) |
ede4db72 | 2088 | { |
8e713be6 KR |
2089 | val = XCDR (val); |
2090 | if (CONSP (val) && NILP (XCDR (val))) | |
2091 | val = XCAR (val); | |
ede4db72 RS |
2092 | } |
2093 | val = clean_local_selection_data (val); | |
2094 | DONE: | |
2095 | UNGCPRO; | |
2096 | return val; | |
2097 | } | |
2098 | ||
a0d76c27 | 2099 | DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal, |
8c1a1077 PJ |
2100 | Sx_disown_selection_internal, 1, 2, 0, |
2101 | doc: /* If we own the selection SELECTION, disown it. | |
2102 | Disowning it means there is no such selection. */) | |
5842a27b | 2103 | (Lisp_Object selection, Lisp_Object time) |
ede4db72 | 2104 | { |
ede4db72 RS |
2105 | Time timestamp; |
2106 | Atom selection_atom; | |
31df61d6 AS |
2107 | union { |
2108 | struct selection_input_event sie; | |
2109 | struct input_event ie; | |
2110 | } event; | |
3834c318 | 2111 | Display *display; |
5c3a351a | 2112 | struct x_display_info *dpyinfo; |
378c33ca | 2113 | struct frame *sf = SELECTED_FRAME (); |
ede4db72 | 2114 | |
703e0710 | 2115 | check_x (); |
428a555e KL |
2116 | if (! FRAME_X_P (sf)) |
2117 | return Qnil; | |
2118 | ||
378c33ca GM |
2119 | display = FRAME_X_DISPLAY (sf); |
2120 | dpyinfo = FRAME_X_DISPLAY_INFO (sf); | |
b7826503 | 2121 | CHECK_SYMBOL (selection); |
ede4db72 | 2122 | if (NILP (time)) |
7da64e5c | 2123 | timestamp = last_event_timestamp; |
ede4db72 RS |
2124 | else |
2125 | timestamp = cons_to_long (time); | |
2126 | ||
2127 | if (NILP (assq_no_quit (selection, Vselection_alist))) | |
2128 | return Qnil; /* Don't disown the selection when we're not the owner. */ | |
2129 | ||
5c3a351a | 2130 | selection_atom = symbol_to_x_atom (dpyinfo, display, selection); |
ede4db72 RS |
2131 | |
2132 | BLOCK_INPUT; | |
2133 | XSetSelectionOwner (display, selection_atom, None, timestamp); | |
2134 | UNBLOCK_INPUT; | |
2135 | ||
eb8c3be9 | 2136 | /* It doesn't seem to be guaranteed that a SelectionClear event will be |
ede4db72 RS |
2137 | generated for a window which owns the selection when that window sets |
2138 | the selection owner to None. The NCD server does, the MIT Sun4 server | |
2139 | doesn't. So we synthesize one; this means we might get two, but | |
2140 | that's ok, because the second one won't have any effect. */ | |
31df61d6 AS |
2141 | SELECTION_EVENT_DISPLAY (&event.sie) = display; |
2142 | SELECTION_EVENT_SELECTION (&event.sie) = selection_atom; | |
2143 | SELECTION_EVENT_TIME (&event.sie) = timestamp; | |
2144 | x_handle_selection_clear (&event.ie); | |
ede4db72 RS |
2145 | |
2146 | return Qt; | |
2147 | } | |
2148 | ||
a87ed99c RS |
2149 | /* Get rid of all the selections in buffer BUFFER. |
2150 | This is used when we kill a buffer. */ | |
2151 | ||
2152 | void | |
971de7fb | 2153 | x_disown_buffer_selections (Lisp_Object buffer) |
a87ed99c RS |
2154 | { |
2155 | Lisp_Object tail; | |
2156 | struct buffer *buf = XBUFFER (buffer); | |
2157 | ||
8e713be6 | 2158 | for (tail = Vselection_alist; CONSP (tail); tail = XCDR (tail)) |
a87ed99c RS |
2159 | { |
2160 | Lisp_Object elt, value; | |
8e713be6 KR |
2161 | elt = XCAR (tail); |
2162 | value = XCDR (elt); | |
2163 | if (CONSP (value) && MARKERP (XCAR (value)) | |
2164 | && XMARKER (XCAR (value))->buffer == buf) | |
2165 | Fx_disown_selection_internal (XCAR (elt), Qnil); | |
a87ed99c RS |
2166 | } |
2167 | } | |
ede4db72 RS |
2168 | |
2169 | DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p, | |
8c1a1077 PJ |
2170 | 0, 1, 0, |
2171 | doc: /* Whether the current Emacs process owns the given X Selection. | |
2172 | The arg should be the name of the selection in question, typically one of | |
2173 | the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. | |
2174 | \(Those are literal upper-case symbol names, since that's what X expects.) | |
2175 | For convenience, the symbol nil is the same as `PRIMARY', | |
2176 | and t is the same as `SECONDARY'. */) | |
5842a27b | 2177 | (Lisp_Object selection) |
ede4db72 | 2178 | { |
703e0710 | 2179 | check_x (); |
b7826503 | 2180 | CHECK_SYMBOL (selection); |
ede4db72 RS |
2181 | if (EQ (selection, Qnil)) selection = QPRIMARY; |
2182 | if (EQ (selection, Qt)) selection = QSECONDARY; | |
1b65481e | 2183 | |
ede4db72 RS |
2184 | if (NILP (Fassq (selection, Vselection_alist))) |
2185 | return Qnil; | |
2186 | return Qt; | |
2187 | } | |
2188 | ||
2189 | DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, | |
8c1a1077 PJ |
2190 | 0, 1, 0, |
2191 | doc: /* Whether there is an owner for the given X Selection. | |
2192 | The arg should be the name of the selection in question, typically one of | |
2193 | the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. | |
2194 | \(Those are literal upper-case symbol names, since that's what X expects.) | |
2195 | For convenience, the symbol nil is the same as `PRIMARY', | |
2196 | and t is the same as `SECONDARY'. */) | |
5842a27b | 2197 | (Lisp_Object selection) |
ede4db72 RS |
2198 | { |
2199 | Window owner; | |
356ba514 | 2200 | Atom atom; |
3834c318 | 2201 | Display *dpy; |
378c33ca | 2202 | struct frame *sf = SELECTED_FRAME (); |
3834c318 | 2203 | |
b8c70430 | 2204 | /* It should be safe to call this before we have an X frame. */ |
378c33ca | 2205 | if (! FRAME_X_P (sf)) |
b8c70430 RS |
2206 | return Qnil; |
2207 | ||
378c33ca | 2208 | dpy = FRAME_X_DISPLAY (sf); |
b7826503 | 2209 | CHECK_SYMBOL (selection); |
ede4db72 RS |
2210 | if (!NILP (Fx_selection_owner_p (selection))) |
2211 | return Qt; | |
356ba514 RS |
2212 | if (EQ (selection, Qnil)) selection = QPRIMARY; |
2213 | if (EQ (selection, Qt)) selection = QSECONDARY; | |
378c33ca | 2214 | atom = symbol_to_x_atom (FRAME_X_DISPLAY_INFO (sf), dpy, selection); |
356ba514 RS |
2215 | if (atom == 0) |
2216 | return Qnil; | |
ede4db72 | 2217 | BLOCK_INPUT; |
356ba514 | 2218 | owner = XGetSelectionOwner (dpy, atom); |
ede4db72 RS |
2219 | UNBLOCK_INPUT; |
2220 | return (owner ? Qt : Qnil); | |
2221 | } | |
2222 | ||
2223 | \f | |
1fb3821b JD |
2224 | /*********************************************************************** |
2225 | Drag and drop support | |
2226 | ***********************************************************************/ | |
2227 | /* Check that lisp values are of correct type for x_fill_property_data. | |
2228 | That is, number, string or a cons with two numbers (low and high 16 | |
2229 | bit parts of a 32 bit number). */ | |
2230 | ||
2231 | int | |
971de7fb | 2232 | x_check_property_data (Lisp_Object data) |
1fb3821b JD |
2233 | { |
2234 | Lisp_Object iter; | |
2235 | int size = 0; | |
2236 | ||
2237 | for (iter = data; CONSP (iter) && size != -1; iter = XCDR (iter), ++size) | |
2238 | { | |
2239 | Lisp_Object o = XCAR (iter); | |
2240 | ||
2241 | if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o)) | |
2242 | size = -1; | |
2243 | else if (CONSP (o) && | |
2244 | (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o)))) | |
2245 | size = -1; | |
2246 | } | |
2247 | ||
2248 | return size; | |
2249 | } | |
2250 | ||
2251 | /* Convert lisp values to a C array. Values may be a number, a string | |
2252 | which is taken as an X atom name and converted to the atom value, or | |
2253 | a cons containing the two 16 bit parts of a 32 bit number. | |
2254 | ||
2255 | DPY is the display use to look up X atoms. | |
2256 | DATA is a Lisp list of values to be converted. | |
2257 | RET is the C array that contains the converted values. It is assumed | |
ff59904a | 2258 | it is big enough to hold all values. |
e22cf39c JD |
2259 | FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to |
2260 | be stored in RET. Note that long is used for 32 even if long is more | |
2261 | than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and | |
2262 | XClientMessageEvent). */ | |
1fb3821b JD |
2263 | |
2264 | void | |
971de7fb | 2265 | x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format) |
1fb3821b | 2266 | { |
e22cf39c JD |
2267 | long val; |
2268 | long *d32 = (long *) ret; | |
2269 | short *d16 = (short *) ret; | |
2270 | char *d08 = (char *) ret; | |
1fb3821b JD |
2271 | Lisp_Object iter; |
2272 | ||
2273 | for (iter = data; CONSP (iter); iter = XCDR (iter)) | |
2274 | { | |
2275 | Lisp_Object o = XCAR (iter); | |
2276 | ||
2277 | if (INTEGERP (o)) | |
e22cf39c | 2278 | val = (long) XFASTINT (o); |
1fb3821b | 2279 | else if (FLOATP (o)) |
e22cf39c | 2280 | val = (long) XFLOAT_DATA (o); |
1fb3821b | 2281 | else if (CONSP (o)) |
e22cf39c | 2282 | val = (long) cons_to_long (o); |
1fb3821b JD |
2283 | else if (STRINGP (o)) |
2284 | { | |
2285 | BLOCK_INPUT; | |
e22cf39c | 2286 | val = (long) XInternAtom (dpy, (char *) SDATA (o), False); |
1fb3821b JD |
2287 | UNBLOCK_INPUT; |
2288 | } | |
2289 | else | |
2290 | error ("Wrong type, must be string, number or cons"); | |
2291 | ||
2292 | if (format == 8) | |
e22cf39c | 2293 | *d08++ = (char) val; |
1fb3821b | 2294 | else if (format == 16) |
e22cf39c | 2295 | *d16++ = (short) val; |
1fb3821b JD |
2296 | else |
2297 | *d32++ = val; | |
2298 | } | |
2299 | } | |
2300 | ||
2301 | /* Convert an array of C values to a Lisp list. | |
2302 | F is the frame to be used to look up X atoms if the TYPE is XA_ATOM. | |
2303 | DATA is a C array of values to be converted. | |
2304 | TYPE is the type of the data. Only XA_ATOM is special, it converts | |
2305 | each number in DATA to its corresponfing X atom as a symbol. | |
2306 | FORMAT is 8, 16 or 32 and gives the size in bits for each C value to | |
2307 | be stored in RET. | |
2308 | SIZE is the number of elements in DATA. | |
2309 | ||
b8d6f4af JD |
2310 | Important: When format is 32, data should contain an array of int, |
2311 | not an array of long as the X library returns. This makes a difference | |
2312 | when sizeof(long) != sizeof(int). | |
2313 | ||
1fb3821b JD |
2314 | Also see comment for selection_data_to_lisp_data above. */ |
2315 | ||
2316 | Lisp_Object | |
eec47d6b DN |
2317 | x_property_data_to_lisp (struct frame *f, const unsigned char *data, |
2318 | Atom type, int format, long unsigned int size) | |
1fb3821b JD |
2319 | { |
2320 | return selection_data_to_lisp_data (FRAME_X_DISPLAY (f), | |
2321 | data, size*format/8, type, format); | |
2322 | } | |
2323 | ||
31f16913 | 2324 | /* Get the mouse position in frame relative coordinates. */ |
1fb3821b JD |
2325 | |
2326 | static void | |
971de7fb | 2327 | mouse_position_for_drop (FRAME_PTR f, int *x, int *y) |
1fb3821b JD |
2328 | { |
2329 | Window root, dummy_window; | |
2330 | int dummy; | |
2331 | ||
2332 | BLOCK_INPUT; | |
2333 | ||
2334 | XQueryPointer (FRAME_X_DISPLAY (f), | |
2335 | DefaultRootWindow (FRAME_X_DISPLAY (f)), | |
2336 | ||
2337 | /* The root window which contains the pointer. */ | |
2338 | &root, | |
2339 | ||
2340 | /* Window pointer is on, not used */ | |
2341 | &dummy_window, | |
2342 | ||
2343 | /* The position on that root window. */ | |
2344 | x, y, | |
2345 | ||
2346 | /* x/y in dummy_window coordinates, not used. */ | |
2347 | &dummy, &dummy, | |
2348 | ||
2349 | /* Modifier keys and pointer buttons, about which | |
2350 | we don't care. */ | |
2351 | (unsigned int *) &dummy); | |
2352 | ||
2353 | ||
2354 | /* Absolute to relative. */ | |
2355 | *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); | |
2356 | *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); | |
2357 | ||
2358 | UNBLOCK_INPUT; | |
2359 | } | |
2360 | ||
2361 | DEFUN ("x-get-atom-name", Fx_get_atom_name, | |
2362 | Sx_get_atom_name, 1, 2, 0, | |
2363 | doc: /* Return the X atom name for VALUE as a string. | |
2364 | VALUE may be a number or a cons where the car is the upper 16 bits and | |
2365 | the cdr is the lower 16 bits of a 32 bit value. | |
2366 | Use the display for FRAME or the current frame if FRAME is not given or nil. | |
2367 | ||
2368 | If the value is 0 or the atom is not known, return the empty string. */) | |
5842a27b | 2369 | (Lisp_Object value, Lisp_Object frame) |
1fb3821b JD |
2370 | { |
2371 | struct frame *f = check_x_frame (frame); | |
2372 | char *name = 0; | |
42ca4633 | 2373 | char empty[] = ""; |
1fb3821b | 2374 | Lisp_Object ret = Qnil; |
1fb3821b JD |
2375 | Display *dpy = FRAME_X_DISPLAY (f); |
2376 | Atom atom; | |
c525d842 | 2377 | int had_errors; |
1fb3821b JD |
2378 | |
2379 | if (INTEGERP (value)) | |
2380 | atom = (Atom) XUINT (value); | |
2381 | else if (FLOATP (value)) | |
ff59904a | 2382 | atom = (Atom) XFLOAT_DATA (value); |
1fb3821b JD |
2383 | else if (CONSP (value)) |
2384 | atom = (Atom) cons_to_long (value); | |
2385 | else | |
2386 | error ("Wrong type, value must be number or cons"); | |
2387 | ||
2388 | BLOCK_INPUT; | |
9ba8e10d | 2389 | x_catch_errors (dpy); |
42ca4633 | 2390 | name = atom ? XGetAtomName (dpy, atom) : empty; |
c525d842 CY |
2391 | had_errors = x_had_errors_p (dpy); |
2392 | x_uncatch_errors (); | |
1fb3821b | 2393 | |
c525d842 | 2394 | if (!had_errors) |
1fb3821b JD |
2395 | ret = make_string (name, strlen (name)); |
2396 | ||
1fb3821b | 2397 | if (atom && name) XFree (name); |
8b3ad112 | 2398 | if (NILP (ret)) ret = empty_unibyte_string; |
1fb3821b JD |
2399 | |
2400 | UNBLOCK_INPUT; | |
2401 | ||
2402 | return ret; | |
2403 | } | |
2404 | ||
9fc68699 JD |
2405 | DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom, |
2406 | Sx_register_dnd_atom, 1, 2, 0, | |
2407 | doc: /* Request that dnd events are made for ClientMessages with ATOM. | |
2408 | ATOM can be a symbol or a string. The ATOM is interned on the display that | |
2409 | FRAME is on. If FRAME is nil, the selected frame is used. */) | |
5842a27b | 2410 | (Lisp_Object atom, Lisp_Object frame) |
9fc68699 JD |
2411 | { |
2412 | Atom x_atom; | |
2413 | struct frame *f = check_x_frame (frame); | |
2414 | size_t i; | |
2415 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); | |
2416 | ||
2417 | ||
2418 | if (SYMBOLP (atom)) | |
2419 | x_atom = symbol_to_x_atom (dpyinfo, FRAME_X_DISPLAY (f), atom); | |
2420 | else if (STRINGP (atom)) | |
2421 | { | |
2422 | BLOCK_INPUT; | |
2423 | x_atom = XInternAtom (FRAME_X_DISPLAY (f), (char *) SDATA (atom), False); | |
2424 | UNBLOCK_INPUT; | |
2425 | } | |
2426 | else | |
2427 | error ("ATOM must be a symbol or a string"); | |
2428 | ||
db9cd97a | 2429 | for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i) |
9fc68699 JD |
2430 | if (dpyinfo->x_dnd_atoms[i] == x_atom) |
2431 | return Qnil; | |
2432 | ||
db9cd97a | 2433 | if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size) |
9fc68699 JD |
2434 | { |
2435 | dpyinfo->x_dnd_atoms_size *= 2; | |
2436 | dpyinfo->x_dnd_atoms = xrealloc (dpyinfo->x_dnd_atoms, | |
2437 | sizeof (*dpyinfo->x_dnd_atoms) | |
2438 | * dpyinfo->x_dnd_atoms_size); | |
2439 | } | |
2440 | ||
2441 | dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom; | |
2442 | return Qnil; | |
2443 | } | |
2444 | ||
2445 | /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */ | |
1fb3821b JD |
2446 | |
2447 | int | |
971de7fb | 2448 | x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, struct x_display_info *dpyinfo, struct input_event *bufp) |
1fb3821b JD |
2449 | { |
2450 | Lisp_Object vec; | |
2451 | Lisp_Object frame; | |
e22cf39c JD |
2452 | /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */ |
2453 | unsigned long size = 160/event->format; | |
1fb3821b | 2454 | int x, y; |
31f16913 JD |
2455 | unsigned char *data = (unsigned char *) event->data.b; |
2456 | int idata[5]; | |
9fc68699 JD |
2457 | size_t i; |
2458 | ||
db9cd97a | 2459 | for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i) |
9fc68699 JD |
2460 | if (dpyinfo->x_dnd_atoms[i] == event->message_type) break; |
2461 | ||
2462 | if (i == dpyinfo->x_dnd_atoms_length) return 0; | |
1fb3821b JD |
2463 | |
2464 | XSETFRAME (frame, f); | |
2465 | ||
31f16913 JD |
2466 | /* On a 64 bit machine, the event->data.l array members are 64 bits (long), |
2467 | but the x_property_data_to_lisp (or rather selection_data_to_lisp_data) | |
2468 | function expects them to be of size int (i.e. 32). So to be able to | |
2469 | use that function, put the data in the form it expects if format is 32. */ | |
2470 | ||
2471 | if (event->format == 32 && event->format < BITS_PER_LONG) | |
2472 | { | |
2473 | int i; | |
2474 | for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */ | |
2475 | idata[i] = (int) event->data.l[i]; | |
2476 | data = (unsigned char *) idata; | |
2477 | } | |
2478 | ||
d2f14999 | 2479 | vec = Fmake_vector (make_number (4), Qnil); |
3ae565b3 SM |
2480 | ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f), |
2481 | event->message_type))); | |
2482 | ASET (vec, 1, frame); | |
2483 | ASET (vec, 2, make_number (event->format)); | |
2484 | ASET (vec, 3, x_property_data_to_lisp (f, | |
2485 | data, | |
2486 | event->message_type, | |
2487 | event->format, | |
2488 | size)); | |
1fb3821b JD |
2489 | |
2490 | mouse_position_for_drop (f, &x, &y); | |
2491 | bufp->kind = DRAG_N_DROP_EVENT; | |
862c94ca | 2492 | bufp->frame_or_window = frame; |
1fb3821b JD |
2493 | bufp->timestamp = CurrentTime; |
2494 | bufp->x = make_number (x); | |
2495 | bufp->y = make_number (y); | |
862c94ca | 2496 | bufp->arg = vec; |
1fb3821b JD |
2497 | bufp->modifiers = 0; |
2498 | ||
2499 | return 1; | |
2500 | } | |
2501 | ||
2502 | DEFUN ("x-send-client-message", Fx_send_client_event, | |
2503 | Sx_send_client_message, 6, 6, 0, | |
2504 | doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY. | |
2505 | ||
2506 | For DISPLAY, specify either a frame or a display name (a string). | |
2507 | If DISPLAY is nil, that stands for the selected frame's display. | |
2508 | DEST may be a number, in which case it is a Window id. The value 0 may | |
2509 | be used to send to the root window of the DISPLAY. | |
2510 | If DEST is a cons, it is converted to a 32 bit number | |
2511 | with the high 16 bits from the car and the lower 16 bit from the cdr. That | |
2512 | number is then used as a window id. | |
2513 | If DEST is a frame the event is sent to the outer window of that frame. | |
69785ad0 | 2514 | A value of nil means the currently selected frame. |
1fb3821b JD |
2515 | If DEST is the string "PointerWindow" the event is sent to the window that |
2516 | contains the pointer. If DEST is the string "InputFocus" the event is | |
2517 | sent to the window that has the input focus. | |
2518 | FROM is the frame sending the event. Use nil for currently selected frame. | |
2519 | MESSAGE-TYPE is the name of an Atom as a string. | |
2520 | FORMAT must be one of 8, 16 or 32 and determines the size of the values in | |
2521 | bits. VALUES is a list of numbers, cons and/or strings containing the values | |
2522 | to send. If a value is a string, it is converted to an Atom and the value of | |
2523 | the Atom is sent. If a value is a cons, it is converted to a 32 bit number | |
2524 | with the high 16 bits from the car and the lower 16 bit from the cdr. | |
2525 | If more values than fits into the event is given, the excessive values | |
2526 | are ignored. */) | |
5842a27b | 2527 | (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Lisp_Object message_type, Lisp_Object format, Lisp_Object values) |
2d9074ba JD |
2528 | { |
2529 | struct x_display_info *dpyinfo = check_x_display_info (display); | |
2530 | ||
933e29ff | 2531 | CHECK_STRING (message_type); |
2d9074ba | 2532 | x_send_client_event(display, dest, from, |
933e29ff J |
2533 | XInternAtom (dpyinfo->display, |
2534 | SDATA (message_type), | |
2535 | False), | |
2d9074ba JD |
2536 | format, values); |
2537 | ||
2538 | return Qnil; | |
2539 | } | |
2540 | ||
2541 | void | |
2542 | x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, Atom message_type, Lisp_Object format, Lisp_Object values) | |
1fb3821b JD |
2543 | { |
2544 | struct x_display_info *dpyinfo = check_x_display_info (display); | |
2545 | Window wdest; | |
2546 | XEvent event; | |
1fb3821b | 2547 | struct frame *f = check_x_frame (from); |
1fb3821b JD |
2548 | int to_root; |
2549 | ||
1fb3821b JD |
2550 | CHECK_NUMBER (format); |
2551 | CHECK_CONS (values); | |
2552 | ||
2553 | if (x_check_property_data (values) == -1) | |
2554 | error ("Bad data in VALUES, must be number, cons or string"); | |
2555 | ||
2556 | event.xclient.type = ClientMessage; | |
2557 | event.xclient.format = XFASTINT (format); | |
2558 | ||
2559 | if (event.xclient.format != 8 && event.xclient.format != 16 | |
2560 | && event.xclient.format != 32) | |
2561 | error ("FORMAT must be one of 8, 16 or 32"); | |
a0ecb2ac | 2562 | |
1fb3821b JD |
2563 | if (FRAMEP (dest) || NILP (dest)) |
2564 | { | |
2565 | struct frame *fdest = check_x_frame (dest); | |
2566 | wdest = FRAME_OUTER_WINDOW (fdest); | |
2567 | } | |
2568 | else if (STRINGP (dest)) | |
2569 | { | |
2570 | if (strcmp (SDATA (dest), "PointerWindow") == 0) | |
2571 | wdest = PointerWindow; | |
2572 | else if (strcmp (SDATA (dest), "InputFocus") == 0) | |
2573 | wdest = InputFocus; | |
2574 | else | |
2575 | error ("DEST as a string must be one of PointerWindow or InputFocus"); | |
2576 | } | |
2577 | else if (INTEGERP (dest)) | |
2578 | wdest = (Window) XFASTINT (dest); | |
2579 | else if (FLOATP (dest)) | |
ff59904a | 2580 | wdest = (Window) XFLOAT_DATA (dest); |
1fb3821b JD |
2581 | else if (CONSP (dest)) |
2582 | { | |
2583 | if (! NUMBERP (XCAR (dest)) || ! NUMBERP (XCDR (dest))) | |
2584 | error ("Both car and cdr for DEST must be numbers"); | |
2585 | else | |
2586 | wdest = (Window) cons_to_long (dest); | |
2587 | } | |
2588 | else | |
2589 | error ("DEST must be a frame, nil, string, number or cons"); | |
2590 | ||
2591 | if (wdest == 0) wdest = dpyinfo->root_window; | |
2592 | to_root = wdest == dpyinfo->root_window; | |
2593 | ||
1fb3821b JD |
2594 | BLOCK_INPUT; |
2595 | ||
2d9074ba | 2596 | event.xclient.message_type = message_type; |
1fb3821b JD |
2597 | event.xclient.display = dpyinfo->display; |
2598 | ||
2599 | /* Some clients (metacity for example) expects sending window to be here | |
2600 | when sending to the root window. */ | |
2601 | event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest; | |
2602 | ||
6e816df5 | 2603 | |
1fb3821b JD |
2604 | memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b)); |
2605 | x_fill_property_data (dpyinfo->display, values, event.xclient.data.b, | |
2606 | event.xclient.format); | |
2607 | ||
2608 | /* If event mask is 0 the event is sent to the client that created | |
2609 | the destination window. But if we are sending to the root window, | |
2610 | there is no such client. Then we set the event mask to 0xffff. The | |
2611 | event then goes to clients selecting for events on the root window. */ | |
9ba8e10d | 2612 | x_catch_errors (dpyinfo->display); |
1fb3821b JD |
2613 | { |
2614 | int propagate = to_root ? False : True; | |
2615 | unsigned mask = to_root ? 0xffff : 0; | |
2616 | XSendEvent (dpyinfo->display, wdest, propagate, mask, &event); | |
2617 | XFlush (dpyinfo->display); | |
2618 | } | |
4545fa20 | 2619 | x_uncatch_errors (); |
1fb3821b | 2620 | UNBLOCK_INPUT; |
1fb3821b JD |
2621 | } |
2622 | ||
2623 | \f | |
ede4db72 | 2624 | void |
971de7fb | 2625 | syms_of_xselect (void) |
ede4db72 | 2626 | { |
ede4db72 RS |
2627 | defsubr (&Sx_get_selection_internal); |
2628 | defsubr (&Sx_own_selection_internal); | |
2629 | defsubr (&Sx_disown_selection_internal); | |
2630 | defsubr (&Sx_selection_owner_p); | |
2631 | defsubr (&Sx_selection_exists_p); | |
2632 | ||
1fb3821b JD |
2633 | defsubr (&Sx_get_atom_name); |
2634 | defsubr (&Sx_send_client_message); | |
9fc68699 | 2635 | defsubr (&Sx_register_dnd_atom); |
1fb3821b | 2636 | |
ede4db72 RS |
2637 | reading_selection_reply = Fcons (Qnil, Qnil); |
2638 | staticpro (&reading_selection_reply); | |
2639 | reading_selection_window = 0; | |
2640 | reading_which_selection = 0; | |
2641 | ||
2642 | property_change_wait_list = 0; | |
2f65feb6 | 2643 | prop_location_identifier = 0; |
ede4db72 RS |
2644 | property_change_reply = Fcons (Qnil, Qnil); |
2645 | staticpro (&property_change_reply); | |
2646 | ||
2647 | Vselection_alist = Qnil; | |
2648 | staticpro (&Vselection_alist); | |
2649 | ||
2650 | DEFVAR_LISP ("selection-converter-alist", &Vselection_converter_alist, | |
8c1a1077 PJ |
2651 | doc: /* An alist associating X Windows selection-types with functions. |
2652 | These functions are called to convert the selection, with three args: | |
2653 | the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD'); | |
2654 | a desired type to which the selection should be converted; | |
2655 | and the local selection value (whatever was given to `x-own-selection'). | |
2656 | ||
2657 | The function should return the value to send to the X server | |
2658 | \(typically a string). A return value of nil | |
2659 | means that the conversion could not be done. | |
2660 | A return value which is the symbol `NULL' | |
2661 | means that a side-effect was executed, | |
2662 | and there is no meaningful selection value. */); | |
ede4db72 RS |
2663 | Vselection_converter_alist = Qnil; |
2664 | ||
c917a8de | 2665 | DEFVAR_LISP ("x-lost-selection-functions", &Vx_lost_selection_functions, |
8c1a1077 PJ |
2666 | doc: /* A list of functions to be called when Emacs loses an X selection. |
2667 | \(This happens when some other X client makes its own selection | |
2668 | or when a Lisp program explicitly clears the selection.) | |
2669 | The functions are called with one argument, the selection type | |
2670 | \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */); | |
c917a8de | 2671 | Vx_lost_selection_functions = Qnil; |
ede4db72 | 2672 | |
c917a8de | 2673 | DEFVAR_LISP ("x-sent-selection-functions", &Vx_sent_selection_functions, |
8c1a1077 PJ |
2674 | doc: /* A list of functions to be called when Emacs answers a selection request. |
2675 | The functions are called with four arguments: | |
2676 | - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD'); | |
2677 | - the selection-type which Emacs was asked to convert the | |
2678 | selection into before sending (for example, `STRING' or `LENGTH'); | |
2679 | - a flag indicating success or failure for responding to the request. | |
2680 | We might have failed (and declined the request) for any number of reasons, | |
2681 | including being asked for a selection that we no longer own, or being asked | |
2682 | to convert into a type that we don't know about or that is inappropriate. | |
2683 | This hook doesn't let you change the behavior of Emacs's selection replies, | |
2684 | it merely informs you that they have happened. */); | |
c917a8de | 2685 | Vx_sent_selection_functions = Qnil; |
ede4db72 RS |
2686 | |
2687 | DEFVAR_INT ("x-selection-timeout", &x_selection_timeout, | |
8c1a1077 PJ |
2688 | doc: /* Number of milliseconds to wait for a selection reply. |
2689 | If the selection owner doesn't reply in this time, we give up. | |
2690 | A value of 0 means wait as long as necessary. This is initialized from the | |
2691 | \"*selectionTimeout\" resource. */); | |
ede4db72 RS |
2692 | x_selection_timeout = 0; |
2693 | ||
9852377f | 2694 | /* QPRIMARY is defined in keyboard.c. */ |
d67b4f80 DN |
2695 | QSECONDARY = intern_c_string ("SECONDARY"); staticpro (&QSECONDARY); |
2696 | QSTRING = intern_c_string ("STRING"); staticpro (&QSTRING); | |
2697 | QINTEGER = intern_c_string ("INTEGER"); staticpro (&QINTEGER); | |
2698 | QCLIPBOARD = intern_c_string ("CLIPBOARD"); staticpro (&QCLIPBOARD); | |
2699 | QTIMESTAMP = intern_c_string ("TIMESTAMP"); staticpro (&QTIMESTAMP); | |
2700 | QTEXT = intern_c_string ("TEXT"); staticpro (&QTEXT); | |
2701 | QCOMPOUND_TEXT = intern_c_string ("COMPOUND_TEXT"); staticpro (&QCOMPOUND_TEXT); | |
2702 | QUTF8_STRING = intern_c_string ("UTF8_STRING"); staticpro (&QUTF8_STRING); | |
2703 | QDELETE = intern_c_string ("DELETE"); staticpro (&QDELETE); | |
2704 | QMULTIPLE = intern_c_string ("MULTIPLE"); staticpro (&QMULTIPLE); | |
2705 | QINCR = intern_c_string ("INCR"); staticpro (&QINCR); | |
2706 | QEMACS_TMP = intern_c_string ("_EMACS_TMP_"); staticpro (&QEMACS_TMP); | |
2707 | QTARGETS = intern_c_string ("TARGETS"); staticpro (&QTARGETS); | |
2708 | QATOM = intern_c_string ("ATOM"); staticpro (&QATOM); | |
2709 | QATOM_PAIR = intern_c_string ("ATOM_PAIR"); staticpro (&QATOM_PAIR); | |
2710 | QNULL = intern_c_string ("NULL"); staticpro (&QNULL); | |
2711 | Qcompound_text_with_extensions = intern_c_string ("compound-text-with-extensions"); | |
5a79ea57 | 2712 | staticpro (&Qcompound_text_with_extensions); |
ede4db72 | 2713 | |
d67b4f80 | 2714 | Qforeign_selection = intern_c_string ("foreign-selection"); |
e57ad4d8 | 2715 | staticpro (&Qforeign_selection); |
ede4db72 | 2716 | } |
ab5796a9 MB |
2717 | |
2718 | /* arch-tag: 7c293b0f-9918-4f69-8ac7-03e142307236 | |
2719 | (do not change this comment) */ |