src/clfswm-circulate-mode.lisp (circulate-loop-function): Use is-a-key-pressed-p...
[clfswm.git] / src / xlib-util.lisp
blob1f6c20a9e5134e10547352e01d13209c7a640966
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2010 Philippe Brochard <hocwp@free.fr>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; --------------------------------------------------------------------------
26 (in-package :clfswm)
28 ;; Window states
29 (defconstant +withdrawn-state+ 0)
30 (defconstant +normal-state+ 1)
31 (defconstant +iconic-state+ 3)
34 (defparameter *window-events* '(:structure-notify
35 :property-change
36 :colormap-change
37 :focus-change
38 :enter-window
39 :exposure)
40 "The events to listen for on managed windows.")
43 (defparameter +netwm-supported+
44 '(:_NET_SUPPORTING_WM_CHECK
45 :_NET_NUMBER_OF_DESKTOPS
46 :_NET_DESKTOP_GEOMETRY
47 :_NET_DESKTOP_VIEWPORT
48 :_NET_CURRENT_DESKTOP
49 :_NET_WM_WINDOW_TYPE
50 :_NET_CLIENT_LIST)
51 "Supported NETWM properties.
52 Window types are in +WINDOW-TYPES+.")
54 (defparameter +netwm-window-types+
55 '((:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
56 (:_NET_WM_WINDOW_TYPE_DOCK . :dock)
57 (:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
58 (:_NET_WM_WINDOW_TYPE_MENU . :menu)
59 (:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
60 (:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
61 (:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
62 (:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
63 "Alist mapping NETWM window types to keywords.")
66 (defmacro with-xlib-protect (&body body)
67 "Prevent Xlib errors"
68 `(handler-case
69 (progn
70 ,@body)
71 ((or xlib:match-error xlib:window-error xlib:drawable-error) (c)
72 ;;(dbg c))))
73 ;;(declare (ignore c)))))
74 (format t "~&Xlib-error: ~A~%Body:~%~A~%" c ',body)
75 (force-output))))
76 ;;(dbg c ',body))))
78 ;;(defmacro with-xlib-protect (&body body)
79 ;; "Prevent Xlib errors"
80 ;; `(progn
81 ;; ,@body))
87 ;;;
88 ;;; Events management functions.
89 ;;;
90 (defparameter *unhandled-events* nil)
91 (defparameter *current-event-mode* nil)
93 (eval-when (:compile-toplevel :load-toplevel :execute)
94 (defun keyword->handle-event (mode keyword)
95 (symb 'handle-event-fun "-" mode "-" keyword)))
97 (defun handle-event->keyword (symbol)
98 (let* ((name (string-downcase (symbol-name symbol)))
99 (pos (search "handle-event-fun-" name)))
100 (when (and pos (zerop pos))
101 (let ((pos-mod (search "mode" name)))
102 (when pos-mod
103 (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
104 (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
107 (defmacro with-handle-event-symbol ((mode) &body body)
108 "Bind symbol to all handle event functions available in mode"
109 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
110 (with-all-internal-symbols (symbol :clfswm)
111 (let ((pos (symbol-search pattern symbol)))
112 (when (and pos (zerop pos))
113 ,@body)))))
116 (defun find-handle-event-function (&optional (mode ""))
117 "Print all handle event functions available in mode"
118 (with-handle-event-symbol (mode)
119 (print symbol)))
121 (defun assoc-keyword-handle-event (mode)
122 "Associate all keywords in mode to their corresponding handle event functions.
123 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
124 (setf *current-event-mode* mode)
125 (with-handle-event-symbol (mode)
126 (let ((keyword (handle-event->keyword symbol)))
127 (when (fboundp symbol)
128 #+:event-debug
129 (progn
130 (format t "~&Associating: ~S with ~S~%" symbol keyword)
131 (force-output))
132 (setf (symbol-function keyword) (symbol-function symbol))))))
134 (defun unassoc-keyword-handle-event (&optional (mode ""))
135 "Unbound all keywords from their corresponding handle event functions."
136 (setf *current-event-mode* nil)
137 (with-handle-event-symbol (mode)
138 (let ((keyword (handle-event->keyword symbol)))
139 (when (fboundp keyword)
140 #+:event-debug
141 (progn
142 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
143 (force-output))
144 (fmakunbound keyword)))))
146 (defmacro define-handler (mode keyword args &body body)
147 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
148 For example (define-handler main-mode :key-press (args) ...)
149 Expand in handle-event-fun-main-mode-key-press"
150 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
151 (declare (ignorable event-slots))
152 #+:event-debug (print (list *current-event-mode* event-key))
153 ,@body))
156 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
157 (with-xlib-protect
158 (if (fboundp event-key)
159 (apply event-key event-slots)
160 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal)))
167 (defun parse-display-string (display)
168 "Parse an X11 DISPLAY string and return the host and display from it."
169 (let* ((colon (position #\: display))
170 (host (subseq display 0 colon))
171 (rest (subseq display (1+ colon)))
172 (dot (position #\. rest))
173 (num (parse-integer (subseq rest 0 dot))))
174 (values host num)))
177 (defun banish-pointer ()
178 "Move the pointer to the lower right corner of the screen"
179 (with-placement (*banish-pointer-placement* x y)
180 (xlib:warp-pointer *root* x y)))
184 (defun window-state (win)
185 "Get the state (iconic, normal, withdraw of a window."
186 (first (xlib:get-property win :WM_STATE)))
189 (defun set-window-state (win state)
190 "Set the state (iconic, normal, withdrawn) of a window."
191 (xlib:change-property win
192 :WM_STATE
193 (list state)
194 :WM_STATE
195 32))
197 (defsetf window-state set-window-state)
201 (defun window-hidden-p (window)
202 (eql (window-state window) +iconic-state+))
205 (defun null-size-window-p (window)
206 (let ((hints (xlib:wm-normal-hints window)))
207 (and hints
208 (not (or (xlib:wm-size-hints-width hints)
209 (xlib:wm-size-hints-height hints)
210 (xlib:wm-size-hints-win-gravity hints)))
211 (xlib:wm-size-hints-user-specified-position-p hints))))
218 (defun unhide-window (window)
219 (when window
220 (with-xlib-protect
221 (when (window-hidden-p window)
222 (xlib:map-subwindows window)
223 (xlib:map-window window)
224 (setf (window-state window) +normal-state+
225 (xlib:window-event-mask window) *window-events*))))
226 (xlib:display-finish-output *display*))
229 (defun map-window (window)
230 (when window
231 (with-xlib-protect
232 (xlib:map-subwindows window)
233 (xlib:map-window window)
234 (xlib:display-finish-output *display*))))
236 (defun delete-window (window)
237 (send-client-message window :WM_PROTOCOLS
238 (xlib:intern-atom *display* "WM_DELETE_WINDOW"))
239 (xlib:display-finish-output *display*))
241 (defun destroy-window (window)
242 (xlib:kill-client *display* (xlib:window-id window))
243 (xlib:display-finish-output *display*))
250 ;;(defconstant +exwm-atoms+
251 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
252 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
253 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
254 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
255 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
256 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
257 ;; "_NET_DESKTOP_LAYOUT"
259 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
260 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
261 ;; "_NET_WM_MOVERESIZE"
263 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
265 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
266 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
267 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
268 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
269 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
270 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
271 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
272 ;; ;; "_NET_WM_MOVE_ACTIONS"
274 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
275 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
276 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
277 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
278 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
279 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
280 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
281 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
282 ;; "_NET_WM_STATE_FULLSCREEN"
283 ;; "_NET_WM_STATE_ABOVE"
284 ;; "_NET_WM_STATE_BELOW"
285 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
287 ;; "_NET_WM_ALLOWED_ACTIONS"
288 ;; "_NET_WM_ACTION_MOVE"
289 ;; "_NET_WM_ACTION_RESIZE"
290 ;; "_NET_WM_ACTION_SHADE"
291 ;; "_NET_WM_ACTION_STICK"
292 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
293 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
294 ;; "_NET_WM_ACTION_FULLSCREEN"
295 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
296 ;; "_NET_WM_ACTION_CLOSE"
298 ;; ))
301 ;;(defun intern-atoms (display)
302 ;; (declare (type xlib:display display))
303 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
304 ;; +exwm-atoms+)
305 ;; (values))
309 ;;(defun get-atoms-property (window property-atom atom-list-p)
310 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
311 ;; a list of atom-id."
312 ;; (xlib:get-property window property-atom
313 ;; :transform (when atom-list-p
314 ;; (lambda (id)
315 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
317 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
318 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
319 ;; or a list of keyword atom-names."
320 ;; (xlib:change-property window property-atom atoms :ATOM 32
321 ;; :mode mode
322 ;; :transform (unless (integerp (car atoms))
323 ;; (lambda (atom-key)
324 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
329 ;;(defun net-wm-state (window)
330 ;; (get-atoms-property window :_NET_WM_STATE t))
332 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
333 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
336 (defun hide-window (window)
337 (when window
338 (with-xlib-protect
339 (setf (window-state window) +iconic-state+
340 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
341 (xlib:unmap-window window)
342 (setf (xlib:window-event-mask window) *window-events*)))
343 (xlib:display-finish-output *display*))
347 (defun window-type (window)
348 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
349 :dialog, :transient, :maxsize and :normal."
350 (or (and (let ((hints (xlib:wm-normal-hints window)))
351 (and hints (or (xlib:wm-size-hints-max-width hints)
352 (xlib:wm-size-hints-max-height hints)
353 (xlib:wm-size-hints-min-aspect hints)
354 (xlib:wm-size-hints-max-aspect hints))))
355 :maxsize)
356 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
357 (when net-wm-window-type
358 (dolist (type-atom net-wm-window-type)
359 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
360 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
361 (and (xlib:get-property window :WM_TRANSIENT_FOR)
362 :transient)
363 :normal))
369 ;;; Stolen from Eclipse
370 (defun send-configuration-notify (window x y w h bw)
371 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
372 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
373 :event-window window
374 :window window
375 :x x :y y
376 :width w
377 :height h
378 :border-width bw
379 :propagate-p nil))
383 (defun send-client-message (window type &rest data)
384 "Send a client message to a client's window."
385 (xlib:send-event window
386 :client-message nil
387 :window window
388 :type type
389 :format 32
390 :data data))
396 (defun raise-window (window)
397 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
398 (when window
399 (with-xlib-protect
400 (when (window-hidden-p window)
401 (unhide-window window))
402 (setf (xlib:window-priority window) :top-if)))
403 (xlib:display-finish-output *display*))
405 (defun focus-window (window)
406 "Give the window focus."
407 (when window
408 (with-xlib-protect
409 (xlib:set-input-focus *display* window :parent)))
410 (xlib:display-finish-output *display*))
418 (defun no-focus ()
419 "don't focus any window but still read keyboard events."
420 (xlib:set-input-focus *display* *no-focus-window* :pointer-root)
421 (xlib:display-finish-output *display*))
426 (let ((cursor-font nil)
427 (cursor nil)
428 (pointer-grabbed nil))
429 (defun free-grab-pointer ()
430 (when cursor
431 (xlib:free-cursor cursor)
432 (setf cursor nil))
433 (when cursor-font
434 (xlib:close-font cursor-font)
435 (setf cursor-font nil)))
437 (defun xgrab-init-pointer ()
438 (setf pointer-grabbed nil))
440 (defun xgrab-pointer-p ()
441 pointer-grabbed)
443 (defun xgrab-pointer (root cursor-char cursor-mask-char
444 &optional (pointer-mask '(:enter-window :pointer-motion
445 :button-press :button-release)) owner-p)
446 "Grab the pointer and set the pointer shape."
447 (when pointer-grabbed
448 (xungrab-pointer))
449 (setf pointer-grabbed t)
450 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
451 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
452 (cond (cursor-char
453 (setf cursor-font (xlib:open-font *display* "cursor")
454 cursor (xlib:create-glyph-cursor :source-font cursor-font
455 :source-char (or cursor-char 68)
456 :mask-font cursor-font
457 :mask-char (or cursor-mask-char 69)
458 :foreground black
459 :background white))
460 (xlib:grab-pointer root pointer-mask
461 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
463 (xlib:grab-pointer root pointer-mask
464 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
466 (defun xungrab-pointer ()
467 "Remove the grab on the cursor and restore the cursor shape."
468 (setf pointer-grabbed nil)
469 (xlib:ungrab-pointer *display*)
470 (free-grab-pointer)))
473 (let ((keyboard-grabbed nil))
474 (defun xgrab-init-keyboard ()
475 (setf keyboard-grabbed nil))
477 (defun xgrab-keyboard-p ()
478 keyboard-grabbed)
480 (defun xgrab-keyboard (root)
481 (setf keyboard-grabbed t)
482 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
485 (defun xungrab-keyboard ()
486 (setf keyboard-grabbed nil)
487 (xlib:ungrab-keyboard *display*)))
494 (defun ungrab-all-buttons (window)
495 (xlib:ungrab-button window :any :modifiers :any))
497 (defun grab-all-buttons (window)
498 (ungrab-all-buttons window)
499 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
500 :modifiers :any
501 :owner-p nil
502 :sync-pointer-p t
503 :sync-keyboard-p nil))
505 (defun ungrab-all-keys (window)
506 (xlib:ungrab-key window :any :modifiers :any))
509 (defun stop-button-event ()
510 (xlib:allow-events *display* :sync-pointer))
512 (defun replay-button-event ()
513 (xlib:allow-events *display* :replay-pointer))
523 ;;; Mouse action on window
524 (let (add-fn add-arg dx dy window)
525 (define-handler move-window-mode :motion-notify (root-x root-y)
526 (unless (compress-motion-notify)
527 (setf (xlib:drawable-x window) (+ root-x dx)
528 (xlib:drawable-y window) (+ root-y dy))
529 (when add-fn
530 (apply add-fn add-arg))))
532 (define-handler move-window-mode :button-release ()
533 (throw 'exit-move-window-mode nil))
535 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
536 (setf window orig-window
537 add-fn additional-fn
538 add-arg additional-arg
539 dx (- (xlib:drawable-x window) orig-x)
540 dy (- (xlib:drawable-y window) orig-y))
541 (raise-window window)
542 (let ((pointer-grabbed-p (xgrab-pointer-p)))
543 (unless pointer-grabbed-p
544 (xgrab-pointer *root* nil nil))
545 (when additional-fn
546 (apply additional-fn additional-arg))
547 (generic-mode 'move-window-mode 'exit-move-window-mode
548 :original-mode '(main-mode))
549 (unless pointer-grabbed-p
550 (xungrab-pointer)))))
553 (let (add-fn add-arg window
554 o-x o-y
555 orig-width orig-height
556 min-width max-width
557 min-height max-height)
558 (define-handler resize-window-mode :motion-notify (root-x root-y)
559 (unless (compress-motion-notify)
560 (setf (xlib:drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
561 (xlib:drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))
562 (when add-fn
563 (apply add-fn add-arg))))
565 (define-handler resize-window-mode :button-release ()
566 (throw 'exit-resize-window-mode nil))
568 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
569 (let* ((pointer-grabbed-p (xgrab-pointer-p))
570 (hints (xlib:wm-normal-hints orig-window)))
571 (setf window orig-window
572 add-fn additional-fn
573 add-arg additional-arg
574 o-x orig-x
575 o-y orig-y
576 orig-width (xlib:drawable-width window)
577 orig-height (xlib:drawable-height window)
578 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
579 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
580 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
581 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum))
582 (raise-window window)
583 (unless pointer-grabbed-p
584 (xgrab-pointer *root* nil nil))
585 (when additional-fn
586 (apply additional-fn additional-arg))
587 (generic-mode 'resize-window-mode 'exit-resize-window-mode
588 :original-mode '(main-mode))
589 (unless pointer-grabbed-p
590 (xungrab-pointer)))))
593 (define-handler wait-mouse-button-release-mode :button-release ()
594 (throw 'exit-wait-mouse-button-release-mode nil))
596 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
597 (let ((pointer-grabbed-p (xgrab-pointer-p)))
598 (unless pointer-grabbed-p
599 (xgrab-pointer *root* cursor-char cursor-mask-char))
600 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)
601 (unless pointer-grabbed-p
602 (xungrab-pointer))))
607 (let ((color-hash (make-hash-table :test 'equal)))
608 (defun get-color (color)
609 (multiple-value-bind (val foundp)
610 (gethash color color-hash)
611 (if foundp
613 (setf (gethash color color-hash)
614 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
618 (defgeneric ->color (color))
620 (defmethod ->color ((color-name string))
621 color-name)
623 (defmethod ->color ((color integer))
624 (labels ((hex->float (color)
625 (/ (logand color #xFF) 256.0)))
626 (xlib:make-color :blue (hex->float color)
627 :green (hex->float (ash color -8))
628 :red (hex->float (ash color -16)))))
630 (defmethod ->color ((color list))
631 (destructuring-bind (red green blue) color
632 (xlib:make-color :blue red :green green :red blue)))
634 (defmethod ->color ((color xlib:color))
635 color)
637 (defmethod ->color (color)
638 (format t "Wrong color type: ~A~%" color)
639 "White")
642 (defun color->rgb (color)
643 (multiple-value-bind (r g b)
644 (xlib:color-rgb color)
645 (+ (ash (round (* 256 r)) +16)
646 (ash (round (* 256 g)) +8)
647 (round (* 256 b)))))
653 (defmacro my-character->keysyms (ch)
654 "Convert a char to a keysym"
655 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
656 ;; some day. Or just copied from MIT-CLX or some other CLX
657 ;; implementation (see translate.lisp and keysyms.lisp). For now,
658 ;; we do like this. It suffices for modifiers and ASCII symbols.
659 (if (fboundp 'xlib:character->keysyms)
660 `(xlib:character->keysyms ,ch)
661 `(list
662 (case ,ch
663 (:character-set-switch #xFF7E)
664 (:left-shift #xFFE1)
665 (:right-shift #xFFE2)
666 (:left-control #xFFE3)
667 (:right-control #xFFE4)
668 (:caps-lock #xFFE5)
669 (:shift-lock #xFFE6)
670 (:left-meta #xFFE7)
671 (:right-meta #xFFE8)
672 (:left-alt #xFFE9)
673 (:right-alt #xFFEA)
674 (:left-super #xFFEB)
675 (:right-super #xFFEC)
676 (:left-hyper #xFFED)
677 (:right-hyper #xFFEE)
679 (etypecase ,ch
680 (character
681 ;; Latin-1 characters have their own value as keysym
682 (if (< 31 (char-code ,ch) 256)
683 (char-code ,ch)
684 (error "Don't know how to get keysym from ~A" ,ch)))))))))
689 (defun char->keycode (char)
690 "Convert a character to a keycode"
691 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
694 (defun keycode->char (code state)
695 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
697 (defun modifiers->state (modifier-list)
698 (apply #'xlib:make-state-mask modifier-list))
700 (defun state->modifiers (state)
701 (xlib:make-state-keys state))
703 (defun keycode->keysym (code modifiers)
704 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
705 ((member :mod-5 modifiers) 2)
706 (t 0))))
709 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask) &body body)
710 `(let ((pointer-grabbed (xgrab-pointer-p))
711 (keyboard-grabbed (xgrab-keyboard-p)))
712 (xgrab-pointer *root* ,cursor ,mask)
713 (unless keyboard-grabbed
714 (xgrab-keyboard *root*))
715 (unwind-protect
716 (progn
717 ,@body)
718 (if pointer-grabbed
719 (xgrab-pointer *root* ,old-cursor ,old-mask)
720 (xungrab-pointer))
721 (unless keyboard-grabbed
722 (xungrab-keyboard)))))
729 (let ((modifier-list nil))
730 (defun init-modifier-list ()
731 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
732 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
733 "Mode_switch" "script_switch" "ISO_Level3_Shift"
734 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
735 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name)) ;; PHIL: todo here
736 (push it modifier-list))))
738 (defun modifier-p (code)
739 (member code modifier-list)))
741 (defun wait-no-key-or-button-press ()
742 (with-grab-keyboard-and-pointer (66 67 66 67)
743 (loop
744 (let ((key (loop for k across (xlib:query-keymap *display*)
745 for code from 0
746 when (and (plusp k) (not (modifier-p code)))
747 return t))
748 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
749 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
750 return t)))
751 (when (and (not key) (not button))
752 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
753 (:motion-notify () t)
754 (:key-press () t)
755 (:key-release () t)
756 (:button-press () t)
757 (:button-release () t)
758 (t nil)))
759 (return))))))
762 (defun wait-a-key-or-button-press ()
763 (with-grab-keyboard-and-pointer (24 25 66 67)
764 (loop
765 (let ((key (loop for k across (xlib:query-keymap *display*)
766 unless (zerop k) return t))
767 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
768 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
769 return t)))
770 (when (or key button)
771 (return))))))
775 (defun compress-motion-notify ()
776 (when *have-to-compress-notify*
777 (loop while (xlib:event-cond (*display* :timeout 0)
778 (:motion-notify () t)))))
781 (defun display-all-cursors (&optional (display-time 1))
782 "Display all X11 cursors for display-time seconds"
783 (loop for i from 0 to 152 by 2
784 do (xgrab-pointer *root* i (1+ i))
785 (dbg i)
786 (sleep display-time)
787 (xungrab-pointer)))
792 ;;; Double buffering tools
793 (defun clear-pixmap-buffer (window gc)
794 (rotatef (xlib:gcontext-foreground gc) (xlib:gcontext-background gc))
795 (xlib:draw-rectangle *pixmap-buffer* gc
796 0 0 (xlib:drawable-width window) (xlib:drawable-height window)
798 (rotatef (xlib:gcontext-foreground gc) (xlib:gcontext-background gc)))
800 (defun copy-pixmap-buffer (window gc)
801 (xlib:copy-area *pixmap-buffer* gc
802 0 0 (xlib:drawable-width window) (xlib:drawable-height window)
803 window 0 0))
806 (defun is-a-key-pressed-p ()
807 (loop for k across (xlib:query-keymap *display*)
808 when (plusp k)
809 return t))