Use children position information from show-all-children instead of recalculating...
[clfswm.git] / src / clfswm-util.lisp
blob8bc8b6240b3ee886c17f898c9573f00fe5f640f3
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility
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)
29 ;;; Configuration file
30 (defun xdg-config-home ()
31 (aif (getenv "XDG_CONFIG_HOME")
32 (pathname-directory (concatenate 'string it "/"))
33 (append (pathname-directory (user-homedir-pathname)) '(".config"))))
36 (let ((saved-conf-name nil))
37 (defun conf-file-name (&optional alternate-name)
38 (unless (and saved-conf-name (not alternate-name))
39 (let* ((user-conf (probe-file (merge-pathnames (user-homedir-pathname) #p".clfswmrc")))
40 (etc-conf (probe-file #p"/etc/clfswmrc"))
41 (config-user-conf (probe-file (make-pathname :directory (append (xdg-config-home) '("clfswm"))
42 :name "clfswmrc")))
43 (alternate-conf (and alternate-name (probe-file alternate-name))))
44 (setf saved-conf-name (or alternate-conf config-user-conf user-conf etc-conf))))
45 (print saved-conf-name)
46 saved-conf-name))
51 (let ((already-warn nil))
52 (defun load-contrib (file)
53 "Load a file in the contrib directory"
54 (let ((truename (merge-pathnames file *contrib-dir*)))
55 (format t "Loading contribution file: ~A~%" truename)
56 (if (probe-file truename)
57 (load truename :verbose nil)
58 (progn
59 (format t " File not found!~%")
60 (unless already-warn
61 (setf already-warn t)
62 (format t " ~&Please, adjust the *contrib-dir* variable to the place where CLFSWM can
63 find its contrib module files. For example: /usr/local/lib/clfswm/.
64 Write (defparameter *contrib-dir* \"/usr/local/lib/clfswm/\") in ~A.~%"
65 (conf-file-name))))))))
68 (defun reload-clfswm ()
69 "Reload clfswm"
70 (format t "~&-*- Reloading CLFSWM -*-~%")
71 (asdf:oos 'asdf:load-op :clfswm)
72 (reset-clfswm))
76 ;;;----------------------------
77 ;;; Lisp image part
78 ;;;----------------------------
79 #+:ECL (require :cmp)
81 (defun build-lisp-image (dump-name)
82 #+:CLISP (ext:saveinitmem dump-name :init-function (lambda () (clfswm:main) (ext:quit)) :executable t)
83 #+:SBCL (sb-ext:save-lisp-and-die dump-name :toplevel 'clfswm:main :executable t)
84 #+:CMU (ext:save-lisp dump-name :init-function (lambda () (clfswm:main) (ext:quit)) :executable t)
85 #+:CCL (ccl:save-application dump-name :toplevel-function (lambda () (clfswm:main) (ccl:quit)) :prepend-kernel t)
86 #+:ECL (c:build-program dump-name :epilogue-code '(clfswm:main)))
91 (defun query-yes-or-no (formatter &rest args)
92 (let ((rep (query-string (apply #'format nil formatter args) "" '("Yes" "No"))))
93 (or (string= rep "")
94 (char= (char rep 0) #\y)
95 (char= (char rep 0) #\Y))))
99 (defun banish-pointer ()
100 "Move the pointer to the lower right corner of the screen"
101 (with-placement (*banish-pointer-placement* x y)
102 (xlib:warp-pointer *root* x y)))
107 ;;; Root functions utility
108 (defun show-current-root ()
109 (when *have-to-show-current-root*
110 (let ((*notify-window-placement* *show-current-root-placement*))
111 (notify-message *show-current-root-delay* *show-current-root-message*))))
113 (defun select-generic-root (fun restart-menu)
114 (no-focus)
115 (let* ((current-root (find-root (current-child)))
116 (parent (find-parent-frame (root-original current-root))))
117 (when parent
118 (setf (frame-child parent) (funcall fun (frame-child parent)))
119 (let ((new-root (find-root (frame-selected-child parent))))
120 (setf (current-child) (aif (root-current-child new-root)
122 (frame-selected-child parent))))))
123 (show-all-children t)
124 (show-current-root)
125 (if restart-menu
126 (open-menu (find-menu 'root-menu))
127 (leave-second-mode)))
129 (defun select-next-root ()
130 "Select the next root"
131 (select-generic-root #'rotate-list nil))
133 (defun select-previous-root ()
134 "Select the previous root"
135 (select-generic-root #'anti-rotate-list nil))
138 (defun select-next-root-restart-menu ()
139 "Select the next root"
140 (select-generic-root #'rotate-list t))
142 (defun select-previous-root-restart-menu ()
143 "Select the previous root"
144 (select-generic-root #'anti-rotate-list t))
147 (defun rotate-root-geometry-generic (fun restart-menu)
148 (no-focus)
149 (funcall fun)
150 (show-all-children t)
151 (show-current-root)
152 (if restart-menu
153 (open-menu (find-menu 'root-menu))
154 (leave-second-mode)))
157 (defun rotate-root-geometry-next ()
158 "Rotate root geometry to next root"
159 (rotate-root-geometry-generic #'rotate-root-geometry nil))
161 (defun rotate-root-geometry-previous ()
162 "Rotate root geometry to previous root"
163 (rotate-root-geometry-generic #'anti-rotate-root-geometry nil))
165 (defun rotate-root-geometry-next-restart-menu ()
166 "Rotate root geometry to next root"
167 (rotate-root-geometry-generic #'rotate-root-geometry t))
169 (defun rotate-root-geometry-previous-restart-menu ()
170 "Rotate root geometry to previous root"
171 (rotate-root-geometry-generic #'anti-rotate-root-geometry t))
175 (defun exchange-root-geometry-with-mouse ()
176 "Exchange two root geometry pointed with the mouse"
177 (open-notify-window '("Select the first root to exchange"))
178 (wait-no-key-or-button-press)
179 (wait-mouse-button-release)
180 (close-notify-window)
181 (multiple-value-bind (x1 y1) (xlib:query-pointer *root*)
182 (open-notify-window '("Select the second root to exchange"))
183 (wait-no-key-or-button-press)
184 (wait-mouse-button-release)
185 (close-notify-window)
186 (multiple-value-bind (x2 y2) (xlib:query-pointer *root*)
187 (exchange-root-geometry (find-root-by-coordinates x1 y1)
188 (find-root-by-coordinates x2 y2))))
189 (show-all-children)
190 (show-current-root)
191 (leave-second-mode))
193 (defun change-current-root-geometry ()
194 "Change the current root geometry"
195 (let* ((root (find-root (current-child)))
196 (x (query-number "New root X position" (root-x root)))
197 (y (query-number "New root Y position" (root-y root)))
198 (w (query-number "New root width" (root-w root)))
199 (h (query-number "New root height" (root-h root))))
200 (setf (root-x root) x (root-y root) y
201 (root-w root) w (root-h root) h)
202 (show-all-children)
203 (show-current-root)
204 (leave-second-mode)))
208 (defun display-all-frame-info ()
209 (with-all-frames (*root-frame* frame)
210 (display-frame-info frame)))
212 (defun display-all-root-frame-info ()
213 (with-all-root-child (root)
214 (display-frame-info root)))
218 (defun place-window-from-hints (window)
219 "Place a window from its hints"
220 (let* ((hints (xlib:wm-normal-hints window))
221 (min-width (or (and hints (xlib:wm-size-hints-min-width hints)) 0))
222 (min-height (or (and hints (xlib:wm-size-hints-min-height hints)) 0))
223 (max-width (or (and hints (xlib:wm-size-hints-max-width hints)) (x-drawable-width *root*)))
224 (max-height (or (and hints (xlib:wm-size-hints-max-height hints)) (x-drawable-height *root*)))
225 (rwidth (or (and hints (or (xlib:wm-size-hints-width hints) (xlib:wm-size-hints-base-width hints)))
226 (x-drawable-width window)))
227 (rheight (or (and hints (or (xlib:wm-size-hints-height hints) (xlib:wm-size-hints-base-height hints)))
228 (x-drawable-height window))))
229 (setf (x-drawable-width window) (min (max min-width rwidth *default-window-width*) max-width)
230 (x-drawable-height window) (min (max min-height rheight *default-window-height*) max-height))
231 (with-placement (*unmanaged-window-placement* x y (x-drawable-width window) (x-drawable-height window))
232 (setf (x-drawable-x window) x
233 (x-drawable-y window) y))
234 (xlib:display-finish-output *display*)))
237 (defun rename-current-child ()
238 "Rename the current child"
239 (let ((name (query-string (format nil "New child name: (last: ~A)" (child-name (current-child)))
240 (child-name (current-child)))))
241 (rename-child (current-child) name)
242 (leave-second-mode)))
245 (defun ask-child-transparency (msg child)
246 (let ((trans (query-number (format nil "New ~A transparency: (last: ~A)"
248 (* 100 (child-transparency child)))
249 (* 100 (child-transparency child)))))
250 (when (numberp trans)
251 (setf (child-transparency child) (float (/ trans 100))))))
253 (defun set-current-child-transparency ()
254 "Set the current child transparency"
255 (ask-child-transparency "child" (current-child))
256 (leave-second-mode))
259 (defun ask-child-border-size (msg child)
260 (let ((size (query-number (format nil "New ~A border size: (last: ~A)"
262 (child-border-size child))
263 (child-border-size child))))
264 (when (numberp size)
265 (setf (child-border-size child) size))))
268 (defun set-current-child-border-size ()
269 "Set the current child border size"
270 (ask-child-border-size "child" (current-child))
271 (leave-second-mode))
274 (defun renumber-current-frame ()
275 "Renumber the current frame"
276 (when (frame-p (current-child))
277 (let ((number (query-number (format nil "New child number: (last: ~A)" (frame-number (current-child)))
278 (frame-number (current-child)))))
279 (setf (frame-number (current-child)) number)
280 (leave-second-mode))))
285 (defun add-default-frame ()
286 "Add a default frame in the current frame"
287 (when (frame-p (current-child))
288 (let ((name (query-string "Frame name")))
289 (push (create-frame :name name) (frame-child (current-child)))))
290 (leave-second-mode))
292 (defun add-frame-in-parent-frame ()
293 "Add a frame in the parent frame (and reorganize parent frame)"
294 (let ((parent (find-parent-frame (current-child))))
295 (when (and parent (not (child-original-root-p (current-child))))
296 (let ((new-frame (create-frame)))
297 (pushnew new-frame (frame-child parent))
298 (awhen (child-root-p (current-child))
299 (change-root it parent))
300 (setf (current-child) parent)
301 (set-layout-once #'tile-space-layout)
302 (setf (current-child) new-frame)
303 (leave-second-mode)))))
308 (defun add-placed-frame ()
309 "Add a placed frame in the current frame"
310 (when (frame-p (current-child))
311 (let ((name (query-string "Frame name"))
312 (x (/ (query-number "Frame x in percent (%)") 100))
313 (y (/ (query-number "Frame y in percent (%)") 100))
314 (w (/ (query-number "Frame width in percent (%)" 100) 100))
315 (h (/ (query-number "Frame height in percent (%)" 100) 100)))
316 (push (create-frame :name name :x x :y y :w w :h h)
317 (frame-child (current-child)))))
318 (leave-second-mode))
322 (defun delete-focus-window-generic (close-fun)
323 (with-focus-window (window)
324 (when (child-equal-p window (current-child))
325 (setf (current-child) (find-current-root)))
326 (delete-child-and-children-in-all-frames window close-fun)))
329 (defun delete-focus-window ()
330 "Close focus window: Delete the focus window in all frames and workspaces"
331 (delete-focus-window-generic 'delete-window))
333 (defun destroy-focus-window ()
334 "Kill focus window: Destroy the focus window in all frames and workspaces"
335 (delete-focus-window-generic 'destroy-window))
337 (defun remove-focus-window ()
338 "Remove the focus window from the current frame"
339 (with-focus-window (window)
340 (setf (current-child) (find-current-root))
341 (hide-child window)
342 (remove-child-in-frame window (find-parent-frame window))
343 (show-all-children)))
346 (defun unhide-all-windows-in-current-child ()
347 "Unhide all hidden windows into the current child"
348 (dolist (window (get-hidden-windows))
349 (unhide-window window)
350 (process-new-window window)
351 (map-window window))
352 (show-all-children))
357 (defun find-child-under-mouse-in-never-managed-windows (x y)
358 "Return the child under mouse from never managed windows"
359 (let ((ret nil))
360 (dolist (win (xlib:query-tree *root*))
361 (unless (window-hidden-p win)
362 (multiple-value-bind (never-managed raise)
363 (never-managed-window-p win)
364 (when (and never-managed raise (in-window win x y))
365 (setf ret win)))))
366 ret))
368 (defun find-child-under-mouse-in-child-tree (x y)
369 (dolist (child-rect (get-displayed-child))
370 (when (in-rect x y (child-rect-x child-rect) (child-rect-y child-rect)
371 (child-rect-w child-rect) (child-rect-h child-rect))
372 (return-from find-child-under-mouse-in-child-tree (child-rect-child child-rect)))))
376 (defun find-child-under-mouse (x y &optional also-never-managed)
377 "Return the child under the mouse"
378 (or (and also-never-managed
379 (find-child-under-mouse-in-never-managed-windows x y))
380 (find-child-under-mouse-in-child-tree x y)))
387 ;;; Selection functions
388 (defun clear-selection ()
389 "Clear the current selection"
390 (setf *child-selection* nil)
391 (display-all-root-frame-info))
393 (defun copy-current-child ()
394 "Copy the current child to the selection"
395 (pushnew (current-child) *child-selection*)
396 (display-all-root-frame-info))
399 (defun cut-current-child (&optional (show-now t))
400 "Cut the current child to the selection"
401 (unless (child-root-p (current-child))
402 (let ((parent (find-parent-frame (current-child))))
403 (hide-all (current-child))
404 (copy-current-child)
405 (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
406 (when parent
407 (setf (current-child) parent))
408 (when show-now
409 (show-all-children t))
410 (current-child))))
412 (defun remove-current-child ()
413 "Remove the current child from its parent frame"
414 (unless (child-root-p (current-child))
415 (let ((parent (find-parent-frame (current-child))))
416 (hide-all (current-child))
417 (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
418 (when parent
419 (setf (current-child) parent))
420 (show-all-children t)
421 (leave-second-mode))))
423 (defun delete-current-child ()
424 "Delete the current child and its children in all frames"
425 (unless (child-root-p (current-child))
426 (hide-all (current-child))
427 (delete-child-and-children-in-all-frames (current-child))
428 (show-all-children t)
429 (leave-second-mode)))
432 (defun paste-selection-no-clear ()
433 "Paste the selection in the current frame - Do not clear the selection after paste"
434 (when (frame-p (current-child))
435 (dolist (child *child-selection*)
436 (unless (find-child-in-parent child (current-child))
437 (pushnew child (frame-child (current-child)) :test #'child-equal-p)))
438 (show-all-children)))
440 (defun paste-selection ()
441 "Paste the selection in the current frame"
442 (when (frame-p (current-child))
443 (paste-selection-no-clear)
444 (setf *child-selection* nil)
445 (display-all-root-frame-info)))
448 (defun copy-focus-window ()
449 "Copy the focus window to the selection"
450 (with-focus-window (window)
451 (with-current-child (window)
452 (copy-current-child))))
455 (defun cut-focus-window ()
456 "Cut the focus window to the selection"
457 (with-focus-window (window)
458 (setf (current-child) (with-current-child (window)
459 (cut-current-child nil)))
460 (show-all-children t)))
467 ;;; Maximize function
468 (defun frame-toggle-maximize ()
469 "Maximize/Unmaximize the current frame in its parent frame"
470 (when (frame-p (current-child))
471 (let ((unmaximized-coords (frame-data-slot (current-child) :unmaximized-coords)))
472 (if unmaximized-coords
473 (with-slots (x y w h) (current-child)
474 (destructuring-bind (nx ny nw nh) unmaximized-coords
475 (setf (frame-data-slot (current-child) :unmaximized-coords) nil
476 x nx y ny w nw h nh)))
477 (with-slots (x y w h) (current-child)
478 (setf (frame-data-slot (current-child) :unmaximized-coords)
479 (list x y w h)
480 x 0 y 0 w 1 h 1))))
481 (show-all-children)
482 (leave-second-mode)))
492 ;;; CONFIG - Identify mode
493 (defun identify-key ()
494 "Identify a key"
495 (let* ((done nil)
496 (font (xlib:open-font *display* *identify-font-string*))
497 (window (xlib:create-window :parent *root*
498 :x 0 :y 0
499 :width (- (xlib:screen-width *screen*) (* *border-size* 2))
500 :height (* 5 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font)))
501 :background (get-color *identify-background*)
502 :border-width *border-size*
503 :border (get-color *identify-border*)
504 :colormap (xlib:screen-default-colormap *screen*)
505 :event-mask '(:exposure)))
506 (gc (xlib:create-gcontext :drawable window
507 :foreground (get-color *identify-foreground*)
508 :background (get-color *identify-background*)
509 :font font
510 :line-style :solid)))
511 (setf (window-transparency window) *identify-transparency*)
512 (labels ((print-doc (msg hash-table-key pos code state)
513 (let ((function (find-key-from-code hash-table-key code state)))
514 (when (and function (fboundp (first function)))
515 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* pos (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
516 (format nil "~A ~A" msg (documentation (first function) 'function))))))
517 (print-key (code state keysym key modifiers)
518 (clear-pixmap-buffer window gc)
519 (setf (xlib:gcontext-foreground gc) (get-color *identify-foreground*))
520 (xlib:draw-glyphs *pixmap-buffer* gc 5 (+ (xlib:max-char-ascent font) 5)
521 (format nil "Press a key to identify. Press 'q' to stop the identify loop."))
522 (when code
523 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* 2 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
524 (format nil "Code=~A KeySym=~S Key=~S Modifiers=~A"
525 code keysym key modifiers))
526 (print-doc "Main mode : " *main-keys* 3 code state)
527 (print-doc "Second mode: " *second-keys* 4 code state))
528 (copy-pixmap-buffer window gc))
529 (handle-identify-key (&rest event-slots &key root code state &allow-other-keys)
530 (declare (ignore event-slots root))
531 (let* ((modifiers (state->modifiers state))
532 (key (keycode->char code state))
533 (keysym (keysym->keysym-name (keycode->keysym code modifiers))))
534 (setf done (and (equal key #\q) (equal modifiers *default-modifiers*)))
535 (dbg code keysym key modifiers)
536 (print-key code state keysym key modifiers)
537 (force-output)))
538 (handle-identify (&rest event-slots &key display event-key &allow-other-keys)
539 (declare (ignore display))
540 (case event-key
541 (:key-press (apply #'handle-identify-key event-slots) t)
542 (:exposure (print-key nil nil nil nil nil)))
544 (xgrab-pointer *root* 92 93)
545 (map-window window)
546 (format t "~&Press 'q' to stop the identify loop~%")
547 (print-key nil nil nil nil nil)
548 (force-output)
549 (unwind-protect
550 (loop until done do
551 (with-xlib-protect (:Identify-Loop nil)
552 (when (xlib:event-listen *display* *loop-timeout*)
553 (xlib:process-event *display* :handler #'handle-identify))
554 (xlib:display-finish-output *display*)))
555 (progn
556 (xlib:destroy-window window)
557 (xlib:close-font font)
558 (xgrab-pointer *root* 66 67))))))
565 (let ((all-symbols (collect-all-symbols)))
566 (defun eval-from-query-string ()
567 "Eval a lisp form from the query input"
568 (let ((form (query-string (format nil "Eval Lisp <~A> " (package-name *package*))
569 "" all-symbols))
570 (result nil))
571 (when (and form (not (equal form "")))
572 (let ((printed-result
573 (with-output-to-string (*standard-output*)
574 (setf result (handler-case
575 (loop for i in (multiple-value-list
576 (eval (read-from-string form)))
577 collect (format nil "~S" i))
578 (error (condition)
579 (format nil "~A" condition)))))))
580 (let ((ret (info-mode (expand-newline (append (ensure-list (format nil "> ~A" form))
581 (ensure-list printed-result)
582 (ensure-list result)))
583 :width (- (xlib:screen-width *screen*) 2))))
584 (when (or (search "defparameter" form :test #'string-equal)
585 (search "defvar" form :test #'string-equal))
586 (let ((elem (split-string form)))
587 (pushnew (string-downcase (if (string= (first elem) "(") (third elem) (second elem)))
588 all-symbols :test #'string=)))
589 (when (search "in-package" form :test #'string-equal)
590 (let ((*notify-window-placement* 'middle-middle-root-placement))
591 (open-notify-window '("Collecting all symbols for Lisp REPL completion."))
592 (setf all-symbols (collect-all-symbols))
593 (close-notify-window)))
594 (when ret
595 (eval-from-query-string))))))))
601 (let ((commands (command-in-path)))
602 (defun run-program-from-query-string ()
603 "Run a program from the query input"
604 (labels ((run-program-from-query-string-fun ()
605 (multiple-value-bind (program return)
606 (query-string "Run:" "" commands)
607 (when (and (equal return :return) program (not (equal program "")))
608 (let ((cmd (concatenate 'string "cd $HOME && exec " program)))
609 (lambda ()
610 (do-shell cmd)))))))
611 (let ((fun (run-program-from-query-string-fun)))
612 (when fun
613 (if *in-second-mode*
614 (progn
615 (setf *second-mode-leave-function* fun)
616 (leave-second-mode))
617 (funcall fun)))))))
623 ;;; Frame name actions
624 (defun ask-frame-name (msg)
625 "Ask a frame name"
626 (let ((all-frame-name nil))
627 (with-all-frames (*root-frame* frame)
628 (awhen (frame-name frame) (push it all-frame-name)))
629 (query-string msg "" all-frame-name)))
632 ;;; Focus by functions
633 (defun focus-frame-by (frame)
634 (when (frame-p frame)
635 (focus-all-children frame (or (find-parent-frame frame (find-current-root))
636 (find-parent-frame frame)
637 *root-frame*))
638 (show-all-children t)))
641 (defun focus-frame-by-name ()
642 "Focus a frame by name"
643 (focus-frame-by (find-frame-by-name (ask-frame-name "Focus frame:")))
644 (leave-second-mode))
646 (defun focus-frame-by-number ()
647 "Focus a frame by number"
648 (focus-frame-by (find-frame-by-number (query-number "Focus frame by number:")))
649 (leave-second-mode))
652 ;;; Open by functions
653 (defun open-frame-by (frame)
654 (when (frame-p frame)
655 (push (create-frame :name (query-string "Frame name")) (frame-child frame))
656 (show-all-children)))
660 (defun open-frame-by-name ()
661 "Open a new frame in a named frame"
662 (open-frame-by (find-frame-by-name (ask-frame-name "Open a new frame in: ")))
663 (leave-second-mode))
665 (defun open-frame-by-number ()
666 "Open a new frame in a numbered frame"
667 (open-frame-by (find-frame-by-number (query-number "Open a new frame in the group numbered:")))
668 (leave-second-mode))
671 ;;; Delete by functions
672 (defun delete-frame-by (frame)
673 (unless (or (child-equal-p frame *root-frame*)
674 (child-root-p frame))
675 (when (child-equal-p frame (current-child))
676 (setf (current-child) (find-current-root)))
677 (remove-child-in-frame frame (find-parent-frame frame)))
678 (show-all-children t))
681 (defun delete-frame-by-name ()
682 "Delete a frame by name"
683 (delete-frame-by (find-frame-by-name (ask-frame-name "Delete frame: ")))
684 (leave-second-mode))
686 (defun delete-frame-by-number ()
687 "Delete a frame by number"
688 (delete-frame-by (find-frame-by-number (query-number "Delete frame by number:")))
689 (leave-second-mode))
692 ;;; Move by function
693 (defun move-child-to (child frame-dest)
694 (when (and child (frame-p frame-dest))
695 (remove-child-in-frame child (find-parent-frame child))
696 (pushnew child (frame-child frame-dest))
697 (focus-all-children child frame-dest)
698 (show-all-children t)))
700 (defun move-current-child-by-name ()
701 "Move current child in a named frame"
702 (move-child-to (current-child)
703 (find-frame-by-name
704 (ask-frame-name (format nil "Move '~A' to frame: " (child-name (current-child))))))
705 (leave-second-mode))
707 (defun move-current-child-by-number ()
708 "Move current child in a numbered frame"
709 (move-child-to (current-child)
710 (find-frame-by-number
711 (query-number (format nil "Move '~A' to frame numbered:" (child-name (current-child))))))
712 (leave-second-mode))
715 ;;; Copy by function
716 (defun copy-child-to (child frame-dest)
717 (when (and child (frame-p frame-dest))
718 (pushnew child (frame-child frame-dest))
719 (focus-all-children child frame-dest)
720 (show-all-children t)))
722 (defun copy-current-child-by-name ()
723 "Copy current child in a named frame"
724 (copy-child-to (current-child)
725 (find-frame-by-name
726 (ask-frame-name (format nil "Copy '~A' to frame: " (child-name (current-child))))))
727 (leave-second-mode))
729 (defun copy-current-child-by-number ()
730 "Copy current child in a numbered frame"
731 (copy-child-to (current-child)
732 (find-frame-by-number
733 (query-number (format nil "Copy '~A' to frame numbered:" (child-name (current-child))))))
734 (leave-second-mode))
739 ;;; Show frame info
740 (defun show-all-frames-info ()
741 "Show all frames info windows"
742 (let ((*show-root-frame-p* t))
743 (show-all-children)
744 (with-all-root-child (root)
745 (with-all-frames (root frame)
746 (raise-window (frame-window frame))
747 (display-frame-info frame)))))
749 (defun hide-all-frames-info ()
750 "Hide all frames info windows"
751 (show-all-children))
753 (defun show-all-frames-info-key ()
754 "Show all frames info windows until a key is release"
755 (show-all-frames-info)
756 (wait-no-key-or-button-press)
757 (hide-all-frames-info))
760 (defun move-frame (frame parent orig-x orig-y)
761 (when (and frame parent (not (child-root-p frame)))
762 (hide-all-children frame)
763 (with-slots (window) frame
764 (move-window window orig-x orig-y #'display-frame-info (list frame))
765 (setf (frame-x frame) (x-px->fl (x-drawable-x window) parent)
766 (frame-y frame) (y-px->fl (x-drawable-y window) parent)))
767 (show-all-children)))
769 (defun resize-frame (frame parent orig-x orig-y)
770 (when (and frame parent (not (child-root-p frame)))
771 (hide-all-children frame)
772 (with-slots (window) frame
773 (resize-window window orig-x orig-y #'display-frame-info (list frame))
774 (setf (frame-w frame) (w-px->fl (anti-adj-border-wh (x-drawable-width window) frame) parent)
775 (frame-h frame) (h-px->fl (anti-adj-border-wh (x-drawable-height window) frame) parent)))
776 (show-all-children)))
780 (defun mouse-click-to-focus-generic (root-x root-y mouse-fn)
781 "Focus the current frame or focus the current window parent
782 mouse-fun is #'move-frame or #'resize-frame"
783 (let* ((to-replay t)
784 (child (find-child-under-mouse root-x root-y))
785 (parent (find-parent-frame child))
786 (root-p (child-root-p child)))
787 (labels ((add-new-frame ()
788 (when (frame-p child)
789 (setf parent child
790 child (create-frame)
791 mouse-fn #'resize-frame
792 (current-child) child)
793 (place-frame child parent root-x root-y 10 10)
794 (map-window (frame-window child))
795 (pushnew child (frame-child parent)))))
796 (when (and root-p *create-frame-on-root*)
797 (add-new-frame))
798 (when (and (frame-p child) (not (child-root-p child)))
799 (funcall mouse-fn child parent root-x root-y))
800 (when (and child parent
801 (focus-all-children child parent (not (child-root-p child))))
802 (when (show-all-children)
803 (setf to-replay nil)))
804 (if to-replay
805 (replay-button-event)
806 (stop-button-event)))))
809 (defun mouse-click-to-focus-and-move (window root-x root-y)
810 "Move and focus the current frame or focus the current window parent.
811 Or do actions on corners"
812 (declare (ignore window))
813 (or (do-corner-action root-x root-y *corner-main-mode-left-button*)
814 (mouse-click-to-focus-generic root-x root-y #'move-frame)))
816 (defun mouse-click-to-focus-and-resize (window root-x root-y)
817 "Resize and focus the current frame or focus the current window parent.
818 Or do actions on corners"
819 (declare (ignore window))
820 (or (do-corner-action root-x root-y *corner-main-mode-right-button*)
821 (mouse-click-to-focus-generic root-x root-y #'resize-frame)))
823 (defun mouse-middle-click (window root-x root-y)
824 "Do actions on corners"
825 (declare (ignore window))
826 (or (do-corner-action root-x root-y *corner-main-mode-middle-button*)
827 (replay-button-event)))
832 (defun mouse-focus-move/resize-generic (root-x root-y mouse-fn window-parent)
833 "Focus the current frame or focus the current window parent
834 mouse-fun is #'move-frame or #'resize-frame.
835 Focus child and its parents -
836 For window: set current child to window or its parent according to window-parent"
837 (labels ((move/resize-managed (child)
838 (let ((parent (find-parent-frame child)))
839 (when (and child
840 (frame-p child)
841 (child-root-p child))
842 (setf parent child
843 child (create-frame)
844 mouse-fn #'resize-frame)
845 (place-frame child parent root-x root-y 10 10)
846 (map-window (frame-window child))
847 (push child (frame-child parent)))
848 (focus-all-children child parent window-parent)
849 (show-all-children)
850 (typecase child
851 (xlib:window
852 (if (managed-window-p child parent)
853 (funcall mouse-fn parent (find-parent-frame parent) root-x root-y)
854 (funcall (cond ((or (eql mouse-fn #'move-frame)
855 (eql mouse-fn #'move-frame-constrained))
856 #'move-window)
857 ((or (eql mouse-fn #'resize-frame)
858 (eql mouse-fn #'resize-frame-constrained))
859 #'resize-window))
860 child root-x root-y)))
861 (frame (funcall mouse-fn child parent root-x root-y)))
862 (show-all-children)))
863 (move/resize-never-managed (child raise-fun)
864 (funcall raise-fun child)
865 (funcall (cond ((eql mouse-fn #'move-frame) #'move-window)
866 ((eql mouse-fn #'resize-frame) #'resize-window))
867 child root-x root-y)))
868 (let ((child (find-child-under-mouse root-x root-y t)))
869 (multiple-value-bind (never-managed raise-fun)
870 (never-managed-window-p child)
871 (if (and (xlib:window-p child) never-managed raise-fun)
872 (move/resize-never-managed child raise-fun)
873 (move/resize-managed child))))))
876 (defun test-mouse-binding (window root-x root-y)
877 (dbg window root-x root-y)
878 (replay-button-event))
882 (defun mouse-select-next-level (window root-x root-y)
883 "Select the next level in frame"
884 (declare (ignore root-x root-y))
885 (let ((frame (find-frame-window window)))
886 (when (or frame (xlib:window-equal window *root*))
887 (select-next-level))
888 (replay-button-event)))
892 (defun mouse-select-previous-level (window root-x root-y)
893 "Select the previous level in frame"
894 (declare (ignore root-x root-y))
895 (let ((frame (find-frame-window window)))
896 (when (or frame (xlib:window-equal window *root*))
897 (select-previous-level))
898 (replay-button-event)))
902 (defun mouse-enter-frame (window root-x root-y)
903 "Enter in the selected frame - ie make it the root frame"
904 (declare (ignore root-x root-y))
905 (let ((frame (find-frame-window window)))
906 (when (or frame (xlib:window-equal window *root*))
907 (enter-frame))
908 (replay-button-event)))
912 (defun mouse-leave-frame (window root-x root-y)
913 "Leave the selected frame - ie make its parent the root frame"
914 (declare (ignore root-x root-y))
915 (let ((frame (find-frame-window window)))
916 (when (or frame (xlib:window-equal window *root*))
917 (leave-frame))
918 (replay-button-event)))
922 ;;;;;,-----
923 ;;;;;| Various definitions
924 ;;;;;`-----
926 (defun show-help (&optional (browser "dillo") (tempfile "/tmp/clfswm.html"))
927 "Show current keys and buttons bindings"
928 (ignore-errors
929 (produce-doc-html-in-file tempfile))
930 (sleep 1)
931 (do-shell (format nil "~A ~A" browser tempfile)))
935 ;;; Bind or jump functions
936 (let ((key-slots (make-array 10 :initial-element nil))
937 (current-slot 1))
938 (defun reset-bind-or-jump-slots ()
939 (dotimes (i 10)
940 (setf (aref key-slots i) nil)))
942 (defun bind-on-slot (&optional (slot current-slot) child)
943 "Bind current child to slot"
944 (setf (aref key-slots slot) (if child child (current-child))))
946 (defun remove-binding-on-slot ()
947 "Remove binding on slot"
948 (setf (aref key-slots current-slot) nil))
950 (defun jump-to-slot ()
951 "Jump to slot"
952 (let ((jump-child (aref key-slots current-slot)))
953 (when (and jump-child (find-child jump-child *root-frame*))
954 (unless (find-child-in-all-root jump-child)
955 (change-root (find-root jump-child) jump-child))
956 (setf (current-child) jump-child)
957 (focus-all-children jump-child jump-child)
958 (show-all-children t))))
960 (defun bind-or-jump (n)
961 "Bind or jump to a slot (a frame or a window)"
962 (setf current-slot (- n 1))
963 (let ((default-bind `("b" bind-on-slot
964 ,(format nil "Bind slot ~A on child: ~A" n (child-fullname (current-child))))))
965 (info-mode-menu (aif (aref key-slots current-slot)
966 `(,default-bind
967 ("BackSpace" remove-binding-on-slot
968 ,(format nil "Remove slot ~A binding on child: ~A" n (child-fullname (current-child))))
969 (" - " nil " -")
970 ("Tab" jump-to-slot
971 ,(format nil "Jump to child: ~A" (aif (aref key-slots current-slot)
972 (child-fullname it)
973 "Not set - Please, bind it with 'b'")))
974 ("Return" jump-to-slot "Same thing")
975 ("space" jump-to-slot "Same thing"))
976 (list default-bind))))))
980 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
981 ;;; Useful function for the second mode ;;;
982 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
983 (defmacro with-movement (&body body)
984 `(when (frame-p (current-child))
985 (unwind-protect
986 (progn
987 ,@body)
988 (show-all-children)
989 (display-all-frame-info)
990 (draw-second-mode-window)
991 (open-menu (find-menu 'frame-movement-menu)))))
994 ;;; Pack
995 (defun current-frame-pack-up ()
996 "Pack the current frame up"
997 (with-movement (pack-frame-up (current-child) (find-parent-frame (current-child) (find-current-root)))))
999 (defun current-frame-pack-down ()
1000 "Pack the current frame down"
1001 (with-movement (pack-frame-down (current-child) (find-parent-frame (current-child) (find-current-root)))))
1003 (defun current-frame-pack-left ()
1004 "Pack the current frame left"
1005 (with-movement (pack-frame-left (current-child) (find-parent-frame (current-child) (find-current-root)))))
1007 (defun current-frame-pack-right ()
1008 "Pack the current frame right"
1009 (with-movement (pack-frame-right (current-child) (find-parent-frame (current-child) (find-current-root)))))
1011 ;;; Center
1012 (defun center-current-frame ()
1013 "Center the current frame"
1014 (with-movement (center-frame (current-child))))
1016 ;;; Fill
1017 (defun current-frame-fill-up ()
1018 "Fill the current frame up"
1019 (with-movement (fill-frame-up (current-child) (find-parent-frame (current-child) (find-current-root)))))
1021 (defun current-frame-fill-down ()
1022 "Fill the current frame down"
1023 (with-movement (fill-frame-down (current-child) (find-parent-frame (current-child) (find-current-root)))))
1025 (defun current-frame-fill-left ()
1026 "Fill the current frame left"
1027 (with-movement (fill-frame-left (current-child) (find-parent-frame (current-child) (find-current-root)))))
1029 (defun current-frame-fill-right ()
1030 "Fill the current frame right"
1031 (with-movement (fill-frame-right (current-child) (find-parent-frame (current-child) (find-current-root)))))
1033 (defun current-frame-fill-all-dir ()
1034 "Fill the current frame in all directions"
1035 (with-movement
1036 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1037 (fill-frame-up (current-child) parent)
1038 (fill-frame-down (current-child) parent)
1039 (fill-frame-left (current-child) parent)
1040 (fill-frame-right (current-child) parent))))
1042 (defun current-frame-fill-vertical ()
1043 "Fill the current frame vertically"
1044 (with-movement
1045 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1046 (fill-frame-up (current-child) parent)
1047 (fill-frame-down (current-child) parent))))
1049 (defun current-frame-fill-horizontal ()
1050 "Fill the current frame horizontally"
1051 (with-movement
1052 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1053 (fill-frame-left (current-child) parent)
1054 (fill-frame-right (current-child) parent))))
1057 ;;; Resize
1058 (defun current-frame-resize-up ()
1059 "Resize the current frame up to its half height"
1060 (with-movement (resize-half-height-up (current-child))))
1062 (defun current-frame-resize-down ()
1063 "Resize the current frame down to its half height"
1064 (with-movement (resize-half-height-down (current-child))))
1066 (defun current-frame-resize-left ()
1067 "Resize the current frame left to its half width"
1068 (with-movement (resize-half-width-left (current-child))))
1070 (defun current-frame-resize-right ()
1071 "Resize the current frame right to its half width"
1072 (with-movement (resize-half-width-right (current-child))))
1074 (defun current-frame-resize-all-dir ()
1075 "Resize down the current frame"
1076 (with-movement (resize-frame-down (current-child))))
1078 (defun current-frame-resize-all-dir-minimal ()
1079 "Resize down the current frame to its minimal size"
1080 (with-movement (resize-minimal-frame (current-child))))
1083 ;;; Children navigation
1084 (defun with-movement-select-next-brother ()
1085 "Select the next brother frame"
1086 (with-movement (select-next-brother-simple)))
1088 (defun with-movement-select-previous-brother ()
1089 "Select the previous brother frame"
1090 (with-movement (select-previous-brother-simple)))
1092 (defun with-movement-select-next-level ()
1093 "Select the next level"
1094 (with-movement (select-next-level)))
1096 (defun with-movement-select-previous-level ()
1097 "Select the previous levelframe"
1098 (with-movement (select-previous-level)))
1100 (defun with-movement-select-next-child ()
1101 "Select the next child"
1102 (with-movement (select-next-child-simple)))
1106 ;;; Adapt frame functions
1107 (defun adapt-current-frame-to-window-hints-generic (width-p height-p)
1108 "Adapt the current frame to the current window minimal size hints"
1109 (when (frame-p (current-child))
1110 (let ((window (first (frame-child (current-child)))))
1111 (when (xlib:window-p window)
1112 (let* ((hints (xlib:wm-normal-hints window))
1113 (min-width (and hints (xlib:wm-size-hints-min-width hints)))
1114 (min-height (and hints (xlib:wm-size-hints-min-height hints))))
1115 (when (and width-p min-width)
1116 (setf (frame-rw (current-child)) min-width))
1117 (when (and height-p min-height)
1118 (setf (frame-rh (current-child)) min-height))
1119 (fixe-real-size (current-child) (find-parent-frame (current-child)))
1120 (leave-second-mode))))))
1122 (defun adapt-current-frame-to-window-hints ()
1123 "Adapt the current frame to the current window minimal size hints"
1124 (adapt-current-frame-to-window-hints-generic t t))
1126 (defun adapt-current-frame-to-window-width-hint ()
1127 "Adapt the current frame to the current window minimal width hint"
1128 (adapt-current-frame-to-window-hints-generic t nil))
1130 (defun adapt-current-frame-to-window-height-hint ()
1131 "Adapt the current frame to the current window minimal height hint"
1132 (adapt-current-frame-to-window-hints-generic nil t))
1137 ;;; Managed window type functions
1138 (defun current-frame-manage-window-type-generic (type-list)
1139 (when (frame-p (current-child))
1140 (setf (frame-managed-type (current-child)) type-list
1141 (frame-forced-managed-window (current-child)) nil
1142 (frame-forced-unmanaged-window (current-child)) nil))
1143 (leave-second-mode))
1146 (defun current-frame-manage-window-type ()
1147 "Change window types to be managed by a frame"
1148 (when (frame-p (current-child))
1149 (let* ((type-str (query-string "Managed window type: (all, normal, transient, maxsize, desktop, dock, toolbar, menu, utility, splash, dialog)"
1150 (format nil "~{~:(~A~) ~}" (frame-managed-type (current-child)))))
1151 (type-list (loop :for type :in (split-string type-str)
1152 :collect (intern (string-upcase type) :keyword))))
1153 (current-frame-manage-window-type-generic type-list))))
1156 (defun current-frame-manage-all-window-type ()
1157 "Manage all window type"
1158 (current-frame-manage-window-type-generic '(:all)))
1160 (defun current-frame-manage-only-normal-window-type ()
1161 "Manage only normal window type"
1162 (current-frame-manage-window-type-generic '(:normal)))
1164 (defun current-frame-manage-no-window-type ()
1165 "Do not manage any window type"
1166 (current-frame-manage-window-type-generic nil))
1175 ;;; Force window functions
1176 (defun force-window-in-frame ()
1177 "Force the current window to move in the frame (Useful only for unmanaged windows)"
1178 (with-current-window
1179 (let ((parent (find-parent-frame window)))
1180 (setf (x-drawable-x window) (frame-rx parent)
1181 (x-drawable-y window) (frame-ry parent))
1182 (xlib:display-finish-output *display*)))
1183 (leave-second-mode))
1186 (defun force-window-center-in-frame ()
1187 "Force the current window to move in the center of the frame (Useful only for unmanaged windows)"
1188 (with-current-window
1189 (let ((parent (find-parent-frame window)))
1190 (setf (x-drawable-x window) (truncate (+ (frame-rx parent)
1191 (/ (- (frame-rw parent)
1192 (x-drawable-width window)) 2)))
1193 (x-drawable-y window) (truncate (+ (frame-ry parent)
1194 (/ (- (frame-rh parent)
1195 (x-drawable-height window)) 2))))
1196 (xlib:display-finish-output *display*)))
1197 (leave-second-mode))
1201 (defun display-current-window-info ()
1202 "Display information on the current window"
1203 (with-current-window
1204 (info-mode (list (format nil "Window: ~A" window)
1205 (format nil "Window name: ~A" (xlib:wm-name window))
1206 (format nil "Window class: ~A" (xlib:get-wm-class window))
1207 (format nil "Window type: ~:(~A~)" (window-type window))
1208 (format nil "Window id: 0x~X" (xlib:window-id window))
1209 (format nil "Window transparency: ~A" (* 100 (window-transparency window))))))
1210 (leave-second-mode))
1212 (defun set-current-window-transparency ()
1213 "Set the current window transparency"
1214 (with-current-window
1215 (ask-child-transparency "window" window))
1216 (leave-second-mode))
1219 (defun manage-current-window ()
1220 "Force to manage the current window by its parent frame"
1221 (with-current-window
1222 (let ((parent (find-parent-frame window)))
1223 (with-slots ((managed forced-managed-window)
1224 (unmanaged forced-unmanaged-window)) parent
1225 (setf unmanaged (child-remove window unmanaged)
1226 unmanaged (remove (xlib:wm-name window) unmanaged :test #'string-equal-p))
1227 (pushnew window managed))))
1228 (leave-second-mode))
1230 (defun unmanage-current-window ()
1231 "Force to not manage the current window by its parent frame"
1232 (with-current-window
1233 (let ((parent (find-parent-frame window)))
1234 (with-slots ((managed forced-managed-window)
1235 (unmanaged forced-unmanaged-window)) parent
1236 (setf managed (child-remove window managed)
1237 managed (remove (xlib:wm-name window) managed :test #'string-equal-p))
1238 (pushnew window unmanaged))))
1239 (leave-second-mode))
1243 ;;; Moving child with the mouse button
1244 (defun mouse-move-child-over-frame (window root-x root-y)
1245 "Move the child under the mouse cursor to another frame"
1246 (declare (ignore window))
1247 (let ((child (find-child-under-mouse root-x root-y)))
1248 (unless (child-root-p child)
1249 (hide-all child)
1250 (remove-child-in-frame child (find-parent-frame child))
1251 (wait-mouse-button-release 50 51)
1252 (multiple-value-bind (x y)
1253 (xlib:query-pointer *root*)
1254 (let ((dest (find-child-under-mouse x y)))
1255 (when (xlib:window-p dest)
1256 (setf dest (find-parent-frame dest)))
1257 (unless (child-equal-p child dest)
1258 (move-child-to child dest)
1259 (show-all-children))))))
1260 (stop-button-event))
1265 ;;; Hide/Show frame window functions
1266 (defun hide/show-frame-window (frame value)
1267 "Hide/show the frame window"
1268 (when (frame-p frame)
1269 (setf (frame-show-window-p (current-child)) value)
1270 (show-all-children))
1271 (leave-second-mode))
1274 (defun hide-current-frame-window ()
1275 "Hide the current frame window"
1276 (hide/show-frame-window (current-child) nil))
1278 (defun show-current-frame-window ()
1279 "Show the current frame window"
1280 (hide/show-frame-window (current-child) t))
1284 ;;; Hide/Unhide current child
1285 (defun hide-current-child ()
1286 "Hide the current child"
1287 (unless (child-root-p (current-child))
1288 (let ((parent (find-parent-frame (current-child))))
1289 (when (frame-p parent)
1290 (with-slots (child hidden-children) parent
1291 (hide-all (current-child))
1292 (setf child (child-remove (current-child) child))
1293 (pushnew (current-child) hidden-children)
1294 (setf (current-child) parent))
1295 (show-all-children)))
1296 (leave-second-mode)))
1299 (defun frame-unhide-child (hidden frame-src frame-dest)
1300 "Unhide a hidden child from frame-src in frame-dest"
1301 (with-slots (hidden-children) frame-src
1302 (setf hidden-children (child-remove hidden hidden-children)))
1303 (with-slots (child) frame-dest
1304 (pushnew hidden child)))
1308 (defun unhide-a-child ()
1309 "Unhide a child in the current frame"
1310 (when (frame-p (current-child))
1311 (with-slots (child hidden-children) (current-child)
1312 (info-mode-menu (loop :for i :from 0
1313 :for hidden :in hidden-children
1314 :collect (list (code-char (+ (char-code #\a) i))
1315 (let ((lhd hidden))
1316 (lambda ()
1317 (frame-unhide-child lhd (current-child) (current-child))))
1318 (format nil "Unhide ~A" (child-fullname hidden))))))
1319 (show-all-children))
1320 (leave-second-mode))
1323 (defun unhide-all-children ()
1324 "Unhide all current frame hidden children"
1325 (when (frame-p (current-child))
1326 (with-slots (child hidden-children) (current-child)
1327 (dolist (c hidden-children)
1328 (pushnew c child))
1329 (setf hidden-children nil))
1330 (show-all-children))
1331 (leave-second-mode))
1334 (defun unhide-a-child-from-all-frames ()
1335 "Unhide a child from all frames in the current frame"
1336 (when (frame-p (current-child))
1337 (let ((acc nil)
1338 (keynum -1))
1339 (with-all-frames (*root-frame* frame)
1340 (when (frame-hidden-children frame)
1341 (push (format nil "~A" (child-fullname frame)) acc)
1342 (dolist (hidden (frame-hidden-children frame))
1343 (push (list (code-char (+ (char-code #\a) (incf keynum)))
1344 (let ((lhd hidden))
1345 (lambda ()
1346 (frame-unhide-child lhd frame (current-child))))
1347 (format nil "Unhide ~A" (child-fullname hidden)))
1348 acc))))
1349 (info-mode-menu (nreverse acc)))
1350 (show-all-children))
1351 (leave-second-mode))
1357 (let ((last-child nil))
1358 (defun init-last-child ()
1359 (setf last-child nil))
1360 (defun switch-to-last-child ()
1361 "Store the current child and switch to the previous one"
1362 (let ((current-child (current-child)))
1363 (when last-child
1364 (change-root (find-root last-child) last-child)
1365 (setf (current-child) last-child)
1366 (focus-all-children (current-child) (current-child))
1367 (show-all-children t))
1368 (setf last-child current-child))
1369 (leave-second-mode)))
1377 ;;; Focus policy functions
1378 (defun set-focus-policy-generic (focus-policy)
1379 (when (frame-p (current-child))
1380 (setf (frame-focus-policy (current-child)) focus-policy))
1381 (leave-second-mode))
1384 (defun current-frame-set-click-focus-policy ()
1385 "Set a click focus policy for the current frame."
1386 (set-focus-policy-generic :click))
1388 (defun current-frame-set-sloppy-focus-policy ()
1389 "Set a sloppy focus policy for the current frame."
1390 (set-focus-policy-generic :sloppy))
1392 (defun current-frame-set-sloppy-strict-focus-policy ()
1393 "Set a (strict) sloppy focus policy only for windows in the current frame."
1394 (set-focus-policy-generic :sloppy-strict))
1396 (defun current-frame-set-sloppy-select-policy ()
1397 "Set a sloppy select policy for the current frame."
1398 (set-focus-policy-generic :sloppy-select))
1402 (defun set-focus-policy-generic-for-all (focus-policy)
1403 (with-all-frames (*root-frame* frame)
1404 (setf (frame-focus-policy frame) focus-policy))
1405 (leave-second-mode))
1408 (defun all-frames-set-click-focus-policy ()
1409 "Set a click focus policy for all frames."
1410 (set-focus-policy-generic-for-all :click))
1412 (defun all-frames-set-sloppy-focus-policy ()
1413 "Set a sloppy focus policy for all frames."
1414 (set-focus-policy-generic-for-all :sloppy))
1416 (defun all-frames-set-sloppy-strict-focus-policy ()
1417 "Set a (strict) sloppy focus policy for all frames."
1418 (set-focus-policy-generic-for-all :sloppy-strict))
1420 (defun all-frames-set-sloppy-select-policy ()
1421 "Set a sloppy select policy for all frames."
1422 (set-focus-policy-generic-for-all :sloppy-select))
1426 ;;; Ensure unique name/number functions
1427 (defun extract-number-from-name (name)
1428 (when (stringp name)
1429 (let* ((pos (1+ (or (position #\. name :from-end t) -1)))
1430 (number (parse-integer name :junk-allowed t :start pos)))
1431 (values number
1432 (if number (subseq name 0 (1- pos)) name)))))
1437 (defun ensure-unique-name ()
1438 "Ensure that all children names are unique"
1439 (with-all-children (*root-frame* child)
1440 (multiple-value-bind (num1 name1)
1441 (extract-number-from-name (child-name child))
1442 (declare (ignore num1))
1443 (when name1
1444 (let ((acc nil))
1445 (with-all-children (*root-frame* c)
1446 (unless (child-equal-p child c))
1447 (multiple-value-bind (num2 name2)
1448 (extract-number-from-name (child-name c))
1449 (when (string-equal name1 name2)
1450 (push num2 acc))))
1451 (dbg acc)
1452 (when (> (length acc) 1)
1453 (setf (child-name child)
1454 (format nil "~A.~A" name1
1455 (1+ (find-free-number (loop for i in acc when i collect (1- i)))))))))))
1456 (leave-second-mode))
1458 (defun ensure-unique-number ()
1459 "Ensure that all children numbers are unique"
1460 (let ((num -1))
1461 (with-all-frames (*root-frame* frame)
1462 (setf (frame-number frame) (incf num))))
1463 (leave-second-mode))
1467 ;;; Standard menu functions - Based on the XDG specifications
1468 (defun um-create-xdg-section-list (menu)
1469 (dolist (section *xdg-section-list*)
1470 (add-sub-menu menu :next section (format nil "~A" section) menu))
1471 (unless (find-toplevel-menu 'Utility menu)
1472 (add-sub-menu menu :next 'Utility (format nil "~A" 'Utility) menu)))
1474 (defun um-find-submenu (menu section-list)
1475 (let ((acc nil))
1476 (dolist (section section-list)
1477 (awhen (find-toplevel-menu (intern (string-upcase section) :clfswm) menu)
1478 (push it acc)))
1479 (if acc
1481 (list (find-toplevel-menu 'Utility menu)))))
1484 (defun um-extract-value (line)
1485 (second (split-string line #\=)))
1488 (defun um-add-desktop (desktop menu)
1489 (let (name exec categories comment)
1490 (when (probe-file desktop)
1491 (with-open-file (stream desktop :direction :input)
1492 (loop for line = (ignore-errors (read-line stream nil nil))
1493 while line
1495 (cond ((first-position "Name=" line) (setf name (um-extract-value line)))
1496 ((first-position "Exec=" line) (setf exec (um-extract-value line)))
1497 ((first-position "Categories=" line) (setf categories (um-extract-value line)))
1498 ((first-position "Comment=" line) (setf comment (um-extract-value line))))
1499 (when (and name exec categories)
1500 (let* ((sub-menu (um-find-submenu menu (split-string categories #\;)))
1501 (fun-name (intern name :clfswm)))
1502 (setf (symbol-function fun-name) (let ((do-exec exec))
1503 (lambda ()
1504 (do-shell do-exec)
1505 (leave-second-mode)))
1506 (documentation fun-name 'function) (format nil "~A~A" name (if comment
1507 (format nil " - ~A" comment)
1508 "")))
1509 (dolist (m sub-menu)
1510 (add-menu-key (menu-name m) :next fun-name m)))
1511 (setf name nil exec nil categories nil comment nil)))))))
1514 (defun update-menus (&optional (menu (make-menu :name 'main :doc "Main menu")))
1515 (um-create-xdg-section-list menu)
1516 (let ((count 0)
1517 (found (make-hash-table :test #'equal)))
1518 (dolist (dir (remove-duplicates
1519 (split-string (or (getenv "XDG_DATA_DIRS") "/usr/local/share/:/usr/share/")
1520 #\:) :test #'string-equal))
1521 (dolist (desktop (directory (concatenate 'string dir "/applications/**/*.desktop")))
1522 (unless (gethash (file-namestring desktop) found)
1523 (setf (gethash (file-namestring desktop) found) t)
1524 (um-add-desktop desktop menu)
1525 (incf count))))
1526 menu))
1530 ;;; Close/Kill focused window
1532 (defun ask-close/kill-current-window ()
1533 "Close or kill the current window (ask before doing anything)"
1534 (let ((window (xlib:input-focus *display*))
1535 (*info-mode-placement* *ask-close/kill-placement*))
1536 (info-mode-menu
1537 (if (and window (not (xlib:window-equal window *no-focus-window*)))
1538 `(,(format nil "Focus window: ~A" (xlib:wm-name window))
1539 (#\s delete-focus-window "Close the focus window")
1540 (#\k destroy-focus-window "Kill the focus window")
1541 (#\x cut-focus-window)
1542 (#\c copy-focus-window)
1543 (#\v paste-selection))
1544 `(,(format nil "Focus window: None")
1545 (#\v paste-selection))))
1550 ;;; Other window manager functions
1551 (defun get-proc-list ()
1552 (let ((proc (do-shell "ps x -o pid=" nil t))
1553 (proc-list nil))
1554 (loop for line = (read-line proc nil nil)
1555 while line
1556 do (push line proc-list))
1557 (dbg proc-list)
1558 proc-list))
1560 (defun run-other-window-manager ()
1561 (let ((proc-start (get-proc-list)))
1562 (do-shell *other-window-manager* nil t :terminal)
1563 (let* ((proc-end (get-proc-list))
1564 (proc-diff (set-difference proc-end proc-start :test #'equal)))
1565 (dbg 'killing-sigterm proc-diff)
1566 (do-shell (format nil "kill ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1567 (dbg 'killing-sigkill proc-diff)
1568 (do-shell (format nil "kill -9 ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1569 (sleep 1))
1570 (setf *other-window-manager* nil)))
1573 (defun do-run-other-window-manager (window-manager)
1574 (setf *other-window-manager* window-manager)
1575 (throw 'exit-main-loop nil))
1577 (defmacro def-run-other-window-manager (name &optional definition)
1578 (let ((definition (or definition name)))
1579 `(defun ,(create-symbol "run-" name) ()
1580 ,(format nil "Run ~A" definition)
1581 (do-run-other-window-manager ,(format nil "~A" name)))))
1583 (def-run-other-window-manager "xterm")
1584 (def-run-other-window-manager "icewm")
1585 (def-run-other-window-manager "twm")
1586 (def-run-other-window-manager "gnome-session" "Gnome")
1587 (def-run-other-window-manager "startkde" "KDE")
1588 (def-run-other-window-manager "xfce4-session" "XFCE")
1590 (defun run-lxde ()
1591 "Run LXDE"
1592 (do-run-other-window-manager "( lxsession & ); xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1594 (defun run-xfce4 ()
1595 "Run LXDE (xterm)"
1596 (do-run-other-window-manager "( xfce4-session &) ; xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1599 (defun run-prompt-wm ()
1600 "Prompt for an other window manager"
1601 (let ((wm (query-string "Run an other window manager:" "icewm")))
1602 (do-run-other-window-manager wm)))
1605 ;;; Hide or show unmanaged windows utility.
1606 (defun set-hide-unmanaged-window ()
1607 "Hide unmanaged windows when frame is not selected"
1608 (when (frame-p (current-child))
1609 (setf (frame-data-slot (current-child) :unmanaged-window-action) :hide)
1610 (leave-second-mode)))
1612 (defun set-show-unmanaged-window ()
1613 "Show unmanaged windows when frame is not selected"
1614 (when (frame-p (current-child))
1615 (setf (frame-data-slot (current-child) :unmanaged-window-action) :show)
1616 (leave-second-mode)))
1618 (defun set-default-hide-unmanaged-window ()
1619 "Set default behaviour to hide or not unmanaged windows when frame is not selected"
1620 (when (frame-p (current-child))
1621 (setf (frame-data-slot (current-child) :unmanaged-window-action) nil)
1622 (leave-second-mode)))
1624 (defun set-globally-hide-unmanaged-window ()
1625 "Hide unmanaged windows by default. This is overriden by functions above"
1626 (setf *hide-unmanaged-window* t)
1627 (leave-second-mode))
1629 (defun set-globally-show-unmanaged-window ()
1630 "Show unmanaged windows by default. This is overriden by functions above"
1631 (setf *hide-unmanaged-window* nil)
1632 (leave-second-mode))
1635 ;;; Speed mouse movement.
1636 (let (minx miny maxx maxy history lx ly)
1637 (labels ((middle (x1 x2)
1638 (round (/ (+ x1 x2) 2)))
1639 (reset-if-moved (x y)
1640 (when (or (/= x (or lx x)) (/= y (or ly y)))
1641 (speed-mouse-reset)))
1642 (add-in-history (x y)
1643 (push (list x y) history)))
1644 (defun speed-mouse-reset ()
1645 "Reset speed mouse coordinates"
1646 (setf minx nil miny nil maxx nil maxy nil history nil lx nil ly nil))
1647 (defun speed-mouse-left ()
1648 "Speed move mouse to left"
1649 (with-x-pointer
1650 (reset-if-moved x y)
1651 (setf maxx x)
1652 (add-in-history x y)
1653 (setf lx (middle (or minx 0) maxx))
1654 (xlib:warp-pointer *root* lx y)))
1655 (defun speed-mouse-right ()
1656 "Speed move mouse to right"
1657 (with-x-pointer
1658 (reset-if-moved x y)
1659 (setf minx x)
1660 (add-in-history x y)
1661 (setf lx (middle minx (or maxx (xlib:screen-width *screen*))))
1662 (xlib:warp-pointer *root* lx y)))
1663 (defun speed-mouse-up ()
1664 "Speed move mouse to up"
1665 (with-x-pointer
1666 (reset-if-moved x y)
1667 (setf maxy y)
1668 (add-in-history x y)
1669 (setf ly (middle (or miny 0) maxy))
1670 (xlib:warp-pointer *root* x ly)))
1671 (defun speed-mouse-down ()
1672 "Speed move mouse to down"
1673 (with-x-pointer
1674 (reset-if-moved x y)
1675 (setf miny y)
1676 (add-in-history x y)
1677 (setf ly (middle miny (or maxy (xlib:screen-height *screen*))))
1678 (xlib:warp-pointer *root* x ly)))
1679 (defun speed-mouse-undo ()
1680 "Undo last speed mouse move"
1681 (when history
1682 (let ((h (pop history)))
1683 (when h
1684 (destructuring-bind (bx by) h
1685 (setf lx bx ly by
1686 minx nil maxx nil
1687 miny nil maxy nil)
1688 (xlib:warp-pointer *root* lx ly))))))
1689 (defun speed-mouse-first-history ()
1690 "Revert to the first speed move mouse"
1691 (when history
1692 (let ((h (first (last history))))
1693 (when h
1694 (setf lx (first h)
1695 ly (second h))
1696 (xlib:warp-pointer *root* lx ly)))))))
1700 ;;; Notify window functions
1701 (let (font
1702 window
1704 width height
1705 text
1706 current-child)
1707 (labels ((text-string (tx)
1708 (typecase tx
1709 (cons (first tx))
1710 (t tx)))
1711 (text-color (tx)
1712 (get-color (typecase tx
1713 (cons (second tx))
1714 (t *notify-window-foreground*)))))
1715 (defun is-notify-window-p (win)
1716 (when (and (xlib:window-p win) (xlib:window-p window))
1717 (xlib:window-equal win window)))
1719 (defun raise-notify-window ()
1720 (raise-window window))
1722 (defun refresh-notify-window ()
1723 (add-timer 0.1 #'refresh-notify-window :refresh-notify-window)
1724 (when (and window gc font)
1725 (raise-window window)
1726 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1727 (loop for tx in text
1728 for i from 1 do
1729 (setf (xlib:gcontext-foreground gc) (text-color tx))
1730 (xlib:draw-glyphs window gc
1731 (truncate (/ (- width (* (xlib:max-char-width font) (length (text-string tx)))) 2))
1732 (* text-height i 2)
1733 (text-string tx))))))
1735 (defun close-notify-window ()
1736 (erase-timer :refresh-notify-window)
1737 (setf *never-managed-window-list*
1738 (remove (list #'is-notify-window-p 'raise-window)
1739 *never-managed-window-list* :test #'equal))
1740 (when gc
1741 (xlib:free-gcontext gc))
1742 (when window
1743 (xlib:destroy-window window))
1744 (when font
1745 (xlib:close-font font))
1746 (xlib:display-finish-output *display*)
1747 (setf window nil
1748 gc nil
1749 font nil))
1751 (defun open-notify-window (text-list)
1752 (close-notify-window)
1753 (setf font (xlib:open-font *display* *notify-window-font-string*))
1754 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1755 (setf text text-list)
1756 (setf width (* (xlib:max-char-width font) (+ (loop for tx in text-list
1757 maximize (length (text-string tx))) 2))
1758 height (+ (* text-height (length text-list) 2) text-height))
1759 (with-placement (*notify-window-placement* x y width height)
1760 (setf window (xlib:create-window :parent *root*
1761 :x x
1762 :y y
1763 :width width
1764 :height height
1765 :background (get-color *notify-window-background*)
1766 :border-width *border-size*
1767 :border (get-color *notify-window-border*)
1768 :colormap (xlib:screen-default-colormap *screen*)
1769 :event-mask '(:exposure :key-press))
1770 gc (xlib:create-gcontext :drawable window
1771 :foreground (get-color *notify-window-foreground*)
1772 :background (get-color *notify-window-background*)
1773 :font font
1774 :line-style :solid))
1775 (setf (window-transparency window) *notify-window-transparency*)
1776 (when (frame-p (current-child))
1777 (setf current-child (current-child)))
1778 (add-in-never-managed-window-list (list 'is-notify-window-p 'raise-window))
1779 (map-window window)
1780 (refresh-notify-window)
1781 (xlib:display-finish-output *display*))))))
1783 (defun notify-message (delay &rest messages)
1784 (erase-timer :close-notify-window)
1785 (funcall #'open-notify-window messages)
1786 (add-timer delay #'close-notify-window :close-notify-window))
1789 (defun display-hello-window ()
1790 (notify-message *notify-window-delay*
1791 '("Welcome to CLFSWM" "yellow")
1792 "Press Alt+F1 for help"))
1795 ;;; Run or raise functions
1796 (defun run-or-raise (raisep run-fn &key (maximized nil))
1797 (let ((window (with-all-windows (*root-frame* win)
1798 (when (funcall raisep win)
1799 (return win)))))
1800 (if window
1801 (let ((parent (find-parent-frame window)))
1802 (setf (current-child) parent)
1803 (put-child-on-top window parent)
1804 (when maximized
1805 (change-root (find-root parent) parent))
1806 (focus-all-children window parent)
1807 (show-all-children t))
1808 (funcall run-fn))))
1810 ;;; Transparency setting
1811 (defun inc-transparency (window root-x root-y)
1812 "Increment the child under mouse transparency"
1813 (declare (ignore root-x root-y))
1814 (unless *in-second-mode* (stop-button-event))
1815 (incf (child-transparency window) 0.1))
1817 (defun dec-transparency (window root-x root-y)
1818 "Decrement the child under mouse transparency"
1819 (declare (ignore root-x root-y))
1820 (unless *in-second-mode* (stop-button-event))
1821 (decf (child-transparency window) 0.1))
1823 (defun inc-transparency-slow (window root-x root-y)
1824 "Increment slowly the child under mouse transparency"
1825 (declare (ignore root-x root-y))
1826 (unless *in-second-mode* (stop-button-event))
1827 (incf (child-transparency window) 0.01))
1829 (defun dec-transparency-slow (window root-x root-y)
1830 "Decrement slowly the child under mouse transparency"
1831 (declare (ignore root-x root-y))
1832 (unless *in-second-mode* (stop-button-event))
1833 (decf (child-transparency window) 0.01))
1836 (defun key-inc-transparency ()
1837 "Increment the current window transparency"
1838 (with-current-window
1839 (incf (child-transparency window) 0.1)))
1841 (defun key-dec-transparency ()
1842 "Decrement the current window transparency"
1843 (with-current-window
1844 (decf (child-transparency window) 0.1)))
1850 ;;; Geometry change functions
1851 (defun swap-frame-geometry ()
1852 "Swap current brother frame geometry"
1853 (when (frame-p (current-child))
1854 (let ((parent (find-parent-frame (current-child))))
1855 (when (frame-p parent)
1856 (let ((brother (second (frame-child parent))))
1857 (when (frame-p brother)
1858 (rotatef (frame-x (current-child)) (frame-x brother))
1859 (rotatef (frame-y (current-child)) (frame-y brother))
1860 (rotatef (frame-w (current-child)) (frame-w brother))
1861 (rotatef (frame-h (current-child)) (frame-h brother))
1862 (show-all-children t)
1863 (leave-second-mode)))))))
1865 (defun rotate-frame-geometry-generic (fun)
1866 "(Rotate brother frame geometry"
1867 (when (frame-p (current-child))
1868 (let ((parent (find-parent-frame (current-child))))
1869 (when (frame-p parent)
1870 (let* ((child-list (funcall fun (frame-child parent)))
1871 (first (first child-list)))
1872 (dolist (child (rest child-list))
1873 (when (and (frame-p first) (frame-p child))
1874 (rotatef (frame-x first) (frame-x child))
1875 (rotatef (frame-y first) (frame-y child))
1876 (rotatef (frame-w first) (frame-w child))
1877 (rotatef (frame-h first) (frame-h child))
1878 (setf first child)))
1879 (show-all-children t))))))
1882 (defun rotate-frame-geometry ()
1883 "Rotate brother frame geometry"
1884 (rotate-frame-geometry-generic #'identity))
1886 (defun anti-rotate-frame-geometry ()
1887 "Anti rotate brother frame geometry"
1888 (rotate-frame-geometry-generic #'reverse))