at commit 31b6c823
| 1 | ;;;; common lisp window manager |
| 2 | ;;;; |
| 3 | ;;;; an EXAMPLE reparenting mixin |
| 4 | ;;;; not suitable for real use |
| 5 | |
| 6 | (in-package #:clwm-ext) |
| 7 | |
| 8 | (defclass reparenting-mixin (interaction-mixin) |
| 9 | ((font :initform nil :accessor frame-font) |
| 10 | (gcontext :initform nil :accessor frame-gcontext)) |
| 11 | (:documentation |
| 12 | "Draw frame windows around clients.")) |
| 13 | |
| 14 | (defclass framed-client () |
| 15 | ((frame :initform nil :accessor client-frame |
| 16 | :documentation "The frame window the client's window is in.") |
| 17 | (original-border :initform 0 :accessor client-original-border |
| 18 | :documentation |
| 19 | "The client window's border width before framing.")) |
| 20 | (:documentation "A client in a frame window.")) |
| 21 | |
| 22 | (defmethod client-mixins append ((wm reparenting-mixin)) |
| 23 | '(framed-client)) |
| 24 | |
| 25 | (defgeneric frame-font-name (wm) |
| 26 | (:documentation "The core X font titles are drawn in.") |
| 27 | (:method ((wm reparenting-mixin)) "fixed")) |
| 28 | (defgeneric frame-colors (wm) |
| 29 | (:documentation |
| 30 | "Color names for frames: title bar, text, frame border, frame.") |
| 31 | (:method ((wm reparenting-mixin)) |
| 32 | (values "#336699" "white" "black" "#cccccc"))) |
| 33 | |
| 34 | ;;; sizes |
| 35 | |
| 36 | ;; defconstant because this is just an example |
| 37 | (defconstant +frame-border+ 1 |
| 38 | "The frame window's X border width.") |
| 39 | (defconstant +frame-margin+ 4 |
| 40 | "The frame's edges beside and below the client.") |
| 41 | |
| 42 | (defun ensure-frame-resources (wm) |
| 43 | "Helper to set up the title font and the graphics context |
| 44 | for drawing frames." |
| 45 | (unless (frame-gcontext wm) |
| 46 | (setf (frame-font wm) (xlib:open-font (display wm) |
| 47 | (frame-font-name wm)) |
| 48 | (frame-gcontext wm) (xlib:create-gcontext |
| 49 | :drawable (root wm) |
| 50 | :font (frame-font wm))))) |
| 51 | |
| 52 | (defun title-height (wm) |
| 53 | "Helper to figure out the height of the window title." |
| 54 | (ensure-frame-resources wm) |
| 55 | (+ (xlib:font-ascent (frame-font wm)) |
| 56 | (xlib:font-descent (frame-font wm)) 4)) |
| 57 | |
| 58 | (defmethod manage-client :before ((wm reparenting-mixin) client) |
| 59 | (let* ((window (client-window client)) |
| 60 | (title (title-height wm))) |
| 61 | (multiple-value-bind |
| 62 | (title-color text-color border-color background) |
| 63 | (frame-colors wm) |
| 64 | (declare (ignore title-color text-color)) |
| 65 | (xlib:with-state (window) |
| 66 | (let ((frame |
| 67 | (xlib:create-window |
| 68 | :parent (root wm) |
| 69 | :x (xlib:drawable-x window) |
| 70 | :y (xlib:drawable-y window) |
| 71 | :width (+ (xlib:drawable-width window) |
| 72 | (* 2 +frame-margin+)) |
| 73 | :height (+ (xlib:drawable-height window) |
| 74 | title |
| 75 | +frame-margin+) |
| 76 | :border-width +frame-border+ |
| 77 | :border (color wm border-color) |
| 78 | :background (color wm background) |
| 79 | :override-redirect :on |
| 80 | :event-mask (xlib:make-event-mask |
| 81 | :substructure-redirect |
| 82 | :substructure-notify |
| 83 | :exposure :enter-window |
| 84 | :button-press :button-release |
| 85 | :button-motion)))) |
| 86 | (setf (client-frame client) frame |
| 87 | (client-original-border client) |
| 88 | (xlib:drawable-border-width window)) |
| 89 | (unless (eql (xlib:window-map-state window) :unmapped) |
| 90 | (incf (pending-unmaps client))) |
| 91 | (setf (xlib:drawable-border-width window) 0) |
| 92 | (xlib:reparent-window window frame +frame-margin+ title) |
| 93 | ;; save the original window |
| 94 | (xlib:add-to-save-set window)))))) |
| 95 | |
| 96 | (defmethod client-windows append ((wm reparenting-mixin) client) |
| 97 | (list (client-frame client))) |
| 98 | |
| 99 | (defmethod unmanage-client :after ((wm reparenting-mixin) client) |
| 100 | (let ((window (client-window client)) |
| 101 | (frame (client-frame client))) |
| 102 | (xlib:with-state (frame) |
| 103 | (xlib:reparent-window window (root wm) |
| 104 | (xlib:drawable-x frame) |
| 105 | (xlib:drawable-y frame))) |
| 106 | (setf (xlib:drawable-border-width window) |
| 107 | (client-original-border client)) |
| 108 | (xlib:remove-from-save-set window) |
| 109 | (xlib:destroy-window frame))) |
| 110 | |
| 111 | (defmethod show-client :after ((wm reparenting-mixin) client) |
| 112 | (xlib:map-window (client-frame client))) |
| 113 | |
| 114 | (defmethod hide-client :after ((wm reparenting-mixin) client) |
| 115 | (xlib:unmap-window (client-frame client))) |
| 116 | |
| 117 | ;;; use frame geometry for everything that cares about our geometry |
| 118 | |
| 119 | (defmethod client-geometry ((wm reparenting-mixin) client) |
| 120 | (let ((frame (client-frame client))) |
| 121 | (xlib:with-state (frame) |
| 122 | (values (xlib:drawable-x frame) (xlib:drawable-y frame) |
| 123 | (+ (xlib:drawable-width frame) (* 2 +frame-border+)) |
| 124 | (+ (xlib:drawable-height frame) (* 2 +frame-border+)))))) |
| 125 | |
| 126 | (defmethod client-decoration-extents ((wm reparenting-mixin) client) |
| 127 | (let ((side (+ +frame-border+ +frame-margin+))) |
| 128 | (values side (+ +frame-border+ (title-height wm)) side side))) |
| 129 | |
| 130 | (defmethod apply-client-geometry |
| 131 | ((wm reparenting-mixin) client |
| 132 | &key x y width height stack-mode sibling) |
| 133 | (flet ((inside (size thickness) |
| 134 | (and size (max 1 (- size thickness))))) |
| 135 | (configure-window (client-frame client) |
| 136 | :x x :y y |
| 137 | :width (inside width (* 2 +frame-border+)) |
| 138 | :height (inside height (* 2 +frame-border+)) |
| 139 | :stack-mode stack-mode |
| 140 | :sibling (and sibling (client-frame sibling))) |
| 141 | (multiple-value-bind (left top right bottom) |
| 142 | (client-decoration-extents wm client) |
| 143 | (configure-window (client-window client) |
| 144 | :width (inside width (+ left right)) |
| 145 | :height (inside height (+ top bottom)))))) |
| 146 | |
| 147 | ;; moving the frame doesn't move the client in its parent, so X won't |
| 148 | ;; tell it on its own; ICCCM says this is our responsibility |
| 149 | (defmethod apply-client-geometry :after ((wm reparenting-mixin) client |
| 150 | &key x y &allow-other-keys) |
| 151 | (when (or x y) |
| 152 | (send-configure-notify wm client))) |
| 153 | |
| 154 | ;;; actually draw very simple frames |
| 155 | ;;; todo: define some dumb widgets to avoid this spaghetti |
| 156 | |
| 157 | (defun draw-frame (wm client) |
| 158 | "Draw a client's title bar: its name, and a close box at the right. |
| 159 | Expects REPARENTING-MIXIN." |
| 160 | (ensure-frame-resources wm) |
| 161 | (let* ((frame (client-frame client)) |
| 162 | (gcontext (frame-gcontext wm)) |
| 163 | (font (frame-font wm)) |
| 164 | (title (title-height wm)) |
| 165 | (width (xlib:drawable-width frame)) |
| 166 | (name (drawable-text |
| 167 | (or (xlib:wm-name (client-window client)) "")))) |
| 168 | (multiple-value-bind (title-color text-color) (frame-colors wm) |
| 169 | (setf (xlib:gcontext-foreground gcontext) (color wm title-color)) |
| 170 | (xlib:draw-rectangle frame gcontext 0 0 width title t) |
| 171 | (setf (xlib:gcontext-foreground gcontext) (color wm text-color) |
| 172 | (xlib:gcontext-background gcontext) (color wm title-color)) |
| 173 | (xlib:draw-glyphs frame gcontext +frame-margin+ |
| 174 | (+ 2 (xlib:font-ascent font)) name) |
| 175 | ;; the close box: a square with an X in it |
| 176 | (let* ((size (- title 6)) |
| 177 | (left (- width size 3)) |
| 178 | (top 3)) |
| 179 | (xlib:draw-rectangle frame gcontext left top size size) |
| 180 | (xlib:draw-line |
| 181 | frame gcontext left top (+ left size) (+ top size)) |
| 182 | (xlib:draw-line |
| 183 | frame gcontext left (+ top size) (+ left size) top))))) |
| 184 | |
| 185 | (defun close-box-p (wm client x y) |
| 186 | "Helper to check if a point in a client's frame is in its close box." |
| 187 | (let ((title (title-height wm))) |
| 188 | (and (< y title) |
| 189 | (>= x |
| 190 | (- (xlib:drawable-width (client-frame client)) title))))) |
| 191 | |
| 192 | ;;; handle events on frames |
| 193 | |
| 194 | (defun frame-event-p (client window) |
| 195 | (and window (xlib:window-equal window (client-frame client)))) |
| 196 | |
| 197 | (defmethod handle-client-event |
| 198 | ((wm reparenting-mixin) client (event-key (eql :exposure)) |
| 199 | &key window count) |
| 200 | (if (frame-event-p client window) |
| 201 | (when (zerop count) |
| 202 | (draw-frame wm client)) |
| 203 | (call-next-method))) |
| 204 | |
| 205 | (defmethod handle-client-event |
| 206 | ((wm reparenting-mixin) client (event-key (eql :property-notify)) |
| 207 | &key atom) |
| 208 | (when (member atom '(:WM_NAME :_NET_WM_NAME)) |
| 209 | (draw-frame wm client)) |
| 210 | (call-next-method)) |
| 211 | |
| 212 | ;; close on close button click, drag otherwise |
| 213 | (defmethod handle-client-event |
| 214 | ((wm reparenting-mixin) client (event-key (eql :button-press)) |
| 215 | &key window child code x y root-x root-y) |
| 216 | (if (and (frame-event-p client window) (null child) (= code 1)) |
| 217 | (cond ((close-box-p wm client x y) |
| 218 | (close-client wm client)) |
| 219 | (t |
| 220 | (let ((function |
| 221 | (if (< y (title-height wm)) |
| 222 | (drag-move wm client root-x root-y) |
| 223 | (drag-resize wm client root-x root-y)))) |
| 224 | (start-interaction |
| 225 | wm |
| 226 | (make-instance 'drag |
| 227 | :client client |
| 228 | :function function))))) |
| 229 | (call-next-method))) |