src/clfswm-util.lisp (copy-focus-window, cut-focus-window): New functions and ask...
[clfswm.git] / src / xlib-util.lisp
blobeb7b500db81bb5da017b4b8d8be5d84155daef05
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2011 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 (with-simple-restart (top-level "Return to clfswm's top level")
70 ,@body)
71 ((or xlib:match-error xlib:window-error xlib:drawable-error) (c)
72 (progn
73 (dbg "Ignore Xlib Error" c ',body)
74 (unassoc-keyword-handle-event)
75 (assoc-keyword-handle-event 'main-mode)
76 (setf *in-second-mode* nil)))))
79 (defmacro with-x-pointer (&body body)
80 "Bind (x y) to mouse pointer positions"
81 `(multiple-value-bind (x y)
82 (xlib:query-pointer *root*)
83 ,@body))
87 (declaim (inline window-x2 window-y2))
88 (defun window-x2 (window)
89 (+ (xlib:drawable-x window) (xlib:drawable-width window)))
91 (defun window-y2 (window)
92 (+ (xlib:drawable-y window) (xlib:drawable-height window)))
96 ;;;
97 ;;; Events management functions.
98 ;;;
99 (defparameter *unhandled-events* nil)
100 (defparameter *current-event-mode* nil)
102 (eval-when (:compile-toplevel :load-toplevel :execute)
103 (defun keyword->handle-event (mode keyword)
104 (symb 'handle-event-fun "-" mode "-" keyword)))
106 (defun handle-event->keyword (symbol)
107 (let* ((name (string-downcase (symbol-name symbol)))
108 (pos (search "handle-event-fun-" name)))
109 (when (and pos (zerop pos))
110 (let ((pos-mod (search "mode" name)))
111 (when pos-mod
112 (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
113 (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
115 (defparameter *handle-event-fun-symbols* nil)
117 (defun fill-handle-event-fun-symbols ()
118 (with-all-internal-symbols (symbol :clfswm)
119 (let ((pos (symbol-search "handle-event-fun-" symbol)))
120 (when (and pos (zerop pos))
121 (pushnew symbol *handle-event-fun-symbols*)))))
124 (defmacro with-handle-event-symbol ((mode) &body body)
125 "Bind symbol to all handle event functions available in mode"
126 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
127 (dolist (symbol *handle-event-fun-symbols*)
128 (let ((pos (symbol-search pattern symbol)))
129 (when (and pos (zerop pos))
130 ,@body)))))
133 (defun find-handle-event-function (&optional (mode ""))
134 "Print all handle event functions available in mode"
135 (with-handle-event-symbol (mode)
136 (print symbol)))
138 (defun assoc-keyword-handle-event (mode)
139 "Associate all keywords in mode to their corresponding handle event functions.
140 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
141 (setf *current-event-mode* mode)
142 (with-handle-event-symbol (mode)
143 (let ((keyword (handle-event->keyword symbol)))
144 (when (fboundp symbol)
145 #+:event-debug
146 (progn
147 (format t "~&Associating: ~S with ~S~%" symbol keyword)
148 (force-output))
149 (setf (symbol-function keyword) (symbol-function symbol))))))
151 (defun unassoc-keyword-handle-event (&optional (mode ""))
152 "Unbound all keywords from their corresponding handle event functions."
153 (setf *current-event-mode* nil)
154 (with-handle-event-symbol (mode)
155 (let ((keyword (handle-event->keyword symbol)))
156 (when (fboundp keyword)
157 #+:event-debug
158 (progn
159 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
160 (force-output))
161 (fmakunbound keyword)))))
163 (defmacro define-handler (mode keyword args &body body)
164 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
165 For example (define-handler main-mode :key-press (args) ...)
166 Expand in handle-event-fun-main-mode-key-press"
167 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
168 (declare (ignorable event-slots))
169 #+:event-debug (print (list *current-event-mode* event-key))
170 ,@body))
174 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
175 (with-xlib-protect
176 (if (fboundp event-key)
177 (apply event-key event-slots)
178 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal))
179 (xlib:display-finish-output *display*))
186 (defun parse-display-string (display)
187 "Parse an X11 DISPLAY string and return the host and display from it."
188 (let* ((colon (position #\: display))
189 (host (subseq display 0 colon))
190 (rest (subseq display (1+ colon)))
191 (dot (position #\. rest))
192 (num (parse-integer (subseq rest 0 dot))))
193 (values host num)))
196 (defun banish-pointer ()
197 "Move the pointer to the lower right corner of the screen"
198 (with-placement (*banish-pointer-placement* x y)
199 (xlib:warp-pointer *root* x y)))
203 (defun window-state (win)
204 "Get the state (iconic, normal, withdraw of a window."
205 (first (xlib:get-property win :WM_STATE)))
208 (defun set-window-state (win state)
209 "Set the state (iconic, normal, withdrawn) of a window."
210 (xlib:change-property win
211 :WM_STATE
212 (list state)
213 :WM_STATE
214 32))
216 (defsetf window-state set-window-state)
220 (defun window-hidden-p (window)
221 (eql (window-state window) +iconic-state+))
224 (defun null-size-window-p (window)
225 (let ((hints (xlib:wm-normal-hints window)))
226 (and hints
227 (not (or (xlib:wm-size-hints-width hints)
228 (xlib:wm-size-hints-height hints)
229 (xlib:wm-size-hints-win-gravity hints)))
230 (xlib:wm-size-hints-user-specified-position-p hints))))
237 (defun unhide-window (window)
238 (when window
239 (when (window-hidden-p window)
240 (xlib:map-window window)
241 (setf (window-state window) +normal-state+
242 (xlib:window-event-mask window) *window-events*))))
245 (defun map-window (window)
246 (when window
247 (xlib:map-window window)))
250 (defun delete-window (window)
251 (send-client-message window :WM_PROTOCOLS
252 (xlib:intern-atom *display* "WM_DELETE_WINDOW")))
254 (defun destroy-window (window)
255 (xlib:kill-client *display* (xlib:window-id window)))
258 ;;(defconstant +exwm-atoms+
259 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
260 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
261 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
262 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
263 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
264 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
265 ;; "_NET_DESKTOP_LAYOUT"
267 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
268 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
269 ;; "_NET_WM_MOVERESIZE"
271 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
273 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
274 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
275 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
276 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
277 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
278 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
279 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
280 ;; ;; "_NET_WM_MOVE_ACTIONS"
282 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
283 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
284 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
285 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
286 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
287 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
288 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
289 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
290 ;; "_NET_WM_STATE_FULLSCREEN"
291 ;; "_NET_WM_STATE_ABOVE"
292 ;; "_NET_WM_STATE_BELOW"
293 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
295 ;; "_NET_WM_ALLOWED_ACTIONS"
296 ;; "_NET_WM_ACTION_MOVE"
297 ;; "_NET_WM_ACTION_RESIZE"
298 ;; "_NET_WM_ACTION_SHADE"
299 ;; "_NET_WM_ACTION_STICK"
300 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
301 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
302 ;; "_NET_WM_ACTION_FULLSCREEN"
303 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
304 ;; "_NET_WM_ACTION_CLOSE"
306 ;; ))
309 ;;(defun intern-atoms (display)
310 ;; (declare (type xlib:display display))
311 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
312 ;; +exwm-atoms+)
313 ;; (values))
317 ;;(defun get-atoms-property (window property-atom atom-list-p)
318 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
319 ;; a list of atom-id."
320 ;; (xlib:get-property window property-atom
321 ;; :transform (when atom-list-p
322 ;; (lambda (id)
323 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
325 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
326 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
327 ;; or a list of keyword atom-names."
328 ;; (xlib:change-property window property-atom atoms :ATOM 32
329 ;; :mode mode
330 ;; :transform (unless (integerp (car atoms))
331 ;; (lambda (atom-key)
332 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
337 ;;(defun net-wm-state (window)
338 ;; (get-atoms-property window :_NET_WM_STATE t))
340 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
341 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
344 (defun hide-window (window)
345 (when window
346 (setf (window-state window) +iconic-state+
347 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
348 (xlib:unmap-window window)
349 (setf (xlib:window-event-mask window) *window-events*)))
353 (defun window-type (window)
354 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
355 :dialog, :transient, :maxsize and :normal."
356 (or (and (let ((hints (xlib:wm-normal-hints window)))
357 (and hints (or (xlib:wm-size-hints-max-width hints)
358 (xlib:wm-size-hints-max-height hints)
359 (xlib:wm-size-hints-min-aspect hints)
360 (xlib:wm-size-hints-max-aspect hints))))
361 :maxsize)
362 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
363 (when net-wm-window-type
364 (dolist (type-atom net-wm-window-type)
365 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
366 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
367 (and (xlib:get-property window :WM_TRANSIENT_FOR)
368 :transient)
369 :normal))
375 ;;; Stolen from Eclipse
376 (defun send-configuration-notify (window x y w h bw)
377 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
378 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
379 :event-window window
380 :window window
381 :x x :y y
382 :width w
383 :height h
384 :border-width bw
385 :propagate-p nil))
389 (defun send-client-message (window type &rest data)
390 "Send a client message to a client's window."
391 (xlib:send-event window
392 :client-message nil
393 :window window
394 :type type
395 :format 32
396 :data data))
402 (defun raise-window (window)
403 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
404 (when (xlib:window-p window)
405 (when (window-hidden-p window)
406 (unhide-window window))
407 (setf (xlib:window-priority window) :above)))
409 (defun focus-window (window)
410 "Give the window focus."
411 (when (xlib:window-p window)
412 (xlib:set-input-focus *display* window :parent)))
414 (defun raise-and-focus-window (window)
415 "Raise and focus."
416 (raise-window window)
417 (focus-window window))
419 (defun no-focus ()
420 "don't focus any window but still read keyboard events."
421 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
424 (defun lower-window (window sibling)
425 "Map the window if needed and bring it just above sibling. Does not affect focus."
426 (when (xlib:window-p window)
427 (when (window-hidden-p window)
428 (unhide-window window))
429 (setf (xlib:window-priority window sibling) :below)))
434 (let ((cursor-font nil)
435 (cursor nil)
436 (pointer-grabbed nil))
437 (defun free-grab-pointer ()
438 (when cursor
439 (xlib:free-cursor cursor)
440 (setf cursor nil))
441 (when cursor-font
442 (xlib:close-font cursor-font)
443 (setf cursor-font nil)))
445 (defun xgrab-init-pointer ()
446 (setf pointer-grabbed nil))
448 (defun xgrab-pointer-p ()
449 pointer-grabbed)
451 (defun xgrab-pointer (root cursor-char cursor-mask-char
452 &optional (pointer-mask '(:enter-window :pointer-motion
453 :button-press :button-release)) owner-p)
454 "Grab the pointer and set the pointer shape."
455 (when pointer-grabbed
456 (xungrab-pointer))
457 (setf pointer-grabbed t)
458 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
459 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
460 (cond (cursor-char
461 (setf cursor-font (xlib:open-font *display* "cursor")
462 cursor (xlib:create-glyph-cursor :source-font cursor-font
463 :source-char (or cursor-char 68)
464 :mask-font cursor-font
465 :mask-char (or cursor-mask-char 69)
466 :foreground black
467 :background white))
468 (xlib:grab-pointer root pointer-mask
469 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
471 (xlib:grab-pointer root pointer-mask
472 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
474 (defun xungrab-pointer ()
475 "Remove the grab on the cursor and restore the cursor shape."
476 (setf pointer-grabbed nil)
477 (xlib:ungrab-pointer *display*)
478 (xlib:display-finish-output *display*)
479 (free-grab-pointer)))
482 (let ((keyboard-grabbed nil))
483 (defun xgrab-init-keyboard ()
484 (setf keyboard-grabbed nil))
486 (defun xgrab-keyboard-p ()
487 keyboard-grabbed)
489 (defun xgrab-keyboard (root)
490 (setf keyboard-grabbed t)
491 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
494 (defun xungrab-keyboard ()
495 (setf keyboard-grabbed nil)
496 (xlib:ungrab-keyboard *display*)))
503 (defun ungrab-all-buttons (window)
504 (xlib:ungrab-button window :any :modifiers :any))
506 (defun grab-all-buttons (window)
507 (ungrab-all-buttons window)
508 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
509 :modifiers :any
510 :owner-p nil
511 :sync-pointer-p t
512 :sync-keyboard-p nil))
514 (defun ungrab-all-keys (window)
515 (xlib:ungrab-key window :any :modifiers :any))
518 (defun stop-button-event ()
519 (xlib:allow-events *display* :sync-pointer))
521 (defun replay-button-event ()
522 (xlib:allow-events *display* :replay-pointer))
532 ;;; Mouse action on window
533 (let (add-fn add-arg dx dy window)
534 (define-handler move-window-mode :motion-notify (root-x root-y)
535 (unless (compress-motion-notify)
536 (if add-fn
537 (multiple-value-bind (move-x move-y)
538 (apply add-fn add-arg)
539 (when move-x
540 (setf (xlib:drawable-x window) (+ root-x dx)))
541 (when move-y
542 (setf (xlib:drawable-y window) (+ root-y dy))))
543 (setf (xlib:drawable-x window) (+ root-x dx)
544 (xlib:drawable-y window) (+ root-y dy)))))
546 (define-handler move-window-mode :key-release ()
547 (throw 'exit-move-window-mode nil))
549 (define-handler move-window-mode :button-release ()
550 (throw 'exit-move-window-mode nil))
552 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
553 (setf window orig-window
554 add-fn additional-fn
555 add-arg additional-arg
556 dx (- (xlib:drawable-x window) orig-x)
557 dy (- (xlib:drawable-y window) orig-y)
558 (xlib:window-border window) (get-color *color-move-window*))
559 (raise-window window)
560 (let ((pointer-grabbed-p (xgrab-pointer-p)))
561 (unless pointer-grabbed-p
562 (xgrab-pointer *root* nil nil))
563 (when additional-fn
564 (apply additional-fn additional-arg))
565 (generic-mode 'move-window-mode 'exit-move-window-mode
566 :original-mode '(main-mode))
567 (unless pointer-grabbed-p
568 (xungrab-pointer)))))
571 (let (add-fn add-arg window
572 o-x o-y
573 orig-width orig-height
574 min-width max-width
575 min-height max-height)
576 (define-handler resize-window-mode :motion-notify (root-x root-y)
577 (unless (compress-motion-notify)
578 (if add-fn
579 (multiple-value-bind (resize-w resize-h)
580 (apply add-fn add-arg)
581 (when resize-w
582 (setf (xlib:drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
583 (when resize-h
584 (setf (xlib:drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
585 (setf (xlib:drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
586 (xlib:drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
588 (define-handler resize-window-mode :key-release ()
589 (throw 'exit-resize-window-mode nil))
591 (define-handler resize-window-mode :button-release ()
592 (throw 'exit-resize-window-mode nil))
594 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
595 (let* ((pointer-grabbed-p (xgrab-pointer-p))
596 (hints (xlib:wm-normal-hints orig-window)))
597 (setf window orig-window
598 add-fn additional-fn
599 add-arg additional-arg
600 o-x orig-x
601 o-y orig-y
602 orig-width (xlib:drawable-width window)
603 orig-height (xlib:drawable-height window)
604 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
605 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
606 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
607 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
608 (xlib:window-border window) (get-color *color-move-window*))
609 (raise-window window)
610 (unless pointer-grabbed-p
611 (xgrab-pointer *root* nil nil))
612 (when additional-fn
613 (apply additional-fn additional-arg))
614 (generic-mode 'resize-window-mode 'exit-resize-window-mode
615 :original-mode '(main-mode))
616 (unless pointer-grabbed-p
617 (xungrab-pointer)))))
620 (define-handler wait-mouse-button-release-mode :button-release ()
621 (throw 'exit-wait-mouse-button-release-mode nil))
623 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
624 (let ((pointer-grabbed-p (xgrab-pointer-p)))
625 (unless pointer-grabbed-p
626 (xgrab-pointer *root* cursor-char cursor-mask-char))
627 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)
628 (unless pointer-grabbed-p
629 (xungrab-pointer))))
634 (let ((color-hash (make-hash-table :test 'equal)))
635 (defun get-color (color)
636 (multiple-value-bind (val foundp)
637 (gethash color color-hash)
638 (if foundp
640 (setf (gethash color color-hash)
641 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
645 (defgeneric ->color (color))
647 (defmethod ->color ((color-name string))
648 color-name)
650 (defmethod ->color ((color integer))
651 (labels ((hex->float (color)
652 (/ (logand color #xFF) 256.0)))
653 (xlib:make-color :blue (hex->float color)
654 :green (hex->float (ash color -8))
655 :red (hex->float (ash color -16)))))
657 (defmethod ->color ((color list))
658 (destructuring-bind (red green blue) color
659 (xlib:make-color :blue red :green green :red blue)))
661 (defmethod ->color ((color xlib:color))
662 color)
664 (defmethod ->color (color)
665 (format t "Wrong color type: ~A~%" color)
666 "White")
669 (defun color->rgb (color)
670 (multiple-value-bind (r g b)
671 (xlib:color-rgb color)
672 (+ (ash (round (* 256 r)) +16)
673 (ash (round (* 256 g)) +8)
674 (round (* 256 b)))))
680 (defmacro my-character->keysyms (ch)
681 "Convert a char to a keysym"
682 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
683 ;; some day. Or just copied from MIT-CLX or some other CLX
684 ;; implementation (see translate.lisp and keysyms.lisp). For now,
685 ;; we do like this. It suffices for modifiers and ASCII symbols.
686 (if (fboundp 'xlib:character->keysyms)
687 `(xlib:character->keysyms ,ch)
688 `(list
689 (case ,ch
690 (:character-set-switch #xFF7E)
691 (:left-shift #xFFE1)
692 (:right-shift #xFFE2)
693 (:left-control #xFFE3)
694 (:right-control #xFFE4)
695 (:caps-lock #xFFE5)
696 (:shift-lock #xFFE6)
697 (:left-meta #xFFE7)
698 (:right-meta #xFFE8)
699 (:left-alt #xFFE9)
700 (:right-alt #xFFEA)
701 (:left-super #xFFEB)
702 (:right-super #xFFEC)
703 (:left-hyper #xFFED)
704 (:right-hyper #xFFEE)
706 (etypecase ,ch
707 (character
708 ;; Latin-1 characters have their own value as keysym
709 (if (< 31 (char-code ,ch) 256)
710 (char-code ,ch)
711 (error "Don't know how to get keysym from ~A" ,ch)))))))))
716 (defun char->keycode (char)
717 "Convert a character to a keycode"
718 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
721 (defun keycode->char (code state)
722 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
724 (defun modifiers->state (modifier-list)
725 (apply #'xlib:make-state-mask modifier-list))
727 (defun state->modifiers (state)
728 (xlib:make-state-keys state))
730 (defun keycode->keysym (code modifiers)
731 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
732 ((member :mod-5 modifiers) 4)
733 (t 0))))
736 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask) &body body)
737 `(let ((pointer-grabbed (xgrab-pointer-p))
738 (keyboard-grabbed (xgrab-keyboard-p)))
739 (xgrab-pointer *root* ,cursor ,mask)
740 (unless keyboard-grabbed
741 (xgrab-keyboard *root*))
742 (unwind-protect
743 (progn
744 ,@body)
745 (if pointer-grabbed
746 (xgrab-pointer *root* ,old-cursor ,old-mask)
747 (xungrab-pointer))
748 (unless keyboard-grabbed
749 (xungrab-keyboard)))))
756 (let ((modifier-list nil))
757 (defun init-modifier-list ()
758 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
759 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
760 "Mode_switch" "script_switch" "ISO_Level3_Shift"
761 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
762 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
763 (push it modifier-list))))
765 (defun modifier-p (code)
766 (member code modifier-list)))
768 (defun wait-no-key-or-button-press ()
769 (with-grab-keyboard-and-pointer (66 67 66 67)
770 (loop
771 (let ((key (loop for k across (xlib:query-keymap *display*)
772 for code from 0
773 when (and (plusp k) (not (modifier-p code)))
774 return t))
775 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
776 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
777 return t)))
778 (when (and (not key) (not button))
779 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
780 (:motion-notify () t)
781 (:key-press () t)
782 (:key-release () t)
783 (:button-press () t)
784 (:button-release () t)
785 (t nil)))
786 (return))))))
789 (defun wait-a-key-or-button-press ()
790 (with-grab-keyboard-and-pointer (24 25 66 67)
791 (loop
792 (let ((key (loop for k across (xlib:query-keymap *display*)
793 unless (zerop k) return t))
794 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
795 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
796 return t)))
797 (when (or key button)
798 (return))))))
802 (defun compress-motion-notify ()
803 (when *have-to-compress-notify*
804 (loop while (xlib:event-cond (*display* :timeout 0)
805 (:motion-notify () t)))))
808 (defun display-all-cursors (&optional (display-time 1))
809 "Display all X11 cursors for display-time seconds"
810 (loop for i from 0 to 152 by 2
811 do (xgrab-pointer *root* i (1+ i))
812 (dbg i)
813 (sleep display-time)
814 (xungrab-pointer)))
819 ;;; Double buffering tools
820 (defun clear-pixmap-buffer (window gc)
821 (rotatef (xlib:gcontext-foreground gc) (xlib:gcontext-background gc))
822 (xlib:draw-rectangle *pixmap-buffer* gc
823 0 0 (xlib:drawable-width window) (xlib:drawable-height window)
825 (rotatef (xlib:gcontext-foreground gc) (xlib:gcontext-background gc)))
827 (defun copy-pixmap-buffer (window gc)
828 (xlib:copy-area *pixmap-buffer* gc
829 0 0 (xlib:drawable-width window) (xlib:drawable-height window)
830 window 0 0))
833 (defun is-a-key-pressed-p ()
834 (loop for k across (xlib:query-keymap *display*)
835 when (plusp k)
836 return t))
838 ;;; Windows wm class and name tests
839 (defmacro defun-equal-wm-class (symbol class)
840 `(defun ,symbol (window)
841 (when (xlib:window-p window)
842 (string-equal (xlib:get-wm-class window) ,class))))
844 (defmacro defun-equal-wm-name (symbol name)
845 `(defun ,symbol (window)
846 (when (xlib:window-p window)
847 (string-equal (xlib:wm-name window) ,name))))