PageRenderTime 72ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/clx/mit-clx/translate.lisp

https://github.com/ynd/clisp-branch--ynd-devel
Lisp | 559 lines | 376 code | 36 blank | 147 comment | 0 complexity | dae3cf47e2a7ccace7c4e4342ae8fa32 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, BSD-3-Clause
  1. ;;; -*- Mode:Lisp; Package:XLIB; Syntax:COMMON-LISP; Base:10; Lowercase:YES -*-
  2. ;;;
  3. ;;; TEXAS INSTRUMENTS INCORPORATED
  4. ;;; P.O. BOX 2909
  5. ;;; AUSTIN, TEXAS 78769
  6. ;;;
  7. ;;; Copyright (C) 1987 Texas Instruments Incorporated.
  8. ;;;
  9. ;;; Permission is granted to any individual or institution to use, copy, modify,
  10. ;;; and distribute this software, provided that this complete copyright and
  11. ;;; permission notice is maintained, intact, in all copies and supporting
  12. ;;; documentation.
  13. ;;;
  14. ;;; Texas Instruments Incorporated provides this software "as is" without
  15. ;;; express or implied warranty.
  16. ;;;
  17. (in-package :xlib)
  18. (defvar *keysym-sets* nil) ;; Alist of (name first-keysym last-keysym)
  19. (defun define-keysym-set (set first-keysym last-keysym)
  20. ;; Define all keysyms from first-keysym up to and including
  21. ;; last-keysym to be in SET (returned from the keysym-set function).
  22. ;; Signals an error if the keysym range overlaps an existing set.
  23. (declare (type keyword set)
  24. (type keysym first-keysym last-keysym))
  25. (when (> first-keysym last-keysym)
  26. (rotatef first-keysym last-keysym))
  27. (setq *keysym-sets* (delete set *keysym-sets* :key #'car))
  28. (dolist (set *keysym-sets*)
  29. (let ((first (second set))
  30. (last (third set)))
  31. (when (or (<= first first-keysym last)
  32. (<= first last-keysym last))
  33. (error "Keysym range overlaps existing set ~s" set))))
  34. (push (list set first-keysym last-keysym) *keysym-sets*)
  35. set)
  36. (defun keysym-set (keysym)
  37. ;; Return the character code set name of keysym
  38. (declare (type keysym keysym)
  39. (clx-values keyword))
  40. (dolist (set *keysym-sets*)
  41. (let ((first (second set))
  42. (last (third set)))
  43. (when (<= first keysym last)
  44. (return (first set))))))
  45. (eval-when (:compile-toplevel :execute :load-toplevel) ;; Required for Vaxlisp ...
  46. (defmacro keysym (keysym &rest bytes)
  47. ;; Build a keysym.
  48. ;; If KEYSYM is an integer, it is used as the most significant bits of
  49. ;; the keysym, and BYTES are used to specify low order bytes. The last
  50. ;; parameter is always byte4 of the keysym. If KEYSYM is not an
  51. ;; integer, the keysym associated with KEYSYM is returned.
  52. ;;
  53. ;; This is a macro and not a function macro to promote compile-time
  54. ;; lookup. All arguments are evaluated.
  55. (declare (type t keysym)
  56. (type list bytes)
  57. (clx-values keysym))
  58. (typecase keysym
  59. ((integer 0 *)
  60. (dolist (b bytes keysym) (setq keysym (+ (ash keysym 8) b))))
  61. (otherwise
  62. (or (car (character->keysyms keysym))
  63. (error "~s Isn't the name of a keysym" keysym)))))
  64. )
  65. (defvar *keysym->character-map*
  66. (make-hash-table :test (keysym->character-map-test) :size 400))
  67. ;; Keysym-mappings are a list of the form (object translate lowercase modifiers mask)
  68. ;; With the following accessor macros. Everything after OBJECT is optional.
  69. (defmacro keysym-mapping-object (keysym-mapping)
  70. ;; Parameter to translate
  71. `(first ,keysym-mapping))
  72. (defmacro keysym-mapping-translate (keysym-mapping)
  73. ;; Function to be called with parameters (display state OBJECT)
  74. ;; when translating KEYSYM and modifiers and mask are satisfied.
  75. `(second ,keysym-mapping))
  76. (defmacro keysym-mapping-lowercase (keysym-mapping)
  77. ;; LOWERCASE is used for uppercase alphabetic keysyms. The value
  78. ;; is the associated lowercase keysym.
  79. `(third ,keysym-mapping))
  80. (defmacro keysym-mapping-modifiers (keysym-mapping)
  81. ;; MODIFIERS is either a modifier-mask or list containing intermixed
  82. ;; keysyms and state-mask-keys specifying when to use this
  83. ;; keysym-translation.
  84. `(fourth ,keysym-mapping))
  85. (defmacro keysym-mapping-mask (keysym-mapping)
  86. ;; MASK is either a modifier-mask or list containing intermixed
  87. ;; keysyms and state-mask-keys specifying which modifiers to look at
  88. ;; (i.e. modifiers not specified are don't-cares)
  89. `(fifth ,keysym-mapping))
  90. (defvar *default-keysym-translate-mask*
  91. (the (or (member :modifiers) mask16 (clx-list (or keysym state-mask-key)))
  92. (logand #xff (lognot (make-state-mask :lock))))
  93. "Default keysym state mask to use during keysym-translation.")
  94. (defun define-keysym (object keysym &key lowercase translate modifiers mask display)
  95. ;; Define the translation from keysym/modifiers to a (usually
  96. ;; character) object. ANy previous keysym definition with
  97. ;; KEYSYM and MODIFIERS is deleted before adding the new definition.
  98. ;;
  99. ;; MODIFIERS is either a modifier-mask or list containing intermixed
  100. ;; keysyms and state-mask-keys specifying when to use this
  101. ;; keysym-translation. The default is NIL.
  102. ;;
  103. ;; MASK is either a modifier-mask or list containing intermixed
  104. ;; keysyms and state-mask-keys specifying which modifiers to look at
  105. ;; (i.e. modifiers not specified are don't-cares).
  106. ;; If mask is :MODIFIERS then the mask is the same as the modifiers
  107. ;; (i.e. modifiers not specified by modifiers are don't cares)
  108. ;; The default mask is *default-keysym-translate-mask*
  109. ;;
  110. ;; If DISPLAY is specified, the translation will be local to DISPLAY,
  111. ;; otherwise it will be the default translation for all displays.
  112. ;;
  113. ;; LOWERCASE is used for uppercase alphabetic keysyms. The value
  114. ;; is the associated lowercase keysym. This information is used
  115. ;; by the keysym-both-case-p predicate (for caps-lock computations)
  116. ;; and by the keysym-downcase function.
  117. ;;
  118. ;; TRANSLATE will be called with parameters (display state OBJECT)
  119. ;; when translating KEYSYM and modifiers and mask are satisfied.
  120. ;; [e.g (zerop (logxor (logand state (or mask *default-keysym-translate-mask*))
  121. ;; (or modifiers 0)))
  122. ;; when mask and modifiers aren't lists of keysyms]
  123. ;; The default is #'default-keysym-translate
  124. ;;
  125. (declare (type (or base-char t) object)
  126. (type keysym keysym)
  127. (type (or null mask16 (clx-list (or keysym state-mask-key)))
  128. modifiers)
  129. (type (or null (member :modifiers) mask16 (clx-list (or keysym state-mask-key)))
  130. mask)
  131. (type (or null display) display)
  132. (type (or null keysym) lowercase)
  133. (type (or null (function (display card16 t) t)) translate))
  134. (flet ((merge-keysym-mappings (new old)
  135. ;; Merge new keysym-mapping with list of old mappings.
  136. ;; Ensure that the mapping with no modifiers or mask comes first.
  137. (let* ((key (keysym-mapping-modifiers new))
  138. (merge (delete key old :key #'cadddr :test #'equal)))
  139. (if key
  140. (nconc merge (list new))
  141. (cons new merge))))
  142. (mask-check (mask)
  143. (unless (or (numberp mask)
  144. (dolist (element mask t)
  145. (unless (or (find element *state-mask-vector*)
  146. (gethash element *keysym->character-map*))
  147. (return nil))))
  148. (x-type-error mask '(or mask16 (clx-list (or modifier-key modifier-keysym)))))))
  149. (let ((entry
  150. ;; Create with a single LIST call, to ensure cdr-coding
  151. (cond
  152. (mask
  153. (unless (eq mask :modifiers)
  154. (mask-check mask))
  155. (when (or (null modifiers) (and (numberp modifiers) (zerop modifiers)))
  156. (error "Mask with no modifiers"))
  157. (list object translate lowercase modifiers mask))
  158. (modifiers (mask-check modifiers)
  159. (list object translate lowercase modifiers))
  160. (lowercase (list object translate lowercase))
  161. (translate (list object translate))
  162. (t (list object)))))
  163. (if display
  164. (let ((previous (assoc keysym (display-keysym-translation display))))
  165. (if previous
  166. (setf (cdr previous) (merge-keysym-mappings entry (cdr previous)))
  167. (push (list keysym entry) (display-keysym-translation display))))
  168. (setf (gethash keysym *keysym->character-map*)
  169. (merge-keysym-mappings entry (gethash keysym *keysym->character-map*)))))
  170. object))
  171. (defun undefine-keysym (object keysym &key display modifiers &allow-other-keys)
  172. ;; Undefine the keysym-translation translating KEYSYM to OBJECT with MODIFIERS.
  173. ;; If DISPLAY is non-nil, undefine the translation for DISPLAY if it exists.
  174. (declare (type (or base-char t) object)
  175. (type keysym keysym)
  176. (type (or null mask16 (clx-list (or keysym state-mask-key)))
  177. modifiers)
  178. (type (or null display) display))
  179. (flet ((match (key entry)
  180. (let ((object (car key))
  181. (modifiers (cdr key)))
  182. (or (eql object (keysym-mapping-object entry))
  183. (equal modifiers (keysym-mapping-modifiers entry))))))
  184. (let* (entry
  185. (previous (if display
  186. (cdr (setq entry (assoc keysym (display-keysym-translation display))))
  187. (gethash keysym *keysym->character-map*)))
  188. (key (cons object modifiers)))
  189. (when (and previous (find key previous :test #'match))
  190. (setq previous (delete key previous :test #'match))
  191. (if display
  192. (setf (cdr entry) previous)
  193. (setf (gethash keysym *keysym->character-map*) previous))))))
  194. (defun keysym-downcase (keysym)
  195. ;; If keysym has a lower-case equivalent, return it, otherwise return keysym.
  196. (declare (type keysym keysym))
  197. (declare (clx-values keysym))
  198. (let ((translations (gethash keysym *keysym->character-map*)))
  199. (or (and translations (keysym-mapping-lowercase (first translations))) keysym)))
  200. (defun keysym-uppercase-alphabetic-p (keysym)
  201. ;; Returns T if keysym is uppercase-alphabetic.
  202. ;; I.E. If it has a lowercase equivalent.
  203. (declare (type keysym keysym))
  204. (declare (clx-values (or null keysym)))
  205. (let ((translations (gethash keysym *keysym->character-map*)))
  206. (and translations
  207. (keysym-mapping-lowercase (first translations)))))
  208. (defun character->keysyms (character &optional display)
  209. ;; Given a character, return a list of all matching keysyms.
  210. ;; If DISPLAY is given, translations specific to DISPLAY are used,
  211. ;; otherwise only global translations are used.
  212. ;; Implementation dependent function.
  213. ;; May be slow [i.e. do a linear search over all known keysyms]
  214. (declare (type t character)
  215. (type (or null display) display)
  216. (clx-values (clx-list keysym)))
  217. (let ((result nil))
  218. (when display
  219. (dolist (mapping (display-keysym-translation display))
  220. (when (eql character (second mapping))
  221. (push (first mapping) result))))
  222. (maphash #'(lambda (keysym mappings)
  223. (dolist (mapping mappings)
  224. (when (eql (keysym-mapping-object mapping) character)
  225. (pushnew keysym result))))
  226. *keysym->character-map*)
  227. result))
  228. (eval-when (:compile-toplevel :execute :load-toplevel) ;; Required for Symbolics...
  229. (defparameter character-set-switch-keysym (keysym 255 126))
  230. (defparameter left-shift-keysym (keysym 255 225))
  231. (defparameter right-shift-keysym (keysym 255 226))
  232. (defparameter left-control-keysym (keysym 255 227))
  233. (defparameter right-control-keysym (keysym 255 228))
  234. (defparameter caps-lock-keysym (keysym 255 229))
  235. (defparameter shift-lock-keysym (keysym 255 230))
  236. (defparameter left-meta-keysym (keysym 255 231))
  237. (defparameter right-meta-keysym (keysym 255 232))
  238. (defparameter left-alt-keysym (keysym 255 233))
  239. (defparameter right-alt-keysym (keysym 255 234))
  240. (defparameter left-super-keysym (keysym 255 235))
  241. (defparameter right-super-keysym (keysym 255 236))
  242. (defparameter left-hyper-keysym (keysym 255 237))
  243. (defparameter right-hyper-keysym (keysym 255 238))
  244. ) ;; end eval-when
  245. ;;-----------------------------------------------------------------------------
  246. ;; Keysym mapping functions
  247. (defun display-keyboard-mapping (display)
  248. (declare (type display display))
  249. (declare (clx-values (simple-array keysym (display-max-keycode keysyms-per-keycode))))
  250. (or (display-keysym-mapping display)
  251. (setf (display-keysym-mapping display) (keyboard-mapping display))))
  252. (defun keycode->keysym (display keycode keysym-index)
  253. (declare (type display display)
  254. (type card8 keycode)
  255. (type card8 keysym-index)
  256. (clx-values keysym))
  257. (let* ((mapping (display-keyboard-mapping display))
  258. (keysym (aref mapping keycode keysym-index)))
  259. (declare (type (simple-array keysym (* *)) mapping)
  260. (type keysym keysym))
  261. ;; The keysym-mapping is brain dammaged.
  262. ;; Mappings for both-case alphabetic characters have the
  263. ;; entry for keysym-index zero set to the uppercase keysym
  264. ;; (this is normally where the lowercase keysym goes), and the
  265. ;; entry for keysym-index one is zero.
  266. (cond ((zerop keysym-index) ; Lowercase alphabetic keysyms
  267. (keysym-downcase keysym))
  268. ((and (zerop keysym) (plusp keysym-index)) ; Get the uppercase keysym
  269. (aref mapping keycode 0))
  270. (t keysym))))
  271. (defun keysym->character (display keysym &optional (state 0))
  272. ;; Find the character associated with a keysym.
  273. ;; STATE can be used to set character attributes.
  274. ;; Implementation dependent function.
  275. (declare (type display display)
  276. (type keysym keysym)
  277. (type card16 state))
  278. (declare (clx-values (or null character)))
  279. (let* ((display-mappings (cdr (assoc keysym (display-keysym-translation display))))
  280. (mapping (or ;; Find the matching display mapping
  281. (dolist (mapping display-mappings)
  282. (when (mapping-matches-p display state mapping)
  283. (return mapping)))
  284. ;; Find the matching static mapping
  285. (dolist (mapping (gethash keysym *keysym->character-map*))
  286. (when (mapping-matches-p display state mapping)
  287. (return mapping))))))
  288. (when mapping
  289. (funcall (or (keysym-mapping-translate mapping) 'default-keysym-translate)
  290. display state (keysym-mapping-object mapping)))))
  291. (defun mapping-matches-p (display state mapping)
  292. ;; Returns T when the modifiers and mask in MAPPING satisfies STATE for DISPLAY
  293. (declare (type display display)
  294. (type mask16 state)
  295. (type list mapping))
  296. (declare (clx-values generalized-boolean))
  297. (flet
  298. ((modifiers->mask (display-mapping modifiers errorp &aux (mask 0))
  299. ;; Convert MODIFIERS, which is a modifier mask, or a list of state-mask-keys into a mask.
  300. ;; If ERRORP is non-nil, return NIL when an unknown modifier is specified,
  301. ;; otherwise ignore unknown modifiers.
  302. (declare (type list display-mapping) ; Alist of (keysym . mask)
  303. (type (or mask16 list) modifiers)
  304. (type mask16 mask))
  305. (declare (clx-values (or null mask16)))
  306. (if (numberp modifiers)
  307. modifiers
  308. (dolist (modifier modifiers mask)
  309. (declare (type symbol modifier))
  310. (let ((bit (position modifier (the simple-vector *state-mask-vector*) :test #'eq)))
  311. (setq mask
  312. (logior mask
  313. (if bit
  314. (ash 1 bit)
  315. (or (cdr (assoc modifier display-mapping))
  316. ;; bad modifier
  317. (if errorp
  318. (return-from modifiers->mask nil)
  319. 0))))))))))
  320. (let* ((display-mapping (get-display-modifier-mapping display))
  321. (mapping-modifiers (keysym-mapping-modifiers mapping))
  322. (modifiers (or (modifiers->mask display-mapping (or mapping-modifiers 0) t)
  323. (return-from mapping-matches-p nil)))
  324. (mapping-mask (or (keysym-mapping-mask mapping) ; If no mask, use the default.
  325. (if mapping-modifiers ; If no modifiers, match anything.
  326. *default-keysym-translate-mask*
  327. 0)))
  328. (mask (if (eq mapping-mask :modifiers)
  329. modifiers
  330. (modifiers->mask display-mapping mapping-mask nil))))
  331. (declare (type mask16 modifiers mask))
  332. (= (logand state mask) modifiers))))
  333. (defun default-keysym-index (display keycode state)
  334. ;; Returns a keysym-index for use with keycode->character
  335. (declare (clx-values card8))
  336. (macrolet ((keystate-p (state keyword)
  337. `(logbitp ,(position keyword *state-mask-vector*) ,state)))
  338. (let* ((mapping (display-keyboard-mapping display))
  339. (keysyms-per-keycode (array-dimension mapping 1))
  340. (symbolp (and (> keysyms-per-keycode 2)
  341. (state-keysymp display state character-set-switch-keysym)))
  342. (result (if symbolp 2 0)))
  343. (declare (type (simple-array keysym (* *)) mapping)
  344. (type generalized-boolean symbolp)
  345. (type card8 keysyms-per-keycode result))
  346. (when (and (< result keysyms-per-keycode)
  347. (keysym-shift-p display state (keysym-uppercase-alphabetic-p
  348. (aref mapping keycode 0))))
  349. (incf result))
  350. result)))
  351. (defun keysym-shift-p (display state uppercase-alphabetic-p &key
  352. shift-lock-xors
  353. (control-modifiers
  354. '#.(list left-meta-keysym left-super-keysym left-hyper-keysym)))
  355. (declare (type display display)
  356. (type card16 state)
  357. (type generalized-boolean uppercase-alphabetic-p)
  358. (type generalized-boolean shift-lock-xors));;; If T, both SHIFT-LOCK and SHIFT is the same
  359. ;;; as neither if the character is alphabetic.
  360. (declare (clx-values generalized-boolean))
  361. (macrolet ((keystate-p (state keyword)
  362. `(logbitp ,(position keyword *state-mask-vector*) ,state)))
  363. (let* ((controlp (or (keystate-p state :control)
  364. (dolist (modifier control-modifiers)
  365. (when (state-keysymp display state modifier)
  366. (return t)))))
  367. (shiftp (keystate-p state :shift))
  368. (lockp (keystate-p state :lock))
  369. (alphap (or uppercase-alphabetic-p
  370. (not (state-keysymp display #.(make-state-mask :lock)
  371. caps-lock-keysym)))))
  372. (declare (type generalized-boolean controlp shiftp lockp alphap))
  373. ;; Control keys aren't affected by lock
  374. (unless controlp
  375. ;; Not a control character - check state of lock modifier
  376. (when (and lockp
  377. alphap
  378. (or (not shiftp) shift-lock-xors)) ; Lock doesn't unshift unless shift-lock-xors
  379. (setq shiftp (not shiftp))))
  380. shiftp)))
  381. ;;; default-keysym-index implements the following tables:
  382. ;;;
  383. ;;; control shift caps-lock character character
  384. ;;; 0 0 0 #\a #\8
  385. ;;; 0 0 1 #\A #\8
  386. ;;; 0 1 0 #\A #\*
  387. ;;; 0 1 1 #\A #\*
  388. ;;; 1 0 0 #\control-A #\control-8
  389. ;;; 1 0 1 #\control-A #\control-8
  390. ;;; 1 1 0 #\control-shift-a #\control-*
  391. ;;; 1 1 1 #\control-shift-a #\control-*
  392. ;;;
  393. ;;; control shift shift-lock character character
  394. ;;; 0 0 0 #\a #\8
  395. ;;; 0 0 1 #\A #\*
  396. ;;; 0 1 0 #\A #\*
  397. ;;; 0 1 1 #\A #\8
  398. ;;; 1 0 0 #\control-A #\control-8
  399. ;;; 1 0 1 #\control-A #\control-*
  400. ;;; 1 1 0 #\control-shift-a #\control-*
  401. ;;; 1 1 1 #\control-shift-a #\control-8
  402. (defun keycode->character (display keycode state &key keysym-index
  403. (keysym-index-function #'default-keysym-index))
  404. ;; keysym-index defaults to the result of keysym-index-function which
  405. ;; is called with the following parameters:
  406. ;; (char0 state caps-lock-p keysyms-per-keycode)
  407. ;; where char0 is the "character" object associated with keysym-index 0 and
  408. ;; caps-lock-p is non-nil when the keysym associated with the lock
  409. ;; modifier is for caps-lock.
  410. ;; STATE can also used for setting character attributes.
  411. ;; Implementation dependent function.
  412. (declare (type display display)
  413. (type card8 keycode)
  414. (type card16 state)
  415. (type (or null card8) keysym-index)
  416. (type (or null (function (base-char card16 generalized-boolean card8) card8))
  417. keysym-index-function))
  418. (declare (clx-values (or null character)))
  419. (let* ((index (or keysym-index
  420. (funcall keysym-index-function display keycode state)))
  421. (keysym (if index (keycode->keysym display keycode index) 0)))
  422. (declare (type (or null card8) index)
  423. (type keysym keysym))
  424. (when (plusp keysym)
  425. (keysym->character display keysym state))))
  426. (defun get-display-modifier-mapping (display)
  427. (labels ((keysym-replace (display modifiers mask &aux result)
  428. (dolist (modifier modifiers result)
  429. (push (cons (keycode->keysym display modifier 0) mask) result))))
  430. (or (display-modifier-mapping display)
  431. (multiple-value-bind (shift lock control mod1 mod2 mod3 mod4 mod5)
  432. (modifier-mapping display)
  433. (setf (display-modifier-mapping display)
  434. (nconc (keysym-replace display shift #.(make-state-mask :shift))
  435. (keysym-replace display lock #.(make-state-mask :lock))
  436. (keysym-replace display control #.(make-state-mask :control))
  437. (keysym-replace display mod1 #.(make-state-mask :mod-1))
  438. (keysym-replace display mod2 #.(make-state-mask :mod-2))
  439. (keysym-replace display mod3 #.(make-state-mask :mod-3))
  440. (keysym-replace display mod4 #.(make-state-mask :mod-4))
  441. (keysym-replace display mod5 #.(make-state-mask :mod-5))))))))
  442. (defun state-keysymp (display state keysym)
  443. ;; Returns T when a modifier key associated with KEYSYM is on in STATE
  444. (declare (type display display)
  445. (type card16 state)
  446. (type keysym keysym))
  447. (declare (clx-values generalized-boolean))
  448. (let* ((mapping (get-display-modifier-mapping display))
  449. (mask (assoc keysym mapping)))
  450. (and mask (plusp (logand state (cdr mask))))))
  451. (defun mapping-notify (display request start count)
  452. ;; Called on a mapping-notify event to update
  453. ;; the keyboard-mapping cache in DISPLAY
  454. (declare (type display display)
  455. (type (member :modifier :keyboard :pointer) request)
  456. (type card8 start count)
  457. (ignore count start))
  458. ;; Invalidate the keyboard mapping to force the next key translation to get it
  459. (case request
  460. (:modifier
  461. (setf (display-modifier-mapping display) nil))
  462. (:keyboard
  463. (setf (display-keysym-mapping display) nil))))
  464. (defun keysym-in-map-p (display keysym keymap)
  465. ;; Returns T if keysym is found in keymap
  466. (declare (type display display)
  467. (type keysym keysym)
  468. (type (bit-vector 256) keymap))
  469. (declare (clx-values generalized-boolean))
  470. ;; The keysym may appear in the keymap more than once,
  471. ;; So we have to search the entire keysym map.
  472. (do* ((min (display-min-keycode display))
  473. (max (display-max-keycode display))
  474. (map (display-keyboard-mapping display))
  475. (jmax (min 2 (array-dimension map 1)))
  476. (i min (1+ i)))
  477. ((> i max))
  478. (declare (type card8 min max jmax)
  479. (type (simple-array keysym (* *)) map))
  480. (when (and (plusp (aref keymap i))
  481. (dotimes (j jmax)
  482. (when (= keysym (aref map i j)) (return t))))
  483. (return t))))
  484. (defun character-in-map-p (display character keymap)
  485. ;; Implementation dependent function.
  486. ;; Returns T if character is found in keymap
  487. (declare (type display display)
  488. (type character character)
  489. (type (bit-vector 256) keymap))
  490. (declare (clx-values generalized-boolean))
  491. ;; Check all one bits in keymap
  492. (do* ((min (display-min-keycode display))
  493. (max (display-max-keycode display))
  494. (jmax (array-dimension (display-keyboard-mapping display) 1))
  495. (i min (1+ i)))
  496. ((> i max))
  497. (declare (type card8 min max jmax))
  498. (when (and (plusp (aref keymap i))
  499. ;; Match when character is in mapping for this keycode
  500. (dotimes (j jmax)
  501. (when (eql character (keycode->character display i 0 :keysym-index j))
  502. (return t))))
  503. (return t))))
  504. (defun keysym->keycodes (display keysym)
  505. ;; Return keycodes for keysym, as multiple values
  506. (declare (type display display)
  507. (type keysym keysym))
  508. (declare (clx-values (or null keycode) (or null keycode) (or null keycode)))
  509. ;; The keysym may appear in the keymap more than once,
  510. ;; So we have to search the entire keysym map.
  511. (do* ((min (display-min-keycode display))
  512. (max (display-max-keycode display))
  513. (map (display-keyboard-mapping display))
  514. (jmax (min 2 (array-dimension map 1)))
  515. (i min (1+ i))
  516. (result nil))
  517. ((> i max) (values-list result))
  518. (declare (type card8 min max jmax)
  519. (type (simple-array keysym (* *)) map))
  520. (dotimes (j jmax)
  521. (when (= keysym (aref map i j))
  522. (push i result)))))