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