9c28356ad897aee2bf9ea9feb7e83cb3ec25c332
[clfswm.git] / src / clfswm-internal.lisp
blob9c28356ad897aee2bf9ea9feb7e83cb3ec25c332
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 child))
1075 (unless (maxmin-size-equal-window-in-tree)
1076 (raise-window window)))
1080 (defgeneric show-child (child parent previous))
1082 (defmethod show-child ((frame frame) parent previous)
1083 (declare (ignore parent))
1084 (with-slots (window show-window-p) frame
1085 (if (and show-window-p
1086 (or *show-root-frame-p* (not (child-root-p frame))))
1087 (progn
1088 (map-window window)
1089 (set-child-stack-order window previous)
1090 (display-frame-info frame))
1091 (hide-window window))))
1095 (defun hide-unmanaged-window-p (parent)
1096 (let ((action (frame-data-slot parent :unmanaged-window-action)))
1097 (case action
1098 (:hide t)
1099 (:show nil)
1100 (t *hide-unmanaged-window*))))
1103 (defmethod show-child ((window xlib:window) parent previous)
1104 (if (or (managed-window-p window parent)
1105 (child-equal-p window (current-child))
1106 (not (hide-unmanaged-window-p parent))
1107 (child-is-a-current-child-p parent))
1108 (progn
1109 (map-window window)
1110 (set-child-stack-order window previous))
1111 (hide-window window)))
1113 (defmethod show-child (child parent raise-p)
1114 (declare (ignore child parent raise-p))
1118 (defgeneric hide-child (child))
1120 (defmethod hide-child ((frame frame))
1121 (with-slots (window) frame
1122 (xlib:unmap-window window)))
1124 (defmethod hide-child ((window xlib:window))
1125 (hide-window window))
1127 (defmethod hide-child (child)
1128 (declare (ignore child))
1132 (defgeneric select-child (child selected))
1134 (labels ((get-selected-color (child selected-p)
1135 (get-color (cond ((child-equal-p child (current-child)) *color-selected*)
1136 (selected-p *color-maybe-selected*)
1137 (t *color-unselected*)))))
1138 (defmethod select-child ((frame frame) selected-p)
1139 (when (and (frame-p frame) (frame-window frame))
1140 (setf (xlib:window-border (frame-window frame))
1141 (get-selected-color frame selected-p))))
1143 (defmethod select-child ((window xlib:window) selected-p)
1144 (setf (xlib:window-border window)
1145 (get-selected-color window selected-p)))
1147 (defmethod select-child (child selected)
1148 (declare (ignore child selected))
1149 ()))
1151 (defun select-current-frame (selected)
1152 (select-child (current-child) selected))
1154 (defun unselect-all-frames ()
1155 (with-all-children (*root-frame* child)
1156 (select-child child nil)))
1160 (defun set-focus-to-current-child ()
1161 (labels ((rec (child)
1162 (typecase child
1163 (xlib:window (focus-window child))
1164 (frame (rec (frame-selected-child child))))))
1165 (no-focus)
1166 (rec (current-child))))
1171 (defun adapt-frame-to-parent (frame parent)
1172 (multiple-value-bind (nx ny nw nh)
1173 (get-parent-layout frame parent)
1174 (with-slots (rx ry rw rh window) frame
1175 (setf rx nx ry ny
1176 rw (max nw 1)
1177 rh (max nh 1)))))
1180 (defun adapt-child-to-rect (rect)
1181 (let ((window (typecase (child-rect-child rect)
1182 (xlib:window (when (managed-window-p (child-rect-child rect) (child-rect-parent rect))
1183 (child-rect-child rect)))
1184 (frame (frame-window (child-rect-child rect))))))
1185 (when window
1186 (let ((change (or (/= (x-drawable-x window) (child-rect-x rect))
1187 (/= (x-drawable-y window) (child-rect-y rect))
1188 (/= (x-drawable-width window) (child-rect-w rect))
1189 (/= (x-drawable-height window) (child-rect-h rect)))))
1190 (when change
1191 (setf (x-drawable-width window) (child-rect-w rect)
1192 (x-drawable-height window) (child-rect-h rect)
1193 (x-drawable-x window) (child-rect-x rect)
1194 (x-drawable-y window) (child-rect-y rect)))
1195 change))))
1199 (let ((displayed-child nil))
1200 (defun get-displayed-child ()
1201 displayed-child)
1203 (defun show-all-children (&optional (from-root-frame nil))
1204 "Show all children and hide those not in a root frame"
1205 (declare (ignore from-root-frame))
1206 (let ((geometry-change nil)
1207 (hidden-child nil))
1208 (labels ((in-displayed-list (child)
1209 (member child displayed-child :test (lambda (c rect)
1210 (child-equal-p c (child-rect-child rect)))))
1212 (add-in-hidden-list (child)
1213 (pushnew child hidden-child :test #'child-equal-p))
1215 (set-geometry (child parent in-current-root child-current-root-p)
1216 (if (or in-current-root child-current-root-p)
1217 (when (frame-p child)
1218 (adapt-frame-to-parent child (if child-current-root-p nil parent)))
1219 (add-in-hidden-list child)))
1221 (recurse-on-frame-child (child in-current-root child-current-root-p selected-p)
1222 (let ((selected-child (frame-selected-child child)))
1223 (dolist (sub-child (frame-child child))
1224 (rec sub-child child
1225 (and selected-p (child-equal-p sub-child selected-child))
1226 (or in-current-root child-current-root-p)))))
1228 (hidden-child-p (rect)
1229 (when (member (window-type (child-rect-child rect)) *show-hide-policy-type*)
1230 (dolist (r displayed-child)
1231 (when (and (rect-hidden-p r rect)
1232 (or (not (xlib:window-p (child-rect-child r)))
1233 (eq (window-type (child-rect-child r)) :normal)))
1234 (return t)))))
1236 (select-and-display (child parent selected-p)
1237 (multiple-value-bind (nx ny nw nh)
1238 (get-parent-layout child parent)
1239 (let ((rect (make-child-rect :child child :parent parent
1240 :selected-p selected-p
1241 :x nx :y ny :w nw :h nh)))
1242 (if (and *show-hide-policy* (hidden-child-p rect))
1243 (add-in-hidden-list child)
1244 (push rect displayed-child)))))
1246 (display-displayed-child ()
1247 (let ((previous nil))
1248 (setf displayed-child (nreverse displayed-child))
1249 (dolist (rect 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 (setf displayed-child nil)
1269 (rec *root-frame* nil t (child-root-p *root-frame*))
1270 (display-displayed-child)
1271 (dolist (child hidden-child)
1272 (hide-child child))
1273 (set-focus-to-current-child)
1274 (xlib:display-finish-output *display*)
1275 geometry-change))))
1280 (defun hide-all-children (root &optional except)
1281 "Hide all root children"
1282 (when (and (frame-p root) (not (child-equal-p root except)))
1283 (dolist (child (frame-child root))
1284 (hide-all child except))))
1286 (defun hide-all (root &optional except)
1287 "Hide root and all its children"
1288 (unless (child-equal-p root except)
1289 (hide-child root))
1290 (hide-all-children root except))
1296 (defun focus-child (child parent)
1297 "Focus child - Return true if something has change"
1298 (when (and (frame-p parent)
1299 (child-member child (frame-child parent)))
1300 (when (not (child-equal-p child (frame-selected-child parent)))
1301 (with-slots ((parent-child child) selected-pos) parent
1302 (setf parent-child (nth-insert selected-pos child (child-remove child parent-child))))
1303 t)))
1305 (defun focus-child-rec (child parent)
1306 "Focus child and its parents - Return true if something has change"
1307 (let ((change nil))
1308 (labels ((rec (child parent)
1309 (when (focus-child child parent)
1310 (setf change t))
1311 (when parent
1312 (rec parent (find-parent-frame parent)))))
1313 (rec child parent))
1314 change))
1317 (defun set-current-child-generic (child)
1318 (unless (child-equal-p (current-child) child)
1319 (setf (current-child) child)
1322 (defgeneric set-current-child (child parent window-parent))
1324 (defmethod set-current-child ((child xlib:window) parent window-parent)
1325 (set-current-child-generic (if window-parent parent child)))
1327 (defmethod set-current-child ((child frame) parent window-parent)
1328 (declare (ignore parent window-parent))
1329 (set-current-child-generic child))
1331 (defmethod set-current-child (child parent window-parent)
1332 (declare (ignore child parent window-parent))
1336 (defun set-current-root (child parent window-parent)
1337 "Set current root if parent is not in current root"
1338 (let ((root (find-root child)))
1339 (when (and root window-parent
1340 (not (child-root-p child))
1341 (not (find-child parent (root-child root))))
1342 (change-root root parent)
1343 t)))
1345 (defun focus-all-children (child parent &optional (window-parent t))
1346 "Focus child and its parents -
1347 For window: set current child to window or its parent according to window-parent"
1348 (let ((new-focus (focus-child-rec child parent))
1349 (new-current-child (set-current-child child parent window-parent))
1350 (new-root (set-current-root child parent window-parent)))
1351 (or new-focus new-current-child new-root)))
1356 (defun select-next-level ()
1357 "Select the next level in frame"
1358 (select-current-frame :maybe)
1359 (when (frame-p (current-child))
1360 (awhen (frame-selected-child (current-child))
1361 (setf (current-child) it)))
1362 (show-all-children))
1364 (defun select-previous-level ()
1365 "Select the previous level in frame"
1366 (unless (child-root-p (current-child))
1367 (select-current-frame :maybe)
1368 (awhen (find-parent-frame (current-child))
1369 (setf (current-child) it))
1370 (show-all-children)))
1373 (defun enter-frame ()
1374 "Enter in the selected frame - ie make it the root frame"
1375 (let ((root (find-root (current-child))))
1376 (when (and root (not (child-equal-p (root-child root) (current-child))))
1377 (change-root root (current-child)))
1378 (show-all-children t)))
1380 (defun leave-frame ()
1381 "Leave the selected frame - ie make its parent the root frame"
1382 (let ((root (find-root (current-child))))
1383 (unless (or (child-equal-p (root-child root) *root-frame*)
1384 (child-original-root-p (root-child root)))
1385 (awhen (and root (find-parent-frame (root-child root)))
1386 (when (frame-p it)
1387 (change-root root it)))
1388 (show-all-children))))
1391 ;;; Other actions (select-next-child, select-next-brother...) are in
1392 ;;; clfswm-circulate-mode.lisp
1396 (defun frame-lower-child ()
1397 "Lower the child in the current frame"
1398 (when (frame-p (current-child))
1399 (with-slots (child selected-pos) (current-child)
1400 (unless (>= selected-pos (length child))
1401 (when (nth (1+ selected-pos) child)
1402 (rotatef (nth selected-pos child)
1403 (nth (1+ selected-pos) child)))
1404 (incf selected-pos)))
1405 (show-all-children)))
1408 (defun frame-raise-child ()
1409 "Raise the child in the current frame"
1410 (when (frame-p (current-child))
1411 (with-slots (child selected-pos) (current-child)
1412 (unless (< selected-pos 1)
1413 (when (nth (1- selected-pos) child)
1414 (rotatef (nth selected-pos child)
1415 (nth (1- selected-pos) child)))
1416 (decf selected-pos)))
1417 (show-all-children)))
1420 (defun frame-select-next-child ()
1421 "Select the next child in the current frame"
1422 (when (frame-p (current-child))
1423 (with-slots (child selected-pos) (current-child)
1424 (setf selected-pos (mod (1+ selected-pos) (max (length child) 1))))
1425 (show-all-children)))
1428 (defun frame-select-previous-child ()
1429 "Select the previous child in the current frame"
1430 (when (frame-p (current-child))
1431 (with-slots (child selected-pos) (current-child)
1432 (setf selected-pos (mod (1- selected-pos) (max (length child) 1))))
1433 (show-all-children)))
1437 (defun switch-to-root-frame (&key (show-later nil))
1438 "Switch to the root frame"
1439 (let ((root (find-root (current-child))))
1440 (when root
1441 (change-root root (root-original root)))
1442 (unless show-later
1443 (show-all-children t))))
1445 (defun switch-and-select-root-frame (&key (show-later nil))
1446 "Switch and select the root frame"
1447 (let ((root (find-root (current-child))))
1448 (when root
1449 (change-root root (root-original root))
1450 (setf (current-child) (root-original root)))
1451 (unless show-later
1452 (show-all-children t))))
1455 (defun toggle-show-root-frame ()
1456 "Show/Hide the root frame"
1457 (setf *show-root-frame-p* (not *show-root-frame-p*))
1458 (show-all-children))
1462 (defun prevent-current-*-equal-child (child)
1463 " Prevent current-root and current-child equal to child"
1464 (if (child-original-root-p child)
1466 (progn
1467 (awhen (child-root-p child)
1468 (change-root it (find-parent-frame child)))
1469 (when (child-equal-p child (current-child))
1470 (awhen (find-root child)
1471 (setf (current-child) (root-child it))))
1472 t)))
1476 (defun remove-child-in-frame (child frame)
1477 "Remove the child in frame"
1478 (when (frame-p frame)
1479 (setf (frame-child frame) (child-remove child (frame-child frame)))))
1481 (defun remove-child-in-frames (child root)
1482 "Remove child in the frame root and in all its children"
1483 (with-all-frames (root frame)
1484 (remove-child-in-frame child frame)))
1487 (defun remove-child-in-all-frames (child)
1488 "Remove child in all frames from *root-frame*"
1489 (when (prevent-current-*-equal-child child)
1490 (remove-child-in-frames child *root-frame*)))
1493 (defun delete-child-in-frames (child root &optional (close-methode 'delete-window))
1494 "Delete child in the frame root and in all its children
1495 Warning:frame window and gc are freeed."
1496 (with-all-frames (root frame)
1497 (remove-child-in-frame child frame)
1498 (unless (find-frame-window (frame-window frame))
1499 (awhen (frame-gc frame) (xlib:free-gcontext it) (setf it nil))
1500 (awhen (frame-window frame) (xlib:destroy-window it) (setf it nil))))
1501 (when (xlib:window-p child)
1502 (funcall close-methode child)
1503 (netwm-remove-in-client-list child)))
1507 (defun delete-child-in-all-frames (child &optional (close-methode 'delete-window))
1508 "Delete child in all frames from *root-frame*"
1509 (when (prevent-current-*-equal-child child)
1510 (delete-child-in-frames child *root-frame* close-methode)))
1512 (defun delete-child-and-children-in-frames (child root &optional (close-methode 'delete-window))
1513 "Delete child and its children in the frame root and in all its children
1514 Warning:frame window and gc are freeed."
1515 (when (and (frame-p child) (frame-child child))
1516 (dolist (ch (frame-child child))
1517 (delete-child-and-children-in-frames ch root close-methode)))
1518 (delete-child-in-frames child root close-methode))
1520 (defun delete-child-and-children-in-all-frames (child &optional (close-methode 'delete-window))
1521 "Delete child and its children in all frames from *root-frame*"
1522 (when (prevent-current-*-equal-child child)
1523 (when (frame-p child)
1524 (delete-child-and-children-in-frames child *root-frame* close-methode))
1525 (when (xlib:window-p child)
1526 (funcall close-methode child))))
1529 (defun clean-windows-in-all-frames ()
1530 "Remove all xlib:windows present in *root-frame* and not in the xlib tree"
1531 (let ((x-tree (xlib:query-tree *root*)))
1532 (with-all-frames (*root-frame* frame)
1533 (dolist (child (frame-child frame))
1534 (when (xlib:window-p child)
1535 (unless (member child x-tree :test #'xlib:window-equal)
1536 (when (prevent-current-*-equal-child child)
1537 (setf (frame-child frame)
1538 (child-remove child (frame-child frame))))))))))
1542 (defun do-all-frames-nw-hook (window)
1543 "Call nw-hook of each frame."
1544 (catch 'nw-hook-loop
1545 (let ((found nil))
1546 (with-all-frames (*root-frame* frame)
1547 (awhen (frame-nw-hook frame)
1548 (setf found (call-hook it frame window))))
1549 found)))
1553 (defun process-new-window (window)
1554 "When a new window is created (or when we are scanning initial
1555 windows), this function dresses the window up and gets it ready to be
1556 managed."
1557 (setf (xlib:window-event-mask window) *window-events*)
1558 (set-window-state window +normal-state+)
1559 (setf (x-drawable-border-width window) (case (window-type window)
1560 (:normal *border-size*)
1561 (:maxsize 0)
1562 (:transient *border-size*)
1563 (:dialog *border-size*)
1564 (t *border-size*)))
1565 (grab-all-buttons window)
1566 (unless (never-managed-window-p window)
1567 (unless (do-all-frames-nw-hook window)
1568 (call-hook *default-nw-hook* *root-frame* window)))
1569 (netwm-add-in-client-list window))
1574 (defun with-all-mapped-windows (screen fun)
1575 (let ((all-windows (get-all-windows)))
1576 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1577 (unless (child-member win all-windows)
1578 (let ((map-state (xlib:window-map-state win))
1579 (wm-state (window-state win)))
1580 (unless (or (eql (xlib:window-override-redirect win) :on)
1581 (eql win *no-focus-window*)
1582 (is-notify-window-p win))
1583 (when (or (eql map-state :viewable)
1584 (eql wm-state +iconic-state+))
1585 (funcall fun win))))))))
1587 (defun store-root-background ()
1588 (with-all-mapped-windows *screen* #'hide-window)
1589 (setf *background-image* (xlib:create-pixmap :width (xlib:screen-width *screen*)
1590 :height (xlib:screen-height *screen*)
1591 :depth (xlib:screen-root-depth *screen*)
1592 :drawable *root*)
1593 *background-gc* (xlib:create-gcontext :drawable *background-image*
1594 :foreground (get-color *frame-foreground*)
1595 :background (get-color *frame-background*)
1596 :font *default-font*
1597 :line-style :solid))
1598 (xlib:copy-area *root* *background-gc*
1599 0 0 (xlib:screen-width *screen*) (xlib:screen-height *screen*)
1600 *background-image* 0 0)
1601 (with-all-mapped-windows *screen* #'unhide-window))
1606 (defun hide-existing-windows (screen)
1607 "Hide all existing windows in screen"
1608 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1609 (hide-window win)))
1613 (defun process-existing-windows (screen)
1614 "Windows present when clfswm starts up must be absorbed by clfswm."
1615 (setf *in-process-existing-windows* t)
1616 (let ((id-list nil)
1617 (all-windows (get-all-windows))
1618 (all-frame-windows (get-all-frame-windows)))
1619 (dolist (win (xlib:query-tree (xlib:screen-root screen)))
1620 (unless (or (child-member win all-windows)
1621 (child-member win all-frame-windows))
1622 (let ((map-state (xlib:window-map-state win))
1623 (wm-state (window-state win)))
1624 (unless (or (eql (xlib:window-override-redirect win) :on)
1625 (eql win *no-focus-window*)
1626 (is-notify-window-p win)
1627 (never-managed-window-p win))
1628 (when (or (eql map-state :viewable)
1629 (eql wm-state +iconic-state+))
1630 (format t "Processing ~S: type=~A ~S~%" (xlib:wm-name win) (window-type win) win)
1631 (unhide-window win)
1632 (process-new-window win)
1633 (map-window win)
1634 (raise-window win)
1635 (pushnew (xlib:window-id win) id-list))))))
1636 (netwm-set-client-list id-list))
1637 (setf *in-process-existing-windows* nil))
1640 ;;; Child order manipulation functions
1641 (defun put-child-on-top (child parent)
1642 "Put the child on top of its parent children"
1643 (when (frame-p parent)
1644 (setf (frame-child parent) (cons child (child-remove child (frame-child parent)))
1645 (frame-selected-pos parent) 0)))
1647 (defun put-child-on-bottom (child parent)
1648 "Put the child at the bottom of its parent children"
1649 (when (frame-p parent)
1650 (setf (frame-child parent) (append (child-remove child (frame-child parent)) (list child))
1651 (frame-selected-pos parent) 0)))