Only windows with no transient windows participate in hidden children optimization
[clfswm.git] / src / xlib-util.lisp
blob3fef7cffa4bde15690fba91d57462266fc822753
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 (defparameter *x-error-count* 0)
73 (defparameter *max-x-error-count* 10000)
74 (defparameter *clfswm-x-error-filename* "/tmp/clfswm-backtrace.error")
77 (defmacro with-xlib-protect ((&optional name tag) &body body)
78 "Ignore Xlib errors in body."
79 `(handler-case
80 (with-simple-restart (top-level "Return to clfswm's top level")
81 ,@body)
82 (xlib::x-error (c)
83 (incf *x-error-count*)
84 (when (> *x-error-count* *max-x-error-count*)
85 (format t "Xlib error: ~A ~A: ~A~%" ,name (if ,tag ,tag ',body) c)
86 (force-output)
87 (write-backtrace *clfswm-x-error-filename*
88 (format nil "~%------- Additional information ---------
89 Xlib error: ~A ~A: ~A
90 Body: ~A
92 Features: ~A"
93 ,name ,tag c ',body
94 *features*))
95 (error "Too many X errors: ~A (logged in ~A)" c *clfswm-x-error-filename*))
96 #+:xlib-debug
97 (progn
98 (format t "Xlib error: ~A ~A: ~A~%" ,name (if ,tag ,tag ',body) c)
99 (force-output)))))
102 ;;(defmacro with-xlib-protect ((&optional name tag) &body body)
103 ;; `(progn
104 ;; ,@body))
108 (defmacro with-x-pointer (&body body)
109 "Bind (x y) to mouse pointer positions"
110 `(multiple-value-bind (x y)
111 (xlib:query-pointer *root*)
112 ,@body))
116 (declaim (inline window-x2 window-y2))
117 (defun window-x2 (window)
118 (+ (x-drawable-x window) (x-drawable-width window)))
120 (defun window-y2 (window)
121 (+ (x-drawable-y window) (x-drawable-height window)))
126 ;;; Events management functions.
128 (defparameter *unhandled-events* nil)
129 (defparameter *current-event-mode* nil)
131 (eval-when (:compile-toplevel :load-toplevel :execute)
132 (defun keyword->handle-event (mode keyword)
133 (create-symbol 'handle-event-fun "-" mode "-" keyword)))
135 (defun handle-event->keyword (symbol)
136 (let* ((name (string-downcase (symbol-name symbol)))
137 (pos (search "handle-event-fun-" name)))
138 (when (and pos (zerop pos))
139 (let ((pos-mod (search "mode" name)))
140 (when pos-mod
141 (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword))))))
142 ;; (values (intern (string-upcase (subseq name (+ pos-mod 5))) :keyword)
143 ;; (subseq name (length "handle-event-fun-") (1- pos-mod))))))))
145 (defparameter *handle-event-fun-symbols* nil)
147 (defun fill-handle-event-fun-symbols ()
148 (with-all-internal-symbols (symbol :clfswm)
149 (let ((pos (symbol-search "handle-event-fun-" symbol)))
150 (when (and pos (zerop pos))
151 (pushnew symbol *handle-event-fun-symbols*)))))
154 (defmacro with-handle-event-symbol ((mode) &body body)
155 "Bind symbol to all handle event functions available in mode"
156 `(let ((pattern (format nil "handle-event-fun-~A" ,mode)))
157 (dolist (symbol *handle-event-fun-symbols*)
158 (let ((pos (symbol-search pattern symbol)))
159 (when (and pos (zerop pos))
160 ,@body)))))
163 (defun find-handle-event-function (&optional (mode ""))
164 "Print all handle event functions available in mode"
165 (with-handle-event-symbol (mode)
166 (print symbol)))
168 (defun assoc-keyword-handle-event (mode)
169 "Associate all keywords in mode to their corresponding handle event functions.
170 For example: main-mode :key-press is bound to handle-event-fun-main-mode-key-press"
171 (setf *current-event-mode* mode)
172 (with-handle-event-symbol (mode)
173 (let ((keyword (handle-event->keyword symbol)))
174 (when (fboundp symbol)
175 #+:event-debug
176 (progn
177 (format t "~&Associating: ~S with ~S~%" symbol keyword)
178 (force-output))
179 (setf (symbol-function keyword) (symbol-function symbol))))))
181 (defun unassoc-keyword-handle-event (&optional (mode ""))
182 "Unbound all keywords from their corresponding handle event functions."
183 (setf *current-event-mode* nil)
184 (with-handle-event-symbol (mode)
185 (let ((keyword (handle-event->keyword symbol)))
186 (when (fboundp keyword)
187 #+:event-debug
188 (progn
189 (format t "~&Unassociating: ~S ~S~%" symbol keyword)
190 (force-output))
191 (fmakunbound keyword)))))
193 (defmacro define-handler (mode keyword args &body body)
194 "Like a defun but with a name expanded as handle-event-fun-'mode'-'keyword'
195 For example (define-handler main-mode :key-press (args) ...)
196 Expand in handle-event-fun-main-mode-key-press"
197 `(defun ,(keyword->handle-event mode keyword) (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
198 (declare (ignorable event-slots))
199 #+:event-debug (print (list *current-event-mode* event-key))
200 ,@body))
203 (defun event-hook-name (event-keyword)
204 (create-symbol-in-package :clfswm '*event- event-keyword '-hook*))
206 (let ((event-hook-list nil))
207 (defun get-event-hook-list ()
208 event-hook-list)
210 (defmacro use-event-hook (event-keyword)
211 (let ((symb (event-hook-name event-keyword)))
212 (pushnew symb event-hook-list)
213 `(defvar ,symb nil)))
215 (defun unuse-event-hook (event-keyword)
216 (let ((symb (event-hook-name event-keyword)))
217 (setf event-hook-list (remove symb event-hook-list))
218 (makunbound symb)))
220 (defmacro add-event-hook (name &rest value)
221 (let ((symb (event-hook-name name)))
222 `(add-hook ,symb ,@value)))
224 (defmacro remove-event-hook (name &rest value)
225 (let ((symb (event-hook-name name)))
226 `(remove-hook ,symb ,@value)))
228 (defun clear-event-hooks ()
229 (dolist (symb event-hook-list)
230 (makunbound symb)))
233 (defun optimize-event-hook ()
234 "Remove unused event hooks"
235 (dolist (symb event-hook-list)
236 (when (and (boundp symb)
237 (null (symbol-value symb)))
238 (makunbound symb)
239 (setf event-hook-list (remove symb event-hook-list))))))
243 (defmacro define-event-hook (event-keyword args &body body)
244 (let ((event-fun (gensym)))
245 `(let ((,event-fun (lambda (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
246 (declare (ignorable event-slots))
247 #+:event-debug (print (list ,event-keyword event-key))
248 ,@body)))
249 (add-event-hook ,event-keyword ,event-fun)
250 ,event-fun)))
253 (defmacro event-defun (name args &body body)
254 `(defun ,name (&rest event-slots &key #+:event-debug event-key ,@args &allow-other-keys)
255 (declare (ignorable event-slots))
256 #+:event-debug (print (list ,event-keyword event-key))
257 ,@body))
259 (defun exit-handle-event ()
260 (throw 'exit-handle-event nil))
263 (defun handle-event (&rest event-slots &key event-key &allow-other-keys)
264 (labels ((make-xlib-window (xobject)
265 "For some reason the clx xid cache screws up returns pixmaps when
266 they should be windows. So use this function to make a window out of them."
267 ;; Workaround for pixmap error taken from STUMPWM - thanks:
268 ;; XXX: In both the clisp and sbcl clx libraries, sometimes what
269 ;; should be a window will be a pixmap instead. In this case, we
270 ;; need to manually translate it to a window to avoid breakage
271 ;; in stumpwm. So far the only slot that seems to be affected is
272 ;; the :window slot for configure-request and reparent-notify
273 ;; events. It appears as though the hash table of XIDs and clx
274 ;; structures gets out of sync with X or perhaps X assigns a
275 ;; duplicate ID for a pixmap and a window.
276 #+clisp (make-instance 'xlib:window :id (slot-value xobject 'xlib::id) :display *display*)
277 #+(or sbcl ecl openmcl) (xlib::make-window :id (slot-value xobject 'xlib::id) :display *display*)
278 #-(or sbcl clisp ecl openmcl)
279 (error 'not-implemented)))
280 (handler-case
281 (catch 'exit-handle-event
282 (let ((win (getf event-slots :window)))
283 (when (and win (not (xlib:window-p win)))
284 (dbg "Pixmap Workaround! Should be a window: " win)
285 (setf (getf event-slots :window) (make-xlib-window win))))
286 (let ((hook-symbol (event-hook-name event-key)))
287 (when (boundp hook-symbol)
288 (apply #'call-hook (symbol-value hook-symbol) event-slots)))
289 (if (fboundp event-key)
290 (apply event-key event-slots)
291 #+:event-debug (pushnew (list *current-event-mode* event-key) *unhandled-events* :test #'equal))
292 (xlib:display-finish-output *display*))
293 ((or xlib:window-error xlib:drawable-error) (c)
294 #-xlib-debug (declare (ignore c))
295 #+xlib-debug (format t "Ignore Xlib synchronous error: ~a~%" c)))
302 (defun parse-display-string (display)
303 "Parse an X11 DISPLAY string and return the host and display from it."
304 (let* ((colon (position #\: display))
305 (host (subseq display 0 colon))
306 (rest (subseq display (1+ colon)))
307 (dot (position #\. rest))
308 (num (parse-integer (subseq rest 0 dot))))
309 (values host num)))
312 ;;; Transparency support
313 (let ((opaque #xFFFFFFFF))
314 (defun window-transparency (window)
315 "Return the window transparency"
316 (float (/ (or (first (xlib:get-property window :_NET_WM_WINDOW_OPACITY)) opaque) opaque)))
318 (defun set-window-transparency (window value)
319 "Set the window transparency"
320 (when (numberp value)
321 (xlib:change-property window :_NET_WM_WINDOW_OPACITY
322 (list (max (min (round (* opaque (if (equal *transparent-background* t) value 1)))
323 opaque)
325 :cardinal 32)))
327 (defsetf window-transparency set-window-transparency))
331 (defun maxmin-size-equal-p (window)
332 (when (xlib:window-p window)
333 (let ((hints (xlib:wm-normal-hints window)))
334 (when hints
335 (let ((hint-x (xlib:wm-size-hints-x hints))
336 (hint-y (xlib:wm-size-hints-y hints))
337 (user-specified-position-p (xlib:wm-size-hints-user-specified-position-p hints))
338 (min-width (xlib:wm-size-hints-min-width hints))
339 (min-height (xlib:wm-size-hints-min-height hints))
340 (max-width (xlib:wm-size-hints-max-width hints))
341 (max-height (xlib:wm-size-hints-max-height hints)))
342 (and hint-x hint-y min-width max-width min-height max-height
343 user-specified-position-p
344 (= hint-x 0) (= hint-y 0)
345 (= min-width max-width)
346 (= min-height max-height)))))))
348 (defun maxmin-size-equal-window-in-tree ()
349 (dolist (win (xlib:query-tree *root*))
350 (when (maxmin-size-equal-p win)
351 (return win))))
356 (defun window-state (win)
357 "Get the state (iconic, normal, withdrawn) of a window."
358 (first (xlib:get-property win :WM_STATE)))
361 (defun set-window-state (win state)
362 "Set the state (iconic, normal, withdrawn) of a window."
363 (xlib:change-property win
364 :WM_STATE
365 (list state)
366 :WM_STATE
367 32))
369 (defsetf window-state set-window-state)
373 (defun window-hidden-p (window)
374 (eql (window-state window) +iconic-state+))
377 (defun window-transient-for (window)
378 (first (xlib:get-property window :WM_TRANSIENT_FOR)))
380 (defun window-leader (window)
381 (when window
382 (or (first (xlib:get-property window :WM_CLIENT_LEADER))
383 (let ((id (window-transient-for window)))
384 (when id
385 (window-leader id))))))
389 (defun unhide-window (window)
390 (when window
391 (when (window-hidden-p window)
392 (xlib:map-window window)
393 (setf (window-state window) +normal-state+
394 (xlib:window-event-mask window) *window-events*))))
397 (defun map-window (window)
398 (when window
399 (xlib:map-window window)))
402 (defun delete-window (window)
403 (send-client-message window :WM_PROTOCOLS
404 (xlib:intern-atom *display* :WM_DELETE_WINDOW)))
406 (defun destroy-window (window)
407 (xlib:kill-client *display* (xlib:window-id window)))
410 ;;(defconstant +exwm-atoms+
411 ;; (list "_NET_SUPPORTED" "_NET_CLIENT_LIST"
412 ;; "_NET_CLIENT_LIST_STACKING" "_NET_NUMBER_OF_DESKTOPS"
413 ;; "_NET_CURRENT_DESKTOP" "_NET_DESKTOP_GEOMETRY"
414 ;; "_NET_DESKTOP_VIEWPORT" "_NET_DESKTOP_NAMES"
415 ;; "_NET_ACTIVE_WINDOW" "_NET_WORKAREA"
416 ;; "_NET_SUPPORTING_WM_CHECK" "_NET_VIRTUAL_ROOTS"
417 ;; "_NET_DESKTOP_LAYOUT"
419 ;; "_NET_RESTACK_WINDOW" "_NET_REQUEST_FRAME_EXTENTS"
420 ;; "_NET_MOVERESIZE_WINDOW" "_NET_CLOSE_WINDOW"
421 ;; "_NET_WM_MOVERESIZE"
423 ;; "_NET_WM_SYNC_REQUEST" "_NET_WM_PING"
425 ;; "_NET_WM_NAME" "_NET_WM_VISIBLE_NAME"
426 ;; "_NET_WM_ICON_NAME" "_NET_WM_VISIBLE_ICON_NAME"
427 ;; "_NET_WM_DESKTOP" "_NET_WM_WINDOW_TYPE"
428 ;; "_NET_WM_STATE" "_NET_WM_STRUT"
429 ;; "_NET_WM_ICON_GEOMETRY" "_NET_WM_ICON"
430 ;; "_NET_WM_PID" "_NET_WM_HANDLED_ICONS"
431 ;; "_NET_WM_USER_TIME" "_NET_FRAME_EXTENTS"
432 ;; ;; "_NET_WM_MOVE_ACTIONS"
434 ;; "_NET_WM_WINDOW_TYPE_DESKTOP" "_NET_WM_STATE_MODAL"
435 ;; "_NET_WM_WINDOW_TYPE_DOCK" "_NET_WM_STATE_STICKY"
436 ;; "_NET_WM_WINDOW_TYPE_TOOLBAR" "_NET_WM_STATE_MAXIMIZED_VERT"
437 ;; "_NET_WM_WINDOW_TYPE_MENU" "_NET_WM_STATE_MAXIMIZED_HORZ"
438 ;; "_NET_WM_WINDOW_TYPE_UTILITY" "_NET_WM_STATE_SHADED"
439 ;; "_NET_WM_WINDOW_TYPE_SPLASH" "_NET_WM_STATE_SKIP_TASKBAR"
440 ;; "_NET_WM_WINDOW_TYPE_DIALOG" "_NET_WM_STATE_SKIP_PAGER"
441 ;; "_NET_WM_WINDOW_TYPE_NORMAL" "_NET_WM_STATE_HIDDEN"
442 ;; "_NET_WM_STATE_FULLSCREEN"
443 ;; "_NET_WM_STATE_ABOVE"
444 ;; "_NET_WM_STATE_BELOW"
445 ;; "_NET_WM_STATE_DEMANDS_ATTENTION"
447 ;; "_NET_WM_ALLOWED_ACTIONS"
448 ;; "_NET_WM_ACTION_MOVE"
449 ;; "_NET_WM_ACTION_RESIZE"
450 ;; "_NET_WM_ACTION_SHADE"
451 ;; "_NET_WM_ACTION_STICK"
452 ;; "_NET_WM_ACTION_MAXIMIZE_HORZ"
453 ;; "_NET_WM_ACTION_MAXIMIZE_VERT"
454 ;; "_NET_WM_ACTION_FULLSCREEN"
455 ;; "_NET_WM_ACTION_CHANGE_DESKTOP"
456 ;; "_NET_WM_ACTION_CLOSE"
458 ;; ))
461 ;;(defun intern-atoms (display)
462 ;; (declare (type xlib:display display))
463 ;; (mapcar #'(lambda (atom-name) (xlib:intern-atom display atom-name))
464 ;; +exwm-atoms+)
465 ;; (values))
469 ;;(defun get-atoms-property (window property-atom atom-list-p)
470 ;; "Returns a list of atom-name (if atom-list-p is t) otherwise returns
471 ;; a list of atom-id."
472 ;; (xlib:get-property window property-atom
473 ;; :transform (when atom-list-p
474 ;; (lambda (id)
475 ;; (xlib:atom-name (xlib:drawable-display window) id)))))
477 ;;(defun set-atoms-property (window atoms property-atom &key (mode :replace))
478 ;; "Sets the property designates by `property-atom'. ATOMS is a list of atom-id
479 ;; or a list of keyword atom-names."
480 ;; (xlib:change-property window property-atom atoms :ATOM 32
481 ;; :mode mode
482 ;; :transform (unless (integerp (car atoms))
483 ;; (lambda (atom-key)
484 ;; (xlib:find-atom (xlib:drawable-display window) atom-key)))))
489 ;;(defun net-wm-state (window)
490 ;; (get-atoms-property window :_NET_WM_STATE t))
492 ;;(defsetf net-wm-state (window &key (mode :replace)) (states)
493 ;; `(set-atoms-property ,window ,states :_NET_WM_STATE :mode ,mode))
496 (defun hide-window (window)
497 (when window
498 (setf (window-state window) +iconic-state+
499 (xlib:window-event-mask window) (remove :structure-notify *window-events*))
500 (xlib:unmap-window window)
501 (setf (xlib:window-event-mask window) *window-events*)))
505 (defun window-type (window)
506 "Return one of :desktop, :dock, :toolbar, :utility, :splash,
507 :dialog, :transient, :maxsize and :normal."
508 (or (and (let ((hints (xlib:wm-normal-hints window)))
509 (and hints (or (and (xlib:wm-size-hints-max-width hints)
510 (< (xlib:wm-size-hints-max-width hints) (x-drawable-width *root*)))
511 (and (xlib:wm-size-hints-max-height hints)
512 (< (xlib:wm-size-hints-max-height hints) (x-drawable-height *root*)))
513 (xlib:wm-size-hints-min-aspect hints)
514 (xlib:wm-size-hints-max-aspect hints))))
515 :maxsize)
516 (let ((net-wm-window-type (xlib:get-property window :_NET_WM_WINDOW_TYPE)))
517 (when net-wm-window-type
518 (dolist (type-atom net-wm-window-type)
519 (when (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)
520 (return (cdr (assoc (xlib:atom-name *display* type-atom) +netwm-window-types+)))))))
521 (and (xlib:get-property window :WM_TRANSIENT_FOR)
522 :transient)
523 :normal))
528 ;;; Stolen from Eclipse
529 (defun send-configuration-notify (window x y w h bw)
530 "Send a synthetic configure notify event to the given window (ICCCM 4.1.5)"
531 (xlib:send-event window :configure-notify (xlib:make-event-mask :structure-notify)
532 :event-window window
533 :window window
534 :x x :y y
535 :width w
536 :height h
537 :border-width bw
538 :propagate-p nil))
542 (defun send-client-message (window type &rest data)
543 "Send a client message to a client's window."
544 (xlib:send-event window
545 :client-message nil
546 :window window
547 :type type
548 :format 32
549 :data data))
555 (defun raise-window (window)
556 "Map the window if needed and bring it to the top of the stack. Does not affect focus."
557 (when (xlib:window-p window)
558 (when (window-hidden-p window)
559 (unhide-window window))
560 (setf (xlib:window-priority window) :above)))
563 (defun no-focus ()
564 "don't focus any window but still read keyboard events."
565 (xlib:set-input-focus *display* *no-focus-window* :pointer-root))
567 (defun focus-window (window)
568 "Give the window focus."
569 (no-focus)
570 (when (xlib:window-p window)
571 (xlib:set-input-focus *display* window :parent)))
573 (defun raise-and-focus-window (window)
574 "Raise and focus."
575 (raise-window window)
576 (focus-window window))
579 (defun lower-window (window sibling)
580 "Map the window if needed and bring it just above sibling. Does not affect focus."
581 (when (xlib:window-p window)
582 (when (window-hidden-p window)
583 (unhide-window window))
584 (setf (xlib:window-priority window sibling) :below)))
589 (let ((cursor-font nil)
590 (cursor nil)
591 (pointer-grabbed nil))
592 (defun free-grab-pointer ()
593 (when cursor
594 (xlib:free-cursor cursor)
595 (setf cursor nil))
596 (when cursor-font
597 (xlib:close-font cursor-font)
598 (setf cursor-font nil)))
600 (defun xgrab-init-pointer ()
601 (setf pointer-grabbed nil))
603 (defun xgrab-pointer-p ()
604 pointer-grabbed)
606 (defun xgrab-pointer (root cursor-char cursor-mask-char
607 &optional (pointer-mask '(:enter-window :pointer-motion
608 :button-press :button-release)) owner-p)
609 "Grab the pointer and set the pointer shape."
610 (when pointer-grabbed
611 (xungrab-pointer))
612 (setf pointer-grabbed t)
613 (let* ((white (xlib:make-color :red 1.0 :green 1.0 :blue 1.0))
614 (black (xlib:make-color :red 0.0 :green 0.0 :blue 0.0)))
615 (cond (cursor-char
616 (setf cursor-font (xlib:open-font *display* "cursor")
617 cursor (xlib:create-glyph-cursor :source-font cursor-font
618 :source-char (or cursor-char 68)
619 :mask-font cursor-font
620 :mask-char (or cursor-mask-char 69)
621 :foreground black
622 :background white))
623 (xlib:grab-pointer root pointer-mask
624 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil :cursor cursor))
626 (xlib:grab-pointer root pointer-mask
627 :owner-p owner-p :sync-keyboard-p nil :sync-pointer-p nil)))))
629 (defun xungrab-pointer ()
630 "Remove the grab on the cursor and restore the cursor shape."
631 (setf pointer-grabbed nil)
632 (xlib:ungrab-pointer *display*)
633 (xlib:display-finish-output *display*)
634 (free-grab-pointer)))
637 (let ((keyboard-grabbed nil))
638 (defun xgrab-init-keyboard ()
639 (setf keyboard-grabbed nil))
641 (defun xgrab-keyboard-p ()
642 keyboard-grabbed)
644 (defun xgrab-keyboard (root)
645 (setf keyboard-grabbed t)
646 (xlib:grab-keyboard root :owner-p nil :sync-keyboard-p nil :sync-pointer-p nil))
649 (defun xungrab-keyboard ()
650 (setf keyboard-grabbed nil)
651 (xlib:ungrab-keyboard *display*)))
658 (defun ungrab-all-buttons (window)
659 (xlib:ungrab-button window :any :modifiers :any))
661 (defun grab-all-buttons (window)
662 (ungrab-all-buttons window)
663 (xlib:grab-button window :any '(:button-press :button-release :pointer-motion)
664 :modifiers :any
665 :owner-p nil
666 :sync-pointer-p t
667 :sync-keyboard-p nil))
669 (defun ungrab-all-keys (window)
670 (xlib:ungrab-key window :any :modifiers :any))
674 (defmacro with-grab-keyboard-and-pointer ((cursor mask old-cursor old-mask &optional ungrab-main) &body body)
675 `(let ((pointer-grabbed (xgrab-pointer-p))
676 (keyboard-grabbed (xgrab-keyboard-p)))
677 (xgrab-pointer *root* ,cursor ,mask)
678 (unless keyboard-grabbed
679 (when ,ungrab-main
680 (ungrab-main-keys))
681 (xgrab-keyboard *root*))
682 (unwind-protect
683 (progn
684 ,@body)
685 (progn
686 (if pointer-grabbed
687 (xgrab-pointer *root* ,old-cursor ,old-mask)
688 (xungrab-pointer))
689 (unless keyboard-grabbed
690 (when ,ungrab-main
691 (grab-main-keys))
692 (xungrab-keyboard))))))
695 (defmacro with-grab-pointer ((&optional cursor-char cursor-mask-char) &body body)
696 `(let ((pointer-grabbed-p (xgrab-pointer-p)))
697 (unless pointer-grabbed-p
698 (xgrab-pointer *root* ,cursor-char ,cursor-mask-char))
699 (unwind-protect
700 (progn
701 ,@body)
702 (unless pointer-grabbed-p
703 (xungrab-pointer)))))
709 (defun stop-button-event ()
710 (xlib:allow-events *display* :sync-pointer))
712 (defun replay-button-event ()
713 (xlib:allow-events *display* :replay-pointer))
723 ;;; Mouse action on window
724 (let (add-fn add-arg dx dy window)
725 (define-handler move-window-mode :motion-notify (root-x root-y)
726 (unless (compress-motion-notify)
727 (if add-fn
728 (multiple-value-bind (move-x move-y)
729 (apply add-fn add-arg)
730 (when move-x
731 (setf (x-drawable-x window) (+ root-x dx)))
732 (when move-y
733 (setf (x-drawable-y window) (+ root-y dy))))
734 (setf (x-drawable-x window) (+ root-x dx)
735 (x-drawable-y window) (+ root-y dy)))))
737 (define-handler move-window-mode :key-release ()
738 (throw 'exit-move-window-mode nil))
740 (define-handler move-window-mode :button-release ()
741 (throw 'exit-move-window-mode nil))
743 (defun move-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
744 (setf window orig-window
745 add-fn additional-fn
746 add-arg additional-arg
747 dx (- (x-drawable-x window) orig-x)
748 dy (- (x-drawable-y window) orig-y)
749 (xlib:window-border window) (get-color *color-move-window*))
750 (raise-window window)
751 (with-grab-pointer ()
752 (when additional-fn
753 (apply additional-fn additional-arg))
754 (generic-mode 'move-window-mode 'exit-move-window-mode
755 :original-mode '(main-mode)))))
758 (let (add-fn add-arg window
759 o-x o-y
760 orig-width orig-height
761 min-width max-width
762 min-height max-height)
763 (define-handler resize-window-mode :motion-notify (root-x root-y)
764 (unless (compress-motion-notify)
765 (if add-fn
766 (multiple-value-bind (resize-w resize-h)
767 (apply add-fn add-arg)
768 (when resize-w
769 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)))
770 (when resize-h
771 (setf (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height))))
772 (setf (x-drawable-width window) (min (max (+ orig-width (- root-x o-x)) 10 min-width) max-width)
773 (x-drawable-height window) (min (max (+ orig-height (- root-y o-y)) 10 min-height) max-height)))))
775 (define-handler resize-window-mode :key-release ()
776 (throw 'exit-resize-window-mode nil))
778 (define-handler resize-window-mode :button-release ()
779 (throw 'exit-resize-window-mode nil))
781 (defun resize-window (orig-window orig-x orig-y &optional additional-fn additional-arg)
782 (let* ((hints (xlib:wm-normal-hints orig-window)))
783 (setf window orig-window
784 add-fn additional-fn
785 add-arg additional-arg
786 o-x orig-x
787 o-y orig-y
788 orig-width (x-drawable-width window)
789 orig-height (x-drawable-height window)
790 min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0)
791 min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0)
792 max-width (or (and hints (xlib:wm-size-hints-max-width hints)) most-positive-fixnum)
793 max-height (or (and hints (xlib:wm-size-hints-max-height hints)) most-positive-fixnum)
794 (xlib:window-border window) (get-color *color-move-window*))
795 (raise-window window)
796 (with-grab-pointer ()
797 (when additional-fn
798 (apply additional-fn additional-arg))
799 (generic-mode 'resize-window-mode 'exit-resize-window-mode
800 :original-mode '(main-mode))))))
804 (define-handler wait-mouse-button-release-mode :button-release ()
805 (throw 'exit-wait-mouse-button-release-mode nil))
807 (defun wait-mouse-button-release (&optional cursor-char cursor-mask-char)
808 (with-grab-pointer (cursor-char cursor-mask-char)
809 (generic-mode 'wait-mouse-button-release 'exit-wait-mouse-button-release-mode)))
815 (let ((color-hash (make-hash-table :test 'equal)))
816 (defun get-color (color)
817 (multiple-value-bind (val foundp)
818 (gethash color color-hash)
819 (if foundp
821 (setf (gethash color color-hash)
822 (xlib:alloc-color (xlib:screen-default-colormap *screen*) color))))))
826 (defgeneric ->color (color))
828 (defmethod ->color ((color-name string))
829 color-name)
831 (defmethod ->color ((color integer))
832 (labels ((hex->float (color)
833 (/ (logand color #xFF) 256.0)))
834 (xlib:make-color :blue (hex->float color)
835 :green (hex->float (ash color -8))
836 :red (hex->float (ash color -16)))))
838 (defmethod ->color ((color list))
839 (destructuring-bind (red green blue) color
840 (xlib:make-color :blue red :green green :red blue)))
842 (defmethod ->color ((color xlib:color))
843 color)
845 (defmethod ->color (color)
846 (format t "Wrong color type: ~A~%" color)
847 "White")
850 (defun color->rgb (color)
851 (multiple-value-bind (r g b)
852 (xlib:color-rgb color)
853 (+ (ash (round (* 256 r)) +16)
854 (ash (round (* 256 g)) +8)
855 (round (* 256 b)))))
861 (defmacro my-character->keysyms (ch)
862 "Convert a char to a keysym"
863 ;; XLIB:CHARACTER->KEYSYMS should probably be implemented in NEW-CLX
864 ;; some day. Or just copied from MIT-CLX or some other CLX
865 ;; implementation (see translate.lisp and keysyms.lisp). For now,
866 ;; we do like this. It suffices for modifiers and ASCII symbols.
867 (if (fboundp 'xlib:character->keysyms)
868 `(xlib:character->keysyms ,ch)
869 `(list
870 (case ,ch
871 (:character-set-switch #xFF7E)
872 (:left-shift #xFFE1)
873 (:right-shift #xFFE2)
874 (:left-control #xFFE3)
875 (:right-control #xFFE4)
876 (:caps-lock #xFFE5)
877 (:shift-lock #xFFE6)
878 (:left-meta #xFFE7)
879 (:right-meta #xFFE8)
880 (:left-alt #xFFE9)
881 (:right-alt #xFFEA)
882 (:left-super #xFFEB)
883 (:right-super #xFFEC)
884 (:left-hyper #xFFED)
885 (:right-hyper #xFFEE)
887 (etypecase ,ch
888 (character
889 ;; Latin-1 characters have their own value as keysym
890 (if (< 31 (char-code ,ch) 256)
891 (char-code ,ch)
892 (error "Don't know how to get keysym from ~A" ,ch)))))))))
897 (defun char->keycode (char)
898 "Convert a character to a keycode"
899 (xlib:keysym->keycodes *display* (first (my-character->keysyms char))))
902 (defun keycode->char (code state)
903 (xlib:keysym->character *display* (xlib:keycode->keysym *display* code 0) state))
905 (defun modifiers->state (modifier-list)
906 (apply #'xlib:make-state-mask modifier-list))
908 (defun state->modifiers (state)
909 (xlib:make-state-keys state))
911 (defun keycode->keysym (code modifiers)
912 (xlib:keycode->keysym *display* code (cond ((member :shift modifiers) 1)
913 ((member :mod-5 modifiers) 4)
914 (t 0))))
920 (let ((modifier-list nil))
921 (defun init-modifier-list ()
922 (dolist (name '("Shift_L" "Shift_R" "Control_L" "Control_R"
923 "Alt_L" "Alt_R" "Meta_L" "Meta_R" "Hyper_L" "Hyper_R"
924 "Mode_switch" "script_switch" "ISO_Level3_Shift"
925 "Caps_Lock" "Scroll_Lock" "Num_Lock"))
926 (awhen (xlib:keysym->keycodes *display* (keysym-name->keysym name))
927 (push it modifier-list))))
929 (defun modifier-p (code)
930 (member code modifier-list)))
932 (defun wait-no-key-or-button-press ()
933 (with-grab-keyboard-and-pointer (66 67 66 67)
934 (loop
935 (let ((key (loop for k across (xlib:query-keymap *display*)
936 for code from 0
937 when (and (plusp k) (not (modifier-p code)))
938 return t))
939 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
940 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
941 return t)))
942 (when (and (not key) (not button))
943 (loop while (xlib:event-case (*display* :discard-p t :peek-p nil :timeout 0)
944 (:motion-notify () t)
945 (:key-press () t)
946 (:key-release () t)
947 (:button-press () t)
948 (:button-release () t)
949 (t nil)))
950 (return))))))
953 (defun wait-a-key-or-button-press ()
954 (with-grab-keyboard-and-pointer (24 25 66 67)
955 (loop
956 (let ((key (loop for k across (xlib:query-keymap *display*)
957 unless (zerop k) return t))
958 (button (loop for b in (xlib:make-state-keys (nth-value 4 (xlib:query-pointer *root*)))
959 when (member b '(:button-1 :button-2 :button-3 :button-4 :button-5))
960 return t)))
961 (when (or key button)
962 (return))))))
966 (defun compress-motion-notify ()
967 (when *have-to-compress-notify*
968 (loop while (xlib:event-cond (*display* :timeout 0)
969 (:motion-notify () t)))))
972 (defun display-all-cursors (&optional (display-time 1))
973 "Display all X11 cursors for display-time seconds"
974 (loop for i from 0 to 152 by 2
975 do (xgrab-pointer *root* i (1+ i))
976 (dbg i)
977 (sleep display-time)
978 (xungrab-pointer)))
982 ;;; Double buffering tools
983 (defun clear-pixmap-buffer (window gc)
984 (if (equal *transparent-background* :pseudo)
985 (xlib:copy-area *background-image* *background-gc*
986 (x-drawable-x window) (x-drawable-y window)
987 (x-drawable-width window) (x-drawable-height window)
988 *pixmap-buffer* 0 0)
989 (xlib:with-gcontext (gc :foreground (xlib:gcontext-background gc)
990 :background (xlib:gcontext-foreground gc))
991 (xlib:draw-rectangle *pixmap-buffer* gc
992 0 0 (x-drawable-width window) (x-drawable-height window)
993 t))))
996 (defun copy-pixmap-buffer (window gc)
997 (xlib:copy-area *pixmap-buffer* gc
998 0 0 (x-drawable-width window) (x-drawable-height window)
999 window 0 0))
1003 (defun is-a-key-pressed-p ()
1004 (loop for k across (xlib:query-keymap *display*)
1005 when (plusp k)
1006 return t))
1008 ;;; Windows wm class and name tests
1009 (defmacro defun-equal-wm-class (symbol class)
1010 `(defun ,symbol (window)
1011 (when (xlib:window-p window)
1012 (string-equal (xlib:get-wm-class window) ,class))))
1014 (defmacro defun-equal-wm-name (symbol name)
1015 `(defun ,symbol (window)
1016 (when (xlib:window-p window)
1017 (string-equal (xlib:wm-name window) ,name))))