Remove overlapping heads instead of equal heads
[clfswm.git] / src / clfswm-internal.lisp
blobe358998761a1112424856ac3db861c213f49f96d
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)))))
493 (defun null-size-window-in-frame (frame)
494 (let ((null-size-window-p nil))
495 (with-all-windows (frame window)
496 (when (null-size-window-p window)
497 (setf null-size-window-p t)))
498 null-size-window-p))
502 (defun create-frame-window ()
503 (let ((win (xlib:create-window :parent *root*
504 :x 0
505 :y 0
506 :width 200
507 :height 200
508 :background (get-color *frame-background*)
509 :colormap (xlib:screen-default-colormap *screen*)
510 :border-width *border-size*
511 :border (get-color *color-selected*)
512 :event-mask '(:exposure :button-press :button-release :pointer-motion :enter-window))))
513 (setf (window-transparency win) *frame-transparency*)
514 win))
516 (defun create-frame-gc (window)
517 (xlib:create-gcontext :drawable window
518 :foreground (get-color *frame-foreground*)
519 :background (get-color *frame-background*)
520 :font *default-font*
521 :line-style :solid))
524 (defun destroy-all-frames-window ()
525 (with-all-frames (*root-frame* frame)
526 (when (frame-gc frame)
527 (xlib:free-gcontext (frame-gc frame))
528 (setf (frame-gc frame) nil))
529 (when (frame-window frame)
530 (xlib:destroy-window (frame-window frame))
531 (setf (frame-window frame) nil))))
533 (defun create-all-frames-window ()
534 (with-all-frames (*root-frame* frame)
535 (unless (frame-window frame)
536 (setf (frame-window frame) (create-frame-window)))
537 (unless (frame-gc frame)
538 (setf (frame-gc frame) (create-frame-gc (frame-window frame)))))
539 (with-all-frames (*root-frame* frame)
540 (dolist (child (frame-child frame))
541 (handler-case
542 (dbg (child-fullname child))
543 (error (c)
544 (setf (frame-child frame) (remove child (frame-child frame) :test #'child-equal-p))
545 (dbg c child))))))
550 (defun frame-find-free-number ()
551 (let ((all-numbers nil))
552 (with-all-frames (*root-frame* frame)
553 (pushnew (frame-number frame) all-numbers))
554 (find-free-number all-numbers)))
557 (defun create-frame (&rest args &key (number (frame-find-free-number)) &allow-other-keys)
558 (let* ((window (create-frame-window))
559 (gc (create-frame-gc window)))
560 (apply #'make-instance 'frame :number number :window window :gc gc args)))
563 (defun add-frame (frame parent)
564 (push frame (frame-child parent))
565 frame)
568 (defun place-frame (frame parent prx pry prw prh)
569 "Place a frame from real (pixel) coordinates"
570 (when (and (frame-p frame) (frame-p parent))
571 (with-slots (window x y w h) frame
572 (setf (x-drawable-x window) prx
573 (x-drawable-y window) pry
574 (x-drawable-width window) prw
575 (x-drawable-height window) prh
576 x (x-px->fl prx parent)
577 y (y-px->fl pry parent)
578 w (w-px->fl prw parent)
579 h (h-px->fl prh parent))
580 (xlib:display-finish-output *display*))))
584 (defun find-child (to-find root)
585 "Find to-find in root or in its children"
586 (with-all-children (root child)
587 (when (child-equal-p child to-find)
588 (return-from find-child t))))
592 (defmacro with-find-in-all-frames (test &optional return-value)
593 `(let (ret)
594 (block return-block
595 (with-all-frames (root frame)
596 (when ,test
597 (if first-foundp
598 (return-from return-block (or ,return-value frame))
599 (setf ret frame))))
600 (or ,return-value ret))))
602 (defun find-parent-frame (to-find &optional (root *root-frame*) first-foundp)
603 "Return the parent frame of to-find"
604 (with-find-in-all-frames
605 (child-member to-find (frame-child frame))))
607 (defun find-frame-window (window &optional (root *root-frame*) first-foundp)
608 "Return the frame with the window window"
609 (with-find-in-all-frames
610 (xlib:window-equal window (frame-window frame))))
612 (defun find-frame-by-name (name &optional (root *root-frame*) first-foundp)
613 "Find a frame from its name"
614 (when name
615 (with-find-in-all-frames
616 (string-equal name (frame-name frame)))))
618 (defun find-frame-by-number (number &optional (root *root-frame*) first-foundp)
619 "Find a frame from its number"
620 (when (numberp number)
621 (with-find-in-all-frames
622 (= number (frame-number frame)))))
625 (defun find-child-in-parent (child base)
626 "Return t if child is in base or in its parents"
627 (labels ((rec (base)
628 (when (child-equal-p child base)
629 (return-from find-child-in-parent t))
630 (let ((parent (find-parent-frame base)))
631 (when parent
632 (rec parent)))))
633 (rec base)))
635 ;;; Multiple roots support (replace the old *current-root* variable)
636 (let ((root-list nil)
637 (current-child nil))
638 (defun get-root-list ()
639 root-list)
641 (let ((save-root-list nil))
642 (defun save-root-list ()
643 (setf save-root-list nil)
644 (dolist (root root-list)
645 (push (copy-root root) save-root-list)))
646 (defun restore-root-list ()
647 (setf root-list nil)
648 (dolist (root save-root-list)
649 (push (copy-root root) root-list))))
651 (defmacro with-saved-root-list (() &body body)
652 `(progn
653 (save-root-list)
654 ,@body
655 (restore-root-list)))
657 (defun reset-root-list ()
658 (setf root-list nil
659 current-child nil))
661 (defun define-as-root (child x y width height)
662 (push (make-root :child child :original child :current-child nil :x x :y y :w width :h height) root-list))
664 (defun find-root-by-coordinates (x y)
665 (dolist (root root-list)
666 (when (in-rect x y (root-x root) (root-y root) (root-w root) (root-h root))
667 (return root))))
669 (defun root (x &optional y)
670 "Return the root at coordinates (x,y) if y is not nil.
671 Otherwise, return the x nth root in root-list"
672 (if y
673 (find-root-by-coordinates x y)
674 (nth x root-list)))
676 (defun all-root-child ()
677 (loop for root in root-list
678 collect (root-child root)))
680 (defmacro with-all-root-child ((root) &body body)
681 (let ((root-symb (gensym)))
682 `(dolist (,root-symb (get-root-list))
683 (let ((,root (root-child ,root-symb)))
684 ,@body))))
686 (labels ((generic-child-root-p (child function)
687 (dolist (root root-list)
688 (when (child-equal-p child (funcall function root))
689 (return root)))))
690 (defun child-root-p (child)
691 (generic-child-root-p child #'root-child))
693 (defun child-original-root-p (child)
694 (generic-child-root-p child #'root-original)))
696 (defun change-root (old-root new-child)
697 (when (and old-root new-child)
698 (setf (root-child old-root) new-child)))
700 (defun find-root (child)
701 (aif (child-original-root-p child)
703 (awhen (find-parent-frame child)
704 (find-root it))))
706 (defun find-child-in-all-root (child)
707 (dolist (root root-list)
708 (when (find-child child (root-child root))
709 (return-from find-child-in-all-root root))))
711 (defun find-current-root ()
712 (root-child (find-root (current-child))))
714 (defun exchange-root-geometry (root-1 root-2)
715 (when (and root-1 root-2)
716 (rotatef (root-x root-1) (root-x root-2))
717 (rotatef (root-y root-1) (root-y root-2))
718 (rotatef (root-w root-1) (root-w root-2))
719 (rotatef (root-h root-1) (root-h root-2))))
721 (defun rotate-root-geometry ()
722 (let* ((current (first root-list))
723 (orig-x (root-x current))
724 (orig-y (root-y current))
725 (orig-w (root-w current))
726 (orig-h (root-h current)))
727 (dolist (elem (rest root-list))
728 (setf (root-x current) (root-x elem)
729 (root-y current) (root-y elem)
730 (root-w current) (root-w elem)
731 (root-h current) (root-h elem)
732 current elem))
733 (let ((last (car (last root-list))))
734 (setf (root-x last) orig-x
735 (root-y last) orig-y
736 (root-w last) orig-w
737 (root-h last) orig-h))))
739 (defun anti-rotate-root-geometry ()
740 (setf root-list (nreverse root-list))
741 (rotate-root-geometry)
742 (setf root-list (nreverse root-list)))
744 ;;; Current child functions
745 (defun current-child ()
746 current-child)
748 (defun current-child-setter (value)
749 (awhen (find-root value)
750 (setf (root-current-child it) value))
751 (setf current-child value))
753 (defmacro with-current-child ((new-child) &body body)
754 "Temporarly change the current child"
755 (let ((old-child (gensym))
756 (ret (gensym)))
757 `(let ((,old-child (current-child)))
758 (setf (current-child) ,new-child)
759 (let ((,ret (multiple-value-list (progn ,@body))))
760 (setf (current-child) ,old-child)
761 (values-list ,ret)))))
763 (defun child-is-a-current-child-p (child)
764 (find child root-list :test #'child-equal-p :key #'root-current-child)))
766 (defsetf current-child current-child-setter)
768 (defun ensure-at-least-one-root ()
769 (unless (get-root-list)
770 (let ((frame (create-frame)))
771 (add-frame frame *root-frame*)
772 (define-as-root frame 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*))
773 (add-frame (create-frame) frame))))
780 (defun is-in-current-child-p (child)
781 (and (frame-p (current-child))
782 (child-member child (frame-child (current-child)))))
786 (defun fixe-real-size (frame parent)
787 "Fixe real (pixel) coordinates in float coordinates"
788 (when (frame-p frame)
789 (with-slots (x y w h rx ry rw rh) frame
790 (setf x (x-px->fl rx parent)
791 y (y-px->fl ry parent)
792 w (w-px->fl (anti-adj-border-wh rw parent) parent)
793 h (h-px->fl (anti-adj-border-wh rh parent) parent)))))
795 (defun fixe-real-size-current-child ()
796 "Fixe real (pixel) coordinates in float coordinates for children in the current child"
797 (when (frame-p (current-child))
798 (dolist (child (frame-child (current-child)))
799 (fixe-real-size child (current-child)))))
803 ;;; Multiple physical screen helper
804 (defun add-placed-frame-tmp (frame n) ;; For test
805 (add-frame (create-frame :x 0.01 :y 0.01 :w 0.4 :h 0.4) frame)
806 (add-frame (create-frame :x 0.55 :y 0.01 :w 0.4 :h 0.4) frame)
807 (add-frame (create-frame :x 0.03 :y 0.5 :w 0.64 :h 0.44) frame)
808 (when (plusp n)
809 (add-placed-frame-tmp (first (frame-child frame)) (1- n))))
811 (defun parse-xinerama-info (line)
812 (remove nil
813 (mapcar (lambda (string)
814 (parse-integer string :junk-allowed t))
815 (split-string (substitute #\space #\x (substitute #\space #\, line))))))
817 (defun get-connected-heads-size (&optional (fake (string= (getenv "DISPLAY") ":1")))
818 (labels ((heads-info ()
819 (if (not fake)
820 (do-shell "xdpyinfo -ext XINERAMA")
821 (progn
822 (setf *show-root-frame-p* t)
823 (do-shell "echo ' available colormap entries: 256 per subfield
824 red, green, blue masks: 0xff0000, 0xff00, 0xff
825 significant bits in color specification: 8 bits
827 XINERAMA version 1.1 opcode: 150
828 head #0: 500x300 @ 10,10
829 head #1: 480x300 @ 520,20
830 head #2: 600x250 @ 310,330'")))))
831 (when (xlib:query-extension *display* "XINERAMA")
832 (let ((output (heads-info))
833 (sizes nil))
834 (loop for line = (read-line output nil nil)
835 while line
836 do (when (search " head " line)
837 (destructuring-bind (w h x y)
838 (parse-xinerama-info line)
839 (push (list x y w h) sizes))))
840 (remove-if (lambda (size)
841 (destructuring-bind (x y w h) size
842 (dolist (s sizes)
843 (unless (equal s size)
844 (destructuring-bind (x1 y1 w1 h1) s
845 (when (and (>= x x1)
846 (>= y y1)
847 (<= (+ x w) (+ x1 w1))
848 (<= (+ y h) (+ y1 h1)))
849 (return t)))))))
850 sizes)))))
851 ;;'((10 10 500 300) (550 50 400 400) (100 320 400 270))))))
852 ;;'((10 10 500 580) (540 50 470 500))))))
855 (let ((last-sizes nil))
856 (defun place-frames-from-xinerama-infos ()
857 "Place frames according to xdpyinfo/xinerama informations"
858 (let ((sizes (get-connected-heads-size))
859 (width (xlib:screen-width *screen*))
860 (height (xlib:screen-height *screen*)))
861 (labels ((update-root-geometry ()
862 (loop for size in sizes
863 for root in (get-root-list)
864 do (destructuring-bind (x y w h) size
865 (setf (root-x root) x
866 (root-y root) y
867 (root-w root) w
868 (root-h root) h)))
869 (setf last-sizes sizes)
870 :update)
871 (create-root-geometry ()
872 (reset-root-list)
873 ;; Add frames in *root-frame* until we get the same number as screen heads
874 (loop while (< (length (frame-child *root-frame*)) (length sizes))
875 do (let ((frame (create-frame)))
876 (add-frame frame *root-frame*)))
877 ;; On the opposite way: remove frames while there is more than screen heads in *root-frame*
878 (when (and sizes (> (length (frame-child *root-frame*)) (length sizes)))
879 (dotimes (i (- (length (frame-child *root-frame*)) (length sizes)))
880 (let ((deleted-child (pop (frame-child *root-frame*))))
881 (typecase deleted-child
882 (xlib:window (push deleted-child (frame-child (first (frame-child *root-frame*)))))
883 (frame (dolist (child (frame-child deleted-child))
884 (push child (frame-child (first (frame-child *root-frame*)))))))
885 (setf (frame-layout (first (frame-child *root-frame*))) 'tile-space-layout
886 (frame-data-slot (first (frame-child *root-frame*)) :tile-layout-keep-position) :yes))))
887 (loop for size in sizes
888 for frame in (frame-child *root-frame*)
889 do (destructuring-bind (x y w h) size
890 (setf (frame-x frame) (float (/ x width))
891 (frame-y frame) (float (/ y height))
892 (frame-w frame) (float (/ w width))
893 (frame-h frame) (float (/ h height)))
894 ;;(add-placed-frame-tmp frame 2) ;; For tests
895 (unless (frame-child frame)
896 (add-frame (create-frame) frame))
897 (define-as-root frame x y w h)))
898 (setf last-sizes sizes)
899 nil))
900 (format t "Screen sizes: ~A~%" sizes)
901 (if (= (length sizes) (length last-sizes))
902 (update-root-geometry)
903 (create-root-geometry))))))
907 (defun finish-configuring-root ()
908 (ensure-at-least-one-root)
909 (setf (current-child) (first (frame-child (first (frame-child *root-frame*))))))
913 (defun get-all-windows (&optional (root *root-frame*))
914 "Return all windows in root and in its children"
915 (let ((acc nil))
916 (with-all-windows (root window)
917 (push window acc))
918 acc))
920 (defun get-all-frame-windows (&optional (root *root-frame*))
921 "Return all frame windows in root and in its children"
922 (let ((acc nil))
923 (with-all-frames (root frame)
924 (push (frame-window frame) acc))
925 acc))
928 (defun get-hidden-windows ()
929 "Return all hiddens windows"
930 (let ((all-windows (get-all-windows))
931 (hidden-windows (remove-if-not #'window-hidden-p
932 (copy-list (xlib:query-tree *root*)))))
933 (set-difference hidden-windows all-windows)))
937 ;;; Current window utilities
938 (defun get-current-window ()
939 (typecase (current-child)
940 (xlib:window (current-child))
941 (frame (frame-selected-child (current-child)))))
943 (defmacro with-current-window (&body body)
944 "Bind 'window' to the current window"
945 `(let ((window (get-current-window)))
946 (when (xlib:window-p window)
947 ,@body)))
949 (defun get-first-window ()
950 (typecase (current-child)
951 (xlib:window (current-child))
952 (frame (or (first (frame-child (current-child)))
953 (current-child)))))
959 (defun display-frame-info (frame)
960 (when (frame-p frame)
961 (let ((dy (+ (xlib:max-char-ascent *default-font*) (xlib:max-char-descent *default-font*))))
962 (with-slots (name number gc window child hidden-children) frame
963 (setf (xlib:gcontext-background gc) (get-color *frame-background*)
964 (xlib:window-background window) (get-color *frame-background*))
965 (clear-pixmap-buffer window gc)
966 (setf (xlib:gcontext-foreground gc) (get-color (if (and (child-root-p frame)
967 (child-equal-p frame (current-child)))
968 *frame-foreground-root* *frame-foreground*)))
969 (xlib:draw-glyphs *pixmap-buffer* gc 5 dy
970 (format nil "Frame: ~A~A"
971 number
972 (if name (format nil " - ~A" name) "")))
973 (let ((pos dy))
974 (when (child-root-p frame)
975 (when *child-selection*
976 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
977 (with-output-to-string (str)
978 (format str " Selection: ")
979 (dolist (child *child-selection*)
980 (typecase child
981 (xlib:window (format str " ~A " (xlib:wm-name child)))
982 (frame (format str " frame:~A[~A] " (frame-number child)
983 (aif (frame-name child) it "")))))))))
984 (dolist (ch child)
985 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
986 (format nil " ~A" (ensure-printable (child-fullname ch))))))
987 (copy-pixmap-buffer window gc)
988 (values t t)))))
991 (defgeneric rename-child (child name))
993 (defmethod rename-child ((child frame) name)
994 (setf (frame-name child) name)
995 (display-frame-info child))
997 (defmethod rename-child ((child xlib:window) name)
998 (setf (xlib:wm-name child) name))
1000 (defmethod rename-child (child name)
1001 (declare (ignore child name)))
1004 (defun get-parent-layout (child parent)
1005 (aif (child-root-p child)
1006 (values (- (root-x it) (child-border-size child)) (- (root-y it) (child-border-size child))
1007 (root-w it) (root-h it))
1008 (if (or (frame-p child) (managed-window-p child parent))
1009 (if (frame-p parent)
1010 (aif (frame-layout parent)
1011 (funcall it child parent)
1012 (no-layout child parent))
1013 (values (- (child-border-size child)) (- (child-border-size child))
1014 (xlib:screen-width *screen*)
1015 (xlib:screen-height *screen*)))
1016 (values (x-drawable-x child) (x-drawable-y child)
1017 (x-drawable-width child) (x-drawable-height child)))))
1023 (defgeneric adapt-child-to-parent (child parent))
1025 (defmethod adapt-child-to-parent ((window xlib:window) parent)
1026 (when (managed-window-p window parent)
1027 (multiple-value-bind (nx ny nw nh)
1028 (get-parent-layout window parent)
1029 (setf nw (max nw 1) nh (max nh 1))
1030 (let ((change nil))
1031 (when (or (/= (x-drawable-x window) nx)
1032 (/= (x-drawable-y window) ny))
1033 (setf change :moved))
1034 (when (or (/= (x-drawable-width window) nw)
1035 (/= (x-drawable-height window) nh))
1036 (setf change :resized))
1037 (when change
1038 (xlib:with-state (window)
1039 (setf (x-drawable-x window) nx
1040 (x-drawable-y window) ny
1041 (x-drawable-width window) nw
1042 (x-drawable-height window) nh)))
1043 change))))
1046 (defmethod adapt-child-to-parent ((frame frame) parent)
1047 (declare (ignore parent))
1048 (with-slots (rx ry rw rh window) frame
1049 (let ((change nil))
1050 (when (or (/= (x-drawable-x window) rx)
1051 (/= (x-drawable-y window) ry))
1052 (setf change :moved))
1053 (when (or (/= (x-drawable-width window) rw)
1054 (/= (x-drawable-height window) rh))
1055 (setf change :resized))
1056 (when change
1057 (xlib:with-state (window)
1058 (setf (x-drawable-x window) rx
1059 (x-drawable-y window) ry
1060 (x-drawable-width window) rw
1061 (x-drawable-height window) rh)))
1062 change)))
1064 (defmethod adapt-child-to-parent (child parent)
1065 (declare (ignore child parent))
1066 nil)
1069 (defgeneric set-child-stack-order (window child)
1070 (:documentation "Raise window if child is NIL else put window just below child"))
1072 (defmethod set-child-stack-order (window (child xlib:window))
1073 (lower-window window child))
1075 (defmethod set-child-stack-order (window (child frame))
1076 (lower-window window (frame-window child)))
1078 (defmethod set-child-stack-order (window child)
1079 (declare (ignore child))
1080 (raise-window window))
1084 (defgeneric show-child (child parent previous))
1086 (defmethod show-child ((frame frame) parent previous)
1087 (declare (ignore parent))
1088 (with-slots (window show-window-p) frame
1089 (if (and show-window-p
1090 (or *show-root-frame-p* (not (child-root-p frame))))
1091 (progn
1092 (map-window window)
1093 (set-child-stack-order window previous)
1094 (display-frame-info frame))
1095 (hide-window window))))
1099 (defun hide-unmanaged-window-p (parent)
1100 (let ((action (frame-data-slot parent :unmanaged-window-action)))
1101 (case action
1102 (:hide t)
1103 (:show nil)
1104 (t *hide-unmanaged-window*))))
1107 (defmethod show-child ((window xlib:window) parent previous)
1108 (if (or (managed-window-p window parent)
1109 (child-equal-p window (current-child))
1110 (not (hide-unmanaged-window-p parent))
1111 (child-is-a-current-child-p parent))
1112 (progn
1113 (map-window window)
1114 (set-child-stack-order window previous))
1115 (hide-window window)))
1117 (defmethod show-child (child parent raise-p)
1118 (declare (ignore child parent raise-p))
1122 (defgeneric hide-child (child))
1124 (defmethod hide-child ((frame frame))
1125 (with-slots (window) frame
1126 (xlib:unmap-window window)))
1128 (defmethod hide-child ((window xlib:window))
1129 (hide-window window))
1131 (defmethod hide-child (child)
1132 (declare (ignore child))
1136 (defgeneric select-child (child selected))
1138 (labels ((get-selected-color (child selected-p)
1139 (get-color (cond ((child-equal-p child (current-child)) *color-selected*)
1140 (selected-p *color-maybe-selected*)
1141 (t *color-unselected*)))))
1142 (defmethod select-child ((frame frame) selected-p)
1143 (when (and (frame-p frame) (frame-window frame))
1144 (setf (xlib:window-border (frame-window frame))
1145 (get-selected-color frame selected-p))))
1147 (defmethod select-child ((window xlib:window) selected-p)
1148 (setf (xlib:window-border window)
1149 (get-selected-color window selected-p)))
1151 (defmethod select-child (child selected)
1152 (declare (ignore child selected))
1153 ()))
1155 (defun select-current-frame (selected)
1156 (select-child (current-child) selected))
1158 (defun unselect-all-frames ()
1159 (with-all-children (*root-frame* child)
1160 (select-child child nil)))
1164 (defun set-focus-to-current-child ()
1165 (labels ((rec (child)
1166 (typecase child
1167 (xlib:window (focus-window child))
1168 (frame (rec (frame-selected-child child))))))
1169 (no-focus)
1170 (rec (current-child))))
1175 (defun adapt-frame-to-parent (frame parent)
1176 (multiple-value-bind (nx ny nw nh)
1177 (get-parent-layout frame parent)
1178 (with-slots (rx ry rw rh window) frame
1179 (setf rx nx ry ny
1180 rw (max nw 1)
1181 rh (max nh 1)))))
1184 (defun adapt-child-to-rect (rect)
1185 (let ((window (typecase (child-rect-child rect)
1186 (xlib:window (when (managed-window-p (child-rect-child rect) (child-rect-parent rect))
1187 (child-rect-child rect)))
1188 (frame (frame-window (child-rect-child rect))))))
1189 (when window
1190 (let ((change (or (/= (x-drawable-x window) (child-rect-x rect))
1191 (/= (x-drawable-y window) (child-rect-y rect))
1192 (/= (x-drawable-width window) (child-rect-w rect))
1193 (/= (x-drawable-height window) (child-rect-h rect)))))
1194 (when change
1195 (setf (x-drawable-x window) (child-rect-x rect)
1196 (x-drawable-y window) (child-rect-y rect)
1197 (x-drawable-width window) (child-rect-w rect)
1198 (x-drawable-height window) (child-rect-h rect)))
1199 change))))
1204 (defun show-all-children (&optional (from-root-frame nil))
1205 "Show all children and hide those not in a root frame"
1206 (declare (ignore from-root-frame))
1207 (let ((geometry-change nil)
1208 (displayed-child nil)
1209 (hidden-child nil))
1210 (labels ((in-displayed-list (child)
1211 (member child displayed-child :test (lambda (c rect)
1212 (child-equal-p c (child-rect-child rect)))))
1214 (add-in-hidden-list (child)
1215 (pushnew child hidden-child :test #'child-equal-p))
1217 (set-geometry (child parent in-current-root child-current-root-p)
1218 (if (or in-current-root child-current-root-p)
1219 (when (frame-p child)
1220 (adapt-frame-to-parent child (if child-current-root-p nil parent)))
1221 (add-in-hidden-list child)))
1223 (recurse-on-frame-child (child in-current-root child-current-root-p selected-p)
1224 (let ((selected-child (frame-selected-child child)))
1225 (dolist (sub-child (frame-child child))
1226 (rec sub-child child
1227 (and selected-p (child-equal-p sub-child selected-child))
1228 (or in-current-root child-current-root-p)))))
1230 (hidden-child-p (rect)
1231 (dolist (r displayed-child)
1232 (when (and (rect-hidden-p r rect)
1233 (or (not (xlib:window-p (child-rect-child r)))
1234 (eq (window-type (child-rect-child r)) :normal)))
1235 (return t))))
1237 (select-and-display (child parent selected-p)
1238 (multiple-value-bind (nx ny nw nh)
1239 (get-parent-layout child parent)
1240 (let ((rect (make-child-rect :child child :parent parent
1241 :selected-p selected-p
1242 :x nx :y ny :w nw :h nh)))
1243 (if (and *show-hide-policy* (hidden-child-p rect))
1244 (add-in-hidden-list child)
1245 (push rect displayed-child)))))
1247 (display-displayed-child ()
1248 (let ((previous nil))
1249 (dolist (rect (nreverse displayed-child))
1250 (when (adapt-child-to-rect rect)
1251 (setf geometry-change t))
1252 (select-child (child-rect-child rect) (child-rect-selected-p rect))
1253 (show-child (child-rect-child rect)
1254 (child-rect-parent rect)
1255 previous)
1256 (setf previous (child-rect-child rect)))))
1258 (rec (child parent selected-p in-current-root)
1259 (let ((child-current-root-p (child-root-p child)))
1260 (unless (in-displayed-list child)
1261 (set-geometry child parent in-current-root child-current-root-p))
1262 (when (frame-p child)
1263 (recurse-on-frame-child child in-current-root child-current-root-p selected-p))
1264 (when (and (or in-current-root child-current-root-p)
1265 (not (in-displayed-list child)))
1266 (select-and-display child parent selected-p)))))
1268 (rec *root-frame* nil t (child-root-p *root-frame*))
1269 (display-displayed-child)
1270 (dolist (child hidden-child)
1271 (hide-child child))
1272 (set-focus-to-current-child)
1273 (xlib:display-finish-output *display*)
1274 geometry-change)))
1279 (defun hide-all-children (root &optional except)
1280 "Hide all root children"
1281 (when (and (frame-p root) (not (child-equal-p root except)))
1282 (dolist (child (frame-child root))
1283 (hide-all child except))))
1285 (defun hide-all (root &optional except)
1286 "Hide root and all its children"
1287 (unless (child-equal-p root except)
1288 (hide-child root))
1289 (hide-all-children root except))
1295 (defun focus-child (child parent)
1296 "Focus child - Return true if something has change"
1297 (when (and (frame-p parent)
1298 (child-member child (frame-child parent)))
1299 (when (not (child-equal-p child (frame-selected-child parent)))
1300 (with-slots ((parent-child child) selected-pos) parent
1301 (setf parent-child (nth-insert selected-pos child (child-remove child parent-child))))
1302 t)))
1304 (defun focus-child-rec (child parent)
1305 "Focus child and its parents - Return true if something has change"
1306 (let ((change nil))
1307 (labels ((rec (child parent)
1308 (when (focus-child child parent)
1309 (setf change t))
1310 (when parent
1311 (rec parent (find-parent-frame parent)))))
1312 (rec child parent))
1313 change))
1316 (defun set-current-child-generic (child)
1317 (unless (child-equal-p (current-child) child)
1318 (setf (current-child) child)
1321 (defgeneric set-current-child (child parent window-parent))
1323 (defmethod set-current-child ((child xlib:window) parent window-parent)
1324 (set-current-child-generic (if window-parent parent child)))
1326 (defmethod set-current-child ((child frame) parent window-parent)
1327 (declare (ignore parent window-parent))
1328 (set-current-child-generic child))
1330 (defmethod set-current-child (child parent window-parent)
1331 (declare (ignore child parent window-parent))
1335 (defun set-current-root (child parent window-parent)
1336 "Set current root if parent is not in current root"
1337 (let ((root (find-root child)))
1338 (when (and root window-parent
1339 (not (child-root-p child))
1340 (not (find-child parent (root-child root))))
1341 (change-root root parent)
1342 t)))
1344 (defun focus-all-children (child parent &optional (window-parent t))
1345 "Focus child and its parents -
1346 For window: set current child to window or its parent according to window-parent"
1347 (let ((new-focus (focus-child-rec child parent))
1348 (new-current-child (set-current-child child parent window-parent))
1349 (new-root (set-current-root child parent window-parent)))
1350 (or new-focus new-current-child new-root)))
1355 (defun select-next-level ()
1356 "Select the next level in frame"
1357 (select-current-frame :maybe)
1358 (when (frame-p (current-child))
1359 (awhen (frame-selected-child (current-child))
1360 (setf (current-child) it)))
1361 (show-all-children))
1363 (defun select-previous-level ()
1364 "Select the previous level in frame"
1365 (unless (child-root-p (current-child))
1366 (select-current-frame :maybe)
1367 (awhen (find-parent-frame (current-child))
1368 (setf (current-child) it))
1369 (show-all-children)))
1372 (defun enter-frame ()
1373 "Enter in the selected frame - ie make it the root frame"
1374 (let ((root (find-root (current-child))))
1375 (when (and root (not (child-equal-p (root-child root) (current-child))))
1376 (change-root root (current-child)))
1377 (show-all-children t)))
1379 (defun leave-frame ()
1380 "Leave the selected frame - ie make its parent the root frame"
1381 (let ((root (find-root (current-child))))
1382 (unless (or (child-equal-p (root-child root) *root-frame*)
1383 (child-original-root-p (root-child root)))
1384 (awhen (and root (find-parent-frame (root-child root)))
1385 (when (frame-p it)
1386 (change-root root it)))
1387 (show-all-children))))
1390 ;;; Other actions (select-next-child, select-next-brother...) are in
1391 ;;; clfswm-circulate-mode.lisp
1395 (defun frame-lower-child ()
1396 "Lower the child in the current frame"
1397 (when (frame-p (current-child))
1398 (with-slots (child selected-pos) (current-child)
1399 (unless (>= selected-pos (length child))
1400 (when (nth (1+ selected-pos) child)
1401 (rotatef (nth selected-pos child)
1402 (nth (1+ selected-pos) child)))
1403 (incf selected-pos)))
1404 (show-all-children)))
1407 (defun frame-raise-child ()
1408 "Raise the child in the current frame"
1409 (when (frame-p (current-child))
1410 (with-slots (child selected-pos) (current-child)
1411 (unless (< selected-pos 1)
1412 (when (nth (1- selected-pos) child)
1413 (rotatef (nth selected-pos child)
1414 (nth (1- selected-pos) child)))
1415 (decf selected-pos)))
1416 (show-all-children)))
1419 (defun frame-select-next-child ()
1420 "Select the next child in the current frame"
1421 (when (frame-p (current-child))
1422 (with-slots (child selected-pos) (current-child)
1423 (setf selected-pos (mod (1+ selected-pos) (max (length child) 1))))
1424 (show-all-children)))
1427 (defun frame-select-previous-child ()
1428 "Select the previous child in the current frame"
1429 (when (frame-p (current-child))
1430 (with-slots (child selected-pos) (current-child)
1431 (setf selected-pos (mod (1- selected-pos) (max (length child) 1))))
1432 (show-all-children)))
1436 (defun switch-to-root-frame (&key (show-later nil))
1437 "Switch to the root frame"
1438 (let ((root (find-root (current-child))))
1439 (when root
1440 (change-root root (root-original root)))
1441 (unless show-later
1442 (show-all-children t))))
1444 (defun switch-and-select-root-frame (&key (show-later nil))
1445 "Switch and select the root frame"
1446 (let ((root (find-root (current-child))))
1447 (when root
1448 (change-root root (root-original root))
1449 (setf (current-child) (root-original root)))
1450 (unless show-later
1451 (show-all-children t))))
1454 (defun toggle-show-root-frame ()
1455 "Show/Hide the root frame"
1456 (setf *show-root-frame-p* (not *show-root-frame-p*))
1457 (show-all-children))
1461 (defun prevent-current-*-equal-child (child)
1462 " Prevent current-root and current-child equal to child"
1463 (if (child-original-root-p child)
1465 (progn
1466 (awhen (child-root-p child)
1467 (change-root it (find-parent-frame child)))
1468 (when (child-equal-p child (current-child))
1469 (awhen (find-root child)
1470 (setf (current-child) (root-child it))))
1471 t)))
1475 (defun remove-child-in-frame (child frame)
1476 "Remove the child in frame"
1477 (when (frame-p frame)
1478 (setf (frame-child frame) (child-remove child (frame-child frame)))))
1480 (defun remove-child-in-frames (child root)
1481 "Remove child in the frame root and in all its children"
1482 (with-all-frames (root frame)
1483 (remove-child-in-frame child frame)))
1486 (defun remove-child-in-all-frames (child)
1487 "Remove child in all frames from *root-frame*"
1488 (when (prevent-current-*-equal-child child)
1489 (remove-child-in-frames child *root-frame*)))
1492 (defun delete-child-in-frames (child root &optional (close-methode 'delete-window))
1493 "Delete child in the frame root and in all its children
1494 Warning:frame window and gc are freeed."
1495 (with-all-frames (root frame)
1496 (remove-child-in-frame child frame)
1497 (unless (find-frame-window (frame-window frame))
1498 (awhen (frame-gc frame) (xlib:free-gcontext it) (setf it nil))
1499 (awhen (frame-window frame) (xlib:destroy-window it) (setf it nil))))
1500 (when (xlib:window-p child)
1501 (funcall close-methode child)
1502 (netwm-remove-in-client-list child)))
1506 (defun delete-child-in-all-frames (child &optional (close-methode 'delete-window))
1507 "Delete child in all frames from *root-frame*"
1508 (when (prevent-current-*-equal-child child)
1509 (delete-child-in-frames child *root-frame* close-methode)))
1511 (defun delete-child-and-children-in-frames (child root &optional (close-methode 'delete-window))
1512 "Delete child and its children in the frame root and in all its children
1513 Warning:frame window and gc are freeed."
1514 (when (and (frame-p child) (frame-child child))
1515 (dolist (ch (frame-child child))
1516 (delete-child-and-children-in-frames ch root close-methode)))
1517 (delete-child-in-frames child root close-methode))
1519 (defun delete-child-and-children-in-all-frames (child &optional (close-methode 'delete-window))
1520 "Delete child and its children in all frames from *root-frame*"
1521 (when (prevent-current-*-equal-child child)
1522 (when (frame-p child)
1523 (delete-child-and-children-in-frames child *root-frame* close-methode))
1524 (when (xlib:window-p child)
1525 (funcall close-methode child))))
1528 (defun clean-windows-in-all-frames ()
1529 "Remove all xlib:windows present in *root-frame* and not in the xlib tree"
1530 (let ((x-tree (xlib:query-tree *root*)))
1531 (with-all-frames (*root-frame* frame)
1532 (dolist (child (frame-child frame))
1533 (when (xlib:window-p child)
1534 (unless (member child x-tree :test #'xlib:window-equal)
1535 (when (prevent-current-*-equal-child child)
1536 (setf (frame-child frame)
1537 (child-remove child (frame-child frame))))))))))
1541 (defun do-all-frames-nw-hook (window)
1542 "Call nw-hook of each frame."
1543 (catch 'nw-hook-loop
1544 (let ((found nil))
1545 (with-all-frames (*root-frame* frame)
1546 (awhen (frame-nw-hook frame)
1547 (setf found (call-hook it frame window))))
1548 found)))
1552 (defun process-new-window (window)
1553 "When a new window is created (or when we are scanning initial
1554 windows), this function dresses the window up and gets it ready to be
1555 managed."
1556 (setf (xlib:window-event-mask window) *window-events*)
1557 (set-window-state window +normal-state+)
1558 (setf (x-drawable-border-width window) (case (window-type window)
1559 (:normal *border-size*)
1560 (:maxsize 0)
1561 (:transient *border-size*)
1562 (:dialog *border-size*)
1563 (t *border-size*)))
1564 (grab-all-buttons window)
1565 (unless (never-managed-window-p window)
1566 (unless (do-all-frames-nw-hook window)
1567 (call-hook *default-nw-hook* *root-frame* window)))
1568 (netwm-add-in-client-list window))
1573 (defun with-all-mapped-windows (screen fun)
1574 (let ((all-windows (get-all-windows)))
1575 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1576 (unless (child-member win all-windows)
1577 (let ((map-state (xlib:window-map-state win))
1578 (wm-state (window-state win)))
1579 (unless (or (eql (xlib:window-override-redirect win) :on)
1580 (eql win *no-focus-window*)
1581 (is-notify-window-p win))
1582 (when (or (eql map-state :viewable)
1583 (eql wm-state +iconic-state+))
1584 (funcall fun win))))))))
1586 (defun store-root-background ()
1587 (with-all-mapped-windows *screen* #'hide-window)
1588 (setf *background-image* (xlib:create-pixmap :width (xlib:screen-width *screen*)
1589 :height (xlib:screen-height *screen*)
1590 :depth (xlib:screen-root-depth *screen*)
1591 :drawable *root*)
1592 *background-gc* (xlib:create-gcontext :drawable *background-image*
1593 :foreground (get-color *frame-foreground*)
1594 :background (get-color *frame-background*)
1595 :font *default-font*
1596 :line-style :solid))
1597 (xlib:copy-area *root* *background-gc*
1598 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*)
1599 *background-image* 0 0)
1600 (with-all-mapped-windows *screen* #'unhide-window))
1605 (defun hide-existing-windows (screen)
1606 "Hide all existing windows in screen"
1607 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1608 (hide-window win)))
1612 (defun process-existing-windows (screen)
1613 "Windows present when clfswm starts up must be absorbed by clfswm."
1614 (setf *in-process-existing-windows* t)
1615 (let ((id-list nil)
1616 (all-windows (get-all-windows))
1617 (all-frame-windows (get-all-frame-windows)))
1618 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1619 (unless (or (child-member win all-windows)
1620 (child-member win all-frame-windows))
1621 (let ((map-state (xlib:window-map-state win))
1622 (wm-state (window-state win)))
1623 (unless (or (eql (xlib:window-override-redirect win) :on)
1624 (eql win *no-focus-window*)
1625 (is-notify-window-p win)
1626 (never-managed-window-p win))
1627 (when (or (eql map-state :viewable)
1628 (eql wm-state +iconic-state+))
1629 (format t "Processing ~S: type=~A ~S~%" (xlib:wm-name win) (window-type win) win)
1630 (unhide-window win)
1631 (process-new-window win)
1632 (map-window win)
1633 (raise-window win)
1634 (pushnew (xlib:window-id win) id-list))))))
1635 (netwm-set-client-list id-list))
1636 (setf *in-process-existing-windows* nil))
1639 ;;; Child order manipulation functions
1640 (defun put-child-on-top (child parent)
1641 "Put the child on top of its parent children"
1642 (when (frame-p parent)
1643 (setf (frame-child parent) (cons child (child-remove child (frame-child parent)))
1644 (frame-selected-pos parent) 0)))
1646 (defun put-child-on-bottom (child parent)
1647 "Put the child at the bottom of its parent children"
1648 (when (frame-p parent)
1649 (setf (frame-child parent) (append (child-remove child (frame-child parent)) (list child))
1650 (frame-selected-pos parent) 0)))