Compare

c63d3797b6dff30e6508a9ab6b23f1e7bc55262a..a82c185dd941b4fbb68478608c1e0d2ee47b2125

clwm-ext: ewmh-maximize-mixin

Raymond E. Pasco <ray@ameretat.dev> · 2026-10-28 01:53 UTC

Browse files
25 files changed, +1901 −1
modified .gitignore
@@ -1,3 +1,4 @@
1 + /clwm
1 2 *~
2 3 \#*\#
3 4 *.fasl
added binary.lisp mode 100644
@@ -0,0 +1,47 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; build a binary (assumes you have quicklisp):
4 + ;;;; sbcl --non-interactive --load binary.lisp
5 + ;;;; usage:
6 + ;;;; clwm user-init.lisp
7 +
8 + (unless (find-package '#:quicklisp)
9 + (load
10 + (merge-pathnames "quicklisp/setup.lisp" (user-homedir-pathname))))
11 +
12 + (asdf:load-asd (merge-pathnames "clwm.asd" *load-truename*))
13 + (ql:quickload "clwm")
14 + ;; get the stuff slynk tries to load on connect into the image
15 + (asdf:load-systems "slynk/arglists" "slynk/fancy-inspector"
16 + "slynk/package-fu" "slynk/mrepl" "slynk/trace-dialog"
17 + "slynk/profiler" "slynk/stickers" "slynk/indentation"
18 + "slynk/retro")
19 +
20 + (in-package #:clwm-user)
21 +
22 + (defun main ()
23 + (let ((args (uiop:command-line-arguments)))
24 + (unless (= (length args) 1)
25 + (format *error-output* "usage: clwm user-init.lisp~%")
26 + (uiop:quit 2))
27 + (handler-case
28 + (let ((*package* (find-package '#:clwm-user)))
29 + (load (first args)))
30 + (error (condition)
31 + (format *error-output* "~&clwm: error loading ~a:~%~a~%"
32 + (first args) condition)
33 + (uiop:quit 1)))
34 + (unless *wm-thread*
35 + (format *error-output* "~&clwm: ~a didn't start a WM~%"
36 + (first args))
37 + (uiop:quit 1))
38 + (uiop:quit (if (member (ignore-errors (bt:join-thread *wm-thread*))
39 + '(:stopped :disconnected))
40 + 0
41 + 1))))
42 +
43 + (sb-ext:save-lisp-and-die
44 + (make-pathname :name "clwm" :type nil :defaults *load-truename*)
45 + :executable t
46 + :toplevel #'main
47 + :save-runtime-options t)
added build.sh mode 100755
@@ -0,0 +1,2 @@
1 + #!/bin/sh
2 + sbcl --non-interactive --load binary.lisp
added clwm-ext.lisp mode 100644
@@ -0,0 +1,67 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; package for provided extensions
4 +
5 + (defpackage #:clwm-ext
6 + (:use :cl :clwm)
7 + (:import-from #:alexandria
8 + #:when-let #:lastcar #:format-symbol #:clamp #:appendf
9 + #:removef #:extremum #:hash-table-keys)
10 + (:export
11 + ;; sloppy-focus-follows-mouse-mixin
12 + #:sloppy-focus-follows-mouse-mixin
13 + ;; focus-tracking-mixin
14 + #:focus-tracking-mixin #:focused-client
15 + ;; ignored-clients-mixin
16 + #:ignored-clients-mixin #:ignore-window-p #:ignored-window-names
17 + ;; interaction mixin
18 + #:interaction-mixin #:current-interaction #:interaction
19 + #:interaction-client #:start-interaction #:stop-interaction
20 + #:interaction-event #:stop-current-interaction
21 + #:abort-current-interaction #:with-interaction-abort-on-error
22 + ;; bindings
23 + #:*lock-modifiers* #:*key-names*
24 + ;; keybinds-mixin
25 + #:keybinds-mixin #:key-bindings #:grab-keys #:call-key-command
26 + ;; pointer-keybinds-mixin
27 + #:pointer-keybinds-mixin
28 + ;; pointer-keybinds respecting commands
29 + #:spawn-command #:close-command #:hide-command
30 + #:raise-command #:lower-command
31 + ;; mousebinds-mixin
32 + #:mousebinds-mixin #:mouse-bindings #:grab-buttons #:drag
33 + #:drag-function
34 + ;; mousebinds respecting commands
35 + #:drag-move #:drag-resize
36 + ;; warp-pointer-mixin
37 + #:warp-pointer-mixin #:warp-pointer-client #:client-pointer
38 + ;; alt-tab-ring-mixin
39 + #:alt-tab-ring-mixin #:client-ring #:ring-cycle
40 + #:cycle-forward #:cycle-backward
41 + ;; screen-geometry-mixin
42 + #:screen-geometry-mixin #:screen-areas #:reserved-edges
43 + #:screen-geometry-changed #:area-at #:work-area
44 + #:geometry-bounds #:constrain-move #:constrain-resize
45 + ;; screen-geometry using commands
46 + #:bounded-drag-move #:bounded-drag-resize
47 + ;; pointer-placement-mixin
48 + #:pointer-placement-mixin
49 + ;; maximize-mixin
50 + #:maximize-mixin #:maximizable-client #:client-maximized
51 + #:maximized-axes #:toggle-maximize #:maximize-state-changed
52 + ;; maximize-mixin using commands
53 + #:maximize-command #:vmaximize-command #:hmaximize-command
54 + ;; drawing helpers
55 + #:color #:drawable-text
56 + ;; modal-commands-mixin
57 + #:modal-commands-mixin
58 + #:modal-window #:modal-window-window #:modal-x #:modal-y
59 + #:draw-modal #:modal-key-press #:modal-button-press
60 + #:menu #:menu-prompt #:menu-items #:menu-input #:menu-selection
61 + #:menu-matches #:run-menu
62 + #:menu-font-name #:menu-colors
63 + #:exec-menu-command #:window-menu-command
64 + ;; ewmh-mixin
65 + #:ewmh-mixin #:ewmh-supported
66 + ;; ewmh-maximize-mixin
67 + #:ewmh-maximize-mixin))
added clwm-ext/alt-tab-ring-mixin.lisp mode 100644
@@ -0,0 +1,146 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; most-recently-used alt-tab ring
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass alt-tab-ring-mixin (interaction-mixin)
8 + ((ring :initform '() :accessor client-ring
9 + :documentation "Most-recently-used list of clients."))
10 + (:documentation "Store clients in an alt-tab ring."))
11 +
12 + (defmethod manage-client :after ((wm alt-tab-ring-mixin) client)
13 + (appendf (client-ring wm) (list client)))
14 +
15 + (defmethod focus-client :after ((wm alt-tab-ring-mixin) client)
16 + (setf (client-ring wm)
17 + (cons client (remove client (client-ring wm)))))
18 +
19 + (defclass ring-cycle (interaction)
20 + ((candidates :initarg :candidates :accessor cycle-candidates
21 + :documentation "Candidate clients for alt-tabbing.")
22 + (index :initform 0 :accessor cycle-index
23 + :documentation "The position of the selected candidate.")
24 + (modifiers :initarg :modifiers :reader cycle-modifiers
25 + :documentation
26 + "The modifiers that must be held to continue cycling.")
27 + (key :initarg :key :reader cycle-key
28 + :documentation "The key that steps through the cycle."))
29 + (:documentation "An alt-tab interaction that cycles through the ring
30 + while a modifier is held, focusing the selected client when released."))
31 +
32 + (defun cycle-held-p (wm cycle)
33 + "Helper to check if the cycle modifiers are held."
34 + (let ((state (nth-value 4 (xlib:query-pointer (root wm))))
35 + (mask (cycle-modifiers cycle)))
36 + (and (plusp mask)
37 + (= mask (logand state mask)))))
38 +
39 + (defun cycle-selection (cycle)
40 + "Helper to get the current candidate."
41 + (nth (cycle-index cycle) (cycle-candidates cycle)))
42 +
43 + (defun cycle-step (wm cycle direction)
44 + "Helper to select the next candidate and raise it."
45 + (let ((count (length (cycle-candidates cycle))))
46 + (when (plusp count)
47 + (setf (cycle-index cycle) (mod
48 + (+ (cycle-index cycle) direction)
49 + count))
50 + (configure-client wm
51 + (cycle-selection cycle)
52 + :stack-mode :above))))
53 +
54 + (defun cycle-clients (wm direction spec)
55 + "Initialize the cycle through clients in DIRECTION 1 or -1."
56 + (let ((candidates (remove :iconic (client-ring wm)
57 + :key (lambda (client)
58 + (wm-state (client-window client))))))
59 + (when (rest candidates)
60 + (let ((cycle
61 + (make-instance 'ring-cycle
62 + :candidates candidates
63 + :modifiers (binding-modifier-mask spec)
64 + :key (binding-key spec))))
65 + (start-interaction wm cycle)
66 + (cycle-step wm cycle direction)
67 + ;; exit right away if it wasn't held (quick alt-tab)
68 + (unless (cycle-held-p wm cycle)
69 + (stop-interaction wm cycle))))))
70 +
71 + (defun cycle-forward (&rest spec)
72 + "Alt-tab cycle forward. Takes a key binding used during the cycle,
73 + which may be different from the one used to invoke it if you have
74 + dextrous fingers.
75 + Expects ALT-TAB-RING-MIXIN.
76 + Respects POINTER-KEYBINDS-MIXIN convention."
77 + (lambda (wm &rest pointer)
78 + (declare (ignore pointer))
79 + (cycle-clients wm 1 spec)))
80 +
81 + (defun cycle-backward (&rest spec)
82 + "Alt-tab cycle backward. Takes a key binding used during the cycle,
83 + which may be different from the one used to invoke it if you have
84 + dextrous fingers.
85 + Expects ALT-TAB-RING-MIXIN.
86 + Respects POINTER-KEYBINDS-MIXIN convention."
87 + (lambda (wm &rest pointer)
88 + (declare (ignore pointer))
89 + (cycle-clients wm -1 spec)))
90 +
91 + (defmethod start-interaction :after
92 + ((wm interaction-mixin) (cycle ring-cycle))
93 + (unless (eql (xlib:grab-keyboard (root wm)) :success)
94 + (stop-interaction wm cycle)))
95 +
96 + (defmethod stop-interaction :after
97 + ((wm interaction-mixin) (cycle ring-cycle))
98 + (xlib:ungrab-keyboard (display wm))
99 + (when-let ((client (cycle-selection cycle)))
100 + (setf (cycle-candidates cycle) '())
101 + (focus-client wm client)))
102 +
103 + ;; remove a client that goes away while alt-tabbing from the ring
104 + (defmethod unmanage-client :after ((wm alt-tab-ring-mixin) client)
105 + (removef (client-ring wm) client)
106 + (let ((cycle (current-interaction wm)))
107 + (when (typep cycle 'ring-cycle)
108 + (let ((position (position client (cycle-candidates cycle))))
109 + (when position
110 + (removef (cycle-candidates cycle) client)
111 + (when (< position (cycle-index cycle))
112 + (decf (cycle-index cycle)))
113 + (when (>= (cycle-index cycle)
114 + (length(cycle-candidates cycle)))
115 + (setf (cycle-index cycle) 0)))))))
116 +
117 + ;; todo: more flexible swapping to reverse cycle than hardcoded shift
118 + (defmethod interaction-event
119 + ((wm interaction-mixin) (cycle ring-cycle)
120 + (event-key (eql :key-press)) &key code state)
121 + (let ((display (display wm)))
122 + (when (member (xlib:keycode->keysym display code 0)
123 + (key-keysyms (cycle-key cycle) display))
124 + (cycle-step wm cycle
125 + (if (logtest state (xlib:make-state-mask :shift))
126 + -1
127 + 1))))
128 + t)
129 +
130 + (defmethod interaction-event
131 + ((wm interaction-mixin) (cycle ring-cycle)
132 + (event-key (eql :key-release)) &key)
133 + (unless (cycle-held-p wm cycle)
134 + (stop-interaction wm cycle))
135 + t)
136 +
137 + ;; if we raise a client into the pointer while alt-tabbing, make sure
138 + ;; this doesn't focus it
139 + (defmethod interaction-event
140 + ((wm interaction-mixin) (cycle ring-cycle)
141 + (event-key (eql :enter-notify)) &key)
142 + t)
143 + (defmethod interaction-event
144 + ((wm interaction-mixin) (cycle ring-cycle)
145 + (event-key (eql :leave-notify)) &key)
146 + t)
added clwm-ext/bindings.lisp mode 100644
@@ -0,0 +1,72 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; helpers for key and mouse bindings
4 +
5 + (in-package #:clwm-ext)
6 +
7 + ;; the overall binding shape is ('(modifiers... key) . function)
8 + ;; this depends on xmodmap and stuff, but what's probably the case is:
9 + ;; :control = ctrl
10 + ;; :shift = shift
11 + ;; :mod-1 = alt
12 + ;; :mod-2 = num lock (!)
13 + ;; :mod-4 = that additional modifier key
14 + ;; :lock = scroll lock
15 + ;; and we don't want scroll lock or num lock to affect keybinds.
16 + ;; this is a parameter not a gf because if it's not constant it's deep
17 + ;; in the user keyboard weeds.
18 + (defparameter *lock-modifiers* '(:lock :mod-2)
19 + "Modifiers that bindings should always ignore.")
20 +
21 + ;; name some keys that CLX doesn't name.
22 + (defparameter *key-names*
23 + (append '((:backspace . #xff08) (:tab . #xff09) (:return . #xff0d)
24 + (:pause . #xff13) (:escape . #xff1b) (:delete . #xffff)
25 + (:home . #xff50) (:left . #xff51) (:up . #xff52)
26 + (:right . #xff53) (:down . #xff54) (:page-up . #xff55)
27 + (:page-down . #xff56) (:end . #xff57) (:print . #xff61)
28 + (:insert . #xff63) (:menu . #xff67) (:space . #x20))
29 + (loop for n from 1 to 24
30 + collect (cons
31 + (format-symbol :keyword "F~d" n)
32 + (+ #xffbd n))))
33 + "Keyword names for keys we can't name with escapes.")
34 +
35 + (defun binding-modifier-mask (spec)
36 + "The X state mask for the modifiers in a binding spec."
37 + (apply #'xlib:make-state-mask (butlast spec)))
38 +
39 + (defun binding-key (spec)
40 + "The key part of a binding spec."
41 + (lastcar spec))
42 +
43 + (defun binding-grab-masks (spec)
44 + "The modifier masks to grab for a binding spec,
45 + ignoring *LOCK-MODIFIERS*."
46 + (reduce
47 + (lambda (masks modifier)
48 + (append masks
49 + (mapcar (lambda (mask)
50 + (logior mask (xlib:make-state-mask modifier)))
51 + masks)))
52 + *lock-modifiers*
53 + :initial-value (list (binding-modifier-mask spec))))
54 +
55 + (defun find-binding (bindings state key &key (test #'eql))
56 + "The command bound to a key under an event's modifier state."
57 + (let ((mask
58 + (logandc2 (logand state #xff)
59 + (apply #'xlib:make-state-mask *lock-modifiers*))))
60 + (cdr (find-if (lambda (spec)
61 + (and (funcall test key (binding-key spec))
62 + (= (binding-modifier-mask spec) mask)))
63 + bindings
64 + :key #'car))))
65 +
66 + (defun key-keysyms (key display)
67 + "Turn a key from a binding into a keysym on the display."
68 + (etypecase key
69 + (character (xlib:character->keysyms key display))
70 + (keyword (list (or (cdr (assoc key *key-names*))
71 + (error "No key named ~s in *KEY-NAMES*." key))))
72 + (integer (list key))))
added clwm-ext/commands.lisp mode 100644
@@ -0,0 +1,109 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; forms intended to be executed as wm commands
4 +
5 + (in-package #:clwm-ext)
6 +
7 + ;;; respecting POINTER-KEYBINDS-MIXIN convention
8 +
9 + (defun spawn-command (program &rest args)
10 + "Spawns a program.
11 + Respects POINTER-KEYBINDS-MIXIN convention."
12 + (lambda (wm &rest pointer)
13 + (declare (ignore pointer))
14 + (apply #'spawn wm program args)))
15 +
16 + (defun close-command (wm client x y)
17 + "Close the client under the pointer.
18 + Respects POINTER-KEYBINDS-MIXIN convention."
19 + (declare (ignore x y))
20 + (when client (close-client wm client)))
21 +
22 + (defun hide-command (wm client x y)
23 + "Hide the client under the pointer.
24 + Respects POINTER-KEYBINDS-MIXIN convention."
25 + (declare (ignore x y))
26 + (when client (hide-client wm client)))
27 +
28 + (defun raise-command (wm client x y)
29 + "Raise the client under the pointer.
30 + Respects POINTER-KEYBINDS-MIXIN convention."
31 + (declare (ignore x y))
32 + (when client (configure-client wm client :stack-mode :above)))
33 +
34 + (defun lower-command (wm client x y)
35 + "Lower the client under the pointer.
36 + Respects POINTER-KEYBINDS-MIXIN convention."
37 + (declare (ignore x y))
38 + (when client (configure-client wm client :stack-mode :below)))
39 +
40 + ;;; respecting MOUSEBINDS-MIXIN convention
41 +
42 + (defun grab-offset (pointer edge size)
43 + "Helper to determine where the pointer is along a client's edge."
44 + (clamp (- pointer edge) 0 (1- size)))
45 +
46 + (defun drag-move (wm client x y)
47 + "Raise the client and move it with the pointer.
48 + Respects MOUSEBINDS-MIXIN convention."
49 + (configure-client wm client :stack-mode :above)
50 + (multiple-value-bind (client-x client-y width height)
51 + (client-geometry wm client)
52 + (declare (ignore width height))
53 + (let ((dx (- client-x x))
54 + (dy (- client-y y)))
55 + (lambda (x y)
56 + (configure-client wm client :x (+ x dx) :y (+ y dy))))))
57 +
58 + (defun drag-resize (wm client x y)
59 + "Raise the client and resize it with the pointer.
60 + Respects MOUSEBINDS-MIXIN convention."
61 + (configure-client wm client :stack-mode :above)
62 + (multiple-value-bind (client-x client-y width height)
63 + (client-geometry wm client)
64 + (declare (ignore client-x client-y))
65 + ;; todo: resize from other corners than bottom right
66 + (lambda (new-x new-y)
67 + (configure-client wm client
68 + :width (+ width (- new-x x))
69 + :height (+ height (- new-y y))))))
70 +
71 + (defun bounded-drag-move (wm client x y)
72 + "Raise the client and move it with the pointer within the bounds.
73 + Expects SCREEN-GEOMETRY-MIXIN.
74 + Respects MOUSEBINDS-MIXIN convention."
75 + (configure-client wm client :stack-mode :above)
76 + (multiple-value-bind (client-x client-y width height)
77 + (client-geometry wm client)
78 + (let ((grab-x (- x client-x))
79 + (grab-y (- y client-y)))
80 + (lambda (x y)
81 + (multiple-value-bind (new-x new-y)
82 + (constrain-move wm client
83 + (- x grab-x) (- y grab-y)
84 + width height)
85 + (setf grab-x (grab-offset x new-x width)
86 + grab-y (grab-offset y new-y height))
87 + (configure-client wm client :x new-x :y new-y))))))
88 +
89 + (defun bounded-drag-resize (wm client x y)
90 + "Raise the client and resize it with the pointer within the bounds.
91 + Expects SCREEN-GEOMETRY-MIXIN.
92 + Respects MOUSEBINDS-MIXIN convention."
93 + (configure-client wm client :stack-mode :above)
94 + (multiple-value-bind (client-x client-y width height)
95 + (client-geometry wm client)
96 + ;; measured from the bottom right corner, which is what moves
97 + (let ((grab-x (- (+ client-x width) x))
98 + (grab-y (- (+ client-y height) y)))
99 + (lambda (x y)
100 + (multiple-value-bind (new-width new-height)
101 + (constrain-resize wm client client-x client-y
102 + (- (+ x grab-x) client-x)
103 + (- (+ y grab-y) client-y))
104 + (setf grab-x (grab-offset (+ client-x new-width)
105 + x new-width)
106 + grab-y (grab-offset (+ client-y new-height)
107 + y new-height))
108 + (configure-client wm client
109 + :width new-width :height new-height))))))
added clwm-ext/drawing.lisp mode 100644
@@ -0,0 +1,25 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; helpers for drawing things ourselves
4 + ;;;; todo: move more stuff here
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defun color (wm name)
9 + "A pixel for a named color, or a #RRGGBB code we parse ourselves."
10 + (let ((color (if (and (= (length name) 7) (char= (char name 0) #\#))
11 + (flet ((component (start)
12 + (/ (parse-integer name :start start
13 + :end (+ start 2)
14 + :radix 16)
15 + 255)))
16 + (xlib:make-color :red (component 1)
17 + :green (component 3)
18 + :blue (component 5)))
19 + name)))
20 + (xlib:alloc-color (xlib:screen-default-colormap (screen wm))
21 + color)))
22 +
23 + (defun drawable-text (string)
24 + "Make STRING safe for core X fonts."
25 + (substitute-if #\? (lambda (c) (>= (char-code c) 256)) string))
added clwm-ext/ewmh-maximize-mixin.lisp mode 100644
@@ -0,0 +1,87 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; ewmh maximize mixin
4 + ;;;; experimental way to have mixins interact
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defclass ewmh-maximize-mixin (ewmh-mixin maximize-mixin) ()
9 + (:documentation "Publish EWMH maximize states and maximize when
10 + requested by EWMH docks or the like."))
11 +
12 + (defparameter *maximize-states*
13 + '((:horizontal . :_NET_WM_STATE_MAXIMIZED_HORZ)
14 + (:vertical . :_NET_WM_STATE_MAXIMIZED_VERT))
15 + "Map our simple symbols to EWMH's long ones.")
16 +
17 + (defun axes-states (axes)
18 + "Helper to get the EWMH atoms for our states."
19 + (loop for (axis . atom) in *maximize-states*
20 + when (member axis axes) collect atom))
21 +
22 + (defun state-axes (atoms)
23 + "Helper to get our states from EWMH atoms."
24 + (loop for (axis . atom) in *maximize-states*
25 + when (member atom atoms) collect axis))
26 +
27 + (defun client-state-axes (client)
28 + "Helper to get the maximized state in _NET_WM_STATE."
29 + (state-axes (window-atoms (client-window client) :_NET_WM_STATE)))
30 +
31 + (defmethod ewmh-supported append ((wm ewmh-maximize-mixin))
32 + (axes-states '(:horizontal :vertical)))
33 +
34 + (defmethod maximize-state-changed :after
35 + ((wm ewmh-maximize-mixin) client)
36 + (update-window-atoms
37 + (client-window client) :_NET_WM_STATE
38 + (lambda (states)
39 + (append (set-difference states (axes-states '(:horizontal
40 + :vertical)))
41 + (axes-states (maximized-axes client))))))
42 +
43 + ;; hack to pick up already-maximized windows on startup
44 + ;; we don't know any restore geometry though
45 + (defvar *adopting* nil)
46 +
47 + (defun note-maximized (wm client axes)
48 + "Helper to note a client as maximized when we don't know any restore
49 + geometry, so we just use its current geometry."
50 + (multiple-value-call #'remember-axes client axes (client-geometry wm client))
51 + (when axes
52 + (maximize-state-changed wm client)))
53 +
54 + (defmethod manage-all-windows :around ((wm ewmh-maximize-mixin))
55 + (let ((*adopting* t))
56 + (call-next-method)))
57 +
58 + (defmethod manage-client :after ((wm ewmh-maximize-mixin) client)
59 + (when *adopting*
60 + (note-maximized wm client (client-state-axes client))))
61 +
62 + (defmethod place-client :after ((wm ewmh-maximize-mixin) client)
63 + (when-let ((axes (client-state-axes client)))
64 + (maximize-axes wm client axes)))
65 +
66 + (defmethod handle-client-event
67 + ((wm ewmh-maximize-mixin) client (event-key (eql :client-message))
68 + &key type data)
69 + (when (eql type :_NET_WM_STATE)
70 + (let* ((display (display wm))
71 + (axes (state-axes
72 + (loop for id across (subseq data 1 3)
73 + unless (zerop id)
74 + collect (xlib:atom-name display id))))
75 + (maximized (maximized-axes client)))
76 + (flet ((maximize (axes)
77 + (maximize-axes wm client
78 + (set-difference axes maximized)))
79 + (restore (axes)
80 + (restore-axes wm client
81 + (intersection axes maximized))))
82 + (case (aref data 0)
83 + (0 (restore axes))
84 + (1 (maximize axes))
85 + (2 (restore axes)
86 + (maximize axes))))))
87 + (call-next-method))
added clwm-ext/ewmh-mixin.lisp mode 100644
@@ -0,0 +1,278 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; EWMH support
4 + ;;;; todo: split into more mixins, don't hardcode 1 desktop
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defclass ewmh-mixin (screen-geometry-mixin
9 + ignored-clients-mixin
10 + focus-tracking-mixin)
11 + ((check-window :initform nil :accessor ewmh-check-window
12 + :documentation
13 + "_NET_SUPPORTING_WM_CHECK window.")
14 + (client-order :initform '() :accessor ewmh-client-order
15 + :documentation
16 + "Clients in the order they were managed.")
17 + (docks :initform '() :accessor ewmh-docks
18 + :documentation "Dock windows we're not managing."))
19 + (:documentation
20 + "Partial EWMH support for pagers, docks, taskbars, &c."))
21 +
22 + (defgeneric ewmh-supported (wm)
23 + (:documentation "The EWMH features to advertise support for.")
24 + (:method-combination append))
25 +
26 + (defmethod ewmh-supported append ((wm ewmh-mixin))
27 + '(:_NET_SUPPORTED :_NET_SUPPORTING_WM_CHECK :_NET_WM_NAME
28 + :_NET_CLIENT_LIST :_NET_CLIENT_LIST_STACKING :_NET_ACTIVE_WINDOW
29 + :_NET_NUMBER_OF_DESKTOPS :_NET_CURRENT_DESKTOP :_NET_DESKTOP_NAMES
30 + :_NET_DESKTOP_GEOMETRY :_NET_DESKTOP_VIEWPORT :_NET_WORKAREA
31 + :_NET_WM_DESKTOP :_NET_WM_STATE :_NET_WM_STATE_HIDDEN
32 + :_NET_CLOSE_WINDOW :_NET_WM_WINDOW_TYPE :_NET_WM_WINDOW_TYPE_DOCK
33 + :_NET_WM_STRUT :_NET_WM_STRUT_PARTIAL))
34 +
35 + ;; todo: check mixin-accuracy
36 + (defparameter *ewmh-root-properties*
37 + '(:_NET_SUPPORTED :_NET_SUPPORTING_WM_CHECK :_NET_CLIENT_LIST
38 + :_NET_CLIENT_LIST_STACKING :_NET_ACTIVE_WINDOW
39 + :_NET_NUMBER_OF_DESKTOPS :_NET_CURRENT_DESKTOP :_NET_DESKTOP_NAMES
40 + :_NET_DESKTOP_GEOMETRY :_NET_DESKTOP_VIEWPORT :_NET_WORKAREA)
41 + "Root properties to clear on exit.")
42 +
43 + (defun set-cardinals (window property values)
44 + "Helper to set window cardinals."
45 + (xlib:change-property window property values :CARDINAL 32))
46 +
47 + (defun set-windows (window property windows)
48 + "Helper to set window associated windows."
49 + (xlib:change-property window property
50 + (mapcar #'xlib:window-id windows)
51 + :WINDOW 32))
52 +
53 + ;; todo: move utf8 helpers to their own thing (fonts?)
54 + ;; todo: non sb-ext?
55 + (defun utf8-octets (string &key null-terminate)
56 + "Helper to coerce a string to UTF-8 octets."
57 + (coerce (sb-ext:string-to-octets string
58 + :external-format :utf-8
59 + :null-terminate null-terminate)
60 + 'list))
61 +
62 + (defun set-utf8-string (window property string)
63 + "Helper to set a property to a UTF-8 string."
64 + (xlib:change-property window property
65 + (utf8-octets string) :UTF8_STRING 8))
66 +
67 + (defun set-utf8-strings (window property strings)
68 + "Helper to set a property to a list of UTF-8 strings."
69 + (xlib:change-property window property
70 + (loop for string in strings
71 + append (utf8-octets string
72 + :null-terminate t))
73 + :UTF8_STRING 8))
74 +
75 + (defun window-atoms (window property)
76 + "Helper to get atom list properties."
77 + (let ((display (xlib:window-display window)))
78 + (mapcar (lambda (id) (xlib:atom-name display id))
79 + (xlib:get-property window property :type :ATOM))))
80 +
81 + (defun set-window-atoms (window property atoms)
82 + "Helper to set atom list properties."
83 + (let ((display (xlib:window-display window)))
84 + (xlib:change-property window property
85 + (mapcar (lambda (atom)
86 + (xlib:intern-atom display atom))
87 + atoms)
88 + :ATOM 32)))
89 +
90 + (defun update-window-atoms (window property function)
91 + "Call FUNCTION to update an atom list property."
92 + (set-window-atoms window property
93 + (funcall function (window-atoms window property))))
94 +
95 + (defun update-client-lists (wm)
96 + "Update the EWMH _NET_CLIENT_LIST and _NET_CLIENT_LIST_STACKING."
97 + (let ((stacking (loop for window in (xlib:query-tree (root wm))
98 + for client = (find-client wm window)
99 + when client collect (client-window client))))
100 + (set-windows (root wm) :_NET_CLIENT_LIST
101 + (mapcar #'client-window (ewmh-client-order wm)))
102 + (set-windows (root wm) :_NET_CLIENT_LIST_STACKING
103 + (remove-duplicates stacking :test #'xlib:window-equal
104 + :from-end t))))
105 +
106 + (defun update-active-window (wm)
107 + "Update _NET_ACTIVE_WINDOW with the focused client."
108 + (let ((client (focused-client wm)))
109 + (xlib:change-property (root wm) :_NET_ACTIVE_WINDOW
110 + (list
111 + (if client
112 + (xlib:window-id (client-window client))
113 + 0))
114 + :WINDOW 32)))
115 +
116 + (defmethod initialize-wm :after ((wm ewmh-mixin))
117 + (let* ((root (root wm))
118 + (screen (screen wm))
119 + (width (xlib:screen-width screen))
120 + (height (xlib:screen-height screen))
121 + (check (xlib:create-window :parent root :x -1 :y -1
122 + :width 1 :height 1
123 + :override-redirect :on)))
124 + (setf (ewmh-check-window wm) check)
125 + (set-windows check :_NET_SUPPORTING_WM_CHECK (list check))
126 + (set-utf8-string check :_NET_WM_NAME "clwm")
127 + (set-windows root :_NET_SUPPORTING_WM_CHECK (list check))
128 + (set-window-atoms root :_NET_SUPPORTED (ewmh-supported wm))
129 + (set-cardinals root :_NET_NUMBER_OF_DESKTOPS '(1))
130 + (set-cardinals root :_NET_CURRENT_DESKTOP '(0))
131 + (set-utf8-strings root :_NET_DESKTOP_NAMES '("1"))
132 + (set-cardinals root :_NET_DESKTOP_GEOMETRY (list width height))
133 + (set-cardinals root :_NET_DESKTOP_VIEWPORT '(0 0))
134 + (update-docks wm)
135 + (update-client-lists wm)
136 + (update-active-window wm)))
137 +
138 + (defun remove-ewmh (wm)
139 + "Helper to remove EWMH on uninitalize or shutdown."
140 + (when-let ((check (shiftf (ewmh-check-window wm) nil)))
141 + (dolist (property *ewmh-root-properties*)
142 + (xlib:delete-property (root wm) property))
143 + (xlib:destroy-window check)))
144 +
145 + (defmethod uninitialize-wm :after ((wm ewmh-mixin))
146 + (remove-ewmh wm))
147 +
148 + (defmethod shutdown-wm :before ((wm ewmh-mixin))
149 + (remove-ewmh wm))
150 +
151 + (defmethod manage-client :after ((wm ewmh-mixin) client)
152 + (appendf (ewmh-client-order wm) (list client))
153 + (set-cardinals (client-window client) :_NET_WM_DESKTOP '(0))
154 + (update-client-lists wm))
155 +
156 + (defmethod unmanage-client :after ((wm ewmh-mixin) client)
157 + (removef (ewmh-client-order wm) client)
158 + (update-client-lists wm))
159 +
160 + (defmethod withdraw-client :before ((wm ewmh-mixin) client)
161 + (xlib:delete-property (client-window client) :_NET_WM_STATE)
162 + (xlib:delete-property (client-window client) :_NET_WM_DESKTOP))
163 +
164 + (defmethod configure-client :after ((wm ewmh-mixin) client
165 + &key stack-mode &allow-other-keys)
166 + (when stack-mode
167 + (raise-docks wm)
168 + (update-client-lists wm)))
169 +
170 + (defmethod show-client :after ((wm ewmh-mixin) client)
171 + (raise-docks wm)
172 + (update-window-atoms (client-window client) :_NET_WM_STATE
173 + (lambda (states)
174 + (remove :_NET_WM_STATE_HIDDEN states))))
175 +
176 + (defmethod hide-client :after ((wm ewmh-mixin) client)
177 + (update-window-atoms (client-window client) :_NET_WM_STATE
178 + (lambda (states)
179 + (adjoin :_NET_WM_STATE_HIDDEN states))))
180 +
181 + (defmethod (setf focused-client) :after (client (wm ewmh-mixin))
182 + (declare (ignore client))
183 + (update-active-window wm))
184 +
185 + (defmethod handle-client-event
186 + ((wm ewmh-mixin) client (event-key (eql :client-message))
187 + &key type)
188 + (case type
189 + (:_NET_ACTIVE_WINDOW
190 + (show-client wm client)
191 + (configure-client wm client :stack-mode :above)
192 + (focus-client wm client))
193 + (:_NET_CLOSE_WINDOW
194 + (close-client wm client))
195 + (t (call-next-method))))
196 +
197 + (defun dock-p (window)
198 + "Helper to check if a window is a dock (if it vanishes before we can
199 + ask, say no)."
200 + (handler-case (member :_NET_WM_WINDOW_TYPE_DOCK
201 + (window-atoms window :_NET_WM_WINDOW_TYPE))
202 + (xlib:window-error () nil)))
203 +
204 + (defmethod ignore-window-p or ((wm ewmh-mixin) window)
205 + (dock-p window))
206 +
207 + (defun dock-strut (window)
208 + "Helper to get a dock window's reserved l/r/t/b strut, or NIL
209 + if we can't."
210 + (let ((strut (or (xlib:get-property window :_NET_WM_STRUT_PARTIAL
211 + :type :CARDINAL)
212 + (xlib:get-property window :_NET_WM_STRUT
213 + :type :CARDINAL))))
214 + (and (>= (length strut) 4)
215 + (subseq strut 0 4))))
216 +
217 + (defmethod reserved-edges append ((wm ewmh-mixin))
218 + (remove nil (mapcar #'dock-strut (ewmh-docks wm))))
219 +
220 + (defun raise-docks (wm)
221 + "Helper to raise dock windows."
222 + (dolist (dock (ewmh-docks wm))
223 + (setf (xlib:window-priority dock) :above)))
224 +
225 + (defun update-docks (wm)
226 + "Helper to update our knowledge of docks and grab masks to
227 + get updates on them."
228 + (setf (ewmh-docks wm)
229 + (loop for window in (xlib:query-tree (root wm))
230 + when (and (eql (xlib:window-map-state window) :viewable)
231 + (not (find-client wm window))
232 + (dock-p window))
233 + collect window))
234 + (dolist (dock (ewmh-docks wm))
235 + (setf (xlib:window-event-mask dock)
236 + (xlib:make-event-mask :property-change)))
237 + (raise-docks wm)
238 + (screen-geometry-changed wm))
239 +
240 + (defun tracked-dock-p (window)
241 + "Helper to check if a window is one of the docks we know about."
242 + (member window (ewmh-docks wm) :test #'xlib:window-equal))
243 +
244 + (defmethod screen-geometry-changed :after ((wm ewmh-mixin))
245 + (multiple-value-bind (x y width height)
246 + (work-area wm (first (screen-areas wm)))
247 + (set-cardinals (root wm) :_NET_WORKAREA (list x y width height))))
248 +
249 + ;;; steal all dock events
250 +
251 + (defmethod handle-event
252 + ((wm ewmh-mixin) (event-key (eql :map-notify)) &key window)
253 + (unless (or (find-client wm window) (tracked-dock-p wm window))
254 + (when (dock-p window)
255 + (update-docks wm)))
256 + (call-next-method))
257 +
258 + (defmethod handle-event
259 + ((wm ewmh-mixin) (event-key (eql :unmap-notify)) &key window)
260 + (when (tracked-dock-p wm window)
261 + (update-docks wm))
262 + (call-next-method))
263 +
264 + (defmethod handle-event
265 + ((wm ewmh-mixin) (event-key (eql :destroy-notify)) &key window)
266 + (when (tracked-dock-p wm window)
267 + (removef (ewmh-docks wm) window :test #'xlib:window-equal)
268 + (raise-docks wm)
269 + (screen-geometry-changed wm))
270 + (call-next-method))
271 +
272 + (defmethod handle-event
273 + ((wm ewmh-mixin) (event-key (eql :property-notify))
274 + &key window atom)
275 + (when (and (member atom '(:_NET_WM_STRUT :_NET_WM_STRUT_PARTIAL))
276 + (tracked-dock-p wm window))
277 + (screen-geometry-changed wm))
278 + (call-next-method))
added clwm-ext/focus-tracking-mixin.lisp mode 100644
@@ -0,0 +1,41 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; track the focused client
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass focus-tracking-mixin ()
8 + ((focused-client
9 + :initform nil :accessor focused-client
10 + :documentation "The client with the input focus or NIL."))
11 + (:documentation "Track the client with the input focus."))
12 +
13 + (defmethod root-event-mask append ((wm focus-tracking-mixin))
14 + '(:focus-change))
15 +
16 + (defmethod client-event-mask append ((wm focus-tracking-mixin) client)
17 + '(:focus-change))
18 +
19 + (defun update-focused-client (wm)
20 + "Ask X where the focus is, find the client, and update.
21 + Expects FOCUS-TRACKING-MIXIN."
22 + (setf (focused-client wm)
23 + (loop for window = (xlib:input-focus (display wm))
24 + then (nth-value 1 (xlib:query-tree window))
25 + while (and (xlib:window-p window)
26 + (not (xlib:window-equal window (root wm))))
27 + thereis (find-client wm window))))
28 +
29 + (defmethod handle-event
30 + ((wm focus-tracking-mixin) (event-key (eql :focus-in)) &key)
31 + (update-focused-client wm)
32 + (call-next-method))
33 +
34 + (defmethod handle-event
35 + ((wm focus-tracking-mixin) (event-key (eql :focus-out)) &key)
36 + (update-focused-client wm)
37 + (call-next-method))
38 +
39 + (defmethod unmanage-client :after ((wm focus-tracking-mixin) client)
40 + (when (eql client (focused-client wm))
41 + (setf (focused-client wm) nil)))
added clwm-ext/ignored-clients-mixin.lisp mode 100644
@@ -0,0 +1,32 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; ignore some clients
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass ignored-clients-mixin () ()
8 + (:documentation "Leave some windows unmanaged."))
9 +
10 + (defgeneric ignore-window-p (wm window)
11 + (:documentation "Whether to leave a window unmanaged.")
12 + (:method-combination or))
13 + (defgeneric ignored-window-names (wm)
14 + (:documentation "Names to match against WM_CLASS and WM_NAME,
15 + and ignore if they match.")
16 + (:method-combination append))
17 +
18 + (defmethod ignore-window-p or ((wm ignored-clients-mixin) window)
19 + (let ((names (ignored-window-names wm)))
20 + (and names
21 + (multiple-value-bind
22 + (instance class) (xlib:get-wm-class window)
23 + (let ((window-names
24 + (list instance class (xlib:wm-name window))))
25 + (some (lambda (name)
26 + (member name window-names :test #'equal))
27 + names))))))
28 + (defmethod ignored-window-names append ((wm ignored-clients-mixin))
29 + '())
30 +
31 + (defmethod manage-window-p and ((wm ignored-clients-mixin) window)
32 + (not (ignore-window-p wm window)))
added clwm-ext/interaction-mixin.lisp mode 100644
@@ -0,0 +1,89 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; modal interactions that get all events
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass interaction-mixin ()
8 + ((interaction :initform nil :accessor current-interaction
9 + :documentation "The current modal interaction or NIL."))
10 + (:documentation
11 + "Allow a single modal interaction, which may get first refusal on
12 + all events."))
13 +
14 + (defclass interaction ()
15 + ((client :initarg :client :initform nil :reader interaction-client))
16 + (:documentation
17 + "A modal interaction which may get first refusal on all events."))
18 +
19 + (defgeneric start-interaction (wm interaction)
20 + (:documentation "Make an interaction the WM's current interaction."))
21 + (defgeneric stop-interaction (wm interaction)
22 + (:documentation "Stop an interaction if it's the current one."))
23 + (defgeneric interaction-event
24 + (wm interaction event-key &key &allow-other-keys)
25 + (:documentation "Offer an event to an interaction.
26 + Non-NIL return consumes the event.")
27 + (:method (wm interaction event-key &key) nil))
28 +
29 + (defun stop-current-interaction (wm)
30 + "End the current interaction, whatever it may or may not be.
31 + Expects INTERACTION-MIXIN."
32 + (when-let ((interaction (current-interaction wm)))
33 + (stop-interaction wm interaction)))
34 +
35 + (defmethod start-interaction
36 + ((wm interaction-mixin) (interaction interaction))
37 + (stop-current-interaction wm)
38 + (setf (current-interaction wm) interaction))
39 + (defmethod stop-interaction
40 + ((wm interaction-mixin) (interaction interaction))
41 + (when (eql interaction (current-interaction wm))
42 + (setf (current-interaction wm) nil)))
43 +
44 + (defun abort-current-interaction (wm)
45 + "Tear down the current interaction after an error, best-effort.
46 + Releases grabs and flushes, so a debugger can be used afterwards.
47 + Expects INTERACTION-MIXIN."
48 + (ignore-errors (stop-current-interaction wm))
49 + ;; in case stop-interaction itself died partway
50 + (ignore-errors (xlib:ungrab-keyboard (display wm))
51 + (xlib:ungrab-pointer (display wm)))
52 + ;; CLX buffers requests; flush before we block in a debugger
53 + (ignore-errors (xlib:display-finish-output (display wm))))
54 +
55 + (defmacro with-interaction-abort-on-error ((wm) &body body)
56 + "Abort WM's current interaction on any error in BODY, then decline,
57 + so outer handlers (SKIP-EVENT, the debugger) still see the error."
58 + (let ((condition (gensym "CONDITION")))
59 + `(handler-bind ((error (lambda (,condition)
60 + (declare (ignore ,condition))
61 + (abort-current-interaction ,wm))))
62 + ,@body)))
63 +
64 + (defmethod handle-event :around
65 + ((wm interaction-mixin) event-key &rest e &key)
66 + (with-interaction-abort-on-error (wm)
67 + (let ((interaction (current-interaction wm)))
68 + (unless (and interaction
69 + (apply #'interaction-event
70 + wm interaction event-key e))
71 + (call-next-method)))))
72 +
73 + ;; async X errors often surface outside HANDLE-EVENT, in the event loop
74 + (defmethod handle-x-error :around
75 + ((wm interaction-mixin) error-key &key)
76 + (with-interaction-abort-on-error (wm)
77 + (call-next-method)))
78 +
79 + (defmethod unmanage-client :before ((wm interaction-mixin) client)
80 + (let ((interaction (current-interaction wm)))
81 + (when (and interaction
82 + (eql client (interaction-client interaction)))
83 + (stop-interaction wm interaction))))
84 +
85 + (defmethod uninitialize-wm :before ((wm interaction-mixin))
86 + (stop-current-interaction wm))
87 +
88 + (defmethod shutdown-wm :before ((wm interaction-mixin))
89 + (stop-current-interaction wm))
added clwm-ext/keybinds-mixin.lisp mode 100644
@@ -0,0 +1,55 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; keybinds.
4 + ;;;; the base convention is (lambda (wm) ...)
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defclass keybinds-mixin () ()
9 + (:documentation "Keys grabbed on the root to execute forms."))
10 +
11 + (defgeneric key-bindings (wm)
12 + (:documentation "Bindings from keybinding specs to functions.")
13 + (:method-combination append))
14 + (defgeneric grab-keys (wm)
15 + (:documentation "Grab the keys of the key bindings on the root."))
16 + (defgeneric call-key-command (wm command &key &allow-other-keys)
17 + (:documentation "Run a key binding's command."))
18 +
19 + (defmethod key-bindings append ((wm keybinds-mixin)) '())
20 +
21 + (defmethod grab-keys ((wm keybinds-mixin))
22 + (xlib:ungrab-key (root wm) :any :modifiers :any)
23 + (loop for (spec) in (key-bindings wm) do
24 + (dolist (keysym (key-keysyms (binding-key spec) (display wm)))
25 + (dolist (code (multiple-value-list
26 + (xlib:keysym->keycodes (display wm) keysym)))
27 + (dolist (mask (binding-grab-masks spec))
28 + (xlib:grab-key (root wm) code :modifiers mask))))))
29 +
30 + (defmethod initialize-wm :after ((wm keybinds-mixin))
31 + (grab-keys wm))
32 +
33 + (defmethod call-key-command ((wm keybinds-mixin) command &key)
34 + (funcall command wm))
35 +
36 + (defmethod handle-event
37 + ((wm keybinds-mixin) (event-key (eql :key-press))
38 + &rest e &key code state)
39 + (let ((command (find-binding
40 + (key-bindings wm) state
41 + (xlib:keycode->keysym (display wm) code 0)
42 + :test
43 + (lambda (keysym key)
44 + (member keysym (key-keysyms key (display wm)))))))
45 + (if command
46 + (apply #'call-key-command wm command e)
47 + (call-next-method))))
48 +
49 + (defmethod handle-event
50 + ((wm keybinds-mixin) (event-key (eql :mapping-notify))
51 + &key request start count)
52 + (xlib:mapping-notify (display wm) request start count)
53 + (when (eql request :keyboard)
54 + (grab-keys wm))
55 + (call-next-method))
added clwm-ext/maximize-mixin.lisp mode 100644
@@ -0,0 +1,119 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; maximize clients
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass maximize-mixin (screen-geometry-mixin) ()
8 + (:documentation "Maximize clients horizontally, vertically, or both,
9 + within the SCREEN-GEOMETRY-MIXIN work area. Geometry is stored to be
10 + restored."))
11 +
12 + (defclass maximizable-client ()
13 + ((maximized :initform nil :accessor client-maximized
14 + :documentation
15 + "A plist of :HORIZONTAL and :VERTICAL axis geometry
16 + to restore, or NIL if totally unmaximized."))
17 + (:documentation "A client that can be maximized."))
18 +
19 + (defmethod client-mixins append ((wm maximize-mixin))
20 + '(maximizable-client))
21 +
22 + (defgeneric maximize-state-changed (wm client)
23 + (:documentation "Handle a maximization state change.")
24 + (:method (wm client)
25 + (declare (ignore wm client))
26 + nil))
27 +
28 + (defun maximized-axes (client)
29 + "The axes a client is maximized along (:HORIZONTAL/:VERTICAL)."
30 + (remove-if-not (lambda (axis) (getf (client-maximized client) axis))
31 + '(:horizontal :vertical)))
32 +
33 + ;; clear maximizations when resizing maximized windows
34 + (defmethod configure-client :before
35 + ((wm maximize-mixin) client &key width height &allow-other-keys)
36 + (let ((horizontal
37 + (and width (remf (client-maximized client) :horizontal)))
38 + (vertical
39 + (and height (remf (client-maximized client) :vertical))))
40 + (when (or horizontal vertical)
41 + (maximize-state-changed wm client))))
42 +
43 + (defun axis-span (axis x y width height)
44 + "The position and size of a rectangle along AXIS
45 + (:HORIZONTAL/:VERTICAL) as a list."
46 + (ecase axis
47 + (:horizontal (list x width))
48 + (:vertical (list y height))))
49 +
50 + (defun span-geometry (axis span)
51 + "Helper to build CONFIGURE-CLIENT arguments putting a client at SPAN
52 + along AXIS."
53 + (destructuring-bind (position size) span
54 + (ecase axis
55 + (:horizontal (list :x position :width size))
56 + (:vertical (list :y position :height size)))))
57 +
58 + (defun remember-axes (client axes x y width height)
59 + "Record outer geometry to restore client to along AXES."
60 + (dolist (axis axes)
61 + (setf (getf (client-maximized client) axis)
62 + (axis-span axis x y width height))))
63 +
64 + (defun maximize-axes (wm client axes)
65 + "Fill the work area along AXES and remember the restore geometry."
66 + (multiple-value-bind (x y width height) (client-geometry wm client)
67 + (let ((area (multiple-value-list
68 + (work-area wm (area-at wm (+ x (floor width 2))
69 + (+ y (floor height 2)))))))
70 + (apply #'configure-client wm client
71 + (loop for axis in axes
72 + append (span-geometry
73 + axis
74 + (apply #'axis-span axis area))))
75 + (remember-axes client axes x y width height)
76 + (when axes
77 + (maximize-state-changed wm client)))))
78 +
79 + (defun restore-axes (wm client axes)
80 + "Restore a client along AXES using the remembered geometry."
81 + (let ((geometry
82 + (loop for axis in axes
83 + for span = (getf (client-maximized client) axis)
84 + when span
85 + append (span-geometry axis span)
86 + do (remf (client-maximized client) axis))))
87 + (when geometry
88 + (apply #'configure-client wm client geometry)
89 + (maximize-state-changed wm client))))
90 +
91 + (defun toggle-maximize (wm client &key (horizontal t) (vertical t))
92 + "Toggle maximization, optionally not along both axes."
93 + (let ((axes (append (when horizontal '(:horizontal))
94 + (when vertical '(:vertical))))
95 + (maximized (maximized-axes client)))
96 + (if (subsetp axes maximized)
97 + (restore-axes wm client axes)
98 + (maximize-axes wm client (set-difference axes maximized)))))
99 +
100 + (defun maximize-command (wm client x y)
101 + "Maximize or restore the client under the pointer.
102 + Expects MAXIMIZE-MIXIN.
103 + Respects POINTER-KEYBINDS-MIXIN convention."
104 + (declare (ignore x y))
105 + (when client (toggle-maximize wm client)))
106 +
107 + (defun vmaximize-command (wm client x y)
108 + "Vertically maximize or restore the client under the pointer.
109 + Expects MAXIMIZE-MIXIN.
110 + Respects POINTER-KEYBINDS-MIXIN convention."
111 + (declare (ignore x y))
112 + (when client (toggle-maximize wm client :horizontal nil)))
113 +
114 + (defun hmaximize-command (wm client x y)
115 + "Horizontally maximize or restore the client under the pointer.
116 + Expects MAXIMIZE-MIXIN.
117 + Respects POINTER-KEYBINDS-MIXIN convention."
118 + (declare (ignore x y))
119 + (when client (toggle-maximize wm client :vertical nil)))
added clwm-ext/modal-commands-mixin.lisp mode 100644
@@ -0,0 +1,350 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; modal commands, like menus
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass modal-commands-mixin (interaction-mixin) ()
8 + (:documentation "Modal menu command support."))
9 +
10 + ;; todo: support Xft and whatever
11 + (defgeneric menu-font-name (wm)
12 + (:documentation "The font menus use."))
13 + (defgeneric menu-colors (wm)
14 + (:documentation "Foreground/background colors for menus."))
15 +
16 + (defmethod menu-font-name ((wm modal-commands-mixin)) "fixed")
17 + (defmethod menu-colors ((wm modal-commands-mixin))
18 + (values "black" "white"))
19 +
20 + (defclass modal-window (interaction)
21 + ((x :initarg :x :initform 0 :accessor modal-x)
22 + (y :initarg :y :initform 0 :accessor modal-y)
23 + ;; should i just call it "modal"? but other things are modal
24 + (window :initform nil :accessor modal-window-window)
25 + (background :initform nil :accessor modal-background)
26 + (border :initform nil :accessor modal-border))
27 + (:documentation
28 + "An interaction that draws a window and grabs all input for it."))
29 +
30 + (defgeneric draw-modal (wm modal)
31 + (:documentation "Draw a modal window."))
32 + (defgeneric modal-key-press (wm modal code state)
33 + (:documentation "Handle a keypress for a modal window."))
34 + (defgeneric modal-button-press (wm modal button x y)
35 + (:documentation "Handle a mouse button press for a modal window."))
36 +
37 + (defun inside-window-p (window x y)
38 + "Helper for cancelling modals by clicking away."
39 + (xlib:with-state (window)
40 + (let ((left (xlib:drawable-x window))
41 + (top (xlib:drawable-y window)))
42 + (and (<= left x (+ left (xlib:drawable-width window)))
43 + (<= top y (+ top (xlib:drawable-height window)))))))
44 +
45 + (defmethod start-interaction :after
46 + ((wm interaction-mixin) (modal modal-window))
47 + (let* ((screen (screen wm))
48 + (window (xlib:create-window
49 + :parent (root wm)
50 + :x (modal-x modal) :y (modal-y modal)
51 + :width 1 :height 1 :border-width 1
52 + :background (or (modal-background modal)
53 + (xlib:screen-white-pixel screen))
54 + :override-redirect :on
55 + :event-mask (xlib:make-event-mask :exposure))))
56 + (setf (modal-window-window modal) window)
57 + (draw-modal wm modal)
58 + (xlib:map-window window)
59 + (unless (and (eql (xlib:grab-keyboard window) :success)
60 + (eql (xlib:grab-pointer window '(:button-press))
61 + :success))
62 + (stop-interaction wm modal))))
63 +
64 + (defmethod stop-interaction :after
65 + ((wm interaction-mixin) (modal modal-window))
66 + (when-let ((window (shiftf (modal-window-window modal) nil)))
67 + (xlib:ungrab-keyboard (display wm))
68 + (xlib:ungrab-pointer (display wm))
69 + (xlib:destroy-window window)))
70 +
71 + ;; clicking away cancels
72 + (defmethod modal-button-press
73 + ((wm interaction-mixin) (modal modal-window) button x y)
74 + (declare (ignore button))
75 + (unless (inside-window-p (modal-window-window modal) x y)
76 + (stop-interaction wm modal)))
77 +
78 + ;; modals consume it all
79 + (defmethod interaction-event
80 + ((wm interaction-mixin) (modal modal-window)
81 + (event-key (eql :key-press)) &key code state)
82 + (modal-key-press wm modal code state)
83 + t)
84 +
85 + (defmethod interaction-event
86 + ((wm interaction-mixin) (modal modal-window)
87 + (event-key (eql :button-press)) &key code root-x root-y)
88 + (modal-button-press wm modal code root-x root-y)
89 + t)
90 +
91 + (defmethod interaction-event
92 + ((wm interaction-mixin) (modal modal-window)
93 + (event-key (eql :key-release)) &key)
94 + t)
95 +
96 + (defmethod interaction-event
97 + ((wm interaction-mixin) (modal modal-window)
98 + (event-key (eql :button-release)) &key)
99 + t)
100 +
101 + (defmethod interaction-event
102 + ((wm interaction-mixin) (modal modal-window)
103 + (event-key (eql :motion-notify)) &key)
104 + t)
105 +
106 + (defmethod interaction-event
107 + ((wm interaction-mixin) (modal modal-window)
108 + (event-key (eql :exposure)) &key window count)
109 + (let ((ours (modal-window-window modal)))
110 + (when (and ours (xlib:window-equal window ours))
111 + (when (zerop count)
112 + (draw-modal wm modal))
113 + t)))
114 +
115 + ;;; cwm-style completing menu
116 +
117 + (defclass menu (modal-window)
118 + ((prompt :initarg :prompt :initform "" :reader menu-prompt)
119 + (items :initarg :items :initform '() :reader menu-items)
120 + (action :initarg :action :reader menu-action)
121 + (input :initform "" :accessor menu-input)
122 + (selection :initform 0 :accessor menu-selection)
123 + (font :initform nil :accessor menu-font)
124 + (gcontext :initform nil :accessor menu-gcontext)
125 + (inverse-gcontext :initform nil :accessor menu-inverse-gcontext)
126 + (line-height :initform 1 :accessor menu-line-height))
127 + (:documentation "A cwm-style menu where typing input filters a list
128 + of candidate items."))
129 +
130 + (defun menu-matches (menu)
131 + "Helper to get the menu candidates matching input."
132 + (let ((input (menu-input menu))
133 + (exact '()) (prefix '()) (other '()))
134 + (dolist (item (menu-items menu))
135 + (let ((position (search input (car item) :test #'char-equal)))
136 + (cond ((null position))
137 + ((and (zerop position)
138 + (= (length input) (length (car item))))
139 + (push item exact))
140 + ((zerop position) (push item prefix))
141 + (t (push item other)))))
142 + (nconc (nreverse exact) (nreverse prefix) (nreverse other))))
143 +
144 + (defun run-menu (wm &key (prompt "") items action x y)
145 + "Base menu runner to build menu commands from.
146 + Expects MODAL-COMMANDS-MIXIN."
147 + (multiple-value-bind (pointer-x pointer-y)
148 + (xlib:query-pointer (root wm))
149 + (start-interaction wm (make-instance 'menu
150 + :prompt prompt :items items
151 + :action action
152 + :x (or x pointer-x)
153 + :y (or y pointer-y)))))
154 +
155 + (defun menu-choose (wm menu)
156 + "Helper for ending a menu interaction by choosing an item."
157 + (let ((item (nth (menu-selection menu) (menu-matches menu)))
158 + (input (menu-input menu)))
159 + (stop-interaction wm menu)
160 + (funcall (menu-action menu) wm (cdr item) input)))
161 +
162 + (defmethod draw-modal ((wm interaction-mixin) (menu menu))
163 + (let* ((window (modal-window-window menu))
164 + (font (menu-font menu))
165 + (screen (screen wm))
166 + (ascent (xlib:font-ascent font))
167 + (line (+ ascent (xlib:font-descent font) 2))
168 + (pad 4)
169 + (max-items (max 1
170 + (1- (floor (xlib:screen-height screen) line))))
171 + (matches (menu-matches menu))
172 + (shown (subseq matches 0 (min (length matches) max-items)))
173 + (lines (mapcar #'drawable-text
174 + (cons (concatenate 'string (menu-prompt menu)
175 + (menu-input menu))
176 + (mapcar #'car shown))))
177 + (width (+ pad pad
178 + (max 200
179 + (reduce #'max lines
180 + :key (lambda (text)
181 + (xlib:text-width font text))))))
182 + (height (* line (length lines))))
183 + (setf (menu-line-height menu) line
184 + (menu-selection menu) (max 0 (min (menu-selection menu)
185 + (1- (length shown)))))
186 + (configure-window
187 + window
188 + :x (max 0 (min (modal-x menu)
189 + (- (xlib:screen-width screen) width 2)))
190 + :y (max 0 (min (modal-y menu)
191 + (- (xlib:screen-height screen) height 2)))
192 + :width width :height height)
193 + (xlib:clear-area window)
194 + (loop for text in lines
195 + for index from -1
196 + for top from 0 by line
197 + for selected = (and shown (= index (menu-selection menu)))
198 + do (when selected
199 + (xlib:draw-rectangle window (menu-gcontext menu)
200 + 0 top width line t))
201 + (xlib:draw-glyphs window (if selected
202 + (menu-inverse-gcontext menu)
203 + (menu-gcontext menu))
204 + pad (+ top 1 ascent) text))))
205 +
206 + (defmethod modal-key-press
207 + ((wm interaction-mixin) (menu menu) code state)
208 + (let* ((display (display wm))
209 + (keysym (xlib:keycode->keysym display code 0))
210 + (letter (xlib:keycode->character display code 0))
211 + (char (xlib:keycode->character display code state))
212 + (control (logtest state (xlib:make-state-mask :control)))
213 + (count (length (menu-matches menu))))
214 + (flet ((control-p (c) (and control (eql letter c)))
215 + (set-input (input)
216 + (setf (menu-input menu) input
217 + (menu-selection menu) 0)))
218 + ;; esc, C-g: exit menu
219 + (cond ((or (= keysym #xff1b) (control-p #\g))
220 + (stop-interaction wm menu))
221 + ;; ret, C-m: choose menu item
222 + ((or (= keysym #xff0d) (control-p #\m))
223 + (menu-choose wm menu))
224 + ;; down, C-n: next menu entry
225 + ((or (= keysym #xff54) (control-p #\n))
226 + (setf (menu-selection menu)
227 + (min (max 0 (1- count))
228 + (1+ (menu-selection menu)))))
229 + ;; up, C-p: prev menu entry
230 + ((or (= keysym #xff52) (control-p #\p))
231 + (setf (menu-selection menu)
232 + (max 0 (1- (menu-selection menu)))))
233 + ;; backspace, C-h: backspace
234 + ((or (= keysym #xff08) (control-p #\h))
235 + (let ((input (menu-input menu)))
236 + (set-input (subseq input 0 (max 0
237 + (1- (length input)))))))
238 + ;; C-u: clear
239 + ((control-p #\u)
240 + (set-input ""))
241 + ;; printables: type the character
242 + ((and (not control) (characterp char) (graphic-char-p char))
243 + (set-input (concatenate 'string (menu-input menu)
244 + (string char))))))
245 + (when (eql menu (current-interaction wm))
246 + (draw-modal wm menu))))
247 +
248 + ;; clicking on menu items
249 + (defmethod modal-button-press
250 + ((wm interaction-mixin) (menu menu) button x y)
251 + (declare (ignore button))
252 + (let ((window (modal-window-window menu)))
253 + (if (inside-window-p window x y)
254 + (let ((row (1- (floor (- y (xlib:drawable-y window))
255 + (menu-line-height menu)))))
256 + (when (< -1 row (length (menu-matches menu)))
257 + (setf (menu-selection menu) row)
258 + (menu-choose wm menu)))
259 + (call-next-method))))
260 +
261 + ;; set up fonts and graphics context for menu
262 + (defmethod start-interaction :before
263 + ((wm modal-commands-mixin) (menu menu))
264 + (let ((font (xlib:open-font (display wm) (menu-font-name wm))))
265 + (multiple-value-bind (foreground background) (menu-colors wm)
266 + (let ((foreground (color wm foreground))
267 + (background (color wm background)))
268 + (setf (menu-font menu) font
269 + (modal-background menu) background
270 + (modal-border menu) foreground
271 + (menu-gcontext menu)
272 + (xlib:create-gcontext :drawable (root wm) :font font
273 + :foreground foreground
274 + :background background)
275 + (menu-inverse-gcontext menu)
276 + (xlib:create-gcontext :drawable (root wm) :font font
277 + :foreground background
278 + :background foreground))))))
279 +
280 + ;; free the stuff from the above method
281 + (defmethod stop-interaction :after ((wm interaction-mixin) (menu menu))
282 + (dolist (gcontext (list (shiftf (menu-gcontext menu) nil)
283 + (shiftf (menu-inverse-gcontext menu) nil)))
284 + (when gcontext (xlib:free-gcontext gcontext)))
285 + (when-let ((font (shiftf (menu-font menu) nil)))
286 + (xlib:close-font font)))
287 +
288 + ;;; actual cwm-style menu commands
289 +
290 + ;; note to self: string< is case sensitive, string-lessp isn't
291 +
292 + (defun path-programs ()
293 + "Helper to grab all executables on the path."
294 + (let ((names (make-hash-table :test 'equal)))
295 + (dolist (directory (uiop:split-string (or (uiop:getenv "PATH") "")
296 + :separator ":"))
297 + (when (plusp (length directory))
298 + (dolist (file (uiop:directory-files
299 + (uiop:ensure-directory-pathname directory)))
300 + (let ((path (uiop:native-namestring file)))
301 + ;; uiop can't check this; is there something portable?
302 + (when (ignore-errors (sb-posix:access path sb-posix:x-ok) t)
303 + (setf (gethash
304 + (subseq path (1+ (position #\/ path :from-end t)))
305 + names)
306 + t))))))
307 + (sort (hash-table-keys names) #'string<)))
308 +
309 + (defun exec-menu-command (wm client x y)
310 + "Spawn an exec menu at the pointer.
311 + Expects MODAL-COMMANDS-MIXIN.
312 + Respects POINTER-KEYBINDS-MIXIN convention."
313 + (declare (ignore client))
314 + (run-menu wm :prompt "exec: " :x x :y y
315 + :items (mapcar (lambda (name) (cons name name))
316 + (path-programs))
317 + :action (lambda (wm program input)
318 + (let ((command (if (or (null program)
319 + (find #\Space input))
320 + input
321 + program)))
322 + (when (plusp (length command))
323 + (spawn wm "/bin/sh" "-c" command))))))
324 +
325 + ;; todo: labeling mixin
326 + (defun client-label (client)
327 + "Helper to name a client for the window menu."
328 + (let ((window (client-window client)))
329 + (format nil "~a~:[~; (hidden)~]"
330 + (or (xlib:wm-name window) "")
331 + (eql (wm-state window) :iconic))))
332 +
333 + (defun window-menu-command (wm client x y)
334 + "Spawn a window menu at the pointer.
335 + Expects MODAL-COMMANDS-MIXIN.
336 + Respects POINTER-KEYBINDS-MIXIN convention."
337 + (declare (ignore client))
338 + (let ((items '()))
339 + (map-clients (lambda (client)
340 + (push (cons (client-label client) client) items))
341 + wm)
342 + (run-menu wm :prompt "window: " :x x :y y
343 + :items (sort items #'string-lessp :key #'car)
344 + :action (lambda (wm client input)
345 + (declare (ignore input))
346 + (when client
347 + (show-client wm client)
348 + (configure-client wm client
349 + :stack-mode :above)
350 + (focus-client wm client))))))
added clwm-ext/mousebinds-mixin.lisp mode 100644
@@ -0,0 +1,61 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; mouse bindings
4 + ;;;; the convention is (lambda (wm client x y) ...)
5 + ;;;; possibly returning a (lambda (x y) ...)
6 + ;;;; which is called on every move until the interaction ends
7 +
8 + (in-package #:clwm-ext)
9 +
10 + (defclass mousebinds-mixin (interaction-mixin) ()
11 + (:documentation "Buttons (and modifiers) grabbed on the root
12 + to execute forms."))
13 +
14 + (defclass drag (interaction)
15 + ((function :initarg :function :reader drag-function))
16 + (:documentation "Call a function on pointer motion until release."))
17 +
18 + (defgeneric mouse-bindings (wm)
19 + (:documentation "Bindings of (modifiers... button) to functions.")
20 + (:method-combination append))
21 + (defgeneric grab-buttons (wm)
22 + (:documentation
23 + "Grab the buttons of the mouse bindings on the root."))
24 +
25 + (defmethod mouse-bindings append ((wm mousebinds-mixin)) '())
26 +
27 + (defmethod grab-buttons ((wm mousebinds-mixin))
28 + (xlib:ungrab-button (root wm) :any :modifiers :any)
29 + (loop for (spec) in (mouse-bindings wm)
30 + do (dolist (mask (binding-grab-masks spec))
31 + (xlib:grab-button (root wm) (binding-key spec)
32 + '(:button-press :button-release
33 + :pointer-motion)
34 + :modifiers mask))))
35 +
36 + (defmethod initialize-wm :after ((wm mousebinds-mixin))
37 + (grab-buttons wm))
38 +
39 + (defmethod handle-client-event
40 + ((wm mousebinds-mixin) client (event-key (eql :button-press))
41 + &key code state root-x root-y)
42 + (let ((command (find-binding (mouse-bindings wm) state code)))
43 + (if command
44 + (let ((function (funcall command wm client root-x root-y)))
45 + (when (functionp function)
46 + (start-interaction
47 + wm
48 + (make-instance 'drag :client client :function function))))
49 + (call-next-method))))
50 +
51 + (defmethod interaction-event
52 + ((wm interaction-mixin) (drag drag) (event-key (eql :motion-notify))
53 + &key root-x root-y)
54 + (funcall (drag-function drag) root-x root-y)
55 + t)
56 +
57 + (defmethod interaction-event
58 + ((wm interaction-mixin) (drag drag)
59 + (event-key (eql :button-release)) &key)
60 + (stop-interaction wm drag)
61 + t)
added clwm-ext/pointer-keybinds-mixin.lisp mode 100644
@@ -0,0 +1,14 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; keybinds that know where the pointer is
4 + ;;;; the convention is (lambda (wm client x y) ...)
5 +
6 + (in-package #:clwm-ext)
7 +
8 + (defclass pointer-keybinds-mixin (keybinds-mixin) ()
9 + (:documentation "Keybinds which know where the pointer is,
10 + and what client is under it."))
11 +
12 + (defmethod call-key-command ((wm pointer-keybinds-mixin) command
13 + &rest e &key root-x root-y)
14 + (funcall command wm (apply #'event-client wm :key-press e) root-x root-y))
added clwm-ext/pointer-placement-mixin.lisp mode 100644
@@ -0,0 +1,39 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; place new clients under the pointer by default
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass pointer-placement-mixin (screen-geometry-mixin) ()
8 + (:documentation
9 + "Place new clients under the pointer if they don't place themselves.
10 + Keep them inside the SCREEN-GEOMETRY-MIXIN work area."))
11 +
12 + (defmethod place-client ((wm pointer-placement-mixin) client)
13 + (let ((hints (client-normal-hints client)))
14 + (unless (and
15 + hints
16 + ;; ridiculously long names
17 + (or (xlib:wm-size-hints-user-specified-position-p hints)
18 + ;; for some reason, not exported
19 + (xlib::wm-size-hints-program-specified-position-p
20 + hints)))
21 + (multiple-value-bind (x y width height)
22 + (client-geometry wm client)
23 + (declare (ignore x y))
24 + (multiple-value-bind (pointer-x pointer-y)
25 + (xlib:query-pointer (root wm))
26 + (multiple-value-bind (area-x area-y area-width area-height)
27 + (work-area wm (area-at wm pointer-x pointer-y))
28 + (flet ((inside (position size start extent)
29 + (max start
30 + (min position (- (+ start extent) size)))))
31 + (configure-client
32 + wm client
33 + :x (inside
34 + (- pointer-x (floor width 2))
35 + width area-x area-width)
36 + :y (inside
37 + (- pointer-y (floor height 2))
38 + height area-y area-height))))))))
39 + (call-next-method))
added clwm-ext/screen-geometry-mixin.lisp mode 100644
@@ -0,0 +1,120 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; screen geometry support. todo: multimonitor
4 +
5 + (in-package #:clwm-ext)
6 +
7 + ;; don't need slots until multimonitor
8 + (defclass screen-geometry-mixin () ()
9 + (:documentation "The screen's areas and work areas."))
10 +
11 + (defgeneric screen-areas (wm)
12 + (:documentation "Rectangles for the various screen areas."))
13 + (defgeneric reserved-edges (wm)
14 + (:documentation "Space reserved at edges (left right top bottom).")
15 + (:method-combination append))
16 + (defgeneric screen-geometry-changed (wm)
17 + (:documentation "Handle the screen areas or reserved edges changing."))
18 +
19 + ;; for now, just one
20 + (defmethod screen-areas ((wm screen-geometry-mixin))
21 + (list (list 0 0
22 + (xlib:screen-width (screen wm))
23 + (xlib:screen-height (screen wm)))))
24 +
25 + (defmethod reserved-edges append ((wm screen-geometry-mixin)) '())
26 +
27 + (defmethod screen-geometry-changed ((wm screen-geometry-mixin))
28 + nil)
29 +
30 + (defun area-at (wm x y)
31 + "Helper to find the area containing a point.
32 + Expects SCREEN-GEOMETRY-MIXIN."
33 + (flet ((distance (area)
34 + (destructuring-bind (left top width height) area
35 + (let ((dx (max 0 (- left x) (- x (+ left width -1))))
36 + (dy (max 0 (- top y) (- y (+ top height -1)))))
37 + (+ (* dx dx) (* dy dy))))))
38 + (extremum (screen-areas wm) #'< :key #'distance)))
39 +
40 + (defun work-area (wm area)
41 + "The rectangle that isn't reserved in an area.
42 + Expects SCREEN-GEOMETRY-MIXIN."
43 + (destructuring-bind (x y width height) area
44 + (let* ((edges (reserved-edges wm))
45 + (screen (screen wm))
46 + (left (max x (reduce #'max edges
47 + :key #'first :initial-value 0)))
48 + (top (max y (reduce #'max edges
49 + :key #'third :initial-value 0)))
50 + (right (min (+ x width)
51 + (- (xlib:screen-width screen)
52 + (reduce #'max edges
53 + :key #'second :initial-value 0))))
54 + (bottom (min (+ y height)
55 + (- (xlib:screen-height screen)
56 + (reduce #'max edges
57 + :key #'fourth :initial-value 0)))))
58 + (if (and (< left right) (< top bottom))
59 + (values left top (- right left) (- bottom top))
60 + (values x y width height)))))
61 +
62 + (defgeneric geometry-bounds (wm client x y width height)
63 + (:documentation
64 + "Edges that clients should not be able to move past.
65 + (Left, top, right, bottom.)")
66 + (:method (wm client x y width height)
67 + (declare (ignore wm client x y width height))
68 + (values nil nil nil nil)))
69 +
70 + (defun neighbor-p (area areas side)
71 + "Whether AREA has a neighbor in AREAS on SIDE."
72 + (destructuring-bind (x y width height) area
73 + (some (lambda (other)
74 + (destructuring-bind (ox oy owidth oheight) other
75 + (let ((rows
76 + (and (< oy (+ y height)) (< y (+ oy oheight))))
77 + (columns
78 + (and (< ox (+ x width)) (< x (+ ox owidth)))))
79 + (ecase side
80 + (:left (and rows (= (+ ox owidth) x)))
81 + (:right (and rows (= ox (+ x width))))
82 + (:top (and columns (= (+ oy oheight) y)))
83 + (:bottom (and columns (= oy (+ y height))))))))
84 + (remove area areas))))
85 +
86 + (defmethod geometry-bounds ((wm screen-geometry-mixin) client
87 + x y width height)
88 + (declare (ignore client))
89 + (let* ((areas (screen-areas wm))
90 + (area (area-at wm
91 + (+ x (floor width 2))
92 + (+ y (floor height 2)))))
93 + (multiple-value-bind (left top work-width work-height)
94 + (work-area wm area)
95 + (flet ((wall (side edge)
96 + (unless (neighbor-p area areas side) edge)))
97 + (values (wall :left left)
98 + (wall :top top)
99 + (wall :right (+ left work-width))
100 + (wall :bottom (+ top work-height)))))))
101 +
102 + (defun constrain-move (wm client x y width height)
103 + "Constrain a move by the geometry bounds."
104 + (multiple-value-bind (left top right bottom)
105 + (geometry-bounds wm client x y width height)
106 + (flet ((inside (position size start end)
107 + (when end (setf position (min position (- end size))))
108 + (when start (setf position (max position start)))
109 + position))
110 + (values (inside x width left right)
111 + (inside y height top bottom)))))
112 +
113 + ;; todo: non bottom right resizes
114 + (defun constrain-resize (wm client x y width height)
115 + "Constrain a resize by the geometry bounds."
116 + (multiple-value-bind (left top right bottom)
117 + (geometry-bounds wm client x y width height)
118 + (declare (ignore left top))
119 + (values (max 1 (if right (min width (- right x)) width))
120 + (max 1 (if bottom (min height (- bottom y)) height)))))
added clwm-ext/sloppy-focus-follows-mouse-mixin.lisp mode 100644
@@ -0,0 +1,21 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; sloppy focus-follows-mouse (don't unfocus on moving out)
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass sloppy-focus-follows-mouse-mixin () ()
8 + (:documentation
9 + "Focus follows mouse, but entering the root doesn't unfocus."))
10 +
11 + (defmethod client-event-mask append
12 + ((wm sloppy-focus-follows-mouse-mixin) client)
13 + '(:enter-window))
14 +
15 + (defmethod handle-client-event
16 + ((wm sloppy-focus-follows-mouse-mixin) client
17 + (event-key (eql :enter-notify))
18 + &key mode kind)
19 + (if (and (eql mode :normal) (not (eql kind :inferior)))
20 + (focus-client wm client)
21 + (call-next-method)))
added clwm-ext/warp-pointer-mixin.lisp mode 100644
@@ -0,0 +1,58 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; save a pointer warp position on every client and use on focus
4 +
5 + (in-package #:clwm-ext)
6 +
7 + (defclass warp-pointer-mixin () ()
8 + (:documentation "Focusing a client by means other than moving the
9 + pointer warps the pointer to where it last was on that client (falling
10 + back to the center)."))
11 +
12 + (defclass warp-pointer-client ()
13 + ((pointer :initform nil :accessor client-pointer
14 + :documentation
15 + "Where the pointer last was on the client, or NIL."))
16 + (:documentation "A client that remembers where the pointer last was
17 + on it."))
18 +
19 + (defmethod client-mixins append ((wm warp-pointer-mixin))
20 + '(warp-pointer-client))
21 +
22 + (defun pointer-client (wm)
23 + "Helper to find the client the pointer is on and the pointer's
24 + coordinates."
25 + (multiple-value-bind (x y same-screen-p child)
26 + (xlib:query-pointer (root wm))
27 + (declare (ignore same-screen-p))
28 + (values (and child (find-client wm child)) x y)))
29 +
30 + (defun remember-pointer (wm client x y)
31 + "Remember where the pointer was on a client.
32 + Expects WARP-POINTER-MIXIN."
33 + (multiple-value-bind (client-x client-y) (client-geometry wm client)
34 + (setf (client-pointer client)
35 + (list (- x client-x) (- y client-y)))))
36 +
37 + (defun warp-to-client (wm client)
38 + "Warp the pointer to where it last was on a client, or fall back
39 + to the center.
40 + Expects WARP-POINTER-MIXIN."
41 + (multiple-value-bind (x y width height) (client-geometry wm client)
42 + (destructuring-bind (&optional px py) (client-pointer client)
43 + (unless (and px (< -1 px width) (< -1 py height))
44 + (setf px (floor width 2)
45 + py (floor height 2)))
46 + (xlib:warp-pointer (root wm) (+ x px) (+ y py)))))
47 +
48 + (defmethod focus-client :after ((wm warp-pointer-mixin) client)
49 + (multiple-value-bind (under x y) (pointer-client wm)
50 + (unless (eql under client)
51 + (when under
52 + (remember-pointer wm under x y))
53 + (warp-to-client wm client))))
54 +
55 + (defmethod hide-client :before ((wm warp-pointer-mixin) client)
56 + (multiple-value-bind (under x y) (pointer-client wm)
57 + (when (eql under client)
58 + (remember-pointer wm client x y))))
modified clwm-user.lisp
@@ -3,4 +3,4 @@
3 3 ;;;; CLWM-USER: package where the user can assemble and run a WM
4 4
5 5 (defpackage #:clwm-user
6 - (:use #:cl #:clwm))
6 + (:use #:cl #:clwm #:clwm-ext))
modified clwm.asd
@@ -5,9 +5,32 @@
5 5 :depends-on ("alexandria"
6 6 "bordeaux-threads"
7 7 "clx"
8 + "sb-posix"
8 9 "serapeum"
9 10 "slynk"
10 11 "uiop")
11 12 :serial t
12 13 :components ((:file "clwm")
14 + (:file "clwm-ext")
15 + (:module "ext"
16 + :pathname "clwm-ext"
17 + :serial t
18 + :components ((:file "sloppy-focus-follows-mouse-mixin")
19 + (:file "focus-tracking-mixin")
20 + (:file "ignored-clients-mixin")
21 + (:file "interaction-mixin")
22 + (:file "bindings")
23 + (:file "keybinds-mixin")
24 + (:file "pointer-keybinds-mixin")
25 + (:file "commands")
26 + (:file "mousebinds-mixin")
27 + (:file "warp-pointer-mixin")
28 + (:file "alt-tab-ring-mixin")
29 + (:file "screen-geometry-mixin")
30 + (:file "pointer-placement-mixin")
31 + (:file "maximize-mixin")
32 + (:file "drawing")
33 + (:file "modal-commands-mixin")
34 + (:file "ewmh-mixin")
35 + (:file "ewmh-maximize-mixin")))
13 36 (:file "clwm-user")))
added example-init.lisp mode 100644
@@ -0,0 +1,44 @@
1 + ;;;; common lisp window manager
2 + ;;;;
3 + ;;;; example user init file
4 +
5 + (in-package #:clwm-user)
6 +
7 + (defclass my-wm (ewmh-maximize-mixin
8 + ewmh-mixin
9 + modal-commands-mixin
10 + maximize-mixin
11 + pointer-placement-mixin
12 + screen-geometry-mixin
13 + alt-tab-ring-mixin
14 + warp-pointer-mixin
15 + mousebinds-mixin
16 + pointer-keybinds-mixin
17 + ignored-clients-mixin
18 + focus-tracking-mixin
19 + sloppy-focus-follows-mouse-mixin
20 + wm)
21 + ())
22 +
23 + (defmethod key-bindings append ((wm my-wm))
24 + `(((:control :mod-1 #\Return) . ,(spawn-command "xterm"))
25 + ((:control :mod-1 #\x) . close-command)
26 + ((:mod-1 #\Return) . hide-command)
27 + ((:mod-1 :up) . raise-command)
28 + ((:mod-1 :down) . lower-command)
29 + ((:mod-1 :tab) . ,(cycle-forward :mod-1 :tab))
30 + ((:mod-1 :shift :tab) . ,(cycle-backward :mod-1 :tab))
31 + ((:control :mod-1 #\m) . maximize-command)
32 + ((:control :mod-1 #\=) . vmaximize-command)
33 + ((:control :mod-1 :shift #\=) . hmaximize-command)
34 + ((:mod-1 :shift #\/) . exec-menu-command)
35 + ((:mod-1 #\/) . window-menu-command)))
36 +
37 + (defmethod mouse-bindings append ((wm my-wm))
38 + '(((:mod-1 1) . bounded-drag-move)
39 + ((:mod-1 3) . bounded-drag-resize)))
40 +
41 + (run-wm 'my-wm)
42 +
43 + ;; if slynk is desired
44 + ;; (start-slynk)