Compare

2aa41890100f87cac4b922b1eb51c0d2804197f6..bfc9a230fa1cb8036be7d51096fbfa7237914288

clwm-ext: modal-commands-mixin

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

Browse files
5 files changed, +396 −5
modified clwm-ext.lisp
@@ -6,7 +6,7 @@
6 6 (:use :cl :clwm)
7 7 (:import-from #:alexandria
8 8 #:when-let #:lastcar #:format-symbol #:clamp #:appendf
9 - #:removef #:extremum)
9 + #:removef #:extremum #:hash-table-keys)
10 10 (:export
11 11 ;; sloppy-focus-follows-mouse-mixin
12 12 #:sloppy-focus-follows-mouse-mixin
@@ -49,4 +49,14 @@
49 49 #:maximize-mixin #:maximizable-client #:client-maximized
50 50 #:maximized-axes #:toggle-maximize #:maximize-state-changed
51 51 ;; maximize-mixin using commands
52 - #:maximize-command #:vmaximize-command #:hmaximize-command))
52 + #:maximize-command #:vmaximize-command #:hmaximize-command
53 + ;; drawing helpers
54 + #:color #:drawable-text
55 + ;; modal-commands-mixin
56 + #:modal-commands-mixin
57 + #:modal-window #:modal-window-window #:modal-x #:modal-y
58 + #:draw-modal #:modal-key-press #:modal-button-press
59 + #:menu #:menu-prompt #:menu-items #:menu-input #:menu-selection
60 + #:menu-matches #:run-menu
61 + #:menu-font-name #:menu-colors
62 + #:exec-menu-command #:window-menu-command))
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/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))))))
modified clwm.asd
@@ -5,6 +5,7 @@
5 5 :depends-on ("alexandria"
6 6 "bordeaux-threads"
7 7 "clx"
8 + "sb-posix"
8 9 "serapeum"
9 10 "slynk"
10 11 "uiop")
@@ -27,5 +28,7 @@
27 28 (:file "alt-tab-ring-mixin")
28 29 (:file "screen-geometry-mixin")
29 30 (:file "pointer-placement-mixin")
30 - (:file "maximize-mixin")))
31 + (:file "maximize-mixin")
32 + (:file "drawing")
33 + (:file "modal-commands-mixin")))
31 34 (:file "clwm-user")))
modified example-init.lisp
@@ -4,7 +4,8 @@
4 4
5 5 (in-package #:clwm-user)
6 6
7 - (defclass my-wm (maximize-mixin
7 + (defclass my-wm (modal-commands-mixin
8 + maximize-mixin
8 9 pointer-placement-mixin
9 10 screen-geometry-mixin
10 11 alt-tab-ring-mixin
@@ -27,7 +28,9 @@
27 28 ((:mod-1 :shift :tab) . ,(cycle-backward :mod-1 :tab))
28 29 ((:control :mod-1 #\m) . maximize-command)
29 30 ((:control :mod-1 #\=) . vmaximize-command)
30 - ((:control :mod-1 :shift #\=) . hmaximize-command)))
31 + ((:control :mod-1 :shift #\=) . hmaximize-command)
32 + ((:mod-1 :shift #\/) . exec-menu-command)
33 + ((:mod-1 #\/) . window-menu-command)))
31 34
32 35 (defmethod mouse-bindings append ((wm my-wm))
33 36 '(((:mod-1 1) . bounded-drag-move)