* test/lisp/mouse-tests.el: Fix tests broken by mouse.el change
[emacs.git] / lisp / x-dnd.el
blob5f8578444a03543ba685c7738fbfa938a1d812f2
1 ;;; x-dnd.el --- drag and drop support for X
3 ;; Copyright (C) 2004-2018 Free Software Foundation, Inc.
5 ;; Author: Jan Djärv <jan.h.d@swipnet.se>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: window, drag, drop
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file provides the drop part only. Currently supported protocols
28 ;; are XDND, Motif and the old KDE 1.x protocol.
30 ;;; Code:
32 (require 'dnd)
34 ;;; Customizable variables
35 (defcustom x-dnd-test-function 'x-dnd-default-test-function
36 "The function drag and drop uses to determine if to accept or reject a drop.
37 The function takes three arguments, WINDOW, ACTION and TYPES.
38 WINDOW is where the mouse is when the function is called. WINDOW may be a
39 frame if the mouse isn't over a real window (i.e. menu bar, tool bar or
40 scroll bar). ACTION is the suggested action from the drag and drop source,
41 one of the symbols move, copy, link or ask. TYPES is a list of available
42 types for the drop.
44 The function shall return nil to reject the drop or a cons with two values,
45 the wanted action as car and the wanted type as cdr. The wanted action
46 can be copy, move, link, ask or private.
47 The default value for this variable is `x-dnd-default-test-function'."
48 :version "22.1"
49 :type 'symbol
50 :group 'x)
54 (defcustom x-dnd-types-alist
56 (,(purecopy "text/uri-list") . x-dnd-handle-uri-list)
57 (,(purecopy "text/x-moz-url") . x-dnd-handle-moz-url)
58 (,(purecopy "_NETSCAPE_URL") . x-dnd-handle-uri-list)
59 (,(purecopy "FILE_NAME") . x-dnd-handle-file-name)
60 (,(purecopy "UTF8_STRING") . x-dnd-insert-utf8-text)
61 (,(purecopy "text/plain;charset=UTF-8") . x-dnd-insert-utf8-text)
62 (,(purecopy "text/plain;charset=utf-8") . x-dnd-insert-utf8-text)
63 (,(purecopy "text/unicode") . x-dnd-insert-utf16-text)
64 (,(purecopy "text/plain") . dnd-insert-text)
65 (,(purecopy "COMPOUND_TEXT") . x-dnd-insert-ctext)
66 (,(purecopy "STRING") . dnd-insert-text)
67 (,(purecopy "TEXT") . dnd-insert-text)
69 "Which function to call to handle a drop of that type.
70 If the type for the drop is not present, or the function is nil,
71 the drop is rejected. The function takes three arguments, WINDOW, ACTION
72 and DATA. WINDOW is where the drop occurred, ACTION is the action for
73 this drop (copy, move, link, private or ask) as determined by a previous
74 call to `x-dnd-test-function'. DATA is the drop data.
75 The function shall return the action used (copy, move, link or private)
76 if drop is successful, nil if not."
77 :version "22.1"
78 :type 'alist
79 :group 'x)
81 (defcustom x-dnd-known-types
82 (mapcar 'purecopy
83 '("text/uri-list"
84 "text/x-moz-url"
85 "_NETSCAPE_URL"
86 "FILE_NAME"
87 "UTF8_STRING"
88 "text/plain;charset=UTF-8"
89 "text/plain;charset=utf-8"
90 "text/unicode"
91 "text/plain"
92 "COMPOUND_TEXT"
93 "STRING"
94 "TEXT"
96 "The types accepted by default for dropped data.
97 The types are chosen in the order they appear in the list."
98 :version "22.1"
99 :type '(repeat string)
100 :group 'x
103 ;; Internal variables
105 (defvar x-dnd-current-state nil
106 "The current state for a drop.
107 This is an alist with one entry for each display. The value for each display
108 is a vector that contains the state for drag and drop for that display.
109 Elements in the vector are:
110 Last buffer drag was in,
111 last window drag was in,
112 types available for drop,
113 the action suggested by the source,
114 the type we want for the drop,
115 the action we want for the drop,
116 any protocol specific data.")
118 (defvar x-dnd-empty-state [nil nil nil nil nil nil nil])
120 (declare-function x-register-dnd-atom "xselect.c")
122 (defun x-dnd-init-frame (&optional frame)
123 "Setup drag and drop for FRAME (i.e. create appropriate properties)."
124 (when (eq 'x (window-system frame))
125 (x-register-dnd-atom "DndProtocol" frame)
126 (x-register-dnd-atom "_MOTIF_DRAG_AND_DROP_MESSAGE" frame)
127 (x-register-dnd-atom "XdndEnter" frame)
128 (x-register-dnd-atom "XdndPosition" frame)
129 (x-register-dnd-atom "XdndLeave" frame)
130 (x-register-dnd-atom "XdndDrop" frame)
131 (x-dnd-init-xdnd-for-frame frame)
132 (x-dnd-init-motif-for-frame frame)))
134 (defun x-dnd-get-state-cons-for-frame (frame-or-window)
135 "Return the entry in `x-dnd-current-state' for a frame or window."
136 (let* ((frame (if (framep frame-or-window) frame-or-window
137 (window-frame frame-or-window)))
138 (display (frame-parameter frame 'display)))
139 (if (not (assoc display x-dnd-current-state))
140 (push (cons display (copy-sequence x-dnd-empty-state))
141 x-dnd-current-state))
142 (assoc display x-dnd-current-state)))
144 (defun x-dnd-get-state-for-frame (frame-or-window)
145 "Return the state in `x-dnd-current-state' for a frame or window."
146 (cdr (x-dnd-get-state-cons-for-frame frame-or-window)))
148 (defun x-dnd-default-test-function (_window _action types)
149 "The default test function for drag and drop.
150 WINDOW is where the mouse is when this function is called. It may be
151 a frame if the mouse is over the menu bar, scroll bar or tool bar.
152 ACTION is the suggested action from the source, and TYPES are the
153 types the drop data can have. This function only accepts drops with
154 types in `x-dnd-known-types'. It always returns the action private."
155 (let ((type (x-dnd-choose-type types)))
156 (when type (cons 'private type))))
159 (defun x-dnd-current-type (frame-or-window)
160 "Return the type we want the DND data to be in for the current drop.
161 FRAME-OR-WINDOW is the frame or window that the mouse is over."
162 (aref (x-dnd-get-state-for-frame frame-or-window) 4))
164 (defun x-dnd-forget-drop (frame-or-window)
165 "Remove all state for the last drop.
166 FRAME-OR-WINDOW is the frame or window that the mouse is over."
167 (setcdr (x-dnd-get-state-cons-for-frame frame-or-window)
168 (copy-sequence x-dnd-empty-state)))
170 (defun x-dnd-maybe-call-test-function (window action)
171 "Call `x-dnd-test-function' if something has changed.
172 WINDOW is the window the mouse is over. ACTION is the suggested
173 action from the source. If nothing has changed, return the last
174 action and type we got from `x-dnd-test-function'."
175 (let ((buffer (when (window-live-p window)
176 (window-buffer window)))
177 (current-state (x-dnd-get-state-for-frame window)))
178 (unless (and (equal buffer (aref current-state 0))
179 (equal window (aref current-state 1))
180 (equal action (aref current-state 3)))
181 (save-current-buffer
182 (when buffer (set-buffer buffer))
183 (let* ((action-type (funcall x-dnd-test-function
184 window
185 action
186 (aref current-state 2)))
187 (handler (cdr (assoc (cdr action-type) x-dnd-types-alist))))
188 ;; Ignore action-type if we have no handler.
189 (setq current-state
190 (x-dnd-save-state window
191 action
192 (when handler action-type)))))))
193 (let ((current-state (x-dnd-get-state-for-frame window)))
194 (cons (aref current-state 5)
195 (aref current-state 4))))
197 (defun x-dnd-save-state (window action action-type &optional types extra-data)
198 "Save the state of the current drag and drop.
199 WINDOW is the window the mouse is over. ACTION is the action suggested
200 by the source. ACTION-TYPE is the result of calling `x-dnd-test-function'.
201 If given, TYPES are the types for the drop data that the source supports.
202 EXTRA-DATA is data needed for a specific protocol."
203 (let ((current-state (x-dnd-get-state-for-frame window)))
204 (aset current-state 5 (car action-type))
205 (aset current-state 4 (cdr action-type))
206 (aset current-state 3 action)
207 (when types (aset current-state 2 types))
208 (when extra-data (aset current-state 6 extra-data))
209 (aset current-state 1 window)
210 (aset current-state 0 (and (window-live-p window) (window-buffer window)))
211 (setcdr (x-dnd-get-state-cons-for-frame window) current-state)))
214 (defun x-dnd-handle-moz-url (window action data)
215 "Handle one item of type text/x-moz-url.
216 WINDOW is the window where the drop happened. ACTION is ignored.
217 DATA is the moz-url, which is formatted as two strings separated by \\r\\n.
218 The first string is the URL, the second string is the title of that URL.
219 DATA is encoded in utf-16. Decode the URL and call `x-dnd-handle-uri-list'."
220 ;; Mozilla and applications based on it use text/unicode, but it is
221 ;; impossible to tell if it is le or be. Use what the machine Emacs
222 ;; runs on uses. This loses if dropping between machines
223 ;; with different endian-ness, but it is the best we can do.
224 (let* ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le))
225 (string (decode-coding-string data coding))
226 (strings (split-string string "[\r\n]" t))
227 ;; Can one drop more than one moz-url ?? Assume not.
228 (url (car strings)))
229 (x-dnd-handle-uri-list window action url)))
231 (defun x-dnd-insert-utf8-text (window action text)
232 "Decode the UTF-8 text and insert it at point.
233 TEXT is the text as a string, WINDOW is the window where the drop happened."
234 (dnd-insert-text window action (decode-coding-string text 'utf-8)))
236 (defun x-dnd-insert-utf16-text (window action text)
237 "Decode the UTF-16 text and insert it at point.
238 TEXT is the text as a string, WINDOW is the window where the drop happened."
239 ;; See comment in x-dnd-handle-moz-url about coding.
240 (let ((coding (if (eq (byteorder) ?B) 'utf-16be 'utf-16le)))
241 (dnd-insert-text window action (decode-coding-string text coding))))
243 (defun x-dnd-insert-ctext (window action text)
244 "Decode the compound text and insert it at point.
245 TEXT is the text as a string, WINDOW is the window where the drop happened."
246 (dnd-insert-text window action
247 (decode-coding-string text
248 'compound-text-with-extensions)))
250 (defun x-dnd-handle-uri-list (window action string)
251 "Split an uri-list into separate URIs and call `dnd-handle-one-url'.
252 WINDOW is the window where the drop happened.
253 STRING is the uri-list as a string. The URIs are separated by \\r\\n."
254 (let ((uri-list (split-string string "[\0\r\n]" t))
255 retval)
256 (dolist (bf uri-list)
257 ;; If one URL is handled, treat as if the whole drop succeeded.
258 (let ((did-action (dnd-handle-one-url window action bf)))
259 (when did-action (setq retval did-action))))
260 retval))
262 (defun x-dnd-handle-file-name (window action string)
263 "Convert file names to URLs and call `dnd-handle-one-url'.
264 WINDOW is the window where the drop happened.
265 STRING is the file names as a string, separated by nulls."
266 (let ((uri-list (split-string string "[\0\r\n]" t))
267 (coding (or file-name-coding-system
268 default-file-name-coding-system))
269 retval)
270 (dolist (bf uri-list)
271 ;; If one URL is handled, treat as if the whole drop succeeded.
272 (if coding (setq bf (encode-coding-string bf coding)))
273 (let* ((file-uri (concat "file://"
274 (mapconcat 'url-hexify-string
275 (split-string bf "/") "/")))
276 (did-action (dnd-handle-one-url window action file-uri)))
277 (when did-action (setq retval did-action))))
278 retval))
281 (defun x-dnd-choose-type (types &optional known-types)
282 "Choose which type we want to receive for the drop.
283 TYPES are the types the source of the drop offers, a vector of type names
284 as strings or symbols. Select among the types in `x-dnd-known-types' or
285 KNOWN-TYPES if given, and return that type name.
286 If no suitable type is found, return nil."
287 (let* ((known-list (or known-types x-dnd-known-types))
288 (first-known-type (car known-list))
289 (types-array types)
290 (found (when first-known-type
291 (catch 'done
292 (dotimes (i (length types-array))
293 (let* ((type (aref types-array i))
294 (typename (if (symbolp type)
295 (symbol-name type) type)))
296 (when (equal first-known-type typename)
297 (throw 'done first-known-type))))
298 nil))))
300 (if (and (not found) (cdr known-list))
301 (x-dnd-choose-type types (cdr known-list))
302 found)))
304 (defun x-dnd-drop-data (event frame window data type)
305 "Drop one data item onto a frame.
306 EVENT is the client message for the drop, FRAME is the frame the drop
307 occurred on. WINDOW is the window of FRAME where the drop happened.
308 DATA is the data received from the source, and type is the type for DATA,
309 see `x-dnd-types-alist').
311 Returns the action used (move, copy, link, private) if drop was successful,
312 nil if not."
313 (let* ((type-info (assoc type x-dnd-types-alist))
314 (handler (cdr type-info))
315 (state (x-dnd-get-state-for-frame frame))
316 (action (aref state 5))
317 (w (posn-window (event-start event))))
318 (when handler
319 (if (and (window-live-p w)
320 (not (window-minibuffer-p w))
321 (not (window-dedicated-p w)))
322 ;; If dropping in an ordinary window which we could use,
323 ;; let dnd-open-file-other-window specify what to do.
324 (progn
325 (when (not mouse-yank-at-point)
326 (goto-char (posn-point (event-start event))))
327 (funcall handler window action data))
328 ;; If we can't display the file here,
329 ;; make a new window for it.
330 (let ((dnd-open-file-other-window t))
331 (select-frame frame)
332 (funcall handler window action data))))))
334 (defun x-dnd-handle-drag-n-drop-event (event)
335 "Receive drag and drop events (X client messages).
336 Currently XDND, Motif and old KDE 1.x protocols are recognized."
337 (interactive "e")
338 (let* ((client-message (car (cdr (cdr event))))
339 (window (posn-window (event-start event)))
340 (message-atom (aref client-message 0))
341 (frame (aref client-message 1))
342 (format (aref client-message 2))
343 (data (aref client-message 3)))
345 (cond ((equal "DndProtocol" message-atom) ; Old KDE 1.x.
346 (x-dnd-handle-old-kde event frame window message-atom format data))
348 ((equal "_MOTIF_DRAG_AND_DROP_MESSAGE" message-atom) ; Motif
349 (x-dnd-handle-motif event frame window message-atom format data))
351 ((and (> (length message-atom) 4) ; XDND protocol.
352 (equal "Xdnd" (substring message-atom 0 4)))
353 (x-dnd-handle-xdnd event frame window message-atom format data)))))
356 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
357 ;;; Old KDE protocol. Only dropping of files.
359 (declare-function x-window-property "xfns.c"
360 (prop &optional frame type source delete-p vector-ret-p))
362 (defun x-dnd-handle-old-kde (_event frame window _message _format _data)
363 "Open the files in a KDE 1.x drop."
364 (let ((values (x-window-property "DndSelection" frame nil 0 t)))
365 (x-dnd-handle-uri-list window 'private
366 (replace-regexp-in-string "\0$" "" values))))
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
371 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
372 ;;; XDND protocol.
374 (defconst x-dnd-xdnd-to-action
375 '(("XdndActionPrivate" . private)
376 ("XdndActionCopy" . copy)
377 ("XdndActionMove" . move)
378 ("XdndActionLink" . link)
379 ("XdndActionAsk" . ask))
380 "Mapping from XDND action types to lisp symbols.")
382 (declare-function x-change-window-property "xfns.c"
383 (prop value &optional frame type format outer-P))
385 (defun x-dnd-init-xdnd-for-frame (frame)
386 "Set the XdndAware property for FRAME to indicate that we do XDND."
387 (x-change-window-property "XdndAware"
388 '(5) ;; The version of XDND we support.
389 frame "ATOM" 32 t))
391 (defun x-dnd-get-drop-width-height (frame w accept)
392 "Return the width/height to be sent in a XDndStatus message.
393 FRAME is the frame and W is the window where the drop happened.
394 If ACCEPT is nil return 0 (empty rectangle),
395 otherwise if W is a window, return its width/height,
396 otherwise return the frame width/height."
397 (if accept
398 (if (windowp w) ;; w is not a window if dropping on the menu bar,
399 ;; scroll bar or tool bar.
400 (let ((edges (window-inside-pixel-edges w)))
401 (cons
402 (- (nth 2 edges) (nth 0 edges)) ;; right - left
403 (- (nth 3 edges) (nth 1 edges)))) ;; bottom - top
404 (cons (frame-pixel-width frame)
405 (frame-pixel-height frame)))
408 (defun x-dnd-get-drop-x-y (frame w)
409 "Return the x/y coordinates to be sent in a XDndStatus message.
410 Coordinates are required to be absolute.
411 FRAME is the frame and W is the window where the drop happened.
412 If W is a window, return its absolute coordinates,
413 otherwise return the frame coordinates."
414 (let* ((frame-left (frame-parameter frame 'left))
415 ;; If the frame is outside the display, frame-left looks like
416 ;; '(0 -16). Extract the -16.
417 (frame-real-left (if (consp frame-left) (car (cdr frame-left))
418 frame-left))
419 (frame-top (frame-parameter frame 'top))
420 (frame-real-top (if (consp frame-top) (car (cdr frame-top))
421 frame-top)))
422 (if (windowp w)
423 (let ((edges (window-inside-pixel-edges w)))
424 (cons
425 (+ frame-real-left (nth 0 edges))
426 (+ frame-real-top (nth 1 edges))))
427 (cons frame-real-left frame-real-top))))
429 (declare-function x-get-atom-name "xselect.c" (value &optional frame))
430 (declare-function x-send-client-message "xselect.c"
431 (display dest from message-type format values))
432 (declare-function x-get-selection-internal "xselect.c"
433 (selection-symbol target-type &optional time-stamp terminal))
435 (defun x-dnd-version-from-flags (flags)
436 "Return the version byte from the 32 bit FLAGS in an XDndEnter message"
437 (if (consp flags) ;; Long as cons
438 (ash (car flags) -8)
439 (ash flags -24))) ;; Ordinary number
441 (defun x-dnd-more-than-3-from-flags (flags)
442 "Return the nmore-than3 bit from the 32 bit FLAGS in an XDndEnter message"
443 (if (consp flags)
444 (logand (cdr flags) 1)
445 (logand flags 1)))
447 (defun x-dnd-handle-xdnd (event frame window message _format data)
448 "Receive one XDND event (client message) and send the appropriate reply.
449 EVENT is the client message. FRAME is where the mouse is now.
450 WINDOW is the window within FRAME where the mouse is now.
451 FORMAT is 32 (not used). MESSAGE is the data part of an XClientMessageEvent."
452 (cond ((equal "XdndEnter" message)
453 (let* ((flags (aref data 1))
454 (version (x-dnd-version-from-flags flags))
455 (more-than-3 (x-dnd-more-than-3-from-flags flags))
456 (dnd-source (aref data 0)))
457 (message "%s %s" version more-than-3)
458 (if version ;; If flags is bad, version will be nil.
459 (x-dnd-save-state
460 window nil nil
461 (if (> more-than-3 0)
462 (x-window-property "XdndTypeList"
463 frame "AnyPropertyType"
464 dnd-source nil t)
465 (vector (x-get-atom-name (aref data 2))
466 (x-get-atom-name (aref data 3))
467 (x-get-atom-name (aref data 4))))))))
469 ((equal "XdndPosition" message)
470 (let* ((action (x-get-atom-name (aref data 4)))
471 (dnd-source (aref data 0))
472 (action-type (x-dnd-maybe-call-test-function
473 window
474 (cdr (assoc action x-dnd-xdnd-to-action))))
475 (reply-action (car (rassoc (car action-type)
476 x-dnd-xdnd-to-action)))
477 (accept ;; 1 = accept, 0 = reject
478 (if (and reply-action action-type) 1 0))
479 (list-to-send
480 (list (string-to-number
481 (frame-parameter frame 'outer-window-id))
482 accept ;; 1 = Accept, 0 = reject.
483 (x-dnd-get-drop-x-y frame window)
484 (x-dnd-get-drop-width-height
485 frame window (eq accept 1))
486 (or reply-action 0)
488 (x-send-client-message
489 frame dnd-source frame "XdndStatus" 32 list-to-send)
492 ((equal "XdndLeave" message)
493 (x-dnd-forget-drop window))
495 ((equal "XdndDrop" message)
496 (if (windowp window) (select-window window))
497 (let* ((dnd-source (aref data 0))
498 (value (and (x-dnd-current-type window)
499 (x-get-selection-internal
500 'XdndSelection
501 (intern (x-dnd-current-type window)))))
502 success action)
504 (setq action (if value
505 (condition-case info
506 (x-dnd-drop-data event frame window value
507 (x-dnd-current-type window))
508 (error
509 (message "Error: %s" info)
510 nil))))
512 (setq success (if action 1 0))
514 (x-send-client-message
515 frame dnd-source frame "XdndFinished" 32
516 (list (string-to-number (frame-parameter frame 'outer-window-id))
517 success ;; 1 = Success, 0 = Error
518 (if success "XdndActionPrivate" 0)
520 (x-dnd-forget-drop window)))
522 (t (error "Unknown XDND message %s %s" message data))))
524 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
525 ;;; Motif protocol.
527 (defun x-dnd-init-motif-for-frame (frame)
528 "Set _MOTIF_DRAG_RECEIVER_INFO for FRAME to indicate that we do Motif DND."
529 (x-change-window-property "_MOTIF_DRAG_RECEIVER_INFO"
530 (list
531 (byteorder)
532 0 ; The Motif DND version.
533 5 ; We want drag dynamic.
534 0 0 0 0 0 0 0
535 0 0 0 0 0 0) ; Property must be 16 bytes.
536 frame "_MOTIF_DRAG_RECEIVER_INFO" 8 t))
538 (defun x-dnd-get-motif-value (data offset size byteorder)
539 (cond ((eq size 2)
540 (if (eq byteorder ?l)
541 (+ (ash (aref data (1+ offset)) 8)
542 (aref data offset))
543 (+ (ash (aref data offset) 8)
544 (aref data (1+ offset)))))
546 ((eq size 4)
547 (if (eq byteorder ?l)
548 (cons (+ (ash (aref data (+ 3 offset)) 8)
549 (aref data (+ 2 offset)))
550 (+ (ash (aref data (1+ offset)) 8)
551 (aref data offset)))
552 (cons (+ (ash (aref data offset) 8)
553 (aref data (1+ offset)))
554 (+ (ash (aref data (+ 2 offset)) 8)
555 (aref data (+ 3 offset))))))))
557 (defun x-dnd-motif-value-to-list (value size byteorder)
558 (let ((bytes (cond ((eq size 2)
559 (list (logand (lsh value -8) ?\xff)
560 (logand value ?\xff)))
562 ((eq size 4)
563 (if (consp value)
564 (list (logand (lsh (car value) -8) ?\xff)
565 (logand (car value) ?\xff)
566 (logand (lsh (cdr value) -8) ?\xff)
567 (logand (cdr value) ?\xff))
568 (list (logand (lsh value -24) ?\xff)
569 (logand (lsh value -16) ?\xff)
570 (logand (lsh value -8) ?\xff)
571 (logand value ?\xff)))))))
572 (if (eq byteorder ?l)
573 (reverse bytes)
574 bytes)))
577 (defvar x-dnd-motif-message-types
578 '((0 . XmTOP_LEVEL_ENTER)
579 (1 . XmTOP_LEVEL_LEAVE)
580 (2 . XmDRAG_MOTION)
581 (3 . XmDROP_SITE_ENTER)
582 (4 . XmDROP_SITE_LEAVE)
583 (5 . XmDROP_START)
584 (6 . XmDROP_FINISH)
585 (7 . XmDRAG_DROP_FINISH)
586 (8 . XmOPERATION_CHANGED))
587 "Mapping from numbers to Motif DND message types.")
589 (defvar x-dnd-motif-to-action
590 '((1 . move)
591 (2 . copy)
592 (3 . link) ; Both 3 and 4 has been seen as link.
593 (4 . link)
594 (2 . private)) ; Motif does not have private, so use copy for private.
595 "Mapping from number to operation for Motif DND.")
597 (defun x-dnd-handle-motif (event frame window message-atom _format data)
598 (let* ((message-type (cdr (assoc (aref data 0) x-dnd-motif-message-types)))
599 (source-byteorder (aref data 1))
600 (my-byteorder (byteorder))
601 (source-flags (x-dnd-get-motif-value data 2 2 source-byteorder))
602 (source-action (cdr (assoc (logand ?\xF source-flags)
603 x-dnd-motif-to-action))))
605 (cond ((eq message-type 'XmTOP_LEVEL_ENTER)
606 (let* ((dnd-source (x-dnd-get-motif-value
607 data 8 4 source-byteorder))
608 (selection-atom (x-dnd-get-motif-value
609 data 12 4 source-byteorder))
610 (atom-name (x-get-atom-name selection-atom))
611 (types (when atom-name
612 (x-get-selection-internal (intern atom-name)
613 'TARGETS))))
614 (x-dnd-forget-drop frame)
615 (when types (x-dnd-save-state window nil nil
616 types
617 dnd-source))))
619 ;; Can not forget drop here, LEAVE comes before DROP_START and
620 ;; we need the state in DROP_START.
621 ((eq message-type 'XmTOP_LEVEL_LEAVE)
622 nil)
624 ((eq message-type 'XmDRAG_MOTION)
625 (let* ((state (x-dnd-get-state-for-frame frame))
626 (timestamp (x-dnd-motif-value-to-list
627 (x-dnd-get-motif-value data 4 4
628 source-byteorder)
629 4 my-byteorder))
630 (x (x-dnd-motif-value-to-list
631 (x-dnd-get-motif-value data 8 2 source-byteorder)
632 2 my-byteorder))
633 (y (x-dnd-motif-value-to-list
634 (x-dnd-get-motif-value data 10 2 source-byteorder)
635 2 my-byteorder))
636 (dnd-source (aref state 6))
637 (first-move (not (aref state 3)))
638 (action-type (x-dnd-maybe-call-test-function
639 window
640 source-action))
641 (reply-action (car (rassoc (car action-type)
642 x-dnd-motif-to-action)))
643 (reply-flags
644 (x-dnd-motif-value-to-list
645 (if reply-action
646 (+ reply-action
647 ?\x30 ; 30: valid drop site
648 ?\x700) ; 700: can do copy, move or link
649 ?\x30) ; 30: drop site, but noop.
650 2 my-byteorder))
651 (reply (append
652 (list
653 (+ ?\x80 ; 0x80 indicates a reply.
654 (if first-move
655 3 ; First time, reply is SITE_ENTER.
656 2)) ; Not first time, reply is DRAG_MOTION.
657 my-byteorder)
658 reply-flags
659 timestamp
661 y)))
662 (x-send-client-message frame
663 dnd-source
664 frame
665 "_MOTIF_DRAG_AND_DROP_MESSAGE"
667 reply)))
669 ((eq message-type 'XmOPERATION_CHANGED)
670 (let* ((state (x-dnd-get-state-for-frame frame))
671 (timestamp (x-dnd-motif-value-to-list
672 (x-dnd-get-motif-value data 4 4 source-byteorder)
673 4 my-byteorder))
674 (dnd-source (aref state 6))
675 (action-type (x-dnd-maybe-call-test-function
676 window
677 source-action))
678 (reply-action (car (rassoc (car action-type)
679 x-dnd-motif-to-action)))
680 (reply-flags
681 (x-dnd-motif-value-to-list
682 (if reply-action
683 (+ reply-action
684 ?\x30 ; 30: valid drop site
685 ?\x700) ; 700: can do copy, move or link
686 ?\x30) ; 30: drop site, but noop
687 2 my-byteorder))
688 (reply (append
689 (list
690 (+ ?\x80 ; 0x80 indicates a reply.
691 8) ; 8 is OPERATION_CHANGED
692 my-byteorder)
693 reply-flags
694 timestamp)))
695 (x-send-client-message frame
696 dnd-source
697 frame
698 "_MOTIF_DRAG_AND_DROP_MESSAGE"
700 reply)))
702 ((eq message-type 'XmDROP_START)
703 (let* ((x (x-dnd-motif-value-to-list
704 (x-dnd-get-motif-value data 8 2 source-byteorder)
705 2 my-byteorder))
706 (y (x-dnd-motif-value-to-list
707 (x-dnd-get-motif-value data 10 2 source-byteorder)
708 2 my-byteorder))
709 (selection-atom (x-dnd-get-motif-value
710 data 12 4 source-byteorder))
711 (atom-name (x-get-atom-name selection-atom))
712 (dnd-source (x-dnd-get-motif-value
713 data 16 4 source-byteorder))
714 (action-type (x-dnd-maybe-call-test-function
715 window
716 source-action))
717 (reply-action (car (rassoc (car action-type)
718 x-dnd-motif-to-action)))
719 (reply-flags
720 (x-dnd-motif-value-to-list
721 (if reply-action
722 (+ reply-action
723 ?\x30 ; 30: valid drop site
724 ?\x700) ; 700: can do copy, move or link
725 (+ ?\x30 ; 30: drop site, but noop.
726 ?\x200)) ; 200: drop cancel.
727 2 my-byteorder))
728 (reply (append
729 (list
730 (+ ?\x80 ; 0x80 indicates a reply.
731 5) ; DROP_START.
732 my-byteorder)
733 reply-flags
736 (timestamp (x-dnd-get-motif-value
737 data 4 4 source-byteorder))
738 action)
740 (x-send-client-message frame
741 dnd-source
742 frame
743 "_MOTIF_DRAG_AND_DROP_MESSAGE"
745 reply)
746 (setq action
747 (when (and reply-action atom-name)
748 (let* ((value (x-get-selection-internal
749 (intern atom-name)
750 (intern (x-dnd-current-type window)))))
751 (when value
752 (condition-case info
753 (x-dnd-drop-data event frame window value
754 (x-dnd-current-type window))
755 (error
756 (message "Error: %s" info)
757 nil))))))
758 (x-get-selection-internal
759 (intern atom-name)
760 (if action 'XmTRANSFER_SUCCESS 'XmTRANSFER_FAILURE)
761 timestamp)
762 (x-dnd-forget-drop frame)))
764 (t (error "Unknown Motif DND message %s %s" message-atom data)))))
769 (provide 'x-dnd)
771 ;;; x-dnd.el ends here