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