428
|
1 /* X Selection processing for XEmacs
|
|
2 Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
|
793
|
3 Copyright (C) 2001, 2002 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not synched with FSF. */
|
|
23
|
|
24 /* Rewritten by jwz */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28
|
771
|
29 #include "charset.h"
|
872
|
30 #include "device-impl.h"
|
|
31 #include "frame-impl.h"
|
800
|
32 #include "opaque.h"
|
|
33 #include "select.h"
|
|
34
|
872
|
35 #include "console-x-impl.h"
|
428
|
36 #include "objects-x.h"
|
|
37
|
|
38 #include "systime.h"
|
|
39
|
|
40 int lisp_to_time (Lisp_Object, time_t *);
|
|
41 Lisp_Object time_to_lisp (time_t);
|
|
42
|
|
43 #ifdef LWLIB_USES_MOTIF
|
|
44 # define MOTIF_CLIPBOARDS
|
|
45 #endif
|
|
46
|
|
47 #ifdef MOTIF_CLIPBOARDS
|
1315
|
48 # include "xmotif.h"
|
|
49 /* Kludge around shadowing warnings */
|
|
50 # define index index_
|
428
|
51 # include <Xm/CutPaste.h>
|
1315
|
52 # undef index
|
428
|
53 static void hack_motif_clipboard_selection (Atom selection_atom,
|
|
54 Lisp_Object selection_value,
|
|
55 Time thyme, Display *display,
|
456
|
56 Window selecting_window,
|
|
57 int owned_p);
|
428
|
58 #endif
|
|
59
|
|
60 #define CUT_BUFFER_SUPPORT
|
|
61
|
|
62 #ifdef CUT_BUFFER_SUPPORT
|
|
63 Lisp_Object QCUT_BUFFER0, QCUT_BUFFER1, QCUT_BUFFER2, QCUT_BUFFER3,
|
|
64 QCUT_BUFFER4, QCUT_BUFFER5, QCUT_BUFFER6, QCUT_BUFFER7;
|
|
65 #endif
|
|
66
|
|
67 Lisp_Object Vx_sent_selection_hooks;
|
|
68
|
|
69 /* If this is a smaller number than the max-request-size of the display,
|
|
70 emacs will use INCR selection transfer when the selection is larger
|
|
71 than this. The max-request-size is usually around 64k, so if you want
|
|
72 emacs to use incremental selection transfers when the selection is
|
|
73 smaller than that, set this. I added this mostly for debugging the
|
|
74 incremental transfer stuff, but it might improve server performance.
|
|
75 */
|
|
76 #define MAX_SELECTION_QUANTUM 0xFFFFFF
|
|
77
|
|
78 #define SELECTION_QUANTUM(dpy) ((XMaxRequestSize (dpy) << 2) - 100)
|
|
79
|
|
80 /* If the selection owner takes too long to reply to a selection request,
|
|
81 we give up on it. This is in seconds (0 = no timeout).
|
|
82 */
|
458
|
83 Fixnum x_selection_timeout;
|
428
|
84
|
456
|
85 /* Enable motif selection optimizations. */
|
|
86 int x_selection_strict_motif_ownership;
|
|
87
|
428
|
88
|
|
89 /* Utility functions */
|
|
90
|
|
91 static Lisp_Object x_get_window_property_as_lisp_data (Display *,
|
|
92 Window,
|
|
93 Atom property,
|
|
94 Lisp_Object target_type,
|
|
95 Atom selection_atom);
|
|
96
|
|
97 static int expect_property_change (Display *, Window, Atom prop, int state);
|
|
98 static void wait_for_property_change (long);
|
|
99 static void unexpect_property_change (int);
|
|
100 static int waiting_for_other_props_on_window (Display *, Window);
|
|
101
|
|
102 /* This converts a Lisp symbol to a server Atom, avoiding a server
|
|
103 roundtrip whenever possible.
|
|
104 */
|
|
105 static Atom
|
|
106 symbol_to_x_atom (struct device *d, Lisp_Object sym, int only_if_exists)
|
|
107 {
|
|
108 Display *display = DEVICE_X_DISPLAY (d);
|
|
109
|
|
110 if (NILP (sym)) return XA_PRIMARY;
|
|
111 if (EQ (sym, Qt)) return XA_SECONDARY;
|
|
112 if (EQ (sym, QPRIMARY)) return XA_PRIMARY;
|
|
113 if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
|
|
114 if (EQ (sym, QSTRING)) return XA_STRING;
|
|
115 if (EQ (sym, QINTEGER)) return XA_INTEGER;
|
|
116 if (EQ (sym, QATOM)) return XA_ATOM;
|
|
117 if (EQ (sym, QCLIPBOARD)) return DEVICE_XATOM_CLIPBOARD (d);
|
|
118 if (EQ (sym, QTIMESTAMP)) return DEVICE_XATOM_TIMESTAMP (d);
|
|
119 if (EQ (sym, QTEXT)) return DEVICE_XATOM_TEXT (d);
|
|
120 if (EQ (sym, QDELETE)) return DEVICE_XATOM_DELETE (d);
|
|
121 if (EQ (sym, QMULTIPLE)) return DEVICE_XATOM_MULTIPLE (d);
|
|
122 if (EQ (sym, QINCR)) return DEVICE_XATOM_INCR (d);
|
|
123 if (EQ (sym, QEMACS_TMP)) return DEVICE_XATOM_EMACS_TMP (d);
|
|
124 if (EQ (sym, QTARGETS)) return DEVICE_XATOM_TARGETS (d);
|
|
125 if (EQ (sym, QNULL)) return DEVICE_XATOM_NULL (d);
|
|
126 if (EQ (sym, QATOM_PAIR)) return DEVICE_XATOM_ATOM_PAIR (d);
|
|
127 if (EQ (sym, QCOMPOUND_TEXT)) return DEVICE_XATOM_COMPOUND_TEXT (d);
|
|
128
|
|
129 #ifdef CUT_BUFFER_SUPPORT
|
|
130 if (EQ (sym, QCUT_BUFFER0)) return XA_CUT_BUFFER0;
|
|
131 if (EQ (sym, QCUT_BUFFER1)) return XA_CUT_BUFFER1;
|
|
132 if (EQ (sym, QCUT_BUFFER2)) return XA_CUT_BUFFER2;
|
|
133 if (EQ (sym, QCUT_BUFFER3)) return XA_CUT_BUFFER3;
|
|
134 if (EQ (sym, QCUT_BUFFER4)) return XA_CUT_BUFFER4;
|
|
135 if (EQ (sym, QCUT_BUFFER5)) return XA_CUT_BUFFER5;
|
|
136 if (EQ (sym, QCUT_BUFFER6)) return XA_CUT_BUFFER6;
|
|
137 if (EQ (sym, QCUT_BUFFER7)) return XA_CUT_BUFFER7;
|
|
138 #endif /* CUT_BUFFER_SUPPORT */
|
|
139
|
|
140 {
|
647
|
141 const Extbyte *nameext;
|
442
|
142 LISP_STRING_TO_EXTERNAL (Fsymbol_name (sym), nameext, Qctext);
|
428
|
143 return XInternAtom (display, nameext, only_if_exists ? True : False);
|
|
144 }
|
|
145 }
|
|
146
|
|
147
|
|
148 /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
|
|
149 and calls to intern whenever possible.
|
|
150 */
|
|
151 static Lisp_Object
|
|
152 x_atom_to_symbol (struct device *d, Atom atom)
|
|
153 {
|
|
154 Display *display = DEVICE_X_DISPLAY (d);
|
|
155
|
|
156 if (! atom) return Qnil;
|
|
157 if (atom == XA_PRIMARY) return QPRIMARY;
|
|
158 if (atom == XA_SECONDARY) return QSECONDARY;
|
|
159 if (atom == XA_STRING) return QSTRING;
|
|
160 if (atom == XA_INTEGER) return QINTEGER;
|
|
161 if (atom == XA_ATOM) return QATOM;
|
|
162 if (atom == DEVICE_XATOM_CLIPBOARD (d)) return QCLIPBOARD;
|
|
163 if (atom == DEVICE_XATOM_TIMESTAMP (d)) return QTIMESTAMP;
|
|
164 if (atom == DEVICE_XATOM_TEXT (d)) return QTEXT;
|
|
165 if (atom == DEVICE_XATOM_DELETE (d)) return QDELETE;
|
|
166 if (atom == DEVICE_XATOM_MULTIPLE (d)) return QMULTIPLE;
|
|
167 if (atom == DEVICE_XATOM_INCR (d)) return QINCR;
|
|
168 if (atom == DEVICE_XATOM_EMACS_TMP (d)) return QEMACS_TMP;
|
|
169 if (atom == DEVICE_XATOM_TARGETS (d)) return QTARGETS;
|
|
170 if (atom == DEVICE_XATOM_NULL (d)) return QNULL;
|
|
171 if (atom == DEVICE_XATOM_ATOM_PAIR (d)) return QATOM_PAIR;
|
|
172 if (atom == DEVICE_XATOM_COMPOUND_TEXT (d)) return QCOMPOUND_TEXT;
|
|
173
|
|
174 #ifdef CUT_BUFFER_SUPPORT
|
|
175 if (atom == XA_CUT_BUFFER0) return QCUT_BUFFER0;
|
|
176 if (atom == XA_CUT_BUFFER1) return QCUT_BUFFER1;
|
|
177 if (atom == XA_CUT_BUFFER2) return QCUT_BUFFER2;
|
|
178 if (atom == XA_CUT_BUFFER3) return QCUT_BUFFER3;
|
|
179 if (atom == XA_CUT_BUFFER4) return QCUT_BUFFER4;
|
|
180 if (atom == XA_CUT_BUFFER5) return QCUT_BUFFER5;
|
|
181 if (atom == XA_CUT_BUFFER6) return QCUT_BUFFER6;
|
|
182 if (atom == XA_CUT_BUFFER7) return QCUT_BUFFER7;
|
|
183 #endif
|
|
184
|
|
185 {
|
867
|
186 Ibyte *intstr;
|
771
|
187 Extbyte *str = XGetAtomName (display, atom);
|
428
|
188
|
|
189 if (! str) return Qnil;
|
|
190
|
440
|
191 TO_INTERNAL_FORMAT (C_STRING, str,
|
|
192 C_STRING_ALLOCA, intstr,
|
|
193 Qctext);
|
428
|
194 XFree (str);
|
771
|
195 return intern_int (intstr);
|
428
|
196 }
|
|
197 }
|
|
198
|
647
|
199 #define PROCESSING_X_CODE
|
|
200 #include "select-common.h"
|
|
201 #undef PROCESSING_X_CODE
|
428
|
202
|
|
203 /* Do protocol to assert ourself as a selection owner.
|
|
204 */
|
|
205 static Lisp_Object
|
442
|
206 x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value,
|
456
|
207 Lisp_Object how_to_add, Lisp_Object selection_type,
|
|
208 int owned_p)
|
428
|
209 {
|
|
210 struct device *d = decode_x_device (Qnil);
|
|
211 Display *display = DEVICE_X_DISPLAY (d);
|
|
212 struct frame *sel_frame = selected_frame ();
|
|
213 Window selecting_window = XtWindow (FRAME_X_TEXT_WIDGET (sel_frame));
|
|
214 Lisp_Object selection_time;
|
|
215 /* Use the time of the last-read mouse or keyboard event.
|
|
216 For selection purposes, we use this as a sleazy way of knowing what the
|
|
217 current time is in server-time. This assumes that the most recently read
|
|
218 mouse or keyboard event has something to do with the assertion of the
|
|
219 selection, which is probably true.
|
|
220 */
|
|
221 Time thyme = DEVICE_X_MOUSE_TIMESTAMP (d);
|
|
222 Atom selection_atom;
|
|
223
|
|
224 CHECK_SYMBOL (selection_name);
|
|
225 selection_atom = symbol_to_x_atom (d, selection_name, 0);
|
|
226
|
|
227 XSetSelectionOwner (display, selection_atom, selecting_window, thyme);
|
|
228
|
|
229 /* We do NOT use time_to_lisp() here any more, like we used to.
|
|
230 That assumed equivalence of time_t and Time, which is not
|
|
231 necessarily the case (e.g. under OSF on the Alphas, where
|
|
232 Time is a 64-bit quantity and time_t is a 32-bit quantity).
|
442
|
233
|
428
|
234 Opaque pointers are the clean way to go here.
|
|
235 */
|
440
|
236 selection_time = make_opaque (&thyme, sizeof (thyme));
|
428
|
237
|
|
238 #ifdef MOTIF_CLIPBOARDS
|
|
239 hack_motif_clipboard_selection (selection_atom, selection_value,
|
456
|
240 thyme, display, selecting_window, owned_p);
|
428
|
241 #endif
|
|
242 return selection_time;
|
|
243 }
|
|
244
|
|
245 #ifdef MOTIF_CLIPBOARDS /* Bend over baby. Take it and like it. */
|
|
246
|
|
247 # ifdef MOTIF_INCREMENTAL_CLIPBOARDS_WORK
|
|
248 static void motif_clipboard_cb ();
|
|
249 # endif
|
|
250
|
|
251 static void
|
|
252 hack_motif_clipboard_selection (Atom selection_atom,
|
|
253 Lisp_Object selection_value,
|
|
254 Time thyme,
|
|
255 Display *display,
|
456
|
256 Window selecting_window,
|
|
257 int owned_p)
|
428
|
258 {
|
|
259 struct device *d = get_device_from_display (display);
|
|
260 /* Those Motif wankers can't be bothered to follow the ICCCM, and do
|
|
261 their own non-Xlib non-Xt clipboard processing. So we have to do
|
|
262 this so that linked-in Motif widgets don't get themselves wedged.
|
|
263 */
|
|
264 if (selection_atom == DEVICE_XATOM_CLIPBOARD (d)
|
|
265 && STRINGP (selection_value)
|
|
266
|
|
267 /* If we already own the clipboard, don't own it again in the Motif
|
|
268 way. This might lose in some subtle way, since the timestamp won't
|
|
269 be current, but owning the selection on the Motif way does a
|
|
270 SHITLOAD of X protocol, and it makes killing text be incredibly
|
|
271 slow when using an X terminal. ARRRRGGGHHH!!!!
|
|
272 */
|
|
273 /* No, this is no good, because then Motif text fields don't bother
|
|
274 to look up the new value, and you can't Copy from a buffer, Paste
|
|
275 into a text field, then Copy something else from the buffer and
|
|
276 paste it into the text field -- it pastes the first thing again. */
|
456
|
277 && (!owned_p
|
|
278 /* Selectively re-enable this because for most users its
|
|
279 just too painful - especially over a remote link. */
|
|
280 || x_selection_strict_motif_ownership)
|
428
|
281 )
|
|
282 {
|
|
283 #ifdef MOTIF_INCREMENTAL_CLIPBOARDS_WORK
|
|
284 Widget widget = FRAME_X_TEXT_WIDGET (selected_frame());
|
|
285 #endif
|
|
286 long itemid;
|
|
287 #if XmVersion >= 1002
|
|
288 long dataid;
|
|
289 #else
|
|
290 int dataid; /* 1.2 wants long, but 1.1.5 wants int... */
|
|
291 #endif
|
|
292 XmString fmh;
|
|
293 String encoding = "STRING";
|
867
|
294 const Ibyte *data = XSTRING_DATA (selection_value);
|
444
|
295 Bytecount bytes = XSTRING_LENGTH (selection_value);
|
428
|
296
|
|
297 #ifdef MULE
|
|
298 {
|
|
299 enum { ASCII, LATIN_1, WORLD } chartypes = ASCII;
|
867
|
300 const Ibyte *ptr = data, *end = ptr + bytes;
|
428
|
301 /* Optimize for the common ASCII case */
|
|
302 while (ptr <= end)
|
|
303 {
|
826
|
304 if (byte_ascii_p (*ptr))
|
428
|
305 {
|
|
306 ptr++;
|
|
307 continue;
|
|
308 }
|
|
309
|
|
310 if ((*ptr) == LEADING_BYTE_LATIN_ISO8859_1 ||
|
|
311 (*ptr) == LEADING_BYTE_CONTROL_1)
|
|
312 {
|
|
313 chartypes = LATIN_1;
|
|
314 ptr += 2;
|
|
315 continue;
|
|
316 }
|
|
317
|
|
318 chartypes = WORLD;
|
|
319 break;
|
|
320 }
|
|
321
|
|
322 if (chartypes == LATIN_1)
|
440
|
323 TO_EXTERNAL_FORMAT (LISP_STRING, selection_value,
|
|
324 ALLOCA, (data, bytes),
|
|
325 Qbinary);
|
428
|
326 else if (chartypes == WORLD)
|
|
327 {
|
440
|
328 TO_EXTERNAL_FORMAT (LISP_STRING, selection_value,
|
|
329 ALLOCA, (data, bytes),
|
|
330 Qctext);
|
428
|
331 encoding = "COMPOUND_TEXT";
|
|
332 }
|
|
333 }
|
|
334 #endif /* MULE */
|
|
335
|
|
336 fmh = XmStringCreateLtoR ("Clipboard", XmSTRING_DEFAULT_CHARSET);
|
|
337 while (ClipboardSuccess !=
|
|
338 XmClipboardStartCopy (display, selecting_window, fmh, thyme,
|
|
339 #ifdef MOTIF_INCREMENTAL_CLIPBOARDS_WORK
|
|
340 widget, motif_clipboard_cb,
|
|
341 #else
|
|
342 0, NULL,
|
|
343 #endif
|
|
344 &itemid))
|
|
345 ;
|
|
346 XmStringFree (fmh);
|
|
347 while (ClipboardSuccess !=
|
|
348 XmClipboardCopy (display, selecting_window, itemid, encoding,
|
|
349 #ifdef MOTIF_INCREMENTAL_CLIPBOARDS_WORK
|
|
350 /* O'Reilly examples say size can be 0,
|
|
351 but this clearly is not the case. */
|
|
352 0, bytes, (int) selecting_window, /* private id */
|
|
353 #else /* !MOTIF_INCREMENTAL_CLIPBOARDS_WORK */
|
|
354 (XtPointer) data, bytes, 0,
|
|
355 #endif /* !MOTIF_INCREMENTAL_CLIPBOARDS_WORK */
|
|
356 &dataid))
|
|
357 ;
|
|
358 while (ClipboardSuccess !=
|
|
359 XmClipboardEndCopy (display, selecting_window, itemid))
|
|
360 ;
|
|
361 }
|
|
362 }
|
|
363
|
|
364 # ifdef MOTIF_INCREMENTAL_CLIPBOARDS_WORK
|
|
365 /* I tried to treat the clipboard like a real selection, and not send
|
|
366 the data until it was requested, but it looks like that just doesn't
|
|
367 work at all unless the selection owner and requestor are in different
|
|
368 processes. From reading the Motif source, it looks like they never
|
|
369 even considered having two widgets in the same application transfer
|
|
370 data between each other using "by-name" clipboard values. What a
|
|
371 bunch of fuckups.
|
|
372 */
|
|
373 static void
|
|
374 motif_clipboard_cb (Widget widget, int *data_id, int *private_id, int *reason)
|
|
375 {
|
|
376 switch (*reason)
|
|
377 {
|
|
378 case XmCR_CLIPBOARD_DATA_REQUEST:
|
|
379 {
|
|
380 Display *dpy = XtDisplay (widget);
|
|
381 Window window = (Window) *private_id;
|
442
|
382 Lisp_Object selection = select_convert_out (QCLIPBOARD, Qnil, Qnil);
|
|
383
|
|
384 /* Whichever lazy git wrote this originally just called abort()
|
|
385 when anything didn't go their way... */
|
|
386
|
|
387 /* Try some other text types */
|
|
388 if (NILP (selection))
|
|
389 selection = select_convert_out (QCLIPBOARD, QSTRING, Qnil);
|
|
390 if (NILP (selection))
|
|
391 selection = select_convert_out (QCLIPBOARD, QTEXT, Qnil);
|
|
392 if (NILP (selection))
|
|
393 selection = select_convert_out (QCLIPBOARD, QCOMPOUND_TEXT, Qnil);
|
|
394
|
|
395 if (CONSP (selection) && SYMBOLP (XCAR (selection))
|
|
396 && (EQ (XCAR (selection), QSTRING)
|
|
397 || EQ (XCAR (selection), QTEXT)
|
|
398 || EQ (XCAR (selection), QCOMPOUND_TEXT)))
|
|
399 selection = XCDR (selection);
|
|
400
|
|
401 if (NILP (selection))
|
563
|
402 signal_error (Qselection_conversion_error, "no selection",
|
|
403 Qunbound);
|
442
|
404
|
|
405 if (!STRINGP (selection))
|
|
406 signal_error (Qselection_conversion_error,
|
563
|
407 "couldn't convert selection to string", Qunbound);
|
442
|
408
|
|
409
|
428
|
410 XmClipboardCopyByName (dpy, window, *data_id,
|
|
411 (char *) XSTRING_DATA (selection),
|
|
412 XSTRING_LENGTH (selection) + 1,
|
|
413 0);
|
|
414 }
|
|
415 break;
|
|
416 case XmCR_CLIPBOARD_DATA_DELETE:
|
|
417 default:
|
|
418 /* don't need to free anything */
|
|
419 break;
|
|
420 }
|
|
421 }
|
|
422 # endif /* MOTIF_INCREMENTAL_CLIPBOARDS_WORK */
|
|
423 #endif /* MOTIF_CLIPBOARDS */
|
|
424
|
|
425
|
|
426
|
|
427
|
|
428 /* Send a SelectionNotify event to the requestor with property=None, meaning
|
|
429 we were unable to do what they wanted.
|
|
430 */
|
|
431 static void
|
|
432 x_decline_selection_request (XSelectionRequestEvent *event)
|
|
433 {
|
|
434 XSelectionEvent reply;
|
|
435 reply.type = SelectionNotify;
|
|
436 reply.display = event->display;
|
|
437 reply.requestor = event->requestor;
|
|
438 reply.selection = event->selection;
|
|
439 reply.time = event->time;
|
|
440 reply.target = event->target;
|
|
441 reply.property = None;
|
|
442
|
|
443 XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
|
|
444 XFlush (reply.display);
|
|
445 }
|
|
446
|
|
447
|
|
448 /* Used as an unwind-protect clause so that, if a selection-converter signals
|
|
449 an error, we tell the requestor that we were unable to do what they wanted
|
|
450 before we throw to top-level or go into the debugger or whatever.
|
|
451 */
|
|
452 static Lisp_Object
|
|
453 x_selection_request_lisp_error (Lisp_Object closure)
|
|
454 {
|
|
455 XSelectionRequestEvent *event = (XSelectionRequestEvent *)
|
|
456 get_opaque_ptr (closure);
|
|
457
|
|
458 free_opaque_ptr (closure);
|
|
459 if (event->type == 0) /* we set this to mean "completed normally" */
|
|
460 return Qnil;
|
|
461 x_decline_selection_request (event);
|
|
462 return Qnil;
|
|
463 }
|
|
464
|
|
465
|
|
466 /* Convert our selection to the requested type, and put that data where the
|
|
467 requestor wants it. Then tell them whether we've succeeded.
|
|
468 */
|
|
469 static void
|
|
470 x_reply_selection_request (XSelectionRequestEvent *event, int format,
|
665
|
471 UChar_Binary *data, Bytecount size, Atom type)
|
428
|
472 {
|
|
473 /* This function can GC */
|
|
474 XSelectionEvent reply;
|
|
475 Display *display = event->display;
|
|
476 struct device *d = get_device_from_display (display);
|
|
477 Window window = event->requestor;
|
665
|
478 Bytecount bytes_remaining;
|
428
|
479 int format_bytes = format/8;
|
665
|
480 Bytecount max_bytes = SELECTION_QUANTUM (display);
|
428
|
481 if (max_bytes > MAX_SELECTION_QUANTUM) max_bytes = MAX_SELECTION_QUANTUM;
|
|
482
|
|
483 reply.type = SelectionNotify;
|
|
484 reply.display = display;
|
|
485 reply.requestor = window;
|
|
486 reply.selection = event->selection;
|
|
487 reply.time = event->time;
|
|
488 reply.target = event->target;
|
|
489 reply.property = (event->property == None ? event->target : event->property);
|
|
490
|
|
491 /* #### XChangeProperty can generate BadAlloc, and we must handle it! */
|
|
492
|
|
493 /* Store the data on the requested property.
|
|
494 If the selection is large, only store the first N bytes of it.
|
|
495 */
|
|
496 bytes_remaining = size * format_bytes;
|
|
497 if (bytes_remaining <= max_bytes)
|
|
498 {
|
|
499 /* Send all the data at once, with minimal handshaking. */
|
|
500 #if 0
|
|
501 stderr_out ("\nStoring all %d\n", bytes_remaining);
|
|
502 #endif
|
|
503 XChangeProperty (display, window, reply.property, type, format,
|
|
504 PropModeReplace, data, size);
|
|
505 /* At this point, the selection was successfully stored; ack it. */
|
|
506 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
|
|
507 XFlush (display);
|
|
508 }
|
|
509 else
|
|
510 {
|
|
511 /* Send an INCR selection. */
|
|
512 int prop_id;
|
|
513
|
|
514 if (x_window_to_frame (d, window)) /* #### debug */
|
563
|
515 invalid_operation ("attempt to transfer an INCR to ourself!", Qunbound);
|
428
|
516 #if 0
|
|
517 stderr_out ("\nINCR %d\n", bytes_remaining);
|
|
518 #endif
|
|
519 prop_id = expect_property_change (display, window, reply.property,
|
|
520 PropertyDelete);
|
|
521
|
|
522 XChangeProperty (display, window, reply.property, DEVICE_XATOM_INCR (d),
|
647
|
523 32, PropModeReplace, (UChar_Binary *)
|
428
|
524 &bytes_remaining, 1);
|
|
525 XSelectInput (display, window, PropertyChangeMask);
|
|
526 /* Tell 'em the INCR data is there... */
|
|
527 XSendEvent (display, window, False, 0L, (XEvent *) &reply);
|
|
528 XFlush (display);
|
|
529
|
|
530 /* First, wait for the requestor to ack by deleting the property.
|
|
531 This can run random lisp code (process handlers) or signal.
|
|
532 */
|
|
533 wait_for_property_change (prop_id);
|
|
534
|
|
535 while (bytes_remaining)
|
|
536 {
|
665
|
537 Bytecount i = ((bytes_remaining < max_bytes)
|
428
|
538 ? bytes_remaining
|
|
539 : max_bytes);
|
|
540 prop_id = expect_property_change (display, window, reply.property,
|
|
541 PropertyDelete);
|
|
542 #if 0
|
|
543 stderr_out (" INCR adding %d\n", i);
|
|
544 #endif
|
|
545 /* Append the next chunk of data to the property. */
|
|
546 XChangeProperty (display, window, reply.property, type, format,
|
|
547 PropModeAppend, data, i / format_bytes);
|
|
548 bytes_remaining -= i;
|
|
549 data += i;
|
|
550
|
|
551 /* Now wait for the requestor to ack this chunk by deleting the
|
|
552 property. This can run random lisp code or signal.
|
|
553 */
|
|
554 wait_for_property_change (prop_id);
|
|
555 }
|
|
556 /* Now write a zero-length chunk to the property to tell the requestor
|
|
557 that we're done. */
|
|
558 #if 0
|
|
559 stderr_out (" INCR done\n");
|
|
560 #endif
|
|
561 if (! waiting_for_other_props_on_window (display, window))
|
|
562 XSelectInput (display, window, 0L);
|
|
563
|
|
564 XChangeProperty (display, window, reply.property, type, format,
|
|
565 PropModeReplace, data, 0);
|
|
566 }
|
|
567 }
|
|
568
|
|
569
|
|
570
|
|
571 /* Called from the event-loop in response to a SelectionRequest event.
|
|
572 */
|
|
573 void
|
|
574 x_handle_selection_request (XSelectionRequestEvent *event)
|
|
575 {
|
|
576 /* This function can GC */
|
442
|
577 struct gcpro gcpro1, gcpro2;
|
|
578 Lisp_Object temp_obj;
|
428
|
579 Lisp_Object selection_symbol;
|
|
580 Lisp_Object target_symbol = Qnil;
|
|
581 Lisp_Object converted_selection = Qnil;
|
|
582 Time local_selection_time;
|
|
583 Lisp_Object successful_p = Qnil;
|
|
584 int count;
|
|
585 struct device *d = get_device_from_display (event->display);
|
|
586
|
442
|
587 GCPRO2 (converted_selection, target_symbol);
|
428
|
588
|
|
589 selection_symbol = x_atom_to_symbol (d, event->selection);
|
442
|
590 target_symbol = x_atom_to_symbol (d, event->target);
|
428
|
591
|
442
|
592 #if 0 /* #### MULTIPLE doesn't work yet */
|
|
593 if (EQ (target_symbol, QMULTIPLE))
|
|
594 target_symbol = fetch_multiple_target (event);
|
428
|
595 #endif
|
|
596
|
442
|
597 temp_obj = Fget_selection_timestamp (selection_symbol);
|
|
598
|
|
599 if (NILP (temp_obj))
|
428
|
600 {
|
442
|
601 /* We don't appear to have the selection. */
|
428
|
602 x_decline_selection_request (event);
|
442
|
603
|
428
|
604 goto DONE_LABEL;
|
|
605 }
|
|
606
|
442
|
607 local_selection_time = * (Time *) XOPAQUE_DATA (temp_obj);
|
428
|
608
|
|
609 if (event->time != CurrentTime &&
|
|
610 local_selection_time > event->time)
|
|
611 {
|
|
612 /* Someone asked for the selection, and we have one, but not the one
|
|
613 they're looking for. */
|
|
614 x_decline_selection_request (event);
|
|
615 goto DONE_LABEL;
|
|
616 }
|
|
617
|
442
|
618 converted_selection = select_convert_out (selection_symbol,
|
|
619 target_symbol, Qnil);
|
|
620
|
|
621 /* #### Is this the right thing to do? I'm no X expert. -- ajh */
|
|
622 if (NILP (converted_selection))
|
|
623 {
|
|
624 /* We don't appear to have a selection in that data type. */
|
|
625 x_decline_selection_request (event);
|
|
626 goto DONE_LABEL;
|
|
627 }
|
|
628
|
428
|
629 count = specpdl_depth ();
|
|
630 record_unwind_protect (x_selection_request_lisp_error,
|
|
631 make_opaque_ptr (event));
|
|
632
|
442
|
633 {
|
647
|
634 UChar_Binary *data;
|
665
|
635 Bytecount size;
|
442
|
636 int format;
|
|
637 Atom type;
|
|
638 lisp_data_to_selection_data (d, converted_selection,
|
|
639 &data, &type, &size, &format);
|
428
|
640
|
442
|
641 x_reply_selection_request (event, format, data, size, type);
|
|
642 successful_p = Qt;
|
|
643 /* Tell x_selection_request_lisp_error() it's cool. */
|
|
644 event->type = 0;
|
1726
|
645 xfree (data, UChar_Binary *);
|
442
|
646 }
|
|
647
|
771
|
648 unbind_to (count);
|
428
|
649
|
|
650 DONE_LABEL:
|
|
651
|
|
652 UNGCPRO;
|
|
653
|
|
654 /* Let random lisp code notice that the selection has been asked for. */
|
|
655 {
|
|
656 Lisp_Object val = Vx_sent_selection_hooks;
|
|
657 if (!UNBOUNDP (val) && !NILP (val))
|
|
658 {
|
442
|
659 Lisp_Object rest;
|
428
|
660 if (CONSP (val) && !EQ (XCAR (val), Qlambda))
|
|
661 for (rest = val; !NILP (rest); rest = Fcdr (rest))
|
442
|
662 call3 (Fcar (rest), selection_symbol, target_symbol, successful_p);
|
428
|
663 else
|
442
|
664 call3 (val, selection_symbol, target_symbol, successful_p);
|
428
|
665 }
|
|
666 }
|
|
667 }
|
|
668
|
|
669
|
|
670 /* Called from the event-loop in response to a SelectionClear event.
|
|
671 */
|
|
672 void
|
|
673 x_handle_selection_clear (XSelectionClearEvent *event)
|
|
674 {
|
|
675 Display *display = event->display;
|
|
676 struct device *d = get_device_from_display (display);
|
|
677 Atom selection = event->selection;
|
|
678 Time changed_owner_time = event->time;
|
|
679
|
442
|
680 Lisp_Object selection_symbol, local_selection_time_lisp;
|
428
|
681 Time local_selection_time;
|
|
682
|
|
683 selection_symbol = x_atom_to_symbol (d, selection);
|
|
684
|
442
|
685 local_selection_time_lisp = Fget_selection_timestamp (selection_symbol);
|
428
|
686
|
442
|
687 /* We don't own the selection, so that's fine. */
|
|
688 if (NILP (local_selection_time_lisp))
|
|
689 return;
|
428
|
690
|
442
|
691 local_selection_time = * (Time *) XOPAQUE_DATA (local_selection_time_lisp);
|
428
|
692
|
|
693 /* This SelectionClear is for a selection that we no longer own, so we can
|
|
694 disregard it. (That is, we have reasserted the selection since this
|
|
695 request was generated.)
|
|
696 */
|
|
697 if (changed_owner_time != CurrentTime &&
|
|
698 local_selection_time > changed_owner_time)
|
|
699 return;
|
442
|
700
|
428
|
701 handle_selection_clear (selection_symbol);
|
|
702 }
|
|
703
|
|
704
|
|
705 /* This stuff is so that INCR selections are reentrant (that is, so we can
|
|
706 be servicing multiple INCR selection requests simultaneously). I haven't
|
|
707 actually tested that yet.
|
|
708 */
|
|
709
|
|
710 static int prop_location_tick;
|
|
711
|
|
712 static struct prop_location {
|
|
713 int tick;
|
|
714 Display *display;
|
|
715 Window window;
|
|
716 Atom property;
|
|
717 int desired_state;
|
|
718 struct prop_location *next;
|
|
719 } *for_whom_the_bell_tolls;
|
|
720
|
|
721
|
|
722 static int
|
|
723 property_deleted_p (void *tick)
|
|
724 {
|
|
725 struct prop_location *rest = for_whom_the_bell_tolls;
|
|
726 while (rest)
|
|
727 if (rest->tick == (long) tick)
|
|
728 return 0;
|
|
729 else
|
|
730 rest = rest->next;
|
|
731 return 1;
|
|
732 }
|
|
733
|
|
734 static int
|
|
735 waiting_for_other_props_on_window (Display *display, Window window)
|
|
736 {
|
|
737 struct prop_location *rest = for_whom_the_bell_tolls;
|
|
738 while (rest)
|
|
739 if (rest->display == display && rest->window == window)
|
|
740 return 1;
|
|
741 else
|
|
742 rest = rest->next;
|
|
743 return 0;
|
|
744 }
|
|
745
|
|
746
|
|
747 static int
|
|
748 expect_property_change (Display *display, Window window,
|
|
749 Atom property, int state)
|
|
750 {
|
|
751 struct prop_location *pl = xnew (struct prop_location);
|
|
752 pl->tick = ++prop_location_tick;
|
|
753 pl->display = display;
|
|
754 pl->window = window;
|
|
755 pl->property = property;
|
|
756 pl->desired_state = state;
|
|
757 pl->next = for_whom_the_bell_tolls;
|
|
758 for_whom_the_bell_tolls = pl;
|
|
759 return pl->tick;
|
|
760 }
|
|
761
|
|
762 static void
|
|
763 unexpect_property_change (int tick)
|
|
764 {
|
|
765 struct prop_location *prev = 0, *rest = for_whom_the_bell_tolls;
|
|
766 while (rest)
|
|
767 {
|
|
768 if (rest->tick == tick)
|
|
769 {
|
|
770 if (prev)
|
|
771 prev->next = rest->next;
|
|
772 else
|
|
773 for_whom_the_bell_tolls = rest->next;
|
1726
|
774 xfree (rest, struct prop_location *);
|
428
|
775 return;
|
|
776 }
|
|
777 prev = rest;
|
|
778 rest = rest->next;
|
|
779 }
|
|
780 }
|
|
781
|
|
782 static void
|
|
783 wait_for_property_change (long tick)
|
|
784 {
|
|
785 /* This function can GC */
|
|
786 wait_delaying_user_input (property_deleted_p, (void *) tick);
|
|
787 }
|
|
788
|
|
789
|
|
790 /* Called from the event-loop in response to a PropertyNotify event.
|
|
791 */
|
|
792 void
|
|
793 x_handle_property_notify (XPropertyEvent *event)
|
|
794 {
|
|
795 struct prop_location *prev = 0, *rest = for_whom_the_bell_tolls;
|
|
796 while (rest)
|
|
797 {
|
|
798 if (rest->property == event->atom &&
|
|
799 rest->window == event->window &&
|
|
800 rest->display == event->display &&
|
|
801 rest->desired_state == event->state)
|
|
802 {
|
|
803 #if 0
|
|
804 stderr_out ("Saw expected prop-%s on %s\n",
|
793
|
805 (event->state == PropertyDelete ? "delete" : "change"),
|
|
806 XSTRING_DATA
|
|
807 (XSYMBOL (x_atom_to_symbol
|
|
808 (get_device_from_display (event->display),
|
1726
|
809 event->atom))->name));
|
428
|
810 #endif
|
|
811 if (prev)
|
|
812 prev->next = rest->next;
|
|
813 else
|
|
814 for_whom_the_bell_tolls = rest->next;
|
1726
|
815 xfree (rest, struct prop_location *);
|
428
|
816 return;
|
|
817 }
|
|
818 prev = rest;
|
|
819 rest = rest->next;
|
|
820 }
|
|
821 #if 0
|
|
822 stderr_out ("Saw UNexpected prop-%s on %s\n",
|
793
|
823 (event->state == PropertyDelete ? "delete" : "change"),
|
|
824 XSTRING_DATA (XSYMBOL (x_atom_to_symbol
|
|
825 (get_device_from_display (event->display),
|
|
826 event->atom))->name));
|
428
|
827 #endif
|
|
828 }
|
|
829
|
|
830
|
|
831
|
|
832 #if 0 /* #### MULTIPLE doesn't work yet */
|
|
833
|
|
834 static Lisp_Object
|
|
835 fetch_multiple_target (XSelectionRequestEvent *event)
|
|
836 {
|
|
837 /* This function can GC */
|
|
838 Display *display = event->display;
|
|
839 Window window = event->requestor;
|
|
840 Atom target = event->target;
|
|
841 Atom selection_atom = event->selection;
|
|
842 int result;
|
|
843
|
|
844 return
|
|
845 Fcons (QMULTIPLE,
|
|
846 x_get_window_property_as_lisp_data (display, window, target,
|
|
847 QMULTIPLE,
|
|
848 selection_atom));
|
|
849 }
|
|
850
|
|
851 static Lisp_Object
|
|
852 copy_multiple_data (Lisp_Object obj)
|
|
853 {
|
|
854 Lisp_Object vec;
|
665
|
855 Elemcount i;
|
|
856 Elemcount len;
|
428
|
857 if (CONSP (obj))
|
|
858 return Fcons (XCAR (obj), copy_multiple_data (XCDR (obj)));
|
|
859
|
|
860 CHECK_VECTOR (obj);
|
|
861 len = XVECTOR_LENGTH (obj);
|
|
862 vec = make_vector (len, Qnil);
|
|
863 for (i = 0; i < len; i++)
|
|
864 {
|
|
865 Lisp_Object vec2 = XVECTOR_DATA (obj) [i];
|
|
866 CHECK_VECTOR (vec2);
|
|
867 if (XVECTOR_LENGTH (vec2) != 2)
|
563
|
868 sferror ("vectors must be of length 2", vec2);
|
428
|
869 XVECTOR_DATA (vec) [i] = make_vector (2, Qnil);
|
|
870 XVECTOR_DATA (XVECTOR_DATA (vec) [i]) [0] = XVECTOR_DATA (vec2) [0];
|
|
871 XVECTOR_DATA (XVECTOR_DATA (vec) [i]) [1] = XVECTOR_DATA (vec2) [1];
|
|
872 }
|
|
873 return vec;
|
|
874 }
|
|
875
|
|
876 #endif /* 0 */
|
|
877
|
|
878
|
|
879 static Window reading_selection_reply;
|
|
880 static Atom reading_which_selection;
|
|
881 static int selection_reply_timed_out;
|
|
882
|
|
883 static int
|
|
884 selection_reply_done (void *ignore)
|
|
885 {
|
|
886 return !reading_selection_reply;
|
|
887 }
|
|
888
|
|
889 static Lisp_Object Qx_selection_reply_timeout_internal;
|
|
890
|
|
891 DEFUN ("x-selection-reply-timeout-internal", Fx_selection_reply_timeout_internal,
|
|
892 1, 1, 0, /*
|
|
893 */
|
|
894 (arg))
|
|
895 {
|
|
896 selection_reply_timed_out = 1;
|
|
897 reading_selection_reply = 0;
|
|
898 return Qnil;
|
|
899 }
|
|
900
|
|
901
|
|
902 /* Do protocol to read selection-data from the server.
|
|
903 Converts this to lisp data and returns it.
|
|
904 */
|
|
905 static Lisp_Object
|
|
906 x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type)
|
|
907 {
|
|
908 /* This function can GC */
|
|
909 struct device *d = decode_x_device (Qnil);
|
|
910 Display *display = DEVICE_X_DISPLAY (d);
|
|
911 struct frame *sel_frame = selected_frame ();
|
|
912 Window requestor_window = XtWindow (FRAME_X_TEXT_WIDGET (sel_frame));
|
|
913 Time requestor_time = DEVICE_X_MOUSE_TIMESTAMP (d);
|
|
914 Atom target_property = DEVICE_XATOM_EMACS_TMP (d);
|
|
915 Atom selection_atom = symbol_to_x_atom (d, selection_symbol, 0);
|
|
916 int speccount;
|
|
917 Atom type_atom = symbol_to_x_atom (d, (CONSP (target_type) ?
|
|
918 XCAR (target_type) : target_type), 0);
|
|
919
|
|
920 XConvertSelection (display, selection_atom, type_atom, target_property,
|
|
921 requestor_window, requestor_time);
|
|
922
|
|
923 /* Block until the reply has been read. */
|
|
924 reading_selection_reply = requestor_window;
|
|
925 reading_which_selection = selection_atom;
|
|
926 selection_reply_timed_out = 0;
|
|
927
|
|
928 speccount = specpdl_depth ();
|
|
929
|
|
930 /* add a timeout handler */
|
|
931 if (x_selection_timeout > 0)
|
|
932 {
|
|
933 Lisp_Object id = Fadd_timeout (make_int (x_selection_timeout),
|
|
934 Qx_selection_reply_timeout_internal,
|
|
935 Qnil, Qnil);
|
|
936 record_unwind_protect (Fdisable_timeout, id);
|
|
937 }
|
|
938
|
|
939 /* This is ^Gable */
|
|
940 wait_delaying_user_input (selection_reply_done, 0);
|
|
941
|
|
942 if (selection_reply_timed_out)
|
563
|
943 signal_error (Qselection_conversion_error, "timed out waiting for reply from selection owner", Qunbound);
|
428
|
944
|
771
|
945 unbind_to (speccount);
|
428
|
946
|
|
947 /* otherwise, the selection is waiting for us on the requested property. */
|
442
|
948
|
|
949 return select_convert_in (selection_symbol,
|
|
950 target_type,
|
|
951 x_get_window_property_as_lisp_data(display,
|
|
952 requestor_window,
|
|
953 target_property,
|
|
954 target_type,
|
|
955 selection_atom));
|
428
|
956 }
|
|
957
|
|
958
|
|
959 static void
|
|
960 x_get_window_property (Display *display, Window window, Atom property,
|
665
|
961 UChar_Binary **data_ret, Bytecount *bytes_ret,
|
428
|
962 Atom *actual_type_ret, int *actual_format_ret,
|
|
963 unsigned long *actual_size_ret, int delete_p)
|
|
964 {
|
665
|
965 Bytecount total_size;
|
428
|
966 unsigned long bytes_remaining;
|
665
|
967 Bytecount offset = 0;
|
647
|
968 UChar_Binary *tmp_data = 0;
|
428
|
969 int result;
|
665
|
970 Bytecount buffer_size = SELECTION_QUANTUM (display);
|
428
|
971 if (buffer_size > MAX_SELECTION_QUANTUM) buffer_size = MAX_SELECTION_QUANTUM;
|
|
972
|
|
973 /* First probe the thing to find out how big it is. */
|
|
974 result = XGetWindowProperty (display, window, property,
|
|
975 0, 0, False, AnyPropertyType,
|
|
976 actual_type_ret, actual_format_ret,
|
|
977 actual_size_ret,
|
|
978 &bytes_remaining, &tmp_data);
|
|
979 if (result != Success)
|
|
980 {
|
|
981 *data_ret = 0;
|
|
982 *bytes_ret = 0;
|
|
983 return;
|
|
984 }
|
|
985 XFree ((char *) tmp_data);
|
|
986
|
|
987 if (*actual_type_ret == None || *actual_format_ret == 0)
|
|
988 {
|
|
989 if (delete_p) XDeleteProperty (display, window, property);
|
|
990 *data_ret = 0;
|
|
991 *bytes_ret = 0;
|
|
992 return;
|
|
993 }
|
|
994
|
|
995 total_size = bytes_remaining + 1;
|
647
|
996 *data_ret = (UChar_Binary *) xmalloc (total_size);
|
428
|
997
|
|
998 /* Now read, until we've gotten it all. */
|
|
999 while (bytes_remaining)
|
|
1000 {
|
|
1001 #if 0
|
665
|
1002 Bytecount last = bytes_remaining;
|
428
|
1003 #endif
|
|
1004 result =
|
|
1005 XGetWindowProperty (display, window, property,
|
|
1006 offset/4, buffer_size/4,
|
|
1007 (delete_p ? True : False),
|
|
1008 AnyPropertyType,
|
|
1009 actual_type_ret, actual_format_ret,
|
|
1010 actual_size_ret, &bytes_remaining, &tmp_data);
|
|
1011 #if 0
|
|
1012 stderr_out ("<< read %d\n", last-bytes_remaining);
|
|
1013 #endif
|
|
1014 /* If this doesn't return Success at this point, it means that
|
|
1015 some clod deleted the selection while we were in the midst of
|
|
1016 reading it. Deal with that, I guess....
|
|
1017 */
|
|
1018 if (result != Success) break;
|
|
1019 *actual_size_ret *= *actual_format_ret / 8;
|
|
1020 memcpy ((*data_ret) + offset, tmp_data, *actual_size_ret);
|
|
1021 offset += *actual_size_ret;
|
|
1022 XFree ((char *) tmp_data);
|
|
1023 }
|
|
1024 *bytes_ret = offset;
|
|
1025 }
|
|
1026
|
|
1027
|
|
1028 static void
|
|
1029 receive_incremental_selection (Display *display, Window window, Atom property,
|
|
1030 /* this one is for error messages only */
|
|
1031 Lisp_Object target_type,
|
665
|
1032 Bytecount min_size_bytes,
|
647
|
1033 UChar_Binary **data_ret,
|
665
|
1034 Bytecount *size_bytes_ret,
|
428
|
1035 Atom *type_ret, int *format_ret,
|
|
1036 unsigned long *size_ret)
|
|
1037 {
|
|
1038 /* This function can GC */
|
665
|
1039 Bytecount offset = 0;
|
428
|
1040 int prop_id;
|
|
1041 *size_bytes_ret = min_size_bytes;
|
647
|
1042 *data_ret = (UChar_Binary *) xmalloc (*size_bytes_ret);
|
428
|
1043 #if 0
|
|
1044 stderr_out ("\nread INCR %d\n", min_size_bytes);
|
|
1045 #endif
|
|
1046 /* At this point, we have read an INCR property, and deleted it (which
|
|
1047 is how we ack its receipt: the sending window will be selecting
|
|
1048 PropertyNotify events on our window to notice this).
|
|
1049
|
|
1050 Now, we must loop, waiting for the sending window to put a value on
|
|
1051 that property, then reading the property, then deleting it to ack.
|
|
1052 We are done when the sender places a property of length 0.
|
|
1053 */
|
|
1054 prop_id = expect_property_change (display, window, property,
|
|
1055 PropertyNewValue);
|
|
1056 while (1)
|
|
1057 {
|
647
|
1058 UChar_Binary *tmp_data;
|
665
|
1059 Bytecount tmp_size_bytes;
|
428
|
1060 wait_for_property_change (prop_id);
|
|
1061 /* expect it again immediately, because x_get_window_property may
|
|
1062 .. no it won't, I don't get it.
|
|
1063 .. Ok, I get it now, the Xt code that implements INCR is broken.
|
|
1064 */
|
|
1065 prop_id = expect_property_change (display, window, property,
|
|
1066 PropertyNewValue);
|
|
1067 x_get_window_property (display, window, property,
|
|
1068 &tmp_data, &tmp_size_bytes,
|
|
1069 type_ret, format_ret, size_ret, 1);
|
|
1070
|
|
1071 if (tmp_size_bytes == 0) /* we're done */
|
|
1072 {
|
|
1073 #if 0
|
|
1074 stderr_out (" read INCR done\n");
|
|
1075 #endif
|
|
1076 unexpect_property_change (prop_id);
|
1726
|
1077 if (tmp_data)
|
|
1078 xfree (tmp_data, UChar_Binary *);
|
428
|
1079 break;
|
|
1080 }
|
|
1081 #if 0
|
|
1082 stderr_out (" read INCR %d\n", tmp_size_bytes);
|
|
1083 #endif
|
|
1084 if (*size_bytes_ret < offset + tmp_size_bytes)
|
|
1085 {
|
|
1086 #if 0
|
|
1087 stderr_out (" read INCR realloc %d -> %d\n",
|
|
1088 *size_bytes_ret, offset + tmp_size_bytes);
|
|
1089 #endif
|
|
1090 *size_bytes_ret = offset + tmp_size_bytes;
|
647
|
1091 *data_ret = (UChar_Binary *) xrealloc (*data_ret, *size_bytes_ret);
|
428
|
1092 }
|
|
1093 memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes);
|
|
1094 offset += tmp_size_bytes;
|
1726
|
1095 xfree (tmp_data, UChar_Binary *);
|
428
|
1096 }
|
|
1097 }
|
|
1098
|
|
1099
|
|
1100 static Lisp_Object
|
|
1101 x_get_window_property_as_lisp_data (Display *display,
|
|
1102 Window window,
|
|
1103 Atom property,
|
|
1104 /* next two for error messages only */
|
|
1105 Lisp_Object target_type,
|
|
1106 Atom selection_atom)
|
|
1107 {
|
|
1108 /* This function can GC */
|
|
1109 Atom actual_type;
|
|
1110 int actual_format;
|
|
1111 unsigned long actual_size;
|
647
|
1112 UChar_Binary *data = NULL;
|
665
|
1113 Bytecount bytes = 0;
|
428
|
1114 Lisp_Object val;
|
|
1115 struct device *d = get_device_from_display (display);
|
|
1116
|
|
1117 x_get_window_property (display, window, property, &data, &bytes,
|
|
1118 &actual_type, &actual_format, &actual_size, 1);
|
|
1119 if (! data)
|
|
1120 {
|
|
1121 if (XGetSelectionOwner (display, selection_atom))
|
|
1122 /* there is a selection owner */
|
563
|
1123 signal_error (Qselection_conversion_error,
|
|
1124 "selection owner couldn't convert",
|
|
1125 Fcons (Qunbound,
|
|
1126 Fcons (x_atom_to_symbol (d, selection_atom),
|
|
1127 actual_type ?
|
|
1128 list2 (target_type,
|
|
1129 x_atom_to_symbol (d, actual_type)) :
|
|
1130 list1 (target_type))));
|
428
|
1131 else
|
563
|
1132 signal_error (Qselection_conversion_error,
|
|
1133 "no selection",
|
|
1134 x_atom_to_symbol (d, selection_atom));
|
428
|
1135 }
|
|
1136
|
|
1137 if (actual_type == DEVICE_XATOM_INCR (d))
|
|
1138 {
|
|
1139 /* Ok, that data wasn't *the* data, it was just the beginning. */
|
|
1140
|
665
|
1141 Bytecount min_size_bytes =
|
647
|
1142 /* careful here. */
|
665
|
1143 (Bytecount) (* ((unsigned int *) data));
|
1726
|
1144 xfree (data, UChar_Binary *);
|
428
|
1145 receive_incremental_selection (display, window, property, target_type,
|
|
1146 min_size_bytes, &data, &bytes,
|
|
1147 &actual_type, &actual_format,
|
|
1148 &actual_size);
|
|
1149 }
|
|
1150
|
|
1151 /* It's been read. Now convert it to a lisp object in some semi-rational
|
|
1152 manner. */
|
|
1153 val = selection_data_to_lisp_data (d, data, bytes,
|
|
1154 actual_type, actual_format);
|
|
1155
|
1726
|
1156 xfree (data, UChar_Binary *);
|
428
|
1157 return val;
|
|
1158 }
|
|
1159
|
|
1160
|
|
1161 /* Called from the event loop to handle SelectionNotify events.
|
|
1162 I don't think this needs to be reentrant.
|
|
1163 */
|
|
1164 void
|
|
1165 x_handle_selection_notify (XSelectionEvent *event)
|
|
1166 {
|
|
1167 if (! reading_selection_reply)
|
|
1168 message ("received an unexpected SelectionNotify event");
|
|
1169 else if (event->requestor != reading_selection_reply)
|
|
1170 message ("received a SelectionNotify event for the wrong window");
|
|
1171 else if (event->selection != reading_which_selection)
|
|
1172 message ("received the wrong selection type in SelectionNotify!");
|
|
1173 else
|
|
1174 reading_selection_reply = 0; /* we're done now. */
|
|
1175 }
|
|
1176
|
|
1177 static void
|
|
1178 x_disown_selection (Lisp_Object selection, Lisp_Object timeval)
|
|
1179 {
|
|
1180 struct device *d = decode_x_device (Qnil);
|
|
1181 Display *display = DEVICE_X_DISPLAY (d);
|
|
1182 Time timestamp;
|
|
1183 Atom selection_atom;
|
|
1184
|
|
1185 CHECK_SYMBOL (selection);
|
|
1186 if (NILP (timeval))
|
|
1187 timestamp = DEVICE_X_MOUSE_TIMESTAMP (d);
|
|
1188 else
|
|
1189 {
|
|
1190 /* #### This is bogus. See the comment above about problems
|
|
1191 on OSF/1 and DEC Alphas. Yet another reason why it sucks
|
|
1192 to have the implementation (i.e. cons of two 16-bit
|
|
1193 integers) exposed. */
|
|
1194 time_t the_time;
|
|
1195 lisp_to_time (timeval, &the_time);
|
|
1196 timestamp = (Time) the_time;
|
|
1197 }
|
|
1198
|
|
1199 selection_atom = symbol_to_x_atom (d, selection, 0);
|
|
1200
|
|
1201 XSetSelectionOwner (display, selection_atom, None, timestamp);
|
|
1202 }
|
|
1203
|
|
1204 static Lisp_Object
|
442
|
1205 x_selection_exists_p (Lisp_Object selection,
|
|
1206 Lisp_Object selection_type)
|
428
|
1207 {
|
|
1208 struct device *d = decode_x_device (Qnil);
|
|
1209 Display *dpy = DEVICE_X_DISPLAY (d);
|
|
1210 return XGetSelectionOwner (dpy, symbol_to_x_atom (d, selection, 0)) != None ?
|
|
1211 Qt : Qnil;
|
|
1212 }
|
|
1213
|
|
1214
|
|
1215 #ifdef CUT_BUFFER_SUPPORT
|
|
1216
|
|
1217 static int cut_buffers_initialized; /* Whether we're sure they all exist */
|
|
1218
|
|
1219 /* Ensure that all 8 cut buffers exist. ICCCM says we gotta... */
|
|
1220 static void
|
|
1221 initialize_cut_buffers (Display *display, Window window)
|
|
1222 {
|
442
|
1223 static unsigned const char * const data = (unsigned const char *) "";
|
428
|
1224 #define FROB(atom) XChangeProperty (display, window, atom, XA_STRING, 8, \
|
|
1225 PropModeAppend, data, 0)
|
|
1226 FROB (XA_CUT_BUFFER0);
|
|
1227 FROB (XA_CUT_BUFFER1);
|
|
1228 FROB (XA_CUT_BUFFER2);
|
|
1229 FROB (XA_CUT_BUFFER3);
|
|
1230 FROB (XA_CUT_BUFFER4);
|
|
1231 FROB (XA_CUT_BUFFER5);
|
|
1232 FROB (XA_CUT_BUFFER6);
|
|
1233 FROB (XA_CUT_BUFFER7);
|
|
1234 #undef FROB
|
|
1235 cut_buffers_initialized = 1;
|
|
1236 }
|
|
1237
|
|
1238 #define CHECK_CUTBUFFER(symbol) do { \
|
|
1239 CHECK_SYMBOL (symbol); \
|
|
1240 if (! (EQ (symbol, QCUT_BUFFER0) || \
|
|
1241 EQ (symbol, QCUT_BUFFER1) || \
|
|
1242 EQ (symbol, QCUT_BUFFER2) || \
|
|
1243 EQ (symbol, QCUT_BUFFER3) || \
|
|
1244 EQ (symbol, QCUT_BUFFER4) || \
|
|
1245 EQ (symbol, QCUT_BUFFER5) || \
|
|
1246 EQ (symbol, QCUT_BUFFER6) || \
|
|
1247 EQ (symbol, QCUT_BUFFER7))) \
|
563
|
1248 invalid_constant ("Doesn't name a cutbuffer", symbol); \
|
428
|
1249 } while (0)
|
|
1250
|
|
1251 DEFUN ("x-get-cutbuffer-internal", Fx_get_cutbuffer_internal, 1, 1, 0, /*
|
|
1252 Return the value of the named CUTBUFFER (typically CUT_BUFFER0).
|
|
1253 */
|
|
1254 (cutbuffer))
|
|
1255 {
|
|
1256 struct device *d = decode_x_device (Qnil);
|
|
1257 Display *display = DEVICE_X_DISPLAY (d);
|
|
1258 Window window = RootWindow (display, 0); /* Cutbuffers are on frame 0 */
|
|
1259 Atom cut_buffer_atom;
|
647
|
1260 UChar_Binary *data;
|
665
|
1261 Bytecount bytes;
|
428
|
1262 Atom type;
|
|
1263 int format;
|
|
1264 unsigned long size;
|
|
1265 Lisp_Object ret;
|
|
1266
|
|
1267 CHECK_CUTBUFFER (cutbuffer);
|
|
1268 cut_buffer_atom = symbol_to_x_atom (d, cutbuffer, 0);
|
|
1269
|
|
1270 x_get_window_property (display, window, cut_buffer_atom, &data, &bytes,
|
|
1271 &type, &format, &size, 0);
|
|
1272 if (!data) return Qnil;
|
|
1273
|
|
1274 if (format != 8 || type != XA_STRING)
|
563
|
1275 invalid_state_2 ("Cut buffer doesn't contain 8-bit STRING data",
|
|
1276 x_atom_to_symbol (d, type),
|
|
1277 make_int (format));
|
428
|
1278
|
|
1279 /* We cheat - if the string contains an ESC character, that's
|
|
1280 technically not allowed in a STRING, so we assume it's
|
|
1281 COMPOUND_TEXT that we stored there ourselves earlier,
|
|
1282 in x-store-cutbuffer-internal */
|
|
1283 ret = (bytes ?
|
647
|
1284 make_ext_string ((Extbyte *) data, bytes,
|
428
|
1285 memchr (data, 0x1b, bytes) ?
|
440
|
1286 Qctext : Qbinary)
|
428
|
1287 : Qnil);
|
1726
|
1288 xfree (data, UChar_Binary *);
|
428
|
1289 return ret;
|
|
1290 }
|
|
1291
|
|
1292
|
|
1293 DEFUN ("x-store-cutbuffer-internal", Fx_store_cutbuffer_internal, 2, 2, 0, /*
|
|
1294 Set the value of the named CUTBUFFER (typically CUT_BUFFER0) to STRING.
|
|
1295 */
|
|
1296 (cutbuffer, string))
|
|
1297 {
|
|
1298 struct device *d = decode_x_device (Qnil);
|
|
1299 Display *display = DEVICE_X_DISPLAY (d);
|
|
1300 Window window = RootWindow (display, 0); /* Cutbuffers are on frame 0 */
|
|
1301 Atom cut_buffer_atom;
|
867
|
1302 const Ibyte *data = XSTRING_DATA (string);
|
444
|
1303 Bytecount bytes = XSTRING_LENGTH (string);
|
|
1304 Bytecount bytes_remaining;
|
665
|
1305 Bytecount max_bytes = SELECTION_QUANTUM (display);
|
428
|
1306 #ifdef MULE
|
867
|
1307 const Ibyte *ptr, *end;
|
428
|
1308 enum { ASCII, LATIN_1, WORLD } chartypes = ASCII;
|
|
1309 #endif
|
|
1310
|
|
1311 if (max_bytes > MAX_SELECTION_QUANTUM)
|
|
1312 max_bytes = MAX_SELECTION_QUANTUM;
|
|
1313
|
|
1314 CHECK_CUTBUFFER (cutbuffer);
|
|
1315 CHECK_STRING (string);
|
|
1316 cut_buffer_atom = symbol_to_x_atom (d, cutbuffer, 0);
|
|
1317
|
|
1318 if (! cut_buffers_initialized)
|
|
1319 initialize_cut_buffers (display, window);
|
|
1320
|
|
1321 /* We use the STRING encoding (Latin-1 only) if we can, else COMPOUND_TEXT.
|
|
1322 We cheat and use type = `STRING' even when using COMPOUND_TEXT.
|
|
1323 The ICCCM requires that this be so, and other clients assume it,
|
|
1324 as we do ourselves in initialize_cut_buffers. */
|
|
1325
|
|
1326 #ifdef MULE
|
|
1327 /* Optimize for the common ASCII case */
|
|
1328 for (ptr = data, end = ptr + bytes; ptr <= end; )
|
|
1329 {
|
826
|
1330 if (byte_ascii_p (*ptr))
|
428
|
1331 {
|
|
1332 ptr++;
|
|
1333 continue;
|
|
1334 }
|
|
1335
|
|
1336 if ((*ptr) == LEADING_BYTE_LATIN_ISO8859_1 ||
|
|
1337 (*ptr) == LEADING_BYTE_CONTROL_1)
|
|
1338 {
|
|
1339 chartypes = LATIN_1;
|
|
1340 ptr += 2;
|
|
1341 continue;
|
|
1342 }
|
|
1343
|
|
1344 chartypes = WORLD;
|
|
1345 break;
|
|
1346 }
|
|
1347
|
|
1348 if (chartypes == LATIN_1)
|
440
|
1349 TO_EXTERNAL_FORMAT (LISP_STRING, string,
|
|
1350 ALLOCA, (data, bytes),
|
|
1351 Qbinary);
|
428
|
1352 else if (chartypes == WORLD)
|
440
|
1353 TO_EXTERNAL_FORMAT (LISP_STRING, string,
|
|
1354 ALLOCA, (data, bytes),
|
|
1355 Qctext);
|
428
|
1356 #endif /* MULE */
|
|
1357
|
|
1358 bytes_remaining = bytes;
|
|
1359
|
|
1360 while (bytes_remaining)
|
|
1361 {
|
665
|
1362 Bytecount chunk =
|
647
|
1363 bytes_remaining < max_bytes ? bytes_remaining : max_bytes;
|
428
|
1364 XChangeProperty (display, window, cut_buffer_atom, XA_STRING, 8,
|
|
1365 (bytes_remaining == bytes
|
|
1366 ? PropModeReplace : PropModeAppend),
|
|
1367 data, chunk);
|
|
1368 data += chunk;
|
|
1369 bytes_remaining -= chunk;
|
|
1370 }
|
|
1371 return string;
|
|
1372 }
|
|
1373
|
|
1374
|
|
1375 DEFUN ("x-rotate-cutbuffers-internal", Fx_rotate_cutbuffers_internal, 1, 1, 0, /*
|
|
1376 Rotate the values of the cutbuffers by the given number of steps;
|
|
1377 positive means move values forward, negative means backward.
|
|
1378 */
|
|
1379 (n))
|
|
1380 {
|
|
1381 struct device *d = decode_x_device (Qnil);
|
|
1382 Display *display = DEVICE_X_DISPLAY (d);
|
|
1383 Window window = RootWindow (display, 0); /* Cutbuffers are on frame 0 */
|
|
1384 Atom props [8];
|
|
1385
|
|
1386 CHECK_INT (n);
|
|
1387 if (XINT (n) == 0)
|
|
1388 return n;
|
|
1389 if (! cut_buffers_initialized)
|
|
1390 initialize_cut_buffers (display, window);
|
|
1391 props[0] = XA_CUT_BUFFER0;
|
|
1392 props[1] = XA_CUT_BUFFER1;
|
|
1393 props[2] = XA_CUT_BUFFER2;
|
|
1394 props[3] = XA_CUT_BUFFER3;
|
|
1395 props[4] = XA_CUT_BUFFER4;
|
|
1396 props[5] = XA_CUT_BUFFER5;
|
|
1397 props[6] = XA_CUT_BUFFER6;
|
|
1398 props[7] = XA_CUT_BUFFER7;
|
|
1399 XRotateWindowProperties (display, window, props, 8, XINT (n));
|
|
1400 return n;
|
|
1401 }
|
|
1402
|
|
1403 #endif /* CUT_BUFFER_SUPPORT */
|
|
1404
|
|
1405
|
|
1406
|
|
1407 /************************************************************************/
|
|
1408 /* initialization */
|
|
1409 /************************************************************************/
|
|
1410
|
|
1411 void
|
440
|
1412 syms_of_select_x (void)
|
428
|
1413 {
|
|
1414
|
|
1415 #ifdef CUT_BUFFER_SUPPORT
|
|
1416 DEFSUBR (Fx_get_cutbuffer_internal);
|
|
1417 DEFSUBR (Fx_store_cutbuffer_internal);
|
|
1418 DEFSUBR (Fx_rotate_cutbuffers_internal);
|
|
1419 #endif /* CUT_BUFFER_SUPPORT */
|
|
1420
|
|
1421 /* Unfortunately, timeout handlers must be lisp functions. */
|
563
|
1422 DEFSYMBOL (Qx_selection_reply_timeout_internal);
|
428
|
1423 DEFSUBR (Fx_selection_reply_timeout_internal);
|
|
1424
|
|
1425 #ifdef CUT_BUFFER_SUPPORT
|
|
1426 defsymbol (&QCUT_BUFFER0, "CUT_BUFFER0");
|
|
1427 defsymbol (&QCUT_BUFFER1, "CUT_BUFFER1");
|
|
1428 defsymbol (&QCUT_BUFFER2, "CUT_BUFFER2");
|
|
1429 defsymbol (&QCUT_BUFFER3, "CUT_BUFFER3");
|
|
1430 defsymbol (&QCUT_BUFFER4, "CUT_BUFFER4");
|
|
1431 defsymbol (&QCUT_BUFFER5, "CUT_BUFFER5");
|
|
1432 defsymbol (&QCUT_BUFFER6, "CUT_BUFFER6");
|
|
1433 defsymbol (&QCUT_BUFFER7, "CUT_BUFFER7");
|
|
1434 #endif /* CUT_BUFFER_SUPPORT */
|
|
1435 }
|
|
1436
|
|
1437 void
|
|
1438 console_type_create_select_x (void)
|
|
1439 {
|
|
1440 CONSOLE_HAS_METHOD (x, own_selection);
|
|
1441 CONSOLE_HAS_METHOD (x, disown_selection);
|
|
1442 CONSOLE_HAS_METHOD (x, get_foreign_selection);
|
|
1443 CONSOLE_HAS_METHOD (x, selection_exists_p);
|
|
1444 }
|
|
1445
|
|
1446 void
|
440
|
1447 reinit_vars_of_select_x (void)
|
428
|
1448 {
|
|
1449 reading_selection_reply = 0;
|
|
1450 reading_which_selection = 0;
|
|
1451 selection_reply_timed_out = 0;
|
|
1452 for_whom_the_bell_tolls = 0;
|
|
1453 prop_location_tick = 0;
|
|
1454 }
|
|
1455
|
|
1456 void
|
440
|
1457 vars_of_select_x (void)
|
428
|
1458 {
|
440
|
1459 reinit_vars_of_select_x ();
|
428
|
1460
|
|
1461 #ifdef CUT_BUFFER_SUPPORT
|
|
1462 cut_buffers_initialized = 0;
|
|
1463 Fprovide (intern ("cut-buffer"));
|
|
1464 #endif
|
|
1465
|
|
1466 DEFVAR_LISP ("x-sent-selection-hooks", &Vx_sent_selection_hooks /*
|
|
1467 A function or functions to be called after we have responded to some
|
|
1468 other client's request for the value of a selection that we own. The
|
|
1469 function(s) will be called with four arguments:
|
|
1470 - the name of the selection (typically PRIMARY, SECONDARY, or CLIPBOARD);
|
|
1471 - the name of the selection-type which we were requested to convert the
|
|
1472 selection into before sending (for example, STRING or LENGTH);
|
|
1473 - and whether we successfully transmitted the selection.
|
|
1474 We might have failed (and declined the request) for any number of reasons,
|
|
1475 including being asked for a selection that we no longer own, or being asked
|
|
1476 to convert into a type that we don't know about or that is inappropriate.
|
|
1477 This hook doesn't let you change the behavior of emacs's selection replies,
|
|
1478 it merely informs you that they have happened.
|
|
1479 */ );
|
|
1480 Vx_sent_selection_hooks = Qunbound;
|
|
1481
|
|
1482 DEFVAR_INT ("x-selection-timeout", &x_selection_timeout /*
|
|
1483 If the selection owner doesn't reply in this many seconds, we give up.
|
|
1484 A value of 0 means wait as long as necessary. This is initialized from the
|
|
1485 \"*selectionTimeout\" resource (which is expressed in milliseconds).
|
|
1486 */ );
|
|
1487 x_selection_timeout = 0;
|
456
|
1488
|
|
1489 DEFVAR_BOOL ("x-selection-strict-motif-ownership", &x_selection_strict_motif_ownership /*
|
863
|
1490 *If nil and XEmacs already owns the clipboard, don't own it again in the
|
456
|
1491 Motif way. Owning the selection on the Motif way does a huge amount of
|
|
1492 X protocol, and it makes killing text incredibly slow when using an
|
|
1493 X terminal. However, when enabled Motif text fields don't bother to look up
|
|
1494 the new value, and you can't Copy from a buffer, Paste into a text
|
|
1495 field, then Copy something else from the buffer and paste it into the
|
|
1496 text field; it pastes the first thing again.
|
|
1497 */ );
|
|
1498 x_selection_strict_motif_ownership = 1;
|
428
|
1499 }
|
|
1500
|
|
1501 void
|
440
|
1502 Xatoms_of_select_x (struct device *d)
|
428
|
1503 {
|
|
1504 Display *D = DEVICE_X_DISPLAY (d);
|
|
1505
|
|
1506 /* Non-predefined atoms that we might end up using a lot */
|
|
1507 DEVICE_XATOM_CLIPBOARD (d) = XInternAtom (D, "CLIPBOARD", False);
|
|
1508 DEVICE_XATOM_TIMESTAMP (d) = XInternAtom (D, "TIMESTAMP", False);
|
|
1509 DEVICE_XATOM_TEXT (d) = XInternAtom (D, "TEXT", False);
|
|
1510 DEVICE_XATOM_DELETE (d) = XInternAtom (D, "DELETE", False);
|
|
1511 DEVICE_XATOM_MULTIPLE (d) = XInternAtom (D, "MULTIPLE", False);
|
|
1512 DEVICE_XATOM_INCR (d) = XInternAtom (D, "INCR", False);
|
|
1513 DEVICE_XATOM_TARGETS (d) = XInternAtom (D, "TARGETS", False);
|
|
1514 DEVICE_XATOM_NULL (d) = XInternAtom (D, "NULL", False);
|
|
1515 DEVICE_XATOM_ATOM_PAIR (d) = XInternAtom (D, "ATOM_PAIR", False);
|
|
1516 DEVICE_XATOM_COMPOUND_TEXT (d) = XInternAtom (D, "COMPOUND_TEXT", False);
|
442
|
1517
|
|
1518 /* #### I don't like the looks of this... what is it for? - ajh */
|
428
|
1519 DEVICE_XATOM_EMACS_TMP (d) = XInternAtom (D, "_EMACS_TMP_", False);
|
|
1520 }
|