x-dnd.el 27 KB

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