Compare
7f2aa7db783bf4b1f5559f054758b3cded685bcd..84db1d645616d0023971f959191a4b469c07c5b3
clwm-ext: pointer-placement-mixin
Raymond E. Pasco <ray@ameretat.dev> · 2026-10-27 10:57 UTC
6 files changed,
+217
−6
- clwm-ext.lisp
- clwm-ext/commands.lisp
- clwm-ext/pointer-placement-mixin.lisp
- clwm-ext/screen-geometry-mixin.lisp
- clwm.asd
- example-init.lisp
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) | |
| 9 | + | #:removef #:extremum) | |
| 10 | 10 | (:export | |
| 11 | 11 | ;; sloppy-focus-follows-mouse-mixin | |
| 12 | 12 | #:sloppy-focus-follows-mouse-mixin | |
| @@ -36,4 +36,12 @@ | |||
| 36 | 36 | #:warp-pointer-mixin #:warp-pointer-client #:client-pointer | |
| 37 | 37 | ;; alt-tab-ring-mixin | |
| 38 | 38 | #:alt-tab-ring-mixin #:client-ring #:ring-cycle | |
| 39 | - | #:cycle-forward #:cycle-backward)) | |
| 39 | + | #:cycle-forward #:cycle-backward | |
| 40 | + | ;; screen-geometry-mixin | |
| 41 | + | #:screen-geometry-mixin #:screen-areas #:reserved-edges | |
| 42 | + | #:screen-geometry-changed #:area-at #:work-area | |
| 43 | + | #:geometry-bounds #:constrain-move #:constrain-resize | |
| 44 | + | ;; screen-geometry using commands | |
| 45 | + | #:bounded-drag-move #:bounded-drag-resize | |
| 46 | + | ;; pointer-placement-mixin | |
| 47 | + | #:pointer-placement-mixin)) | |
modified clwm-ext/commands.lisp
| @@ -67,3 +67,43 @@ Respects MOUSEBINDS-MIXIN convention." | |||
| 67 | 67 | (configure-client wm client | |
| 68 | 68 | :width (+ width (- new-x x)) | |
| 69 | 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/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))))) | |
modified clwm.asd
| @@ -24,5 +24,7 @@ | |||
| 24 | 24 | (:file "commands") | |
| 25 | 25 | (:file "mousebinds-mixin") | |
| 26 | 26 | (:file "warp-pointer-mixin") | |
| 27 | - | (:file "alt-tab-ring-mixin"))) | |
| 27 | + | (:file "alt-tab-ring-mixin") | |
| 28 | + | (:file "screen-geometry-mixin") | |
| 29 | + | (:file "pointer-placement-mixin"))) | |
| 28 | 30 | (: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 (alt-tab-ring-mixin | |
| 7 | + | (defclass my-wm (pointer-placement-mixin | |
| 8 | + | screen-geometry-mixin | |
| 9 | + | alt-tab-ring-mixin | |
| 8 | 10 | warp-pointer-mixin | |
| 9 | 11 | mousebinds-mixin | |
| 10 | 12 | pointer-keybinds-mixin | |
| @@ -24,8 +26,8 @@ | |||
| 24 | 26 | ((:mod-1 :shift :tab) . ,(cycle-backward :mod-1 :tab)))) | |
| 25 | 27 | ||
| 26 | 28 | (defmethod mouse-bindings append ((wm my-wm)) | |
| 27 | - | '(((:mod-1 1) . drag-move) | |
| 28 | - | ((:mod-1 3) . drag-resize))) | |
| 29 | + | '(((:mod-1 1) . bounded-drag-move) | |
| 30 | + | ((:mod-1 3) . bounded-drag-resize))) | |
| 29 | 31 | ||
| 30 | 32 | (run-wm 'my-wm) | |
| 31 | 33 | ||