xselect.c 94.3 KB
Newer Older
1
/* X Selection processing for Emacs.
2
   Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2003,
Glenn Morris's avatar
Glenn Morris committed
3
                 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4 5 6 7 8

This file is part of GNU Emacs.

GNU Emacs is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
10 11 12 13 14 15 16 17 18
any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs; see the file COPYING.  If not, write to
Lute Kamstra's avatar
Lute Kamstra committed
19 20
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
21

Jim Blandy's avatar
Jim Blandy committed
22

Richard M. Stallman's avatar
Richard M. Stallman committed
23 24
/* Rewritten by jwz */

25
#include <config.h>
Jan Djärv's avatar
Jan Djärv committed
26
#include <stdio.h>      /* termhooks.h needs this */
27 28 29 30 31 32 33 34

#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
35 36
#include "lisp.h"
#include "xterm.h"	/* for all of the X includes */
37 38
#include "dispextern.h"	/* frame.h seems to want this */
#include "frame.h"	/* Need this to get the X window of selected_frame */
39
#include "blockinput.h"
Kenichi Handa's avatar
Kenichi Handa committed
40
#include "buffer.h"
Andreas Schwab's avatar
Andreas Schwab committed
41
#include "process.h"
42
#include "termhooks.h"
43
#include "keyboard.h"
44 45

#include <X11/Xproto.h>
46

47 48 49 50 51 52
struct prop_location;

static Lisp_Object x_atom_to_symbol P_ ((Display *dpy, Atom atom));
static Atom symbol_to_x_atom P_ ((struct x_display_info *, Display *,
				  Lisp_Object));
static void x_own_selection P_ ((Lisp_Object, Lisp_Object));
Kenichi Handa's avatar
Kenichi Handa committed
53
static Lisp_Object x_get_local_selection P_ ((Lisp_Object, Lisp_Object, int));
54 55 56 57
static void x_decline_selection_request P_ ((struct input_event *));
static Lisp_Object x_selection_request_lisp_error P_ ((Lisp_Object));
static Lisp_Object queue_selection_requests_unwind P_ ((Lisp_Object));
static Lisp_Object some_frame_on_display P_ ((struct x_display_info *));
58
static Lisp_Object x_catch_errors_unwind P_ ((Lisp_Object));
59 60 61 62 63 64 65 66
static void x_reply_selection_request P_ ((struct input_event *, int,
					   unsigned char *, int, Atom));
static int waiting_for_other_props_on_window P_ ((Display *, Window));
static struct prop_location *expect_property_change P_ ((Display *, Window,
							 Atom, int));
static void unexpect_property_change P_ ((struct prop_location *));
static Lisp_Object wait_for_property_change_unwind P_ ((Lisp_Object));
static void wait_for_property_change P_ ((struct prop_location *));
67 68 69
static Lisp_Object x_get_foreign_selection P_ ((Lisp_Object,
                                                Lisp_Object,
                                                Lisp_Object));
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
static void x_get_window_property P_ ((Display *, Window, Atom,
				       unsigned char **, int *,
				       Atom *, int *, unsigned long *, int));
static void receive_incremental_selection P_ ((Display *, Window, Atom,
					       Lisp_Object, unsigned,
					       unsigned char **, int *,
					       Atom *, int *, unsigned long *));
static Lisp_Object x_get_window_property_as_lisp_data P_ ((Display *,
							   Window, Atom,
							   Lisp_Object, Atom));
static Lisp_Object selection_data_to_lisp_data P_ ((Display *, unsigned char *,
						    int, Atom, int));
static void lisp_data_to_selection_data P_ ((Display *, Lisp_Object,
					     unsigned char **, Atom *,
					     unsigned *, int *, int *));
static Lisp_Object clean_local_selection_data P_ ((Lisp_Object));
static void initialize_cut_buffers P_ ((Display *, Window));


/* Printing traces to stderr.  */

#ifdef TRACE_SELECTION
#define TRACE0(fmt) \
  fprintf (stderr, "%d: " fmt "\n", getpid ())
#define TRACE1(fmt, a0) \
  fprintf (stderr, "%d: " fmt "\n", getpid (), a0)
#define TRACE2(fmt, a0, a1) \
  fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1)
98 99
#define TRACE3(fmt, a0, a1, a2) \
  fprintf (stderr, "%d: " fmt "\n", getpid (), a0, a1, a2)
100 101 102 103
#else
#define TRACE0(fmt)		(void) 0
#define TRACE1(fmt, a0)		(void) 0
#define TRACE2(fmt, a0, a1)	(void) 0
104
#define TRACE3(fmt, a0, a1)	(void) 0
105 106 107
#endif


Richard M. Stallman's avatar
Richard M. Stallman committed
108 109 110 111 112 113
#define CUT_BUFFER_SUPPORT

Lisp_Object QPRIMARY, QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP,
  QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL,
  QATOM_PAIR;

Karl Heuer's avatar
Karl Heuer committed
114
Lisp_Object QCOMPOUND_TEXT;	/* This is a type of selection.  */
Kenichi Handa's avatar
Kenichi Handa committed
115
Lisp_Object QUTF8_STRING;	/* This is a type of selection.  */
Karl Heuer's avatar
Karl Heuer committed
116

117
Lisp_Object Qcompound_text_with_extensions;
118

Richard M. Stallman's avatar
Richard M. Stallman committed
119 120 121 122 123
#ifdef CUT_BUFFER_SUPPORT
Lisp_Object QCUT_BUFFER0, QCUT_BUFFER1, QCUT_BUFFER2, QCUT_BUFFER3,
  QCUT_BUFFER4, QCUT_BUFFER5, QCUT_BUFFER6, QCUT_BUFFER7;
#endif

124 125
static Lisp_Object Vx_lost_selection_functions;
static Lisp_Object Vx_sent_selection_functions;
126 127
/* Coding system for communicating with other X clients via selection
   and clipboard.  */
128
static Lisp_Object Vselection_coding_system;
Richard M. Stallman's avatar
Richard M. Stallman committed
129

130 131 132
/* Coding system for the next communicating with other X clients.  */
static Lisp_Object Vnext_selection_coding_system;

133 134
static Lisp_Object Qforeign_selection;

Richard M. Stallman's avatar
Richard M. Stallman committed
135 136 137
/* If this is a smaller number than the max-request-size of the display,
   emacs will use INCR selection transfer when the selection is larger
   than this.  The max-request-size is usually around 64k, so if you want
138
   emacs to use incremental selection transfers when the selection is
Richard M. Stallman's avatar
Richard M. Stallman committed
139
   smaller than that, set this.  I added this mostly for debugging the
140
   incremental transfer stuff, but it might improve server performance.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
141 142
#define MAX_SELECTION_QUANTUM 0xFFFFFF

143 144 145 146 147
#ifdef HAVE_X11R4
#define SELECTION_QUANTUM(dpy) ((XMaxRequestSize(dpy) << 2) - 100)
#else
#define SELECTION_QUANTUM(dpy) (((dpy)->max_request_size << 2) - 100)
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
148

149
/* The timestamp of the last input event Emacs received from the X server.  */
150 151
/* Defined in keyboard.c.  */
extern unsigned long last_event_timestamp;
Richard M. Stallman's avatar
Richard M. Stallman committed
152 153

/* This is an association list whose elements are of the form
154 155 156
     ( SELECTION-NAME SELECTION-VALUE SELECTION-TIMESTAMP FRAME)
   SELECTION-NAME is a lisp symbol, whose name is the name of an X Atom.
   SELECTION-VALUE is the value that emacs owns for that selection.
Richard M. Stallman's avatar
Richard M. Stallman committed
157
     It may be any kind of Lisp object.
158
   SELECTION-TIMESTAMP is the time at which emacs began owning this selection,
Richard M. Stallman's avatar
Richard M. Stallman committed
159
     as a cons of two 16-bit numbers (making a 32 bit time.)
160 161
   FRAME is the frame for which we made the selection.
   If there is an entry in this alist, then it can be assumed that Emacs owns
Richard M. Stallman's avatar
Richard M. Stallman committed
162 163
    that selection.
   The only (eq) parts of this list that are visible from Lisp are the
164 165
    selection-values.  */
static Lisp_Object Vselection_alist;
Richard M. Stallman's avatar
Richard M. Stallman committed
166 167 168

/* This is an alist whose CARs are selection-types (whose names are the same
   as the names of X Atoms) and whose CDRs are the names of Lisp functions to
169
   call to convert the given Emacs selection value to a string representing
Richard M. Stallman's avatar
Richard M. Stallman committed
170
   the given selection type.  This is for Lisp-level extension of the emacs
171 172
   selection handling.  */
static Lisp_Object Vselection_converter_alist;
Richard M. Stallman's avatar
Richard M. Stallman committed
173 174

/* If the selection owner takes too long to reply to a selection request,
175
   we give up on it.  This is in milliseconds (0 = no timeout.)  */
176
static EMACS_INT x_selection_timeout;
Richard M. Stallman's avatar
Richard M. Stallman committed
177

178 179


180 181
/* Define a queue to save up SELECTION_REQUEST_EVENT events for later
   handling.  */
182 183 184 185 186 187 188 189 190

struct selection_event_queue
  {
    struct input_event event;
    struct selection_event_queue *next;
  };

static struct selection_event_queue *selection_queue;

191
/* Nonzero means queue up SELECTION_REQUEST_EVENT events.  */
192 193 194

static int x_queue_selection_requests;

195
/* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later.  */
196 197 198 199 200 201 202

static void
x_queue_event (event)
     struct input_event *event;
{
  struct selection_event_queue *queue_tmp;

203 204
  /* Don't queue repeated requests.
     This only happens for large requests which uses the incremental protocol.  */
205 206 207 208
  for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next)
    {
      if (!bcmp (&queue_tmp->event, event, sizeof (*event)))
	{
209 210
	  TRACE1 ("DECLINE DUP SELECTION EVENT %08lx", (unsigned long)queue_tmp);
	  x_decline_selection_request (event);
211 212 213 214 215 216 217 218 219
	  return;
	}
    }

  queue_tmp
    = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));

  if (queue_tmp != NULL)
    {
220
      TRACE1 ("QUEUE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
221 222 223 224 225 226
      queue_tmp->event = *event;
      queue_tmp->next = selection_queue;
      selection_queue = queue_tmp;
    }
}

227
/* Start queuing SELECTION_REQUEST_EVENT events.  */
228 229 230 231 232 233 234 235 236 237 238

static void
x_start_queuing_selection_requests ()
{
  if (x_queue_selection_requests)
    abort ();

  x_queue_selection_requests++;
  TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests);
}

239
/* Stop queuing SELECTION_REQUEST_EVENT events.  */
240 241 242 243 244 245 246 247 248 249 250 251 252

static void
x_stop_queuing_selection_requests ()
{
  TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests);
  --x_queue_selection_requests;

  /* Take all the queued events and put them back
     so that they get processed afresh.  */

  while (selection_queue != NULL)
    {
      struct selection_event_queue *queue_tmp = selection_queue;
253
      TRACE1 ("RESTORE SELECTION EVENT %08lx", (unsigned long)queue_tmp);
254 255 256 257 258 259 260
      kbd_buffer_unget_event (&queue_tmp->event);
      selection_queue = queue_tmp->next;
      xfree ((char *)queue_tmp);
    }
}


261
/* This converts a Lisp symbol to a server Atom, avoiding a server
Richard M. Stallman's avatar
Richard M. Stallman committed
262 263 264
   roundtrip whenever possible.  */

static Atom
265 266
symbol_to_x_atom (dpyinfo, display, sym)
     struct x_display_info *dpyinfo;
Richard M. Stallman's avatar
Richard M. Stallman committed
267 268 269 270 271 272 273 274 275 276
     Display *display;
     Lisp_Object sym;
{
  Atom val;
  if (NILP (sym))	    return 0;
  if (EQ (sym, QPRIMARY))   return XA_PRIMARY;
  if (EQ (sym, QSECONDARY)) return XA_SECONDARY;
  if (EQ (sym, QSTRING))    return XA_STRING;
  if (EQ (sym, QINTEGER))   return XA_INTEGER;
  if (EQ (sym, QATOM))	    return XA_ATOM;
277 278 279
  if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD;
  if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP;
  if (EQ (sym, QTEXT))	    return dpyinfo->Xatom_TEXT;
Karl Heuer's avatar
Karl Heuer committed
280
  if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT;
Kenichi Handa's avatar
Kenichi Handa committed
281
  if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING;
282 283 284 285 286 287
  if (EQ (sym, QDELETE))    return dpyinfo->Xatom_DELETE;
  if (EQ (sym, QMULTIPLE))  return dpyinfo->Xatom_MULTIPLE;
  if (EQ (sym, QINCR))	    return dpyinfo->Xatom_INCR;
  if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP;
  if (EQ (sym, QTARGETS))   return dpyinfo->Xatom_TARGETS;
  if (EQ (sym, QNULL))	    return dpyinfo->Xatom_NULL;
Richard M. Stallman's avatar
Richard M. Stallman committed
288 289 290 291 292 293 294 295 296 297 298 299
#ifdef CUT_BUFFER_SUPPORT
  if (EQ (sym, QCUT_BUFFER0)) return XA_CUT_BUFFER0;
  if (EQ (sym, QCUT_BUFFER1)) return XA_CUT_BUFFER1;
  if (EQ (sym, QCUT_BUFFER2)) return XA_CUT_BUFFER2;
  if (EQ (sym, QCUT_BUFFER3)) return XA_CUT_BUFFER3;
  if (EQ (sym, QCUT_BUFFER4)) return XA_CUT_BUFFER4;
  if (EQ (sym, QCUT_BUFFER5)) return XA_CUT_BUFFER5;
  if (EQ (sym, QCUT_BUFFER6)) return XA_CUT_BUFFER6;
  if (EQ (sym, QCUT_BUFFER7)) return XA_CUT_BUFFER7;
#endif
  if (!SYMBOLP (sym)) abort ();

300
  TRACE1 (" XInternAtom %s", (char *) SDATA (SYMBOL_NAME (sym)));
Richard M. Stallman's avatar
Richard M. Stallman committed
301
  BLOCK_INPUT;
302
  val = XInternAtom (display, (char *) SDATA (SYMBOL_NAME (sym)), False);
Richard M. Stallman's avatar
Richard M. Stallman committed
303 304 305 306 307 308 309 310 311
  UNBLOCK_INPUT;
  return val;
}


/* This converts a server Atom to a Lisp symbol, avoiding server roundtrips
   and calls to intern whenever possible.  */

static Lisp_Object
312 313
x_atom_to_symbol (dpy, atom)
     Display *dpy;
Richard M. Stallman's avatar
Richard M. Stallman committed
314 315
     Atom atom;
{
316
  struct x_display_info *dpyinfo;
Richard M. Stallman's avatar
Richard M. Stallman committed
317 318
  char *str;
  Lisp_Object val;
319

320 321
  if (! atom)
    return Qnil;
322

323 324 325 326 327 328 329 330 331 332 333 334
  switch (atom)
    {
    case XA_PRIMARY:
      return QPRIMARY;
    case XA_SECONDARY:
      return QSECONDARY;
    case XA_STRING:
      return QSTRING;
    case XA_INTEGER:
      return QINTEGER;
    case XA_ATOM:
      return QATOM;
Richard M. Stallman's avatar
Richard M. Stallman committed
335
#ifdef CUT_BUFFER_SUPPORT
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
    case XA_CUT_BUFFER0:
      return QCUT_BUFFER0;
    case XA_CUT_BUFFER1:
      return QCUT_BUFFER1;
    case XA_CUT_BUFFER2:
      return QCUT_BUFFER2;
    case XA_CUT_BUFFER3:
      return QCUT_BUFFER3;
    case XA_CUT_BUFFER4:
      return QCUT_BUFFER4;
    case XA_CUT_BUFFER5:
      return QCUT_BUFFER5;
    case XA_CUT_BUFFER6:
      return QCUT_BUFFER6;
    case XA_CUT_BUFFER7:
      return QCUT_BUFFER7;
Richard M. Stallman's avatar
Richard M. Stallman committed
352
#endif
353 354
    }

355
  dpyinfo = x_display_info_for_display (dpy);
356
  if (atom == dpyinfo->Xatom_CLIPBOARD)
357
    return QCLIPBOARD;
358
  if (atom == dpyinfo->Xatom_TIMESTAMP)
359
    return QTIMESTAMP;
360
  if (atom == dpyinfo->Xatom_TEXT)
361
    return QTEXT;
Karl Heuer's avatar
Karl Heuer committed
362 363
  if (atom == dpyinfo->Xatom_COMPOUND_TEXT)
    return QCOMPOUND_TEXT;
Kenichi Handa's avatar
Kenichi Handa committed
364 365
  if (atom == dpyinfo->Xatom_UTF8_STRING)
    return QUTF8_STRING;
366
  if (atom == dpyinfo->Xatom_DELETE)
367
    return QDELETE;
368
  if (atom == dpyinfo->Xatom_MULTIPLE)
369
    return QMULTIPLE;
370
  if (atom == dpyinfo->Xatom_INCR)
371
    return QINCR;
372
  if (atom == dpyinfo->Xatom_EMACS_TMP)
373
    return QEMACS_TMP;
374
  if (atom == dpyinfo->Xatom_TARGETS)
375
    return QTARGETS;
376
  if (atom == dpyinfo->Xatom_NULL)
377
    return QNULL;
Richard M. Stallman's avatar
Richard M. Stallman committed
378 379

  BLOCK_INPUT;
380
  str = XGetAtomName (dpy, atom);
Richard M. Stallman's avatar
Richard M. Stallman committed
381
  UNBLOCK_INPUT;
382
  TRACE1 ("XGetAtomName --> %s", str);
Richard M. Stallman's avatar
Richard M. Stallman committed
383 384 385
  if (! str) return Qnil;
  val = intern (str);
  BLOCK_INPUT;
386
  /* This was allocated by Xlib, so use XFree.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
387 388 389 390
  XFree (str);
  UNBLOCK_INPUT;
  return val;
}
391

Richard M. Stallman's avatar
Richard M. Stallman committed
392
/* Do protocol to assert ourself as a selection owner.
393
   Update the Vselection_alist so that we can reply to later requests for
Richard M. Stallman's avatar
Richard M. Stallman committed
394 395 396 397 398 399
   our selection.  */

static void
x_own_selection (selection_name, selection_value)
     Lisp_Object selection_name, selection_value;
{
400
  struct frame *sf = SELECTED_FRAME ();
401 402
  Window selecting_window;
  Display *display;
403
  Time time = last_event_timestamp;
Richard M. Stallman's avatar
Richard M. Stallman committed
404
  Atom selection_atom;
405
  struct x_display_info *dpyinfo;
Richard M. Stallman's avatar
Richard M. Stallman committed
406

407 408 409 410 411 412 413
  if (! FRAME_X_P (sf))
    return;

  selecting_window = FRAME_X_WINDOW (sf);
  display = FRAME_X_DISPLAY (sf);
  dpyinfo = FRAME_X_DISPLAY_INFO (sf);
  
414
  CHECK_SYMBOL (selection_name);
415
  selection_atom = symbol_to_x_atom (dpyinfo, display, selection_name);
Richard M. Stallman's avatar
Richard M. Stallman committed
416 417

  BLOCK_INPUT;
418
  x_catch_errors (display);
Richard M. Stallman's avatar
Richard M. Stallman committed
419
  XSetSelectionOwner (display, selection_atom, selecting_window, time);
420
  x_check_errors (display, "Can't set selection: %s");
421
  x_uncatch_errors ();
Richard M. Stallman's avatar
Richard M. Stallman committed
422 423 424 425 426 427 428 429 430 431 432
  UNBLOCK_INPUT;

  /* Now update the local cache */
  {
    Lisp_Object selection_time;
    Lisp_Object selection_data;
    Lisp_Object prev_value;

    selection_time = long_to_cons ((unsigned long) time);
    selection_data = Fcons (selection_name,
			    Fcons (selection_value,
433
				   Fcons (selection_time,
434
					  Fcons (selected_frame, Qnil))));
Richard M. Stallman's avatar
Richard M. Stallman committed
435 436 437 438 439 440 441 442 443 444 445
    prev_value = assq_no_quit (selection_name, Vselection_alist);

    Vselection_alist = Fcons (selection_data, Vselection_alist);

    /* If we already owned the selection, remove the old selection data.
       Perhaps we should destructively modify it instead.
       Don't use Fdelq as that may QUIT.  */
    if (!NILP (prev_value))
      {
	Lisp_Object rest;	/* we know it's not the CAR, so it's easy.  */
	for (rest = Vselection_alist; !NILP (rest); rest = Fcdr (rest))
446
	  if (EQ (prev_value, Fcar (XCDR (rest))))
Richard M. Stallman's avatar
Richard M. Stallman committed
447
	    {
448
	      XSETCDR (rest, Fcdr (XCDR (rest)));
Richard M. Stallman's avatar
Richard M. Stallman committed
449 450 451 452 453 454 455 456 457
	      break;
	    }
      }
  }
}

/* Given a selection-name and desired type, look up our local copy of
   the selection value and convert it to the type.
   The value is nil or a string.
Kenichi Handa's avatar
Kenichi Handa committed
458 459
   This function is used both for remote requests (LOCAL_REQUEST is zero)
   and for local x-get-selection-internal (LOCAL_REQUEST is nonzero).
Richard M. Stallman's avatar
Richard M. Stallman committed
460 461 462 463

   This calls random Lisp code, and may signal or gc.  */

static Lisp_Object
Kenichi Handa's avatar
Kenichi Handa committed
464
x_get_local_selection (selection_symbol, target_type, local_request)
Richard M. Stallman's avatar
Richard M. Stallman committed
465
     Lisp_Object selection_symbol, target_type;
Kenichi Handa's avatar
Kenichi Handa committed
466
     int local_request;
Richard M. Stallman's avatar
Richard M. Stallman committed
467 468 469 470 471 472 473 474 475 476 477 478 479
{
  Lisp_Object local_value;
  Lisp_Object handler_fn, value, type, check;
  int count;

  local_value = assq_no_quit (selection_symbol, Vselection_alist);

  if (NILP (local_value)) return Qnil;

  /* TIMESTAMP and MULTIPLE are special cases 'cause that's easiest.  */
  if (EQ (target_type, QTIMESTAMP))
    {
      handler_fn = Qnil;
480
      value = XCAR (XCDR (XCDR (local_value)));
Richard M. Stallman's avatar
Richard M. Stallman committed
481 482 483 484 485 486 487
    }
#if 0
  else if (EQ (target_type, QDELETE))
    {
      handler_fn = Qnil;
      Fx_disown_selection_internal
	(selection_symbol,
488
	 XCAR (XCDR (XCDR (local_value))));
Richard M. Stallman's avatar
Richard M. Stallman committed
489 490 491 492 493 494
      value = QNULL;
    }
#endif

#if 0 /* #### MULTIPLE doesn't work yet */
  else if (CONSP (target_type)
495
	   && XCAR (target_type) == QMULTIPLE)
Richard M. Stallman's avatar
Richard M. Stallman committed
496
    {
497 498
      Lisp_Object pairs;
      int size;
Richard M. Stallman's avatar
Richard M. Stallman committed
499
      int i;
500
      pairs = XCDR (target_type);
501
      size = XVECTOR (pairs)->size;
Richard M. Stallman's avatar
Richard M. Stallman committed
502 503 504 505 506 507 508
      /* If the target is MULTIPLE, then target_type looks like
	  (MULTIPLE . [[SELECTION1 TARGET1] [SELECTION2 TARGET2] ... ])
	 We modify the second element of each pair in the vector and
	 return it as [[SELECTION1 <value1>] [SELECTION2 <value2>] ... ]
       */
      for (i = 0; i < size; i++)
	{
509 510
	  Lisp_Object pair;
	  pair = XVECTOR (pairs)->contents [i];
Richard M. Stallman's avatar
Richard M. Stallman committed
511 512
	  XVECTOR (pair)->contents [1]
	    = x_get_local_selection (XVECTOR (pair)->contents [0],
Kenichi Handa's avatar
Kenichi Handa committed
513 514
				     XVECTOR (pair)->contents [1],
				     local_request);
Richard M. Stallman's avatar
Richard M. Stallman committed
515 516 517 518 519 520 521 522 523
	}
      return pairs;
    }
#endif
  else
    {
      /* Don't allow a quit within the converter.
	 When the user types C-g, he would be surprised
	 if by luck it came during a converter.  */
Juanma Barranquero's avatar
Juanma Barranquero committed
524
      count = SPECPDL_INDEX ();
Richard M. Stallman's avatar
Richard M. Stallman committed
525 526
      specbind (Qinhibit_quit, Qt);

527
      CHECK_SYMBOL (target_type);
Richard M. Stallman's avatar
Richard M. Stallman committed
528
      handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist));
529 530 531
      /* gcpro is not needed here since nothing but HANDLER_FN
	 is live, and that ought to be a symbol.  */

532 533
      if (!NILP (handler_fn))
	value = call3 (handler_fn,
Kenichi Handa's avatar
Kenichi Handa committed
534
		       selection_symbol, (local_request ? Qnil : target_type),
535
		       XCAR (XCDR (local_value)));
536 537
      else
	value = Qnil;
Richard M. Stallman's avatar
Richard M. Stallman committed
538 539 540 541 542
      unbind_to (count, Qnil);
    }

  /* Make sure this value is of a type that we could transmit
     to another X client.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
543

Richard M. Stallman's avatar
Richard M. Stallman committed
544 545
  check = value;
  if (CONSP (value)
546 547 548
      && SYMBOLP (XCAR (value)))
    type = XCAR (value),
    check = XCDR (value);
549

Richard M. Stallman's avatar
Richard M. Stallman committed
550 551 552
  if (STRINGP (check)
      || VECTORP (check)
      || SYMBOLP (check)
553
      || INTEGERP (check)
Richard M. Stallman's avatar
Richard M. Stallman committed
554 555
      || NILP (value))
    return value;
Richard M. Stallman's avatar
Richard M. Stallman committed
556
  /* Check for a value that cons_to_long could handle.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
557
  else if (CONSP (check)
558 559
	   && INTEGERP (XCAR (check))
	   && (INTEGERP (XCDR (check))
Richard M. Stallman's avatar
Richard M. Stallman committed
560
	       ||
561 562 563
	       (CONSP (XCDR (check))
		&& INTEGERP (XCAR (XCDR (check)))
		&& NILP (XCDR (XCDR (check))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
564
    return value;
565 566 567

  signal_error ("Invalid data returned by selection-conversion function",
		list2 (handler_fn, value));
Richard M. Stallman's avatar
Richard M. Stallman committed
568 569 570 571
}

/* Subroutines of x_reply_selection_request.  */

572
/* Send a SelectionNotify event to the requestor with property=None,
Richard M. Stallman's avatar
Richard M. Stallman committed
573 574 575 576 577 578 579
   meaning we were unable to do what they wanted.  */

static void
x_decline_selection_request (event)
     struct input_event *event;
{
  XSelectionEvent reply;
580

Richard M. Stallman's avatar
Richard M. Stallman committed
581 582
  reply.type = SelectionNotify;
  reply.display = SELECTION_EVENT_DISPLAY (event);
583
  reply.requestor = SELECTION_EVENT_REQUESTOR (event);
Richard M. Stallman's avatar
Richard M. Stallman committed
584 585 586 587 588
  reply.selection = SELECTION_EVENT_SELECTION (event);
  reply.time = SELECTION_EVENT_TIME (event);
  reply.target = SELECTION_EVENT_TARGET (event);
  reply.property = None;

589 590
  /* The reason for the error may be that the receiver has
     died in the meantime.  Handle that case.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
591
  BLOCK_INPUT;
592
  x_catch_errors (reply.display);
593
  XSendEvent (reply.display, reply.requestor, False, 0L, (XEvent *) &reply);
594
  XFlush (reply.display);
595
  x_uncatch_errors ();
Richard M. Stallman's avatar
Richard M. Stallman committed
596 597 598 599 600 601 602
  UNBLOCK_INPUT;
}

/* This is the selection request currently being processed.
   It is set to zero when the request is fully processed.  */
static struct input_event *x_selection_current_request;

603 604 605 606
/* Display info in x_selection_request.  */

static struct x_display_info *selection_request_dpyinfo;

Richard M. Stallman's avatar
Richard M. Stallman committed
607
/* Used as an unwind-protect clause so that, if a selection-converter signals
608
   an error, we tell the requester that we were unable to do what they wanted
Richard M. Stallman's avatar
Richard M. Stallman committed
609 610 611 612 613 614
   before we throw to top-level or go into the debugger or whatever.  */

static Lisp_Object
x_selection_request_lisp_error (ignore)
     Lisp_Object ignore;
{
615 616
  if (x_selection_current_request != 0
      && selection_request_dpyinfo->display)
Richard M. Stallman's avatar
Richard M. Stallman committed
617 618 619
    x_decline_selection_request (x_selection_current_request);
  return Qnil;
}
620 621 622 623 624 625 626 627

static Lisp_Object
x_catch_errors_unwind (dummy)
     Lisp_Object dummy;
{
  BLOCK_INPUT;
  x_uncatch_errors ();
  UNBLOCK_INPUT;
628
  return Qnil;
629
}
Richard M. Stallman's avatar
Richard M. Stallman committed
630

631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

/* This stuff is so that INCR selections are reentrant (that is, so we can
   be servicing multiple INCR selection requests simultaneously.)  I haven't
   actually tested that yet.  */

/* Keep a list of the property changes that are awaited.  */

struct prop_location
{
  int identifier;
  Display *display;
  Window window;
  Atom property;
  int desired_state;
  int arrived;
  struct prop_location *next;
};

static struct prop_location *expect_property_change ();
static void wait_for_property_change ();
static void unexpect_property_change ();
static int waiting_for_other_props_on_window ();

static int prop_location_identifier;

static Lisp_Object property_change_reply;

static struct prop_location *property_change_reply_object;

static struct prop_location *property_change_wait_list;
661 662

static Lisp_Object
663 664
queue_selection_requests_unwind (tem)
     Lisp_Object tem;
665
{
666
  x_stop_queuing_selection_requests ();
667
  return Qnil;
668 669 670 671 672 673 674 675 676 677 678 679 680
}

/* Return some frame whose display info is DPYINFO.
   Return nil if there is none.  */

static Lisp_Object
some_frame_on_display (dpyinfo)
     struct x_display_info *dpyinfo;
{
  Lisp_Object list, frame;

  FOR_EACH_FRAME (list, frame)
    {
681 682
      if (FRAME_X_P (XFRAME (frame))
          && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
683 684 685 686 687
	return frame;
    }

  return Qnil;
}
688

Richard M. Stallman's avatar
Richard M. Stallman committed
689 690 691 692 693
/* Send the reply to a selection request event EVENT.
   TYPE is the type of selection data requested.
   DATA and SIZE describe the data to send, already converted.
   FORMAT is the unit-size (in bits) of the data to be transmitted.  */

694 695 696 697
#ifdef TRACE_SELECTION
static int cnt;
#endif  /* TRACE_SELECTION */

Richard M. Stallman's avatar
Richard M. Stallman committed
698 699 700 701 702 703 704 705 706
static void
x_reply_selection_request (event, format, data, size, type)
     struct input_event *event;
     int format, size;
     unsigned char *data;
     Atom type;
{
  XSelectionEvent reply;
  Display *display = SELECTION_EVENT_DISPLAY (event);
707
  Window window = SELECTION_EVENT_REQUESTOR (event);
Richard M. Stallman's avatar
Richard M. Stallman committed
708 709 710
  int bytes_remaining;
  int format_bytes = format/8;
  int max_bytes = SELECTION_QUANTUM (display);
711
  struct x_display_info *dpyinfo = x_display_info_for_display (display);
712
  int count = SPECPDL_INDEX ();
Richard M. Stallman's avatar
Richard M. Stallman committed
713 714 715 716 717 718

  if (max_bytes > MAX_SELECTION_QUANTUM)
    max_bytes = MAX_SELECTION_QUANTUM;

  reply.type = SelectionNotify;
  reply.display = display;
719
  reply.requestor = window;
Richard M. Stallman's avatar
Richard M. Stallman committed
720 721 722 723 724 725 726
  reply.selection = SELECTION_EVENT_SELECTION (event);
  reply.time = SELECTION_EVENT_TIME (event);
  reply.target = SELECTION_EVENT_TARGET (event);
  reply.property = SELECTION_EVENT_PROPERTY (event);
  if (reply.property == None)
    reply.property = reply.target;

727
  BLOCK_INPUT;
728 729 730 731
  /* The protected block contains wait_for_property_change, which can
     run random lisp code (process handlers) or signal.  Therefore, we
     put the x_uncatch_errors call in an unwind.  */
  record_unwind_protect (x_catch_errors_unwind, Qnil);
732
  x_catch_errors (display);
Richard M. Stallman's avatar
Richard M. Stallman committed
733

734 735 736 737 738 739 740 741 742 743
#ifdef TRACE_SELECTION
  {
    char *sel = XGetAtomName (display, reply.selection);
    char *tgt = XGetAtomName (display, reply.target);
    TRACE3 ("%s, target %s (%d)", sel, tgt, ++cnt);
    if (sel) XFree (sel);
    if (tgt) XFree (tgt);
  }
#endif /* TRACE_SELECTION */

Richard M. Stallman's avatar
Richard M. Stallman committed
744 745 746 747 748 749 750
  /* Store the data on the requested property.
     If the selection is large, only store the first N bytes of it.
   */
  bytes_remaining = size * format_bytes;
  if (bytes_remaining <= max_bytes)
    {
      /* Send all the data at once, with minimal handshaking.  */
751
      TRACE1 ("Sending all %d bytes", bytes_remaining);
Richard M. Stallman's avatar
Richard M. Stallman committed
752 753 754
      XChangeProperty (display, window, reply.property, type, format,
		       PropModeReplace, data, size);
      /* At this point, the selection was successfully stored; ack it.  */
755
      XSendEvent (display, window, False, 0L, (XEvent *) &reply);
Richard M. Stallman's avatar
Richard M. Stallman committed
756 757 758 759
    }
  else
    {
      /* Send an INCR selection.  */
760
      struct prop_location *wait_object;
761
      int had_errors;
762
      Lisp_Object frame;
Richard M. Stallman's avatar
Richard M. Stallman committed
763

764 765 766 767 768 769 770
      frame = some_frame_on_display (dpyinfo);

      /* If the display no longer has frames, we can't expect
	 to get many more selection requests from it, so don't
	 bother trying to queue them.  */
      if (!NILP (frame))
	{
771
	  x_start_queuing_selection_requests ();
772 773

	  record_unwind_protect (queue_selection_requests_unwind,
774
				 Qnil);
775
	}
776

777
      if (x_window_to_frame (dpyinfo, window)) /* #### debug */
778
	error ("Attempt to transfer an INCR to ourself!");
779

780 781
      TRACE2 ("Start sending %d bytes incrementally (%s)",
	      bytes_remaining,  XGetAtomName (display, reply.property));
782 783
      wait_object = expect_property_change (display, window, reply.property,
					    PropertyDelete);
Richard M. Stallman's avatar
Richard M. Stallman committed
784

785 786
      TRACE1 ("Set %s to number of bytes to send",
	      XGetAtomName (display, reply.property));
787 788 789 790 791 792 793 794 795 796 797
      {
        /* XChangeProperty expects an array of long even if long is more than
           32 bits.  */
        long value[1];

        value[0] = bytes_remaining;
        XChangeProperty (display, window, reply.property, dpyinfo->Xatom_INCR,
                         32, PropModeReplace,
                         (unsigned char *) value, 1);
      }

Richard M. Stallman's avatar
Richard M. Stallman committed
798
      XSelectInput (display, window, PropertyChangeMask);
799

Richard M. Stallman's avatar
Richard M. Stallman committed
800
      /* Tell 'em the INCR data is there...  */
801
      TRACE0 ("Send SelectionNotify event");
802
      XSendEvent (display, window, False, 0L, (XEvent *) &reply);
803
      XFlush (display);
804 805

      had_errors = x_had_errors_p (display);
806
      UNBLOCK_INPUT;
Richard M. Stallman's avatar
Richard M. Stallman committed
807

808
      /* First, wait for the requester to ack by deleting the property.
Richard M. Stallman's avatar
Richard M. Stallman committed
809
	 This can run random lisp code (process handlers) or signal.  */
810
      if (! had_errors)
811 812 813 814 815
	{
	  TRACE1 ("Waiting for ACK (deletion of %s)",
		  XGetAtomName (display, reply.property));
	  wait_for_property_change (wait_object);
	}
816 817
      else
	unexpect_property_change (wait_object);
Richard M. Stallman's avatar
Richard M. Stallman committed
818

819
      TRACE0 ("Got ACK");
Richard M. Stallman's avatar
Richard M. Stallman committed
820 821
      while (bytes_remaining)
	{
822 823 824
          int i = ((bytes_remaining < max_bytes)
                   ? bytes_remaining
                   : max_bytes);
825 826 827

	  BLOCK_INPUT;

828 829 830
	  wait_object
	    = expect_property_change (display, window, reply.property,
				      PropertyDelete);
831 832 833 834

	  TRACE1 ("Sending increment of %d bytes", i);
	  TRACE1 ("Set %s to increment data",
		  XGetAtomName (display, reply.property));
835

Richard M. Stallman's avatar
Richard M. Stallman committed
836 837 838 839 840
	  /* Append the next chunk of data to the property.  */
	  XChangeProperty (display, window, reply.property, type, format,
			   PropModeAppend, data, i / format_bytes);
	  bytes_remaining -= i;
	  data += i;
841
	  XFlush (display);
842
	  had_errors = x_had_errors_p (display);
843
	  UNBLOCK_INPUT;
Richard M. Stallman's avatar
Richard M. Stallman committed
844

845 846 847
	  if (had_errors)
	    break;

848
	  /* Now wait for the requester to ack this chunk by deleting the
Juanma Barranquero's avatar
Juanma Barranquero committed
849
	     property.  This can run random lisp code or signal.  */
850 851
	  TRACE1 ("Waiting for increment ACK (deletion of %s)",
		  XGetAtomName (display, reply.property));
852
	  wait_for_property_change (wait_object);
Richard M. Stallman's avatar
Richard M. Stallman committed
853
	}
854

855 856
      /* Now write a zero-length chunk to the property to tell the
	 requester that we're done.  */
857
      BLOCK_INPUT;
Richard M. Stallman's avatar
Richard M. Stallman committed
858 859 860
      if (! waiting_for_other_props_on_window (display, window))
	XSelectInput (display, window, 0L);

861 862
      TRACE1 ("Set %s to a 0-length chunk to indicate EOF",
	      XGetAtomName (display, reply.property));
Richard M. Stallman's avatar
Richard M. Stallman committed
863 864
      XChangeProperty (display, window, reply.property, type, format,
		       PropModeReplace, data, 0);
865
      TRACE0 ("Done sending incrementally");
Richard M. Stallman's avatar
Richard M. Stallman committed
866
    }
867

Richard M. Stallman's avatar
Richard M. Stallman committed
868
  /* rms, 2003-01-03: I think I have fixed this bug.  */
869 870 871 872 873 874
  /* The window we're communicating with may have been deleted
     in the meantime (that's a real situation from a bug report).
     In this case, there may be events in the event queue still
     refering to the deleted window, and we'll get a BadWindow error
     in XTread_socket when processing the events.  I don't have
     an idea how to fix that.  gerd, 2001-01-98.   */
875 876 877
  /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are
     delivered before uncatch errors.  */
  XSync (display, False);
878
  UNBLOCK_INPUT;
879 880 881 882 883

  /* GTK queues events in addition to the queue in Xlib.  So we
     UNBLOCK to enter the event loop and get possible errors delivered,
     and then BLOCK again because x_uncatch_errors requires it.  */
  BLOCK_INPUT;
884
  /* This calls x_uncatch_errors.  */
885
  unbind_to (count, Qnil);
886
  UNBLOCK_INPUT;
Richard M. Stallman's avatar
Richard M. Stallman committed
887 888 889 890 891
}

/* Handle a SelectionRequest event EVENT.
   This is called from keyboard.c when such an event is found in the queue.  */

892
static void
Richard M. Stallman's avatar
Richard M. Stallman committed
893 894 895 896
x_handle_selection_request (event)
     struct input_event *event;
{
  struct gcpro gcpro1, gcpro2, gcpro3;
897
  Lisp_Object local_selection_data;
Richard M. Stallman's avatar
Richard M. Stallman committed
898
  Lisp_Object selection_symbol;
899 900
  Lisp_Object target_symbol;
  Lisp_Object converted_selection;
Richard M. Stallman's avatar
Richard M. Stallman committed
901
  Time local_selection_time;
902
  Lisp_Object successful_p;
Richard M. Stallman's avatar
Richard M. Stallman committed
903
  int count;
904 905
  struct x_display_info *dpyinfo
    = x_display_info_for_display (SELECTION_EVENT_DISPLAY (event));
Richard M. Stallman's avatar
Richard M. Stallman committed
906

907 908 909
  TRACE2 ("x_handle_selection_request, from=0x%08lx time=%lu",
	  (unsigned long) SELECTION_EVENT_REQUESTOR (event),
	  (unsigned long) SELECTION_EVENT_TIME (event));
910

911 912 913 914 915
  local_selection_data = Qnil;
  target_symbol = Qnil;
  converted_selection = Qnil;
  successful_p = Qnil;

Richard M. Stallman's avatar
Richard M. Stallman committed
916 917
  GCPRO3 (local_selection_data, converted_selection, target_symbol);

918
  selection_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
Richard M. Stallman's avatar
Richard M. Stallman committed
919 920 921 922 923 924 925 926 927 928 929 930 931
				       SELECTION_EVENT_SELECTION (event));

  local_selection_data = assq_no_quit (selection_symbol, Vselection_alist);

  if (NILP (local_selection_data))
    {
      /* Someone asked for the selection, but we don't have it any more.
       */
      x_decline_selection_request (event);
      goto DONE;
    }

  local_selection_time = (Time)
932
    cons_to_long (XCAR (XCDR (XCDR (local_selection_data))));
Richard M. Stallman's avatar
Richard M. Stallman committed
933 934

  if (SELECTION_EVENT_TIME (event) != CurrentTime
935
      && local_selection_time > SELECTION_EVENT_TIME (event))
Richard M. Stallman's avatar
Richard M. Stallman committed
936 937 938 939 940 941 942 943 944
    {
      /* Someone asked for the selection, and we have one, but not the one
	 they're looking for.
       */
      x_decline_selection_request (event);
      goto DONE;
    }

  x_selection_current_request = event;
945
  count = SPECPDL_INDEX ();
946
  selection_request_dpyinfo = dpyinfo;
Richard M. Stallman's avatar
Richard M. Stallman committed
947 948
  record_unwind_protect (x_selection_request_lisp_error, Qnil);

949
  target_symbol = x_atom_to_symbol (SELECTION_EVENT_DISPLAY (event),
Richard M. Stallman's avatar
Richard M. Stallman committed
950 951 952 953 954 955
				    SELECTION_EVENT_TARGET (event));

#if 0 /* #### MULTIPLE doesn't work yet */
  if (EQ (target_symbol, QMULTIPLE))
    target_symbol = fetch_multiple_target (event);
#endif
956

Richard M. Stallman's avatar
Richard M. Stallman committed
957
  /* Convert lisp objects back into binary data */
958

Richard M. Stallman's avatar
Richard M. Stallman committed
959
  converted_selection
Kenichi Handa's avatar
Kenichi Handa committed
960
    = x_get_local_selection (selection_symbol, target_symbol, 0);
961

Richard M. Stallman's avatar
Richard M. Stallman committed
962 963 964 965 966 967
  if (! NILP (converted_selection))
    {
      unsigned char *data;
      unsigned int size;
      int format;
      Atom type;
968 969
      int nofree;

970 971 972 973 974 975
      if (CONSP (converted_selection) && NILP (XCDR (converted_selection)))
        {
          x_decline_selection_request (event);
          goto DONE2;
        }

976 977
      lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event),
				   converted_selection,
978
				   &data, &type, &size, &format, &nofree);
979

Richard M. Stallman's avatar
Richard M. Stallman committed
980 981 982 983
      x_reply_selection_request (event, format, data, size, type);
      successful_p = Qt;

      /* Indicate we have successfully processed this event.  */