at commit bfc9a230
| 1 | ;;;; common lisp window manager |
| 2 | ;;;; |
| 3 | ;;;; CLWM: extensible ICCCM-compliant do-nothing window manager |
| 4 | |
| 5 | (defpackage #:clwm |
| 6 | (:use #:cl) |
| 7 | (:import-from #:alexandria |
| 8 | #:ensure-gethash #:format-symbol) |
| 9 | (:import-from #:serapeum |
| 10 | #:standard/context) |
| 11 | (:export |
| 12 | ;; special variables |
| 13 | #:*the-wm* #:*wm-thread* |
| 14 | #:*debug-wm* #:*debug-event-handlers* |
| 15 | #:*print-all-events* #:*print-ignored-events* |
| 16 | #:*print-ignored-x-errors* |
| 17 | ;; running |
| 18 | #:run-wm #:restart-wm #:start-slynk |
| 19 | ;; wm |
| 20 | #:wm |
| 21 | #:display #:screen #:root #:clients #:running-p #:last-event-time |
| 22 | ;; client |
| 23 | #:client #:client-mixins #:client-class |
| 24 | #:client-window |
| 25 | #:client-normal-hints #:client-wm-hints #:client-protocols |
| 26 | #:pending-unmaps |
| 27 | ;; protocol |
| 28 | #:initialize-wm #:uninitialize-wm #:shutdown-wm #:become-the-wm |
| 29 | #:root-event-mask #:manage-all-windows #:set-focus-mode |
| 30 | #:set-cursor #:wm-event-loop #:handle-x-error |
| 31 | #:manage-window-p #:manage-window #:manage-client #:unmanage-client |
| 32 | #:send-protocol-message #:place-client #:show-client #:hide-client |
| 33 | #:withdraw-client #:close-client #:client-event-mask #:client-windows |
| 34 | #:focus-client #:client-geometry #:client-decoration-extents |
| 35 | #:configure-client #:apply-client-geometry #:constrain-client-size |
| 36 | #:send-configure-notify |
| 37 | #:handle-event #:event-client #:handle-client-event |
| 38 | #:handle-configure-request-geometry |
| 39 | #:handle-configure-request-stacking |
| 40 | #:find-client #:map-clients #:wm-state #:set-wm-state |
| 41 | #:configure-window #:apply-size-hints #:gravity-alignment |
| 42 | #:gravitate #:spawn)) |
| 43 | (in-package #:clwm) |
| 44 | |
| 45 | (defvar *the-wm* nil |
| 46 | "The running WM, if any.") |
| 47 | (defvar *wm-thread* nil |
| 48 | "The event loop thread started by RUN-WM.") |
| 49 | |
| 50 | (defvar *composed-classes* (make-hash-table :test 'equal)) |
| 51 | |
| 52 | (defparameter *debug-wm* nil |
| 53 | "Whether an error should enter the debugger.") |
| 54 | (defparameter *debug-event-handlers* nil |
| 55 | "Whether an error in an event handler should enter the debugger.") |
| 56 | (defparameter *print-all-events* nil |
| 57 | "Whether all X events should be printed.") |
| 58 | (defparameter *print-ignored-events* nil |
| 59 | "Whether otherwise-ignored X events should be printed.") |
| 60 | (defparameter *print-ignored-x-errors* nil |
| 61 | "Whether otherwise-ignored X errors should be printed.") |
| 62 | |
| 63 | ;;; some classes |
| 64 | |
| 65 | (defclass wm () |
| 66 | ((display :initarg :display :reader display |
| 67 | :documentation "The X server display.") |
| 68 | (screen :reader screen |
| 69 | :documentation "The X screen (we assume just one).") |
| 70 | (root :reader root |
| 71 | :documentation "The root window.") |
| 72 | (clients :initform (make-hash-table) :reader clients |
| 73 | :documentation "Drawable ID to client object map. |
| 74 | Client objects are allowed to recur under multiple IDs.") |
| 75 | (running :initform t :accessor running-p |
| 76 | :documentation "Flag used to end the event loop.") |
| 77 | (last-event-time :initform 0 :accessor last-event-time |
| 78 | :documentation |
| 79 | "The latest server timestamp seen on an event.") |
| 80 | (manager-window :initform nil :accessor manager-window |
| 81 | :documentation |
| 82 | "The window that owns the WM_Sn selection for us. |
| 83 | (ICCCM 4.3)") |
| 84 | (restart :initform nil :accessor restart-request |
| 85 | :documentation "Class to change into on restart. |
| 86 | NIL to stop, T to keep the same class."))) |
| 87 | |
| 88 | (defmethod initialize-instance :after ((wm wm) &key) |
| 89 | (with-slots (display screen root) wm |
| 90 | (setf screen (xlib:display-default-screen display) |
| 91 | root (xlib:screen-root screen)) |
| 92 | (setf (xlib:display-error-handler display) |
| 93 | (lambda (display error-key &rest keys) |
| 94 | (declare (ignore display)) |
| 95 | (apply #'handle-x-error wm error-key keys))))) |
| 96 | |
| 97 | (defclass client () |
| 98 | ((window :initarg :window :reader client-window |
| 99 | :documentation "The client's own window.") |
| 100 | (normal-hints :initform nil :accessor client-normal-hints |
| 101 | :documentation "The window's WM_NORMAL_HINTS.") |
| 102 | (wm-hints :initform nil :accessor client-wm-hints |
| 103 | :documentation "The window's WM_HINTS.") |
| 104 | (protocols :initform '() :accessor client-protocols |
| 105 | :documentation "The window's WM_PROTOCOLS.") |
| 106 | (pending-unmaps :initform 0 :accessor pending-unmaps |
| 107 | :documentation |
| 108 | "Unmaps the WM made that X hasn't reported yet."))) |
| 109 | |
| 110 | ;;; some gfs |
| 111 | |
| 112 | ;;; anonymous client classes |
| 113 | (defgeneric client-mixins (wm) |
| 114 | (:documentation |
| 115 | "Mixin classes that should be superclasses of the client class.") |
| 116 | (:method-combination append)) |
| 117 | (defgeneric client-class (wm) |
| 118 | (:documentation "The effective client class of the WM.") |
| 119 | (:method (wm) |
| 120 | (compose-class (client-mixins wm)))) |
| 121 | |
| 122 | ;;; initialization steps |
| 123 | (defgeneric initialize-wm (wm) |
| 124 | (:documentation "Carry out the initialization steps.")) |
| 125 | (defgeneric uninitialize-wm (wm) |
| 126 | (:documentation "Undo INITIALIZE-WM so it can run again. |
| 127 | (Possibly after changing class.)")) |
| 128 | (defgeneric shutdown-wm (wm) |
| 129 | (:documentation "Undo what should not outlive the WM.")) |
| 130 | (defgeneric become-the-wm (wm) |
| 131 | (:documentation "Acquire the substructure masks on the root window.")) |
| 132 | (defgeneric root-event-mask (wm) |
| 133 | (:documentation "Event masks the root should hold on the root window.") |
| 134 | (:method-combination append) |
| 135 | (:method :around (wm) |
| 136 | (apply #'xlib:make-event-mask (call-next-method)))) |
| 137 | (defgeneric manage-all-windows (wm) |
| 138 | (:documentation "Adopt all existing windows.")) |
| 139 | (defgeneric set-focus-mode (wm) |
| 140 | (:documentation "Set the X focus mode.")) |
| 141 | (defgeneric set-cursor (wm) |
| 142 | (:documentation "Set the root window cursor.")) |
| 143 | |
| 144 | ;;; managing windows |
| 145 | (defgeneric manage-window-p (wm window) |
| 146 | (:documentation "Whether to manage a window. |
| 147 | An unmanaged window has requests honored as though there were no WM.") |
| 148 | (:method-combination and)) |
| 149 | (defgeneric manage-window (wm window) |
| 150 | (:documentation "Create and hold a client object for a window.")) |
| 151 | (defgeneric manage-client (wm client) |
| 152 | (:documentation "Start managing a newly created client.")) |
| 153 | (defgeneric unmanage-client (wm client) |
| 154 | (:documentation "Stop managing a client.")) |
| 155 | (defgeneric show-client (wm client) |
| 156 | (:documentation "Map a client and put it in the Normal state.")) |
| 157 | (defgeneric hide-client (wm client) |
| 158 | (:documentation "Unmap a client and put it in the Iconic state.")) |
| 159 | (defgeneric withdraw-client (wm client) |
| 160 | (:documentation "Stop managing a client whose window was withdrawn.")) |
| 161 | (defgeneric place-client (wm client) |
| 162 | (:documentation |
| 163 | "Position a newly mapped client before it's first shown.")) |
| 164 | (defgeneric close-client (wm client) |
| 165 | (:documentation |
| 166 | "Ask a client to close its window if possible, or disconnect it |
| 167 | (ICCCM 4.2.8.1).")) |
| 168 | (defgeneric client-event-mask (wm client) |
| 169 | (:documentation |
| 170 | "Event masks the WM should hold on a client's window.") |
| 171 | (:method-combination append) |
| 172 | (:method :around (wm client) |
| 173 | (apply #'xlib:make-event-mask (call-next-method)))) |
| 174 | (defgeneric client-windows (wm client) |
| 175 | (:documentation "Windows that identify a client.") |
| 176 | (:method-combination append)) |
| 177 | (defgeneric focus-client (wm client) |
| 178 | (:documentation "Give a client the input focus.")) |
| 179 | |
| 180 | ;;; window geometry & configure requests |
| 181 | (defgeneric client-geometry (wm client) |
| 182 | (:documentation |
| 183 | "The client's x, y, width, and height in root coordinates.")) |
| 184 | (defgeneric client-decoration-extents (wm client) |
| 185 | (:documentation |
| 186 | "The thickness of the left, right, top, and bottom decorations.")) |
| 187 | (defgeneric configure-client |
| 188 | (wm client &key x y width height stack-mode sibling) |
| 189 | (:documentation |
| 190 | "Move, resize, or restack a client (by outer geometry).")) |
| 191 | (defgeneric apply-client-geometry |
| 192 | (wm client &key x y width height stack-mode sibling) |
| 193 | (:documentation |
| 194 | "Put a client at outer geometry that CONFIGURE-CLIENT has |
| 195 | already constrained.")) |
| 196 | (defgeneric constrain-client-size (wm client width height) |
| 197 | (:documentation |
| 198 | "The outer width and height closest to the given ones that complies |
| 199 | with the client's size hints.")) |
| 200 | (defgeneric send-configure-notify (wm client) |
| 201 | (:documentation |
| 202 | "Tell a client its window geometry (ICCCM 4.1.5).")) |
| 203 | (defgeneric handle-configure-request-geometry |
| 204 | (wm client &key x y width height border-width) |
| 205 | (:documentation |
| 206 | "Handle the geometry a client asked for in a configure request. |
| 207 | Values it didn't ask to change are present but NIL.")) |
| 208 | (defgeneric handle-configure-request-stacking |
| 209 | (wm client stack-mode sibling) |
| 210 | (:documentation |
| 211 | "Handle the stacking a client asked for in a configure request. |
| 212 | SIBLING may be a client or NIL.")) |
| 213 | |
| 214 | ;;; X error handling |
| 215 | (defgeneric handle-x-error (wm error-key &key &allow-other-keys) |
| 216 | (:documentation "Handle an X error. |
| 217 | (Returning rather than signalling counts as 'ignored'.)")) |
| 218 | |
| 219 | ;;; event loop |
| 220 | (defgeneric wm-event-loop (wm) |
| 221 | (:documentation "Loop to process X events.")) |
| 222 | |
| 223 | ;;; event handling |
| 224 | (defgeneric handle-event (wm event-key &key &allow-other-keys) |
| 225 | (:documentation "Handle a single event.") |
| 226 | (:method-combination standard/context) |
| 227 | (:method :before (wm event-key &rest e &key) |
| 228 | (when *print-all-events* |
| 229 | (format t "~a event:~% ~a~%" event-key e))) |
| 230 | (:method (wm event-key &rest e &key) |
| 231 | ;; try dispatch to client-based handler |
| 232 | (let ((client (apply #'event-client wm event-key e))) |
| 233 | (if client |
| 234 | (apply #'handle-client-event wm client event-key e) |
| 235 | (ignore-event event-key e))))) |
| 236 | (defgeneric event-client (wm event-key &key &allow-other-keys) |
| 237 | (:documentation "The client an event is about, if any.") |
| 238 | (:method (wm event-key &key window child) |
| 239 | ;; input events on the root will have the window in the "child" key |
| 240 | (or (and child (find-client wm child)) |
| 241 | (and window (find-client wm window))))) |
| 242 | (defgeneric handle-client-event |
| 243 | (wm client event-key &key &allow-other-keys) |
| 244 | (:documentation |
| 245 | "Handle an event about a client that HANDLE-EVENT didn't.") |
| 246 | (:method (wm client event-key &rest e &key) |
| 247 | (ignore-event event-key e))) |
| 248 | |
| 249 | ;;; some helpers |
| 250 | |
| 251 | ;;; anonymous client classes |
| 252 | (defun compose-class (names) |
| 253 | "Compose a class from mixins, caching it in *COMPOSED-CLASSES*." |
| 254 | (let ((names (remove-duplicates names :from-end t))) |
| 255 | (values |
| 256 | (ensure-gethash names *composed-classes* |
| 257 | (if (null (rest names)) |
| 258 | (find-class (first names)) |
| 259 | (make-instance |
| 260 | 'standard-class |
| 261 | :name (format-symbol nil "~{~a~^+~}" names) |
| 262 | :direct-superclasses |
| 263 | (mapcar #'find-class names))))))) |
| 264 | |
| 265 | ;;; clients |
| 266 | (defun find-client (wm window) |
| 267 | "Find the client associated with a window if it exists." |
| 268 | (values (gethash (xlib:drawable-id window) (clients wm)))) |
| 269 | (defun map-clients (function wm) |
| 270 | "Call a function on each client only once. |
| 271 | (No relation to X11 mapping.)" |
| 272 | (maphash (lambda (id client) |
| 273 | (when (= id (xlib:drawable-id (client-window client))) |
| 274 | (funcall function client))) |
| 275 | (clients wm))) |
| 276 | |
| 277 | ;;; window states |
| 278 | (defun wm-state (window) |
| 279 | "A window's ICCCM WM_STATE (Normal, Iconic, Withdrawn)." |
| 280 | (case (first (xlib:get-property window :WM_STATE)) |
| 281 | (1 :normal) |
| 282 | (3 :iconic) |
| 283 | (t :withdrawn))) |
| 284 | (defun set-wm-state (window state) |
| 285 | "Set a window's ICCCM WM_STATE to :normal, :iconic, or :withdrawn." |
| 286 | (if (eql state :withdrawn) |
| 287 | (xlib:delete-property window :WM_STATE) |
| 288 | (let ((data (list (ecase state (:normal 1) (:iconic 3)) 0))) |
| 289 | (xlib:change-property |
| 290 | window :WM_STATE data :WM_STATE 32)))) |
| 291 | |
| 292 | ;;; factored out window configuration actions |
| 293 | (defun configure-window |
| 294 | (window &key x y width height border-width stack-mode sibling) |
| 295 | "Change a window's geometry and stacking. |
| 296 | Unsupplied values remain unchanged." |
| 297 | (xlib:with-state (window) |
| 298 | (when x (setf (xlib:drawable-x window) x)) |
| 299 | (when y (setf (xlib:drawable-y window) y)) |
| 300 | (when width (setf (xlib:drawable-width window) width)) |
| 301 | (when height (setf (xlib:drawable-height window) height)) |
| 302 | (when border-width |
| 303 | (setf (xlib:drawable-border-width window) border-width)) |
| 304 | (when stack-mode |
| 305 | (setf (xlib:window-priority window sibling) stack-mode)))) |
| 306 | (defun apply-size-hints (hints width height) |
| 307 | "Constrain a window's inner size by its WM_NORMAL_HINTS |
| 308 | (ICCCM 4.1.2.3)." |
| 309 | (let* ((base-width (or (xlib:wm-size-hints-base-width hints) |
| 310 | (xlib:wm-size-hints-min-width hints) |
| 311 | 0)) |
| 312 | (base-height (or (xlib:wm-size-hints-base-height hints) |
| 313 | (xlib:wm-size-hints-min-height hints) |
| 314 | 0)) |
| 315 | (min-width (or (xlib:wm-size-hints-min-width hints) |
| 316 | base-width)) |
| 317 | (min-height (or (xlib:wm-size-hints-min-height hints) |
| 318 | base-height)) |
| 319 | (max-width (or (xlib:wm-size-hints-max-width hints) |
| 320 | width)) |
| 321 | (max-height (or (xlib:wm-size-hints-max-height hints) |
| 322 | height)) |
| 323 | (width-inc (max 1 (or (xlib:wm-size-hints-width-inc hints) |
| 324 | 1))) |
| 325 | (height-inc (max 1 (or (xlib:wm-size-hints-height-inc hints) |
| 326 | 1))) |
| 327 | (min-aspect (xlib:wm-size-hints-min-aspect hints)) |
| 328 | (max-aspect (xlib:wm-size-hints-max-aspect hints))) |
| 329 | ;; aspect limits are on size beyond base; shrink whichever |
| 330 | ;; dimension is too big |
| 331 | (let ((aspect-width (- width base-width)) |
| 332 | (aspect-height (- height base-height))) |
| 333 | (when (and (plusp aspect-width) (plusp aspect-height)) |
| 334 | (cond ((and max-aspect |
| 335 | (> (/ aspect-width aspect-height) max-aspect)) |
| 336 | (setf width |
| 337 | (+ base-width |
| 338 | (round (* aspect-height max-aspect))))) |
| 339 | ((and min-aspect (plusp min-aspect) |
| 340 | (< (/ aspect-width aspect-height) min-aspect)) |
| 341 | (setf height |
| 342 | (+ base-height |
| 343 | (round aspect-width min-aspect))))))) |
| 344 | (flet ((constrain (size base inc min max) |
| 345 | ;; oh god |
| 346 | (max 1 |
| 347 | min |
| 348 | (min max |
| 349 | (+ base (* inc (floor (- size base) inc))))))) |
| 350 | (values (constrain width base-width width-inc |
| 351 | min-width max-width) |
| 352 | (constrain height base-height height-inc |
| 353 | min-height max-height))))) |
| 354 | (defun gravity-alignment (gravity axis) |
| 355 | "Where on the :x or :y axis a window's gravity point is." |
| 356 | (if (eql gravity :static) :static |
| 357 | (let ((column (case gravity |
| 358 | ((:north-east :east :south-east) :end) |
| 359 | ((:north :center :south) :center) |
| 360 | (t :start))) |
| 361 | (row (case gravity |
| 362 | ((:south-west :south :south-east) :end) |
| 363 | ((:west :center :east) :center) |
| 364 | (t :start)))) |
| 365 | (ecase axis (:x column) (:y row))))) |
| 366 | (defun gravitate |
| 367 | (alignment position size border outer-size start-extent) |
| 368 | "The outer position for a window that requested POSITION, such that |
| 369 | the outer rectangle's reference point is where the window's would be. |
| 370 | SIZE excludes BORDER, START-EXTENT is the left or top extent." |
| 371 | (let ((window-size (+ size border border))) |
| 372 | (ecase alignment |
| 373 | (:start position) |
| 374 | (:center (+ position (floor (- window-size outer-size) 2))) |
| 375 | (:end (+ position (- window-size outer-size))) |
| 376 | (:static (+ position border (- start-extent)))))) |
| 377 | |
| 378 | ;;; other factored-out helpers |
| 379 | (defun manager-selection (wm) |
| 380 | "Helper to make and intern the manager selection keyword |
| 381 | (i.e. :WM_S0)." |
| 382 | (format-symbol |
| 383 | :keyword "WM_S~d" |
| 384 | (position (screen wm) (xlib:display-roots (display wm))))) |
| 385 | (defun read-client-property (client atom) |
| 386 | "Read and cache window properties. |
| 387 | T if anything was updated, else NIL." |
| 388 | (let ((window (client-window client))) |
| 389 | (case atom |
| 390 | (:WM_NORMAL_HINTS |
| 391 | (setf (client-normal-hints client) |
| 392 | (xlib:wm-normal-hints window))) |
| 393 | (:WM_HINTS |
| 394 | (setf (client-wm-hints client) |
| 395 | (xlib:wm-hints window))) |
| 396 | (:WM_PROTOCOLS |
| 397 | (setf (client-protocols client) |
| 398 | (xlib:wm-protocols window))) |
| 399 | (t (return-from read-client-property nil))) |
| 400 | t)) |
| 401 | (defun send-protocol-message (wm client protocol) |
| 402 | "Send a client a WM_PROTOCOLS message, like :WM_DELETE_WINDOW." |
| 403 | (let ((window (client-window client))) |
| 404 | (xlib:send-event window :client-message '() |
| 405 | :window window :type :WM_PROTOCOLS :format 32 |
| 406 | :data |
| 407 | (list (xlib:intern-atom (display wm) protocol) |
| 408 | (last-event-time wm) 0 0 0)))) |
| 409 | |
| 410 | ;;; event helpers |
| 411 | (defun event-handler (wm) |
| 412 | "A CLX event handler that hands one event to the WM." |
| 413 | (lambda (&rest e &key event-key &allow-other-keys) |
| 414 | (apply #'handle-event wm event-key e) |
| 415 | t)) |
| 416 | (defun ignore-event (event-key e) |
| 417 | "Do nothing with an event, possibly printing it." |
| 418 | (when (and *print-ignored-events* (not *print-all-events*)) |
| 419 | (format t "ignoring ~a event:~% ~a~%" event-key e))) |
| 420 | (defun process-pending-events (wm) |
| 421 | "Handle the events X has already sent, but don't wait for more." |
| 422 | (xlib:display-finish-output (display wm)) |
| 423 | (loop with handler = (event-handler wm) |
| 424 | while (xlib:process-event |
| 425 | (display wm) |
| 426 | :handler handler :timeout 0 :discard-p t))) |
| 427 | |
| 428 | ;;; executable wm actions |
| 429 | (defun spawn (wm program &rest args) |
| 430 | "Run a program on the WM's display." |
| 431 | (uiop:launch-program |
| 432 | (list* "env" |
| 433 | (format nil "DISPLAY=~a:~d" |
| 434 | (xlib:display-host (display wm)) |
| 435 | (xlib:display-display (display wm))) |
| 436 | program args))) |
| 437 | |
| 438 | ;;; some methods |
| 439 | |
| 440 | ;;; anonymous client classes |
| 441 | (defmethod client-mixins append ((wm wm)) '(client)) |
| 442 | |
| 443 | ;;; initialization steps |
| 444 | (defmethod initialize-wm ((wm wm)) |
| 445 | (become-the-wm wm) |
| 446 | (manage-all-windows wm) |
| 447 | (set-focus-mode wm) |
| 448 | (set-cursor wm)) |
| 449 | (defmethod uninitialize-wm ((wm wm)) |
| 450 | (process-pending-events wm) |
| 451 | ;; todo: i think there's an alexandria idiom for this |
| 452 | (let ((clients '())) |
| 453 | (map-clients (lambda (client) (push client clients)) wm) |
| 454 | (dolist (client clients) |
| 455 | (unmanage-client wm client))) |
| 456 | ;; eat the events unmanaging caused above |
| 457 | (process-pending-events wm)) |
| 458 | (defmethod shutdown-wm ((wm wm)) |
| 459 | (set-focus-mode wm) |
| 460 | (xlib:display-finish-output (display wm))) |
| 461 | (defmethod become-the-wm ((wm wm)) |
| 462 | (handler-case |
| 463 | (progn |
| 464 | ;; this setf is how we become the WM to X. |
| 465 | (setf (xlib:window-event-mask (root wm)) (root-event-mask wm)) |
| 466 | ;; this does a sync. useful at the repl! |
| 467 | (xlib:display-finish-output (display wm))) |
| 468 | ;; X only allows one process to hold the substructure redirect mask |
| 469 | ;; that's what makes it "the" WM to X. |
| 470 | (xlib:access-error () |
| 471 | (error "Can't steal from already running WM"))) |
| 472 | ;; to ICCCM clients, however, |
| 473 | ;; the WM is whoever holds the WM_Sn selection (ICCCM 2.8). |
| 474 | ;; we need to make our own window to hold it. |
| 475 | (unless (manager-window wm) |
| 476 | (let ((display (display wm)) |
| 477 | (window (xlib:create-window :parent (root wm) :x -1 :y -1 |
| 478 | :width 1 :height 1 |
| 479 | :override-redirect :on))) |
| 480 | (setf (xlib:selection-owner display (manager-selection wm)) window) |
| 481 | (if (xlib:window-equal window (xlib:selection-owner |
| 482 | display (manager-selection wm))) |
| 483 | (setf (manager-window wm) window) |
| 484 | (error "Couldn't take the ~a selection" |
| 485 | (manager-selection wm))) |
| 486 | (xlib:send-event (root wm) :client-message '(:structure-notify) |
| 487 | :window (root wm) :type :MANAGER :format 32 |
| 488 | :data (list 0 (xlib:intern-atom |
| 489 | display (manager-selection wm)) |
| 490 | (xlib:window-id window) 0 0))))) |
| 491 | (defmethod root-event-mask append ((wm wm)) |
| 492 | '(:substructure-redirect :substructure-notify)) |
| 493 | (defmethod manage-all-windows ((wm wm)) |
| 494 | (xlib:with-server-grabbed ((display wm)) |
| 495 | (dolist (window (xlib:query-tree (root wm))) |
| 496 | (when (manage-window-p wm window) |
| 497 | (cond ((eql (xlib:window-map-state window) :viewable) |
| 498 | (show-client wm (manage-window wm window))) |
| 499 | ;; hidden by a previous WM, keep it that way |
| 500 | ((eql (wm-state window) :iconic) |
| 501 | (hide-client wm (manage-window wm window)))))))) |
| 502 | ;; a little beyond the minimal WM behavior, but consistently resetting |
| 503 | ;; to the X default avoids surprises |
| 504 | (defmethod set-focus-mode ((wm wm)) |
| 505 | (xlib:set-input-focus (display wm) :pointer-root :pointer-root)) |
| 506 | ;; a little beyond the minimal WM behavior, but the presence of the |
| 507 | ;; default X cursor is universally interpreted as "something went wrong" |
| 508 | (defmethod set-cursor ((wm wm)) |
| 509 | (let* ((font (xlib:open-font (display wm) "cursor")) |
| 510 | (cursor |
| 511 | (xlib:create-glyph-cursor |
| 512 | :source-font font :source-char 68 |
| 513 | :mask-font font :mask-char 69 |
| 514 | :foreground (xlib:make-color :red 0 :green 0 :blue 0) |
| 515 | :background (xlib:make-color :red 1 :green 1 :blue 1)))) |
| 516 | (setf (xlib:window-cursor (root wm)) cursor))) |
| 517 | |
| 518 | ;;; managing windows |
| 519 | (defmethod manage-window ((wm wm) window) |
| 520 | (or (find-client wm window) |
| 521 | (let ((client (make-instance (client-class wm) :window window))) |
| 522 | (manage-client wm client) |
| 523 | client))) |
| 524 | (defmethod manage-window-p and ((wm wm) window) |
| 525 | (eql (xlib:window-override-redirect window) :off)) |
| 526 | (defmethod manage-client ((wm wm) (client client)) |
| 527 | (setf (xlib:window-event-mask (client-window client)) |
| 528 | (client-event-mask wm client)) |
| 529 | (dolist (atom '(:WM_NORMAL_HINTS :WM_HINTS :WM_PROTOCOLS)) |
| 530 | (read-client-property client atom)) |
| 531 | (dolist (window (client-windows wm client)) |
| 532 | (setf (gethash (xlib:drawable-id window) (clients wm)) client))) |
| 533 | (defmethod unmanage-client ((wm wm) (client client)) |
| 534 | (dolist (window (client-windows wm client)) |
| 535 | (remhash (xlib:drawable-id window) (clients wm)))) |
| 536 | (defmethod show-client ((wm wm) (client client)) |
| 537 | (set-wm-state (client-window client) :normal) |
| 538 | (xlib:map-window (client-window client))) |
| 539 | (defmethod hide-client ((wm wm) (client client)) |
| 540 | (let ((window (client-window client))) |
| 541 | (unless (eql (xlib:window-map-state window) :unmapped) |
| 542 | (incf (pending-unmaps client)) |
| 543 | (xlib:unmap-window window)) |
| 544 | (set-wm-state window :iconic))) |
| 545 | (defmethod withdraw-client ((wm wm) (client client)) |
| 546 | (let ((window (client-window client))) |
| 547 | (setf (xlib:window-event-mask window) 0) |
| 548 | (set-wm-state window :withdrawn)) |
| 549 | (unmanage-client wm client)) |
| 550 | (defmethod place-client ((wm wm) (client client)) |
| 551 | nil) |
| 552 | (defmethod close-client ((wm wm) (client client)) |
| 553 | (if (member :WM_DELETE_WINDOW (client-protocols client)) |
| 554 | (send-protocol-message wm client :WM_DELETE_WINDOW) |
| 555 | (xlib:kill-client (display wm) |
| 556 | (xlib:window-id (client-window client))))) |
| 557 | (defmethod client-event-mask append ((wm wm) (client client)) |
| 558 | '(:property-change)) |
| 559 | (defmethod client-windows append ((wm wm) (client client)) |
| 560 | (list (client-window client))) |
| 561 | ;; per ICCCM 4.1.7, the input hint can say not to set the input focus, |
| 562 | ;; and we send WM_TAKE_FOCUS if supported |
| 563 | (defmethod focus-client ((wm wm) (client client)) |
| 564 | (let ((hints (client-wm-hints client))) |
| 565 | (unless (and hints (eql (xlib:wm-hints-input hints) :off)) |
| 566 | (xlib:set-input-focus (display wm) (client-window client) |
| 567 | :pointer-root)) |
| 568 | (when (member :WM_TAKE_FOCUS (client-protocols client)) |
| 569 | (send-protocol-message wm client :WM_TAKE_FOCUS)))) |
| 570 | |
| 571 | ;;; window geometry & configure requests |
| 572 | (defmethod client-geometry ((wm wm) (client client)) |
| 573 | (let ((window (client-window client))) |
| 574 | (xlib:with-state (window) |
| 575 | (multiple-value-bind (left top right bottom) |
| 576 | (client-decoration-extents wm client) |
| 577 | (values (xlib:drawable-x window) |
| 578 | (xlib:drawable-y window) |
| 579 | (+ (xlib:drawable-width window) left right) |
| 580 | (+ (xlib:drawable-height window) top bottom)))))) |
| 581 | (defmethod client-decoration-extents ((wm wm) (client client)) |
| 582 | (let ((border (xlib:drawable-border-width (client-window client)))) |
| 583 | (values border border border border))) |
| 584 | (defmethod configure-client ((wm wm) (client client) |
| 585 | &key x y width height stack-mode sibling) |
| 586 | (when (or width height) |
| 587 | (multiple-value-bind (client-x client-y client-width client-height) |
| 588 | (client-geometry wm client) |
| 589 | (declare (ignore client-x client-y)) |
| 590 | (multiple-value-setq (width height) |
| 591 | (constrain-client-size wm client |
| 592 | (or width client-width) |
| 593 | (or height client-height))))) |
| 594 | (apply-client-geometry wm client :x x :y y :width width :height height |
| 595 | :stack-mode stack-mode :sibling sibling)) |
| 596 | (defmethod apply-client-geometry ((wm wm) (client client) |
| 597 | &key x y width height |
| 598 | stack-mode sibling) |
| 599 | (multiple-value-bind (left top right bottom) |
| 600 | (client-decoration-extents wm client) |
| 601 | (configure-window (client-window client) |
| 602 | :x x :y y |
| 603 | :width (and width (max 1 (- width left right))) |
| 604 | :height (and height (max 1 (- height top bottom))) |
| 605 | :stack-mode stack-mode |
| 606 | :sibling (and sibling (client-window sibling))))) |
| 607 | (defmethod constrain-client-size ((wm wm) (client client) width height) |
| 608 | (let ((hints (client-normal-hints client))) |
| 609 | (if (null hints) (values width height) |
| 610 | (multiple-value-bind (left top right bottom) |
| 611 | (client-decoration-extents wm client) |
| 612 | (multiple-value-bind (inner-width inner-height) |
| 613 | (apply-size-hints hints |
| 614 | (- width left right) |
| 615 | (- height top bottom)) |
| 616 | (values (+ inner-width left right) |
| 617 | (+ inner-height top bottom))))))) |
| 618 | (defmethod send-configure-notify ((wm wm) (client client)) |
| 619 | (let ((window (client-window client))) |
| 620 | (xlib:with-state (window) |
| 621 | (let ((border (xlib:drawable-border-width window))) |
| 622 | (multiple-value-bind (x y) |
| 623 | (xlib:translate-coordinates window 0 0 (root wm)) |
| 624 | (xlib:send-event window :configure-notify '(:structure-notify) |
| 625 | :event-window window :window window |
| 626 | :x (- x border) :y (- y border) |
| 627 | :width (xlib:drawable-width window) |
| 628 | :height (xlib:drawable-height window) |
| 629 | :border-width border |
| 630 | :above-sibling nil |
| 631 | :override-redirect-p nil)))))) |
| 632 | ;; these are factored from the configure-request event handler |
| 633 | ;; could probably be dissolved into the above |
| 634 | (defmethod handle-configure-request-geometry |
| 635 | ((wm wm) (client client) &key x y width height border-width) |
| 636 | (let ((window (client-window client))) |
| 637 | (when border-width |
| 638 | (setf (xlib:drawable-border-width window) border-width)) |
| 639 | (multiple-value-bind (left top right bottom) |
| 640 | (client-decoration-extents wm client) |
| 641 | (let* ((border (xlib:drawable-border-width window)) |
| 642 | (inner-width (or width (xlib:drawable-width window))) |
| 643 | (inner-height (or height (xlib:drawable-height window))) |
| 644 | (outer-width (+ inner-width left right)) |
| 645 | (outer-height (+ inner-height top bottom)) |
| 646 | (hints (client-normal-hints client)) |
| 647 | (gravity (or (and |
| 648 | hints (xlib:wm-size-hints-win-gravity hints)) |
| 649 | :north-west))) |
| 650 | (configure-client |
| 651 | wm client |
| 652 | :x (and x (gravitate (gravity-alignment gravity :x) |
| 653 | x inner-width border outer-width left)) |
| 654 | :y (and y (gravitate (gravity-alignment gravity :y) |
| 655 | y inner-height border outer-height top)) |
| 656 | :width (and width outer-width) |
| 657 | :height (and height outer-height)))))) |
| 658 | (defmethod handle-configure-request-stacking |
| 659 | ((wm wm) (client client) stack-mode sibling) |
| 660 | (configure-client wm client :stack-mode stack-mode :sibling sibling)) |
| 661 | |
| 662 | ;;; X error handling |
| 663 | (defmethod handle-x-error ((wm wm) error-key &rest keys |
| 664 | &key asynchronous major &allow-other-keys) |
| 665 | ;; ignore some common unimportant async errors |
| 666 | (if (and asynchronous |
| 667 | (or (member error-key |
| 668 | '(xlib:window-error xlib:drawable-error)) |
| 669 | (and (eql error-key 'xlib:match-error) (eql major 42)))) |
| 670 | (when *print-ignored-x-errors* |
| 671 | (format *error-output* "ignoring ~a:~% ~a ~%" error-key keys)) |
| 672 | (apply #'xlib:default-error-handler (display wm) error-key keys))) |
| 673 | |
| 674 | ;;; event loop |
| 675 | (defmethod wm-event-loop ((wm wm)) |
| 676 | (loop with handler = (event-handler wm) |
| 677 | while (running-p wm) |
| 678 | do (xlib:process-event |
| 679 | (display wm) |
| 680 | :handler handler :timeout 1 :discard-p t))) |
| 681 | |
| 682 | ;;; event handling prelude |
| 683 | |
| 684 | (defmethod handle-event :context ((wm wm) event-key &key time) |
| 685 | (when time |
| 686 | (setf (last-event-time wm) time)) |
| 687 | (with-simple-restart (skip-event "skipping ~a event" event-key) |
| 688 | (handler-bind ((error (lambda (condition) |
| 689 | (when *debug-event-handlers* |
| 690 | (invoke-debugger condition)) |
| 691 | (format *error-output* |
| 692 | "~a: ~a" event-key condition) |
| 693 | (invoke-restart 'skip-event)))) |
| 694 | (call-next-method)))) |
| 695 | |
| 696 | ;;; some event handlers |
| 697 | |
| 698 | ;; intercept another WM taking over by taking the ICCCM WM selection |
| 699 | ;; and shut ourselves down |
| 700 | (defmethod handle-event |
| 701 | ((wm wm) (event-key (eql :selection-clear)) &key window selection) |
| 702 | (if (and (eql selection (manager-selection wm)) |
| 703 | (manager-window wm) |
| 704 | (xlib:window-equal window (manager-window wm))) |
| 705 | (progn |
| 706 | (format *error-output* "~&clwm: replaced by another WM~%") |
| 707 | (setf (running-p wm) nil)) |
| 708 | (call-next-method))) |
| 709 | |
| 710 | ;; if we shouldn't manage it, just let its map request through |
| 711 | (defmethod handle-event |
| 712 | ((wm wm) (event-key (eql :map-request)) &key window) |
| 713 | (let ((new (not (find-client wm window)))) |
| 714 | (if (and new (not (manage-window-p wm window))) |
| 715 | (xlib:map-window window) |
| 716 | (let ((client (manage-window wm window))) |
| 717 | (when new |
| 718 | (place-client wm client)) |
| 719 | (let ((hints (client-wm-hints client))) |
| 720 | (if (and new hints |
| 721 | (eql (xlib:wm-hints-initial-state hints) :iconic)) |
| 722 | (hide-client wm client) |
| 723 | (show-client wm client))))))) |
| 724 | |
| 725 | (defmethod handle-client-event |
| 726 | ((wm wm) (client client) (event-key (eql :destroy-notify)) |
| 727 | &key window) |
| 728 | ;; only unmanage if it's the real window |
| 729 | ;; (i.e., not a frame we made in REPARENTING-MIXIN or something) |
| 730 | (if (xlib:window-equal window (client-window client)) |
| 731 | (unmanage-client wm client) |
| 732 | (call-next-method))) |
| 733 | |
| 734 | ;; per ICCCM 4.1.4, if we get notified of an unmap we didn't make |
| 735 | ;; (could be real or synthetic), this should be handled as a withdraw |
| 736 | (defmethod handle-client-event |
| 737 | ((wm wm) (client client) (event-key (eql :unmap-notify)) |
| 738 | &key event-window window send-event-p) |
| 739 | (cond ((or (xlib:window-equal event-window window) |
| 740 | ;; could potentially be a reparenting frame or something |
| 741 | (not (xlib:window-equal window (client-window client)))) |
| 742 | (call-next-method)) |
| 743 | ((and (plusp (pending-unmaps client)) (not send-event-p)) |
| 744 | (decf (pending-unmaps client))) |
| 745 | (t |
| 746 | (withdraw-client wm client)))) |
| 747 | |
| 748 | ;; ICCCM 4.1.4, clients can request iconification |
| 749 | (defmethod handle-client-event |
| 750 | ((wm wm) (client client) (event-key (eql :client-message)) |
| 751 | &key type data) |
| 752 | (if (and (eql type :WM_CHANGE_STATE) (eql (aref data 0) 3)) |
| 753 | (hide-client wm client) |
| 754 | (call-next-method))) |
| 755 | |
| 756 | (defmethod handle-client-event |
| 757 | ((wm wm) (client client) (event-key (eql :property-notify)) |
| 758 | &key atom) |
| 759 | (unless (read-client-property client atom) |
| 760 | (call-next-method))) |
| 761 | |
| 762 | (defmethod handle-event ((wm wm) (event-key (eql :configure-request)) |
| 763 | &key window |
| 764 | x y width height |
| 765 | border-width stack-mode above-sibling |
| 766 | value-mask) |
| 767 | (flet ((requested (bit value) (and (logbitp bit value-mask) value))) |
| 768 | (let ((client (find-client wm window)) |
| 769 | (x (requested 0 x)) |
| 770 | (y (requested 1 y)) |
| 771 | (width (requested 2 width)) |
| 772 | (height (requested 3 height)) |
| 773 | (border-width (requested 4 border-width)) |
| 774 | (sibling (requested 5 above-sibling)) |
| 775 | (stack-mode (requested 6 stack-mode))) |
| 776 | (if client |
| 777 | (progn |
| 778 | (handle-configure-request-geometry |
| 779 | wm client :x x :y y :width width :height height |
| 780 | :border-width border-width) |
| 781 | (when stack-mode |
| 782 | (handle-configure-request-stacking |
| 783 | wm client stack-mode |
| 784 | (and sibling (find-client wm sibling)))) |
| 785 | (send-configure-notify wm client)) |
| 786 | ;; we're not managing it, just pass through its request |
| 787 | (configure-window |
| 788 | window :x x :y y :width width :height height |
| 789 | :border-width border-width |
| 790 | :stack-mode stack-mode :sibling sibling))))) |
| 791 | |
| 792 | ;;; running the WM |
| 793 | |
| 794 | (defun run-wm (class &key display-name) |
| 795 | "Run a WM of class CLASS on a display (default $DISPLAY) in a thread." |
| 796 | (setf *wm-thread* |
| 797 | (bt:make-thread |
| 798 | (lambda () (run-wm-thread class display-name)) |
| 799 | :name (format nil "wm thread for ~a" |
| 800 | (or display-name "default display"))))) |
| 801 | |
| 802 | (defun run-wm-thread (class display-name) |
| 803 | "Run the WM thread, handling the event loop and restarts." |
| 804 | (let* ((display (xlib:open-default-display display-name)) |
| 805 | (wm (make-instance class :display display)) |
| 806 | (outcome :stopped)) |
| 807 | (setf *the-wm* wm) |
| 808 | (unwind-protect |
| 809 | (with-simple-restart (stop-wm "Stop the WM.") |
| 810 | (handler-bind |
| 811 | ((xlib:closed-display |
| 812 | (lambda (condition) |
| 813 | (declare (ignore condition)) |
| 814 | (setf outcome :disconnected) |
| 815 | (format *error-output* |
| 816 | "~&clwm: lost connection to ~a~%" |
| 817 | (or display-name "default display")) |
| 818 | (invoke-restart 'stop-wm))) |
| 819 | (error |
| 820 | (lambda (condition) |
| 821 | (when *debug-wm* |
| 822 | (invoke-debugger condition)) |
| 823 | (format *error-output* |
| 824 | "~&clwm: stopping on error: ~a~%" |
| 825 | condition) |
| 826 | (uiop:print-backtrace :stream *error-output* |
| 827 | :count 30) |
| 828 | (setf outcome :error) |
| 829 | (invoke-restart 'stop-wm)))) |
| 830 | (loop (initialize-wm wm) |
| 831 | (wm-event-loop wm) |
| 832 | (let ((request (shiftf (restart-request wm) nil))) |
| 833 | (unless request (return)) |
| 834 | (uninitialize-wm wm) |
| 835 | (unless (eql request t) |
| 836 | (change-class wm request)) |
| 837 | (setf (running-p wm) t))))) |
| 838 | (let ((connected (not (eql outcome :disconnected)))) |
| 839 | (unwind-protect (when connected (shutdown-wm wm)) |
| 840 | (xlib:close-display display :abort (not connected)) |
| 841 | (setf *the-wm* nil)))) |
| 842 | outcome)) |
| 843 | |
| 844 | (defun restart-wm (&optional class (wm *the-wm*)) |
| 845 | "Restart the WM destructively (state isn't kept. |
| 846 | Optionally CHANGE-CLASS to CLASS. |
| 847 | Requests it of the WM thread, so thread-safe to call." |
| 848 | (unless wm |
| 849 | (error "No WM is running.")) |
| 850 | (setf (restart-request wm) (or class t) |
| 851 | (running-p wm) nil) |
| 852 | wm) |
| 853 | |
| 854 | (defun start-slynk (&key (port 4005)) |
| 855 | "Helper to start a Slynk server." |
| 856 | (slynk:create-server :port port :dont-close t)) |