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