at commit 9b7c4fe7
| 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))))) |