1 /* X Selection processing for emacs
2 Copyright (C) 1990, 1992 Free Software Foundation.
4 This file is part of GNU Emacs.
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
28 /* Macros for X Selections */
29 #define MAX_SELECTION(dpy) (((dpy)->max_request_size << 2) - 100)
30 #define SELECTION_LENGTH(len,format) ((len) * ((format) >> 2))
32 /* The last 23 bits of the timestamp of the last mouse button event. */
33 extern Time mouse_timestamp;
35 /* An expedient hack! Fix this! */
36 #define last_event_timestamp CurrentTime
38 /* t if a mouse button is depressed. */
39 extern Lisp_Object Vmouse_grabbed;
41 /* When emacs became the PRIMARY selection owner. */
42 Time x_begin_selection_own;
44 /* When emacs became the SECONDARY selection owner. */
45 Time x_begin_secondary_selection_own;
47 /* When emacs became the CLIPBOARD selection owner. */
48 Time x_begin_clipboard_own;
50 /* The value of the current CLIPBOARD selection. */
51 Lisp_Object Vx_clipboard_value;
53 /* The value of the current PRIMARY selection. */
54 Lisp_Object Vx_selection_value;
56 /* The value of the current SECONDARY selection. */
57 Lisp_Object Vx_secondary_selection_value;
59 /* Types of selections we may make. */
60 Lisp_Object Qprimary, Qsecondary, Qclipboard;
62 /* Emacs' selection property identifiers. */
63 Atom Xatom_emacs_selection;
64 Atom Xatom_emacs_secondary_selection;
66 /* Clipboard selection atom. */
67 Atom Xatom_clipboard_selection;
72 /* Atom for indicating incremental selection transfer. */
73 Atom Xatom_incremental;
75 /* Atom for indicating multiple selection request list */
78 /* Atom for what targets emacs handles. */
81 /* Atom for indicating timstamp selection request */
84 /* Atom requesting we delete our selection. */
87 /* Selection magic. */
88 Atom Xatom_insert_selection;
90 /* Type of property for INSERT_SELECTION. */
93 /* More selection magic. */
94 Atom Xatom_insert_property;
96 /* Atom for indicating property type TEXT */
99 /* Kinds of protocol things we may receive. */
100 Atom Xatom_wm_take_focus;
101 Atom Xatom_wm_save_yourself;
102 Atom Xatom_wm_delete_window;
104 /* Communication with window managers. */
105 Atom Xatom_wm_protocols;
107 /* These are to handle incremental selection transfer. */
108 Window incr_requestor;
111 unsigned char *incr_value;
112 unsigned char *incr_ptr;
114 /* SELECTION OWNER CODE */
117 /* Request selection ownership if we do not already have it. */
120 own_selection (selection_type, time)
124 Window owner_window, selecting_window;
126 if ((EQ (selection_type, Qprimary) && !NILP (Vx_selection_value))
127 || ((EQ (selection_type, Qsecondary)) && !NILP (Vx_secondary_selection_value))
128 || ((EQ (selection_type, Qclipboard)) && !NILP (Vx_clipboard_value)))
131 selecting_window = selected_screen->display.x->window_desc;
132 XSetSelectionOwner (x_current_display, selection_type,
133 selecting_window, time);
134 owner_window = XGetSelectionOwner (x_current_display, selection_type);
136 if (owner_window != selecting_window)
142 /* Become the selection owner and make our data the selection value.
143 If we are already the owner, merely change data and timestamp values.
144 This avoids generating SelectionClear events for ourselves. */
146 DEFUN ("x-own-selection", Fx_own_selection, Sx_own_selection,
148 "Make STRING the selection value. Default is the primary selection,\n\
149 but optional second argument TYPE may specify secondary or clipboard.")
151 register Lisp_Object string, type;
155 Time event_time = mouse_timestamp;
156 CHECK_STRING (string, 0);
158 if (NILP (type) || EQ (type, Qprimary))
161 if (own_selection (XA_PRIMARY, event_time))
163 x_begin_selection_own = event_time;
164 val = Vx_selection_value = string;
168 else if (EQ (type, Qsecondary))
171 if (own_selection (XA_SECONDARY, event_time))
173 x_begin_secondary_selection_own = event_time;
174 val = Vx_secondary_selection_value = string;
178 else if (EQ (type, Qclipboard))
181 if (own_selection (Xatom_clipboard, event_time))
183 x_begin_clipboard_own = event_time;
184 val = Vx_clipboard_value = string;
189 error ("Invalid X selection type");
194 /* Clear our selection ownership data, as some other client has
198 x_disown_selection (old_owner, selection, changed_owner_time)
201 Time changed_owner_time;
203 struct screen *s = x_window_to_screen (old_owner);
205 if (s) /* We are the owner */
207 if (selection == XA_PRIMARY)
209 x_begin_selection_own = 0;
210 Vx_selection_value = Qnil;
212 else if (selection == XA_SECONDARY)
214 x_begin_secondary_selection_own = 0;
215 Vx_secondary_selection_value = Qnil;
217 else if (selection == Xatom_clipboard)
219 x_begin_clipboard_own = 0;
220 Vx_clipboard_value = Qnil;
226 abort (); /* Inconsistent state. */
229 int x_selection_alloc_error;
230 int x_converting_selection;
232 /* Reply to some client's request for our selection data. Data is
233 placed in a propery supplied by the requesting window.
235 If the data exceeds the maximum amount the server can send,
236 then prepare to send it incrementally, and reply to the client with
237 the total size of the data.
239 But first, check for all the other crufty stuff we could get. */
242 x_answer_selection_request (event)
243 XSelectionRequestEvent event;
246 Lisp_Object selection_value;
248 int format = 8; /* We have only byte sized (text) data. */
250 evt.type = SelectionNotify; /* Construct reply event */
251 evt.display = event.display;
252 evt.requestor = event.requestor;
253 evt.selection = event.selection;
254 evt.time = event.time;
255 evt.target = event.target;
257 if (event.selection == XA_PRIMARY)
259 emacs_own_time = x_begin_selection_own;
260 selection_value = Vx_selection_value;
262 else if (event.selection == XA_SECONDARY)
264 emacs_own_time = x_begin_secondary_selection_own;
265 selection_value = Vx_secondary_selection_value;
267 else if (event.selection == Xatom_clipboard)
269 emacs_own_time = x_begin_clipboard_own;
270 selection_value = Vx_clipboard_value;
275 if (event.time != CurrentTime
276 && event.time < emacs_own_time)
280 if (event.property == None) /* obsolete client */
281 evt.property = event.target;
283 evt.property = event.property;
286 if (event.target == Xatom_targets) /* Send List of target atoms */
289 else if (event.target == Xatom_multiple) /* Recvd list: <target, prop> */
293 unsigned long items, bytes_left;
297 if (event.property == 0 /* 0 == NILP */
298 || event.property == None)
301 result = XGetWindowProperty (event.display, event.requestor,
302 event.property, 0L, 10000000L,
303 True, Xatom_pair, &type, &return_format,
304 &items, &bytes_left, &data);
306 if (result == Success && type == Xatom_pair)
307 for (i = items; i > 0; i--)
309 /* Convert each element of the list. */
312 (void) XSendEvent (x_current_display, evt.requestor, False,
313 0L, (XEvent *) &evt);
316 else if (event.target == Xatom_timestamp) /* Send ownership timestamp */
318 if (! emacs_own_time)
322 XChangeProperty (evt.display, evt.requestor, evt.property,
323 evt.target, format, PropModeReplace,
324 (unsigned char *) &emacs_own_time, 1);
327 else if (event.target == Xatom_delete) /* Delete our selection. */
329 if (EQ (Qnil, selection_value))
332 x_disown_selection (event.owner, event.selection, event.time);
334 /* Now return property of type NILP, length 0. */
335 XChangeProperty (event.display, event.requestor, event.property,
336 0, format, PropModeReplace, (unsigned char *) 0, 0);
339 else if (event.target == Xatom_insert_selection)
343 unsigned long items, bytes_left;
345 int result = XGetWindowProperty (event.display, event.requestor,
346 event.property, 0L, 10000000L,
347 True, Xatom_pair, &type, &return_format,
348 &items, &bytes_left, &data);
349 if (result == Success && type == Xatom_pair)
351 /* Convert the first atom to (a selection) to the target
352 indicated by the second atom. */
355 else if (event.target == Xatom_insert_property)
359 unsigned long items, bytes_left;
361 int result = XGetWindowProperty (event.display, event.requestor,
362 event.property, 0L, 10000000L,
363 True, XA_STRING, &type, &return_format,
364 &items, &bytes_left, &data);
366 if (result == Success && type == XA_STRING && return_format == 8)
368 if (event.selection == Xatom_emacs_selection)
369 Vx_selection_value = make_string (data);
370 else if (event.selection == Xatom_emacs_secondary_selection)
371 Vx_secondary_selection_value = make_string (data);
372 else if (event.selection == Xatom_clipboard_selection)
373 Vx_clipboard_value = make_string (data);
380 else if ((event.target == Xatom_text
381 || event.target == XA_STRING))
383 int size = XSTRING (selection_value)->size;
384 unsigned char *data = XSTRING (selection_value)->data;
386 if (EQ (Qnil, selection_value))
389 /* Place data on requestor window's property. */
390 if (SELECTION_LENGTH (size, format)
391 <= MAX_SELECTION (x_current_display))
393 x_converting_selection = 1;
394 XChangeProperty (evt.display, evt.requestor, evt.property,
395 evt.target, format, PropModeReplace,
397 if (x_selection_alloc_error)
399 x_selection_alloc_error = 0;
402 x_converting_selection = 0;
404 else /* Send incrementally */
406 evt.target = Xatom_incremental;
407 incr_requestor = evt.requestor;
408 incr_property = evt.property;
409 x_converting_selection = 1;
411 /* Need to handle Alloc errors on these requests. */
412 XChangeProperty (evt.display, incr_requestor, incr_property,
413 Xatom_incremental, 32,
415 (unsigned char *) &size, 1);
416 if (x_selection_alloc_error)
418 x_selection_alloc_error = 0;
419 x_converting_selection = 0;
421 /* Now abort the send. */
428 /* Ask for notification when requestor deletes property. */
429 XSelectInput (x_current_display, incr_requestor, PropertyChangeMask);
431 /* If we're sending incrementally, perhaps block here
438 /* Don't do this if there was an Alloc error: abort the transfer
440 (void) XSendEvent (x_current_display, evt.requestor, False,
441 0L, (XEvent *) &evt);
444 /* Send an increment of selection data in response to a PropertyNotify event.
445 The increment is placed in a property on the requestor's window.
446 When the requestor has processed the increment, it deletes the property,
447 which sends us another PropertyNotify event.
449 When there is no more data to send, we send a zero-length increment. */
452 x_send_incremental (event)
453 XPropertyEvent event;
456 && incr_requestor == event.window
457 && incr_property == event.atom
458 && event.state == PropertyDelete)
461 int length = MAX_SELECTION (x_current_display);
462 int bytes_left = (incr_nbytes - (incr_ptr - incr_value));
464 if (length > bytes_left) /* Also sends 0 len when finished. */
466 XChangeProperty (x_current_display, incr_requestor,
467 incr_property, XA_STRING, format,
468 PropModeAppend, incr_ptr, length);
469 if (x_selection_alloc_error)
471 x_selection_alloc_error = 0;
472 x_converting_selection = 0;
473 /* Abandon the transmission. */
479 { /* Everything's sent */
480 XSelectInput (x_current_display, incr_requestor, 0L);
481 incr_requestor = (Window) 0;
482 incr_property = (Atom) 0;
484 incr_value = (unsigned char *) 0;
485 incr_ptr = (unsigned char *) 0;
486 x_converting_selection = 0;
491 /* SELECTION REQUESTOR CODE */
493 /* Predicate function used to match a requested event. */
496 XCheckSelectionEvent (dpy, event, window)
501 if (event->type == SelectionNotify)
502 if (event->xselection.requestor == (Window) window)
508 /* Request a selection value from its owner. This will block until
509 all the data is arrived. */
512 get_selection_value (type)
517 Time requestor_time; /* Timestamp of selection request. */
518 Window requestor_window;
521 requestor_time = mouse_timestamp;
522 requestor_window = selected_screen->display.x->window_desc;
523 XConvertSelection (x_current_display, type, XA_STRING,
524 Xatom_emacs_selection, requestor_window, requestor_time);
525 XIfEvent (x_current_display,
527 XCheckSelectionEvent,
528 (char *) requestor_window);
529 val = x_selection_arrival (&event, requestor_window, requestor_time);
535 /* Request a selection value from the owner. If we are the owner,
536 simply return our selection value. If we are not the owner, this
537 will block until all of the data has arrived. */
539 DEFUN ("x-selection-value", Fx_selection_value, Sx_selection_value,
541 "Return the value of one of the selections. Default is the primary\n\
542 selection, but optional argument TYPE may specify secondary or clipboard.")
544 register Lisp_Object type;
548 if (NILP (type) || EQ (type, Qprimary))
550 if (!NILP (Vx_selection_value))
551 return Vx_selection_value;
553 return get_selection_value (XA_PRIMARY);
555 else if (EQ (type, Qsecondary))
557 if (!NILP (Vx_secondary_selection_value))
558 return Vx_secondary_selection_value;
560 return get_selection_value (XA_SECONDARY);
562 else if (EQ (type, Qclipboard))
564 if (!NILP (Vx_clipboard_value))
565 return Vx_clipboard_value;
567 return get_selection_value (Xatom_clipboard);
570 error ("Invalid X selection type");
574 x_selection_arrival (event, requestor_window, requestor_time)
575 register XSelectionEvent *event;
576 Window requestor_window;
580 Atom type, selection;
583 unsigned long bytes_left;
584 unsigned char *data = 0;
587 if (event->selection == XA_PRIMARY)
588 selection = Xatom_emacs_selection;
589 else if (event->selection == XA_SECONDARY)
590 selection = Xatom_emacs_secondary_selection;
591 else if (event->selection == Xatom_clipboard)
592 selection = Xatom_clipboard_selection;
596 if (event->requestor == requestor_window
597 && event->time == requestor_time
598 && event->property != None)
599 if (event->target != Xatom_incremental)
601 unsigned char *return_string =
602 (unsigned char *) alloca (MAX_SELECTION (x_current_display));
606 result = XGetWindowProperty (x_current_display, requestor_window,
608 10000000L, True, XA_STRING,
609 &type, &format, &items,
611 if (result == Success && type == XA_STRING && format == 8
612 && offset < MAX_SELECTION (x_current_display))
614 bcopy (data, return_string + offset, items);
617 XFree ((char *) data);
621 return make_string (return_string, offset);
623 else /* Prepare incremental transfer. */
625 unsigned char *increment_value;
626 unsigned char *increment_ptr;
628 int *increment_nbytes = 0;
630 result = XGetWindowProperty (x_current_display, requestor_window,
631 selection, 0L, 10000000L, False,
632 event->property, &type, &format,
634 (unsigned char **) &increment_nbytes);
635 if (result == Success)
637 XPropertyEvent property_event;
639 total_size = *increment_nbytes;
640 increment_value = (unsigned char *) alloca (total_size);
641 increment_ptr = increment_value;
643 XDeleteProperty (x_current_display, event->requestor,
645 XFlush (x_current_display);
646 XFree ((char *) increment_nbytes);
649 { /* NOTE: this blocks. */
650 XWindowEvent (x_current_display, requestor_window,
652 (XEvent *) &property_event);
654 if (property_event.atom == selection
655 && property_event.state == PropertyNewValue)
658 result = XGetWindowProperty (x_current_display,
666 if (result == Success && type == XA_STRING
669 bcopy (data, increment_ptr, items);
670 increment_ptr += items;
676 while (increment_ptr < (increment_value + total_size));
678 return make_string (increment_value,
679 (increment_ptr - increment_value));
689 DEFVAR_LISP ("x-selection-value", &Vx_selection_value,
690 "The value of emacs' last cut-string.");
691 Vx_selection_value = Qnil;
693 DEFVAR_LISP ("x-secondary-selection-value", &Vx_secondary_selection_value,
694 "The value of emacs' last secondary cut-string.");
695 Vx_secondary_selection_value = Qnil;
697 DEFVAR_LISP ("x-clipboard-value", &Vx_clipboard_value,
698 "The string emacs last sent to the clipboard.");
699 Vx_clipboard_value = Qnil;
701 Qprimary = intern ("primary");
702 staticpro (&Qprimary);
703 Qsecondary = intern ("secondary");
704 staticpro (&Qsecondary);
705 Qclipboard = intern ("clipboard");
706 staticpro (&Qclipboard);
708 defsubr (&Sx_own_selection);
709 defsubr (&Sx_selection_value);