| 1 | /* X Selection processing for Emacs. |
| 2 | Copyright (C) 1993-1997, 2000-2011 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 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 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 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. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | |
| 20 | /* Rewritten by jwz */ |
| 21 | |
| 22 | #include <config.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdio.h> /* termhooks.h needs this */ |
| 25 | #include <setjmp.h> |
| 26 | |
| 27 | #ifdef HAVE_SYS_TYPES_H |
| 28 | #include <sys/types.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include "lisp.h" |
| 34 | #include "xterm.h" /* for all of the X includes */ |
| 35 | #include "dispextern.h" /* frame.h seems to want this */ |
| 36 | #include "frame.h" /* Need this to get the X window of selected_frame */ |
| 37 | #include "blockinput.h" |
| 38 | #include "buffer.h" |
| 39 | #include "process.h" |
| 40 | #include "termhooks.h" |
| 41 | #include "keyboard.h" |
| 42 | #include "character.h" |
| 43 | |
| 44 | #include <X11/Xproto.h> |
| 45 | |
| 46 | struct prop_location; |
| 47 | struct selection_data; |
| 48 | |
| 49 | static Lisp_Object x_atom_to_symbol (Display *dpy, Atom atom); |
| 50 | static Atom symbol_to_x_atom (struct x_display_info *, Lisp_Object); |
| 51 | static void x_own_selection (Lisp_Object, Lisp_Object, Lisp_Object); |
| 52 | static Lisp_Object x_get_local_selection (Lisp_Object, Lisp_Object, int, |
| 53 | struct x_display_info *); |
| 54 | static void x_decline_selection_request (struct input_event *); |
| 55 | static Lisp_Object x_selection_request_lisp_error (Lisp_Object); |
| 56 | static Lisp_Object queue_selection_requests_unwind (Lisp_Object); |
| 57 | static Lisp_Object x_catch_errors_unwind (Lisp_Object); |
| 58 | static void x_reply_selection_request (struct input_event *, struct x_display_info *); |
| 59 | static int x_convert_selection (struct input_event *, Lisp_Object, Lisp_Object, |
| 60 | Atom, int, struct x_display_info *); |
| 61 | static int waiting_for_other_props_on_window (Display *, Window); |
| 62 | static struct prop_location *expect_property_change (Display *, Window, |
| 63 | Atom, int); |
| 64 | static void unexpect_property_change (struct prop_location *); |
| 65 | static Lisp_Object wait_for_property_change_unwind (Lisp_Object); |
| 66 | static void wait_for_property_change (struct prop_location *); |
| 67 | static Lisp_Object x_get_foreign_selection (Lisp_Object, Lisp_Object, |
| 68 | Lisp_Object, Lisp_Object); |
| 69 | static Lisp_Object x_get_window_property_as_lisp_data (Display *, |
| 70 | Window, Atom, |
| 71 | Lisp_Object, Atom); |
| 72 | static Lisp_Object selection_data_to_lisp_data (Display *, |
| 73 | const unsigned char *, |
| 74 | ptrdiff_t, Atom, int); |
| 75 | static void lisp_data_to_selection_data (Display *, Lisp_Object, |
| 76 | unsigned char **, Atom *, |
| 77 | ptrdiff_t *, int *, int *); |
| 78 | static Lisp_Object clean_local_selection_data (Lisp_Object); |
| 79 | |
| 80 | /* Printing traces to stderr. */ |
| 81 | |
| 82 | #ifdef TRACE_SELECTION |
| 83 | #define TRACE0(fmt) \ |
| 84 | fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid ()) |
| 85 | #define TRACE1(fmt, a0) \ |
| 86 | fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0) |
| 87 | #define TRACE2(fmt, a0, a1) \ |
| 88 | fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1) |
| 89 | #define TRACE3(fmt, a0, a1, a2) \ |
| 90 | fprintf (stderr, "%"pMd": " fmt "\n", (printmax_t) getpid (), a0, a1, a2) |
| 91 | #else |
| 92 | #define TRACE0(fmt) (void) 0 |
| 93 | #define TRACE1(fmt, a0) (void) 0 |
| 94 | #define TRACE2(fmt, a0, a1) (void) 0 |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | static Lisp_Object QSECONDARY, QSTRING, QINTEGER, QCLIPBOARD, QTIMESTAMP, |
| 99 | QTEXT, QDELETE, QMULTIPLE, QINCR, QEMACS_TMP, QTARGETS, QATOM, QNULL, |
| 100 | QATOM_PAIR, QCLIPBOARD_MANAGER, QSAVE_TARGETS; |
| 101 | |
| 102 | static Lisp_Object QCOMPOUND_TEXT; /* This is a type of selection. */ |
| 103 | static Lisp_Object QUTF8_STRING; /* This is a type of selection. */ |
| 104 | |
| 105 | static Lisp_Object Qcompound_text_with_extensions; |
| 106 | |
| 107 | static Lisp_Object Qforeign_selection; |
| 108 | static Lisp_Object Qx_lost_selection_functions, Qx_sent_selection_functions; |
| 109 | |
| 110 | /* Bytes needed to represent 'long' data. This is as per libX11; it |
| 111 | is not necessarily sizeof (long). */ |
| 112 | #define X_LONG_SIZE 4 |
| 113 | |
| 114 | /* Extreme 'short' and 'long' values suitable for libX11. */ |
| 115 | #define X_SHRT_MAX 0x7fff |
| 116 | #define X_SHRT_MIN (-1 - X_SHRT_MAX) |
| 117 | #define X_LONG_MAX 0x7fffffff |
| 118 | #define X_LONG_MIN (-1 - X_LONG_MAX) |
| 119 | #define X_ULONG_MAX 0xffffffffUL |
| 120 | |
| 121 | /* If this is a smaller number than the max-request-size of the display, |
| 122 | emacs will use INCR selection transfer when the selection is larger |
| 123 | than this. The max-request-size is usually around 64k, so if you want |
| 124 | emacs to use incremental selection transfers when the selection is |
| 125 | smaller than that, set this. I added this mostly for debugging the |
| 126 | incremental transfer stuff, but it might improve server performance. |
| 127 | |
| 128 | This value cannot exceed INT_MAX / max (X_LONG_SIZE, sizeof (long)) |
| 129 | because it is multiplied by X_LONG_SIZE and by sizeof (long) in |
| 130 | subscript calculations. Similarly for PTRDIFF_MAX - 1 or SIZE_MAX |
| 131 | - 1 in place of INT_MAX. */ |
| 132 | #define MAX_SELECTION_QUANTUM \ |
| 133 | ((int) min (0xFFFFFF, (min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) - 1) \ |
| 134 | / max (X_LONG_SIZE, sizeof (long))))) |
| 135 | |
| 136 | static int |
| 137 | selection_quantum (Display *display) |
| 138 | { |
| 139 | long mrs = XMaxRequestSize (display); |
| 140 | return (mrs < MAX_SELECTION_QUANTUM / X_LONG_SIZE + 25 |
| 141 | ? (mrs - 25) * X_LONG_SIZE |
| 142 | : MAX_SELECTION_QUANTUM); |
| 143 | } |
| 144 | |
| 145 | #define LOCAL_SELECTION(selection_symbol,dpyinfo) \ |
| 146 | assq_no_quit (selection_symbol, dpyinfo->terminal->Vselection_alist) |
| 147 | |
| 148 | \f |
| 149 | /* Define a queue to save up SELECTION_REQUEST_EVENT events for later |
| 150 | handling. */ |
| 151 | |
| 152 | struct selection_event_queue |
| 153 | { |
| 154 | struct input_event event; |
| 155 | struct selection_event_queue *next; |
| 156 | }; |
| 157 | |
| 158 | static struct selection_event_queue *selection_queue; |
| 159 | |
| 160 | /* Nonzero means queue up SELECTION_REQUEST_EVENT events. */ |
| 161 | |
| 162 | static int x_queue_selection_requests; |
| 163 | |
| 164 | /* Queue up an SELECTION_REQUEST_EVENT *EVENT, to be processed later. */ |
| 165 | |
| 166 | static void |
| 167 | x_queue_event (struct input_event *event) |
| 168 | { |
| 169 | struct selection_event_queue *queue_tmp; |
| 170 | |
| 171 | /* Don't queue repeated requests. |
| 172 | This only happens for large requests which uses the incremental protocol. */ |
| 173 | for (queue_tmp = selection_queue; queue_tmp; queue_tmp = queue_tmp->next) |
| 174 | { |
| 175 | if (!memcmp (&queue_tmp->event, event, sizeof (*event))) |
| 176 | { |
| 177 | TRACE1 ("DECLINE DUP SELECTION EVENT %p", queue_tmp); |
| 178 | x_decline_selection_request (event); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | queue_tmp |
| 184 | = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue)); |
| 185 | |
| 186 | if (queue_tmp != NULL) |
| 187 | { |
| 188 | TRACE1 ("QUEUE SELECTION EVENT %p", queue_tmp); |
| 189 | queue_tmp->event = *event; |
| 190 | queue_tmp->next = selection_queue; |
| 191 | selection_queue = queue_tmp; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* Start queuing SELECTION_REQUEST_EVENT events. */ |
| 196 | |
| 197 | static void |
| 198 | x_start_queuing_selection_requests (void) |
| 199 | { |
| 200 | if (x_queue_selection_requests) |
| 201 | abort (); |
| 202 | |
| 203 | x_queue_selection_requests++; |
| 204 | TRACE1 ("x_start_queuing_selection_requests %d", x_queue_selection_requests); |
| 205 | } |
| 206 | |
| 207 | /* Stop queuing SELECTION_REQUEST_EVENT events. */ |
| 208 | |
| 209 | static void |
| 210 | x_stop_queuing_selection_requests (void) |
| 211 | { |
| 212 | TRACE1 ("x_stop_queuing_selection_requests %d", x_queue_selection_requests); |
| 213 | --x_queue_selection_requests; |
| 214 | |
| 215 | /* Take all the queued events and put them back |
| 216 | so that they get processed afresh. */ |
| 217 | |
| 218 | while (selection_queue != NULL) |
| 219 | { |
| 220 | struct selection_event_queue *queue_tmp = selection_queue; |
| 221 | TRACE1 ("RESTORE SELECTION EVENT %p", queue_tmp); |
| 222 | kbd_buffer_unget_event (&queue_tmp->event); |
| 223 | selection_queue = queue_tmp->next; |
| 224 | xfree ((char *)queue_tmp); |
| 225 | } |
| 226 | } |
| 227 | \f |
| 228 | |
| 229 | /* This converts a Lisp symbol to a server Atom, avoiding a server |
| 230 | roundtrip whenever possible. */ |
| 231 | |
| 232 | static Atom |
| 233 | symbol_to_x_atom (struct x_display_info *dpyinfo, Lisp_Object sym) |
| 234 | { |
| 235 | Atom val; |
| 236 | if (NILP (sym)) return 0; |
| 237 | if (EQ (sym, QPRIMARY)) return XA_PRIMARY; |
| 238 | if (EQ (sym, QSECONDARY)) return XA_SECONDARY; |
| 239 | if (EQ (sym, QSTRING)) return XA_STRING; |
| 240 | if (EQ (sym, QINTEGER)) return XA_INTEGER; |
| 241 | if (EQ (sym, QATOM)) return XA_ATOM; |
| 242 | if (EQ (sym, QCLIPBOARD)) return dpyinfo->Xatom_CLIPBOARD; |
| 243 | if (EQ (sym, QTIMESTAMP)) return dpyinfo->Xatom_TIMESTAMP; |
| 244 | if (EQ (sym, QTEXT)) return dpyinfo->Xatom_TEXT; |
| 245 | if (EQ (sym, QCOMPOUND_TEXT)) return dpyinfo->Xatom_COMPOUND_TEXT; |
| 246 | if (EQ (sym, QUTF8_STRING)) return dpyinfo->Xatom_UTF8_STRING; |
| 247 | if (EQ (sym, QDELETE)) return dpyinfo->Xatom_DELETE; |
| 248 | if (EQ (sym, QMULTIPLE)) return dpyinfo->Xatom_MULTIPLE; |
| 249 | if (EQ (sym, QINCR)) return dpyinfo->Xatom_INCR; |
| 250 | if (EQ (sym, QEMACS_TMP)) return dpyinfo->Xatom_EMACS_TMP; |
| 251 | if (EQ (sym, QTARGETS)) return dpyinfo->Xatom_TARGETS; |
| 252 | if (EQ (sym, QNULL)) return dpyinfo->Xatom_NULL; |
| 253 | if (!SYMBOLP (sym)) abort (); |
| 254 | |
| 255 | TRACE1 (" XInternAtom %s", SSDATA (SYMBOL_NAME (sym))); |
| 256 | BLOCK_INPUT; |
| 257 | val = XInternAtom (dpyinfo->display, SSDATA (SYMBOL_NAME (sym)), False); |
| 258 | UNBLOCK_INPUT; |
| 259 | return val; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | /* This converts a server Atom to a Lisp symbol, avoiding server roundtrips |
| 264 | and calls to intern whenever possible. */ |
| 265 | |
| 266 | static Lisp_Object |
| 267 | x_atom_to_symbol (Display *dpy, Atom atom) |
| 268 | { |
| 269 | struct x_display_info *dpyinfo; |
| 270 | char *str; |
| 271 | Lisp_Object val; |
| 272 | |
| 273 | if (! atom) |
| 274 | return Qnil; |
| 275 | |
| 276 | switch (atom) |
| 277 | { |
| 278 | case XA_PRIMARY: |
| 279 | return QPRIMARY; |
| 280 | case XA_SECONDARY: |
| 281 | return QSECONDARY; |
| 282 | case XA_STRING: |
| 283 | return QSTRING; |
| 284 | case XA_INTEGER: |
| 285 | return QINTEGER; |
| 286 | case XA_ATOM: |
| 287 | return QATOM; |
| 288 | } |
| 289 | |
| 290 | dpyinfo = x_display_info_for_display (dpy); |
| 291 | if (dpyinfo == NULL) |
| 292 | return Qnil; |
| 293 | if (atom == dpyinfo->Xatom_CLIPBOARD) |
| 294 | return QCLIPBOARD; |
| 295 | if (atom == dpyinfo->Xatom_TIMESTAMP) |
| 296 | return QTIMESTAMP; |
| 297 | if (atom == dpyinfo->Xatom_TEXT) |
| 298 | return QTEXT; |
| 299 | if (atom == dpyinfo->Xatom_COMPOUND_TEXT) |
| 300 | return QCOMPOUND_TEXT; |
| 301 | if (atom == dpyinfo->Xatom_UTF8_STRING) |
| 302 | return QUTF8_STRING; |
| 303 | if (atom == dpyinfo->Xatom_DELETE) |
| 304 | return QDELETE; |
| 305 | if (atom == dpyinfo->Xatom_MULTIPLE) |
| 306 | return QMULTIPLE; |
| 307 | if (atom == dpyinfo->Xatom_INCR) |
| 308 | return QINCR; |
| 309 | if (atom == dpyinfo->Xatom_EMACS_TMP) |
| 310 | return QEMACS_TMP; |
| 311 | if (atom == dpyinfo->Xatom_TARGETS) |
| 312 | return QTARGETS; |
| 313 | if (atom == dpyinfo->Xatom_NULL) |
| 314 | return QNULL; |
| 315 | |
| 316 | BLOCK_INPUT; |
| 317 | str = XGetAtomName (dpy, atom); |
| 318 | UNBLOCK_INPUT; |
| 319 | TRACE1 ("XGetAtomName --> %s", str); |
| 320 | if (! str) return Qnil; |
| 321 | val = intern (str); |
| 322 | BLOCK_INPUT; |
| 323 | /* This was allocated by Xlib, so use XFree. */ |
| 324 | XFree (str); |
| 325 | UNBLOCK_INPUT; |
| 326 | return val; |
| 327 | } |
| 328 | \f |
| 329 | /* Do protocol to assert ourself as a selection owner. |
| 330 | FRAME shall be the owner; it must be a valid X frame. |
| 331 | Update the Vselection_alist so that we can reply to later requests for |
| 332 | our selection. */ |
| 333 | |
| 334 | static void |
| 335 | x_own_selection (Lisp_Object selection_name, Lisp_Object selection_value, |
| 336 | Lisp_Object frame) |
| 337 | { |
| 338 | struct frame *f = XFRAME (frame); |
| 339 | Window selecting_window = FRAME_X_WINDOW (f); |
| 340 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 341 | Display *display = dpyinfo->display; |
| 342 | Time timestamp = last_event_timestamp; |
| 343 | Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_name); |
| 344 | |
| 345 | BLOCK_INPUT; |
| 346 | x_catch_errors (display); |
| 347 | XSetSelectionOwner (display, selection_atom, selecting_window, timestamp); |
| 348 | x_check_errors (display, "Can't set selection: %s"); |
| 349 | x_uncatch_errors (); |
| 350 | UNBLOCK_INPUT; |
| 351 | |
| 352 | /* Now update the local cache */ |
| 353 | { |
| 354 | Lisp_Object selection_data; |
| 355 | Lisp_Object prev_value; |
| 356 | |
| 357 | selection_data = list4 (selection_name, selection_value, |
| 358 | INTEGER_TO_CONS (timestamp), frame); |
| 359 | prev_value = LOCAL_SELECTION (selection_name, dpyinfo); |
| 360 | |
| 361 | dpyinfo->terminal->Vselection_alist |
| 362 | = Fcons (selection_data, dpyinfo->terminal->Vselection_alist); |
| 363 | |
| 364 | /* If we already owned the selection, remove the old selection |
| 365 | data. Don't use Fdelq as that may QUIT. */ |
| 366 | if (!NILP (prev_value)) |
| 367 | { |
| 368 | /* We know it's not the CAR, so it's easy. */ |
| 369 | Lisp_Object rest = dpyinfo->terminal->Vselection_alist; |
| 370 | for (; CONSP (rest); rest = XCDR (rest)) |
| 371 | if (EQ (prev_value, Fcar (XCDR (rest)))) |
| 372 | { |
| 373 | XSETCDR (rest, XCDR (XCDR (rest))); |
| 374 | break; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | \f |
| 380 | /* Given a selection-name and desired type, look up our local copy of |
| 381 | the selection value and convert it to the type. |
| 382 | Return nil, a string, a vector, a symbol, an integer, or a cons |
| 383 | that CONS_TO_INTEGER could plausibly handle. |
| 384 | This function is used both for remote requests (LOCAL_REQUEST is zero) |
| 385 | and for local x-get-selection-internal (LOCAL_REQUEST is nonzero). |
| 386 | |
| 387 | This calls random Lisp code, and may signal or gc. */ |
| 388 | |
| 389 | static Lisp_Object |
| 390 | x_get_local_selection (Lisp_Object selection_symbol, Lisp_Object target_type, |
| 391 | int local_request, struct x_display_info *dpyinfo) |
| 392 | { |
| 393 | Lisp_Object local_value; |
| 394 | Lisp_Object handler_fn, value, check; |
| 395 | |
| 396 | local_value = LOCAL_SELECTION (selection_symbol, dpyinfo); |
| 397 | |
| 398 | if (NILP (local_value)) return Qnil; |
| 399 | |
| 400 | /* TIMESTAMP is a special case. */ |
| 401 | if (EQ (target_type, QTIMESTAMP)) |
| 402 | { |
| 403 | handler_fn = Qnil; |
| 404 | value = XCAR (XCDR (XCDR (local_value))); |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | /* Don't allow a quit within the converter. |
| 409 | When the user types C-g, he would be surprised |
| 410 | if by luck it came during a converter. */ |
| 411 | ptrdiff_t count = SPECPDL_INDEX (); |
| 412 | specbind (Qinhibit_quit, Qt); |
| 413 | |
| 414 | CHECK_SYMBOL (target_type); |
| 415 | handler_fn = Fcdr (Fassq (target_type, Vselection_converter_alist)); |
| 416 | /* gcpro is not needed here since nothing but HANDLER_FN |
| 417 | is live, and that ought to be a symbol. */ |
| 418 | |
| 419 | if (!NILP (handler_fn)) |
| 420 | value = call3 (handler_fn, |
| 421 | selection_symbol, (local_request ? Qnil : target_type), |
| 422 | XCAR (XCDR (local_value))); |
| 423 | else |
| 424 | value = Qnil; |
| 425 | unbind_to (count, Qnil); |
| 426 | } |
| 427 | |
| 428 | /* Make sure this value is of a type that we could transmit |
| 429 | to another X client. */ |
| 430 | |
| 431 | check = value; |
| 432 | if (CONSP (value) |
| 433 | && SYMBOLP (XCAR (value))) |
| 434 | check = XCDR (value); |
| 435 | |
| 436 | if (STRINGP (check) |
| 437 | || VECTORP (check) |
| 438 | || SYMBOLP (check) |
| 439 | || INTEGERP (check) |
| 440 | || NILP (value)) |
| 441 | return value; |
| 442 | /* Check for a value that CONS_TO_INTEGER could handle. */ |
| 443 | else if (CONSP (check) |
| 444 | && INTEGERP (XCAR (check)) |
| 445 | && (INTEGERP (XCDR (check)) |
| 446 | || |
| 447 | (CONSP (XCDR (check)) |
| 448 | && INTEGERP (XCAR (XCDR (check))) |
| 449 | && NILP (XCDR (XCDR (check)))))) |
| 450 | return value; |
| 451 | |
| 452 | signal_error ("Invalid data returned by selection-conversion function", |
| 453 | list2 (handler_fn, value)); |
| 454 | } |
| 455 | \f |
| 456 | /* Subroutines of x_reply_selection_request. */ |
| 457 | |
| 458 | /* Send a SelectionNotify event to the requestor with property=None, |
| 459 | meaning we were unable to do what they wanted. */ |
| 460 | |
| 461 | static void |
| 462 | x_decline_selection_request (struct input_event *event) |
| 463 | { |
| 464 | XEvent reply_base; |
| 465 | XSelectionEvent *reply = &(reply_base.xselection); |
| 466 | |
| 467 | reply->type = SelectionNotify; |
| 468 | reply->display = SELECTION_EVENT_DISPLAY (event); |
| 469 | reply->requestor = SELECTION_EVENT_REQUESTOR (event); |
| 470 | reply->selection = SELECTION_EVENT_SELECTION (event); |
| 471 | reply->time = SELECTION_EVENT_TIME (event); |
| 472 | reply->target = SELECTION_EVENT_TARGET (event); |
| 473 | reply->property = None; |
| 474 | |
| 475 | /* The reason for the error may be that the receiver has |
| 476 | died in the meantime. Handle that case. */ |
| 477 | BLOCK_INPUT; |
| 478 | x_catch_errors (reply->display); |
| 479 | XSendEvent (reply->display, reply->requestor, False, 0L, &reply_base); |
| 480 | XFlush (reply->display); |
| 481 | x_uncatch_errors (); |
| 482 | UNBLOCK_INPUT; |
| 483 | } |
| 484 | |
| 485 | /* This is the selection request currently being processed. |
| 486 | It is set to zero when the request is fully processed. */ |
| 487 | static struct input_event *x_selection_current_request; |
| 488 | |
| 489 | /* Display info in x_selection_request. */ |
| 490 | |
| 491 | static struct x_display_info *selection_request_dpyinfo; |
| 492 | |
| 493 | /* Raw selection data, for sending to a requestor window. */ |
| 494 | |
| 495 | struct selection_data |
| 496 | { |
| 497 | unsigned char *data; |
| 498 | ptrdiff_t size; |
| 499 | int format; |
| 500 | Atom type; |
| 501 | int nofree; |
| 502 | Atom property; |
| 503 | /* This can be set to non-NULL during x_reply_selection_request, if |
| 504 | the selection is waiting for an INCR transfer to complete. Don't |
| 505 | free these; that's done by unexpect_property_change. */ |
| 506 | struct prop_location *wait_object; |
| 507 | struct selection_data *next; |
| 508 | }; |
| 509 | |
| 510 | /* Linked list of the above (in support of MULTIPLE targets). */ |
| 511 | |
| 512 | static struct selection_data *converted_selections; |
| 513 | |
| 514 | /* "Data" to send a requestor for a failed MULTIPLE subtarget. */ |
| 515 | static Atom conversion_fail_tag; |
| 516 | |
| 517 | /* Used as an unwind-protect clause so that, if a selection-converter signals |
| 518 | an error, we tell the requester that we were unable to do what they wanted |
| 519 | before we throw to top-level or go into the debugger or whatever. */ |
| 520 | |
| 521 | static Lisp_Object |
| 522 | x_selection_request_lisp_error (Lisp_Object ignore) |
| 523 | { |
| 524 | struct selection_data *cs, *next; |
| 525 | |
| 526 | for (cs = converted_selections; cs; cs = next) |
| 527 | { |
| 528 | next = cs->next; |
| 529 | if (cs->nofree == 0 && cs->data) |
| 530 | xfree (cs->data); |
| 531 | xfree (cs); |
| 532 | } |
| 533 | converted_selections = NULL; |
| 534 | |
| 535 | if (x_selection_current_request != 0 |
| 536 | && selection_request_dpyinfo->display) |
| 537 | x_decline_selection_request (x_selection_current_request); |
| 538 | return Qnil; |
| 539 | } |
| 540 | |
| 541 | static Lisp_Object |
| 542 | x_catch_errors_unwind (Lisp_Object dummy) |
| 543 | { |
| 544 | BLOCK_INPUT; |
| 545 | x_uncatch_errors (); |
| 546 | UNBLOCK_INPUT; |
| 547 | return Qnil; |
| 548 | } |
| 549 | \f |
| 550 | |
| 551 | /* This stuff is so that INCR selections are reentrant (that is, so we can |
| 552 | be servicing multiple INCR selection requests simultaneously.) I haven't |
| 553 | actually tested that yet. */ |
| 554 | |
| 555 | /* Keep a list of the property changes that are awaited. */ |
| 556 | |
| 557 | struct prop_location |
| 558 | { |
| 559 | int identifier; |
| 560 | Display *display; |
| 561 | Window window; |
| 562 | Atom property; |
| 563 | int desired_state; |
| 564 | int arrived; |
| 565 | struct prop_location *next; |
| 566 | }; |
| 567 | |
| 568 | static struct prop_location *expect_property_change (Display *display, Window window, Atom property, int state); |
| 569 | static void wait_for_property_change (struct prop_location *location); |
| 570 | static void unexpect_property_change (struct prop_location *location); |
| 571 | static int waiting_for_other_props_on_window (Display *display, Window window); |
| 572 | |
| 573 | static int prop_location_identifier; |
| 574 | |
| 575 | static Lisp_Object property_change_reply; |
| 576 | |
| 577 | static struct prop_location *property_change_reply_object; |
| 578 | |
| 579 | static struct prop_location *property_change_wait_list; |
| 580 | |
| 581 | static Lisp_Object |
| 582 | queue_selection_requests_unwind (Lisp_Object tem) |
| 583 | { |
| 584 | x_stop_queuing_selection_requests (); |
| 585 | return Qnil; |
| 586 | } |
| 587 | |
| 588 | \f |
| 589 | /* Send the reply to a selection request event EVENT. */ |
| 590 | |
| 591 | #ifdef TRACE_SELECTION |
| 592 | static int x_reply_selection_request_cnt; |
| 593 | #endif /* TRACE_SELECTION */ |
| 594 | |
| 595 | static void |
| 596 | x_reply_selection_request (struct input_event *event, |
| 597 | struct x_display_info *dpyinfo) |
| 598 | { |
| 599 | XEvent reply_base; |
| 600 | XSelectionEvent *reply = &(reply_base.xselection); |
| 601 | Display *display = SELECTION_EVENT_DISPLAY (event); |
| 602 | Window window = SELECTION_EVENT_REQUESTOR (event); |
| 603 | ptrdiff_t bytes_remaining; |
| 604 | int max_bytes = selection_quantum (display); |
| 605 | ptrdiff_t count = SPECPDL_INDEX (); |
| 606 | struct selection_data *cs; |
| 607 | |
| 608 | reply->type = SelectionNotify; |
| 609 | reply->display = display; |
| 610 | reply->requestor = window; |
| 611 | reply->selection = SELECTION_EVENT_SELECTION (event); |
| 612 | reply->time = SELECTION_EVENT_TIME (event); |
| 613 | reply->target = SELECTION_EVENT_TARGET (event); |
| 614 | reply->property = SELECTION_EVENT_PROPERTY (event); |
| 615 | if (reply->property == None) |
| 616 | reply->property = reply->target; |
| 617 | |
| 618 | BLOCK_INPUT; |
| 619 | /* The protected block contains wait_for_property_change, which can |
| 620 | run random lisp code (process handlers) or signal. Therefore, we |
| 621 | put the x_uncatch_errors call in an unwind. */ |
| 622 | record_unwind_protect (x_catch_errors_unwind, Qnil); |
| 623 | x_catch_errors (display); |
| 624 | |
| 625 | /* Loop over converted selections, storing them in the requested |
| 626 | properties. If data is large, only store the first N bytes |
| 627 | (section 2.7.2 of ICCCM). Note that we store the data for a |
| 628 | MULTIPLE request in the opposite order; the ICCM says only that |
| 629 | the conversion itself must be done in the same order. */ |
| 630 | for (cs = converted_selections; cs; cs = cs->next) |
| 631 | { |
| 632 | if (cs->property == None) |
| 633 | continue; |
| 634 | |
| 635 | bytes_remaining = cs->size; |
| 636 | bytes_remaining *= cs->format >> 3; |
| 637 | if (bytes_remaining <= max_bytes) |
| 638 | { |
| 639 | /* Send all the data at once, with minimal handshaking. */ |
| 640 | TRACE1 ("Sending all %"pD"d bytes", bytes_remaining); |
| 641 | XChangeProperty (display, window, cs->property, |
| 642 | cs->type, cs->format, PropModeReplace, |
| 643 | cs->data, cs->size); |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | /* Send an INCR tag to initiate incremental transfer. */ |
| 648 | long value[1]; |
| 649 | |
| 650 | TRACE2 ("Start sending %"pD"d bytes incrementally (%s)", |
| 651 | bytes_remaining, XGetAtomName (display, cs->property)); |
| 652 | cs->wait_object |
| 653 | = expect_property_change (display, window, cs->property, |
| 654 | PropertyDelete); |
| 655 | |
| 656 | /* XChangeProperty expects an array of long even if long is |
| 657 | more than 32 bits. */ |
| 658 | value[0] = min (bytes_remaining, X_LONG_MAX); |
| 659 | XChangeProperty (display, window, cs->property, |
| 660 | dpyinfo->Xatom_INCR, 32, PropModeReplace, |
| 661 | (unsigned char *) value, 1); |
| 662 | XSelectInput (display, window, PropertyChangeMask); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /* Now issue the SelectionNotify event. */ |
| 667 | XSendEvent (display, window, False, 0L, &reply_base); |
| 668 | XFlush (display); |
| 669 | |
| 670 | #ifdef TRACE_SELECTION |
| 671 | { |
| 672 | char *sel = XGetAtomName (display, reply->selection); |
| 673 | char *tgt = XGetAtomName (display, reply->target); |
| 674 | TRACE3 ("Sent SelectionNotify: %s, target %s (%d)", |
| 675 | sel, tgt, ++x_reply_selection_request_cnt); |
| 676 | if (sel) XFree (sel); |
| 677 | if (tgt) XFree (tgt); |
| 678 | } |
| 679 | #endif /* TRACE_SELECTION */ |
| 680 | |
| 681 | /* Finish sending the rest of each of the INCR values. This should |
| 682 | be improved; there's a chance of deadlock if more than one |
| 683 | subtarget in a MULTIPLE selection requires an INCR transfer, and |
| 684 | the requestor and Emacs loop waiting on different transfers. */ |
| 685 | for (cs = converted_selections; cs; cs = cs->next) |
| 686 | if (cs->wait_object) |
| 687 | { |
| 688 | int format_bytes = cs->format / 8; |
| 689 | int had_errors = x_had_errors_p (display); |
| 690 | UNBLOCK_INPUT; |
| 691 | |
| 692 | bytes_remaining = cs->size; |
| 693 | bytes_remaining *= format_bytes; |
| 694 | |
| 695 | /* Wait for the requester to ack by deleting the property. |
| 696 | This can run Lisp code (process handlers) or signal. */ |
| 697 | if (! had_errors) |
| 698 | { |
| 699 | TRACE1 ("Waiting for ACK (deletion of %s)", |
| 700 | XGetAtomName (display, cs->property)); |
| 701 | wait_for_property_change (cs->wait_object); |
| 702 | } |
| 703 | else |
| 704 | unexpect_property_change (cs->wait_object); |
| 705 | |
| 706 | while (bytes_remaining) |
| 707 | { |
| 708 | int i = ((bytes_remaining < max_bytes) |
| 709 | ? bytes_remaining |
| 710 | : max_bytes) / format_bytes; |
| 711 | BLOCK_INPUT; |
| 712 | |
| 713 | cs->wait_object |
| 714 | = expect_property_change (display, window, cs->property, |
| 715 | PropertyDelete); |
| 716 | |
| 717 | TRACE1 ("Sending increment of %d elements", i); |
| 718 | TRACE1 ("Set %s to increment data", |
| 719 | XGetAtomName (display, cs->property)); |
| 720 | |
| 721 | /* Append the next chunk of data to the property. */ |
| 722 | XChangeProperty (display, window, cs->property, |
| 723 | cs->type, cs->format, PropModeAppend, |
| 724 | cs->data, i); |
| 725 | bytes_remaining -= i * format_bytes; |
| 726 | cs->data += i * ((cs->format == 32) ? sizeof (long) |
| 727 | : format_bytes); |
| 728 | XFlush (display); |
| 729 | had_errors = x_had_errors_p (display); |
| 730 | UNBLOCK_INPUT; |
| 731 | |
| 732 | if (had_errors) break; |
| 733 | |
| 734 | /* Wait for the requester to ack this chunk by deleting |
| 735 | the property. This can run Lisp code or signal. */ |
| 736 | TRACE1 ("Waiting for increment ACK (deletion of %s)", |
| 737 | XGetAtomName (display, cs->property)); |
| 738 | wait_for_property_change (cs->wait_object); |
| 739 | } |
| 740 | |
| 741 | /* Now write a zero-length chunk to the property to tell the |
| 742 | requester that we're done. */ |
| 743 | BLOCK_INPUT; |
| 744 | if (! waiting_for_other_props_on_window (display, window)) |
| 745 | XSelectInput (display, window, 0L); |
| 746 | |
| 747 | TRACE1 ("Set %s to a 0-length chunk to indicate EOF", |
| 748 | XGetAtomName (display, cs->property)); |
| 749 | XChangeProperty (display, window, cs->property, |
| 750 | cs->type, cs->format, PropModeReplace, |
| 751 | cs->data, 0); |
| 752 | TRACE0 ("Done sending incrementally"); |
| 753 | } |
| 754 | |
| 755 | /* rms, 2003-01-03: I think I have fixed this bug. */ |
| 756 | /* The window we're communicating with may have been deleted |
| 757 | in the meantime (that's a real situation from a bug report). |
| 758 | In this case, there may be events in the event queue still |
| 759 | refering to the deleted window, and we'll get a BadWindow error |
| 760 | in XTread_socket when processing the events. I don't have |
| 761 | an idea how to fix that. gerd, 2001-01-98. */ |
| 762 | /* 2004-09-10: XSync and UNBLOCK so that possible protocol errors are |
| 763 | delivered before uncatch errors. */ |
| 764 | XSync (display, False); |
| 765 | UNBLOCK_INPUT; |
| 766 | |
| 767 | /* GTK queues events in addition to the queue in Xlib. So we |
| 768 | UNBLOCK to enter the event loop and get possible errors delivered, |
| 769 | and then BLOCK again because x_uncatch_errors requires it. */ |
| 770 | BLOCK_INPUT; |
| 771 | /* This calls x_uncatch_errors. */ |
| 772 | unbind_to (count, Qnil); |
| 773 | UNBLOCK_INPUT; |
| 774 | } |
| 775 | \f |
| 776 | /* Handle a SelectionRequest event EVENT. |
| 777 | This is called from keyboard.c when such an event is found in the queue. */ |
| 778 | |
| 779 | static void |
| 780 | x_handle_selection_request (struct input_event *event) |
| 781 | { |
| 782 | struct gcpro gcpro1, gcpro2; |
| 783 | Time local_selection_time; |
| 784 | |
| 785 | Display *display = SELECTION_EVENT_DISPLAY (event); |
| 786 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
| 787 | Atom selection = SELECTION_EVENT_SELECTION (event); |
| 788 | Lisp_Object selection_symbol = x_atom_to_symbol (display, selection); |
| 789 | Atom target = SELECTION_EVENT_TARGET (event); |
| 790 | Lisp_Object target_symbol = x_atom_to_symbol (display, target); |
| 791 | Atom property = SELECTION_EVENT_PROPERTY (event); |
| 792 | Lisp_Object local_selection_data; |
| 793 | int success = 0; |
| 794 | ptrdiff_t count = SPECPDL_INDEX (); |
| 795 | GCPRO2 (local_selection_data, target_symbol); |
| 796 | |
| 797 | if (!dpyinfo) goto DONE; |
| 798 | |
| 799 | local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo); |
| 800 | |
| 801 | /* Decline if we don't own any selections. */ |
| 802 | if (NILP (local_selection_data)) goto DONE; |
| 803 | |
| 804 | /* Decline requests issued prior to our acquiring the selection. */ |
| 805 | CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))), |
| 806 | Time, local_selection_time); |
| 807 | if (SELECTION_EVENT_TIME (event) != CurrentTime |
| 808 | && local_selection_time > SELECTION_EVENT_TIME (event)) |
| 809 | goto DONE; |
| 810 | |
| 811 | x_selection_current_request = event; |
| 812 | selection_request_dpyinfo = dpyinfo; |
| 813 | record_unwind_protect (x_selection_request_lisp_error, Qnil); |
| 814 | |
| 815 | /* We might be able to handle nested x_handle_selection_requests, |
| 816 | but this is difficult to test, and seems unimportant. */ |
| 817 | x_start_queuing_selection_requests (); |
| 818 | record_unwind_protect (queue_selection_requests_unwind, Qnil); |
| 819 | |
| 820 | TRACE2 ("x_handle_selection_request: selection=%s, target=%s", |
| 821 | SDATA (SYMBOL_NAME (selection_symbol)), |
| 822 | SDATA (SYMBOL_NAME (target_symbol))); |
| 823 | |
| 824 | if (EQ (target_symbol, QMULTIPLE)) |
| 825 | { |
| 826 | /* For MULTIPLE targets, the event property names a list of atom |
| 827 | pairs; the first atom names a target and the second names a |
| 828 | non-None property. */ |
| 829 | Window requestor = SELECTION_EVENT_REQUESTOR (event); |
| 830 | Lisp_Object multprop; |
| 831 | ptrdiff_t j, nselections; |
| 832 | |
| 833 | if (property == None) goto DONE; |
| 834 | multprop |
| 835 | = x_get_window_property_as_lisp_data (display, requestor, property, |
| 836 | QMULTIPLE, selection); |
| 837 | |
| 838 | if (!VECTORP (multprop) || ASIZE (multprop) % 2) |
| 839 | goto DONE; |
| 840 | |
| 841 | nselections = ASIZE (multprop) / 2; |
| 842 | /* Perform conversions. This can signal. */ |
| 843 | for (j = 0; j < nselections; j++) |
| 844 | { |
| 845 | Lisp_Object subtarget = AREF (multprop, 2*j); |
| 846 | Atom subproperty = symbol_to_x_atom (dpyinfo, |
| 847 | AREF (multprop, 2*j+1)); |
| 848 | |
| 849 | if (subproperty != None) |
| 850 | x_convert_selection (event, selection_symbol, subtarget, |
| 851 | subproperty, 1, dpyinfo); |
| 852 | } |
| 853 | success = 1; |
| 854 | } |
| 855 | else |
| 856 | { |
| 857 | if (property == None) |
| 858 | property = SELECTION_EVENT_TARGET (event); |
| 859 | success = x_convert_selection (event, selection_symbol, |
| 860 | target_symbol, property, |
| 861 | 0, dpyinfo); |
| 862 | } |
| 863 | |
| 864 | DONE: |
| 865 | |
| 866 | if (success) |
| 867 | x_reply_selection_request (event, dpyinfo); |
| 868 | else |
| 869 | x_decline_selection_request (event); |
| 870 | x_selection_current_request = 0; |
| 871 | |
| 872 | /* Run the `x-sent-selection-functions' abnormal hook. */ |
| 873 | if (!NILP (Vx_sent_selection_functions) |
| 874 | && !EQ (Vx_sent_selection_functions, Qunbound)) |
| 875 | { |
| 876 | Lisp_Object args[4]; |
| 877 | args[0] = Qx_sent_selection_functions; |
| 878 | args[1] = selection_symbol; |
| 879 | args[2] = target_symbol; |
| 880 | args[3] = success ? Qt : Qnil; |
| 881 | Frun_hook_with_args (4, args); |
| 882 | } |
| 883 | |
| 884 | unbind_to (count, Qnil); |
| 885 | UNGCPRO; |
| 886 | } |
| 887 | |
| 888 | /* Perform the requested selection conversion, and write the data to |
| 889 | the converted_selections linked list, where it can be accessed by |
| 890 | x_reply_selection_request. If FOR_MULTIPLE is non-zero, write out |
| 891 | the data even if conversion fails, using conversion_fail_tag. |
| 892 | |
| 893 | Return 0 if the selection failed to convert, 1 otherwise. */ |
| 894 | |
| 895 | static int |
| 896 | x_convert_selection (struct input_event *event, Lisp_Object selection_symbol, |
| 897 | Lisp_Object target_symbol, Atom property, |
| 898 | int for_multiple, struct x_display_info *dpyinfo) |
| 899 | { |
| 900 | struct gcpro gcpro1; |
| 901 | Lisp_Object lisp_selection; |
| 902 | struct selection_data *cs; |
| 903 | GCPRO1 (lisp_selection); |
| 904 | |
| 905 | lisp_selection |
| 906 | = x_get_local_selection (selection_symbol, target_symbol, |
| 907 | 0, dpyinfo); |
| 908 | |
| 909 | /* A nil return value means we can't perform the conversion. */ |
| 910 | if (NILP (lisp_selection) |
| 911 | || (CONSP (lisp_selection) && NILP (XCDR (lisp_selection)))) |
| 912 | { |
| 913 | if (for_multiple) |
| 914 | { |
| 915 | cs = xmalloc (sizeof (struct selection_data)); |
| 916 | cs->data = (unsigned char *) &conversion_fail_tag; |
| 917 | cs->size = 1; |
| 918 | cs->format = 32; |
| 919 | cs->type = XA_ATOM; |
| 920 | cs->nofree = 1; |
| 921 | cs->property = property; |
| 922 | cs->wait_object = NULL; |
| 923 | cs->next = converted_selections; |
| 924 | converted_selections = cs; |
| 925 | } |
| 926 | |
| 927 | UNGCPRO; |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /* Otherwise, record the converted selection to binary. */ |
| 932 | cs = xmalloc (sizeof (struct selection_data)); |
| 933 | cs->nofree = 1; |
| 934 | cs->property = property; |
| 935 | cs->wait_object = NULL; |
| 936 | cs->next = converted_selections; |
| 937 | converted_selections = cs; |
| 938 | lisp_data_to_selection_data (SELECTION_EVENT_DISPLAY (event), |
| 939 | lisp_selection, |
| 940 | &(cs->data), &(cs->type), |
| 941 | &(cs->size), &(cs->format), |
| 942 | &(cs->nofree)); |
| 943 | UNGCPRO; |
| 944 | return 1; |
| 945 | } |
| 946 | \f |
| 947 | /* Handle a SelectionClear event EVENT, which indicates that some |
| 948 | client cleared out our previously asserted selection. |
| 949 | This is called from keyboard.c when such an event is found in the queue. */ |
| 950 | |
| 951 | static void |
| 952 | x_handle_selection_clear (struct input_event *event) |
| 953 | { |
| 954 | Display *display = SELECTION_EVENT_DISPLAY (event); |
| 955 | Atom selection = SELECTION_EVENT_SELECTION (event); |
| 956 | Time changed_owner_time = SELECTION_EVENT_TIME (event); |
| 957 | |
| 958 | Lisp_Object selection_symbol, local_selection_data; |
| 959 | Time local_selection_time; |
| 960 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
| 961 | Lisp_Object Vselection_alist; |
| 962 | |
| 963 | TRACE0 ("x_handle_selection_clear"); |
| 964 | |
| 965 | if (!dpyinfo) return; |
| 966 | |
| 967 | selection_symbol = x_atom_to_symbol (display, selection); |
| 968 | local_selection_data = LOCAL_SELECTION (selection_symbol, dpyinfo); |
| 969 | |
| 970 | /* Well, we already believe that we don't own it, so that's just fine. */ |
| 971 | if (NILP (local_selection_data)) return; |
| 972 | |
| 973 | CONS_TO_INTEGER (XCAR (XCDR (XCDR (local_selection_data))), |
| 974 | Time, local_selection_time); |
| 975 | |
| 976 | /* We have reasserted the selection since this SelectionClear was |
| 977 | generated, so we can disregard it. */ |
| 978 | if (changed_owner_time != CurrentTime |
| 979 | && local_selection_time > changed_owner_time) |
| 980 | return; |
| 981 | |
| 982 | /* Otherwise, really clear. Don't use Fdelq as that may QUIT;. */ |
| 983 | Vselection_alist = dpyinfo->terminal->Vselection_alist; |
| 984 | if (EQ (local_selection_data, CAR (Vselection_alist))) |
| 985 | Vselection_alist = XCDR (Vselection_alist); |
| 986 | else |
| 987 | { |
| 988 | Lisp_Object rest; |
| 989 | for (rest = Vselection_alist; CONSP (rest); rest = XCDR (rest)) |
| 990 | if (EQ (local_selection_data, CAR (XCDR (rest)))) |
| 991 | { |
| 992 | XSETCDR (rest, XCDR (XCDR (rest))); |
| 993 | break; |
| 994 | } |
| 995 | } |
| 996 | dpyinfo->terminal->Vselection_alist = Vselection_alist; |
| 997 | |
| 998 | /* Run the `x-lost-selection-functions' abnormal hook. */ |
| 999 | { |
| 1000 | Lisp_Object args[2]; |
| 1001 | args[0] = Qx_lost_selection_functions; |
| 1002 | args[1] = selection_symbol; |
| 1003 | Frun_hook_with_args (2, args); |
| 1004 | } |
| 1005 | |
| 1006 | prepare_menu_bars (); |
| 1007 | redisplay_preserve_echo_area (20); |
| 1008 | } |
| 1009 | |
| 1010 | void |
| 1011 | x_handle_selection_event (struct input_event *event) |
| 1012 | { |
| 1013 | TRACE0 ("x_handle_selection_event"); |
| 1014 | if (event->kind != SELECTION_REQUEST_EVENT) |
| 1015 | x_handle_selection_clear (event); |
| 1016 | else if (x_queue_selection_requests) |
| 1017 | x_queue_event (event); |
| 1018 | else |
| 1019 | x_handle_selection_request (event); |
| 1020 | } |
| 1021 | |
| 1022 | |
| 1023 | /* Clear all selections that were made from frame F. |
| 1024 | We do this when about to delete a frame. */ |
| 1025 | |
| 1026 | void |
| 1027 | x_clear_frame_selections (FRAME_PTR f) |
| 1028 | { |
| 1029 | Lisp_Object frame; |
| 1030 | Lisp_Object rest; |
| 1031 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 1032 | struct terminal *t = dpyinfo->terminal; |
| 1033 | |
| 1034 | XSETFRAME (frame, f); |
| 1035 | |
| 1036 | /* Delete elements from the beginning of Vselection_alist. */ |
| 1037 | while (CONSP (t->Vselection_alist) |
| 1038 | && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (t->Vselection_alist))))))) |
| 1039 | { |
| 1040 | /* Run the `x-lost-selection-functions' abnormal hook. */ |
| 1041 | Lisp_Object args[2]; |
| 1042 | args[0] = Qx_lost_selection_functions; |
| 1043 | args[1] = Fcar (Fcar (t->Vselection_alist)); |
| 1044 | Frun_hook_with_args (2, args); |
| 1045 | |
| 1046 | t->Vselection_alist = XCDR (t->Vselection_alist); |
| 1047 | } |
| 1048 | |
| 1049 | /* Delete elements after the beginning of Vselection_alist. */ |
| 1050 | for (rest = t->Vselection_alist; CONSP (rest); rest = XCDR (rest)) |
| 1051 | if (CONSP (XCDR (rest)) |
| 1052 | && EQ (frame, XCAR (XCDR (XCDR (XCDR (XCAR (XCDR (rest)))))))) |
| 1053 | { |
| 1054 | Lisp_Object args[2]; |
| 1055 | args[0] = Qx_lost_selection_functions; |
| 1056 | args[1] = XCAR (XCAR (XCDR (rest))); |
| 1057 | Frun_hook_with_args (2, args); |
| 1058 | XSETCDR (rest, XCDR (XCDR (rest))); |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | \f |
| 1063 | /* Nonzero if any properties for DISPLAY and WINDOW |
| 1064 | are on the list of what we are waiting for. */ |
| 1065 | |
| 1066 | static int |
| 1067 | waiting_for_other_props_on_window (Display *display, Window window) |
| 1068 | { |
| 1069 | struct prop_location *rest = property_change_wait_list; |
| 1070 | while (rest) |
| 1071 | if (rest->display == display && rest->window == window) |
| 1072 | return 1; |
| 1073 | else |
| 1074 | rest = rest->next; |
| 1075 | return 0; |
| 1076 | } |
| 1077 | |
| 1078 | /* Add an entry to the list of property changes we are waiting for. |
| 1079 | DISPLAY, WINDOW, PROPERTY, STATE describe what we will wait for. |
| 1080 | The return value is a number that uniquely identifies |
| 1081 | this awaited property change. */ |
| 1082 | |
| 1083 | static struct prop_location * |
| 1084 | expect_property_change (Display *display, Window window, |
| 1085 | Atom property, int state) |
| 1086 | { |
| 1087 | struct prop_location *pl = (struct prop_location *) xmalloc (sizeof *pl); |
| 1088 | pl->identifier = ++prop_location_identifier; |
| 1089 | pl->display = display; |
| 1090 | pl->window = window; |
| 1091 | pl->property = property; |
| 1092 | pl->desired_state = state; |
| 1093 | pl->next = property_change_wait_list; |
| 1094 | pl->arrived = 0; |
| 1095 | property_change_wait_list = pl; |
| 1096 | return pl; |
| 1097 | } |
| 1098 | |
| 1099 | /* Delete an entry from the list of property changes we are waiting for. |
| 1100 | IDENTIFIER is the number that uniquely identifies the entry. */ |
| 1101 | |
| 1102 | static void |
| 1103 | unexpect_property_change (struct prop_location *location) |
| 1104 | { |
| 1105 | struct prop_location *prev = 0, *rest = property_change_wait_list; |
| 1106 | while (rest) |
| 1107 | { |
| 1108 | if (rest == location) |
| 1109 | { |
| 1110 | if (prev) |
| 1111 | prev->next = rest->next; |
| 1112 | else |
| 1113 | property_change_wait_list = rest->next; |
| 1114 | xfree (rest); |
| 1115 | return; |
| 1116 | } |
| 1117 | prev = rest; |
| 1118 | rest = rest->next; |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | /* Remove the property change expectation element for IDENTIFIER. */ |
| 1123 | |
| 1124 | static Lisp_Object |
| 1125 | wait_for_property_change_unwind (Lisp_Object loc) |
| 1126 | { |
| 1127 | struct prop_location *location = XSAVE_VALUE (loc)->pointer; |
| 1128 | |
| 1129 | unexpect_property_change (location); |
| 1130 | if (location == property_change_reply_object) |
| 1131 | property_change_reply_object = 0; |
| 1132 | return Qnil; |
| 1133 | } |
| 1134 | |
| 1135 | /* Actually wait for a property change. |
| 1136 | IDENTIFIER should be the value that expect_property_change returned. */ |
| 1137 | |
| 1138 | static void |
| 1139 | wait_for_property_change (struct prop_location *location) |
| 1140 | { |
| 1141 | int secs, usecs; |
| 1142 | ptrdiff_t count = SPECPDL_INDEX (); |
| 1143 | |
| 1144 | if (property_change_reply_object) |
| 1145 | abort (); |
| 1146 | |
| 1147 | /* Make sure to do unexpect_property_change if we quit or err. */ |
| 1148 | record_unwind_protect (wait_for_property_change_unwind, |
| 1149 | make_save_value (location, 0)); |
| 1150 | |
| 1151 | XSETCAR (property_change_reply, Qnil); |
| 1152 | property_change_reply_object = location; |
| 1153 | |
| 1154 | /* If the event we are waiting for arrives beyond here, it will set |
| 1155 | property_change_reply, because property_change_reply_object says so. */ |
| 1156 | if (! location->arrived) |
| 1157 | { |
| 1158 | secs = x_selection_timeout / 1000; |
| 1159 | usecs = (x_selection_timeout % 1000) * 1000; |
| 1160 | TRACE2 (" Waiting %d secs, %d usecs", secs, usecs); |
| 1161 | wait_reading_process_output (secs, usecs, 0, 0, |
| 1162 | property_change_reply, NULL, 0); |
| 1163 | |
| 1164 | if (NILP (XCAR (property_change_reply))) |
| 1165 | { |
| 1166 | TRACE0 (" Timed out"); |
| 1167 | error ("Timed out waiting for property-notify event"); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | unbind_to (count, Qnil); |
| 1172 | } |
| 1173 | |
| 1174 | /* Called from XTread_socket in response to a PropertyNotify event. */ |
| 1175 | |
| 1176 | void |
| 1177 | x_handle_property_notify (XPropertyEvent *event) |
| 1178 | { |
| 1179 | struct prop_location *rest; |
| 1180 | |
| 1181 | for (rest = property_change_wait_list; rest; rest = rest->next) |
| 1182 | { |
| 1183 | if (!rest->arrived |
| 1184 | && rest->property == event->atom |
| 1185 | && rest->window == event->window |
| 1186 | && rest->display == event->display |
| 1187 | && rest->desired_state == event->state) |
| 1188 | { |
| 1189 | TRACE2 ("Expected %s of property %s", |
| 1190 | (event->state == PropertyDelete ? "deletion" : "change"), |
| 1191 | XGetAtomName (event->display, event->atom)); |
| 1192 | |
| 1193 | rest->arrived = 1; |
| 1194 | |
| 1195 | /* If this is the one wait_for_property_change is waiting for, |
| 1196 | tell it to wake up. */ |
| 1197 | if (rest == property_change_reply_object) |
| 1198 | XSETCAR (property_change_reply, Qt); |
| 1199 | |
| 1200 | return; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | \f |
| 1207 | /* Variables for communication with x_handle_selection_notify. */ |
| 1208 | static Atom reading_which_selection; |
| 1209 | static Lisp_Object reading_selection_reply; |
| 1210 | static Window reading_selection_window; |
| 1211 | |
| 1212 | /* Do protocol to read selection-data from the server. |
| 1213 | Converts this to Lisp data and returns it. |
| 1214 | FRAME is the frame whose X window shall request the selection. */ |
| 1215 | |
| 1216 | static Lisp_Object |
| 1217 | x_get_foreign_selection (Lisp_Object selection_symbol, Lisp_Object target_type, |
| 1218 | Lisp_Object time_stamp, Lisp_Object frame) |
| 1219 | { |
| 1220 | struct frame *f = XFRAME (frame); |
| 1221 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 1222 | Display *display = dpyinfo->display; |
| 1223 | Window requestor_window = FRAME_X_WINDOW (f); |
| 1224 | Time requestor_time = last_event_timestamp; |
| 1225 | Atom target_property = dpyinfo->Xatom_EMACS_TMP; |
| 1226 | Atom selection_atom = symbol_to_x_atom (dpyinfo, selection_symbol); |
| 1227 | Atom type_atom = (CONSP (target_type) |
| 1228 | ? symbol_to_x_atom (dpyinfo, XCAR (target_type)) |
| 1229 | : symbol_to_x_atom (dpyinfo, target_type)); |
| 1230 | int secs, usecs; |
| 1231 | |
| 1232 | if (!FRAME_LIVE_P (f)) |
| 1233 | return Qnil; |
| 1234 | |
| 1235 | if (! NILP (time_stamp)) |
| 1236 | CONS_TO_INTEGER (time_stamp, Time, requestor_time); |
| 1237 | |
| 1238 | BLOCK_INPUT; |
| 1239 | TRACE2 ("Get selection %s, type %s", |
| 1240 | XGetAtomName (display, type_atom), |
| 1241 | XGetAtomName (display, target_property)); |
| 1242 | |
| 1243 | x_catch_errors (display); |
| 1244 | XConvertSelection (display, selection_atom, type_atom, target_property, |
| 1245 | requestor_window, requestor_time); |
| 1246 | x_check_errors (display, "Can't convert selection: %s"); |
| 1247 | x_uncatch_errors (); |
| 1248 | |
| 1249 | /* Prepare to block until the reply has been read. */ |
| 1250 | reading_selection_window = requestor_window; |
| 1251 | reading_which_selection = selection_atom; |
| 1252 | XSETCAR (reading_selection_reply, Qnil); |
| 1253 | |
| 1254 | /* It should not be necessary to stop handling selection requests |
| 1255 | during this time. In fact, the SAVE_TARGETS mechanism requires |
| 1256 | us to handle a clipboard manager's requests before it returns |
| 1257 | SelectionNotify. */ |
| 1258 | #if 0 |
| 1259 | x_start_queuing_selection_requests (); |
| 1260 | record_unwind_protect (queue_selection_requests_unwind, Qnil); |
| 1261 | #endif |
| 1262 | |
| 1263 | UNBLOCK_INPUT; |
| 1264 | |
| 1265 | /* This allows quits. Also, don't wait forever. */ |
| 1266 | secs = x_selection_timeout / 1000; |
| 1267 | usecs = (x_selection_timeout % 1000) * 1000; |
| 1268 | TRACE1 (" Start waiting %d secs for SelectionNotify", secs); |
| 1269 | wait_reading_process_output (secs, usecs, 0, 0, |
| 1270 | reading_selection_reply, NULL, 0); |
| 1271 | TRACE1 (" Got event = %d", !NILP (XCAR (reading_selection_reply))); |
| 1272 | |
| 1273 | if (NILP (XCAR (reading_selection_reply))) |
| 1274 | error ("Timed out waiting for reply from selection owner"); |
| 1275 | if (EQ (XCAR (reading_selection_reply), Qlambda)) |
| 1276 | return Qnil; |
| 1277 | |
| 1278 | /* Otherwise, the selection is waiting for us on the requested property. */ |
| 1279 | return |
| 1280 | x_get_window_property_as_lisp_data (display, requestor_window, |
| 1281 | target_property, target_type, |
| 1282 | selection_atom); |
| 1283 | } |
| 1284 | \f |
| 1285 | /* Subroutines of x_get_window_property_as_lisp_data */ |
| 1286 | |
| 1287 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
| 1288 | |
| 1289 | static void |
| 1290 | x_get_window_property (Display *display, Window window, Atom property, |
| 1291 | unsigned char **data_ret, ptrdiff_t *bytes_ret, |
| 1292 | Atom *actual_type_ret, int *actual_format_ret, |
| 1293 | unsigned long *actual_size_ret, int delete_p) |
| 1294 | { |
| 1295 | ptrdiff_t total_size; |
| 1296 | unsigned long bytes_remaining; |
| 1297 | ptrdiff_t offset = 0; |
| 1298 | unsigned char *data = 0; |
| 1299 | unsigned char *tmp_data = 0; |
| 1300 | int result; |
| 1301 | int buffer_size = selection_quantum (display); |
| 1302 | |
| 1303 | /* Wide enough to avoid overflow in expressions using it. */ |
| 1304 | ptrdiff_t x_long_size = X_LONG_SIZE; |
| 1305 | |
| 1306 | /* Maximum value for TOTAL_SIZE. It cannot exceed PTRDIFF_MAX - 1 |
| 1307 | and SIZE_MAX - 1, for an extra byte at the end. And it cannot |
| 1308 | exceed LONG_MAX * X_LONG_SIZE, for XGetWindowProperty. */ |
| 1309 | ptrdiff_t total_size_max = |
| 1310 | ((min (PTRDIFF_MAX, SIZE_MAX) - 1) / x_long_size < LONG_MAX |
| 1311 | ? min (PTRDIFF_MAX, SIZE_MAX) - 1 |
| 1312 | : LONG_MAX * x_long_size); |
| 1313 | |
| 1314 | BLOCK_INPUT; |
| 1315 | |
| 1316 | /* First probe the thing to find out how big it is. */ |
| 1317 | result = XGetWindowProperty (display, window, property, |
| 1318 | 0L, 0L, False, AnyPropertyType, |
| 1319 | actual_type_ret, actual_format_ret, |
| 1320 | actual_size_ret, |
| 1321 | &bytes_remaining, &tmp_data); |
| 1322 | if (result != Success) |
| 1323 | goto done; |
| 1324 | |
| 1325 | /* This was allocated by Xlib, so use XFree. */ |
| 1326 | XFree ((char *) tmp_data); |
| 1327 | |
| 1328 | if (*actual_type_ret == None || *actual_format_ret == 0) |
| 1329 | goto done; |
| 1330 | |
| 1331 | if (total_size_max < bytes_remaining) |
| 1332 | goto size_overflow; |
| 1333 | total_size = bytes_remaining; |
| 1334 | data = malloc (total_size + 1); |
| 1335 | if (! data) |
| 1336 | goto memory_exhausted; |
| 1337 | |
| 1338 | /* Now read, until we've gotten it all. */ |
| 1339 | while (bytes_remaining) |
| 1340 | { |
| 1341 | ptrdiff_t bytes_gotten; |
| 1342 | int bytes_per_item; |
| 1343 | result |
| 1344 | = XGetWindowProperty (display, window, property, |
| 1345 | offset / X_LONG_SIZE, |
| 1346 | buffer_size / X_LONG_SIZE, |
| 1347 | False, |
| 1348 | AnyPropertyType, |
| 1349 | actual_type_ret, actual_format_ret, |
| 1350 | actual_size_ret, &bytes_remaining, &tmp_data); |
| 1351 | |
| 1352 | /* If this doesn't return Success at this point, it means that |
| 1353 | some clod deleted the selection while we were in the midst of |
| 1354 | reading it. Deal with that, I guess.... */ |
| 1355 | if (result != Success) |
| 1356 | break; |
| 1357 | |
| 1358 | bytes_per_item = *actual_format_ret >> 3; |
| 1359 | xassert (*actual_size_ret <= buffer_size / bytes_per_item); |
| 1360 | |
| 1361 | /* The man page for XGetWindowProperty says: |
| 1362 | "If the returned format is 32, the returned data is represented |
| 1363 | as a long array and should be cast to that type to obtain the |
| 1364 | elements." |
| 1365 | This applies even if long is more than 32 bits, the X library |
| 1366 | converts from 32 bit elements received from the X server to long |
| 1367 | and passes the long array to us. Thus, for that case memcpy can not |
| 1368 | be used. We convert to a 32 bit type here, because so much code |
| 1369 | assume on that. |
| 1370 | |
| 1371 | The bytes and offsets passed to XGetWindowProperty refers to the |
| 1372 | property and those are indeed in 32 bit quantities if format is 32. */ |
| 1373 | |
| 1374 | bytes_gotten = *actual_size_ret; |
| 1375 | bytes_gotten *= bytes_per_item; |
| 1376 | |
| 1377 | TRACE2 ("Read %"pD"d bytes from property %s", |
| 1378 | bytes_gotten, XGetAtomName (display, property)); |
| 1379 | |
| 1380 | if (total_size - offset < bytes_gotten) |
| 1381 | { |
| 1382 | unsigned char *data1; |
| 1383 | ptrdiff_t remaining_lim = total_size_max - offset - bytes_gotten; |
| 1384 | if (remaining_lim < 0 || remaining_lim < bytes_remaining) |
| 1385 | goto size_overflow; |
| 1386 | total_size = offset + bytes_gotten + bytes_remaining; |
| 1387 | data1 = realloc (data, total_size + 1); |
| 1388 | if (! data1) |
| 1389 | goto memory_exhausted; |
| 1390 | data = data1; |
| 1391 | } |
| 1392 | |
| 1393 | if (32 < BITS_PER_LONG && *actual_format_ret == 32) |
| 1394 | { |
| 1395 | unsigned long i; |
| 1396 | int *idata = (int *) (data + offset); |
| 1397 | long *ldata = (long *) tmp_data; |
| 1398 | |
| 1399 | for (i = 0; i < *actual_size_ret; ++i) |
| 1400 | idata[i] = ldata[i]; |
| 1401 | } |
| 1402 | else |
| 1403 | memcpy (data + offset, tmp_data, bytes_gotten); |
| 1404 | |
| 1405 | offset += bytes_gotten; |
| 1406 | |
| 1407 | /* This was allocated by Xlib, so use XFree. */ |
| 1408 | XFree ((char *) tmp_data); |
| 1409 | } |
| 1410 | |
| 1411 | XFlush (display); |
| 1412 | data[offset] = '\0'; |
| 1413 | |
| 1414 | done: |
| 1415 | UNBLOCK_INPUT; |
| 1416 | *data_ret = data; |
| 1417 | *bytes_ret = offset; |
| 1418 | return; |
| 1419 | |
| 1420 | size_overflow: |
| 1421 | free (data); |
| 1422 | UNBLOCK_INPUT; |
| 1423 | memory_full (SIZE_MAX); |
| 1424 | |
| 1425 | memory_exhausted: |
| 1426 | free (data); |
| 1427 | UNBLOCK_INPUT; |
| 1428 | memory_full (total_size + 1); |
| 1429 | } |
| 1430 | \f |
| 1431 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
| 1432 | |
| 1433 | static void |
| 1434 | receive_incremental_selection (Display *display, Window window, Atom property, |
| 1435 | Lisp_Object target_type, |
| 1436 | unsigned int min_size_bytes, |
| 1437 | unsigned char **data_ret, |
| 1438 | ptrdiff_t *size_bytes_ret, |
| 1439 | Atom *type_ret, int *format_ret, |
| 1440 | unsigned long *size_ret) |
| 1441 | { |
| 1442 | ptrdiff_t offset = 0; |
| 1443 | struct prop_location *wait_object; |
| 1444 | if (min (PTRDIFF_MAX, SIZE_MAX) < min_size_bytes) |
| 1445 | memory_full (SIZE_MAX); |
| 1446 | *data_ret = (unsigned char *) xmalloc (min_size_bytes); |
| 1447 | *size_bytes_ret = min_size_bytes; |
| 1448 | |
| 1449 | TRACE1 ("Read %u bytes incrementally", min_size_bytes); |
| 1450 | |
| 1451 | /* At this point, we have read an INCR property. |
| 1452 | Delete the property to ack it. |
| 1453 | (But first, prepare to receive the next event in this handshake.) |
| 1454 | |
| 1455 | Now, we must loop, waiting for the sending window to put a value on |
| 1456 | that property, then reading the property, then deleting it to ack. |
| 1457 | We are done when the sender places a property of length 0. |
| 1458 | */ |
| 1459 | BLOCK_INPUT; |
| 1460 | XSelectInput (display, window, STANDARD_EVENT_SET | PropertyChangeMask); |
| 1461 | TRACE1 (" Delete property %s", |
| 1462 | SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property)))); |
| 1463 | XDeleteProperty (display, window, property); |
| 1464 | TRACE1 (" Expect new value of property %s", |
| 1465 | SDATA (SYMBOL_NAME (x_atom_to_symbol (display, property)))); |
| 1466 | wait_object = expect_property_change (display, window, property, |
| 1467 | PropertyNewValue); |
| 1468 | XFlush (display); |
| 1469 | UNBLOCK_INPUT; |
| 1470 | |
| 1471 | while (1) |
| 1472 | { |
| 1473 | unsigned char *tmp_data; |
| 1474 | ptrdiff_t tmp_size_bytes; |
| 1475 | |
| 1476 | TRACE0 (" Wait for property change"); |
| 1477 | wait_for_property_change (wait_object); |
| 1478 | |
| 1479 | /* expect it again immediately, because x_get_window_property may |
| 1480 | .. no it won't, I don't get it. |
| 1481 | .. Ok, I get it now, the Xt code that implements INCR is broken. */ |
| 1482 | TRACE0 (" Get property value"); |
| 1483 | x_get_window_property (display, window, property, |
| 1484 | &tmp_data, &tmp_size_bytes, |
| 1485 | type_ret, format_ret, size_ret, 1); |
| 1486 | |
| 1487 | TRACE1 (" Read increment of %"pD"d bytes", tmp_size_bytes); |
| 1488 | |
| 1489 | if (tmp_size_bytes == 0) /* we're done */ |
| 1490 | { |
| 1491 | TRACE0 ("Done reading incrementally"); |
| 1492 | |
| 1493 | if (! waiting_for_other_props_on_window (display, window)) |
| 1494 | XSelectInput (display, window, STANDARD_EVENT_SET); |
| 1495 | /* Use xfree, not XFree, because x_get_window_property |
| 1496 | calls xmalloc itself. */ |
| 1497 | xfree (tmp_data); |
| 1498 | break; |
| 1499 | } |
| 1500 | |
| 1501 | BLOCK_INPUT; |
| 1502 | TRACE1 (" ACK by deleting property %s", |
| 1503 | XGetAtomName (display, property)); |
| 1504 | XDeleteProperty (display, window, property); |
| 1505 | wait_object = expect_property_change (display, window, property, |
| 1506 | PropertyNewValue); |
| 1507 | XFlush (display); |
| 1508 | UNBLOCK_INPUT; |
| 1509 | |
| 1510 | if (*size_bytes_ret - offset < tmp_size_bytes) |
| 1511 | *data_ret = xpalloc (*data_ret, size_bytes_ret, |
| 1512 | tmp_size_bytes - (*size_bytes_ret - offset), |
| 1513 | -1, 1); |
| 1514 | |
| 1515 | memcpy ((*data_ret) + offset, tmp_data, tmp_size_bytes); |
| 1516 | offset += tmp_size_bytes; |
| 1517 | |
| 1518 | /* Use xfree, not XFree, because x_get_window_property |
| 1519 | calls xmalloc itself. */ |
| 1520 | xfree (tmp_data); |
| 1521 | } |
| 1522 | } |
| 1523 | |
| 1524 | \f |
| 1525 | /* Fetch a value from property PROPERTY of X window WINDOW on display |
| 1526 | DISPLAY. TARGET_TYPE and SELECTION_ATOM are used in error message |
| 1527 | if this fails. */ |
| 1528 | |
| 1529 | static Lisp_Object |
| 1530 | x_get_window_property_as_lisp_data (Display *display, Window window, |
| 1531 | Atom property, |
| 1532 | Lisp_Object target_type, |
| 1533 | Atom selection_atom) |
| 1534 | { |
| 1535 | Atom actual_type; |
| 1536 | int actual_format; |
| 1537 | unsigned long actual_size; |
| 1538 | unsigned char *data = 0; |
| 1539 | ptrdiff_t bytes = 0; |
| 1540 | Lisp_Object val; |
| 1541 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
| 1542 | |
| 1543 | TRACE0 ("Reading selection data"); |
| 1544 | |
| 1545 | x_get_window_property (display, window, property, &data, &bytes, |
| 1546 | &actual_type, &actual_format, &actual_size, 1); |
| 1547 | if (! data) |
| 1548 | { |
| 1549 | int there_is_a_selection_owner; |
| 1550 | BLOCK_INPUT; |
| 1551 | there_is_a_selection_owner |
| 1552 | = XGetSelectionOwner (display, selection_atom); |
| 1553 | UNBLOCK_INPUT; |
| 1554 | if (there_is_a_selection_owner) |
| 1555 | signal_error ("Selection owner couldn't convert", |
| 1556 | actual_type |
| 1557 | ? list2 (target_type, |
| 1558 | x_atom_to_symbol (display, actual_type)) |
| 1559 | : target_type); |
| 1560 | else |
| 1561 | signal_error ("No selection", |
| 1562 | x_atom_to_symbol (display, selection_atom)); |
| 1563 | } |
| 1564 | |
| 1565 | if (actual_type == dpyinfo->Xatom_INCR) |
| 1566 | { |
| 1567 | /* That wasn't really the data, just the beginning. */ |
| 1568 | |
| 1569 | unsigned int min_size_bytes = * ((unsigned int *) data); |
| 1570 | BLOCK_INPUT; |
| 1571 | /* Use xfree, not XFree, because x_get_window_property |
| 1572 | calls xmalloc itself. */ |
| 1573 | xfree ((char *) data); |
| 1574 | UNBLOCK_INPUT; |
| 1575 | receive_incremental_selection (display, window, property, target_type, |
| 1576 | min_size_bytes, &data, &bytes, |
| 1577 | &actual_type, &actual_format, |
| 1578 | &actual_size); |
| 1579 | } |
| 1580 | |
| 1581 | BLOCK_INPUT; |
| 1582 | TRACE1 (" Delete property %s", XGetAtomName (display, property)); |
| 1583 | XDeleteProperty (display, window, property); |
| 1584 | XFlush (display); |
| 1585 | UNBLOCK_INPUT; |
| 1586 | |
| 1587 | /* It's been read. Now convert it to a lisp object in some semi-rational |
| 1588 | manner. */ |
| 1589 | val = selection_data_to_lisp_data (display, data, bytes, |
| 1590 | actual_type, actual_format); |
| 1591 | |
| 1592 | /* Use xfree, not XFree, because x_get_window_property |
| 1593 | calls xmalloc itself. */ |
| 1594 | xfree ((char *) data); |
| 1595 | return val; |
| 1596 | } |
| 1597 | \f |
| 1598 | /* These functions convert from the selection data read from the server into |
| 1599 | something that we can use from Lisp, and vice versa. |
| 1600 | |
| 1601 | Type: Format: Size: Lisp Type: |
| 1602 | ----- ------- ----- ----------- |
| 1603 | * 8 * String |
| 1604 | ATOM 32 1 Symbol |
| 1605 | ATOM 32 > 1 Vector of Symbols |
| 1606 | * 16 1 Integer |
| 1607 | * 16 > 1 Vector of Integers |
| 1608 | * 32 1 if <=16 bits: Integer |
| 1609 | if > 16 bits: Cons of top16, bot16 |
| 1610 | * 32 > 1 Vector of the above |
| 1611 | |
| 1612 | When converting a Lisp number to C, it is assumed to be of format 16 if |
| 1613 | it is an integer, and of format 32 if it is a cons of two integers. |
| 1614 | |
| 1615 | When converting a vector of numbers from Lisp to C, it is assumed to be |
| 1616 | of format 16 if every element in the vector is an integer, and is assumed |
| 1617 | to be of format 32 if any element is a cons of two integers. |
| 1618 | |
| 1619 | When converting an object to C, it may be of the form (SYMBOL . <data>) |
| 1620 | where SYMBOL is what we should claim that the type is. Format and |
| 1621 | representation are as above. |
| 1622 | |
| 1623 | Important: When format is 32, data should contain an array of int, |
| 1624 | not an array of long as the X library returns. This makes a difference |
| 1625 | when sizeof(long) != sizeof(int). */ |
| 1626 | |
| 1627 | |
| 1628 | |
| 1629 | static Lisp_Object |
| 1630 | selection_data_to_lisp_data (Display *display, const unsigned char *data, |
| 1631 | ptrdiff_t size, Atom type, int format) |
| 1632 | { |
| 1633 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
| 1634 | |
| 1635 | if (type == dpyinfo->Xatom_NULL) |
| 1636 | return QNULL; |
| 1637 | |
| 1638 | /* Convert any 8-bit data to a string, for compactness. */ |
| 1639 | else if (format == 8) |
| 1640 | { |
| 1641 | Lisp_Object str, lispy_type; |
| 1642 | |
| 1643 | str = make_unibyte_string ((char *) data, size); |
| 1644 | /* Indicate that this string is from foreign selection by a text |
| 1645 | property `foreign-selection' so that the caller of |
| 1646 | x-get-selection-internal (usually x-get-selection) can know |
| 1647 | that the string must be decode. */ |
| 1648 | if (type == dpyinfo->Xatom_COMPOUND_TEXT) |
| 1649 | lispy_type = QCOMPOUND_TEXT; |
| 1650 | else if (type == dpyinfo->Xatom_UTF8_STRING) |
| 1651 | lispy_type = QUTF8_STRING; |
| 1652 | else |
| 1653 | lispy_type = QSTRING; |
| 1654 | Fput_text_property (make_number (0), make_number (size), |
| 1655 | Qforeign_selection, lispy_type, str); |
| 1656 | return str; |
| 1657 | } |
| 1658 | /* Convert a single atom to a Lisp_Symbol. Convert a set of atoms to |
| 1659 | a vector of symbols. */ |
| 1660 | else if (type == XA_ATOM |
| 1661 | /* Treat ATOM_PAIR type similar to list of atoms. */ |
| 1662 | || type == dpyinfo->Xatom_ATOM_PAIR) |
| 1663 | { |
| 1664 | ptrdiff_t i; |
| 1665 | /* On a 64 bit machine sizeof(Atom) == sizeof(long) == 8. |
| 1666 | But the callers of these function has made sure the data for |
| 1667 | format == 32 is an array of int. Thus, use int instead |
| 1668 | of Atom. */ |
| 1669 | int *idata = (int *) data; |
| 1670 | |
| 1671 | if (size == sizeof (int)) |
| 1672 | return x_atom_to_symbol (display, (Atom) idata[0]); |
| 1673 | else |
| 1674 | { |
| 1675 | Lisp_Object v = Fmake_vector (make_number (size / sizeof (int)), |
| 1676 | make_number (0)); |
| 1677 | for (i = 0; i < size / sizeof (int); i++) |
| 1678 | Faset (v, make_number (i), |
| 1679 | x_atom_to_symbol (display, (Atom) idata[i])); |
| 1680 | return v; |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | /* Convert a single 16-bit number or a small 32-bit number to a Lisp_Int. |
| 1685 | If the number is 32 bits and won't fit in a Lisp_Int, |
| 1686 | convert it to a cons of integers, 16 bits in each half. |
| 1687 | */ |
| 1688 | else if (format == 32 && size == sizeof (int)) |
| 1689 | return INTEGER_TO_CONS (((int *) data) [0]); |
| 1690 | else if (format == 16 && size == sizeof (short)) |
| 1691 | return make_number (((short *) data) [0]); |
| 1692 | |
| 1693 | /* Convert any other kind of data to a vector of numbers, represented |
| 1694 | as above (as an integer, or a cons of two 16 bit integers.) |
| 1695 | */ |
| 1696 | else if (format == 16) |
| 1697 | { |
| 1698 | ptrdiff_t i; |
| 1699 | Lisp_Object v; |
| 1700 | v = Fmake_vector (make_number (size / 2), make_number (0)); |
| 1701 | for (i = 0; i < size / 2; i++) |
| 1702 | { |
| 1703 | short j = ((short *) data) [i]; |
| 1704 | Faset (v, make_number (i), make_number (j)); |
| 1705 | } |
| 1706 | return v; |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | ptrdiff_t i; |
| 1711 | Lisp_Object v = Fmake_vector (make_number (size / X_LONG_SIZE), |
| 1712 | make_number (0)); |
| 1713 | for (i = 0; i < size / X_LONG_SIZE; i++) |
| 1714 | { |
| 1715 | int j = ((int *) data) [i]; |
| 1716 | Faset (v, make_number (i), INTEGER_TO_CONS (j)); |
| 1717 | } |
| 1718 | return v; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /* Convert OBJ to an X long value, and return it as unsigned long. |
| 1723 | OBJ should be an integer or a cons representing an integer. |
| 1724 | Treat values in the range X_LONG_MAX + 1 .. X_ULONG_MAX as X |
| 1725 | unsigned long values: in theory these values are supposed to be |
| 1726 | signed but in practice unsigned 32-bit data are communicated via X |
| 1727 | selections and we need to support that. */ |
| 1728 | static unsigned long |
| 1729 | cons_to_x_long (Lisp_Object obj) |
| 1730 | { |
| 1731 | if (X_ULONG_MAX <= INTMAX_MAX |
| 1732 | || XINT (INTEGERP (obj) ? obj : XCAR (obj)) < 0) |
| 1733 | return cons_to_signed (obj, X_LONG_MIN, min (X_ULONG_MAX, INTMAX_MAX)); |
| 1734 | else |
| 1735 | return cons_to_unsigned (obj, X_ULONG_MAX); |
| 1736 | } |
| 1737 | |
| 1738 | /* Use xfree, not XFree, to free the data obtained with this function. */ |
| 1739 | |
| 1740 | static void |
| 1741 | lisp_data_to_selection_data (Display *display, Lisp_Object obj, |
| 1742 | unsigned char **data_ret, Atom *type_ret, |
| 1743 | ptrdiff_t *size_ret, |
| 1744 | int *format_ret, int *nofree_ret) |
| 1745 | { |
| 1746 | Lisp_Object type = Qnil; |
| 1747 | struct x_display_info *dpyinfo = x_display_info_for_display (display); |
| 1748 | |
| 1749 | *nofree_ret = 0; |
| 1750 | |
| 1751 | if (CONSP (obj) && SYMBOLP (XCAR (obj))) |
| 1752 | { |
| 1753 | type = XCAR (obj); |
| 1754 | obj = XCDR (obj); |
| 1755 | if (CONSP (obj) && NILP (XCDR (obj))) |
| 1756 | obj = XCAR (obj); |
| 1757 | } |
| 1758 | |
| 1759 | if (EQ (obj, QNULL) || (EQ (type, QNULL))) |
| 1760 | { /* This is not the same as declining */ |
| 1761 | *format_ret = 32; |
| 1762 | *size_ret = 0; |
| 1763 | *data_ret = 0; |
| 1764 | type = QNULL; |
| 1765 | } |
| 1766 | else if (STRINGP (obj)) |
| 1767 | { |
| 1768 | if (SCHARS (obj) < SBYTES (obj)) |
| 1769 | /* OBJ is a multibyte string containing a non-ASCII char. */ |
| 1770 | signal_error ("Non-ASCII string must be encoded in advance", obj); |
| 1771 | if (NILP (type)) |
| 1772 | type = QSTRING; |
| 1773 | *format_ret = 8; |
| 1774 | *size_ret = SBYTES (obj); |
| 1775 | *data_ret = SDATA (obj); |
| 1776 | *nofree_ret = 1; |
| 1777 | } |
| 1778 | else if (SYMBOLP (obj)) |
| 1779 | { |
| 1780 | *data_ret = (unsigned char *) xmalloc (sizeof (Atom) + 1); |
| 1781 | *format_ret = 32; |
| 1782 | *size_ret = 1; |
| 1783 | (*data_ret) [sizeof (Atom)] = 0; |
| 1784 | (*(Atom **) data_ret) [0] = symbol_to_x_atom (dpyinfo, obj); |
| 1785 | if (NILP (type)) type = QATOM; |
| 1786 | } |
| 1787 | else if (RANGED_INTEGERP (X_SHRT_MIN, obj, X_SHRT_MAX)) |
| 1788 | { |
| 1789 | *data_ret = (unsigned char *) xmalloc (sizeof (short) + 1); |
| 1790 | *format_ret = 16; |
| 1791 | *size_ret = 1; |
| 1792 | (*data_ret) [sizeof (short)] = 0; |
| 1793 | (*(short **) data_ret) [0] = XINT (obj); |
| 1794 | if (NILP (type)) type = QINTEGER; |
| 1795 | } |
| 1796 | else if (INTEGERP (obj) |
| 1797 | || (CONSP (obj) && INTEGERP (XCAR (obj)) |
| 1798 | && (INTEGERP (XCDR (obj)) |
| 1799 | || (CONSP (XCDR (obj)) |
| 1800 | && INTEGERP (XCAR (XCDR (obj))))))) |
| 1801 | { |
| 1802 | *data_ret = (unsigned char *) xmalloc (sizeof (unsigned long) + 1); |
| 1803 | *format_ret = 32; |
| 1804 | *size_ret = 1; |
| 1805 | (*data_ret) [sizeof (unsigned long)] = 0; |
| 1806 | (*(unsigned long **) data_ret) [0] = cons_to_x_long (obj); |
| 1807 | if (NILP (type)) type = QINTEGER; |
| 1808 | } |
| 1809 | else if (VECTORP (obj)) |
| 1810 | { |
| 1811 | /* Lisp_Vectors may represent a set of ATOMs; |
| 1812 | a set of 16 or 32 bit INTEGERs; |
| 1813 | or a set of ATOM_PAIRs (represented as [[A1 A2] [A3 A4] ...] |
| 1814 | */ |
| 1815 | ptrdiff_t i; |
| 1816 | ptrdiff_t size = ASIZE (obj); |
| 1817 | |
| 1818 | if (SYMBOLP (XVECTOR (obj)->contents [0])) |
| 1819 | /* This vector is an ATOM set */ |
| 1820 | { |
| 1821 | if (NILP (type)) type = QATOM; |
| 1822 | for (i = 0; i < size; i++) |
| 1823 | if (!SYMBOLP (XVECTOR (obj)->contents [i])) |
| 1824 | signal_error ("All elements of selection vector must have same type", obj); |
| 1825 | |
| 1826 | *data_ret = xnmalloc (size, sizeof (Atom)); |
| 1827 | *format_ret = 32; |
| 1828 | *size_ret = size; |
| 1829 | for (i = 0; i < size; i++) |
| 1830 | (*(Atom **) data_ret) [i] |
| 1831 | = symbol_to_x_atom (dpyinfo, XVECTOR (obj)->contents [i]); |
| 1832 | } |
| 1833 | else |
| 1834 | /* This vector is an INTEGER set, or something like it */ |
| 1835 | { |
| 1836 | int format = 16; |
| 1837 | int data_size = sizeof (short); |
| 1838 | if (NILP (type)) type = QINTEGER; |
| 1839 | for (i = 0; i < size; i++) |
| 1840 | { |
| 1841 | if (! RANGED_INTEGERP (X_SHRT_MIN, XVECTOR (obj)->contents[i], |
| 1842 | X_SHRT_MAX)) |
| 1843 | { |
| 1844 | /* Use sizeof (long) even if it is more than 32 bits. |
| 1845 | See comment in x_get_window_property and |
| 1846 | x_fill_property_data. */ |
| 1847 | data_size = sizeof (long); |
| 1848 | format = 32; |
| 1849 | break; |
| 1850 | } |
| 1851 | } |
| 1852 | *data_ret = xnmalloc (size, data_size); |
| 1853 | *format_ret = format; |
| 1854 | *size_ret = size; |
| 1855 | for (i = 0; i < size; i++) |
| 1856 | { |
| 1857 | if (format == 32) |
| 1858 | (*((unsigned long **) data_ret)) [i] = |
| 1859 | cons_to_x_long (XVECTOR (obj)->contents[i]); |
| 1860 | else |
| 1861 | (*((short **) data_ret)) [i] = |
| 1862 | XINT (XVECTOR (obj)->contents[i]); |
| 1863 | } |
| 1864 | } |
| 1865 | } |
| 1866 | else |
| 1867 | signal_error (/* Qselection_error */ "Unrecognized selection data", obj); |
| 1868 | |
| 1869 | *type_ret = symbol_to_x_atom (dpyinfo, type); |
| 1870 | } |
| 1871 | |
| 1872 | static Lisp_Object |
| 1873 | clean_local_selection_data (Lisp_Object obj) |
| 1874 | { |
| 1875 | if (CONSP (obj) |
| 1876 | && INTEGERP (XCAR (obj)) |
| 1877 | && CONSP (XCDR (obj)) |
| 1878 | && INTEGERP (XCAR (XCDR (obj))) |
| 1879 | && NILP (XCDR (XCDR (obj)))) |
| 1880 | obj = Fcons (XCAR (obj), XCDR (obj)); |
| 1881 | |
| 1882 | if (CONSP (obj) |
| 1883 | && INTEGERP (XCAR (obj)) |
| 1884 | && INTEGERP (XCDR (obj))) |
| 1885 | { |
| 1886 | if (XINT (XCAR (obj)) == 0) |
| 1887 | return XCDR (obj); |
| 1888 | if (XINT (XCAR (obj)) == -1) |
| 1889 | return make_number (- XINT (XCDR (obj))); |
| 1890 | } |
| 1891 | if (VECTORP (obj)) |
| 1892 | { |
| 1893 | ptrdiff_t i; |
| 1894 | ptrdiff_t size = ASIZE (obj); |
| 1895 | Lisp_Object copy; |
| 1896 | if (size == 1) |
| 1897 | return clean_local_selection_data (XVECTOR (obj)->contents [0]); |
| 1898 | copy = Fmake_vector (make_number (size), Qnil); |
| 1899 | for (i = 0; i < size; i++) |
| 1900 | XVECTOR (copy)->contents [i] |
| 1901 | = clean_local_selection_data (XVECTOR (obj)->contents [i]); |
| 1902 | return copy; |
| 1903 | } |
| 1904 | return obj; |
| 1905 | } |
| 1906 | \f |
| 1907 | /* Called from XTread_socket to handle SelectionNotify events. |
| 1908 | If it's the selection we are waiting for, stop waiting |
| 1909 | by setting the car of reading_selection_reply to non-nil. |
| 1910 | We store t there if the reply is successful, lambda if not. */ |
| 1911 | |
| 1912 | void |
| 1913 | x_handle_selection_notify (XSelectionEvent *event) |
| 1914 | { |
| 1915 | if (event->requestor != reading_selection_window) |
| 1916 | return; |
| 1917 | if (event->selection != reading_which_selection) |
| 1918 | return; |
| 1919 | |
| 1920 | TRACE0 ("Received SelectionNotify"); |
| 1921 | XSETCAR (reading_selection_reply, |
| 1922 | (event->property != 0 ? Qt : Qlambda)); |
| 1923 | } |
| 1924 | |
| 1925 | \f |
| 1926 | /* From a Lisp_Object, return a suitable frame for selection |
| 1927 | operations. OBJECT may be a frame, a terminal object, or nil |
| 1928 | (which stands for the selected frame--or, if that is not an X |
| 1929 | frame, the first X display on the list). If no suitable frame can |
| 1930 | be found, return NULL. */ |
| 1931 | |
| 1932 | static struct frame * |
| 1933 | frame_for_x_selection (Lisp_Object object) |
| 1934 | { |
| 1935 | Lisp_Object tail; |
| 1936 | struct frame *f; |
| 1937 | |
| 1938 | if (NILP (object)) |
| 1939 | { |
| 1940 | f = XFRAME (selected_frame); |
| 1941 | if (FRAME_X_P (f) && FRAME_LIVE_P (f)) |
| 1942 | return f; |
| 1943 | |
| 1944 | for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail)) |
| 1945 | { |
| 1946 | f = XFRAME (XCAR (tail)); |
| 1947 | if (FRAME_X_P (f) && FRAME_LIVE_P (f)) |
| 1948 | return f; |
| 1949 | } |
| 1950 | } |
| 1951 | else if (TERMINALP (object)) |
| 1952 | { |
| 1953 | struct terminal *t = get_terminal (object, 1); |
| 1954 | if (t->type == output_x_window) |
| 1955 | { |
| 1956 | for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail)) |
| 1957 | { |
| 1958 | f = XFRAME (XCAR (tail)); |
| 1959 | if (FRAME_LIVE_P (f) && f->terminal == t) |
| 1960 | return f; |
| 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | else if (FRAMEP (object)) |
| 1965 | { |
| 1966 | f = XFRAME (object); |
| 1967 | if (FRAME_X_P (f) && FRAME_LIVE_P (f)) |
| 1968 | return f; |
| 1969 | } |
| 1970 | |
| 1971 | return NULL; |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | DEFUN ("x-own-selection-internal", Fx_own_selection_internal, |
| 1976 | Sx_own_selection_internal, 2, 3, 0, |
| 1977 | doc: /* Assert an X selection of type SELECTION and value VALUE. |
| 1978 | SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. |
| 1979 | \(Those are literal upper-case symbol names, since that's what X expects.) |
| 1980 | VALUE is typically a string, or a cons of two markers, but may be |
| 1981 | anything that the functions on `selection-converter-alist' know about. |
| 1982 | |
| 1983 | FRAME should be a frame that should own the selection. If omitted or |
| 1984 | nil, it defaults to the selected frame. */) |
| 1985 | (Lisp_Object selection, Lisp_Object value, Lisp_Object frame) |
| 1986 | { |
| 1987 | if (NILP (frame)) frame = selected_frame; |
| 1988 | if (!FRAME_LIVE_P (XFRAME (frame)) || !FRAME_X_P (XFRAME (frame))) |
| 1989 | error ("X selection unavailable for this frame"); |
| 1990 | |
| 1991 | CHECK_SYMBOL (selection); |
| 1992 | if (NILP (value)) error ("VALUE may not be nil"); |
| 1993 | x_own_selection (selection, value, frame); |
| 1994 | return value; |
| 1995 | } |
| 1996 | |
| 1997 | |
| 1998 | /* Request the selection value from the owner. If we are the owner, |
| 1999 | simply return our selection value. If we are not the owner, this |
| 2000 | will block until all of the data has arrived. */ |
| 2001 | |
| 2002 | DEFUN ("x-get-selection-internal", Fx_get_selection_internal, |
| 2003 | Sx_get_selection_internal, 2, 4, 0, |
| 2004 | doc: /* Return text selected from some X window. |
| 2005 | SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'. |
| 2006 | \(Those are literal upper-case symbol names, since that's what X expects.) |
| 2007 | TYPE is the type of data desired, typically `STRING'. |
| 2008 | TIME_STAMP is the time to use in the XConvertSelection call for foreign |
| 2009 | selections. If omitted, defaults to the time for the last event. |
| 2010 | |
| 2011 | TERMINAL should be a terminal object or a frame specifying the X |
| 2012 | server to query. If omitted or nil, that stands for the selected |
| 2013 | frame's display, or the first available X display. */) |
| 2014 | (Lisp_Object selection_symbol, Lisp_Object target_type, |
| 2015 | Lisp_Object time_stamp, Lisp_Object terminal) |
| 2016 | { |
| 2017 | Lisp_Object val = Qnil; |
| 2018 | struct gcpro gcpro1, gcpro2; |
| 2019 | struct frame *f = frame_for_x_selection (terminal); |
| 2020 | GCPRO2 (target_type, val); /* we store newly consed data into these */ |
| 2021 | |
| 2022 | CHECK_SYMBOL (selection_symbol); |
| 2023 | CHECK_SYMBOL (target_type); |
| 2024 | if (EQ (target_type, QMULTIPLE)) |
| 2025 | error ("Retrieving MULTIPLE selections is currently unimplemented"); |
| 2026 | if (!f) |
| 2027 | error ("X selection unavailable for this frame"); |
| 2028 | |
| 2029 | val = x_get_local_selection (selection_symbol, target_type, 1, |
| 2030 | FRAME_X_DISPLAY_INFO (f)); |
| 2031 | |
| 2032 | if (NILP (val) && FRAME_LIVE_P (f)) |
| 2033 | { |
| 2034 | Lisp_Object frame; |
| 2035 | XSETFRAME (frame, f); |
| 2036 | RETURN_UNGCPRO (x_get_foreign_selection (selection_symbol, target_type, |
| 2037 | time_stamp, frame)); |
| 2038 | } |
| 2039 | |
| 2040 | if (CONSP (val) && SYMBOLP (XCAR (val))) |
| 2041 | { |
| 2042 | val = XCDR (val); |
| 2043 | if (CONSP (val) && NILP (XCDR (val))) |
| 2044 | val = XCAR (val); |
| 2045 | } |
| 2046 | RETURN_UNGCPRO (clean_local_selection_data (val)); |
| 2047 | } |
| 2048 | |
| 2049 | DEFUN ("x-disown-selection-internal", Fx_disown_selection_internal, |
| 2050 | Sx_disown_selection_internal, 1, 3, 0, |
| 2051 | doc: /* If we own the selection SELECTION, disown it. |
| 2052 | Disowning it means there is no such selection. |
| 2053 | |
| 2054 | TERMINAL should be a terminal object or a frame specifying the X |
| 2055 | server to query. If omitted or nil, that stands for the selected |
| 2056 | frame's display, or the first available X display. */) |
| 2057 | (Lisp_Object selection, Lisp_Object time_object, Lisp_Object terminal) |
| 2058 | { |
| 2059 | Time timestamp; |
| 2060 | Atom selection_atom; |
| 2061 | union { |
| 2062 | struct selection_input_event sie; |
| 2063 | struct input_event ie; |
| 2064 | } event; |
| 2065 | struct frame *f = frame_for_x_selection (terminal); |
| 2066 | struct x_display_info *dpyinfo; |
| 2067 | |
| 2068 | if (!f) |
| 2069 | return Qnil; |
| 2070 | |
| 2071 | dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 2072 | CHECK_SYMBOL (selection); |
| 2073 | |
| 2074 | /* Don't disown the selection when we're not the owner. */ |
| 2075 | if (NILP (LOCAL_SELECTION (selection, dpyinfo))) |
| 2076 | return Qnil; |
| 2077 | |
| 2078 | selection_atom = symbol_to_x_atom (dpyinfo, selection); |
| 2079 | |
| 2080 | BLOCK_INPUT; |
| 2081 | if (NILP (time_object)) |
| 2082 | timestamp = last_event_timestamp; |
| 2083 | else |
| 2084 | CONS_TO_INTEGER (time_object, Time, timestamp); |
| 2085 | XSetSelectionOwner (dpyinfo->display, selection_atom, None, timestamp); |
| 2086 | UNBLOCK_INPUT; |
| 2087 | |
| 2088 | /* It doesn't seem to be guaranteed that a SelectionClear event will be |
| 2089 | generated for a window which owns the selection when that window sets |
| 2090 | the selection owner to None. The NCD server does, the MIT Sun4 server |
| 2091 | doesn't. So we synthesize one; this means we might get two, but |
| 2092 | that's ok, because the second one won't have any effect. */ |
| 2093 | SELECTION_EVENT_DISPLAY (&event.sie) = dpyinfo->display; |
| 2094 | SELECTION_EVENT_SELECTION (&event.sie) = selection_atom; |
| 2095 | SELECTION_EVENT_TIME (&event.sie) = timestamp; |
| 2096 | x_handle_selection_clear (&event.ie); |
| 2097 | |
| 2098 | return Qt; |
| 2099 | } |
| 2100 | |
| 2101 | DEFUN ("x-selection-owner-p", Fx_selection_owner_p, Sx_selection_owner_p, |
| 2102 | 0, 2, 0, |
| 2103 | doc: /* Whether the current Emacs process owns the given X Selection. |
| 2104 | The arg should be the name of the selection in question, typically one of |
| 2105 | the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. |
| 2106 | \(Those are literal upper-case symbol names, since that's what X expects.) |
| 2107 | For convenience, the symbol nil is the same as `PRIMARY', |
| 2108 | and t is the same as `SECONDARY'. |
| 2109 | |
| 2110 | TERMINAL should be a terminal object or a frame specifying the X |
| 2111 | server to query. If omitted or nil, that stands for the selected |
| 2112 | frame's display, or the first available X display. */) |
| 2113 | (Lisp_Object selection, Lisp_Object terminal) |
| 2114 | { |
| 2115 | struct frame *f = frame_for_x_selection (terminal); |
| 2116 | |
| 2117 | CHECK_SYMBOL (selection); |
| 2118 | if (EQ (selection, Qnil)) selection = QPRIMARY; |
| 2119 | if (EQ (selection, Qt)) selection = QSECONDARY; |
| 2120 | |
| 2121 | if (f && !NILP (LOCAL_SELECTION (selection, FRAME_X_DISPLAY_INFO (f)))) |
| 2122 | return Qt; |
| 2123 | else |
| 2124 | return Qnil; |
| 2125 | } |
| 2126 | |
| 2127 | DEFUN ("x-selection-exists-p", Fx_selection_exists_p, Sx_selection_exists_p, |
| 2128 | 0, 2, 0, |
| 2129 | doc: /* Whether there is an owner for the given X selection. |
| 2130 | SELECTION should be the name of the selection in question, typically |
| 2131 | one of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. (X expects |
| 2132 | these literal upper-case names.) The symbol nil is the same as |
| 2133 | `PRIMARY', and t is the same as `SECONDARY'. |
| 2134 | |
| 2135 | TERMINAL should be a terminal object or a frame specifying the X |
| 2136 | server to query. If omitted or nil, that stands for the selected |
| 2137 | frame's display, or the first available X display. */) |
| 2138 | (Lisp_Object selection, Lisp_Object terminal) |
| 2139 | { |
| 2140 | Window owner; |
| 2141 | Atom atom; |
| 2142 | struct frame *f = frame_for_x_selection (terminal); |
| 2143 | struct x_display_info *dpyinfo; |
| 2144 | |
| 2145 | CHECK_SYMBOL (selection); |
| 2146 | if (EQ (selection, Qnil)) selection = QPRIMARY; |
| 2147 | if (EQ (selection, Qt)) selection = QSECONDARY; |
| 2148 | |
| 2149 | if (!f) |
| 2150 | return Qnil; |
| 2151 | |
| 2152 | dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 2153 | |
| 2154 | if (!NILP (LOCAL_SELECTION (selection, dpyinfo))) |
| 2155 | return Qt; |
| 2156 | |
| 2157 | atom = symbol_to_x_atom (dpyinfo, selection); |
| 2158 | if (atom == 0) return Qnil; |
| 2159 | BLOCK_INPUT; |
| 2160 | owner = XGetSelectionOwner (dpyinfo->display, atom); |
| 2161 | UNBLOCK_INPUT; |
| 2162 | return (owner ? Qt : Qnil); |
| 2163 | } |
| 2164 | |
| 2165 | \f |
| 2166 | /* Send clipboard manager a SAVE_TARGETS request with a UTF8_STRING |
| 2167 | property (http://www.freedesktop.org/wiki/ClipboardManager). */ |
| 2168 | |
| 2169 | static Lisp_Object |
| 2170 | x_clipboard_manager_save (Lisp_Object frame) |
| 2171 | { |
| 2172 | struct frame *f = XFRAME (frame); |
| 2173 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 2174 | Atom data = dpyinfo->Xatom_UTF8_STRING; |
| 2175 | |
| 2176 | XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), |
| 2177 | dpyinfo->Xatom_EMACS_TMP, |
| 2178 | dpyinfo->Xatom_ATOM, 32, PropModeReplace, |
| 2179 | (unsigned char *) &data, 1); |
| 2180 | x_get_foreign_selection (QCLIPBOARD_MANAGER, QSAVE_TARGETS, |
| 2181 | Qnil, frame); |
| 2182 | return Qt; |
| 2183 | } |
| 2184 | |
| 2185 | /* Error handler for x_clipboard_manager_save_frame. */ |
| 2186 | |
| 2187 | static Lisp_Object |
| 2188 | x_clipboard_manager_error_1 (Lisp_Object err) |
| 2189 | { |
| 2190 | Lisp_Object args[2]; |
| 2191 | args[0] = build_string ("X clipboard manager error: %s\n\ |
| 2192 | If the problem persists, set `x-select-enable-clipboard-manager' to nil."); |
| 2193 | args[1] = CAR (CDR (err)); |
| 2194 | Fmessage (2, args); |
| 2195 | return Qnil; |
| 2196 | } |
| 2197 | |
| 2198 | /* Error handler for x_clipboard_manager_save_all. */ |
| 2199 | |
| 2200 | static Lisp_Object |
| 2201 | x_clipboard_manager_error_2 (Lisp_Object err) |
| 2202 | { |
| 2203 | fprintf (stderr, "Error saving to X clipboard manager.\n\ |
| 2204 | If the problem persists, set `x-select-enable-clipboard-manager' \ |
| 2205 | to nil.\n"); |
| 2206 | return Qnil; |
| 2207 | } |
| 2208 | |
| 2209 | /* Called from delete_frame: save any clipboard owned by FRAME to the |
| 2210 | clipboard manager. Do nothing if FRAME does not own the clipboard, |
| 2211 | or if no clipboard manager is present. */ |
| 2212 | |
| 2213 | void |
| 2214 | x_clipboard_manager_save_frame (Lisp_Object frame) |
| 2215 | { |
| 2216 | struct frame *f; |
| 2217 | |
| 2218 | if (!NILP (Vx_select_enable_clipboard_manager) |
| 2219 | && FRAMEP (frame) |
| 2220 | && (f = XFRAME (frame), FRAME_X_P (f)) |
| 2221 | && FRAME_LIVE_P (f)) |
| 2222 | { |
| 2223 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 2224 | Lisp_Object local_selection |
| 2225 | = LOCAL_SELECTION (QCLIPBOARD, dpyinfo); |
| 2226 | |
| 2227 | if (!NILP (local_selection) |
| 2228 | && EQ (frame, XCAR (XCDR (XCDR (XCDR (local_selection))))) |
| 2229 | && XGetSelectionOwner (dpyinfo->display, |
| 2230 | dpyinfo->Xatom_CLIPBOARD_MANAGER)) |
| 2231 | internal_condition_case_1 (x_clipboard_manager_save, frame, Qt, |
| 2232 | x_clipboard_manager_error_1); |
| 2233 | } |
| 2234 | } |
| 2235 | |
| 2236 | /* Called from Fkill_emacs: save any clipboard owned by FRAME to the |
| 2237 | clipboard manager. Do nothing if FRAME does not own the clipboard, |
| 2238 | or if no clipboard manager is present. */ |
| 2239 | |
| 2240 | void |
| 2241 | x_clipboard_manager_save_all (void) |
| 2242 | { |
| 2243 | /* Loop through all X displays, saving owned clipboards. */ |
| 2244 | struct x_display_info *dpyinfo; |
| 2245 | Lisp_Object local_selection, local_frame; |
| 2246 | |
| 2247 | if (NILP (Vx_select_enable_clipboard_manager)) |
| 2248 | return; |
| 2249 | |
| 2250 | for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next) |
| 2251 | { |
| 2252 | local_selection = LOCAL_SELECTION (QCLIPBOARD, dpyinfo); |
| 2253 | if (NILP (local_selection) |
| 2254 | || !XGetSelectionOwner (dpyinfo->display, |
| 2255 | dpyinfo->Xatom_CLIPBOARD_MANAGER)) |
| 2256 | continue; |
| 2257 | |
| 2258 | local_frame = XCAR (XCDR (XCDR (XCDR (local_selection)))); |
| 2259 | if (FRAME_LIVE_P (XFRAME (local_frame))) |
| 2260 | internal_condition_case_1 (x_clipboard_manager_save, local_frame, |
| 2261 | Qt, x_clipboard_manager_error_2); |
| 2262 | } |
| 2263 | } |
| 2264 | |
| 2265 | \f |
| 2266 | /*********************************************************************** |
| 2267 | Drag and drop support |
| 2268 | ***********************************************************************/ |
| 2269 | /* Check that lisp values are of correct type for x_fill_property_data. |
| 2270 | That is, number, string or a cons with two numbers (low and high 16 |
| 2271 | bit parts of a 32 bit number). Return the number of items in DATA, |
| 2272 | or -1 if there is an error. */ |
| 2273 | |
| 2274 | int |
| 2275 | x_check_property_data (Lisp_Object data) |
| 2276 | { |
| 2277 | Lisp_Object iter; |
| 2278 | int size = 0; |
| 2279 | |
| 2280 | for (iter = data; CONSP (iter); iter = XCDR (iter)) |
| 2281 | { |
| 2282 | Lisp_Object o = XCAR (iter); |
| 2283 | |
| 2284 | if (! NUMBERP (o) && ! STRINGP (o) && ! CONSP (o)) |
| 2285 | return -1; |
| 2286 | else if (CONSP (o) && |
| 2287 | (! NUMBERP (XCAR (o)) || ! NUMBERP (XCDR (o)))) |
| 2288 | return -1; |
| 2289 | if (size == INT_MAX) |
| 2290 | return -1; |
| 2291 | size++; |
| 2292 | } |
| 2293 | |
| 2294 | return size; |
| 2295 | } |
| 2296 | |
| 2297 | /* Convert lisp values to a C array. Values may be a number, a string |
| 2298 | which is taken as an X atom name and converted to the atom value, or |
| 2299 | a cons containing the two 16 bit parts of a 32 bit number. |
| 2300 | |
| 2301 | DPY is the display use to look up X atoms. |
| 2302 | DATA is a Lisp list of values to be converted. |
| 2303 | RET is the C array that contains the converted values. It is assumed |
| 2304 | it is big enough to hold all values. |
| 2305 | FORMAT is 8, 16 or 32 and denotes char/short/long for each C value to |
| 2306 | be stored in RET. Note that long is used for 32 even if long is more |
| 2307 | than 32 bits (see man pages for XChangeProperty, XGetWindowProperty and |
| 2308 | XClientMessageEvent). */ |
| 2309 | |
| 2310 | void |
| 2311 | x_fill_property_data (Display *dpy, Lisp_Object data, void *ret, int format) |
| 2312 | { |
| 2313 | long val; |
| 2314 | long *d32 = (long *) ret; |
| 2315 | short *d16 = (short *) ret; |
| 2316 | char *d08 = (char *) ret; |
| 2317 | Lisp_Object iter; |
| 2318 | |
| 2319 | for (iter = data; CONSP (iter); iter = XCDR (iter)) |
| 2320 | { |
| 2321 | Lisp_Object o = XCAR (iter); |
| 2322 | |
| 2323 | if (INTEGERP (o) || FLOATP (o) || CONSP (o)) |
| 2324 | val = cons_to_signed (o, LONG_MIN, LONG_MAX); |
| 2325 | else if (STRINGP (o)) |
| 2326 | { |
| 2327 | BLOCK_INPUT; |
| 2328 | val = (long) XInternAtom (dpy, SSDATA (o), False); |
| 2329 | UNBLOCK_INPUT; |
| 2330 | } |
| 2331 | else |
| 2332 | error ("Wrong type, must be string, number or cons"); |
| 2333 | |
| 2334 | if (format == 8) |
| 2335 | { |
| 2336 | if (CHAR_MIN <= val && val <= CHAR_MAX) |
| 2337 | *d08++ = val; |
| 2338 | else |
| 2339 | error ("Out of 'char' range"); |
| 2340 | } |
| 2341 | else if (format == 16) |
| 2342 | { |
| 2343 | if (SHRT_MIN <= val && val <= SHRT_MAX) |
| 2344 | *d16++ = val; |
| 2345 | else |
| 2346 | error ("Out of 'short' range"); |
| 2347 | } |
| 2348 | else |
| 2349 | *d32++ = val; |
| 2350 | } |
| 2351 | } |
| 2352 | |
| 2353 | /* Convert an array of C values to a Lisp list. |
| 2354 | F is the frame to be used to look up X atoms if the TYPE is XA_ATOM. |
| 2355 | DATA is a C array of values to be converted. |
| 2356 | TYPE is the type of the data. Only XA_ATOM is special, it converts |
| 2357 | each number in DATA to its corresponfing X atom as a symbol. |
| 2358 | FORMAT is 8, 16 or 32 and gives the size in bits for each C value to |
| 2359 | be stored in RET. |
| 2360 | SIZE is the number of elements in DATA. |
| 2361 | |
| 2362 | Important: When format is 32, data should contain an array of int, |
| 2363 | not an array of long as the X library returns. This makes a difference |
| 2364 | when sizeof(long) != sizeof(int). |
| 2365 | |
| 2366 | Also see comment for selection_data_to_lisp_data above. */ |
| 2367 | |
| 2368 | Lisp_Object |
| 2369 | x_property_data_to_lisp (struct frame *f, const unsigned char *data, |
| 2370 | Atom type, int format, long unsigned int size) |
| 2371 | { |
| 2372 | ptrdiff_t format_bytes = format >> 3; |
| 2373 | if (PTRDIFF_MAX / format_bytes < size) |
| 2374 | memory_full (SIZE_MAX); |
| 2375 | return selection_data_to_lisp_data (FRAME_X_DISPLAY (f), |
| 2376 | data, size * format_bytes, type, format); |
| 2377 | } |
| 2378 | |
| 2379 | /* Get the mouse position in frame relative coordinates. */ |
| 2380 | |
| 2381 | static void |
| 2382 | mouse_position_for_drop (FRAME_PTR f, int *x, int *y) |
| 2383 | { |
| 2384 | Window root, dummy_window; |
| 2385 | int dummy; |
| 2386 | |
| 2387 | BLOCK_INPUT; |
| 2388 | |
| 2389 | XQueryPointer (FRAME_X_DISPLAY (f), |
| 2390 | DefaultRootWindow (FRAME_X_DISPLAY (f)), |
| 2391 | |
| 2392 | /* The root window which contains the pointer. */ |
| 2393 | &root, |
| 2394 | |
| 2395 | /* Window pointer is on, not used */ |
| 2396 | &dummy_window, |
| 2397 | |
| 2398 | /* The position on that root window. */ |
| 2399 | x, y, |
| 2400 | |
| 2401 | /* x/y in dummy_window coordinates, not used. */ |
| 2402 | &dummy, &dummy, |
| 2403 | |
| 2404 | /* Modifier keys and pointer buttons, about which |
| 2405 | we don't care. */ |
| 2406 | (unsigned int *) &dummy); |
| 2407 | |
| 2408 | |
| 2409 | /* Absolute to relative. */ |
| 2410 | *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f); |
| 2411 | *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f); |
| 2412 | |
| 2413 | UNBLOCK_INPUT; |
| 2414 | } |
| 2415 | |
| 2416 | DEFUN ("x-get-atom-name", Fx_get_atom_name, |
| 2417 | Sx_get_atom_name, 1, 2, 0, |
| 2418 | doc: /* Return the X atom name for VALUE as a string. |
| 2419 | VALUE may be a number or a cons where the car is the upper 16 bits and |
| 2420 | the cdr is the lower 16 bits of a 32 bit value. |
| 2421 | Use the display for FRAME or the current frame if FRAME is not given or nil. |
| 2422 | |
| 2423 | If the value is 0 or the atom is not known, return the empty string. */) |
| 2424 | (Lisp_Object value, Lisp_Object frame) |
| 2425 | { |
| 2426 | struct frame *f = check_x_frame (frame); |
| 2427 | char *name = 0; |
| 2428 | char empty[] = ""; |
| 2429 | Lisp_Object ret = Qnil; |
| 2430 | Display *dpy = FRAME_X_DISPLAY (f); |
| 2431 | Atom atom; |
| 2432 | int had_errors; |
| 2433 | |
| 2434 | CONS_TO_INTEGER (value, Atom, atom); |
| 2435 | |
| 2436 | BLOCK_INPUT; |
| 2437 | x_catch_errors (dpy); |
| 2438 | name = atom ? XGetAtomName (dpy, atom) : empty; |
| 2439 | had_errors = x_had_errors_p (dpy); |
| 2440 | x_uncatch_errors (); |
| 2441 | |
| 2442 | if (!had_errors) |
| 2443 | ret = build_string (name); |
| 2444 | |
| 2445 | if (atom && name) XFree (name); |
| 2446 | if (NILP (ret)) ret = empty_unibyte_string; |
| 2447 | |
| 2448 | UNBLOCK_INPUT; |
| 2449 | |
| 2450 | return ret; |
| 2451 | } |
| 2452 | |
| 2453 | DEFUN ("x-register-dnd-atom", Fx_register_dnd_atom, |
| 2454 | Sx_register_dnd_atom, 1, 2, 0, |
| 2455 | doc: /* Request that dnd events are made for ClientMessages with ATOM. |
| 2456 | ATOM can be a symbol or a string. The ATOM is interned on the display that |
| 2457 | FRAME is on. If FRAME is nil, the selected frame is used. */) |
| 2458 | (Lisp_Object atom, Lisp_Object frame) |
| 2459 | { |
| 2460 | Atom x_atom; |
| 2461 | struct frame *f = check_x_frame (frame); |
| 2462 | ptrdiff_t i; |
| 2463 | struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f); |
| 2464 | |
| 2465 | |
| 2466 | if (SYMBOLP (atom)) |
| 2467 | x_atom = symbol_to_x_atom (dpyinfo, atom); |
| 2468 | else if (STRINGP (atom)) |
| 2469 | { |
| 2470 | BLOCK_INPUT; |
| 2471 | x_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (atom), False); |
| 2472 | UNBLOCK_INPUT; |
| 2473 | } |
| 2474 | else |
| 2475 | error ("ATOM must be a symbol or a string"); |
| 2476 | |
| 2477 | for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i) |
| 2478 | if (dpyinfo->x_dnd_atoms[i] == x_atom) |
| 2479 | return Qnil; |
| 2480 | |
| 2481 | if (dpyinfo->x_dnd_atoms_length == dpyinfo->x_dnd_atoms_size) |
| 2482 | dpyinfo->x_dnd_atoms = |
| 2483 | xpalloc (dpyinfo->x_dnd_atoms, &dpyinfo->x_dnd_atoms_size, |
| 2484 | 1, -1, sizeof *dpyinfo->x_dnd_atoms); |
| 2485 | |
| 2486 | dpyinfo->x_dnd_atoms[dpyinfo->x_dnd_atoms_length++] = x_atom; |
| 2487 | return Qnil; |
| 2488 | } |
| 2489 | |
| 2490 | /* Convert an XClientMessageEvent to a Lisp event of type DRAG_N_DROP_EVENT. */ |
| 2491 | |
| 2492 | int |
| 2493 | x_handle_dnd_message (struct frame *f, XClientMessageEvent *event, |
| 2494 | struct x_display_info *dpyinfo, struct input_event *bufp) |
| 2495 | { |
| 2496 | Lisp_Object vec; |
| 2497 | Lisp_Object frame; |
| 2498 | /* format 32 => size 5, format 16 => size 10, format 8 => size 20 */ |
| 2499 | unsigned long size = 160/event->format; |
| 2500 | int x, y; |
| 2501 | unsigned char *data = (unsigned char *) event->data.b; |
| 2502 | int idata[5]; |
| 2503 | ptrdiff_t i; |
| 2504 | |
| 2505 | for (i = 0; i < dpyinfo->x_dnd_atoms_length; ++i) |
| 2506 | if (dpyinfo->x_dnd_atoms[i] == event->message_type) break; |
| 2507 | |
| 2508 | if (i == dpyinfo->x_dnd_atoms_length) return 0; |
| 2509 | |
| 2510 | XSETFRAME (frame, f); |
| 2511 | |
| 2512 | /* On a 64 bit machine, the event->data.l array members are 64 bits (long), |
| 2513 | but the x_property_data_to_lisp (or rather selection_data_to_lisp_data) |
| 2514 | function expects them to be of size int (i.e. 32). So to be able to |
| 2515 | use that function, put the data in the form it expects if format is 32. */ |
| 2516 | |
| 2517 | if (32 < BITS_PER_LONG && event->format == 32) |
| 2518 | { |
| 2519 | for (i = 0; i < 5; ++i) /* There are only 5 longs in a ClientMessage. */ |
| 2520 | idata[i] = event->data.l[i]; |
| 2521 | data = (unsigned char *) idata; |
| 2522 | } |
| 2523 | |
| 2524 | vec = Fmake_vector (make_number (4), Qnil); |
| 2525 | ASET (vec, 0, SYMBOL_NAME (x_atom_to_symbol (FRAME_X_DISPLAY (f), |
| 2526 | event->message_type))); |
| 2527 | ASET (vec, 1, frame); |
| 2528 | ASET (vec, 2, make_number (event->format)); |
| 2529 | ASET (vec, 3, x_property_data_to_lisp (f, |
| 2530 | data, |
| 2531 | event->message_type, |
| 2532 | event->format, |
| 2533 | size)); |
| 2534 | |
| 2535 | mouse_position_for_drop (f, &x, &y); |
| 2536 | bufp->kind = DRAG_N_DROP_EVENT; |
| 2537 | bufp->frame_or_window = frame; |
| 2538 | bufp->timestamp = CurrentTime; |
| 2539 | bufp->x = make_number (x); |
| 2540 | bufp->y = make_number (y); |
| 2541 | bufp->arg = vec; |
| 2542 | bufp->modifiers = 0; |
| 2543 | |
| 2544 | return 1; |
| 2545 | } |
| 2546 | |
| 2547 | DEFUN ("x-send-client-message", Fx_send_client_event, |
| 2548 | Sx_send_client_message, 6, 6, 0, |
| 2549 | doc: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY. |
| 2550 | |
| 2551 | For DISPLAY, specify either a frame or a display name (a string). |
| 2552 | If DISPLAY is nil, that stands for the selected frame's display. |
| 2553 | DEST may be a number, in which case it is a Window id. The value 0 may |
| 2554 | be used to send to the root window of the DISPLAY. |
| 2555 | If DEST is a cons, it is converted to a 32 bit number |
| 2556 | with the high 16 bits from the car and the lower 16 bit from the cdr. That |
| 2557 | number is then used as a window id. |
| 2558 | If DEST is a frame the event is sent to the outer window of that frame. |
| 2559 | A value of nil means the currently selected frame. |
| 2560 | If DEST is the string "PointerWindow" the event is sent to the window that |
| 2561 | contains the pointer. If DEST is the string "InputFocus" the event is |
| 2562 | sent to the window that has the input focus. |
| 2563 | FROM is the frame sending the event. Use nil for currently selected frame. |
| 2564 | MESSAGE-TYPE is the name of an Atom as a string. |
| 2565 | FORMAT must be one of 8, 16 or 32 and determines the size of the values in |
| 2566 | bits. VALUES is a list of numbers, cons and/or strings containing the values |
| 2567 | to send. If a value is a string, it is converted to an Atom and the value of |
| 2568 | the Atom is sent. If a value is a cons, it is converted to a 32 bit number |
| 2569 | with the high 16 bits from the car and the lower 16 bit from the cdr. |
| 2570 | If more values than fits into the event is given, the excessive values |
| 2571 | are ignored. */) |
| 2572 | (Lisp_Object display, Lisp_Object dest, Lisp_Object from, |
| 2573 | Lisp_Object message_type, Lisp_Object format, Lisp_Object values) |
| 2574 | { |
| 2575 | struct x_display_info *dpyinfo = check_x_display_info (display); |
| 2576 | |
| 2577 | CHECK_STRING (message_type); |
| 2578 | x_send_client_event (display, dest, from, |
| 2579 | XInternAtom (dpyinfo->display, |
| 2580 | SSDATA (message_type), |
| 2581 | False), |
| 2582 | format, values); |
| 2583 | |
| 2584 | return Qnil; |
| 2585 | } |
| 2586 | |
| 2587 | void |
| 2588 | x_send_client_event (Lisp_Object display, Lisp_Object dest, Lisp_Object from, |
| 2589 | Atom message_type, Lisp_Object format, Lisp_Object values) |
| 2590 | { |
| 2591 | struct x_display_info *dpyinfo = check_x_display_info (display); |
| 2592 | Window wdest; |
| 2593 | XEvent event; |
| 2594 | struct frame *f = check_x_frame (from); |
| 2595 | int to_root; |
| 2596 | |
| 2597 | CHECK_NUMBER (format); |
| 2598 | CHECK_CONS (values); |
| 2599 | |
| 2600 | if (x_check_property_data (values) == -1) |
| 2601 | error ("Bad data in VALUES, must be number, cons or string"); |
| 2602 | |
| 2603 | if (XINT (format) != 8 && XINT (format) != 16 && XINT (format) != 32) |
| 2604 | error ("FORMAT must be one of 8, 16 or 32"); |
| 2605 | |
| 2606 | event.xclient.type = ClientMessage; |
| 2607 | event.xclient.format = XINT (format); |
| 2608 | |
| 2609 | if (FRAMEP (dest) || NILP (dest)) |
| 2610 | { |
| 2611 | struct frame *fdest = check_x_frame (dest); |
| 2612 | wdest = FRAME_OUTER_WINDOW (fdest); |
| 2613 | } |
| 2614 | else if (STRINGP (dest)) |
| 2615 | { |
| 2616 | if (strcmp (SSDATA (dest), "PointerWindow") == 0) |
| 2617 | wdest = PointerWindow; |
| 2618 | else if (strcmp (SSDATA (dest), "InputFocus") == 0) |
| 2619 | wdest = InputFocus; |
| 2620 | else |
| 2621 | error ("DEST as a string must be one of PointerWindow or InputFocus"); |
| 2622 | } |
| 2623 | else if (INTEGERP (dest) || FLOATP (dest) || CONSP (dest)) |
| 2624 | CONS_TO_INTEGER (dest, Window, wdest); |
| 2625 | else |
| 2626 | error ("DEST must be a frame, nil, string, number or cons"); |
| 2627 | |
| 2628 | if (wdest == 0) wdest = dpyinfo->root_window; |
| 2629 | to_root = wdest == dpyinfo->root_window; |
| 2630 | |
| 2631 | BLOCK_INPUT; |
| 2632 | |
| 2633 | event.xclient.message_type = message_type; |
| 2634 | event.xclient.display = dpyinfo->display; |
| 2635 | |
| 2636 | /* Some clients (metacity for example) expects sending window to be here |
| 2637 | when sending to the root window. */ |
| 2638 | event.xclient.window = to_root ? FRAME_OUTER_WINDOW (f) : wdest; |
| 2639 | |
| 2640 | |
| 2641 | memset (event.xclient.data.b, 0, sizeof (event.xclient.data.b)); |
| 2642 | x_fill_property_data (dpyinfo->display, values, event.xclient.data.b, |
| 2643 | event.xclient.format); |
| 2644 | |
| 2645 | /* If event mask is 0 the event is sent to the client that created |
| 2646 | the destination window. But if we are sending to the root window, |
| 2647 | there is no such client. Then we set the event mask to 0xffff. The |
| 2648 | event then goes to clients selecting for events on the root window. */ |
| 2649 | x_catch_errors (dpyinfo->display); |
| 2650 | { |
| 2651 | int propagate = to_root ? False : True; |
| 2652 | unsigned mask = to_root ? 0xffff : 0; |
| 2653 | XSendEvent (dpyinfo->display, wdest, propagate, mask, &event); |
| 2654 | XFlush (dpyinfo->display); |
| 2655 | } |
| 2656 | x_uncatch_errors (); |
| 2657 | UNBLOCK_INPUT; |
| 2658 | } |
| 2659 | |
| 2660 | \f |
| 2661 | void |
| 2662 | syms_of_xselect (void) |
| 2663 | { |
| 2664 | defsubr (&Sx_get_selection_internal); |
| 2665 | defsubr (&Sx_own_selection_internal); |
| 2666 | defsubr (&Sx_disown_selection_internal); |
| 2667 | defsubr (&Sx_selection_owner_p); |
| 2668 | defsubr (&Sx_selection_exists_p); |
| 2669 | |
| 2670 | defsubr (&Sx_get_atom_name); |
| 2671 | defsubr (&Sx_send_client_message); |
| 2672 | defsubr (&Sx_register_dnd_atom); |
| 2673 | |
| 2674 | reading_selection_reply = Fcons (Qnil, Qnil); |
| 2675 | staticpro (&reading_selection_reply); |
| 2676 | reading_selection_window = 0; |
| 2677 | reading_which_selection = 0; |
| 2678 | |
| 2679 | property_change_wait_list = 0; |
| 2680 | prop_location_identifier = 0; |
| 2681 | property_change_reply = Fcons (Qnil, Qnil); |
| 2682 | staticpro (&property_change_reply); |
| 2683 | |
| 2684 | converted_selections = NULL; |
| 2685 | conversion_fail_tag = None; |
| 2686 | |
| 2687 | DEFVAR_LISP ("selection-converter-alist", Vselection_converter_alist, |
| 2688 | doc: /* An alist associating X Windows selection-types with functions. |
| 2689 | These functions are called to convert the selection, with three args: |
| 2690 | the name of the selection (typically `PRIMARY', `SECONDARY', or `CLIPBOARD'); |
| 2691 | a desired type to which the selection should be converted; |
| 2692 | and the local selection value (whatever was given to `x-own-selection'). |
| 2693 | |
| 2694 | The function should return the value to send to the X server |
| 2695 | \(typically a string). A return value of nil |
| 2696 | means that the conversion could not be done. |
| 2697 | A return value which is the symbol `NULL' |
| 2698 | means that a side-effect was executed, |
| 2699 | and there is no meaningful selection value. */); |
| 2700 | Vselection_converter_alist = Qnil; |
| 2701 | |
| 2702 | DEFVAR_LISP ("x-lost-selection-functions", Vx_lost_selection_functions, |
| 2703 | doc: /* A list of functions to be called when Emacs loses an X selection. |
| 2704 | \(This happens when some other X client makes its own selection |
| 2705 | or when a Lisp program explicitly clears the selection.) |
| 2706 | The functions are called with one argument, the selection type |
| 2707 | \(a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'). */); |
| 2708 | Vx_lost_selection_functions = Qnil; |
| 2709 | |
| 2710 | DEFVAR_LISP ("x-sent-selection-functions", Vx_sent_selection_functions, |
| 2711 | doc: /* A list of functions to be called when Emacs answers a selection request. |
| 2712 | The functions are called with three arguments: |
| 2713 | - the selection name (typically `PRIMARY', `SECONDARY', or `CLIPBOARD'); |
| 2714 | - the selection-type which Emacs was asked to convert the |
| 2715 | selection into before sending (for example, `STRING' or `LENGTH'); |
| 2716 | - a flag indicating success or failure for responding to the request. |
| 2717 | We might have failed (and declined the request) for any number of reasons, |
| 2718 | including being asked for a selection that we no longer own, or being asked |
| 2719 | to convert into a type that we don't know about or that is inappropriate. |
| 2720 | This hook doesn't let you change the behavior of Emacs's selection replies, |
| 2721 | it merely informs you that they have happened. */); |
| 2722 | Vx_sent_selection_functions = Qnil; |
| 2723 | |
| 2724 | DEFVAR_LISP ("x-select-enable-clipboard-manager", |
| 2725 | Vx_select_enable_clipboard_manager, |
| 2726 | doc: /* Whether to enable X clipboard manager support. |
| 2727 | If non-nil, then whenever Emacs is killed or an Emacs frame is deleted |
| 2728 | while owning the X clipboard, the clipboard contents are saved to the |
| 2729 | clipboard manager if one is present. */); |
| 2730 | Vx_select_enable_clipboard_manager = Qt; |
| 2731 | |
| 2732 | DEFVAR_INT ("x-selection-timeout", x_selection_timeout, |
| 2733 | doc: /* Number of milliseconds to wait for a selection reply. |
| 2734 | If the selection owner doesn't reply in this time, we give up. |
| 2735 | A value of 0 means wait as long as necessary. This is initialized from the |
| 2736 | \"*selectionTimeout\" resource. */); |
| 2737 | x_selection_timeout = 0; |
| 2738 | |
| 2739 | /* QPRIMARY is defined in keyboard.c. */ |
| 2740 | DEFSYM (QSECONDARY, "SECONDARY"); |
| 2741 | DEFSYM (QSTRING, "STRING"); |
| 2742 | DEFSYM (QINTEGER, "INTEGER"); |
| 2743 | DEFSYM (QCLIPBOARD, "CLIPBOARD"); |
| 2744 | DEFSYM (QTIMESTAMP, "TIMESTAMP"); |
| 2745 | DEFSYM (QTEXT, "TEXT"); |
| 2746 | DEFSYM (QCOMPOUND_TEXT, "COMPOUND_TEXT"); |
| 2747 | DEFSYM (QUTF8_STRING, "UTF8_STRING"); |
| 2748 | DEFSYM (QDELETE, "DELETE"); |
| 2749 | DEFSYM (QMULTIPLE, "MULTIPLE"); |
| 2750 | DEFSYM (QINCR, "INCR"); |
| 2751 | DEFSYM (QEMACS_TMP, "_EMACS_TMP_"); |
| 2752 | DEFSYM (QTARGETS, "TARGETS"); |
| 2753 | DEFSYM (QATOM, "ATOM"); |
| 2754 | DEFSYM (QATOM_PAIR, "ATOM_PAIR"); |
| 2755 | DEFSYM (QCLIPBOARD_MANAGER, "CLIPBOARD_MANAGER"); |
| 2756 | DEFSYM (QSAVE_TARGETS, "SAVE_TARGETS"); |
| 2757 | DEFSYM (QNULL, "NULL"); |
| 2758 | DEFSYM (Qcompound_text_with_extensions, "compound-text-with-extensions"); |
| 2759 | DEFSYM (Qforeign_selection, "foreign-selection"); |
| 2760 | DEFSYM (Qx_lost_selection_functions, "x-lost-selection-functions"); |
| 2761 | DEFSYM (Qx_sent_selection_functions, "x-sent-selection-functions"); |
| 2762 | } |