Correctly Configure roots on monitor change
[clfswm.git] / src / clfswm-internal.lisp
blob81c5e7ede70439bc7f10056d76ff7d56ee4efca5
1 ;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Main functions
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; --------------------------------------------------------------------------
26 (in-package :clfswm)
28 (defgeneric child-border-size (child))
30 (defmethod child-border-size ((child frame))
31 (x-drawable-border-width (frame-window child)))
33 (defmethod child-border-size ((child xlib:window))
34 (x-drawable-border-width child))
36 (defmethod child-border-size (child)
37 (declare (ignore child))
40 (defgeneric set-child-border-size (child value))
42 (defmethod set-child-border-size ((child frame) value)
43 (setf (x-drawable-border-width (frame-window child)) value))
45 (defmethod set-child-border-size ((child xlib:window) value)
46 (setf (x-drawable-border-width child) value))
48 (defmethod set-child-border-size (child value)
49 (declare (ignore child value)))
51 (defsetf child-border-size set-child-border-size)
55 ;;; Conversion functions
56 ;;; Float -> Pixel conversion
57 (defun x-fl->px (x parent)
58 "Convert float X coordinate to pixel"
59 (round (+ (* x (frame-rw parent)) (frame-rx parent))))
61 (defun y-fl->px (y parent)
62 "Convert float Y coordinate to pixel"
63 (round (+ (* y (frame-rh parent)) (frame-ry parent))))
65 (defun w-fl->px (w parent)
66 "Convert float Width coordinate to pixel"
67 (round (* w (frame-rw parent))))
69 (defun h-fl->px (h parent)
70 "Convert float Height coordinate to pixel"
71 (round (* h (frame-rh parent))))
73 ;;; Pixel -> Float conversion
74 (defun x-px->fl (x parent)
75 "Convert pixel X coordinate to float"
76 (/ (- x (frame-rx parent) (child-border-size parent)) (frame-rw parent)))
78 (defun y-px->fl (y parent)
79 "Convert pixel Y coordinate to float"
80 (/ (- y (frame-ry parent) (child-border-size parent)) (frame-rh parent)))
82 (defun w-px->fl (w parent)
83 "Convert pixel Width coordinate to float"
84 (/ w (frame-rw parent)))
86 (defun h-px->fl (h parent)
87 "Convert pixel Height coordinate to float"
88 (/ h (frame-rh parent)))
92 (defun rect-hidden-p (rect1 rect2)
93 "Return T if child-rect1 hide child-rect2"
94 (and *show-hide-policy*
95 (funcall *show-hide-policy* (child-rect-x rect1) (child-rect-x rect2))
96 (funcall *show-hide-policy* (child-rect-y rect1) (child-rect-y rect2))
97 (funcall *show-hide-policy* (+ (child-rect-x rect2) (child-rect-w rect2))
98 (+ (child-rect-x rect1) (child-rect-w rect1)))
99 (funcall *show-hide-policy* (+ (child-rect-y rect2) (child-rect-h rect2))
100 (+ (child-rect-y rect1) (child-rect-h rect1)))))
104 (defgeneric frame-p (frame))
105 (defmethod frame-p ((frame frame))
106 (declare (ignore frame))
108 (defmethod frame-p (frame)
109 (declare (ignore frame))
110 nil)
114 ;;; in-*: Find if point (x,y) is in frame, window or child
115 (defun in-rect (x y xr yr wr hr)
116 (and (<= xr x (+ xr wr))
117 (<= yr y (+ yr hr))))
119 (defun in-frame (frame x y)
120 (and (frame-p frame)
121 (in-rect x y (frame-rx frame) (frame-ry frame) (frame-rw frame) (frame-rh frame))))
123 (defun in-window (window x y)
124 (and (xlib:window-p window)
125 (in-rect x y
126 (x-drawable-x window) (x-drawable-y window)
127 (x-drawable-width window) (x-drawable-height window))))
129 (defgeneric in-child (child x y))
131 (defmethod in-child ((child frame) x y)
132 (in-frame child x y))
133 (defmethod in-child ((child xlib:window) x y)
134 (in-window child x y))
135 (defmethod in-child (child x y)
136 (declare (ignore child x y))
137 nil)
142 (defun frame-selected-child (frame)
143 (when (frame-p frame)
144 (with-slots (child selected-pos) frame
145 (let ((len (length child)))
146 (cond ((minusp selected-pos) (setf selected-pos 0))
147 ((>= selected-pos len) (setf selected-pos (max (1- len) 0)))))
148 (nth selected-pos child))))
154 (defgeneric child-equal-p (child-1 child-2))
156 (defmethod child-equal-p ((child-1 xlib:window) (child-2 xlib:window))
157 (xlib:window-equal child-1 child-2))
159 (defmethod child-equal-p ((child-1 frame) (child-2 frame))
160 (equal child-1 child-2))
162 (defmethod child-equal-p (child-1 child-2)
163 (declare (ignore child-1 child-2))
164 nil)
169 (declaim (inline child-member child-remove child-position))
171 (defun child-member (child list)
172 (member child list :test #'child-equal-p))
174 (defun child-remove (child list)
175 (remove child list :test #'child-equal-p))
177 (defun child-position (child list)
178 (position child list :test #'child-equal-p))
182 ;;; Frame data manipulation functions
183 (defun frame-data-slot (frame slot)
184 "Return the value associated to data slot"
185 (when (frame-p frame)
186 (second (assoc slot (frame-data frame)))))
188 (defun set-frame-data-slot (frame slot value)
189 "Set the value associated to data slot"
190 (when (frame-p frame)
191 (with-slots (data) frame
192 (setf data (remove (assoc slot data) data))
193 (push (list slot value) data))
194 value))
196 (defsetf frame-data-slot set-frame-data-slot)
199 (defun remove-frame-data-slot (frame slot)
200 "Remove a slot in frame data slots"
201 (when (frame-p frame)
202 (with-slots (data) frame
203 (setf data (remove (assoc slot data) data)))))
207 (defun managed-window-p (window frame)
208 "Return t only if window is managed by frame"
209 (if (frame-p frame)
210 (with-slots ((managed forced-managed-window)
211 (unmanaged forced-unmanaged-window)) frame
212 (and (xlib:window-p window)
213 (not (child-member window unmanaged))
214 (not (member (xlib:wm-name window) unmanaged :test #'string-equal-p))
215 (or (member :all (frame-managed-type frame))
216 (member (window-type window) (frame-managed-type frame))
217 (child-member window managed)
218 (member (xlib:wm-name window) managed :test #'string-equal-p))))
222 (defun add-in-never-managed-window-list (value)
223 (pushnew value *never-managed-window-list* :test #'equal))
225 (defun never-managed-window-p (window)
226 (when (xlib:window-p window)
227 (dolist (type *never-managed-window-list*)
228 (when (funcall (first type) window)
229 (return (values t (second type)))))))
233 (defgeneric child-name (child))
235 (defmethod child-name ((child xlib:window))
236 (xlib:wm-name child))
238 (defmethod child-name ((child frame))
239 (frame-name child))
241 (defmethod child-name (child)
242 (declare (ignore child))
243 "???")
246 (defgeneric set-child-name (child name))
248 (defmethod set-child-name ((child xlib:window) name)
249 (setf (xlib:wm-name child) name))
251 (defmethod set-child-name ((child frame) name)
252 (setf (frame-name child) name))
254 (defmethod set-child-name (child name)
255 (declare (ignore child name)))
257 (defsetf child-name set-child-name)
262 (defgeneric child-fullname (child))
264 (defmethod child-fullname ((child xlib:window))
265 (format nil "~A (~A)" (or (xlib:wm-name child) "?") (or (xlib:get-wm-class child) "?")))
267 (defmethod child-fullname ((child frame))
268 (aif (frame-name child)
269 (format nil "~A (Frame ~A)" it (frame-number child))
270 (format nil "Frame ~A" (frame-number child))))
272 (defmethod child-fullname (child)
273 (declare (ignore child))
274 "???")
277 (defgeneric child-transparency (child))
279 (defmethod child-transparency ((child xlib:window))
280 (window-transparency child))
282 (defmethod child-transparency ((child frame))
283 (window-transparency (frame-window child)))
285 (defmethod child-transparency (child)
286 (declare (ignore child))
289 (defgeneric set-child-transparency (child value))
291 (defmethod set-child-transparency ((child xlib:window) value)
292 (setf (window-transparency child) value))
294 (defmethod set-child-transparency ((child frame) value)
295 (setf (window-transparency (frame-window child)) value))
297 (defmethod set-child-transparency (child value)
298 (declare (ignore child value)))
300 (defsetf child-transparency set-child-transparency)
304 (defgeneric child-x (child))
305 (defmethod child-x ((child xlib:window))
306 (x-drawable-x child))
307 (defmethod child-x ((child frame))
308 (frame-rx child))
310 (defgeneric child-y (child))
311 (defmethod child-y ((child xlib:window))
312 (x-drawable-y child))
313 (defmethod child-y ((child frame))
314 (frame-ry child))
316 (defgeneric child-width (child))
317 (defmethod child-width ((child xlib:window))
318 (x-drawable-width child))
319 (defmethod child-width ((child frame))
320 (frame-rw child))
322 (defgeneric child-height (child))
323 (defmethod child-height ((child xlib:window))
324 (x-drawable-height child))
325 (defmethod child-height ((child frame))
326 (frame-rh child))
328 (defgeneric child-x2 (child))
329 (defmethod child-x2 ((child xlib:window))
330 (+ (x-drawable-x child) (x-drawable-width child)))
331 (defmethod child-x2 ((child frame))
332 (+ (frame-rx child) (frame-rw child)))
334 (defgeneric child-y2 (child))
335 (defmethod child-y2 ((child xlib:window))
336 (+ (x-drawable-y child) (x-drawable-height child)))
337 (defmethod child-y2 ((child frame))
338 (+ (frame-ry child) (frame-rh child)))
342 (defgeneric child-center (child))
344 (defmethod child-center ((child xlib:window))
345 (values (+ (x-drawable-x child) (/ (x-drawable-width child) 2))
346 (+ (x-drawable-y child) (/ (x-drawable-height child) 2))))
348 (defmethod child-center ((child frame))
349 (values (+ (frame-rx child) (/ (frame-rw child) 2))
350 (+ (frame-ry child) (/ (frame-rh child) 2))))
352 (defun child-distance (child1 child2)
353 (multiple-value-bind (x1 y1) (child-center child1)
354 (multiple-value-bind (x2 y2) (child-center child2)
355 (values (+ (abs (- x2 x1)) (abs (- y2 y1)))
356 (- x2 x1)
357 (- y2 y1)))))
359 (defun middle-child-x (child)
360 (+ (child-x child) (/ (child-width child) 2)))
362 (defun middle-child-y (child)
363 (+ (child-y child) (/ (child-height child) 2)))
365 (declaim (inline adj-border-xy adj-border-wh))
366 (defgeneric adj-border-xy (value child))
367 (defgeneric adj-border-wh (value child))
369 (defmethod adj-border-xy (v (child xlib:window))
370 (+ v (x-drawable-border-width child)))
372 (defmethod adj-border-xy (v (child frame))
373 (+ v (x-drawable-border-width (frame-window child))))
375 (defmethod adj-border-wh (v (child xlib:window))
376 (- v (* (x-drawable-border-width child) 2)))
378 (defmethod adj-border-wh (v (child frame))
379 (- v (* (x-drawable-border-width (frame-window child)) 2)))
382 (declaim (inline anti-adj-border-xy anti-adj-border-wh))
383 (defgeneric anti-adj-border-xy (value child))
384 (defgeneric anti-adj-border-wh (value child))
386 (defmethod anti-adj-border-xy (v (child xlib:window))
387 (- v (x-drawable-border-width child)))
389 (defmethod anti-adj-border-xy (v (child frame))
390 (- v (x-drawable-border-width (frame-window child))))
392 (defmethod anti-adj-border-wh (v (child xlib:window))
393 (+ v (* (x-drawable-border-width child) 2)))
395 (defmethod anti-adj-border-wh (v (child frame))
396 (+ v (* (x-drawable-border-width (frame-window child)) 2)))
401 (defmacro with-focus-window ((window) &body body)
402 `(let ((,window (xlib:input-focus *display*)))
403 (when (and ,window (not (xlib:window-equal ,window *no-focus-window*)))
404 ,@body)))
408 ;; (with-all-children (*root-frame* child) (typecase child (xlib:window (print child)) (frame (print (frame-number child)))))
409 (defmacro with-all-children ((root child) &body body)
410 (let ((rec (gensym))
411 (sub-child (gensym)))
412 `(block nil
413 (labels ((,rec (,child)
414 ,@body
415 (when (frame-p ,child)
416 (dolist (,sub-child (reverse (frame-child ,child)))
417 (,rec ,sub-child)))))
418 (,rec ,root)))))
421 ;; (with-all-children (*root-frame* child) (typecase child (xlib:window (print child)) (frame (print (frame-number child)))))
422 (defmacro with-all-children-reversed ((root child) &body body)
423 (let ((rec (gensym))
424 (sub-child (gensym)))
425 `(block nil
426 (labels ((,rec (,child)
427 ,@body
428 (when (frame-p ,child)
429 (dolist (,sub-child (frame-child ,child))
430 (,rec ,sub-child)))))
431 (,rec ,root)))))
437 ;; (with-all-frames (*root-frame* frame) (print (frame-number frame)))
438 (defmacro with-all-frames ((root frame) &body body)
439 (let ((rec (gensym))
440 (child (gensym)))
441 `(block nil
442 (labels ((,rec (,frame)
443 (when (frame-p ,frame)
444 ,@body
445 (dolist (,child (reverse (frame-child ,frame)))
446 (,rec ,child)))))
447 (,rec ,root)))))
450 ;; (with-all-windows (*root-frame* window) (print window))
451 (defmacro with-all-windows ((root window) &body body)
452 (let ((rec (gensym))
453 (child (gensym)))
454 `(block nil
455 (labels ((,rec (,window)
456 (when (xlib:window-p ,window)
457 ,@body)
458 (when (frame-p ,window)
459 (dolist (,child (reverse (frame-child ,window)))
460 (,rec ,child)))))
461 (,rec ,root)))))
465 ;; (with-all-frames-windows (*root-frame* child) (print child) (print (frame-number child)))
466 (defmacro with-all-windows-frames ((root child) body-window body-frame)
467 (let ((rec (gensym))
468 (sub-child (gensym)))
469 `(block nil
470 (labels ((,rec (,child)
471 (typecase ,child
472 (xlib:window ,body-window)
473 (frame ,body-frame
474 (dolist (,sub-child (reverse (frame-child ,child)))
475 (,rec ,sub-child))))))
476 (,rec ,root)))))
478 (defmacro with-all-windows-frames-and-parent ((root child parent) body-window body-frame)
479 (let ((rec (gensym))
480 (sub-child (gensym)))
481 `(block nil
482 (labels ((,rec (,child ,parent)
483 (typecase ,child
484 (xlib:window ,body-window)
485 (frame ,body-frame
486 (dolist (,sub-child (reverse (frame-child ,child)))
487 (,rec ,sub-child ,child))))))
488 (,rec ,root nil)))))
492 (defun create-frame-window ()
493 (let ((win (xlib:create-window :parent *root*
494 :x 0
495 :y 0
496 :width 200
497 :height 200
498 :background (get-color *frame-background*)
499 :colormap (xlib:screen-default-colormap *screen*)
500 :border-width *border-size*
501 :border (get-color *color-selected*)
502 :event-mask '(:exposure :button-press :button-release :pointer-motion :enter-window))))
503 (setf (window-transparency win) *frame-transparency*)
504 win))
506 (defun create-frame-gc (window)
507 (xlib:create-gcontext :drawable window
508 :foreground (get-color *frame-foreground*)
509 :background (get-color *frame-background*)
510 :font *default-font*
511 :line-style :solid))
514 (defun destroy-all-frames-window ()
515 (with-all-frames (*root-frame* frame)
516 (when (frame-gc frame)
517 (xlib:free-gcontext (frame-gc frame))
518 (setf (frame-gc frame) nil))
519 (when (frame-window frame)
520 (xlib:destroy-window (frame-window frame))
521 (setf (frame-window frame) nil))))
523 (defun create-all-frames-window ()
524 (with-all-frames (*root-frame* frame)
525 (unless (frame-window frame)
526 (setf (frame-window frame) (create-frame-window)))
527 (unless (frame-gc frame)
528 (setf (frame-gc frame) (create-frame-gc (frame-window frame)))))
529 (with-all-frames (*root-frame* frame)
530 (dolist (child (frame-child frame))
531 (handler-case
532 (dbg (child-fullname child))
533 (error (c)
534 (setf (frame-child frame) (remove child (frame-child frame) :test #'child-equal-p))
535 (dbg c child))))))
540 (defun frame-find-free-number ()
541 (let ((all-numbers nil))
542 (with-all-frames (*root-frame* frame)
543 (pushnew (frame-number frame) all-numbers))
544 (find-free-number all-numbers)))
547 (defun create-frame (&rest args &key (number (frame-find-free-number)) &allow-other-keys)
548 (let* ((window (create-frame-window))
549 (gc (create-frame-gc window)))
550 (apply #'make-instance 'frame :number number :window window :gc gc args)))
553 (defun add-frame (frame parent)
554 (push frame (frame-child parent))
555 frame)
558 (defun place-frame (frame parent prx pry prw prh)
559 "Place a frame from real (pixel) coordinates"
560 (when (and (frame-p frame) (frame-p parent))
561 (with-slots (window x y w h) frame
562 (setf (x-drawable-x window) prx
563 (x-drawable-y window) pry
564 (x-drawable-width window) prw
565 (x-drawable-height window) prh
566 x (x-px->fl prx parent)
567 y (y-px->fl pry parent)
568 w (w-px->fl prw parent)
569 h (h-px->fl prh parent))
570 (xlib:display-finish-output *display*))))
574 (defun find-child (to-find root)
575 "Find to-find in root or in its children"
576 (with-all-children (root child)
577 (when (child-equal-p child to-find)
578 (return-from find-child t))))
582 (defmacro with-find-in-all-frames (test &optional return-value)
583 `(let (ret)
584 (block return-block
585 (with-all-frames (root frame)
586 (when ,test
587 (if first-foundp
588 (return-from return-block (or ,return-value frame))
589 (setf ret frame))))
590 (or ,return-value ret))))
592 (defun find-parent-frame (to-find &optional (root *root-frame*) first-foundp)
593 "Return the parent frame of to-find"
594 (with-find-in-all-frames
595 (child-member to-find (frame-child frame))))
597 (defun find-frame-window (window &optional (root *root-frame*) first-foundp)
598 "Return the frame with the window window"
599 (with-find-in-all-frames
600 (xlib:window-equal window (frame-window frame))))
602 (defun find-frame-by-name (name &optional (root *root-frame*) first-foundp)
603 "Find a frame from its name"
604 (when name
605 (with-find-in-all-frames
606 (string-equal name (frame-name frame)))))
608 (defun find-frame-by-number (number &optional (root *root-frame*) first-foundp)
609 "Find a frame from its number"
610 (when (numberp number)
611 (with-find-in-all-frames
612 (= number (frame-number frame)))))
615 (defun find-child-in-parent (child base)
616 "Return t if child is in base or in its parents"
617 (labels ((rec (base)
618 (when (child-equal-p child base)
619 (return-from find-child-in-parent t))
620 (let ((parent (find-parent-frame base)))
621 (when parent
622 (rec parent)))))
623 (rec base)))
625 ;;; Multiple roots support (replace the old *current-root* variable)
626 (let ((root-list nil)
627 (current-child nil))
628 (defun get-root-list ()
629 root-list)
631 (let ((save-root-list nil))
632 (defun save-root-list ()
633 (setf save-root-list nil)
634 (dolist (root root-list)
635 (push (copy-root root) save-root-list)))
636 (defun restore-root-list ()
637 (setf root-list nil)
638 (dolist (root save-root-list)
639 (push (copy-root root) root-list))))
641 (defmacro with-saved-root-list (() &body body)
642 `(progn
643 (save-root-list)
644 ,@body
645 (restore-root-list)))
647 (defun reset-root-list ()
648 (setf root-list nil
649 current-child nil))
651 (defun define-as-root (child x y width height)
652 (push (make-root :child child :original child :current-child nil :x x :y y :w width :h height) root-list))
654 (defun find-root-by-coordinates (x y)
655 (dolist (root root-list)
656 (when (in-rect x y (root-x root) (root-y root) (root-w root) (root-h root))
657 (return root))))
659 (defun root (x &optional y)
660 "Return the root at coordinates (x,y) if y is not nil.
661 Otherwise, return the x nth root in root-list"
662 (if y
663 (find-root-by-coordinates x y)
664 (nth x root-list)))
666 (defun all-root-child ()
667 (loop for root in root-list
668 collect (root-child root)))
670 (defmacro with-all-root-child ((root) &body body)
671 (let ((root-symb (gensym)))
672 `(dolist (,root-symb (get-root-list))
673 (let ((,root (root-child ,root-symb)))
674 ,@body))))
676 (labels ((generic-child-root-p (child function)
677 (dolist (root root-list)
678 (when (child-equal-p child (funcall function root))
679 (return root)))))
680 (defun child-root-p (child)
681 (generic-child-root-p child #'root-child))
683 (defun child-original-root-p (child)
684 (generic-child-root-p child #'root-original)))
686 (defun change-root (old-root new-child)
687 (when (and old-root new-child)
688 (setf (root-child old-root) new-child)))
690 (defun find-root (child)
691 (aif (child-original-root-p child)
693 (awhen (find-parent-frame child)
694 (find-root it))))
696 (defun find-child-in-all-root (child)
697 (dolist (root root-list)
698 (when (find-child child (root-child root))
699 (return-from find-child-in-all-root root))))
701 (defun find-current-root ()
702 (root-child (find-root (current-child))))
704 (defun exchange-root-geometry (root-1 root-2)
705 (when (and root-1 root-2)
706 (rotatef (root-x root-1) (root-x root-2))
707 (rotatef (root-y root-1) (root-y root-2))
708 (rotatef (root-w root-1) (root-w root-2))
709 (rotatef (root-h root-1) (root-h root-2))))
711 (defun rotate-root-geometry ()
712 (let* ((current (first root-list))
713 (orig-x (root-x current))
714 (orig-y (root-y current))
715 (orig-w (root-w current))
716 (orig-h (root-h current)))
717 (dolist (elem (rest root-list))
718 (setf (root-x current) (root-x elem)
719 (root-y current) (root-y elem)
720 (root-w current) (root-w elem)
721 (root-h current) (root-h elem)
722 current elem))
723 (let ((last (car (last root-list))))
724 (setf (root-x last) orig-x
725 (root-y last) orig-y
726 (root-w last) orig-w
727 (root-h last) orig-h))))
729 (defun anti-rotate-root-geometry ()
730 (setf root-list (nreverse root-list))
731 (rotate-root-geometry)
732 (setf root-list (nreverse root-list)))
734 ;;; Current child functions
735 (defun current-child ()
736 current-child)
738 (defun current-child-setter (value)
739 (awhen (find-root value)
740 (setf (root-current-child it) value))
741 (setf current-child value))
743 (defmacro with-current-child ((new-child) &body body)
744 "Temporarly change the current child"
745 (let ((old-child (gensym))
746 (ret (gensym)))
747 `(let ((,old-child (current-child)))
748 (setf (current-child) ,new-child)
749 (let ((,ret (multiple-value-list (progn ,@body))))
750 (setf (current-child) ,old-child)
751 (values-list ,ret)))))
753 (defun child-is-a-current-child-p (child)
754 (find child root-list :test #'child-equal-p :key #'root-current-child)))
756 (defsetf current-child current-child-setter)
758 (defun ensure-at-least-one-root ()
759 (unless (get-root-list)
760 (let ((frame (create-frame)))
761 (add-frame frame *root-frame*)
762 (define-as-root frame 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*))
763 (add-frame (create-frame) frame))))
770 (defun is-in-current-child-p (child)
771 (and (frame-p (current-child))
772 (child-member child (frame-child (current-child)))))
776 (defun fixe-real-size (frame parent)
777 "Fixe real (pixel) coordinates in float coordinates"
778 (when (frame-p frame)
779 (with-slots (x y w h rx ry rw rh) frame
780 (setf x (x-px->fl rx parent)
781 y (y-px->fl ry parent)
782 w (w-px->fl (anti-adj-border-wh rw parent) parent)
783 h (h-px->fl (anti-adj-border-wh rh parent) parent)))))
785 (defun fixe-real-size-current-child ()
786 "Fixe real (pixel) coordinates in float coordinates for children in the current child"
787 (when (frame-p (current-child))
788 (dolist (child (frame-child (current-child)))
789 (fixe-real-size child (current-child)))))
793 ;;; Multiple physical screen helper
794 (defun add-placed-frame-tmp (frame n) ;; For test
795 (add-frame (create-frame :x 0.01 :y 0.01 :w 0.4 :h 0.4) frame)
796 (add-frame (create-frame :x 0.55 :y 0.01 :w 0.4 :h 0.4) frame)
797 (add-frame (create-frame :x 0.03 :y 0.5 :w 0.64 :h 0.44) frame)
798 (when (plusp n)
799 (add-placed-frame-tmp (first (frame-child frame)) (1- n))))
801 (defun parse-xinerama-info (line)
802 (remove nil
803 (mapcar (lambda (string)
804 (parse-integer string :junk-allowed t))
805 (split-string (substitute #\space #\x (substitute #\space #\, line))))))
807 (defun get-connected-heads-size (&optional (fake (string= (getenv "DISPLAY") ":1")))
808 (labels ((heads-info ()
809 (if (not fake)
810 (do-shell "xdpyinfo -ext XINERAMA")
811 (progn
812 (setf *show-root-frame-p* t)
813 (do-shell "echo ' available colormap entries: 256 per subfield
814 red, green, blue masks: 0xff0000, 0xff00, 0xff
815 significant bits in color specification: 8 bits
817 XINERAMA version 1.1 opcode: 150
818 head #0: 500x300 @ 10,10
819 head #1: 480x300 @ 520,20
820 head #2: 600x250 @ 310,330'")))))
821 (when (xlib:query-extension *display* "XINERAMA")
822 (let ((output (heads-info))
823 (sizes nil))
824 (loop for line = (read-line output nil nil)
825 while line
826 do (when (search " head " line)
827 (destructuring-bind (w h x y)
828 (parse-xinerama-info line)
829 (push (list x y w h) sizes))))
830 (remove-duplicates sizes :test #'equal)))))
831 ;;'((10 10 500 300) (550 50 400 400) (100 320 400 270))))))
832 ;;'((10 10 500 580) (540 50 470 500))))))
835 (defun place-frames-from-xinerama-infos ()
836 "Place frames according to xdpyinfo/xinerama informations"
837 (reset-root-list)
838 (let ((sizes (get-connected-heads-size))
839 (width (xlib:screen-width *screen*))
840 (height (xlib:screen-height *screen*)))
841 (format t "Screen sizes: ~A~%" sizes)
842 ;; Add frames in *root-frame* until we get the same number as screen heads
843 (loop while (< (length (frame-child *root-frame*)) (length sizes))
844 do (let ((frame (create-frame)))
845 (add-frame frame *root-frame*)))
846 ;; On the opposite way: remove frames until there is more than screen heads in *root-frame*
847 (when (and sizes (> (length (frame-child *root-frame*)) (length sizes)))
848 (dotimes (i (- (length (frame-child *root-frame*)) (length sizes)))
849 (let ((deleted-child (pop (frame-child *root-frame*))))
850 (typecase deleted-child
851 (xlib:window (push deleted-child (frame-child (first (frame-child *root-frame*)))))
852 (frame (dolist (child (frame-child deleted-child))
853 (push child (frame-child (first (frame-child *root-frame*)))))))
854 (setf (frame-layout (first (frame-child *root-frame*))) 'tile-space-layout
855 (frame-data-slot (first (frame-child *root-frame*)) :tile-layout-keep-position) :yes))))
856 (loop for size in sizes
857 for frame in (frame-child *root-frame*)
858 do (destructuring-bind (x y w h) size
859 (setf (frame-x frame) (float (/ x width))
860 (frame-y frame) (float (/ y height))
861 (frame-w frame) (float (/ w width))
862 (frame-h frame) (float (/ h height)))
863 ;;(add-placed-frame-tmp frame 2) ;; For tests
864 (unless (frame-child frame)
865 (add-frame (create-frame) frame))
866 (define-as-root frame x y w h)))))
869 (defun finish-configuring-root ()
870 (ensure-at-least-one-root)
871 (setf (current-child) (first (frame-child (first (frame-child *root-frame*))))))
875 (defun get-all-windows (&optional (root *root-frame*))
876 "Return all windows in root and in its children"
877 (let ((acc nil))
878 (with-all-windows (root window)
879 (push window acc))
880 acc))
883 (defun get-hidden-windows ()
884 "Return all hiddens windows"
885 (let ((all-windows (get-all-windows))
886 (hidden-windows (remove-if-not #'window-hidden-p
887 (copy-list (xlib:query-tree *root*)))))
888 (set-difference hidden-windows all-windows)))
892 ;;; Current window utilities
893 (defun get-current-window ()
894 (typecase (current-child)
895 (xlib:window (current-child))
896 (frame (frame-selected-child (current-child)))))
898 (defmacro with-current-window (&body body)
899 "Bind 'window' to the current window"
900 `(let ((window (get-current-window)))
901 (when (xlib:window-p window)
902 ,@body)))
904 (defun get-first-window ()
905 (typecase (current-child)
906 (xlib:window (current-child))
907 (frame (or (first (frame-child (current-child)))
908 (current-child)))))
914 (defun display-frame-info (frame)
915 (when (frame-p frame)
916 (let ((dy (+ (xlib:max-char-ascent *default-font*) (xlib:max-char-descent *default-font*))))
917 (with-slots (name number gc window child hidden-children) frame
918 (setf (xlib:gcontext-background gc) (get-color *frame-background*)
919 (xlib:window-background window) (get-color *frame-background*))
920 (clear-pixmap-buffer window gc)
921 (setf (xlib:gcontext-foreground gc) (get-color (if (and (child-root-p frame)
922 (child-equal-p frame (current-child)))
923 *frame-foreground-root* *frame-foreground*)))
924 (xlib:draw-glyphs *pixmap-buffer* gc 5 dy
925 (format nil "Frame: ~A~A"
926 number
927 (if name (format nil " - ~A" name) "")))
928 (let ((pos dy))
929 (when (child-root-p frame)
930 (when *child-selection*
931 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
932 (with-output-to-string (str)
933 (format str " Selection: ")
934 (dolist (child *child-selection*)
935 (typecase child
936 (xlib:window (format str " ~A " (xlib:wm-name child)))
937 (frame (format str " frame:~A[~A] " (frame-number child)
938 (aif (frame-name child) it "")))))))))
939 (dolist (ch child)
940 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
941 (format nil " ~A" (ensure-printable (child-fullname ch))))))
942 (copy-pixmap-buffer window gc)
943 (values t t)))))
946 (defgeneric rename-child (child name))
948 (defmethod rename-child ((child frame) name)
949 (setf (frame-name child) name)
950 (display-frame-info child))
952 (defmethod rename-child ((child xlib:window) name)
953 (setf (xlib:wm-name child) name))
955 (defmethod rename-child (child name)
956 (declare (ignore child name)))
959 (defun get-parent-layout (child parent)
960 (aif (child-root-p child)
961 (values (- (root-x it) (child-border-size child)) (- (root-y it) (child-border-size child))
962 (root-w it) (root-h it))
963 (if (or (frame-p child) (managed-window-p child parent))
964 (if (frame-p parent)
965 (aif (frame-layout parent)
966 (funcall it child parent)
967 (no-layout child parent))
968 (values (- (child-border-size child)) (- (child-border-size child))
969 (xlib:screen-width *screen*)
970 (xlib:screen-height *screen*)))
971 (values (x-drawable-x child) (x-drawable-y child)
972 (x-drawable-width child) (x-drawable-height child)))))
978 (defgeneric adapt-child-to-parent (child parent))
980 (defmethod adapt-child-to-parent ((window xlib:window) parent)
981 (when (managed-window-p window parent)
982 (multiple-value-bind (nx ny nw nh)
983 (get-parent-layout window parent)
984 (setf nw (max nw 1) nh (max nh 1))
985 (let ((change nil))
986 (when (or (/= (x-drawable-x window) nx)
987 (/= (x-drawable-y window) ny))
988 (setf change :moved))
989 (when (or (/= (x-drawable-width window) nw)
990 (/= (x-drawable-height window) nh))
991 (setf change :resized))
992 (when change
993 (xlib:with-state (window)
994 (setf (x-drawable-x window) nx
995 (x-drawable-y window) ny
996 (x-drawable-width window) nw
997 (x-drawable-height window) nh)))
998 change))))
1001 (defmethod adapt-child-to-parent ((frame frame) parent)
1002 (declare (ignore parent))
1003 (with-slots (rx ry rw rh window) frame
1004 (let ((change nil))
1005 (when (or (/= (x-drawable-x window) rx)
1006 (/= (x-drawable-y window) ry))
1007 (setf change :moved))
1008 (when (or (/= (x-drawable-width window) rw)
1009 (/= (x-drawable-height window) rh))
1010 (setf change :resized))
1011 (when change
1012 (xlib:with-state (window)
1013 (setf (x-drawable-x window) rx
1014 (x-drawable-y window) ry
1015 (x-drawable-width window) rw
1016 (x-drawable-height window) rh)))
1017 change)))
1019 (defmethod adapt-child-to-parent (child parent)
1020 (declare (ignore child parent))
1021 nil)
1024 (defgeneric set-child-stack-order (window child)
1025 (:documentation "Raise window if child is NIL else put window just below child"))
1027 (defmethod set-child-stack-order (window (child xlib:window))
1028 (lower-window window child))
1030 (defmethod set-child-stack-order (window (child frame))
1031 (lower-window window (frame-window child)))
1033 (defmethod set-child-stack-order (window child)
1034 (declare (ignore child))
1035 (raise-window window))
1039 (defgeneric show-child (child parent previous))
1041 (defmethod show-child ((frame frame) parent previous)
1042 (declare (ignore parent))
1043 (with-slots (window show-window-p) frame
1044 (if (and show-window-p
1045 (or *show-root-frame-p* (not (child-root-p frame))))
1046 (progn
1047 (map-window window)
1048 (set-child-stack-order window previous)
1049 (display-frame-info frame))
1050 (hide-window window))))
1054 (defun hide-unmanaged-window-p (parent)
1055 (let ((action (frame-data-slot parent :unmanaged-window-action)))
1056 (case action
1057 (:hide t)
1058 (:show nil)
1059 (t *hide-unmanaged-window*))))
1062 (defmethod show-child ((window xlib:window) parent previous)
1063 (if (or (managed-window-p window parent)
1064 (child-equal-p window (current-child))
1065 (not (hide-unmanaged-window-p parent))
1066 (child-is-a-current-child-p parent))
1067 (progn
1068 (map-window window)
1069 (set-child-stack-order window previous))
1070 (hide-window window)))
1072 (defmethod show-child (child parent raise-p)
1073 (declare (ignore child parent raise-p))
1077 (defgeneric hide-child (child))
1079 (defmethod hide-child ((frame frame))
1080 (with-slots (window) frame
1081 (xlib:unmap-window window)))
1083 (defmethod hide-child ((window xlib:window))
1084 (hide-window window))
1086 (defmethod hide-child (child)
1087 (declare (ignore child))
1091 (defgeneric select-child (child selected))
1093 (labels ((get-selected-color (child selected-p)
1094 (get-color (cond ((child-equal-p child (current-child)) *color-selected*)
1095 (selected-p *color-maybe-selected*)
1096 (t *color-unselected*)))))
1097 (defmethod select-child ((frame frame) selected-p)
1098 (when (and (frame-p frame) (frame-window frame))
1099 (setf (xlib:window-border (frame-window frame))
1100 (get-selected-color frame selected-p))))
1102 (defmethod select-child ((window xlib:window) selected-p)
1103 (setf (xlib:window-border window)
1104 (get-selected-color window selected-p)))
1106 (defmethod select-child (child selected)
1107 (declare (ignore child selected))
1108 ()))
1110 (defun select-current-frame (selected)
1111 (select-child (current-child) selected))
1113 (defun unselect-all-frames ()
1114 (with-all-children (*root-frame* child)
1115 (select-child child nil)))
1119 (defun set-focus-to-current-child ()
1120 (labels ((rec (child)
1121 (typecase child
1122 (xlib:window (focus-window child))
1123 (frame (rec (frame-selected-child child))))))
1124 (no-focus)
1125 (rec (current-child))))
1130 (defun adapt-frame-to-parent (frame parent)
1131 (multiple-value-bind (nx ny nw nh)
1132 (get-parent-layout frame parent)
1133 (with-slots (rx ry rw rh window) frame
1134 (setf rx nx ry ny
1135 rw (max nw 1)
1136 rh (max nh 1)))))
1139 (defun adapt-child-to-rect (rect)
1140 (let ((window (typecase (child-rect-child rect)
1141 (xlib:window (when (managed-window-p (child-rect-child rect) (child-rect-parent rect))
1142 (child-rect-child rect)))
1143 (frame (frame-window (child-rect-child rect))))))
1144 (when window
1145 (let ((change (or (/= (x-drawable-x window) (child-rect-x rect))
1146 (/= (x-drawable-y window) (child-rect-y rect))
1147 (/= (x-drawable-width window) (child-rect-w rect))
1148 (/= (x-drawable-height window) (child-rect-h rect)))))
1149 (when change
1150 (setf (x-drawable-x window) (child-rect-x rect)
1151 (x-drawable-y window) (child-rect-y rect)
1152 (x-drawable-width window) (child-rect-w rect)
1153 (x-drawable-height window) (child-rect-h rect)))
1154 change))))
1159 (defun show-all-children (&optional (from-root-frame nil))
1160 "Show all children and hide those not in a root frame"
1161 (declare (ignore from-root-frame))
1162 (let ((geometry-change nil)
1163 (displayed-child nil)
1164 (hidden-child nil))
1165 (labels ((in-displayed-list (child)
1166 (member child displayed-child :test (lambda (c rect)
1167 (child-equal-p c (child-rect-child rect)))))
1169 (add-in-hidden-list (child)
1170 (pushnew child hidden-child :test #'child-equal-p))
1172 (set-geometry (child parent in-current-root child-current-root-p)
1173 (if (or in-current-root child-current-root-p)
1174 (when (frame-p child)
1175 (adapt-frame-to-parent child (if child-current-root-p nil parent)))
1176 (add-in-hidden-list child)))
1178 (recurse-on-frame-child (child in-current-root child-current-root-p selected-p)
1179 (let ((selected-child (frame-selected-child child)))
1180 (dolist (sub-child (frame-child child))
1181 (rec sub-child child
1182 (and selected-p (child-equal-p sub-child selected-child))
1183 (or in-current-root child-current-root-p)))))
1185 (hidden-child-p (rect)
1186 (dolist (r displayed-child)
1187 (when (and (rect-hidden-p r rect)
1188 (or (not (xlib:window-p (child-rect-child r)))
1189 (eq (window-type (child-rect-child r)) :normal)))
1190 (return t))))
1192 (select-and-display (child parent selected-p)
1193 (multiple-value-bind (nx ny nw nh)
1194 (get-parent-layout child parent)
1195 (let ((rect (make-child-rect :child child :parent parent
1196 :selected-p selected-p
1197 :x nx :y ny :w nw :h nh)))
1198 (if (and *show-hide-policy* (hidden-child-p rect))
1199 (add-in-hidden-list child)
1200 (push rect displayed-child)))))
1202 (display-displayed-child ()
1203 (let ((previous nil))
1204 (dolist (rect (nreverse displayed-child))
1205 (when (adapt-child-to-rect rect)
1206 (setf geometry-change t))
1207 (select-child (child-rect-child rect) (child-rect-selected-p rect))
1208 (show-child (child-rect-child rect)
1209 (child-rect-parent rect)
1210 previous)
1211 (setf previous (child-rect-child rect)))))
1213 (rec (child parent selected-p in-current-root)
1214 (let ((child-current-root-p (child-root-p child)))
1215 (unless (in-displayed-list child)
1216 (set-geometry child parent in-current-root child-current-root-p))
1217 (when (frame-p child)
1218 (recurse-on-frame-child child in-current-root child-current-root-p selected-p))
1219 (when (and (or in-current-root child-current-root-p)
1220 (not (in-displayed-list child)))
1221 (select-and-display child parent selected-p)))))
1223 (rec *root-frame* nil t (child-root-p *root-frame*))
1224 (display-displayed-child)
1225 (dolist (child hidden-child)
1226 (hide-child child))
1227 (set-focus-to-current-child)
1228 (xlib:display-finish-output *display*)
1229 geometry-change)))
1234 (defun hide-all-children (root &optional except)
1235 "Hide all root children"
1236 (when (and (frame-p root) (not (child-equal-p root except)))
1237 (dolist (child (frame-child root))
1238 (hide-all child except))))
1240 (defun hide-all (root &optional except)
1241 "Hide root and all its children"
1242 (unless (child-equal-p root except)
1243 (hide-child root))
1244 (hide-all-children root except))
1250 (defun focus-child (child parent)
1251 "Focus child - Return true if something has change"
1252 (when (and (frame-p parent)
1253 (child-member child (frame-child parent)))
1254 (when (not (child-equal-p child (frame-selected-child parent)))
1255 (with-slots ((parent-child child) selected-pos) parent
1256 (setf parent-child (nth-insert selected-pos child (child-remove child parent-child))))
1257 t)))
1259 (defun focus-child-rec (child parent)
1260 "Focus child and its parents - Return true if something has change"
1261 (let ((change nil))
1262 (labels ((rec (child parent)
1263 (when (focus-child child parent)
1264 (setf change t))
1265 (when parent
1266 (rec parent (find-parent-frame parent)))))
1267 (rec child parent))
1268 change))
1271 (defun set-current-child-generic (child)
1272 (unless (child-equal-p (current-child) child)
1273 (setf (current-child) child)
1276 (defgeneric set-current-child (child parent window-parent))
1278 (defmethod set-current-child ((child xlib:window) parent window-parent)
1279 (set-current-child-generic (if window-parent parent child)))
1281 (defmethod set-current-child ((child frame) parent window-parent)
1282 (declare (ignore parent window-parent))
1283 (set-current-child-generic child))
1285 (defmethod set-current-child (child parent window-parent)
1286 (declare (ignore child parent window-parent))
1290 (defun set-current-root (child parent window-parent)
1291 "Set current root if parent is not in current root"
1292 (let ((root (find-root child)))
1293 (when (and root window-parent
1294 (not (child-root-p child))
1295 (not (find-child parent (root-child root))))
1296 (change-root root parent)
1297 t)))
1299 (defun focus-all-children (child parent &optional (window-parent t))
1300 "Focus child and its parents -
1301 For window: set current child to window or its parent according to window-parent"
1302 (let ((new-focus (focus-child-rec child parent))
1303 (new-current-child (set-current-child child parent window-parent))
1304 (new-root (set-current-root child parent window-parent)))
1305 (or new-focus new-current-child new-root)))
1310 (defun select-next-level ()
1311 "Select the next level in frame"
1312 (select-current-frame :maybe)
1313 (when (frame-p (current-child))
1314 (awhen (frame-selected-child (current-child))
1315 (setf (current-child) it)))
1316 (show-all-children))
1318 (defun select-previous-level ()
1319 "Select the previous level in frame"
1320 (unless (child-root-p (current-child))
1321 (select-current-frame :maybe)
1322 (awhen (find-parent-frame (current-child))
1323 (setf (current-child) it))
1324 (show-all-children)))
1327 (defun enter-frame ()
1328 "Enter in the selected frame - ie make it the root frame"
1329 (let ((root (find-root (current-child))))
1330 (when (and root (not (child-equal-p (root-child root) (current-child))))
1331 (change-root root (current-child)))
1332 (show-all-children t)))
1334 (defun leave-frame ()
1335 "Leave the selected frame - ie make its parent the root frame"
1336 (let ((root (find-root (current-child))))
1337 (unless (or (child-equal-p (root-child root) *root-frame*)
1338 (child-original-root-p (root-child root)))
1339 (awhen (and root (find-parent-frame (root-child root)))
1340 (when (frame-p it)
1341 (change-root root it)))
1342 (show-all-children))))
1345 ;;; Other actions (select-next-child, select-next-brother...) are in
1346 ;;; clfswm-circulate-mode.lisp
1350 (defun frame-lower-child ()
1351 "Lower the child in the current frame"
1352 (when (frame-p (current-child))
1353 (with-slots (child selected-pos) (current-child)
1354 (unless (>= selected-pos (length child))
1355 (when (nth (1+ selected-pos) child)
1356 (rotatef (nth selected-pos child)
1357 (nth (1+ selected-pos) child)))
1358 (incf selected-pos)))
1359 (show-all-children)))
1362 (defun frame-raise-child ()
1363 "Raise the child in the current frame"
1364 (when (frame-p (current-child))
1365 (with-slots (child selected-pos) (current-child)
1366 (unless (< selected-pos 1)
1367 (when (nth (1- selected-pos) child)
1368 (rotatef (nth selected-pos child)
1369 (nth (1- selected-pos) child)))
1370 (decf selected-pos)))
1371 (show-all-children)))
1374 (defun frame-select-next-child ()
1375 "Select the next child in the current frame"
1376 (when (frame-p (current-child))
1377 (with-slots (child selected-pos) (current-child)
1378 (setf selected-pos (mod (1+ selected-pos) (max (length child) 1))))
1379 (show-all-children)))
1382 (defun frame-select-previous-child ()
1383 "Select the previous child in the current frame"
1384 (when (frame-p (current-child))
1385 (with-slots (child selected-pos) (current-child)
1386 (setf selected-pos (mod (1- selected-pos) (max (length child) 1))))
1387 (show-all-children)))
1391 (defun switch-to-root-frame (&key (show-later nil))
1392 "Switch to the root frame"
1393 (let ((root (find-root (current-child))))
1394 (when root
1395 (change-root root (root-original root)))
1396 (unless show-later
1397 (show-all-children t))))
1399 (defun switch-and-select-root-frame (&key (show-later nil))
1400 "Switch and select the root frame"
1401 (let ((root (find-root (current-child))))
1402 (when root
1403 (change-root root (root-original root))
1404 (setf (current-child) (root-original root)))
1405 (unless show-later
1406 (show-all-children t))))
1409 (defun toggle-show-root-frame ()
1410 "Show/Hide the root frame"
1411 (setf *show-root-frame-p* (not *show-root-frame-p*))
1412 (show-all-children))
1416 (defun prevent-current-*-equal-child (child)
1417 " Prevent current-root and current-child equal to child"
1418 (if (child-original-root-p child)
1420 (progn
1421 (awhen (child-root-p child)
1422 (change-root it (find-parent-frame child)))
1423 (when (child-equal-p child (current-child))
1424 (awhen (find-root child)
1425 (setf (current-child) (root-child it))))
1426 t)))
1430 (defun remove-child-in-frame (child frame)
1431 "Remove the child in frame"
1432 (when (frame-p frame)
1433 (setf (frame-child frame) (child-remove child (frame-child frame)))))
1435 (defun remove-child-in-frames (child root)
1436 "Remove child in the frame root and in all its children"
1437 (with-all-frames (root frame)
1438 (remove-child-in-frame child frame)))
1441 (defun remove-child-in-all-frames (child)
1442 "Remove child in all frames from *root-frame*"
1443 (when (prevent-current-*-equal-child child)
1444 (remove-child-in-frames child *root-frame*)))
1447 (defun delete-child-in-frames (child root &optional (close-methode 'delete-window))
1448 "Delete child in the frame root and in all its children
1449 Warning:frame window and gc are freeed."
1450 (with-all-frames (root frame)
1451 (remove-child-in-frame child frame)
1452 (unless (find-frame-window (frame-window frame))
1453 (awhen (frame-gc frame) (xlib:free-gcontext it) (setf it nil))
1454 (awhen (frame-window frame) (xlib:destroy-window it) (setf it nil))))
1455 (when (xlib:window-p child)
1456 (funcall close-methode child)
1457 (netwm-remove-in-client-list child)))
1461 (defun delete-child-in-all-frames (child &optional (close-methode 'delete-window))
1462 "Delete child in all frames from *root-frame*"
1463 (when (prevent-current-*-equal-child child)
1464 (delete-child-in-frames child *root-frame* close-methode)))
1466 (defun delete-child-and-children-in-frames (child root &optional (close-methode 'delete-window))
1467 "Delete child and its children in the frame root and in all its children
1468 Warning:frame window and gc are freeed."
1469 (when (and (frame-p child) (frame-child child))
1470 (dolist (ch (frame-child child))
1471 (delete-child-and-children-in-frames ch root close-methode)))
1472 (delete-child-in-frames child root close-methode))
1474 (defun delete-child-and-children-in-all-frames (child &optional (close-methode 'delete-window))
1475 "Delete child and its children in all frames from *root-frame*"
1476 (when (prevent-current-*-equal-child child)
1477 (when (frame-p child)
1478 (delete-child-and-children-in-frames child *root-frame* close-methode))
1479 (when (xlib:window-p child)
1480 (funcall close-methode child))))
1483 (defun clean-windows-in-all-frames ()
1484 "Remove all xlib:windows present in *root-frame* and not in the xlib tree"
1485 (let ((x-tree (xlib:query-tree *root*)))
1486 (with-all-frames (*root-frame* frame)
1487 (dolist (child (frame-child frame))
1488 (when (xlib:window-p child)
1489 (unless (member child x-tree :test #'xlib:window-equal)
1490 (when (prevent-current-*-equal-child child)
1491 (setf (frame-child frame)
1492 (child-remove child (frame-child frame))))))))))
1496 (defun do-all-frames-nw-hook (window)
1497 "Call nw-hook of each frame."
1498 (catch 'nw-hook-loop
1499 (let ((found nil))
1500 (with-all-frames (*root-frame* frame)
1501 (awhen (frame-nw-hook frame)
1502 (setf found (call-hook it frame window))))
1503 found)))
1507 (defun process-new-window (window)
1508 "When a new window is created (or when we are scanning initial
1509 windows), this function dresses the window up and gets it ready to be
1510 managed."
1511 (setf (xlib:window-event-mask window) *window-events*)
1512 (set-window-state window +normal-state+)
1513 (setf (x-drawable-border-width window) (case (window-type window)
1514 (:normal *border-size*)
1515 (:maxsize 0)
1516 (:transient *border-size*)
1517 (:dialog *border-size*)
1518 (t *border-size*)))
1519 (grab-all-buttons window)
1520 (unless (never-managed-window-p window)
1521 (unless (do-all-frames-nw-hook window)
1522 (call-hook *default-nw-hook* *root-frame* window)))
1523 (netwm-add-in-client-list window))
1528 (defun with-all-mapped-windows (screen fun)
1529 (let ((all-windows (get-all-windows)))
1530 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1531 (unless (child-member win all-windows)
1532 (let ((map-state (xlib:window-map-state win))
1533 (wm-state (window-state win)))
1534 (unless (or (eql (xlib:window-override-redirect win) :on)
1535 (eql win *no-focus-window*)
1536 (is-notify-window-p win))
1537 (when (or (eql map-state :viewable)
1538 (eql wm-state +iconic-state+))
1539 (funcall fun win))))))))
1541 (defun store-root-background ()
1542 (with-all-mapped-windows *screen* #'hide-window)
1543 (setf *background-image* (xlib:create-pixmap :width (xlib:screen-width *screen*)
1544 :height (xlib:screen-height *screen*)
1545 :depth (xlib:screen-root-depth *screen*)
1546 :drawable *root*)
1547 *background-gc* (xlib:create-gcontext :drawable *background-image*
1548 :foreground (get-color *frame-foreground*)
1549 :background (get-color *frame-background*)
1550 :font *default-font*
1551 :line-style :solid))
1552 (xlib:copy-area *root* *background-gc*
1553 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*)
1554 *background-image* 0 0)
1555 (with-all-mapped-windows *screen* #'unhide-window))
1560 (defun hide-existing-windows (screen)
1561 "Hide all existing windows in screen"
1562 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1563 (hide-window win)))
1567 (defun process-existing-windows (screen)
1568 "Windows present when clfswm starts up must be absorbed by clfswm."
1569 (setf *in-process-existing-windows* t)
1570 (let ((id-list nil)
1571 (all-windows (get-all-windows)))
1572 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1573 (unless (child-member win all-windows)
1574 (let ((map-state (xlib:window-map-state win))
1575 (wm-state (window-state win)))
1576 (unless (or (eql (xlib:window-override-redirect win) :on)
1577 (eql win *no-focus-window*)
1578 (is-notify-window-p win)
1579 (never-managed-window-p win))
1580 (when (or (eql map-state :viewable)
1581 (eql wm-state +iconic-state+))
1582 (format t "Processing ~S: type=~A ~S~%" (xlib:wm-name win) (window-type win) win)
1583 (unhide-window win)
1584 (process-new-window win)
1585 (map-window win)
1586 (raise-window win)
1587 (pushnew (xlib:window-id win) id-list))))))
1588 (netwm-set-client-list id-list))
1589 (setf *in-process-existing-windows* nil))
1592 ;;; Child order manipulation functions
1593 (defun put-child-on-top (child parent)
1594 "Put the child on top of its parent children"
1595 (when (frame-p parent)
1596 (setf (frame-child parent) (cons child (child-remove child (frame-child parent)))
1597 (frame-selected-pos parent) 0)))
1599 (defun put-child-on-bottom (child parent)
1600 "Put the child at the bottom of its parent children"
1601 (when (frame-p parent)
1602 (setf (frame-child parent) (append (child-remove child (frame-child parent)) (list child))
1603 (frame-selected-pos parent) 0)))