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