Compare

a0296e8f7685b3c487dbf75f257dffdb21f81d27..7f2aa7db783bf4b1f5559f054758b3cded685bcd

clwm-ext: alt-tab-ring-mixin

Raymond E. Pasco <ray@ameretat.dev> · 2026-10-27 10:12 UTC

Browse files
5 files changed, +221 −5
modified clwm-ext.lisp
@@ -5,7 +5,8 @@
5 5 (defpackage #:clwm-ext
6 6 (:use :cl :clwm)
7 7 (:import-from #:alexandria
8 - #:when-let #:lastcar #:format-symbol #:clamp)
8 + #:when-let #:lastcar #:format-symbol #:clamp #:appendf
9 + #:removef)
9 10 (:export
10 11 ;; sloppy-focus-follows-mouse-mixin
11 12 #:sloppy-focus-follows-mouse-mixin
@@ -30,4 +31,9 @@
30 31 #:mousebinds-mixin #:mouse-bindings #:grab-buttons #:drag
31 32 #:drag-function
32 33 ;; mousebinds respecting commands
33 - #:drag-move #:drag-resize))
34 + #:drag-move #:drag-resize
35 + ;; warp-pointer-mixin
36 + #:warp-pointer-mixin #:warp-pointer-client #:client-pointer
37 + ;; alt-tab-ring-mixin
38 + #:alt-tab-ring-mixin #:client-ring #:ring-cycle
39 + #:cycle-forward #:cycle-backward))
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/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.asd
@@ -22,5 +22,7 @@
22 22 (:file "keybinds-mixin")
23 23 (:file "pointer-keybinds-mixin")
24 24 (:file "commands")
25 - (:file "mousebinds-mixin")))
25 + (:file "mousebinds-mixin")
26 + (:file "warp-pointer-mixin")
27 + (:file "alt-tab-ring-mixin")))
26 28 (:file "clwm-user")))
modified example-init.lisp
@@ -4,7 +4,9 @@
4 4
5 5 (in-package #:clwm-user)
6 6
7 - (defclass my-wm (mousebinds-mixin
7 + (defclass my-wm (alt-tab-ring-mixin
8 + warp-pointer-mixin
9 + mousebinds-mixin
8 10 pointer-keybinds-mixin
9 11 ignored-clients-mixin
10 12 focus-tracking-mixin
@@ -17,7 +19,9 @@
17 19 ((:control :mod-1 #\x) . close-command)
18 20 ((:mod-1 #\Return) . hide-command)
19 21 ((:mod-1 :up) . raise-command)
20 - ((:mod-1 :down) . lower-command)))
22 + ((:mod-1 :down) . lower-command)
23 + ((:mod-1 :tab) . ,(cycle-forward :mod-1 :tab))
24 + ((:mod-1 :shift :tab) . ,(cycle-backward :mod-1 :tab))))
21 25
22 26 (defmethod mouse-bindings append ((wm my-wm))
23 27 '(((:mod-1 1) . drag-move)