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