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