Compare

bfc9a230fa1cb8036be7d51096fbfa7237914288..6b56d7d8fd47e64730d20c560eb1149990aff829

clwm-ext: ewmh-mixin

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

Browse files
5 files changed, +318 −7
modified clwm-ext.lisp
@@ -18,6 +18,7 @@
18 18 #:interaction-mixin #:current-interaction #:interaction
19 19 #:interaction-client #:start-interaction #:stop-interaction
20 20 #:interaction-event #:stop-current-interaction
21 + #:abort-current-interaction #:with-interaction-abort-on-error
21 22 ;; bindings
22 23 #:*lock-modifiers* #:*key-names*
23 24 ;; keybinds-mixin
@@ -59,4 +60,6 @@
59 60 #:menu #:menu-prompt #:menu-items #:menu-input #:menu-selection
60 61 #:menu-matches #:run-menu
61 62 #:menu-font-name #:menu-colors
62 - #:exec-menu-command #:window-menu-command))
63 + #:exec-menu-command #:window-menu-command
64 + ;; ewmh-mixin
65 + #:ewmh-mixin #:ewmh-supported))
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))
modified clwm-ext/interaction-mixin.lisp
@@ -41,12 +41,40 @@ Expects INTERACTION-MIXIN."
41 41 (when (eql interaction (current-interaction wm))
42 42 (setf (current-interaction wm) nil)))
43 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 +
44 64 (defmethod handle-event :around
45 65 ((wm interaction-mixin) event-key &rest e &key)
46 - (let ((interaction (current-interaction wm)))
47 - (unless (and interaction
48 - (apply #'interaction-event wm interaction event-key e))
49 - (call-next-method))))
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)))
50 78
51 79 (defmethod unmanage-client :before ((wm interaction-mixin) client)
52 80 (let ((interaction (current-interaction wm)))
modified clwm.asd
@@ -30,5 +30,6 @@
30 30 (:file "pointer-placement-mixin")
31 31 (:file "maximize-mixin")
32 32 (:file "drawing")
33 - (:file "modal-commands-mixin")))
33 + (:file "modal-commands-mixin")
34 + (:file "ewmh-mixin")))
34 35 (: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 (modal-commands-mixin
7 + (defclass my-wm (ewmh-mixin
8 + modal-commands-mixin
8 9 maximize-mixin
9 10 pointer-placement-mixin
10 11 screen-geometry-mixin