Set windows with max-size greater than root sizes to normal windows type
[clfswm.git] / src / clfswm-internal.lisp
blob819a420ccf22eb568a5e6038ec06b70365bb83e1
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))))
130 (defgeneric in-child (child x y))
132 (defmethod in-child ((child frame) x y)
133 (in-frame child x y))
134 (defmethod in-child ((child xlib:window) x y)
135 (in-window child x y))
136 (defmethod in-child (child x y)
137 (declare (ignore child x y))
138 nil)
143 (defun frame-selected-child (frame)
144 (when (frame-p frame)
145 (with-slots (child selected-pos) frame
146 (let ((len (length child)))
147 (cond ((minusp selected-pos) (setf selected-pos 0))
148 ((>= selected-pos len) (setf selected-pos (max (1- len) 0)))))
149 (nth selected-pos child))))
155 (defgeneric child-equal-p (child-1 child-2))
157 (defmethod child-equal-p ((child-1 xlib:window) (child-2 xlib:window))
158 (xlib:window-equal child-1 child-2))
160 (defmethod child-equal-p ((child-1 frame) (child-2 frame))
161 (equal child-1 child-2))
163 (defmethod child-equal-p (child-1 child-2)
164 (declare (ignore child-1 child-2))
165 nil)
170 (declaim (inline child-member child-remove child-position))
172 (defun child-member (child list)
173 (member child list :test #'child-equal-p))
175 (defun child-remove (child list)
176 (remove child list :test #'child-equal-p))
178 (defun child-position (child list)
179 (position child list :test #'child-equal-p))
183 ;;; Frame data manipulation functions
184 (defun frame-data-slot (frame slot)
185 "Return the value associated to data slot"
186 (when (frame-p frame)
187 (second (assoc slot (frame-data frame)))))
189 (defun set-frame-data-slot (frame slot value)
190 "Set the value associated to data slot"
191 (when (frame-p frame)
192 (with-slots (data) frame
193 (setf data (remove (assoc slot data) data))
194 (push (list slot value) data))
195 value))
197 (defsetf frame-data-slot set-frame-data-slot)
200 (defun remove-frame-data-slot (frame slot)
201 "Remove a slot in frame data slots"
202 (when (frame-p frame)
203 (with-slots (data) frame
204 (setf data (remove (assoc slot data) data)))))
208 (defun managed-window-p (window frame)
209 "Return t only if window is managed by frame"
210 (if (frame-p frame)
211 (with-slots ((managed forced-managed-window)
212 (unmanaged forced-unmanaged-window)) frame
213 (and (xlib:window-p window)
214 (not (child-member window unmanaged))
215 (not (member (xlib:wm-name window) unmanaged :test #'string-equal-p))
216 (or (member :all (frame-managed-type frame))
217 (member (window-type window) (frame-managed-type frame))
218 (child-member window managed)
219 (member (xlib:wm-name window) managed :test #'string-equal-p))))
223 (defun add-in-never-managed-window-list (value)
224 (pushnew value *never-managed-window-list* :test #'equal))
226 (defun never-managed-window-p (window)
227 (when (xlib:window-p window)
228 (dolist (type *never-managed-window-list*)
229 (when (funcall (first type) window)
230 (return (values t (second type)))))))
234 (defgeneric child-name (child))
236 (defmethod child-name ((child xlib:window))
237 (xlib:wm-name child))
239 (defmethod child-name ((child frame))
240 (frame-name child))
242 (defmethod child-name (child)
243 (declare (ignore child))
244 "???")
247 (defgeneric set-child-name (child name))
249 (defmethod set-child-name ((child xlib:window) name)
250 (setf (xlib:wm-name child) name))
252 (defmethod set-child-name ((child frame) name)
253 (setf (frame-name child) name))
255 (defmethod set-child-name (child name)
256 (declare (ignore child name)))
258 (defsetf child-name set-child-name)
263 (defgeneric child-fullname (child))
265 (defmethod child-fullname ((child xlib:window))
266 (format nil "~A (~A)" (or (xlib:wm-name child) "?") (or (xlib:get-wm-class child) "?")))
268 (defmethod child-fullname ((child frame))
269 (aif (frame-name child)
270 (format nil "~A (Frame ~A)" it (frame-number child))
271 (format nil "Frame ~A" (frame-number child))))
273 (defmethod child-fullname (child)
274 (declare (ignore child))
275 "???")
278 (defgeneric child-transparency (child))
280 (defmethod child-transparency ((child xlib:window))
281 (window-transparency child))
283 (defmethod child-transparency ((child frame))
284 (window-transparency (frame-window child)))
286 (defmethod child-transparency (child)
287 (declare (ignore child))
290 (defgeneric set-child-transparency (child value))
292 (defmethod set-child-transparency ((child xlib:window) value)
293 (setf (window-transparency child) value))
295 (defmethod set-child-transparency ((child frame) value)
296 (setf (window-transparency (frame-window child)) value))
298 (defmethod set-child-transparency (child value)
299 (declare (ignore child value)))
301 (defsetf child-transparency set-child-transparency)
305 (defgeneric child-x (child))
306 (defmethod child-x ((child xlib:window))
307 (x-drawable-x child))
308 (defmethod child-x ((child frame))
309 (frame-rx child))
311 (defgeneric child-y (child))
312 (defmethod child-y ((child xlib:window))
313 (x-drawable-y child))
314 (defmethod child-y ((child frame))
315 (frame-ry child))
317 (defgeneric child-width (child))
318 (defmethod child-width ((child xlib:window))
319 (x-drawable-width child))
320 (defmethod child-width ((child frame))
321 (frame-rw child))
323 (defgeneric child-height (child))
324 (defmethod child-height ((child xlib:window))
325 (x-drawable-height child))
326 (defmethod child-height ((child frame))
327 (frame-rh child))
329 (defgeneric child-x2 (child))
330 (defmethod child-x2 ((child xlib:window))
331 (+ (x-drawable-x child) (x-drawable-width child)))
332 (defmethod child-x2 ((child frame))
333 (+ (frame-rx child) (frame-rw child)))
335 (defgeneric child-y2 (child))
336 (defmethod child-y2 ((child xlib:window))
337 (+ (x-drawable-y child) (x-drawable-height child)))
338 (defmethod child-y2 ((child frame))
339 (+ (frame-ry child) (frame-rh child)))
343 (defgeneric child-center (child))
345 (defmethod child-center ((child xlib:window))
346 (values (+ (x-drawable-x child) (/ (x-drawable-width child) 2))
347 (+ (x-drawable-y child) (/ (x-drawable-height child) 2))))
349 (defmethod child-center ((child frame))
350 (values (+ (frame-rx child) (/ (frame-rw child) 2))
351 (+ (frame-ry child) (/ (frame-rh child) 2))))
353 (defun child-distance (child1 child2)
354 (multiple-value-bind (x1 y1) (child-center child1)
355 (multiple-value-bind (x2 y2) (child-center child2)
356 (values (+ (abs (- x2 x1)) (abs (- y2 y1)))
357 (- x2 x1)
358 (- y2 y1)))))
360 (defun middle-child-x (child)
361 (+ (child-x child) (/ (child-width child) 2)))
363 (defun middle-child-y (child)
364 (+ (child-y child) (/ (child-height child) 2)))
366 (declaim (inline adj-border-xy adj-border-wh))
367 (defgeneric adj-border-xy (value child))
368 (defgeneric adj-border-wh (value child))
370 (defmethod adj-border-xy (v (child xlib:window))
371 (+ v (x-drawable-border-width child)))
373 (defmethod adj-border-xy (v (child frame))
374 (+ v (x-drawable-border-width (frame-window child))))
376 (defmethod adj-border-wh (v (child xlib:window))
377 (- v (* (x-drawable-border-width child) 2)))
379 (defmethod adj-border-wh (v (child frame))
380 (- v (* (x-drawable-border-width (frame-window child)) 2)))
383 (declaim (inline anti-adj-border-xy anti-adj-border-wh))
384 (defgeneric anti-adj-border-xy (value child))
385 (defgeneric anti-adj-border-wh (value child))
387 (defmethod anti-adj-border-xy (v (child xlib:window))
388 (- v (x-drawable-border-width child)))
390 (defmethod anti-adj-border-xy (v (child frame))
391 (- v (x-drawable-border-width (frame-window child))))
393 (defmethod anti-adj-border-wh (v (child xlib:window))
394 (+ v (* (x-drawable-border-width child) 2)))
396 (defmethod anti-adj-border-wh (v (child frame))
397 (+ v (* (x-drawable-border-width (frame-window child)) 2)))
402 (defmacro with-focus-window ((window) &body body)
403 `(let ((,window (xlib:input-focus *display*)))
404 (when (and ,window (not (xlib:window-equal ,window *no-focus-window*)))
405 ,@body)))
409 ;; (with-all-children (*root-frame* child) (typecase child (xlib:window (print child)) (frame (print (frame-number child)))))
410 (defmacro with-all-children ((root child) &body body)
411 (let ((rec (gensym))
412 (sub-child (gensym)))
413 `(block nil
414 (labels ((,rec (,child)
415 ,@body
416 (when (frame-p ,child)
417 (dolist (,sub-child (reverse (frame-child ,child)))
418 (,rec ,sub-child)))))
419 (,rec ,root)))))
422 ;; (with-all-children (*root-frame* child) (typecase child (xlib:window (print child)) (frame (print (frame-number child)))))
423 (defmacro with-all-children-reversed ((root child) &body body)
424 (let ((rec (gensym))
425 (sub-child (gensym)))
426 `(block nil
427 (labels ((,rec (,child)
428 ,@body
429 (when (frame-p ,child)
430 (dolist (,sub-child (frame-child ,child))
431 (,rec ,sub-child)))))
432 (,rec ,root)))))
438 ;; (with-all-frames (*root-frame* frame) (print (frame-number frame)))
439 (defmacro with-all-frames ((root frame) &body body)
440 (let ((rec (gensym))
441 (child (gensym)))
442 `(block nil
443 (labels ((,rec (,frame)
444 (when (frame-p ,frame)
445 ,@body
446 (dolist (,child (reverse (frame-child ,frame)))
447 (,rec ,child)))))
448 (,rec ,root)))))
451 ;; (with-all-windows (*root-frame* window) (print window))
452 (defmacro with-all-windows ((root window) &body body)
453 (let ((rec (gensym))
454 (child (gensym)))
455 `(block nil
456 (labels ((,rec (,window)
457 (when (xlib:window-p ,window)
458 ,@body)
459 (when (frame-p ,window)
460 (dolist (,child (reverse (frame-child ,window)))
461 (,rec ,child)))))
462 (,rec ,root)))))
466 ;; (with-all-frames-windows (*root-frame* child) (print child) (print (frame-number child)))
467 (defmacro with-all-windows-frames ((root child) body-window body-frame)
468 (let ((rec (gensym))
469 (sub-child (gensym)))
470 `(block nil
471 (labels ((,rec (,child)
472 (typecase ,child
473 (xlib:window ,body-window)
474 (frame ,body-frame
475 (dolist (,sub-child (reverse (frame-child ,child)))
476 (,rec ,sub-child))))))
477 (,rec ,root)))))
479 (defmacro with-all-windows-frames-and-parent ((root child parent) body-window body-frame)
480 (let ((rec (gensym))
481 (sub-child (gensym)))
482 `(block nil
483 (labels ((,rec (,child ,parent)
484 (typecase ,child
485 (xlib:window ,body-window)
486 (frame ,body-frame
487 (dolist (,sub-child (reverse (frame-child ,child)))
488 (,rec ,sub-child ,child))))))
489 (,rec ,root nil)))))
494 (defun create-frame-window ()
495 (let ((win (xlib:create-window :parent *root*
496 :x 0
497 :y 0
498 :width 200
499 :height 200
500 :background (get-color *frame-background*)
501 :colormap (xlib:screen-default-colormap *screen*)
502 :border-width *border-size*
503 :border (get-color *color-selected*)
504 :event-mask '(:exposure :button-press :button-release :pointer-motion :enter-window))))
505 (setf (window-transparency win) *frame-transparency*)
506 win))
508 (defun create-frame-gc (window)
509 (xlib:create-gcontext :drawable window
510 :foreground (get-color *frame-foreground*)
511 :background (get-color *frame-background*)
512 :font *default-font*
513 :line-style :solid))
516 (defun destroy-all-frames-window ()
517 (with-all-frames (*root-frame* frame)
518 (when (frame-gc frame)
519 (xlib:free-gcontext (frame-gc frame))
520 (setf (frame-gc frame) nil))
521 (when (frame-window frame)
522 (xlib:destroy-window (frame-window frame))
523 (setf (frame-window frame) nil))))
525 (defun create-all-frames-window ()
526 (with-all-frames (*root-frame* frame)
527 (unless (frame-window frame)
528 (setf (frame-window frame) (create-frame-window)))
529 (unless (frame-gc frame)
530 (setf (frame-gc frame) (create-frame-gc (frame-window frame)))))
531 (with-all-frames (*root-frame* frame)
532 (dolist (child (frame-child frame))
533 (handler-case
534 (dbg (child-fullname child))
535 (error (c)
536 (setf (frame-child frame) (remove child (frame-child frame) :test #'child-equal-p))
537 (dbg c child))))))
542 (defun frame-find-free-number ()
543 (let ((all-numbers nil))
544 (with-all-frames (*root-frame* frame)
545 (pushnew (frame-number frame) all-numbers))
546 (find-free-number all-numbers)))
549 (defun create-frame (&rest args &key (number (frame-find-free-number)) &allow-other-keys)
550 (let* ((window (create-frame-window))
551 (gc (create-frame-gc window)))
552 (apply #'make-instance 'frame :number number :window window :gc gc args)))
555 (defun add-frame (frame parent)
556 (push frame (frame-child parent))
557 frame)
560 (defun place-frame (frame parent prx pry prw prh)
561 "Place a frame from real (pixel) coordinates"
562 (when (and (frame-p frame) (frame-p parent))
563 (with-slots (window x y w h) frame
564 (setf (x-drawable-x window) prx
565 (x-drawable-y window) pry
566 (x-drawable-width window) prw
567 (x-drawable-height window) prh
568 x (x-px->fl prx parent)
569 y (y-px->fl pry parent)
570 w (w-px->fl prw parent)
571 h (h-px->fl prh parent))
572 (xlib:display-finish-output *display*))))
576 (defun find-child (to-find root)
577 "Find to-find in root or in its children"
578 (with-all-children (root child)
579 (when (child-equal-p child to-find)
580 (return-from find-child t))))
584 (defmacro with-find-in-all-frames (test &optional return-value)
585 `(let (ret)
586 (block return-block
587 (with-all-frames (root frame)
588 (when ,test
589 (if first-foundp
590 (return-from return-block (or ,return-value frame))
591 (setf ret frame))))
592 (or ,return-value ret))))
594 (defun find-parent-frame (to-find &optional (root *root-frame*) first-foundp)
595 "Return the parent frame of to-find"
596 (with-find-in-all-frames
597 (child-member to-find (frame-child frame))))
599 (defun find-frame-window (window &optional (root *root-frame*) first-foundp)
600 "Return the frame with the window window"
601 (with-find-in-all-frames
602 (xlib:window-equal window (frame-window frame))))
604 (defun find-frame-by-name (name &optional (root *root-frame*) first-foundp)
605 "Find a frame from its name"
606 (when name
607 (with-find-in-all-frames
608 (string-equal name (frame-name frame)))))
610 (defun find-frame-by-number (number &optional (root *root-frame*) first-foundp)
611 "Find a frame from its number"
612 (when (numberp number)
613 (with-find-in-all-frames
614 (= number (frame-number frame)))))
617 (defun find-child-in-parent (child base)
618 "Return t if child is in base or in its parents"
619 (labels ((rec (base)
620 (when (child-equal-p child base)
621 (return-from find-child-in-parent t))
622 (let ((parent (find-parent-frame base)))
623 (when parent
624 (rec parent)))))
625 (rec base)))
627 ;;; Multiple roots support (replace the old *current-root* variable)
628 (let ((root-list nil)
629 (current-child nil))
630 (defun get-root-list ()
631 root-list)
633 (let ((save-root-list nil))
634 (defun save-root-list ()
635 (setf save-root-list nil)
636 (dolist (root root-list)
637 (push (copy-root root) save-root-list)))
638 (defun restore-root-list ()
639 (setf root-list nil)
640 (dolist (root save-root-list)
641 (push (copy-root root) root-list))))
643 (defmacro with-saved-root-list (() &body body)
644 `(progn
645 (save-root-list)
646 ,@body
647 (restore-root-list)))
649 (defun reset-root-list ()
650 (setf root-list nil
651 current-child nil))
653 (defun define-as-root (child x y width height)
654 (push (make-root :child child :original child :current-child nil :x x :y y :w width :h height) root-list))
656 (defun find-root-by-coordinates (x y)
657 (dolist (root root-list)
658 (when (in-rect x y (root-x root) (root-y root) (root-w root) (root-h root))
659 (return root))))
661 (defun root (x &optional y)
662 "Return the root at coordinates (x,y) if y is not nil.
663 Otherwise, return the x nth root in root-list"
664 (if y
665 (find-root-by-coordinates x y)
666 (nth x root-list)))
668 (defun all-root-child ()
669 (loop for root in root-list
670 collect (root-child root)))
672 (defmacro with-all-root-child ((root) &body body)
673 (let ((root-symb (gensym)))
674 `(dolist (,root-symb (get-root-list))
675 (let ((,root (root-child ,root-symb)))
676 ,@body))))
678 (labels ((generic-child-root-p (child function)
679 (dolist (root root-list)
680 (when (child-equal-p child (funcall function root))
681 (return root)))))
682 (defun child-root-p (child)
683 (generic-child-root-p child #'root-child))
685 (defun child-original-root-p (child)
686 (generic-child-root-p child #'root-original)))
688 (defun change-root (old-root new-child)
689 (when (and old-root new-child)
690 (setf (root-child old-root) new-child)))
692 (defun find-root (child)
693 (aif (child-original-root-p child)
695 (awhen (find-parent-frame child)
696 (find-root it))))
698 (defun find-child-in-all-root (child)
699 (dolist (root root-list)
700 (when (find-child child (root-child root))
701 (return-from find-child-in-all-root root))))
703 (defun find-current-root ()
704 (root-child (find-root current-child)))
706 (defun exchange-root-geometry (root-1 root-2)
707 (when (and root-1 root-2)
708 (rotatef (root-x root-1) (root-x root-2))
709 (rotatef (root-y root-1) (root-y root-2))
710 (rotatef (root-w root-1) (root-w root-2))
711 (rotatef (root-h root-1) (root-h root-2))))
713 (defun rotate-root-geometry ()
714 (let* ((current (first root-list))
715 (orig-x (root-x current))
716 (orig-y (root-y current))
717 (orig-w (root-w current))
718 (orig-h (root-h current)))
719 (dolist (elem (rest root-list))
720 (setf (root-x current) (root-x elem)
721 (root-y current) (root-y elem)
722 (root-w current) (root-w elem)
723 (root-h current) (root-h elem)
724 current elem))
725 (let ((last (car (last root-list))))
726 (setf (root-x last) orig-x
727 (root-y last) orig-y
728 (root-w last) orig-w
729 (root-h last) orig-h))))
731 (defun anti-rotate-root-geometry ()
732 (setf root-list (nreverse root-list))
733 (rotate-root-geometry)
734 (setf root-list (nreverse root-list)))
736 ;;; Current child functions
737 (defun current-child ()
738 current-child)
740 (defun current-child-setter (value)
741 (when value
742 (awhen (find-root value)
743 (setf (root-current-child it) value))
744 (setf current-child value)))
746 (defmacro with-current-child ((new-child) &body body)
747 "Temporarly change the current child"
748 (let ((old-child (gensym))
749 (ret (gensym)))
750 `(let ((,old-child (current-child)))
751 (setf (current-child) ,new-child)
752 (let ((,ret (multiple-value-list (progn ,@body))))
753 (setf (current-child) ,old-child)
754 (values-list ,ret)))))
756 (defun child-is-a-current-child-p (child)
757 (find child root-list :test #'child-equal-p :key #'root-current-child)))
759 (defsetf current-child current-child-setter)
761 (defun ensure-at-least-one-root ()
762 (unless (get-root-list)
763 (let ((frame (create-frame)))
764 (add-frame frame *root-frame*)
765 (define-as-root frame 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*))
766 (add-frame (create-frame) frame))))
773 (defun is-in-current-child-p (child)
774 (and (frame-p (current-child))
775 (child-member child (frame-child (current-child)))))
779 (defun fixe-real-size (frame parent)
780 "Fixe real (pixel) coordinates in float coordinates"
781 (when (frame-p frame)
782 (with-slots (x y w h rx ry rw rh) frame
783 (setf x (x-px->fl rx parent)
784 y (y-px->fl ry parent)
785 w (w-px->fl (anti-adj-border-wh rw parent) parent)
786 h (h-px->fl (anti-adj-border-wh rh parent) parent)))))
788 (defun fixe-real-size-current-child ()
789 "Fixe real (pixel) coordinates in float coordinates for children in the current child"
790 (when (frame-p (current-child))
791 (dolist (child (frame-child (current-child)))
792 (fixe-real-size child (current-child)))))
796 ;;; Multiple physical screen helper
797 (defun add-placed-frame-tmp (frame n) ;; For test
798 (add-frame (create-frame :x 0.01 :y 0.01 :w 0.4 :h 0.4) frame)
799 (add-frame (create-frame :x 0.55 :y 0.01 :w 0.4 :h 0.4) frame)
800 (add-frame (create-frame :x 0.03 :y 0.5 :w 0.64 :h 0.44) frame)
801 (when (plusp n)
802 (add-placed-frame-tmp (first (frame-child frame)) (1- n))))
804 (defun parse-xinerama-info (line)
805 (remove nil
806 (mapcar (lambda (string)
807 (parse-integer string :junk-allowed t))
808 (split-string (substitute #\space #\x (substitute #\space #\, line))))))
810 (defun get-connected-heads-size (&optional fake)
811 (labels ((heads-info ()
812 (if (not fake)
813 (do-shell "xdpyinfo -ext XINERAMA")
814 (progn
815 (setf *show-root-frame-p* t)
816 (do-shell "echo ' available colormap entries: 256 per subfield
817 red, green, blue masks: 0xff0000, 0xff00, 0xff
818 significant bits in color specification: 8 bits
820 XINERAMA version 1.1 opcode: 150
821 head #0: 500x300 @ 10,10
822 head #1: 480x300 @ 520,20
823 head #2: 600x250 @ 310,330'")))))
824 (when (xlib:query-extension *display* "XINERAMA")
825 (let ((output (heads-info))
826 (sizes nil))
827 (loop for line = (read-line output nil nil)
828 while line
829 do (when (search " head " line)
830 (destructuring-bind (w h x y)
831 (parse-xinerama-info line)
832 (let ((found
833 (dolist (s sizes)
834 (destructuring-bind (x1 y1 w1 h1) s
835 (when (and (>= x x1)
836 (>= y y1)
837 (<= (+ x w) (+ x1 w1))
838 (<= (+ y h) (+ y1 h1)))
839 (return t))))))
840 (unless found
841 (push (list x y w h) sizes))))))
842 sizes))))
843 ;;'((10 10 500 300) (550 50 400 400) (100 320 400 270))))))
844 ;;'((10 10 500 580) (540 50 470 500))))))
847 (let ((last-sizes nil))
848 (defun reset-last-head-size ()
849 (setf last-sizes nil))
851 (defun place-frames-from-xinerama-infos ()
852 "Place frames according to xdpyinfo/xinerama informations"
853 (let ((sizes (get-connected-heads-size))
854 (width (xlib:screen-width *screen*))
855 (height (xlib:screen-height *screen*)))
856 (labels ((update-root-geometry ()
857 (loop for size in sizes
858 for root in (get-root-list)
859 do (destructuring-bind (x y w h) size
860 (setf (root-x root) x
861 (root-y root) y
862 (root-w root) w
863 (root-h root) h)))
864 (setf last-sizes sizes)
865 :update)
866 (create-root-geometry ()
867 (reset-root-list)
868 ;; Add frames in *root-frame* until we get the same number as screen heads
869 (loop while (< (length (frame-child *root-frame*)) (length sizes))
870 do (let ((frame (create-frame)))
871 (add-frame frame *root-frame*)))
872 ;; On the opposite way: remove frames while there is more than screen heads in *root-frame*
873 (when (and sizes (> (length (frame-child *root-frame*)) (length sizes)))
874 (dotimes (i (- (length (frame-child *root-frame*)) (length sizes)))
875 (let ((deleted-child (pop (frame-child *root-frame*))))
876 (typecase deleted-child
877 (xlib:window (push deleted-child (frame-child (first (frame-child *root-frame*)))))
878 (frame (dolist (child (frame-child deleted-child))
879 (push child (frame-child (first (frame-child *root-frame*)))))))
880 (setf (frame-layout (first (frame-child *root-frame*))) 'tile-space-layout
881 (frame-data-slot (first (frame-child *root-frame*)) :tile-layout-keep-position) :yes))))
882 (loop for size in sizes
883 for frame in (frame-child *root-frame*)
884 do (destructuring-bind (x y w h) size
885 (setf (frame-x frame) (float (/ x width))
886 (frame-y frame) (float (/ y height))
887 (frame-w frame) (float (/ w width))
888 (frame-h frame) (float (/ h height)))
889 ;;(add-placed-frame-tmp frame 2) ;; For tests
890 (unless (frame-child frame)
891 (add-frame (create-frame) frame))
892 (define-as-root frame x y w h)))
893 (setf last-sizes sizes)
894 nil))
895 (format t "Screen sizes: ~A~%" sizes)
896 (if (= (length sizes) (length last-sizes))
897 (update-root-geometry)
898 (create-root-geometry))))))
902 (defun finish-configuring-root ()
903 (ensure-at-least-one-root)
904 (setf (current-child) (first (frame-child (first (frame-child *root-frame*))))))
908 (defun get-all-windows (&optional (root *root-frame*))
909 "Return all windows in root and in its children"
910 (let ((acc nil))
911 (with-all-windows (root window)
912 (push window acc))
913 acc))
915 (defun get-all-frame-windows (&optional (root *root-frame*))
916 "Return all frame windows in root and in its children"
917 (let ((acc nil))
918 (with-all-frames (root frame)
919 (push (frame-window frame) acc))
920 acc))
923 (defun get-hidden-windows ()
924 "Return all hiddens windows"
925 (let ((all-windows (get-all-windows))
926 (hidden-windows (remove-if-not #'window-hidden-p
927 (copy-list (xlib:query-tree *root*)))))
928 (set-difference hidden-windows all-windows)))
932 ;;; Current window utilities
933 (defun get-current-window ()
934 (typecase (current-child)
935 (xlib:window (current-child))
936 (frame (frame-selected-child (current-child)))))
938 (defmacro with-current-window (&body body)
939 "Bind 'window' to the current window"
940 `(let ((window (get-current-window)))
941 (when (xlib:window-p window)
942 ,@body)))
944 (defun get-first-window ()
945 (typecase (current-child)
946 (xlib:window (current-child))
947 (frame (or (first (frame-child (current-child)))
948 (current-child)))))
954 (defun display-frame-info (frame)
955 (when (frame-p frame)
956 (let ((dy (+ (xlib:max-char-ascent *default-font*) (xlib:max-char-descent *default-font*))))
957 (with-slots (name number gc window child hidden-children) frame
958 (setf (xlib:gcontext-background gc) (get-color *frame-background*)
959 (xlib:window-background window) (get-color *frame-background*))
960 (clear-pixmap-buffer window gc)
961 (setf (xlib:gcontext-foreground gc) (get-color (if (and (child-root-p frame)
962 (child-equal-p frame (current-child)))
963 *frame-foreground-root* *frame-foreground*)))
964 (xlib:draw-glyphs *pixmap-buffer* gc 5 dy
965 (format nil "Frame: ~A~A"
966 number
967 (if name (format nil " - ~A" name) "")))
968 (let ((pos dy))
969 (when (child-root-p frame)
970 (when *child-selection*
971 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
972 (with-output-to-string (str)
973 (format str " Selection: ")
974 (dolist (child *child-selection*)
975 (typecase child
976 (xlib:window (format str " ~A " (xlib:wm-name child)))
977 (frame (format str " frame:~A[~A] " (frame-number child)
978 (aif (frame-name child) it "")))))))))
979 (dolist (ch child)
980 (xlib:draw-glyphs *pixmap-buffer* gc 5 (incf pos dy)
981 (format nil " ~A" (ensure-printable (child-fullname ch))))))
982 (copy-pixmap-buffer window gc)
983 (values t t)))))
986 (defgeneric rename-child (child name))
988 (defmethod rename-child ((child frame) name)
989 (setf (frame-name child) name)
990 (display-frame-info child))
992 (defmethod rename-child ((child xlib:window) name)
993 (setf (xlib:wm-name child) name))
995 (defmethod rename-child (child name)
996 (declare (ignore child name)))
999 (defun get-parent-layout (child parent)
1000 (aif (child-root-p child)
1001 (values (- (root-x it) (child-border-size child)) (- (root-y it) (child-border-size child))
1002 (root-w it) (root-h it))
1003 (if (or (frame-p child) (managed-window-p child parent))
1004 (if (frame-p parent)
1005 (aif (frame-layout parent)
1006 (funcall it child parent)
1007 (no-layout child parent))
1008 (values (- (child-border-size child)) (- (child-border-size child))
1009 (xlib:screen-width *screen*)
1010 (xlib:screen-height *screen*)))
1011 (values (x-drawable-x child) (x-drawable-y child)
1012 (x-drawable-width child) (x-drawable-height child)))))
1018 (defgeneric adapt-child-to-parent (child parent))
1020 (defmethod adapt-child-to-parent ((window xlib:window) parent)
1021 (when (managed-window-p window parent)
1022 (multiple-value-bind (nx ny nw nh)
1023 (get-parent-layout window parent)
1024 (setf nw (max nw 1) nh (max nh 1))
1025 (let ((change nil))
1026 (when (or (/= (x-drawable-x window) nx)
1027 (/= (x-drawable-y window) ny))
1028 (setf change :moved))
1029 (when (or (/= (x-drawable-width window) nw)
1030 (/= (x-drawable-height window) nh))
1031 (setf change :resized))
1032 (when change
1033 (xlib:with-state (window)
1034 (setf (x-drawable-x window) nx
1035 (x-drawable-y window) ny
1036 (x-drawable-width window) nw
1037 (x-drawable-height window) nh)))
1038 change))))
1041 (defmethod adapt-child-to-parent ((frame frame) parent)
1042 (declare (ignore parent))
1043 (with-slots (rx ry rw rh window) frame
1044 (let ((change nil))
1045 (when (or (/= (x-drawable-x window) rx)
1046 (/= (x-drawable-y window) ry))
1047 (setf change :moved))
1048 (when (or (/= (x-drawable-width window) rw)
1049 (/= (x-drawable-height window) rh))
1050 (setf change :resized))
1051 (when change
1052 (xlib:with-state (window)
1053 (setf (x-drawable-x window) rx
1054 (x-drawable-y window) ry
1055 (x-drawable-width window) rw
1056 (x-drawable-height window) rh)))
1057 change)))
1059 (defmethod adapt-child-to-parent (child parent)
1060 (declare (ignore child parent))
1061 nil)
1064 (defgeneric set-child-stack-order (window child)
1065 (:documentation "Put window just below child"))
1067 (defmethod set-child-stack-order (window (child xlib:window))
1068 (lower-window window child))
1070 (defmethod set-child-stack-order (window (child frame))
1071 (lower-window window (frame-window child)))
1073 (defmethod set-child-stack-order (window child)
1074 (declare (ignore window child)))
1078 (defgeneric show-child (child parent previous))
1080 (defmethod show-child ((frame frame) parent previous)
1081 (declare (ignore parent))
1082 (with-slots (window show-window-p) frame
1083 (if (and show-window-p
1084 (or *show-root-frame-p* (not (child-root-p frame))))
1085 (progn
1086 (map-window window)
1087 (set-child-stack-order window previous)
1088 (display-frame-info frame))
1089 (hide-window window))))
1093 (defun hide-unmanaged-window-p (parent)
1094 (let ((action (frame-data-slot parent :unmanaged-window-action)))
1095 (case action
1096 (:hide t)
1097 (:show nil)
1098 (t *hide-unmanaged-window*))))
1101 (defmethod show-child ((window xlib:window) parent previous)
1102 (if (or (managed-window-p window parent)
1103 (child-equal-p window (current-child))
1104 (not (hide-unmanaged-window-p parent))
1105 (child-is-a-current-child-p parent))
1106 (progn
1107 (map-window window)
1108 (set-child-stack-order window previous))
1109 (hide-window window)))
1111 (defmethod show-child (child parent raise-p)
1112 (declare (ignore child parent raise-p))
1116 (defgeneric hide-child (child))
1118 (defmethod hide-child ((frame frame))
1119 (with-slots (window) frame
1120 (xlib:unmap-window window)))
1122 (defmethod hide-child ((window xlib:window))
1123 (hide-window window))
1125 (defmethod hide-child (child)
1126 (declare (ignore child))
1130 (defgeneric select-child (child selected))
1132 (labels ((get-selected-color (child selected-p)
1133 (get-color (cond ((child-equal-p child (current-child)) *color-selected*)
1134 (selected-p *color-maybe-selected*)
1135 (t *color-unselected*)))))
1136 (defmethod select-child ((frame frame) selected-p)
1137 (when (and (frame-p frame) (frame-window frame))
1138 (setf (xlib:window-border (frame-window frame))
1139 (get-selected-color frame selected-p))))
1141 (defmethod select-child ((window xlib:window) selected-p)
1142 (setf (xlib:window-border window)
1143 (get-selected-color window selected-p)))
1145 (defmethod select-child (child selected)
1146 (declare (ignore child selected))
1147 ()))
1149 (defun select-current-frame (selected)
1150 (select-child (current-child) selected))
1152 (defun unselect-all-frames ()
1153 (with-all-children (*root-frame* child)
1154 (select-child child nil)))
1158 (defun set-focus-to-current-child ()
1159 (labels ((rec (child)
1160 (typecase child
1161 (xlib:window (focus-window child))
1162 (frame (rec (frame-selected-child child))))))
1163 (no-focus)
1164 (rec (current-child))))
1169 (defun adapt-frame-to-parent (frame parent)
1170 (multiple-value-bind (nx ny nw nh)
1171 (get-parent-layout frame parent)
1172 (with-slots (rx ry rw rh window) frame
1173 (setf rx nx ry ny
1174 rw (max nw 1)
1175 rh (max nh 1)))))
1178 (defun adapt-child-to-rect (rect)
1179 (let ((window (typecase (child-rect-child rect)
1180 (xlib:window (when (managed-window-p (child-rect-child rect) (child-rect-parent rect))
1181 (child-rect-child rect)))
1182 (frame (frame-window (child-rect-child rect))))))
1183 (when window
1184 (let ((change (or (/= (x-drawable-x window) (child-rect-x rect))
1185 (/= (x-drawable-y window) (child-rect-y rect))
1186 (/= (x-drawable-width window) (child-rect-w rect))
1187 (/= (x-drawable-height window) (child-rect-h rect)))))
1188 (when change
1189 (setf (x-drawable-width window) (child-rect-w rect)
1190 (x-drawable-height window) (child-rect-h rect)
1191 (x-drawable-x window) (child-rect-x rect)
1192 (x-drawable-y window) (child-rect-y rect)))
1193 change))))
1197 (let ((displayed-child nil))
1198 (defun get-displayed-child ()
1199 displayed-child)
1201 (defun show-all-children (&optional (from-root-frame nil))
1202 "Show all children and hide those not in a root frame"
1203 (declare (ignore from-root-frame))
1204 (let ((geometry-change nil)
1205 (hidden-child nil))
1206 (labels ((in-displayed-list (child)
1207 (member child displayed-child :test (lambda (c rect)
1208 (child-equal-p c (child-rect-child rect)))))
1210 (add-in-hidden-list (child)
1211 (pushnew child hidden-child :test #'child-equal-p))
1213 (set-geometry (child parent in-current-root child-current-root-p)
1214 (if (or in-current-root child-current-root-p)
1215 (when (frame-p child)
1216 (adapt-frame-to-parent child (if child-current-root-p nil parent)))
1217 (add-in-hidden-list child)))
1219 (recurse-on-frame-child (child in-current-root child-current-root-p selected-p)
1220 (let ((selected-child (frame-selected-child child)))
1221 (dolist (sub-child (frame-child child))
1222 (rec sub-child child
1223 (and selected-p (child-equal-p sub-child selected-child))
1224 (or in-current-root child-current-root-p)))))
1226 (hidden-child-p (rect)
1227 (dolist (r displayed-child)
1228 (when (and (rect-hidden-p r rect)
1229 (or (not (xlib:window-p (child-rect-child r)))
1230 (eq (window-type (child-rect-child r)) :normal)))
1231 (return t))))
1233 (select-and-display (child parent selected-p)
1234 (multiple-value-bind (nx ny nw nh)
1235 (get-parent-layout child parent)
1236 (let ((rect (make-child-rect :child child :parent parent
1237 :selected-p selected-p
1238 :x nx :y ny :w nw :h nh)))
1239 (if (and *show-hide-policy* (hidden-child-p rect))
1240 (add-in-hidden-list child)
1241 (push rect displayed-child)))))
1243 (display-displayed-child ()
1244 (let ((previous nil))
1245 (setf displayed-child (nreverse displayed-child))
1246 (dolist (rect displayed-child)
1247 (when (adapt-child-to-rect rect)
1248 (setf geometry-change t))
1249 (select-child (child-rect-child rect) (child-rect-selected-p rect))
1250 (show-child (child-rect-child rect)
1251 (child-rect-parent rect)
1252 previous)
1253 (setf previous (child-rect-child rect)))))
1255 (rec (child parent selected-p in-current-root)
1256 (let ((child-current-root-p (child-root-p child)))
1257 (unless (in-displayed-list child)
1258 (set-geometry child parent in-current-root child-current-root-p))
1259 (when (frame-p child)
1260 (recurse-on-frame-child child in-current-root child-current-root-p selected-p))
1261 (when (and (or in-current-root child-current-root-p)
1262 (not (in-displayed-list child)))
1263 (select-and-display child parent selected-p)))))
1265 (setf displayed-child nil)
1266 (rec *root-frame* nil t (child-root-p *root-frame*))
1267 (display-displayed-child)
1268 (dolist (child hidden-child)
1269 (hide-child child))
1270 (set-focus-to-current-child)
1271 (xlib:display-finish-output *display*)
1272 geometry-change))))
1277 (defun hide-all-children (root &optional except)
1278 "Hide all root children"
1279 (when (and (frame-p root) (not (child-equal-p root except)))
1280 (dolist (child (frame-child root))
1281 (hide-all child except))))
1283 (defun hide-all (root &optional except)
1284 "Hide root and all its children"
1285 (unless (child-equal-p root except)
1286 (hide-child root))
1287 (hide-all-children root except))
1293 (defun focus-child (child parent)
1294 "Focus child - Return true if something has change"
1295 (when (and (frame-p parent)
1296 (child-member child (frame-child parent)))
1297 (when (not (child-equal-p child (frame-selected-child parent)))
1298 (with-slots ((parent-child child) selected-pos) parent
1299 (setf parent-child (nth-insert selected-pos child (child-remove child parent-child))))
1300 t)))
1302 (defun focus-child-rec (child parent)
1303 "Focus child and its parents - Return true if something has change"
1304 (let ((change nil))
1305 (labels ((rec (child parent)
1306 (when (focus-child child parent)
1307 (setf change t))
1308 (when parent
1309 (rec parent (find-parent-frame parent)))))
1310 (rec child parent))
1311 change))
1314 (defun set-current-child-generic (child)
1315 (unless (child-equal-p (current-child) child)
1316 (setf (current-child) child)
1319 (defgeneric set-current-child (child parent window-parent))
1321 (defmethod set-current-child ((child xlib:window) parent window-parent)
1322 (set-current-child-generic (if window-parent parent child)))
1324 (defmethod set-current-child ((child frame) parent window-parent)
1325 (declare (ignore parent window-parent))
1326 (set-current-child-generic child))
1328 (defmethod set-current-child (child parent window-parent)
1329 (declare (ignore child parent window-parent))
1333 (defun set-current-root (child parent window-parent)
1334 "Set current root if parent is not in current root"
1335 (let ((root (find-root child)))
1336 (when (and root window-parent
1337 (not (child-root-p child))
1338 (not (find-child parent (root-child root))))
1339 (change-root root parent)
1340 t)))
1342 (defun focus-all-children (child parent &optional (window-parent t))
1343 "Focus child and its parents -
1344 For window: set current child to window or its parent according to window-parent"
1345 (let ((new-focus (focus-child-rec child parent))
1346 (new-current-child (set-current-child child parent window-parent))
1347 (new-root (set-current-root child parent window-parent)))
1348 (or new-focus new-current-child new-root)))
1353 (defun select-next-level ()
1354 "Select the next level in frame"
1355 (select-current-frame :maybe)
1356 (when (frame-p (current-child))
1357 (awhen (frame-selected-child (current-child))
1358 (setf (current-child) it)))
1359 (show-all-children))
1361 (defun select-previous-level ()
1362 "Select the previous level in frame"
1363 (unless (child-root-p (current-child))
1364 (select-current-frame :maybe)
1365 (awhen (find-parent-frame (current-child))
1366 (setf (current-child) it))
1367 (show-all-children)))
1370 (defun enter-frame ()
1371 "Enter in the selected frame - ie make it the root frame"
1372 (let ((root (find-root (current-child))))
1373 (when (and root (not (child-equal-p (root-child root) (current-child))))
1374 (change-root root (current-child)))
1375 (show-all-children t)))
1377 (defun leave-frame ()
1378 "Leave the selected frame - ie make its parent the root frame"
1379 (let ((root (find-root (current-child))))
1380 (unless (or (child-equal-p (root-child root) *root-frame*)
1381 (child-original-root-p (root-child root)))
1382 (awhen (and root (find-parent-frame (root-child root)))
1383 (when (frame-p it)
1384 (change-root root it)))
1385 (show-all-children))))
1388 ;;; Other actions (select-next-child, select-next-brother...) are in
1389 ;;; clfswm-circulate-mode.lisp
1393 (defun frame-lower-child ()
1394 "Lower the child in the current frame"
1395 (when (frame-p (current-child))
1396 (with-slots (child selected-pos) (current-child)
1397 (unless (>= selected-pos (length child))
1398 (when (nth (1+ selected-pos) child)
1399 (rotatef (nth selected-pos child)
1400 (nth (1+ selected-pos) child)))
1401 (incf selected-pos)))
1402 (show-all-children)))
1405 (defun frame-raise-child ()
1406 "Raise the child in the current frame"
1407 (when (frame-p (current-child))
1408 (with-slots (child selected-pos) (current-child)
1409 (unless (< selected-pos 1)
1410 (when (nth (1- selected-pos) child)
1411 (rotatef (nth selected-pos child)
1412 (nth (1- selected-pos) child)))
1413 (decf selected-pos)))
1414 (show-all-children)))
1417 (defun frame-select-next-child ()
1418 "Select the next child in the current frame"
1419 (when (frame-p (current-child))
1420 (with-slots (child selected-pos) (current-child)
1421 (setf selected-pos (mod (1+ selected-pos) (max (length child) 1))))
1422 (show-all-children)))
1425 (defun frame-select-previous-child ()
1426 "Select the previous child in the current frame"
1427 (when (frame-p (current-child))
1428 (with-slots (child selected-pos) (current-child)
1429 (setf selected-pos (mod (1- selected-pos) (max (length child) 1))))
1430 (show-all-children)))
1434 (defun switch-to-root-frame (&key (show-later nil))
1435 "Switch to the root frame"
1436 (let ((root (find-root (current-child))))
1437 (when root
1438 (change-root root (root-original root)))
1439 (unless show-later
1440 (show-all-children t))))
1442 (defun switch-and-select-root-frame (&key (show-later nil))
1443 "Switch and select the root frame"
1444 (let ((root (find-root (current-child))))
1445 (when root
1446 (change-root root (root-original root))
1447 (setf (current-child) (root-original root)))
1448 (unless show-later
1449 (show-all-children t))))
1452 (defun toggle-show-root-frame ()
1453 "Show/Hide the root frame"
1454 (setf *show-root-frame-p* (not *show-root-frame-p*))
1455 (show-all-children))
1459 (defun prevent-current-*-equal-child (child)
1460 " Prevent current-root and current-child equal to child"
1461 (if (child-original-root-p child)
1463 (progn
1464 (awhen (child-root-p child)
1465 (change-root it (find-parent-frame child)))
1466 (when (child-equal-p child (current-child))
1467 (awhen (find-root child)
1468 (setf (current-child) (root-child it))))
1469 t)))
1473 (defun remove-child-in-frame (child frame)
1474 "Remove the child in frame"
1475 (when (frame-p frame)
1476 (setf (frame-child frame) (child-remove child (frame-child frame)))))
1478 (defun remove-child-in-frames (child root)
1479 "Remove child in the frame root and in all its children"
1480 (with-all-frames (root frame)
1481 (remove-child-in-frame child frame)))
1484 (defun remove-child-in-all-frames (child)
1485 "Remove child in all frames from *root-frame*"
1486 (when (prevent-current-*-equal-child child)
1487 (remove-child-in-frames child *root-frame*)))
1490 (defun delete-child-in-frames (child root &optional (close-methode 'delete-window))
1491 "Delete child in the frame root and in all its children
1492 Warning:frame window and gc are freeed."
1493 (with-all-frames (root frame)
1494 (remove-child-in-frame child frame)
1495 (unless (find-frame-window (frame-window frame))
1496 (awhen (frame-gc frame) (xlib:free-gcontext it) (setf it nil))
1497 (awhen (frame-window frame) (xlib:destroy-window it) (setf it nil))))
1498 (when (xlib:window-p child)
1499 (funcall close-methode child)
1500 (netwm-remove-in-client-list child)))
1504 (defun delete-child-in-all-frames (child &optional (close-methode 'delete-window))
1505 "Delete child in all frames from *root-frame*"
1506 (when (prevent-current-*-equal-child child)
1507 (delete-child-in-frames child *root-frame* close-methode)))
1509 (defun delete-child-and-children-in-frames (child root &optional (close-methode 'delete-window))
1510 "Delete child and its children in the frame root and in all its children
1511 Warning:frame window and gc are freeed."
1512 (when (and (frame-p child) (frame-child child))
1513 (dolist (ch (frame-child child))
1514 (delete-child-and-children-in-frames ch root close-methode)))
1515 (delete-child-in-frames child root close-methode))
1517 (defun delete-child-and-children-in-all-frames (child &optional (close-methode 'delete-window))
1518 "Delete child and its children in all frames from *root-frame*"
1519 (when (prevent-current-*-equal-child child)
1520 (when (frame-p child)
1521 (delete-child-and-children-in-frames child *root-frame* close-methode))
1522 (when (xlib:window-p child)
1523 (funcall close-methode child))))
1526 (defun clean-windows-in-all-frames ()
1527 "Remove all xlib:windows present in *root-frame* and not in the xlib tree"
1528 (let ((x-tree (xlib:query-tree *root*)))
1529 (with-all-frames (*root-frame* frame)
1530 (dolist (child (frame-child frame))
1531 (when (xlib:window-p child)
1532 (unless (member child x-tree :test #'xlib:window-equal)
1533 (when (prevent-current-*-equal-child child)
1534 (setf (frame-child frame)
1535 (child-remove child (frame-child frame))))))))))
1539 (defun do-all-frames-nw-hook (window)
1540 "Call nw-hook of each frame."
1541 (catch 'nw-hook-loop
1542 (let ((found nil))
1543 (with-all-frames (*root-frame* frame)
1544 (awhen (frame-nw-hook frame)
1545 (setf found (call-hook it frame window))))
1546 found)))
1550 (defun process-new-window (window)
1551 "When a new window is created (or when we are scanning initial
1552 windows), this function dresses the window up and gets it ready to be
1553 managed."
1554 (setf (xlib:window-event-mask window) *window-events*)
1555 (set-window-state window +normal-state+)
1556 (setf (x-drawable-border-width window) (case (window-type window)
1557 (:normal *border-size*)
1558 (:maxsize 0)
1559 (:transient *border-size*)
1560 (:dialog *border-size*)
1561 (t *border-size*)))
1562 (grab-all-buttons window)
1563 (unless (never-managed-window-p window)
1564 (unless (do-all-frames-nw-hook window)
1565 (call-hook *default-nw-hook* *root-frame* window)))
1566 (netwm-add-in-client-list window))
1571 (defun with-all-mapped-windows (screen fun)
1572 (let ((all-windows (get-all-windows)))
1573 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1574 (unless (child-member win all-windows)
1575 (let ((map-state (xlib:window-map-state win))
1576 (wm-state (window-state win)))
1577 (unless (or (eql (xlib:window-override-redirect win) :on)
1578 (eql win *no-focus-window*)
1579 (is-notify-window-p win))
1580 (when (or (eql map-state :viewable)
1581 (eql wm-state +iconic-state+))
1582 (funcall fun win))))))))
1584 (defun store-root-background ()
1585 (with-all-mapped-windows *screen* #'hide-window)
1586 (setf *background-image* (xlib:create-pixmap :width (xlib:screen-width *screen*)
1587 :height (xlib:screen-height *screen*)
1588 :depth (xlib:screen-root-depth *screen*)
1589 :drawable *root*)
1590 *background-gc* (xlib:create-gcontext :drawable *background-image*
1591 :foreground (get-color *frame-foreground*)
1592 :background (get-color *frame-background*)
1593 :font *default-font*
1594 :line-style :solid))
1595 (xlib:copy-area *root* *background-gc*
1596 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*)
1597 *background-image* 0 0)
1598 (with-all-mapped-windows *screen* #'unhide-window))
1603 (defun hide-existing-windows (screen)
1604 "Hide all existing windows in screen"
1605 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1606 (hide-window win)))
1610 (defun process-existing-windows (screen)
1611 "Windows present when clfswm starts up must be absorbed by clfswm."
1612 (setf *in-process-existing-windows* t)
1613 (let ((id-list nil)
1614 (all-windows (get-all-windows))
1615 (all-frame-windows (get-all-frame-windows)))
1616 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1617 (unless (or (child-member win all-windows)
1618 (child-member win all-frame-windows))
1619 (let ((map-state (xlib:window-map-state win))
1620 (wm-state (window-state win)))
1621 (unless (or (eql (xlib:window-override-redirect win) :on)
1622 (eql win *no-focus-window*)
1623 (is-notify-window-p win)
1624 (never-managed-window-p win))
1625 (when (or (eql map-state :viewable)
1626 (eql wm-state +iconic-state+))
1627 (format t "Processing ~S: type=~A ~S~%" (xlib:wm-name win) (window-type win) win)
1628 (unhide-window win)
1629 (process-new-window win)
1630 (map-window win)
1631 (raise-window win)
1632 (pushnew (xlib:window-id win) id-list))))))
1633 (netwm-set-client-list id-list))
1634 (setf *in-process-existing-windows* nil))
1637 ;;; Child order manipulation functions
1638 (defun put-child-on-top (child parent)
1639 "Put the child on top of its parent children"
1640 (when (frame-p parent)
1641 (setf (frame-child parent) (cons child (child-remove child (frame-child parent)))
1642 (frame-selected-pos parent) 0)))
1644 (defun put-child-on-bottom (child parent)
1645 "Put the child at the bottom of its parent children"
1646 (when (frame-p parent)
1647 (setf (frame-child parent) (append (child-remove child (frame-child parent)) (list child))
1648 (frame-selected-pos parent) 0)))