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