4521c0cb8d243ba32106c54819c8a9d1a235bda8
[clfswm.git] / src / clfswm-util.lisp
blob4521c0cb8d243ba32106c54819c8a9d1a235bda8
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2005-2013 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-child (child-rect-child child-rect) x y)
371 (return-from find-child-under-mouse-in-child-tree (child-rect-child child-rect)))))
375 (defun find-child-under-mouse (x y &optional also-never-managed)
376 "Return the child under the mouse"
377 (or (and also-never-managed
378 (find-child-under-mouse-in-never-managed-windows x y))
379 (find-child-under-mouse-in-child-tree x y)))
384 ;;; Selection functions
385 (defun clear-selection ()
386 "Clear the current selection"
387 (setf *child-selection* nil)
388 (display-all-root-frame-info))
390 (defun copy-current-child ()
391 "Copy the current child to the selection"
392 (pushnew (current-child) *child-selection*)
393 (display-all-root-frame-info))
396 (defun cut-current-child (&optional (show-now t))
397 "Cut the current child to the selection"
398 (unless (child-root-p (current-child))
399 (let ((parent (find-parent-frame (current-child))))
400 (hide-all (current-child))
401 (copy-current-child)
402 (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
403 (when parent
404 (setf (current-child) parent))
405 (when show-now
406 (show-all-children t))
407 (current-child))))
409 (defun remove-current-child ()
410 "Remove the current child from its parent frame"
411 (unless (child-root-p (current-child))
412 (let ((parent (find-parent-frame (current-child))))
413 (hide-all (current-child))
414 (remove-child-in-frame (current-child) (find-parent-frame (current-child) (find-current-root)))
415 (when parent
416 (setf (current-child) parent))
417 (show-all-children t)
418 (leave-second-mode))))
420 (defun delete-current-child ()
421 "Delete the current child and its children in all frames"
422 (unless (child-root-p (current-child))
423 (hide-all (current-child))
424 (delete-child-and-children-in-all-frames (current-child))
425 (show-all-children t)
426 (leave-second-mode)))
429 (defun paste-selection-no-clear ()
430 "Paste the selection in the current frame - Do not clear the selection after paste"
431 (when (frame-p (current-child))
432 (dolist (child *child-selection*)
433 (unless (find-child-in-parent child (current-child))
434 (pushnew child (frame-child (current-child)) :test #'child-equal-p)))
435 (show-all-children)))
437 (defun paste-selection ()
438 "Paste the selection in the current frame"
439 (when (frame-p (current-child))
440 (paste-selection-no-clear)
441 (setf *child-selection* nil)
442 (display-all-root-frame-info)))
445 (defun copy-focus-window ()
446 "Copy the focus window to the selection"
447 (with-focus-window (window)
448 (with-current-child (window)
449 (copy-current-child))))
452 (defun cut-focus-window ()
453 "Cut the focus window to the selection"
454 (with-focus-window (window)
455 (setf (current-child) (with-current-child (window)
456 (cut-current-child nil)))
457 (show-all-children t)))
464 ;;; Maximize function
465 (defun frame-toggle-maximize ()
466 "Maximize/Unmaximize the current frame in its parent frame"
467 (when (frame-p (current-child))
468 (let ((unmaximized-coords (frame-data-slot (current-child) :unmaximized-coords)))
469 (if unmaximized-coords
470 (with-slots (x y w h) (current-child)
471 (destructuring-bind (nx ny nw nh) unmaximized-coords
472 (setf (frame-data-slot (current-child) :unmaximized-coords) nil
473 x nx y ny w nw h nh)))
474 (with-slots (x y w h) (current-child)
475 (setf (frame-data-slot (current-child) :unmaximized-coords)
476 (list x y w h)
477 x 0 y 0 w 1 h 1))))
478 (show-all-children)
479 (leave-second-mode)))
489 ;;; CONFIG - Identify mode
490 (defun identify-key ()
491 "Identify a key"
492 (let* ((done nil)
493 (font (xlib:open-font *display* *identify-font-string*))
494 (window (xlib:create-window :parent *root*
495 :x 0 :y 0
496 :width (- (xlib:screen-width *screen*) (* *border-size* 2))
497 :height (* 5 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font)))
498 :background (get-color *identify-background*)
499 :border-width *border-size*
500 :border (get-color *identify-border*)
501 :colormap (xlib:screen-default-colormap *screen*)
502 :event-mask '(:exposure)))
503 (gc (xlib:create-gcontext :drawable window
504 :foreground (get-color *identify-foreground*)
505 :background (get-color *identify-background*)
506 :font font
507 :line-style :solid)))
508 (setf (window-transparency window) *identify-transparency*)
509 (labels ((print-doc (msg hash-table-key pos code state)
510 (let ((function (find-key-from-code hash-table-key code state)))
511 (when (and function (fboundp (first function)))
512 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* pos (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
513 (format nil "~A ~A" msg (documentation (first function) 'function))))))
514 (print-key (code state keysym key modifiers)
515 (clear-pixmap-buffer window gc)
516 (setf (xlib:gcontext-foreground gc) (get-color *identify-foreground*))
517 (xlib:draw-glyphs *pixmap-buffer* gc 5 (+ (xlib:max-char-ascent font) 5)
518 (format nil "Press a key to identify. Press 'q' to stop the identify loop."))
519 (when code
520 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* 2 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
521 (format nil "Code=~A KeySym=~S Key=~S Modifiers=~A"
522 code keysym key modifiers))
523 (print-doc "Main mode : " *main-keys* 3 code state)
524 (print-doc "Second mode: " *second-keys* 4 code state))
525 (copy-pixmap-buffer window gc))
526 (handle-identify-key (&rest event-slots &key root code state &allow-other-keys)
527 (declare (ignore event-slots root))
528 (let* ((modifiers (state->modifiers state))
529 (key (keycode->char code state))
530 (keysym (keysym->keysym-name (keycode->keysym code modifiers))))
531 (setf done (and (equal key #\q) (equal modifiers *default-modifiers*)))
532 (dbg code keysym key modifiers)
533 (print-key code state keysym key modifiers)
534 (force-output)))
535 (handle-identify (&rest event-slots &key display event-key &allow-other-keys)
536 (declare (ignore display))
537 (case event-key
538 (:key-press (apply #'handle-identify-key event-slots) t)
539 (:exposure (print-key nil nil nil nil nil)))
541 (xgrab-pointer *root* 92 93)
542 (map-window window)
543 (format t "~&Press 'q' to stop the identify loop~%")
544 (print-key nil nil nil nil nil)
545 (force-output)
546 (unwind-protect
547 (loop until done do
548 (with-xlib-protect (:Identify-Loop nil)
549 (when (xlib:event-listen *display* *loop-timeout*)
550 (xlib:process-event *display* :handler #'handle-identify))
551 (xlib:display-finish-output *display*)))
552 (progn
553 (xlib:destroy-window window)
554 (xlib:close-font font)
555 (xgrab-pointer *root* 66 67))))))
562 (let ((all-symbols (collect-all-symbols)))
563 (defun eval-from-query-string ()
564 "Eval a lisp form from the query input"
565 (let ((form (query-string (format nil "Eval Lisp <~A> " (package-name *package*))
566 "" all-symbols))
567 (result nil))
568 (when (and form (not (equal form "")))
569 (let ((printed-result
570 (with-output-to-string (*standard-output*)
571 (setf result (handler-case
572 (loop for i in (multiple-value-list
573 (eval (read-from-string form)))
574 collect (format nil "~S" i))
575 (error (condition)
576 (format nil "~A" condition)))))))
577 (let ((ret (info-mode (expand-newline (append (ensure-list (format nil "> ~A" form))
578 (ensure-list printed-result)
579 (ensure-list result)))
580 :width (- (xlib:screen-width *screen*) 2))))
581 (when (or (search "defparameter" form :test #'string-equal)
582 (search "defvar" form :test #'string-equal))
583 (let ((elem (split-string form)))
584 (pushnew (string-downcase (if (string= (first elem) "(") (third elem) (second elem)))
585 all-symbols :test #'string=)))
586 (when (search "in-package" form :test #'string-equal)
587 (let ((*notify-window-placement* 'middle-middle-root-placement))
588 (open-notify-window '("Collecting all symbols for Lisp REPL completion."))
589 (setf all-symbols (collect-all-symbols))
590 (close-notify-window)))
591 (when ret
592 (eval-from-query-string))))))))
598 (let ((commands (command-in-path)))
599 (defun run-program-from-query-string ()
600 "Run a program from the query input"
601 (labels ((run-program-from-query-string-fun ()
602 (multiple-value-bind (program return)
603 (query-string "Run:" "" commands)
604 (when (and (equal return :return) program (not (equal program "")))
605 (let ((cmd (concatenate 'string "cd $HOME && exec " program)))
606 (lambda ()
607 (do-shell cmd)))))))
608 (let ((fun (run-program-from-query-string-fun)))
609 (when fun
610 (if *in-second-mode*
611 (progn
612 (setf *second-mode-leave-function* fun)
613 (leave-second-mode))
614 (funcall fun)))))))
620 ;;; Frame name actions
621 (defun ask-frame-name (msg)
622 "Ask a frame name"
623 (let ((all-frame-name nil))
624 (with-all-frames (*root-frame* frame)
625 (awhen (frame-name frame) (push it all-frame-name)))
626 (query-string msg "" all-frame-name)))
629 ;;; Focus by functions
630 (defun focus-frame-by (frame)
631 (when (frame-p frame)
632 (focus-all-children frame (or (find-parent-frame frame (find-current-root))
633 (find-parent-frame frame)
634 *root-frame*))
635 (show-all-children t)))
638 (defun focus-frame-by-name ()
639 "Focus a frame by name"
640 (focus-frame-by (find-frame-by-name (ask-frame-name "Focus frame:")))
641 (leave-second-mode))
643 (defun focus-frame-by-number ()
644 "Focus a frame by number"
645 (focus-frame-by (find-frame-by-number (query-number "Focus frame by number:")))
646 (leave-second-mode))
649 ;;; Open by functions
650 (defun open-frame-by (frame)
651 (when (frame-p frame)
652 (push (create-frame :name (query-string "Frame name")) (frame-child frame))
653 (show-all-children)))
657 (defun open-frame-by-name ()
658 "Open a new frame in a named frame"
659 (open-frame-by (find-frame-by-name (ask-frame-name "Open a new frame in: ")))
660 (leave-second-mode))
662 (defun open-frame-by-number ()
663 "Open a new frame in a numbered frame"
664 (open-frame-by (find-frame-by-number (query-number "Open a new frame in the group numbered:")))
665 (leave-second-mode))
668 ;;; Delete by functions
669 (defun delete-frame-by (frame)
670 (unless (or (child-equal-p frame *root-frame*)
671 (child-root-p frame))
672 (when (child-equal-p frame (current-child))
673 (setf (current-child) (find-current-root)))
674 (remove-child-in-frame frame (find-parent-frame frame)))
675 (show-all-children t))
678 (defun delete-frame-by-name ()
679 "Delete a frame by name"
680 (delete-frame-by (find-frame-by-name (ask-frame-name "Delete frame: ")))
681 (leave-second-mode))
683 (defun delete-frame-by-number ()
684 "Delete a frame by number"
685 (delete-frame-by (find-frame-by-number (query-number "Delete frame by number:")))
686 (leave-second-mode))
689 ;;; Move by function
690 (defun move-current-child-by-name ()
691 "Move current child in a named frame"
692 (move-child-to (current-child)
693 (find-frame-by-name
694 (ask-frame-name (format nil "Move '~A' to frame: " (child-name (current-child))))))
695 (leave-second-mode))
697 (defun move-current-child-by-number ()
698 "Move current child in a numbered frame"
699 (move-child-to (current-child)
700 (find-frame-by-number
701 (query-number (format nil "Move '~A' to frame numbered:" (child-name (current-child))))))
702 (leave-second-mode))
705 ;;; Copy by function
706 (defun copy-child-to (child frame-dest)
707 (when (and child (frame-p frame-dest))
708 (pushnew child (frame-child frame-dest))
709 (focus-all-children child frame-dest)
710 (show-all-children t)))
712 (defun copy-current-child-by-name ()
713 "Copy current child in a named frame"
714 (copy-child-to (current-child)
715 (find-frame-by-name
716 (ask-frame-name (format nil "Copy '~A' to frame: " (child-name (current-child))))))
717 (leave-second-mode))
719 (defun copy-current-child-by-number ()
720 "Copy current child in a numbered frame"
721 (copy-child-to (current-child)
722 (find-frame-by-number
723 (query-number (format nil "Copy '~A' to frame numbered:" (child-name (current-child))))))
724 (leave-second-mode))
729 ;;; Show frame info
730 (defun show-all-frames-info ()
731 "Show all frames info windows"
732 (let ((*show-root-frame-p* t))
733 (show-all-children)
734 (with-all-root-child (root)
735 (with-all-frames (root frame)
736 (raise-window (frame-window frame))
737 (display-frame-info frame)))))
739 (defun hide-all-frames-info ()
740 "Hide all frames info windows"
741 (show-all-children))
743 (defun show-all-frames-info-key ()
744 "Show all frames info windows until a key is release"
745 (show-all-frames-info)
746 (wait-no-key-or-button-press)
747 (hide-all-frames-info))
750 (defun move-frame (frame parent orig-x orig-y)
751 (when (and frame parent (not (child-root-p frame)))
752 (hide-all-children frame)
753 (with-slots (window) frame
754 (move-window window orig-x orig-y #'display-frame-info (list frame))
755 (setf (frame-x frame) (x-px->fl (x-drawable-x window) parent)
756 (frame-y frame) (y-px->fl (x-drawable-y window) parent)))
757 (show-all-children)))
759 (defun resize-frame (frame parent orig-x orig-y)
760 (when (and frame parent (not (child-root-p frame)))
761 (hide-all-children frame)
762 (with-slots (window) frame
763 (resize-window window orig-x orig-y #'display-frame-info (list frame))
764 (setf (frame-w frame) (w-px->fl (anti-adj-border-wh (x-drawable-width window) frame) parent)
765 (frame-h frame) (h-px->fl (anti-adj-border-wh (x-drawable-height window) frame) parent)))
766 (show-all-children)))
770 (defun mouse-click-to-focus-generic (root-x root-y mouse-fn)
771 "Focus the current frame or focus the current window parent
772 mouse-fun is #'move-frame or #'resize-frame"
773 (let* ((to-replay t)
774 (child (find-child-under-mouse root-x root-y))
775 (parent (find-parent-frame child))
776 (root-p (child-root-p child)))
777 (labels ((add-new-frame ()
778 (when (frame-p child)
779 (setf parent child
780 child (create-frame)
781 mouse-fn #'resize-frame
782 (current-child) child)
783 (place-frame child parent root-x root-y 10 10)
784 (map-window (frame-window child))
785 (pushnew child (frame-child parent)))))
786 (when (and root-p *create-frame-on-root*)
787 (add-new-frame))
788 (when (and (frame-p child) (not (child-root-p child)))
789 (funcall mouse-fn child parent root-x root-y))
790 (when (and child parent
791 (focus-all-children child parent (not (child-root-p child))))
792 (when (show-all-children)
793 (setf to-replay nil)))
794 (if to-replay
795 (replay-button-event)
796 (stop-button-event)))))
799 (defun mouse-click-to-focus-and-move (window root-x root-y)
800 "Move and focus the current frame or focus the current window parent.
801 Or do actions on corners"
802 (declare (ignore window))
803 (or (do-corner-action root-x root-y *corner-main-mode-left-button*)
804 (mouse-click-to-focus-generic root-x root-y #'move-frame)))
806 (defun mouse-click-to-focus-and-resize (window root-x root-y)
807 "Resize and focus the current frame or focus the current window parent.
808 Or do actions on corners"
809 (declare (ignore window))
810 (or (do-corner-action root-x root-y *corner-main-mode-right-button*)
811 (mouse-click-to-focus-generic root-x root-y #'resize-frame)))
813 (defun mouse-middle-click (window root-x root-y)
814 "Do actions on corners"
815 (declare (ignore window))
816 (or (do-corner-action root-x root-y *corner-main-mode-middle-button*)
817 (replay-button-event)))
822 (defun mouse-focus-move/resize-generic (root-x root-y mouse-fn window-parent)
823 "Focus the current frame or focus the current window parent
824 mouse-fun is #'move-frame or #'resize-frame.
825 Focus child and its parents -
826 For window: set current child to window or its parent according to window-parent"
827 (labels ((move/resize-managed (child)
828 (let ((parent (find-parent-frame child)))
829 (when (and child
830 (frame-p child)
831 (child-root-p child))
832 (setf parent child
833 child (create-frame)
834 mouse-fn #'resize-frame)
835 (place-frame child parent root-x root-y 10 10)
836 (map-window (frame-window child))
837 (push child (frame-child parent)))
838 (focus-all-children child parent window-parent)
839 (show-all-children)
840 (typecase child
841 (xlib:window
842 (if (managed-window-p child parent)
843 (funcall mouse-fn parent (find-parent-frame parent) root-x root-y)
844 (funcall (cond ((or (eql mouse-fn #'move-frame)
845 (eql mouse-fn #'move-frame-constrained))
846 #'move-window)
847 ((or (eql mouse-fn #'resize-frame)
848 (eql mouse-fn #'resize-frame-constrained))
849 #'resize-window))
850 child root-x root-y)))
851 (frame (funcall mouse-fn child parent root-x root-y)))
852 (show-all-children)))
853 (move/resize-never-managed (child raise-fun)
854 (funcall raise-fun child)
855 (funcall (cond ((eql mouse-fn #'move-frame) #'move-window)
856 ((eql mouse-fn #'resize-frame) #'resize-window))
857 child root-x root-y)))
858 (let ((child (find-child-under-mouse root-x root-y t)))
859 (multiple-value-bind (never-managed raise-fun)
860 (never-managed-window-p child)
861 (if (and (xlib:window-p child) never-managed raise-fun)
862 (move/resize-never-managed child raise-fun)
863 (move/resize-managed child))))))
866 (defun test-mouse-binding (window root-x root-y)
867 (dbg window root-x root-y)
868 (replay-button-event))
872 (defun mouse-select-next-level (window root-x root-y)
873 "Select the next level in frame"
874 (declare (ignore root-x root-y))
875 (let ((frame (find-frame-window window)))
876 (when (or frame (xlib:window-equal window *root*))
877 (select-next-level))
878 (replay-button-event)))
882 (defun mouse-select-previous-level (window root-x root-y)
883 "Select the previous 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-previous-level))
888 (replay-button-event)))
892 (defun mouse-enter-frame (window root-x root-y)
893 "Enter in the selected frame - ie make it the root 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 (enter-frame))
898 (replay-button-event)))
902 (defun mouse-leave-frame (window root-x root-y)
903 "Leave the selected frame - ie make its parent 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 (leave-frame))
908 (replay-button-event)))
912 ;;;;;,-----
913 ;;;;;| Various definitions
914 ;;;;;`-----
916 (defun show-help (&optional (browser "dillo") (tempfile "/tmp/clfswm.html"))
917 "Show current keys and buttons bindings"
918 (ignore-errors
919 (produce-doc-html-in-file tempfile))
920 (sleep 1)
921 (do-shell (format nil "~A ~A" browser tempfile)))
925 ;;; Bind or jump functions
926 (let ((key-slots (make-array 10 :initial-element nil))
927 (current-slot 1))
928 (defun reset-bind-or-jump-slots ()
929 (dotimes (i 10)
930 (setf (aref key-slots i) nil)))
932 (defun bind-on-slot (&optional (slot current-slot) child)
933 "Bind current child to slot"
934 (setf (aref key-slots slot) (if child child (current-child))))
936 (defun remove-binding-on-slot ()
937 "Remove binding on slot"
938 (setf (aref key-slots current-slot) nil))
940 (defun jump-to-slot ()
941 "Jump to slot"
942 (let ((jump-child (aref key-slots current-slot)))
943 (when (and jump-child (find-child jump-child *root-frame*))
944 (unless (find-child-in-all-root jump-child)
945 (change-root (find-root jump-child) jump-child))
946 (setf (current-child) jump-child)
947 (focus-all-children jump-child jump-child)
948 (show-all-children t))))
950 (defun bind-or-jump (n)
951 "Bind or jump to a slot (a frame or a window)"
952 (setf current-slot (- n 1))
953 (let ((default-bind `("b" bind-on-slot
954 ,(format nil "Bind slot ~A on child: ~A" n (child-fullname (current-child))))))
955 (info-mode-menu (aif (aref key-slots current-slot)
956 `(,default-bind
957 ("BackSpace" remove-binding-on-slot
958 ,(format nil "Remove slot ~A binding on child: ~A" n (child-fullname (current-child))))
959 (" - " nil " -")
960 ("Tab" jump-to-slot
961 ,(format nil "Jump to child: ~A" (aif (aref key-slots current-slot)
962 (child-fullname it)
963 "Not set - Please, bind it with 'b'")))
964 ("Return" jump-to-slot "Same thing")
965 ("space" jump-to-slot "Same thing"))
966 (list default-bind))))))
970 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
971 ;;; Useful function for the second mode ;;;
972 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
973 (defmacro with-movement (&body body)
974 `(when (frame-p (current-child))
975 (unwind-protect
976 (progn
977 ,@body)
978 (show-all-children)
979 (display-all-frame-info)
980 (draw-second-mode-window)
981 (open-menu (find-menu 'frame-movement-menu)))))
984 ;;; Pack
985 (defun current-frame-pack-up ()
986 "Pack the current frame up"
987 (with-movement (pack-frame-up (current-child) (find-parent-frame (current-child) (find-current-root)))))
989 (defun current-frame-pack-down ()
990 "Pack the current frame down"
991 (with-movement (pack-frame-down (current-child) (find-parent-frame (current-child) (find-current-root)))))
993 (defun current-frame-pack-left ()
994 "Pack the current frame left"
995 (with-movement (pack-frame-left (current-child) (find-parent-frame (current-child) (find-current-root)))))
997 (defun current-frame-pack-right ()
998 "Pack the current frame right"
999 (with-movement (pack-frame-right (current-child) (find-parent-frame (current-child) (find-current-root)))))
1001 ;;; Center
1002 (defun center-current-frame ()
1003 "Center the current frame"
1004 (with-movement (center-frame (current-child))))
1006 ;;; Fill
1007 (defun current-frame-fill-up ()
1008 "Fill the current frame up"
1009 (with-movement (fill-frame-up (current-child) (find-parent-frame (current-child) (find-current-root)))))
1011 (defun current-frame-fill-down ()
1012 "Fill the current frame down"
1013 (with-movement (fill-frame-down (current-child) (find-parent-frame (current-child) (find-current-root)))))
1015 (defun current-frame-fill-left ()
1016 "Fill the current frame left"
1017 (with-movement (fill-frame-left (current-child) (find-parent-frame (current-child) (find-current-root)))))
1019 (defun current-frame-fill-right ()
1020 "Fill the current frame right"
1021 (with-movement (fill-frame-right (current-child) (find-parent-frame (current-child) (find-current-root)))))
1023 (defun current-frame-fill-all-dir ()
1024 "Fill the current frame in all directions"
1025 (with-movement
1026 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1027 (fill-frame-up (current-child) parent)
1028 (fill-frame-down (current-child) parent)
1029 (fill-frame-left (current-child) parent)
1030 (fill-frame-right (current-child) parent))))
1032 (defun current-frame-fill-vertical ()
1033 "Fill the current frame vertically"
1034 (with-movement
1035 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1036 (fill-frame-up (current-child) parent)
1037 (fill-frame-down (current-child) parent))))
1039 (defun current-frame-fill-horizontal ()
1040 "Fill the current frame horizontally"
1041 (with-movement
1042 (let ((parent (find-parent-frame (current-child) (find-current-root))))
1043 (fill-frame-left (current-child) parent)
1044 (fill-frame-right (current-child) parent))))
1047 ;;; Resize
1048 (defun current-frame-resize-up ()
1049 "Resize the current frame up to its half height"
1050 (with-movement (resize-half-height-up (current-child))))
1052 (defun current-frame-resize-down ()
1053 "Resize the current frame down to its half height"
1054 (with-movement (resize-half-height-down (current-child))))
1056 (defun current-frame-resize-left ()
1057 "Resize the current frame left to its half width"
1058 (with-movement (resize-half-width-left (current-child))))
1060 (defun current-frame-resize-right ()
1061 "Resize the current frame right to its half width"
1062 (with-movement (resize-half-width-right (current-child))))
1064 (defun current-frame-resize-all-dir ()
1065 "Resize down the current frame"
1066 (with-movement (resize-frame-down (current-child))))
1068 (defun current-frame-resize-all-dir-minimal ()
1069 "Resize down the current frame to its minimal size"
1070 (with-movement (resize-minimal-frame (current-child))))
1073 ;;; Children navigation
1074 (defun with-movement-select-next-brother ()
1075 "Select the next brother frame"
1076 (with-movement (select-next-brother-simple)))
1078 (defun with-movement-select-previous-brother ()
1079 "Select the previous brother frame"
1080 (with-movement (select-previous-brother-simple)))
1082 (defun with-movement-select-next-level ()
1083 "Select the next level"
1084 (with-movement (select-next-level)))
1086 (defun with-movement-select-previous-level ()
1087 "Select the previous levelframe"
1088 (with-movement (select-previous-level)))
1090 (defun with-movement-select-next-child ()
1091 "Select the next child"
1092 (with-movement (select-next-child-simple)))
1096 ;;; Adapt frame functions
1097 (defun adapt-current-frame-to-window-hints-generic (width-p height-p)
1098 "Adapt the current frame to the current window minimal size hints"
1099 (when (frame-p (current-child))
1100 (let ((window (first (frame-child (current-child)))))
1101 (when (xlib:window-p window)
1102 (let* ((hints (xlib:wm-normal-hints window))
1103 (min-width (and hints (xlib:wm-size-hints-min-width hints)))
1104 (min-height (and hints (xlib:wm-size-hints-min-height hints))))
1105 (when (and width-p min-width)
1106 (setf (frame-rw (current-child)) min-width))
1107 (when (and height-p min-height)
1108 (setf (frame-rh (current-child)) min-height))
1109 (fixe-real-size (current-child) (find-parent-frame (current-child)))
1110 (leave-second-mode))))))
1112 (defun adapt-current-frame-to-window-hints ()
1113 "Adapt the current frame to the current window minimal size hints"
1114 (adapt-current-frame-to-window-hints-generic t t))
1116 (defun adapt-current-frame-to-window-width-hint ()
1117 "Adapt the current frame to the current window minimal width hint"
1118 (adapt-current-frame-to-window-hints-generic t nil))
1120 (defun adapt-current-frame-to-window-height-hint ()
1121 "Adapt the current frame to the current window minimal height hint"
1122 (adapt-current-frame-to-window-hints-generic nil t))
1127 ;;; Managed window type functions
1128 (defun current-frame-manage-window-type-generic (type-list)
1129 (when (frame-p (current-child))
1130 (setf (frame-managed-type (current-child)) type-list
1131 (frame-forced-managed-window (current-child)) nil
1132 (frame-forced-unmanaged-window (current-child)) nil))
1133 (leave-second-mode))
1136 (defun current-frame-manage-window-type ()
1137 "Change window types to be managed by a frame"
1138 (when (frame-p (current-child))
1139 (let* ((type-str (query-string "Managed window type: (all, normal, transient, maxsize, desktop, dock, toolbar, menu, utility, splash, dialog)"
1140 (format nil "~{~:(~A~) ~}" (frame-managed-type (current-child)))))
1141 (type-list (loop :for type :in (split-string type-str)
1142 :collect (intern (string-upcase type) :keyword))))
1143 (current-frame-manage-window-type-generic type-list))))
1146 (defun current-frame-manage-all-window-type ()
1147 "Manage all window type"
1148 (current-frame-manage-window-type-generic '(:all)))
1150 (defun current-frame-manage-only-normal-window-type ()
1151 "Manage only normal window type"
1152 (current-frame-manage-window-type-generic '(:normal)))
1154 (defun current-frame-manage-no-window-type ()
1155 "Do not manage any window type"
1156 (current-frame-manage-window-type-generic nil))
1165 ;;; Force window functions
1166 (defun force-window-in-frame ()
1167 "Force the current window to move in the frame (Useful only for unmanaged windows)"
1168 (with-current-window
1169 (let ((parent (find-parent-frame window)))
1170 (setf (x-drawable-x window) (frame-rx parent)
1171 (x-drawable-y window) (frame-ry parent))
1172 (xlib:display-finish-output *display*)))
1173 (leave-second-mode))
1176 (defun force-window-center-in-frame ()
1177 "Force the current window to move in the center of the frame (Useful only for unmanaged windows)"
1178 (with-current-window
1179 (let ((parent (find-parent-frame window)))
1180 (setf (x-drawable-x window) (truncate (+ (frame-rx parent)
1181 (/ (- (frame-rw parent)
1182 (x-drawable-width window)) 2)))
1183 (x-drawable-y window) (truncate (+ (frame-ry parent)
1184 (/ (- (frame-rh parent)
1185 (x-drawable-height window)) 2))))
1186 (xlib:display-finish-output *display*)))
1187 (leave-second-mode))
1191 (defun display-current-window-info ()
1192 "Display information on the current window"
1193 (with-current-window
1194 (info-mode (append (list (format nil "Window: ~A" window)
1195 (format nil "Window name: ~A" (xlib:wm-name window))
1196 (format nil "Window class: ~A" (xlib:get-wm-class window))
1197 (format nil "Window type: ~:(~A~)" (window-type window))
1198 (format nil "Window id: 0x~X" (xlib:window-id window))
1199 (format nil "Window transparency: ~A" (* 100 (window-transparency window)))
1200 (format nil " X=~A Y=~A W=~A H=~A"
1201 (x-drawable-x window) (x-drawable-y window)
1202 (x-drawable-width window) (x-drawable-height window)))
1203 (split-string (format nil "~A" (xlib:wm-normal-hints window)) #\Newline))))
1204 (leave-second-mode))
1206 (defun set-current-window-transparency ()
1207 "Set the current window transparency"
1208 (with-current-window
1209 (ask-child-transparency "window" window))
1210 (leave-second-mode))
1213 (defun manage-current-window ()
1214 "Force to manage the current window by its parent frame"
1215 (with-current-window
1216 (let ((parent (find-parent-frame window)))
1217 (with-slots ((managed forced-managed-window)
1218 (unmanaged forced-unmanaged-window)) parent
1219 (setf unmanaged (child-remove window unmanaged)
1220 unmanaged (remove (xlib:wm-name window) unmanaged :test #'string-equal-p))
1221 (pushnew window managed))))
1222 (leave-second-mode))
1224 (defun unmanage-current-window ()
1225 "Force to not manage the current window by its parent frame"
1226 (with-current-window
1227 (let ((parent (find-parent-frame window)))
1228 (with-slots ((managed forced-managed-window)
1229 (unmanaged forced-unmanaged-window)) parent
1230 (setf managed (child-remove window managed)
1231 managed (remove (xlib:wm-name window) managed :test #'string-equal-p))
1232 (pushnew window unmanaged))))
1233 (leave-second-mode))
1237 ;;; Moving child with the mouse button
1238 (defun mouse-move-child-over-frame (window root-x root-y)
1239 "Move the child under the mouse cursor to another frame"
1240 (declare (ignore window))
1241 (let ((child (find-child-under-mouse root-x root-y)))
1242 (unless (child-root-p child)
1243 (hide-all child)
1244 (let ((parent (find-parent-frame child)))
1245 (remove-child-in-frame child parent)
1246 (show-all-children)
1247 (wait-mouse-button-release 50 51)
1248 (multiple-value-bind (x y)
1249 (xlib:query-pointer *root*)
1250 (let ((dest (find-child-under-mouse x y)))
1251 (when (xlib:window-p dest)
1252 (setf dest (find-parent-frame dest)))
1253 (unless (child-equal-p child dest)
1254 (move-child-to child (or dest parent))))))
1255 (show-all-children)))
1256 (stop-button-event))
1261 ;;; Hide/Show frame window functions
1262 (defun hide/show-frame-window (frame value)
1263 "Hide/show the frame window"
1264 (when (frame-p frame)
1265 (setf (frame-show-window-p (current-child)) value)
1266 (show-all-children))
1267 (leave-second-mode))
1270 (defun hide-current-frame-window ()
1271 "Hide the current frame window"
1272 (hide/show-frame-window (current-child) nil))
1274 (defun show-current-frame-window ()
1275 "Show the current frame window"
1276 (hide/show-frame-window (current-child) t))
1280 ;;; Hide/Unhide current child
1281 (defun hide-current-child ()
1282 "Hide the current child"
1283 (unless (child-root-p (current-child))
1284 (let ((parent (find-parent-frame (current-child))))
1285 (when (frame-p parent)
1286 (with-slots (child hidden-children) parent
1287 (hide-all (current-child))
1288 (setf child (child-remove (current-child) child))
1289 (pushnew (current-child) hidden-children)
1290 (setf (current-child) parent))
1291 (show-all-children)))
1292 (leave-second-mode)))
1295 (defun frame-unhide-child (hidden frame-src frame-dest)
1296 "Unhide a hidden child from frame-src in frame-dest"
1297 (with-slots (hidden-children) frame-src
1298 (setf hidden-children (child-remove hidden hidden-children)))
1299 (with-slots (child) frame-dest
1300 (pushnew hidden child)))
1304 (defun unhide-a-child ()
1305 "Unhide a child in the current frame"
1306 (when (frame-p (current-child))
1307 (with-slots (child hidden-children) (current-child)
1308 (info-mode-menu (loop :for i :from 0
1309 :for hidden :in hidden-children
1310 :collect (list (code-char (+ (char-code #\a) i))
1311 (let ((lhd hidden))
1312 (lambda ()
1313 (frame-unhide-child lhd (current-child) (current-child))))
1314 (format nil "Unhide ~A" (child-fullname hidden))))))
1315 (show-all-children))
1316 (leave-second-mode))
1319 (defun unhide-all-children ()
1320 "Unhide all current frame hidden children"
1321 (when (frame-p (current-child))
1322 (with-slots (child hidden-children) (current-child)
1323 (dolist (c hidden-children)
1324 (pushnew c child))
1325 (setf hidden-children nil))
1326 (show-all-children))
1327 (leave-second-mode))
1330 (defun unhide-a-child-from-all-frames ()
1331 "Unhide a child from all frames in the current frame"
1332 (when (frame-p (current-child))
1333 (let ((acc nil)
1334 (keynum -1))
1335 (with-all-frames (*root-frame* frame)
1336 (when (frame-hidden-children frame)
1337 (push (format nil "~A" (child-fullname frame)) acc)
1338 (dolist (hidden (frame-hidden-children frame))
1339 (push (list (code-char (+ (char-code #\a) (incf keynum)))
1340 (let ((lhd hidden))
1341 (lambda ()
1342 (frame-unhide-child lhd frame (current-child))))
1343 (format nil "Unhide ~A" (child-fullname hidden)))
1344 acc))))
1345 (info-mode-menu (nreverse acc)))
1346 (show-all-children))
1347 (leave-second-mode))
1353 (let ((last-child nil))
1354 (defun init-last-child ()
1355 (setf last-child nil))
1356 (defun switch-to-last-child ()
1357 "Store the current child and switch to the previous one"
1358 (let ((current-child (current-child)))
1359 (when last-child
1360 (change-root (find-root last-child) last-child)
1361 (setf (current-child) last-child)
1362 (focus-all-children (current-child) (current-child))
1363 (show-all-children t))
1364 (setf last-child current-child))
1365 (leave-second-mode)))
1373 ;;; Focus policy functions
1374 (defun set-focus-policy-generic (focus-policy)
1375 (when (frame-p (current-child))
1376 (setf (frame-focus-policy (current-child)) focus-policy))
1377 (leave-second-mode))
1380 (defun current-frame-set-click-focus-policy ()
1381 "Set a click focus policy for the current frame."
1382 (set-focus-policy-generic :click))
1384 (defun current-frame-set-sloppy-focus-policy ()
1385 "Set a sloppy focus policy for the current frame."
1386 (set-focus-policy-generic :sloppy))
1388 (defun current-frame-set-sloppy-strict-focus-policy ()
1389 "Set a (strict) sloppy focus policy only for windows in the current frame."
1390 (set-focus-policy-generic :sloppy-strict))
1392 (defun current-frame-set-sloppy-select-policy ()
1393 "Set a sloppy select policy for the current frame."
1394 (set-focus-policy-generic :sloppy-select))
1396 (defun current-frame-set-sloppy-select-window-policy ()
1397 "Set a sloppy select window policy for the current frame."
1398 (set-focus-policy-generic :sloppy-select-window))
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))
1424 (defun all-frames-set-sloppy-select-window-policy ()
1425 "Set a sloppy select window policy for all frames."
1426 (set-focus-policy-generic-for-all :sloppy-select-window))
1430 ;;; Ensure unique name/number functions
1431 (defun extract-number-from-name (name)
1432 (when (stringp name)
1433 (let* ((pos (1+ (or (position #\. name :from-end t) -1)))
1434 (number (parse-integer name :junk-allowed t :start pos)))
1435 (values number
1436 (if number (subseq name 0 (1- pos)) name)))))
1441 (defun ensure-unique-name ()
1442 "Ensure that all children names are unique"
1443 (with-all-children (*root-frame* child)
1444 (multiple-value-bind (num1 name1)
1445 (extract-number-from-name (child-name child))
1446 (declare (ignore num1))
1447 (when name1
1448 (let ((acc nil))
1449 (with-all-children (*root-frame* c)
1450 (unless (child-equal-p child c))
1451 (multiple-value-bind (num2 name2)
1452 (extract-number-from-name (child-name c))
1453 (when (string-equal name1 name2)
1454 (push num2 acc))))
1455 (dbg acc)
1456 (when (> (length acc) 1)
1457 (setf (child-name child)
1458 (format nil "~A.~A" name1
1459 (1+ (find-free-number (loop for i in acc when i collect (1- i)))))))))))
1460 (leave-second-mode))
1462 (defun ensure-unique-number ()
1463 "Ensure that all children numbers are unique"
1464 (let ((num -1))
1465 (with-all-frames (*root-frame* frame)
1466 (setf (frame-number frame) (incf num))))
1467 (leave-second-mode))
1471 ;;; Standard menu functions - Based on the XDG specifications
1472 (defun um-create-xdg-section-list (menu)
1473 (dolist (section *xdg-section-list*)
1474 (add-sub-menu menu :next section (format nil "~A" section) menu))
1475 (unless (find-toplevel-menu 'Utility menu)
1476 (add-sub-menu menu :next 'Utility (format nil "~A" 'Utility) menu)))
1478 (defun um-find-submenu (menu section-list)
1479 (let ((acc nil))
1480 (dolist (section section-list)
1481 (awhen (find-toplevel-menu (intern (string-upcase section) :clfswm) menu)
1482 (push it acc)))
1483 (if acc
1485 (list (find-toplevel-menu 'Utility menu)))))
1488 (defun um-extract-value (line)
1489 (second (split-string line #\=)))
1492 (defun um-add-desktop (desktop menu)
1493 (let (name exec categories comment)
1494 (when (probe-file desktop)
1495 (with-open-file (stream desktop :direction :input)
1496 (loop for line = (ignore-errors (read-line stream nil nil))
1497 while line
1499 (cond ((first-position "Name=" line) (setf name (um-extract-value line)))
1500 ((first-position "Exec=" line) (setf exec (um-extract-value line)))
1501 ((first-position "Categories=" line) (setf categories (um-extract-value line)))
1502 ((first-position "Comment=" line) (setf comment (um-extract-value line))))
1503 (when (and name exec categories)
1504 (let* ((sub-menu (um-find-submenu menu (split-string categories #\;)))
1505 (fun-name (intern name :clfswm)))
1506 (setf (symbol-function fun-name) (let ((do-exec exec))
1507 (lambda ()
1508 (do-shell do-exec)
1509 (leave-second-mode)))
1510 (documentation fun-name 'function) (format nil "~A~A" name (if comment
1511 (format nil " - ~A" comment)
1512 "")))
1513 (dolist (m sub-menu)
1514 (add-menu-key (menu-name m) :next fun-name m)))
1515 (setf name nil exec nil categories nil comment nil)))))))
1518 (defun update-menus (&optional (menu (make-menu :name 'main :doc "Main menu")))
1519 (um-create-xdg-section-list menu)
1520 (let ((count 0)
1521 (found (make-hash-table :test #'equal)))
1522 (dolist (dir (remove-duplicates
1523 (split-string (or (getenv "XDG_DATA_DIRS") "/usr/local/share/:/usr/share/")
1524 #\:) :test #'string-equal))
1525 (dolist (desktop (directory (concatenate 'string dir "/applications/**/*.desktop")))
1526 (unless (gethash (file-namestring desktop) found)
1527 (setf (gethash (file-namestring desktop) found) t)
1528 (um-add-desktop desktop menu)
1529 (incf count))))
1530 menu))
1534 ;;; Close/Kill focused window
1536 (defun ask-close/kill-current-window ()
1537 "Close or kill the current window (ask before doing anything)"
1538 (let ((window (xlib:input-focus *display*))
1539 (*info-mode-placement* *ask-close/kill-placement*))
1540 (info-mode-menu
1541 (if (and window (not (xlib:window-equal window *no-focus-window*)))
1542 `(,(format nil "Focus window: ~A" (xlib:wm-name window))
1543 (#\s delete-focus-window "Close the focus window")
1544 (#\k destroy-focus-window "Kill the focus window")
1545 (#\x cut-focus-window)
1546 (#\c copy-focus-window)
1547 (#\v paste-selection))
1548 `(,(format nil "Focus window: None")
1549 (#\v paste-selection))))
1554 ;;; Other window manager functions
1555 (defun get-proc-list ()
1556 (let ((proc (do-shell "ps x -o pid=" nil t))
1557 (proc-list nil))
1558 (loop for line = (read-line proc nil nil)
1559 while line
1560 do (push line proc-list))
1561 (dbg proc-list)
1562 proc-list))
1564 (defun run-other-window-manager ()
1565 (let ((proc-start (get-proc-list)))
1566 (do-shell *other-window-manager* nil t :terminal)
1567 (let* ((proc-end (get-proc-list))
1568 (proc-diff (set-difference proc-end proc-start :test #'equal)))
1569 (dbg 'killing-sigterm proc-diff)
1570 (do-shell (format nil "kill ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1571 (dbg 'killing-sigkill proc-diff)
1572 (do-shell (format nil "kill -9 ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1573 (sleep 1))
1574 (setf *other-window-manager* nil)))
1577 (defun do-run-other-window-manager (window-manager)
1578 (setf *other-window-manager* window-manager)
1579 (throw 'exit-main-loop nil))
1581 (defmacro def-run-other-window-manager (name &optional definition)
1582 (let ((definition (or definition name)))
1583 `(defun ,(create-symbol "run-" name) ()
1584 ,(format nil "Run ~A" definition)
1585 (do-run-other-window-manager ,(format nil "~A" name)))))
1587 (def-run-other-window-manager "xterm")
1588 (def-run-other-window-manager "icewm")
1589 (def-run-other-window-manager "twm")
1590 (def-run-other-window-manager "gnome-session" "Gnome")
1591 (def-run-other-window-manager "startkde" "KDE")
1592 (def-run-other-window-manager "xfce4-session" "XFCE")
1594 (defun run-lxde ()
1595 "Run LXDE"
1596 (do-run-other-window-manager "( lxsession & ); xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1598 (defun run-xfce4 ()
1599 "Run LXDE (xterm)"
1600 (do-run-other-window-manager "( xfce4-session &) ; xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1603 (defun run-prompt-wm ()
1604 "Prompt for an other window manager"
1605 (let ((wm (query-string "Run an other window manager:" "icewm")))
1606 (do-run-other-window-manager wm)))
1609 ;;; Hide or show unmanaged windows utility.
1610 (defun set-hide-unmanaged-window ()
1611 "Hide unmanaged windows when frame is not selected"
1612 (when (frame-p (current-child))
1613 (setf (frame-data-slot (current-child) :unmanaged-window-action) :hide)
1614 (leave-second-mode)))
1616 (defun set-show-unmanaged-window ()
1617 "Show unmanaged windows when frame is not selected"
1618 (when (frame-p (current-child))
1619 (setf (frame-data-slot (current-child) :unmanaged-window-action) :show)
1620 (leave-second-mode)))
1622 (defun set-default-hide-unmanaged-window ()
1623 "Set default behaviour to hide or not unmanaged windows when frame is not selected"
1624 (when (frame-p (current-child))
1625 (setf (frame-data-slot (current-child) :unmanaged-window-action) nil)
1626 (leave-second-mode)))
1628 (defun set-globally-hide-unmanaged-window ()
1629 "Hide unmanaged windows by default. This is overriden by functions above"
1630 (setf *hide-unmanaged-window* t)
1631 (leave-second-mode))
1633 (defun set-globally-show-unmanaged-window ()
1634 "Show unmanaged windows by default. This is overriden by functions above"
1635 (setf *hide-unmanaged-window* nil)
1636 (leave-second-mode))
1639 ;;; Speed mouse movement.
1640 (let (minx miny maxx maxy history lx ly)
1641 (labels ((middle (x1 x2)
1642 (round (/ (+ x1 x2) 2)))
1643 (reset-if-moved (x y)
1644 (when (or (/= x (or lx x)) (/= y (or ly y)))
1645 (speed-mouse-reset)))
1646 (add-in-history (x y)
1647 (push (list x y) history)))
1648 (defun speed-mouse-reset ()
1649 "Reset speed mouse coordinates"
1650 (setf minx nil miny nil maxx nil maxy nil history nil lx nil ly nil))
1651 (defun speed-mouse-left ()
1652 "Speed move mouse to left"
1653 (with-x-pointer
1654 (reset-if-moved x y)
1655 (setf maxx x)
1656 (add-in-history x y)
1657 (setf lx (middle (or minx 0) maxx))
1658 (xlib:warp-pointer *root* lx y)))
1659 (defun speed-mouse-right ()
1660 "Speed move mouse to right"
1661 (with-x-pointer
1662 (reset-if-moved x y)
1663 (setf minx x)
1664 (add-in-history x y)
1665 (setf lx (middle minx (or maxx (xlib:screen-width *screen*))))
1666 (xlib:warp-pointer *root* lx y)))
1667 (defun speed-mouse-up ()
1668 "Speed move mouse to up"
1669 (with-x-pointer
1670 (reset-if-moved x y)
1671 (setf maxy y)
1672 (add-in-history x y)
1673 (setf ly (middle (or miny 0) maxy))
1674 (xlib:warp-pointer *root* x ly)))
1675 (defun speed-mouse-down ()
1676 "Speed move mouse to down"
1677 (with-x-pointer
1678 (reset-if-moved x y)
1679 (setf miny y)
1680 (add-in-history x y)
1681 (setf ly (middle miny (or maxy (xlib:screen-height *screen*))))
1682 (xlib:warp-pointer *root* x ly)))
1683 (defun speed-mouse-undo ()
1684 "Undo last speed mouse move"
1685 (when history
1686 (let ((h (pop history)))
1687 (when h
1688 (destructuring-bind (bx by) h
1689 (setf lx bx ly by
1690 minx nil maxx nil
1691 miny nil maxy nil)
1692 (xlib:warp-pointer *root* lx ly))))))
1693 (defun speed-mouse-first-history ()
1694 "Revert to the first speed move mouse"
1695 (when history
1696 (let ((h (first (last history))))
1697 (when h
1698 (setf lx (first h)
1699 ly (second h))
1700 (xlib:warp-pointer *root* lx ly)))))))
1704 ;;; Notify window functions
1705 (let (font
1706 window
1708 width height
1709 text
1710 current-child)
1711 (labels ((text-string (tx)
1712 (typecase tx
1713 (cons (first tx))
1714 (t tx)))
1715 (text-color (tx)
1716 (get-color (typecase tx
1717 (cons (second tx))
1718 (t *notify-window-foreground*)))))
1719 (defun is-notify-window-p (win)
1720 (when (and (xlib:window-p win) (xlib:window-p window))
1721 (xlib:window-equal win window)))
1723 (defun raise-notify-window ()
1724 (raise-window window))
1726 (defun refresh-notify-window ()
1727 (add-timer 0.1 #'refresh-notify-window :refresh-notify-window)
1728 (when (and window gc font)
1729 (raise-window window)
1730 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1731 (loop for tx in text
1732 for i from 1 do
1733 (setf (xlib:gcontext-foreground gc) (text-color tx))
1734 (xlib:draw-glyphs window gc
1735 (truncate (/ (- width (* (xlib:max-char-width font) (length (text-string tx)))) 2))
1736 (* text-height i 2)
1737 (text-string tx))))))
1739 (defun close-notify-window ()
1740 (erase-timer :refresh-notify-window)
1741 (setf *never-managed-window-list*
1742 (remove (list #'is-notify-window-p 'raise-window)
1743 *never-managed-window-list* :test #'equal))
1744 (when gc
1745 (xlib:free-gcontext gc))
1746 (when window
1747 (xlib:destroy-window window))
1748 (when font
1749 (xlib:close-font font))
1750 (xlib:display-finish-output *display*)
1751 (setf window nil
1752 gc nil
1753 font nil))
1755 (defun open-notify-window (text-list)
1756 (close-notify-window)
1757 (setf font (xlib:open-font *display* *notify-window-font-string*))
1758 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1759 (setf text text-list)
1760 (setf width (* (xlib:max-char-width font) (+ (loop for tx in text-list
1761 maximize (length (text-string tx))) 2))
1762 height (+ (* text-height (length text-list) 2) text-height))
1763 (with-placement (*notify-window-placement* x y width height)
1764 (setf window (xlib:create-window :parent *root*
1765 :x x
1766 :y y
1767 :width width
1768 :height height
1769 :background (get-color *notify-window-background*)
1770 :border-width *border-size*
1771 :border (get-color *notify-window-border*)
1772 :colormap (xlib:screen-default-colormap *screen*)
1773 :event-mask '(:exposure :key-press))
1774 gc (xlib:create-gcontext :drawable window
1775 :foreground (get-color *notify-window-foreground*)
1776 :background (get-color *notify-window-background*)
1777 :font font
1778 :line-style :solid))
1779 (setf (window-transparency window) *notify-window-transparency*)
1780 (when (frame-p (current-child))
1781 (setf current-child (current-child)))
1782 (add-in-never-managed-window-list (list 'is-notify-window-p 'raise-window))
1783 (map-window window)
1784 (refresh-notify-window)
1785 (xlib:display-finish-output *display*))))))
1787 (defun notify-message (delay &rest messages)
1788 (erase-timer :close-notify-window)
1789 (funcall #'open-notify-window messages)
1790 (add-timer delay #'close-notify-window :close-notify-window))
1793 (defun display-hello-window ()
1794 (notify-message *notify-window-delay*
1795 '("Welcome to CLFSWM" "yellow")
1796 "Press Alt+F1 for help"))
1799 ;;; Run or raise functions
1800 (defun run-or-raise (raisep run-fn &key (maximized nil))
1801 (let ((window (with-all-windows (*root-frame* win)
1802 (when (funcall raisep win)
1803 (return win)))))
1804 (if window
1805 (let ((parent (find-parent-frame window)))
1806 (setf (current-child) parent)
1807 (put-child-on-top window parent)
1808 (when maximized
1809 (change-root (find-root parent) parent))
1810 (focus-all-children window parent)
1811 (show-all-children t))
1812 (funcall run-fn))))
1814 ;;; Transparency setting
1815 (defun inc-transparency (window root-x root-y)
1816 "Increment the child under mouse transparency"
1817 (declare (ignore root-x root-y))
1818 (unless *in-second-mode* (stop-button-event))
1819 (incf (child-transparency window) 0.1))
1821 (defun dec-transparency (window root-x root-y)
1822 "Decrement the child under mouse transparency"
1823 (declare (ignore root-x root-y))
1824 (unless *in-second-mode* (stop-button-event))
1825 (decf (child-transparency window) 0.1))
1827 (defun inc-transparency-slow (window root-x root-y)
1828 "Increment slowly the child under mouse transparency"
1829 (declare (ignore root-x root-y))
1830 (unless *in-second-mode* (stop-button-event))
1831 (incf (child-transparency window) 0.01))
1833 (defun dec-transparency-slow (window root-x root-y)
1834 "Decrement slowly the child under mouse transparency"
1835 (declare (ignore root-x root-y))
1836 (unless *in-second-mode* (stop-button-event))
1837 (decf (child-transparency window) 0.01))
1840 (defun key-inc-transparency ()
1841 "Increment the current window transparency"
1842 (with-current-window
1843 (incf (child-transparency window) 0.1)))
1845 (defun key-dec-transparency ()
1846 "Decrement the current window transparency"
1847 (with-current-window
1848 (decf (child-transparency window) 0.1)))
1854 ;;; Geometry change functions
1855 (defun swap-frame-geometry ()
1856 "Swap current brother frame geometry"
1857 (when (frame-p (current-child))
1858 (let ((parent (find-parent-frame (current-child))))
1859 (when (frame-p parent)
1860 (let ((brother (second (frame-child parent))))
1861 (when (frame-p brother)
1862 (rotatef (frame-x (current-child)) (frame-x brother))
1863 (rotatef (frame-y (current-child)) (frame-y brother))
1864 (rotatef (frame-w (current-child)) (frame-w brother))
1865 (rotatef (frame-h (current-child)) (frame-h brother))
1866 (show-all-children t)
1867 (leave-second-mode)))))))
1869 (defun rotate-frame-geometry-generic (fun)
1870 "(Rotate brother frame geometry"
1871 (when (frame-p (current-child))
1872 (let ((parent (find-parent-frame (current-child))))
1873 (when (frame-p parent)
1874 (let* ((child-list (funcall fun (frame-child parent)))
1875 (first (first child-list)))
1876 (dolist (child (rest child-list))
1877 (when (and (frame-p first) (frame-p child))
1878 (rotatef (frame-x first) (frame-x child))
1879 (rotatef (frame-y first) (frame-y child))
1880 (rotatef (frame-w first) (frame-w child))
1881 (rotatef (frame-h first) (frame-h child))
1882 (setf first child)))
1883 (show-all-children t))))))
1886 (defun rotate-frame-geometry ()
1887 "Rotate brother frame geometry"
1888 (rotate-frame-geometry-generic #'identity))
1890 (defun anti-rotate-frame-geometry ()
1891 "Anti rotate brother frame geometry"
1892 (rotate-frame-geometry-generic #'reverse))