src/*.lisp: Remove uneeded with-xlib-protect.
[clfswm.git] / src / xlib-util.lisp
blobfa6f1e30612ff2227bff1c55c7f111fc01e89a27
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
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 ;;; --------------------------------------------------------------------------
27 (in-package :clfswm)
30 ;; Window states
31 (defconstant +withdrawn-state+ 0)
32 (defconstant +normal-state+ 1)
33 (defconstant +iconic-state+ 3)
36 (defparameter *window-events* '(:structure-notify
37 :property-change
38 :colormap-change
39 :focus-change
40 :enter-window
41 :leave-window
42 :pointer-motion
43 :exposure)
44 "The events to listen for on managed windows.")
47 (defparameter +netwm-supported+
48 '(:_NET_SUPPORTING_WM_CHECK
49 :_NET_NUMBER_OF_DESKTOPS
50 :_NET_DESKTOP_GEOMETRY
51 :_NET_DESKTOP_VIEWPORT
52 :_NET_CURRENT_DESKTOP
53 :_NET_WM_WINDOW_TYPE
54 :_NET_CLIENT_LIST)
55 "Supported NETWM properties.
56 Window types are in +WINDOW-TYPES+.")
58 (defparameter +netwm-window-types+
59 '((:_NET_WM_WINDOW_TYPE_DESKTOP . :desktop)
60 (:_NET_WM_WINDOW_TYPE_DOCK . :dock)
61 (:_NET_WM_WINDOW_TYPE_TOOLBAR . :toolbar)
62 (:_NET_WM_WINDOW_TYPE_MENU . :menu)
63 (:_NET_WM_WINDOW_TYPE_UTILITY . :utility)
64 (:_NET_WM_WINDOW_TYPE_SPLASH . :splash)
65 (:_NET_WM_WINDOW_TYPE_DIALOG . :dialog)
66 (:_NET_WM_WINDOW_TYPE_NORMAL . :normal))
67 "Alist mapping NETWM window types to keywords.")
72 (defmacro with-xlib-protect ((&optional name tag) &body body)
73 "Prevent Xlib errors"
74 #-:xlib-debug (declare (ignore name tag))
75 `(handler-case
76 (with-simple-restart (top-level "Return to clfswm's top level")
77 ,@body)
78 (xlib::x-error (c)
79 #-:xlib-debug (declare (ignore c))
80 #+:xlib-debug
81 (progn
82 (if ,tag
83 (format t "Xlib error: ~A ~A: ~A~%" ,name ,tag c)
84 (format t "Xlib error: ~A ~A: ~A~%" ,name ',body c))
85 (force-output)))))
90 (defmacro with-x-pointer (&body body)
91 "Bind (x y) to mouse pointer positions"
92 `(multiple-value-bind (x y)
93 (xlib:query-pointer *root*)
94 ,@body))
98 (declaim (inline window-x2 window-y2))
99 (defun window-x2 (window)
100 (+ (x-drawable-x window) (x-drawable-width window)))
102 (defun window-y2 (window)
103 (+ (x-drawable-y window) (x-drawable-height window)))
108 ;;; Events management functions.
110 (defparameter *unhandled-events* nil)
111 (defparameter *current-event-mode* nil)
113 (eval-when (:compile-toplevel :load-toplevel :execute)
114 (defun keyword->handle-event (mode keyword)
115 (create-symbol 'handle-event-fun "-" mode "-" keyword)))
117 (defun handle-event->keyword (symbol)
118 (let* ((name (string-downcase (symbol-name symbol)))
119 (pos (search "handle-event-fun-" name)))
120 (when (and pos (zerop pos))
121 (let ((pos-mod (search "mode" name)))
122 (when pos-mod
123 (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword))))))
124 ;; (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
125 ;; (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
127 (defparameter *handle-event-fun-symbols* nil)
129 (defun fill-handle-event-fun-symbols ()
130 (with-all-internal-symbols (symbol :clfswm)
131 (let ((pos (symbol-search "handle-event-fun-" symbol)))
132 (when (and pos (zerop pos))
133 (pushnew symbol *handle-event-fun-symbols*)))))
136 (defmacro with-handle-event-symbol ((mode) &body body)
137 "Bind symbol to all handle event functions available in mode"
138 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
139 (dolist (symbol *handle-event-fun-symbols*)
140 (let ((pos (symbol-search pattern symbol)))
141 (when (and pos (zerop pos))
142 ,@body)))))
145 (defun find-handle-event-function (&optional (mode ""))
146 "Print all handle event functions available in mode"
147 (with-handle-event-symbol (mode)
148 (print symbol)))
150 (defun assoc-keyword-handle-event (mode)
151 "Associate all keywords in mode to their corresponding handle event functions.
152 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
153 (setf *current-event-mode* mode)
154 (with-handle-event-symbol (mode)
155 (let ((keyword (handle-event->keyword symbol)))
156 (when (fboundp symbol)
157 #+:event-debug
158 (progn
159 (format t "~&Associating: ~S with ~S~%" symbol keyword)
160 (force-output))
161 (setf (symbol-function keyword) (symbol-function symbol))))))
163 (defun unassoc-keyword-handle-event (&optional (mode ""))
164 "Unbound all keywords from their corresponding handle event functions."
165 (setf *current-event-mode* nil)
166 (with-handle-event-symbol (mode)
167 (let ((keyword (handle-event->keyword symbol)))
168 (when (fboundp keyword)
169 #+:event-debug
170 (progn
171 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
172 (force-output))
173 (fmakunbound keyword)))))
175 (defmacro define-handler (mode keyword args &body body)
176 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
177 For example (define-handler main-mode :key-press (args) ...)
178 Expand in handle-event-fun-main-mode-key-press"
179 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
180 (declare (ignorable event-slots))
181 #+:event-debug (print (list *current-event-mode* event-key))
182 ,@body))
185 (defun event-hook-name (event-keyword)
186 (create-symbol-in-package :clfswm '*event- event-keyword '-hook*))
188 (let ((event-hook-list nil))
189 (defun get-event-hook-list ()
190 event-hook-list)
192 (defmacro use-event-hook (event-keyword)
193 (let ((symb (event-hook-name event-keyword)))
194 (pushnew symb event-hook-list)
195 `(defvar ,symb nil)))
197 (defun unuse-event-hook (event-keyword)
198 (let ((symb (event-hook-name event-keyword)))
199 (setf event-hook-list (remove symb event-hook-list))
200 (makunbound symb)))
202 (defmacro add-event-hook (name &rest value)
203 (let ((symb (event-hook-name name)))
204 `(add-hook ,symb ,@value)))
206 (defmacro remove-event-hook (name &rest value)
207 (let ((symb (event-hook-name name)))
208 `(remove-hook ,symb ,@value)))
210 (defun clear-event-hooks ()
211 (dolist (symb event-hook-list)
212 (makunbound symb)))
215 (defun optimize-event-hook ()
216 "Remove unused event hooks"
217 (dolist (symb event-hook-list)
218 (when (and (boundp symb)
219 (null (symbol-value symb)))
220 (makunbound symb)
221 (setf event-hook-list (remove symb event-hook-list))))))
225 (defmacro define-event-hook (event-keyword args &body body)
226 (let ((event-fun (gensym)))
227 `(let ((,event-fun (lambda (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
228 (declare (ignorable event-slots))
229 #+:event-debug (print (list ,event-keyword event-key))
230 ,@body)))
231 (add-event-hook ,event-keyword ,event-fun)
232 ,event-fun)))
235 (defmacro event-defun (name args &body body)
236 `(defun ,name (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
237 (declare (ignorable event-slots))
238 #+:event-debug (print (list ,event-keyword event-key))
239 ,@body))
241 (defun exit-handle-event ()
242 (throw 'exit-handle-event nil))
245 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
246 (labels ((make-xlib-window (xobject)
247 "For some reason the clx xid cache screws up returns pixmaps when
248 they should be windows. So use this function to make a window out of them."
249 ;; Workaround for pixmap error taken from STUMPWM - thanks:
250 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
251 ;; should be a window will be a pixmap instead. In this case, we
252 ;; need to manually translate it to a window to avoid breakage
253 ;; in stumpwm. So far the only slot that seems to be affected is
254 ;; the :window slot for configure-request and reparent-notify
255 ;; events. It appears as though the hash table of XIDs and clx
256 ;; structures gets out of sync with X or perhaps X assigns a
257 ;; duplicate ID for a pixmap and a window.
258 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
259 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
260 #-(or sbcl clisp ecl openmcl)
261 (error 'not-implemented)))
262 (catch 'exit-handle-event
263 (let ((win (getf event-slots :window)))
264 (when (and win (not (xlib:window-p win)))
265 (dbg "Pixmap Workaround! Should be a window: " win)
266 (setf (getf event-slots :window) (make-xlib-window win))))
267 (let ((hook-symbol (event-hook-name event-key)))
268 (when (boundp hook-symbol)
269 (apply #'call-hook (symbol-value hook-symbol) event-slots)))
270 (if (fboundp event-key)
271 (apply event-key event-slots)
272 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal))
273 (xlib:display-finish-output *display*))
280 (defun parse-display-string (display)
281 "Parse an X11 DISPLAY string and return the host and display from it."
282 (let* ((colon (position #\: display))
283 (host (subseq display 0 colon))
284 (rest (subseq display (1+ colon)))
285 (dot (position #\. rest))
286 (num (parse-integer (subseq rest 0 dot))))
287 (values host num)))
290 ;;; Transparency support
291 (let ((opaque #xFFFFFFFF))
292 (defun window-transparency (window)
293 "Return the window transparency"
294 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
296 (defun set-window-transparency (window value)
297 "Set the window transparency"
298 (when (numberp value)
299 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
300 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
301 opaque)
303 :cardinal 32)))
305 (defsetf window-transparency set-window-transparency))
309 (defun window-state (win)
310 "Get the state (iconic, normal, withdrawn) of a window."
311 (first (xlib:get-property win :WM_STATE)))
314 (defun set-window-state (win state)
315 "Set the state (iconic, normal, withdrawn) of a window."
316 (xlib:change-property win
317 :WM_STATE
318 (list state)
319 :WM_STATE
320 32))
322 (defsetf window-state set-window-state)
326 (defun window-hidden-p (window)
327 (eql (window-state window) +iconic-state+))
330 (defun null-size-window-p (window)
331 (let ((hints (xlib:wm-normal-hints window)))
332 (and hints
333 (not (or (xlib:wm-size-hints-width hints)
334 (xlib:wm-size-hints-height hints)
335 (xlib:wm-size-hints-win-gravity hints)))
336 (xlib:wm-size-hints-user-specified-position-p hints))))
343 (defun unhide-window (window)
344 (when window
345 (when (window-hidden-p window)
346 (xlib:map-window window)
347 (setf (window-state window) +normal-state+
348 (xlib:window-event-mask window) *window-events*))))
351 (defun map-window (window)
352 (when window
353 (xlib:map-window window)))
356 (defun delete-window (window)
357 (send-client-message window :WM_PROTOCOLS
358 (xlib:intern-atom *display* "WM_DELETE_WINDOW")))
360 (defun destroy-window (window)
361 (xlib:kill-client *display* (xlib:window-id window)))
364 ;;(defconstant +exwm-atoms+
365 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
366 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
367 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
368 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
369 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
370 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
371 ;; "_NET_DESKTOP_LAYOUT"
373 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
374 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
375 ;; "_NET_WM_MOVERESIZE"
377 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
379 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
380 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
381 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
382 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
383 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
384 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
385 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
386 ;; ;; "_NET_WM_MOVE_ACTIONS"
388 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
389 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
390 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
391 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
392 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
393 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
394 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
395 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
396 ;; "_NET_WM_STATE_FULLSCREEN"
397 ;; "_NET_WM_STATE_ABOVE"
398 ;; "_NET_WM_STATE_BELOW"
399 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
401 ;; "_NET_WM_ALLOWED_ACTIONS"
402 ;; "_NET_WM_ACTION_MOVE"
403 ;; "_NET_WM_ACTION_RESIZE"
404 ;; "_NET_WM_ACTION_SHADE"
405 ;; "_NET_WM_ACTION_STICK"
406 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
407 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
408 ;; "_NET_WM_ACTION_FULLSCREEN"
409 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
410 ;; "_NET_WM_ACTION_CLOSE"
412 ;; ))
415 ;;(defun intern-atoms (display)
416 ;; (declare (type xlib:display display))
417 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
418 ;; +exwm-atoms+)
419 ;; (values))
423 ;;(defun get-atoms-property (window property-atom atom-list-p)
424 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
425 ;; a list of atom-id."
426 ;; (xlib:get-property window property-atom
427 ;; :transform (when atom-list-p
428 ;; (lambda (id)
429 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
431 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
432 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
433 ;; or a list of keyword atom-names."
434 ;; (xlib:change-property window property-atom atoms :ATOM 32
435 ;; :mode mode
436 ;; :transform (unless (integerp (car atoms))
437 ;; (lambda (atom-key)
438 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
443 ;;(defun net-wm-state (window)
444 ;; (get-atoms-property window :_NET_WM_STATE t))
446 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
447 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
450 (defun hide-window (window)
451 (when window
452 (setf (window-state window) +iconic-state+
453 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
454 (xlib:unmap-window window)
455 (setf (xlib:window-event-mask window) *window-events*)))
459 (defun window-type (window)
460 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
461 :dialog, :transient, :maxsize and :normal."
462 (or (and (let ((hints (xlib:wm-normal-hints window)))
463 (and hints (or (xlib:wm-size-hints-max-width hints)
464 (xlib:wm-size-hints-max-height hints)
465 (xlib:wm-size-hints-min-aspect hints)
466 (xlib:wm-size-hints-max-aspect hints))))
467 :maxsize)
468 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
469 (when net-wm-window-type
470 (dolist (type-atom net-wm-window-type)
471 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
472 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
473 (and (xlib:get-property window :WM_TRANSIENT_FOR)
474 :transient)
475 :normal))
480 ;;; Stolen from Eclipse
481 (defun send-configuration-notify (window x y w h bw)
482 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
483 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
484 :event-window window
485 :window window
486 :x x :y y
487 :width w
488 :height h
489 :border-width bw
490 :propagate-p nil))
494 (defun send-client-message (window type &rest data)
495 "Send a client message to a client's window."
496 (xlib:send-event window
497 :client-message nil
498 :window window
499 :type type
500 :format 32
501 :data data))
507 (defun raise-window (window)
508 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
509 (when (xlib:window-p window)
510 (when (window-hidden-p window)
511 (unhide-window window))
512 (setf (xlib:window-priority window) :above)))
514 (defun focus-window (window)
515 "Give the window focus."
516 (when (xlib:window-p window)
517 (xlib:set-input-focus *display* window :parent)))
519 (defun raise-and-focus-window (window)
520 "Raise and focus."
521 (raise-window window)
522 (focus-window window))
524 (defun no-focus ()
525 "don't focus any window but still read keyboard events."
526 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
529 (defun lower-window (window sibling)
530 "Map the window if needed and bring it just above sibling. Does not affect focus."
531 (when (xlib:window-p window)
532 (when (window-hidden-p window)
533 (unhide-window window))
534 (setf (xlib:window-priority window sibling) :below)))
539 (let ((cursor-font nil)
540 (cursor nil)
541 (pointer-grabbed nil))
542 (defun free-grab-pointer ()
543 (when cursor
544 (xlib:free-cursor cursor)
545 (setf cursor nil))
546 (when cursor-font
547 (xlib:close-font cursor-font)
548 (setf cursor-font nil)))
550 (defun xgrab-init-pointer ()
551 (setf pointer-grabbed nil))
553 (defun xgrab-pointer-p ()
554 pointer-grabbed)
556 (defun xgrab-pointer (root cursor-char cursor-mask-char
557 &optional (pointer-mask '(:enter-window :pointer-motion
558 :button-press :button-release)) owner-p)
559 "Grab the pointer and set the pointer shape."
560 (when pointer-grabbed
561 (xungrab-pointer))
562 (setf pointer-grabbed t)
563 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
564 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
565 (cond (cursor-char
566 (setf cursor-font (xlib:open-font *display* "cursor")
567 cursor (xlib:create-glyph-cursor :source-font cursor-font
568 :source-char (or cursor-char 68)
569 :mask-font cursor-font
570 :mask-char (or cursor-mask-char 69)
571 :foreground black
572 :background white))
573 (xlib:grab-pointer root pointer-mask
574 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
576 (xlib:grab-pointer root pointer-mask
577 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
579 (defun xungrab-pointer ()
580 "Remove the grab on the cursor and restore the cursor shape."
581 (setf pointer-grabbed nil)
582 (xlib:ungrab-pointer *display*)
583 (xlib:display-finish-output *display*)
584 (free-grab-pointer)))
587 (let ((keyboard-grabbed nil))
588 (defun xgrab-init-keyboard ()
589 (setf keyboard-grabbed nil))
591 (defun xgrab-keyboard-p ()
592 keyboard-grabbed)
594 (defun xgrab-keyboard (root)
595 (setf keyboard-grabbed t)
596 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
599 (defun xungrab-keyboard ()
600 (setf keyboard-grabbed nil)
601 (xlib:ungrab-keyboard *display*)))
608 (defun ungrab-all-buttons (window)
609 (xlib:ungrab-button window :any :modifiers :any))
611 (defun grab-all-buttons (window)
612 (ungrab-all-buttons window)
613 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
614 :modifiers :any
615 :owner-p nil
616 :sync-pointer-p t
617 :sync-keyboard-p nil))
619 (defun ungrab-all-keys (window)
620 (xlib:ungrab-key window :any :modifiers :any))
624 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask &optional ungrab-main) &body body)
625 `(let ((pointer-grabbed (xgrab-pointer-p))
626 (keyboard-grabbed (xgrab-keyboard-p)))
627 (xgrab-pointer *root* ,cursor ,mask)
628 (unless keyboard-grabbed
629 (when ,ungrab-main
630 (ungrab-main-keys))
631 (xgrab-keyboard *root*))
632 (unwind-protect
633 (progn
634 ,@body)
635 (progn
636 (if pointer-grabbed
637 (xgrab-pointer *root* ,old-cursor ,old-mask)
638 (xungrab-pointer))
639 (unless keyboard-grabbed
640 (when ,ungrab-main
641 (grab-main-keys))
642 (xungrab-keyboard))))))
645 (defmacro with-grab-pointer ((&optional cursor-char cursor-mask-char) &body body)
646 `(let ((pointer-grabbed-p (xgrab-pointer-p)))
647 (unless pointer-grabbed-p
648 (xgrab-pointer *root* ,cursor-char ,cursor-mask-char))
649 (unwind-protect
650 (progn
651 ,@body)
652 (unless pointer-grabbed-p
653 (xungrab-pointer)))))
659 (defun stop-button-event ()
660 (xlib:allow-events *display* :sync-pointer))
662 (defun replay-button-event ()
663 (xlib:allow-events *display* :replay-pointer))
673 ;;; Mouse action on window
674 (let (add-fn add-arg dx dy window)
675 (define-handler move-window-mode :motion-notify (root-x root-y)
676 (unless (compress-motion-notify)
677 (if add-fn
678 (multiple-value-bind (move-x move-y)
679 (apply add-fn add-arg)
680 (when move-x
681 (setf (x-drawable-x window) (+ root-x dx)))
682 (when move-y
683 (setf (x-drawable-y window) (+ root-y dy))))
684 (setf (x-drawable-x window) (+ root-x dx)
685 (x-drawable-y window) (+ root-y dy)))))
687 (define-handler move-window-mode :key-release ()
688 (throw 'exit-move-window-mode nil))
690 (define-handler move-window-mode :button-release ()
691 (throw 'exit-move-window-mode nil))
693 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
694 (setf window orig-window
695 add-fn additional-fn
696 add-arg additional-arg
697 dx (- (x-drawable-x window) orig-x)
698 dy (- (x-drawable-y window) orig-y)
699 (xlib:window-border window) (get-color *color-move-window*))
700 (raise-window window)
701 (with-grab-pointer ()
702 (when additional-fn
703 (apply additional-fn additional-arg))
704 (generic-mode 'move-window-mode 'exit-move-window-mode
705 :original-mode '(main-mode)))))
708 (let (add-fn add-arg window
709 o-x o-y
710 orig-width orig-height
711 min-width max-width
712 min-height max-height)
713 (define-handler resize-window-mode :motion-notify (root-x root-y)
714 (unless (compress-motion-notify)
715 (if add-fn
716 (multiple-value-bind (resize-w resize-h)
717 (apply add-fn add-arg)
718 (when resize-w
719 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
720 (when resize-h
721 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
722 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
723 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
725 (define-handler resize-window-mode :key-release ()
726 (throw 'exit-resize-window-mode nil))
728 (define-handler resize-window-mode :button-release ()
729 (throw 'exit-resize-window-mode nil))
731 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
732 (let* ((hints (xlib:wm-normal-hints orig-window)))
733 (setf window orig-window
734 add-fn additional-fn
735 add-arg additional-arg
736 o-x orig-x
737 o-y orig-y
738 orig-width (x-drawable-width window)
739 orig-height (x-drawable-height window)
740 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
741 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
742 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
743 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
744 (xlib:window-border window) (get-color *color-move-window*))
745 (raise-window window)
746 (with-grab-pointer ()
747 (when additional-fn
748 (apply additional-fn additional-arg))
749 (generic-mode 'resize-window-mode 'exit-resize-window-mode
750 :original-mode '(main-mode))))))
754 (define-handler wait-mouse-button-release-mode :button-release ()
755 (throw 'exit-wait-mouse-button-release-mode nil))
757 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
758 (with-grab-pointer (cursor-char cursor-mask-char)
759 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)))
765 (let ((color-hash (make-hash-table :test 'equal)))
766 (defun get-color (color)
767 (multiple-value-bind (val foundp)
768 (gethash color color-hash)
769 (if foundp
771 (setf (gethash color color-hash)
772 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
776 (defgeneric ->color (color))
778 (defmethod ->color ((color-name string))
779 color-name)
781 (defmethod ->color ((color integer))
782 (labels ((hex->float (color)
783 (/ (logand color #xFF) 256.0)))
784 (xlib:make-color :blue (hex->float color)
785 :green (hex->float (ash color -8))
786 :red (hex->float (ash color -16)))))
788 (defmethod ->color ((color list))
789 (destructuring-bind (red green blue) color
790 (xlib:make-color :blue red :green green :red blue)))
792 (defmethod ->color ((color xlib:color))
793 color)
795 (defmethod ->color (color)
796 (format t "Wrong color type: ~A~%" color)
797 "White")
800 (defun color->rgb (color)
801 (multiple-value-bind (r g b)
802 (xlib:color-rgb color)
803 (+ (ash (round (* 256 r)) +16)
804 (ash (round (* 256 g)) +8)
805 (round (* 256 b)))))
811 (defmacro my-character->keysyms (ch)
812 "Convert a char to a keysym"
813 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
814 ;; some day. Or just copied from MIT-CLX or some other CLX
815 ;; implementation (see translate.lisp and keysyms.lisp). For now,
816 ;; we do like this. It suffices for modifiers and ASCII symbols.
817 (if (fboundp 'xlib:character->keysyms)
818 `(xlib:character->keysyms ,ch)
819 `(list
820 (case ,ch
821 (:character-set-switch #xFF7E)
822 (:left-shift #xFFE1)
823 (:right-shift #xFFE2)
824 (:left-control #xFFE3)
825 (:right-control #xFFE4)
826 (:caps-lock #xFFE5)
827 (:shift-lock #xFFE6)
828 (:left-meta #xFFE7)
829 (:right-meta #xFFE8)
830 (:left-alt #xFFE9)
831 (:right-alt #xFFEA)
832 (:left-super #xFFEB)
833 (:right-super #xFFEC)
834 (:left-hyper #xFFED)
835 (:right-hyper #xFFEE)
837 (etypecase ,ch
838 (character
839 ;; Latin-1 characters have their own value as keysym
840 (if (< 31 (char-code ,ch) 256)
841 (char-code ,ch)
842 (error "Don't know how to get keysym from ~A" ,ch)))))))))
847 (defun char->keycode (char)
848 "Convert a character to a keycode"
849 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
852 (defun keycode->char (code state)
853 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
855 (defun modifiers->state (modifier-list)
856 (apply #'xlib:make-state-mask modifier-list))
858 (defun state->modifiers (state)
859 (xlib:make-state-keys state))
861 (defun keycode->keysym (code modifiers)
862 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
863 ((member :mod-5 modifiers) 4)
864 (t 0))))
870 (let ((modifier-list nil))
871 (defun init-modifier-list ()
872 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
873 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
874 "Mode_switch" "script_switch" "ISO_Level3_Shift"
875 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
876 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
877 (push it modifier-list))))
879 (defun modifier-p (code)
880 (member code modifier-list)))
882 (defun wait-no-key-or-button-press ()
883 (with-grab-keyboard-and-pointer (66 67 66 67)
884 (loop
885 (let ((key (loop for k across (xlib:query-keymap *display*)
886 for code from 0
887 when (and (plusp k) (not (modifier-p code)))
888 return t))
889 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
890 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
891 return t)))
892 (when (and (not key) (not button))
893 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
894 (:motion-notify () t)
895 (:key-press () t)
896 (:key-release () t)
897 (:button-press () t)
898 (:button-release () t)
899 (t nil)))
900 (return))))))
903 (defun wait-a-key-or-button-press ()
904 (with-grab-keyboard-and-pointer (24 25 66 67)
905 (loop
906 (let ((key (loop for k across (xlib:query-keymap *display*)
907 unless (zerop k) return t))
908 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
909 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
910 return t)))
911 (when (or key button)
912 (return))))))
916 (defun compress-motion-notify ()
917 (when *have-to-compress-notify*
918 (loop while (xlib:event-cond (*display* :timeout 0)
919 (:motion-notify () t)))))
922 (defun display-all-cursors (&optional (display-time 1))
923 "Display all X11 cursors for display-time seconds"
924 (loop for i from 0 to 152 by 2
925 do (xgrab-pointer *root* i (1+ i))
926 (dbg i)
927 (sleep display-time)
928 (xungrab-pointer)))
932 ;;; Double buffering tools
933 (defun clear-pixmap-buffer (window gc)
934 (if (equal *transparent-background* :pseudo)
935 (xlib:copy-area *background-image* *background-gc*
936 (x-drawable-x window) (x-drawable-y window)
937 (x-drawable-width window) (x-drawable-height window)
938 *pixmap-buffer* 0 0)
939 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
940 :background (xlib:gcontext-foreground gc))
941 (xlib:draw-rectangle *pixmap-buffer* gc
942 0 0 (x-drawable-width window) (x-drawable-height window)
943 t))))
946 (defun copy-pixmap-buffer (window gc)
947 (xlib:copy-area *pixmap-buffer* gc
948 0 0 (x-drawable-width window) (x-drawable-height window)
949 window 0 0))
953 (defun is-a-key-pressed-p ()
954 (loop for k across (xlib:query-keymap *display*)
955 when (plusp k)
956 return t))
958 ;;; Windows wm class and name tests
959 (defmacro defun-equal-wm-class (symbol class)
960 `(defun ,symbol (window)
961 (when (xlib:window-p window)
962 (string-equal (xlib:get-wm-class window) ,class))))
964 (defmacro defun-equal-wm-name (symbol name)
965 `(defun ,symbol (window)
966 (when (xlib:window-p window)
967 (string-equal (xlib:wm-name window) ,name))))