src/clfswm-second-mode.lisp (sm-leave-function): Do not use *second-mode-program...
[clfswm.git] / src / clfswm-util.lisp
blobc3c60c75ecce40eab7c6fe1ae39fbb71e5392912
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Utility
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2010 Philippe Brochard <hocwp@free.fr>
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 (pathname-directory (concatenate 'string (or (getenv "XDG_CONFIG_HOME")
32 (getenv "HOME"))
33 "/")))
35 (let ((saved-conf-name nil))
36 (defun conf-file-name (&optional alternate-name)
37 (unless (and saved-conf-name (not alternate-name))
38 (let* ((user-conf (probe-file (merge-pathnames (user-homedir-pathname) #p".clfswmrc")))
39 (etc-conf (probe-file #p"/etc/clfswmrc"))
40 (config-user-conf (probe-file (make-pathname :directory (append (xdg-config-home) '("clfswm"))
41 :name "clfswmrc")))
42 (alternate-conf (and alternate-name (probe-file alternate-name))))
43 (setf saved-conf-name (or alternate-conf config-user-conf user-conf etc-conf))))
44 (print saved-conf-name)
45 saved-conf-name))
50 (defun load-contrib (file)
51 "Load a file in the contrib directory"
52 (let ((truename (concatenate 'string *contrib-dir* "contrib/" file)))
53 (format t "Loading contribution file: ~A~%" truename)
54 (when (probe-file truename)
55 (load truename :verbose nil))))
58 (defun reload-clfswm ()
59 "Reload clfswm"
60 (format t "~&-*- Reloading CLFSWM -*-~%")
61 (asdf:oos 'asdf:load-op :clfswm)
62 (reset-clfswm))
67 (defun rename-current-child ()
68 "Rename the current child"
69 (let ((name (query-string (format nil "New child name: (last: ~A)" (child-name *current-child*))
70 (child-name *current-child*))))
71 (rename-child *current-child* name)
72 (leave-second-mode)))
75 (defun renumber-current-frame ()
76 "Renumber the current frame"
77 (when (frame-p *current-child*)
78 (let ((number (query-number (format nil "New child number: (last: ~A)" (frame-number *current-child*))
79 (frame-number *current-child*))))
80 (setf (frame-number *current-child*) number)
81 (leave-second-mode))))
86 (defun add-default-frame ()
87 "Add a default frame in the current frame"
88 (when (frame-p *current-child*)
89 (let ((name (query-string "Frame name")))
90 (push (create-frame :name name) (frame-child *current-child*))))
91 (leave-second-mode))
94 (defun add-placed-frame ()
95 "Add a placed frame in the current frame"
96 (when (frame-p *current-child*)
97 (let ((name (query-string "Frame name"))
98 (x (/ (query-number "Frame x in percent (%)") 100))
99 (y (/ (query-number "Frame y in percent (%)") 100))
100 (w (/ (query-number "Frame width in percent (%)") 100))
101 (h (/ (query-number "Frame height in percent (%)") 100)))
102 (push (create-frame :name name :x x :y y :w w :h h)
103 (frame-child *current-child*))))
104 (leave-second-mode))
108 (defun delete-focus-window-generic (close-fun)
109 (let ((window (xlib:input-focus *display*)))
110 (when (and window (not (xlib:window-equal window *no-focus-window*)))
111 (when (child-equal-p window *current-child*)
112 (setf *current-child* *current-root*))
113 (hide-child window)
114 (delete-child-and-children-in-all-frames window close-fun)
115 (show-all-children))))
117 (defun delete-focus-window ()
118 "Close focus window: Delete the focus window in all frames and workspaces"
119 (delete-focus-window-generic 'delete-window))
121 (defun destroy-focus-window ()
122 "Kill focus window: Destroy the focus window in all frames and workspaces"
123 (delete-focus-window-generic 'destroy-window))
125 (defun remove-focus-window ()
126 "Remove the focus window from the current frame"
127 (let ((window (xlib:input-focus *display*)))
128 (when (and window (not (xlib:window-equal window *no-focus-window*)))
129 (setf *current-child* *current-root*)
130 (hide-child window)
131 (remove-child-in-frame window (find-parent-frame window))
132 (show-all-children))))
135 (defun unhide-all-windows-in-current-child ()
136 "Unhide all hidden windows into the current child"
137 (dolist (window (get-hidden-windows))
138 (unhide-window window)
139 (process-new-window window)
140 (map-window window))
141 (show-all-children))
146 (defun find-window-under-mouse (x y)
147 "Return the child window under the mouse"
148 (let ((win *root*))
149 (with-all-windows-frames-and-parent (*current-root* child parent)
150 (when (and (or (managed-window-p child parent) (child-equal-p parent *current-child*))
151 (<= (xlib:drawable-x child) x (+ (xlib:drawable-x child) (xlib:drawable-width child)))
152 (<= (xlib:drawable-y child) y (+ (xlib:drawable-y child) (xlib:drawable-height child))))
153 (setf win child))
154 (when (and (<= (frame-rx child) x (+ (frame-rx child) (frame-rw child)))
155 (<= (frame-ry child) y (+ (frame-ry child) (frame-rh child))))
156 (setf win (frame-window child))))
157 win))
160 (defun find-child-under-mouse (x y &optional first-foundp)
161 "Return the child under the mouse"
162 (let ((ret nil))
163 (with-all-windows-frames-and-parent (*current-root* child parent)
164 (when (and (not (window-hidden-p child))
165 (or (managed-window-p child parent) (child-equal-p parent *current-child*))
166 (<= (xlib:drawable-x child) x (+ (xlib:drawable-x child) (xlib:drawable-width child)))
167 (<= (xlib:drawable-y child) y (+ (xlib:drawable-y child) (xlib:drawable-height child))))
168 (if first-foundp
169 (return-from find-child-under-mouse child)
170 (setf ret child)))
171 (when (and (<= (frame-rx child) x (+ (frame-rx child) (frame-rw child)))
172 (<= (frame-ry child) y (+ (frame-ry child) (frame-rh child))))
173 (if first-foundp
174 (return-from find-child-under-mouse child)
175 (setf ret child))))
176 ret))
183 ;;; Selection functions
184 (defun clear-selection ()
185 "Clear the current selection"
186 (setf *child-selection* nil)
187 (display-frame-info *current-root*))
189 (defun copy-current-child ()
190 "Copy the current child to the selection"
191 (pushnew *current-child* *child-selection*)
192 (display-frame-info *current-root*))
195 (defun cut-current-child ()
196 "Cut the current child to the selection"
197 (copy-current-child)
198 (hide-all *current-child*)
199 (remove-child-in-frame *current-child* (find-parent-frame *current-child* *current-root*))
200 (setf *current-child* *current-root*)
201 (show-all-children))
203 (defun remove-current-child ()
204 "Remove the current child from its parent frame"
205 (hide-all *current-child*)
206 (remove-child-in-frame *current-child* (find-parent-frame *current-child* *current-root*))
207 (setf *current-child* *current-root*)
208 (leave-second-mode))
210 (defun delete-current-child ()
211 "Delete the current child and its children in all frames"
212 (hide-all *current-child*)
213 (delete-child-and-children-in-all-frames *current-child*)
214 (leave-second-mode))
217 (defun paste-selection-no-clear ()
218 "Paste the selection in the current frame - Do not clear the selection after paste"
219 (let ((frame-dest (typecase *current-child*
220 (xlib:window (find-parent-frame *current-child* *current-root*))
221 (frame *current-child*))))
222 (when frame-dest
223 (dolist (child *child-selection*)
224 (unless (find-child-in-parent child frame-dest)
225 (pushnew child (frame-child frame-dest))))
226 (show-all-children))))
228 (defun paste-selection ()
229 "Paste the selection in the current frame"
230 (paste-selection-no-clear)
231 (setf *child-selection* nil)
232 (display-frame-info *current-root*))
237 ;;; Maximize function
238 (defun frame-toggle-maximize ()
239 "Maximize/Unmaximize the current frame in its parent frame"
240 (when (frame-p *current-child*)
241 (let ((unmaximized-coords (frame-data-slot *current-child* :unmaximized-coords)))
242 (if unmaximized-coords
243 (with-slots (x y w h) *current-child*
244 (destructuring-bind (nx ny nw nh) unmaximized-coords
245 (setf (frame-data-slot *current-child* :unmaximized-coords) nil
246 x nx y ny w nw h nh)))
247 (with-slots (x y w h) *current-child*
248 (setf (frame-data-slot *current-child* :unmaximized-coords)
249 (list x y w h)
250 x 0 y 0 w 1 h 1))))
251 (show-all-children (find-parent-frame *current-child*))
252 (leave-second-mode)))
262 ;;; CONFIG - Identify mode
263 (defun identify-key ()
264 "Identify a key"
265 (let* ((done nil)
266 (font (xlib:open-font *display* *identify-font-string*))
267 (window (xlib:create-window :parent *root*
268 :x 0 :y 0
269 :width (- (xlib:screen-width *screen*) 2)
270 :height (* 5 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font)))
271 :background (get-color *identify-background*)
272 :border-width 1
273 :border (get-color *identify-border*)
274 :colormap (xlib:screen-default-colormap *screen*)
275 :event-mask '(:exposure)))
276 (gc (xlib:create-gcontext :drawable window
277 :foreground (get-color *identify-foreground*)
278 :background (get-color *identify-background*)
279 :font font
280 :line-style :solid)))
281 (labels ((print-doc (msg hash-table-key pos code state)
282 (let ((function (find-key-from-code hash-table-key code state)))
283 (when (and function (fboundp (first function)))
284 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* pos (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
285 (format nil "~A ~A" msg (documentation (first function) 'function))))))
286 (print-key (code state keysym key modifiers)
287 (clear-pixmap-buffer window gc)
288 (setf (xlib:gcontext-foreground gc) (get-color *identify-foreground*))
289 (xlib:draw-glyphs *pixmap-buffer* gc 5 (+ (xlib:max-char-ascent font) 5)
290 (format nil "Press a key to identify. Press 'q' to stop the identify loop."))
291 (when code
292 (xlib:draw-glyphs *pixmap-buffer* gc 10 (+ (* 2 (+ (xlib:max-char-ascent font) (xlib:max-char-descent font))) 5)
293 (format nil "Code=~A KeySym=~S Key=~S Modifiers=~A"
294 code keysym key modifiers))
295 (print-doc "Main mode : " *main-keys* 3 code state)
296 (print-doc "Second mode: " *second-keys* 4 code state))
297 (copy-pixmap-buffer window gc))
298 (handle-identify-key (&rest event-slots &key root code state &allow-other-keys)
299 (declare (ignore event-slots root))
300 (let* ((modifiers (state->modifiers state))
301 (key (keycode->char code state))
302 (keysym (keysym->keysym-name (keycode->keysym code modifiers))))
303 (setf done (and (equal key #\q) (equal modifiers *default-modifiers*)))
304 (dbg code keysym key modifiers)
305 (print-key code state keysym key modifiers)
306 (force-output)))
307 (handle-identify (&rest event-slots &key display event-key &allow-other-keys)
308 (declare (ignore display))
309 (case event-key
310 (:key-press (apply #'handle-identify-key event-slots) t)
311 (:exposure (print-key nil nil nil nil nil)))
313 (xgrab-pointer *root* 92 93)
314 (map-window window)
315 (format t "~&Press 'q' to stop the identify loop~%")
316 (print-key nil nil nil nil nil)
317 (force-output)
318 (unwind-protect
319 (loop until done do
320 (when (xlib:event-listen *display* *loop-timeout*)
321 (xlib:process-event *display* :handler #'handle-identify))
322 (xlib:display-finish-output *display*))
323 (xlib:destroy-window window)
324 (xlib:close-font font)
325 (xgrab-pointer *root* 66 67)))))
332 (defun eval-from-query-string ()
333 "Eval a lisp form from the query input"
334 (let ((form (query-string (format nil "Eval Lisp - ~A" (package-name *package*))))
335 (result nil))
336 (when (and form (not (equal form "")))
337 (let ((printed-result
338 (with-output-to-string (*standard-output*)
339 (setf result (handler-case
340 (loop for i in (multiple-value-list
341 (eval (read-from-string form)))
342 collect (format nil "~S" i))
343 (error (condition)
344 (format nil "~A" condition)))))))
345 (info-mode (expand-newline (append (ensure-list (format nil "> ~A" form))
346 (ensure-list printed-result)
347 (ensure-list result)))
348 :width (- (xlib:screen-width *screen*) 2))
349 (eval-from-query-string)))))
354 (defun run-program-from-query-string ()
355 "Run a program from the query input"
356 (multiple-value-bind (program return)
357 (query-string "Run:")
358 (when (and (equal return :return) program (not (equal program "")))
359 (setf *second-mode-leave-function* (let ((cmd (concatenate 'string "cd $HOME && " program)))
360 (lambda ()
361 (do-shell cmd))))
362 (leave-second-mode))))
367 ;;; Frame name actions
368 (defun ask-frame-name (msg)
369 "Ask a frame name"
370 (let ((all-frame-name nil))
371 (with-all-frames (*root-frame* frame)
372 (awhen (frame-name frame) (push it all-frame-name)))
373 (query-string msg "" all-frame-name)))
376 ;;; Focus by functions
377 (defun focus-frame-by (frame)
378 (when (frame-p frame)
379 (hide-all *current-root*)
380 (focus-all-children frame (or (find-parent-frame frame *current-root*)
381 (find-parent-frame frame)
382 *root-frame*))
383 (show-all-children *current-root*)))
386 (defun focus-frame-by-name ()
387 "Focus a frame by name"
388 (focus-frame-by (find-frame-by-name (ask-frame-name "Focus frame:")))
389 (leave-second-mode))
391 (defun focus-frame-by-number ()
392 "Focus a frame by number"
393 (focus-frame-by (find-frame-by-number (query-number "Focus frame by number:")))
394 (leave-second-mode))
397 ;;; Open by functions
398 (defun open-frame-by (frame)
399 (when (frame-p frame)
400 (push (create-frame :name (query-string "Frame name")) (frame-child frame))
401 (show-all-children *current-root*)))
405 (defun open-frame-by-name ()
406 "Open a new frame in a named frame"
407 (open-frame-by (find-frame-by-name (ask-frame-name "Open a new frame in: ")))
408 (leave-second-mode))
410 (defun open-frame-by-number ()
411 "Open a new frame in a numbered frame"
412 (open-frame-by (find-frame-by-number (query-number "Open a new frame in the group numbered:")))
413 (leave-second-mode))
416 ;;; Delete by functions
417 (defun delete-frame-by (frame)
418 (hide-all *current-root*)
419 (unless (child-equal-p frame *root-frame*)
420 (when (child-equal-p frame *current-root*)
421 (setf *current-root* *root-frame*))
422 (when (child-equal-p frame *current-child*)
423 (setf *current-child* *current-root*))
424 (remove-child-in-frame frame (find-parent-frame frame)))
425 (show-all-children *current-root*))
428 (defun delete-frame-by-name ()
429 "Delete a frame by name"
430 (delete-frame-by (find-frame-by-name (ask-frame-name "Delete frame: ")))
431 (leave-second-mode))
433 (defun delete-frame-by-number ()
434 "Delete a frame by number"
435 (delete-frame-by (find-frame-by-number (query-number "Delete frame by number:")))
436 (leave-second-mode))
439 ;;; Move by function
440 (defun move-child-to (child frame-dest)
441 (when (and child (frame-p frame-dest))
442 (hide-all *current-root*)
443 (remove-child-in-frame child (find-parent-frame child))
444 (pushnew child (frame-child frame-dest))
445 (focus-all-children child frame-dest)
446 (show-all-children *current-root*)))
448 (defun move-current-child-by-name ()
449 "Move current child in a named frame"
450 (move-child-to *current-child*
451 (find-frame-by-name
452 (ask-frame-name (format nil "Move '~A' to frame: " (child-name *current-child*)))))
453 (leave-second-mode))
455 (defun move-current-child-by-number ()
456 "Move current child in a numbered frame"
457 (move-child-to *current-child*
458 (find-frame-by-number
459 (query-number (format nil "Move '~A' to frame numbered:" (child-name *current-child*)))))
460 (leave-second-mode))
463 ;;; Copy by function
464 (defun copy-child-to (child frame-dest)
465 (when (and child (frame-p frame-dest))
466 (hide-all *current-root*)
467 (pushnew child (frame-child frame-dest))
468 (focus-all-children child frame-dest)
469 (show-all-children *current-root*)))
471 (defun copy-current-child-by-name ()
472 "Copy current child in a named frame"
473 (copy-child-to *current-child*
474 (find-frame-by-name
475 (ask-frame-name (format nil "Copy '~A' to frame: " (child-name *current-child*)))))
476 (leave-second-mode))
478 (defun copy-current-child-by-number ()
479 "Copy current child in a numbered frame"
480 (copy-child-to *current-child*
481 (find-frame-by-number
482 (query-number (format nil "Copy '~A' to frame numbered:" (child-name *current-child*)))))
483 (leave-second-mode))
488 ;;; Show frame info
489 (defun show-all-frames-info ()
490 "Show all frames info windows"
491 (let ((*show-root-frame-p* t))
492 (show-all-children)
493 (with-all-frames (*current-root* frame)
494 (raise-window (frame-window frame))
495 (display-frame-info frame))))
497 (defun hide-all-frames-info ()
498 "Hide all frames info windows"
499 (with-all-windows (*current-root* window)
500 (raise-window window))
501 (hide-child *current-root*)
502 (show-all-children))
504 (defun show-all-frames-info-key ()
505 "Show all frames info windows until a key is release"
506 (show-all-frames-info)
507 (wait-no-key-or-button-press)
508 (hide-all-frames-info))
515 (defun move-frame (frame parent orig-x orig-y)
516 (when (and frame parent)
517 (hide-all-children frame)
518 (with-slots (window) frame
519 (move-window window orig-x orig-y #'display-frame-info (list frame))
520 (setf (frame-x frame) (x-px->fl (xlib:drawable-x window) parent)
521 (frame-y frame) (y-px->fl (xlib:drawable-y window) parent)))
522 (show-all-children frame)))
525 (defun resize-frame (frame parent orig-x orig-y)
526 (when (and frame parent)
527 (hide-all-children frame)
528 (with-slots (window) frame
529 (resize-window window orig-x orig-y #'display-frame-info (list frame))
530 (setf (frame-w frame) (w-px->fl (xlib:drawable-width window) parent)
531 (frame-h frame) (h-px->fl (xlib:drawable-height window) parent)))
532 (show-all-children frame)))
536 (defun mouse-click-to-focus-generic (window root-x root-y mouse-fn)
537 "Focus the current frame or focus the current window parent
538 mouse-fun is #'move-frame or #'resize-frame"
539 (let* ((to-replay t)
540 (child (find-child-under-mouse root-x root-y))
541 (parent (find-parent-frame child))
542 (root-p (or (child-equal-p window *root*)
543 (and (frame-p *current-root*)
544 (child-equal-p child (frame-window *current-root*))))))
545 (labels ((add-new-frame ()
546 (setf child (create-frame)
547 parent *current-root*
548 mouse-fn #'resize-frame)
549 (place-frame child parent root-x root-y 10 10)
550 (map-window (frame-window child))
551 (pushnew child (frame-child *current-root*))))
552 (when (or (not root-p) *create-frame-on-root*)
553 (unless parent
554 (if root-p
555 (add-new-frame)
556 (progn
557 (unless (equal (type-of child) 'frame)
558 (setf child (find-frame-window child *current-root*)))
559 (setf parent (find-parent-frame child)))))
560 (when (equal (type-of child) 'frame)
561 (funcall mouse-fn child parent root-x root-y))
562 (when (and child parent (focus-all-children child parent
563 (not (and (child-equal-p *current-child* *current-root*)
564 (xlib:window-p *current-root*)))))
565 (when (show-all-children *current-root*)
566 (setf to-replay nil))))
567 (if to-replay
568 (replay-button-event)
569 (stop-button-event)))))
572 (defun mouse-click-to-focus-and-move (window root-x root-y)
573 "Move and focus the current frame or focus the current window parent.
574 Or do actions on corners"
575 (or (do-corner-action root-x root-y *corner-main-mode-left-button*)
576 (mouse-click-to-focus-generic window root-x root-y #'move-frame)))
578 (defun mouse-click-to-focus-and-resize (window root-x root-y)
579 "Resize and focus the current frame or focus the current window parent.
580 Or do actions on corners"
581 (or (do-corner-action root-x root-y *corner-main-mode-right-button*)
582 (mouse-click-to-focus-generic window root-x root-y #'resize-frame)))
584 (defun mouse-middle-click (window root-x root-y)
585 "Do actions on corners"
586 (declare (ignore window))
587 (or (do-corner-action root-x root-y *corner-main-mode-middle-button*)
588 (replay-button-event)))
593 (defun mouse-focus-move/resize-generic (root-x root-y mouse-fn window-parent)
594 "Focus the current frame or focus the current window parent
595 mouse-fun is #'move-frame or #'resize-frame.
596 Focus child and its parents -
597 For window: set current child to window or its parent according to window-parent"
598 (let* ((child (find-child-under-mouse root-x root-y))
599 (parent (find-parent-frame child)))
600 (when (and (child-equal-p child *current-root*)
601 (frame-p *current-root*))
602 (setf child (create-frame)
603 parent *current-root*
604 mouse-fn #'resize-frame)
605 (place-frame child parent root-x root-y 10 10)
606 (map-window (frame-window child))
607 (pushnew child (frame-child *current-root*)))
608 (typecase child
609 (xlib:window
610 (if (managed-window-p child parent)
611 (funcall mouse-fn parent (find-parent-frame parent) root-x root-y)
612 (funcall(cond ((eql mouse-fn #'move-frame) #'move-window)
613 ((eql mouse-fn #'resize-frame) #'resize-window))
614 child root-x root-y)))
615 (frame (funcall mouse-fn child parent root-x root-y)))
616 (focus-all-children child parent window-parent)
617 (show-all-children *current-root*)))
622 (defun test-mouse-binding (window root-x root-y)
623 (dbg window root-x root-y)
624 (replay-button-event))
628 (defun mouse-select-next-level (window root-x root-y)
629 "Select the next level in frame"
630 (declare (ignore root-x root-y))
631 (let ((frame (find-frame-window window)))
632 (when (or frame (xlib:window-equal window *root*))
633 (select-next-level))
634 (replay-button-event)))
638 (defun mouse-select-previous-level (window root-x root-y)
639 "Select the previous level in frame"
640 (declare (ignore root-x root-y))
641 (let ((frame (find-frame-window window)))
642 (when (or frame (xlib:window-equal window *root*))
643 (select-previous-level))
644 (replay-button-event)))
648 (defun mouse-enter-frame (window root-x root-y)
649 "Enter in the selected frame - ie make it the root frame"
650 (declare (ignore root-x root-y))
651 (let ((frame (find-frame-window window)))
652 (when (or frame (xlib:window-equal window *root*))
653 (enter-frame))
654 (replay-button-event)))
658 (defun mouse-leave-frame (window root-x root-y)
659 "Leave the selected frame - ie make its parent the root frame"
660 (declare (ignore root-x root-y))
661 (let ((frame (find-frame-window window)))
662 (when (or frame (xlib:window-equal window *root*))
663 (leave-frame))
664 (replay-button-event)))
668 ;;;;;,-----
669 ;;;;;| Various definitions
670 ;;;;;`-----
672 (defun show-help (&optional (browser "dillo") (tempfile "/tmp/clfswm.html"))
673 "Show current keys and buttons bindings"
674 (ignore-errors
675 (produce-doc-html-in-file tempfile))
676 (sleep 1)
677 (do-shell (format nil "~A ~A" browser tempfile)))
681 ;;; Bind or jump functions
682 (let ((key-slots (make-array 10 :initial-element nil))
683 (current-slot 1))
684 (defun bind-on-slot (&optional (slot current-slot))
685 "Bind current child to slot"
686 (setf (aref key-slots slot) *current-child*))
688 (defun remove-binding-on-slot ()
689 "Remove binding on slot"
690 (setf (aref key-slots current-slot) nil))
692 (defun jump-to-slot ()
693 "Jump to slot"
694 (let ((jump-child (aref key-slots current-slot)))
695 (when (find-child jump-child *root-frame*)
696 (hide-all *current-root*)
697 (setf *current-root* jump-child
698 *current-child* *current-root*)
699 (focus-all-children *current-child* *current-child*)
700 (show-all-children *current-root*))))
702 (defun bind-or-jump (n)
703 "Bind or jump to a slot (a frame or a window)"
704 (setf current-slot (- n 1))
705 (let ((default-bind `("b" bind-on-slot
706 ,(format nil "Bind slot ~A on child: ~A" n (child-fullname *current-child*)))))
707 (info-mode-menu (aif (aref key-slots current-slot)
708 `(,default-bind
709 ("BackSpace" remove-binding-on-slot
710 ,(format nil "Remove slot ~A binding on child: ~A" n (child-fullname *current-child*)))
711 (" - " nil " -")
712 ("Tab" jump-to-slot
713 ,(format nil "Jump to child: ~A" (aif (aref key-slots current-slot)
714 (child-fullname it)
715 "Not set - Please, bind it with 'b'")))
716 ("Return" jump-to-slot "Same thing")
717 ("space" jump-to-slot "Same thing"))
718 (list default-bind))))))
722 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
723 ;;; Useful function for the second mode ;;;
724 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
725 (defmacro with-movement (&body body)
726 `(when (frame-p *current-child*)
727 ,@body
728 (show-all-children)
729 (display-all-frame-info)
730 (draw-second-mode-window)
731 (open-menu (find-menu 'frame-movement-menu))))
734 ;;; Pack
735 (defun current-frame-pack-up ()
736 "Pack the current frame up"
737 (with-movement (pack-frame-up *current-child* (find-parent-frame *current-child* *current-root*))))
739 (defun current-frame-pack-down ()
740 "Pack the current frame down"
741 (with-movement (pack-frame-down *current-child* (find-parent-frame *current-child* *current-root*))))
743 (defun current-frame-pack-left ()
744 "Pack the current frame left"
745 (with-movement (pack-frame-left *current-child* (find-parent-frame *current-child* *current-root*))))
747 (defun current-frame-pack-right ()
748 "Pack the current frame right"
749 (with-movement (pack-frame-right *current-child* (find-parent-frame *current-child* *current-root*))))
751 ;;; Center
752 (defun center-current-frame ()
753 "Center the current frame"
754 (with-movement (center-frame *current-child*)))
756 ;;; Fill
757 (defun current-frame-fill-up ()
758 "Fill the current frame up"
759 (with-movement (fill-frame-up *current-child* (find-parent-frame *current-child* *current-root*))))
761 (defun current-frame-fill-down ()
762 "Fill the current frame down"
763 (with-movement (fill-frame-down *current-child* (find-parent-frame *current-child* *current-root*))))
765 (defun current-frame-fill-left ()
766 "Fill the current frame left"
767 (with-movement (fill-frame-left *current-child* (find-parent-frame *current-child* *current-root*))))
769 (defun current-frame-fill-right ()
770 "Fill the current frame right"
771 (with-movement (fill-frame-right *current-child* (find-parent-frame *current-child* *current-root*))))
773 (defun current-frame-fill-all-dir ()
774 "Fill the current frame in all directions"
775 (with-movement
776 (let ((parent (find-parent-frame *current-child* *current-root*)))
777 (fill-frame-up *current-child* parent)
778 (fill-frame-down *current-child* parent)
779 (fill-frame-left *current-child* parent)
780 (fill-frame-right *current-child* parent))))
782 (defun current-frame-fill-vertical ()
783 "Fill the current frame vertically"
784 (with-movement
785 (let ((parent (find-parent-frame *current-child* *current-root*)))
786 (fill-frame-up *current-child* parent)
787 (fill-frame-down *current-child* parent))))
789 (defun current-frame-fill-horizontal ()
790 "Fill the current frame horizontally"
791 (with-movement
792 (let ((parent (find-parent-frame *current-child* *current-root*)))
793 (fill-frame-left *current-child* parent)
794 (fill-frame-right *current-child* parent))))
797 ;;; Resize
798 (defun current-frame-resize-up ()
799 "Resize the current frame up to its half height"
800 (with-movement (resize-half-height-up *current-child*)))
802 (defun current-frame-resize-down ()
803 "Resize the current frame down to its half height"
804 (with-movement (resize-half-height-down *current-child*)))
806 (defun current-frame-resize-left ()
807 "Resize the current frame left to its half width"
808 (with-movement (resize-half-width-left *current-child*)))
810 (defun current-frame-resize-right ()
811 "Resize the current frame right to its half width"
812 (with-movement (resize-half-width-right *current-child*)))
814 (defun current-frame-resize-all-dir ()
815 "Resize down the current frame"
816 (with-movement (resize-frame-down *current-child*)))
818 (defun current-frame-resize-all-dir-minimal ()
819 "Resize down the current frame to its minimal size"
820 (with-movement (resize-minimal-frame *current-child*)))
823 ;;; Children navigation
824 (defun with-movement-select-next-brother ()
825 "Select the next brother frame"
826 (with-movement (select-next-brother)))
828 (defun with-movement-select-previous-brother ()
829 "Select the previous brother frame"
830 (with-movement (select-previous-brother)))
832 (defun with-movement-select-next-level ()
833 "Select the next level"
834 (with-movement (select-next-level)))
836 (defun with-movement-select-previous-level ()
837 "Select the previous levelframe"
838 (with-movement (select-previous-level)))
840 (defun with-movement-select-next-child ()
841 "Select the next child"
842 (with-movement (select-next-child)))
846 ;;; Adapt frame functions
847 (defun adapt-current-frame-to-window-hints-generic (width-p height-p)
848 "Adapt the current frame to the current window minimal size hints"
849 (when (frame-p *current-child*)
850 (let ((window (first (frame-child *current-child*))))
851 (when (xlib:window-p window)
852 (let* ((hints (xlib:wm-normal-hints window))
853 (min-width (and hints (xlib:wm-size-hints-min-width hints)))
854 (min-height (and hints (xlib:wm-size-hints-min-height hints))))
855 (when (and width-p min-width)
856 (setf (frame-rw *current-child*) min-width))
857 (when (and height-p min-height)
858 (setf (frame-rh *current-child*) min-height))
859 (fixe-real-size *current-child* (find-parent-frame *current-child*))
860 (leave-second-mode))))))
862 (defun adapt-current-frame-to-window-hints ()
863 "Adapt the current frame to the current window minimal size hints"
864 (adapt-current-frame-to-window-hints-generic t t))
866 (defun adapt-current-frame-to-window-width-hint ()
867 "Adapt the current frame to the current window minimal width hint"
868 (adapt-current-frame-to-window-hints-generic t nil))
870 (defun adapt-current-frame-to-window-height-hint ()
871 "Adapt the current frame to the current window minimal height hint"
872 (adapt-current-frame-to-window-hints-generic nil t))
877 ;;; Managed window type functions
878 (defun current-frame-manage-window-type-generic (type-list)
879 (when (frame-p *current-child*)
880 (setf (frame-managed-type *current-child*) type-list
881 (frame-forced-managed-window *current-child*) nil
882 (frame-forced-unmanaged-window *current-child*) nil))
883 (leave-second-mode))
886 (defun current-frame-manage-window-type ()
887 "Change window types to be managed by a frame"
888 (when (frame-p *current-child*)
889 (let* ((type-str (query-string "Managed window type: (all, normal, transient, maxsize, desktop, dock, toolbar, menu, utility, splash, dialog)"
890 (format nil "~{~:(~A~) ~}" (frame-managed-type *current-child*))))
891 (type-list (loop :for type :in (split-string type-str)
892 :collect (intern (string-upcase type) :keyword))))
893 (current-frame-manage-window-type-generic type-list))))
896 (defun current-frame-manage-all-window-type ()
897 "Manage all window type"
898 (current-frame-manage-window-type-generic '(:all)))
900 (defun current-frame-manage-only-normal-window-type ()
901 "Manage only normal window type"
902 (current-frame-manage-window-type-generic '(:normal)))
904 (defun current-frame-manage-no-window-type ()
905 "Do not manage any window type"
906 (current-frame-manage-window-type-generic nil))
915 ;;; Force window functions
916 (defun force-window-in-frame ()
917 "Force the current window to move in the frame (Useful only for unmanaged windows)"
918 (with-current-window
919 (let ((parent (find-parent-frame window)))
920 (setf (xlib:drawable-x window) (frame-rx parent)
921 (xlib:drawable-y window) (frame-ry parent))
922 (xlib:display-finish-output *display*)))
923 (leave-second-mode))
926 (defun force-window-center-in-frame ()
927 "Force the current window to move in the center of the frame (Useful only for unmanaged windows)"
928 (with-current-window
929 (let ((parent (find-parent-frame window)))
930 (setf (xlib:drawable-x window) (truncate (+ (frame-rx parent)
931 (/ (- (frame-rw parent)
932 (xlib:drawable-width window)) 2)))
933 (xlib:drawable-y window) (truncate (+ (frame-ry parent)
934 (/ (- (frame-rh parent)
935 (xlib:drawable-height window)) 2))))
936 (xlib:display-finish-output *display*)))
937 (leave-second-mode))
941 (defun display-current-window-info ()
942 "Display information on the current window"
943 (with-current-window
944 (info-mode (list (format nil "Window: ~A" window)
945 (format nil "Window name: ~A" (xlib:wm-name window))
946 (format nil "Window class: ~A" (xlib:get-wm-class window))
947 (format nil "Window type: ~:(~A~)" (window-type window))
948 (format nil "Window id: 0x~X" (xlib:window-id window)))))
949 (leave-second-mode))
952 (defun manage-current-window ()
953 "Force to manage the current window by its parent frame"
954 (with-current-window
955 (let ((parent (find-parent-frame window)))
956 (with-slots ((managed forced-managed-window)
957 (unmanaged forced-unmanaged-window)) parent
958 (setf unmanaged (child-remove window unmanaged)
959 unmanaged (remove (xlib:wm-name window) unmanaged :test #'string-equal-p))
960 (pushnew window managed))))
961 (leave-second-mode))
963 (defun unmanage-current-window ()
964 "Force to not manage the current window by its parent frame"
965 (with-current-window
966 (let ((parent (find-parent-frame window)))
967 (with-slots ((managed forced-managed-window)
968 (unmanaged forced-unmanaged-window)) parent
969 (setf managed (child-remove window managed)
970 managed (remove (xlib:wm-name window) managed :test #'string-equal-p))
971 (pushnew window unmanaged))))
972 (leave-second-mode))
976 ;;; Moving child with the mouse button
977 (defun mouse-move-child-over-frame (window root-x root-y)
978 "Move the child under the mouse cursor to another frame"
979 (declare (ignore window))
980 (let ((child (find-child-under-mouse root-x root-y)))
981 (unless (child-equal-p child *current-root*)
982 (hide-all child)
983 (remove-child-in-frame child (find-parent-frame child))
984 (wait-mouse-button-release 50 51)
985 (multiple-value-bind (x y)
986 (xlib:query-pointer *root*)
987 (let ((dest (find-child-under-mouse x y)))
988 (when (xlib:window-p dest)
989 (setf dest (find-parent-frame dest)))
990 (unless (child-equal-p child dest)
991 (move-child-to child dest)
992 (show-all-children *current-root*))))))
993 (stop-button-event))
998 ;;; Hide/Show frame window functions
999 (defun hide/show-frame-window (frame value)
1000 "Hide/show the frame window"
1001 (when (frame-p frame)
1002 (setf (frame-show-window-p *current-child*) value)
1003 (show-all-children *current-root*))
1004 (leave-second-mode))
1007 (defun hide-current-frame-window ()
1008 "Hide the current frame window"
1009 (hide/show-frame-window *current-child* nil))
1011 (defun show-current-frame-window ()
1012 "Show the current frame window"
1013 (hide/show-frame-window *current-child* t))
1017 ;;; Hide/Unhide current child
1018 (defun hide-current-child ()
1019 "Hide the current child"
1020 (unless (child-equal-p *current-child* *current-root*)
1021 (let ((parent (find-parent-frame *current-child*)))
1022 (when (frame-p parent)
1023 (with-slots (child hidden-children) parent
1024 (hide-all *current-child*)
1025 (setf child (child-remove *current-child* child))
1026 (pushnew *current-child* hidden-children)
1027 (setf *current-child* parent))
1028 (show-all-children)))
1029 (leave-second-mode)))
1032 (defun frame-unhide-child (hidden frame-src frame-dest)
1033 "Unhide a hidden child from frame-src in frame-dest"
1034 (with-slots (hidden-children) frame-src
1035 (setf hidden-children (child-remove hidden hidden-children)))
1036 (with-slots (child) frame-dest
1037 (pushnew hidden child)))
1041 (defun unhide-a-child ()
1042 "Unhide a child in the current frame"
1043 (when (frame-p *current-child*)
1044 (with-slots (child hidden-children) *current-child*
1045 (info-mode-menu (loop :for i :from 0
1046 :for hidden :in hidden-children
1047 :collect (list (code-char (+ (char-code #\a) i))
1048 (let ((lhd hidden))
1049 (lambda ()
1050 (frame-unhide-child lhd *current-child* *current-child*)))
1051 (format nil "Unhide ~A" (child-fullname hidden))))))
1052 (show-all-children))
1053 (leave-second-mode))
1056 (defun unhide-all-children ()
1057 "Unhide all current frame hidden children"
1058 (when (frame-p *current-child*)
1059 (with-slots (child hidden-children) *current-child*
1060 (dolist (c hidden-children)
1061 (pushnew c child))
1062 (setf hidden-children nil))
1063 (show-all-children))
1064 (leave-second-mode))
1067 (defun unhide-a-child-from-all-frames ()
1068 "Unhide a child from all frames in the current frame"
1069 (when (frame-p *current-child*)
1070 (let ((acc nil)
1071 (keynum -1))
1072 (with-all-frames (*root-frame* frame)
1073 (when (frame-hidden-children frame)
1074 (push (format nil "~A" (child-fullname frame)) acc)
1075 (dolist (hidden (frame-hidden-children frame))
1076 (push (list (code-char (+ (char-code #\a) (incf keynum)))
1077 (let ((lhd hidden))
1078 (lambda ()
1079 (frame-unhide-child lhd frame *current-child*)))
1080 (format nil "Unhide ~A" (child-fullname hidden)))
1081 acc))))
1082 (info-mode-menu (nreverse acc)))
1083 (show-all-children))
1084 (leave-second-mode))
1090 (let ((last-child nil))
1091 (defun init-last-child ()
1092 (setf last-child nil))
1093 (defun switch-to-last-child ()
1094 "Store the current child and switch to the previous one"
1095 (let ((current-child *current-child*))
1096 (when last-child
1097 (hide-all *current-root*)
1098 (setf *current-root* last-child
1099 *current-child* *current-root*)
1100 (focus-all-children *current-child* *current-child*)
1101 (show-all-children *current-root*))
1102 (setf last-child current-child))))
1110 ;;; Focus policy functions
1111 (defun set-focus-policy-generic (focus-policy)
1112 (when (frame-p *current-child*)
1113 (setf (frame-focus-policy *current-child*) focus-policy))
1114 (leave-second-mode))
1117 (defun current-frame-set-click-focus-policy ()
1118 "Set a click focus policy for the current frame."
1119 (set-focus-policy-generic :click))
1121 (defun current-frame-set-sloppy-focus-policy ()
1122 "Set a sloppy focus policy for the current frame."
1123 (set-focus-policy-generic :sloppy))
1125 (defun current-frame-set-sloppy-strict-focus-policy ()
1126 "Set a (strict) sloppy focus policy only for windows in the current frame."
1127 (set-focus-policy-generic :sloppy-strict))
1129 (defun current-frame-set-sloppy-select-policy ()
1130 "Set a sloppy select policy for the current frame."
1131 (set-focus-policy-generic :sloppy-select))
1135 (defun set-focus-policy-generic-for-all (focus-policy)
1136 (with-all-frames (*root-frame* frame)
1137 (setf (frame-focus-policy frame) focus-policy))
1138 (leave-second-mode))
1141 (defun all-frames-set-click-focus-policy ()
1142 "Set a click focus policy for all frames."
1143 (set-focus-policy-generic-for-all :click))
1145 (defun all-frames-set-sloppy-focus-policy ()
1146 "Set a sloppy focus policy for all frames."
1147 (set-focus-policy-generic-for-all :sloppy))
1149 (defun all-frames-set-sloppy-strict-focus-policy ()
1150 "Set a (strict) sloppy focus policy for all frames."
1151 (set-focus-policy-generic-for-all :sloppy-strict))
1153 (defun all-frames-set-sloppy-select-policy ()
1154 "Set a sloppy select policy for all frames."
1155 (set-focus-policy-generic-for-all :sloppy-select))
1159 ;;; Ensure unique name/number functions
1160 (defun extract-number-from-name (name)
1161 (when (stringp name)
1162 (let* ((pos (1+ (or (position #\. name :from-end t) -1)))
1163 (number (parse-integer name :junk-allowed t :start pos)))
1164 (values number
1165 (if number (subseq name 0 (1- pos)) name)))))
1170 (defun ensure-unique-name ()
1171 "Ensure that all children names are unique"
1172 (with-all-children (*root-frame* child)
1173 (multiple-value-bind (num1 name1)
1174 (extract-number-from-name (child-name child))
1175 (declare (ignore num1))
1176 (when name1
1177 (let ((acc nil))
1178 (with-all-children (*root-frame* c)
1179 (unless (child-equal-p child c))
1180 (multiple-value-bind (num2 name2)
1181 (extract-number-from-name (child-name c))
1182 (when (string-equal name1 name2)
1183 (push num2 acc))))
1184 (dbg acc)
1185 (when (> (length acc) 1)
1186 (setf (child-name child)
1187 (format nil "~A.~A" name1
1188 (1+ (find-free-number (loop for i in acc when i collect (1- i)))))))))))
1189 (leave-second-mode))
1191 (defun ensure-unique-number ()
1192 "Ensure that all children numbers are unique"
1193 (let ((num -1))
1194 (with-all-frames (*root-frame* frame)
1195 (setf (frame-number frame) (incf num))))
1196 (leave-second-mode))
1200 ;;; Standard menu functions - Based on the XDG specifications
1201 (defparameter *xdg-section-list* (append '(TextEditor FileManager WebBrowser)
1202 '(AudioVideo Audio Video Development Education Game Graphics Network Office Settings System Utility)
1203 '(TerminalEmulator Archlinux Screensaver))
1204 "Config(Menu group): Standard menu sections")
1207 (defun um-create-xdg-section-list (menu)
1208 (dolist (section *xdg-section-list*)
1209 (add-sub-menu menu :next section (format nil "~A" section) menu)))
1211 (defun um-find-submenu (menu section-list)
1212 (let ((acc nil))
1213 (dolist (section section-list)
1214 (awhen (find-toplevel-menu (intern (string-upcase section) :clfswm) menu)
1215 (push it acc)))
1216 (if acc
1218 (list (find-toplevel-menu 'Utility menu)))))
1221 (defun um-extract-value (line)
1222 (second (split-string line #\=)))
1225 (defun um-add-desktop (desktop menu)
1226 (let (name exec categories comment)
1227 (when (probe-file desktop)
1228 (with-open-file (stream desktop :direction :input)
1229 (loop for line = (read-line stream nil nil)
1230 while line
1232 (cond ((first-position "Name=" line) (setf name (um-extract-value line)))
1233 ((first-position "Exec=" line) (setf exec (um-extract-value line)))
1234 ((first-position "Categories=" line) (setf categories (um-extract-value line)))
1235 ((first-position "Comment=" line) (setf comment (um-extract-value line))))
1236 (when (and name exec categories)
1237 (let* ((sub-menu (um-find-submenu menu (split-string categories #\;)))
1238 (fun-name (intern name :clfswm)))
1239 (setf (symbol-function fun-name) (let ((do-exec exec))
1240 (lambda ()
1241 (do-shell do-exec)
1242 (leave-second-mode)))
1243 (documentation fun-name 'function) (format nil "~A~A" name (if comment
1244 (format nil " - ~A" comment)
1245 "")))
1246 (dolist (m sub-menu)
1247 (add-menu-key (menu-name m) :next fun-name m)))
1248 (setf name nil exec nil categories nil comment nil)))))))
1251 (defun update-menus (&optional (menu (make-menu :name 'main :doc "Main menu")))
1252 (um-create-xdg-section-list menu)
1253 (let ((count 0)
1254 (found (make-hash-table :test #'equal)))
1255 (dolist (dir (remove-duplicates
1256 (split-string (getenv "XDG_DATA_DIRS") #\:) :test #'string-equal))
1257 (dolist (desktop (directory (concatenate 'string dir "/applications/**/*.desktop")))
1258 (unless (gethash (file-namestring desktop) found)
1259 (setf (gethash (file-namestring desktop) found) t)
1260 (um-add-desktop desktop menu)
1261 (incf count))))
1262 menu))
1266 ;;; Close/Kill focused window
1268 (defun ask-close/kill-current-window ()
1269 "Close or kill the current window (ask before doing anything)"
1270 (let ((window (xlib:input-focus *display*)))
1271 (info-mode-menu
1272 (if (and window (not (xlib:window-equal window *no-focus-window*)))
1273 `(,(format nil "Focus window: ~A" (xlib:wm-name window))
1274 (#\c delete-focus-window "Close the focus window")
1275 (#\k destroy-focus-window "Kill the focus window")
1276 (#\r remove-focus-window)
1277 (#\u unhide-all-windows-in-current-child))
1278 `(,(format nil "Focus window: None")
1279 (#\u unhide-all-windows-in-current-child))))))
1283 ;;; Other window manager functions
1284 (defun get-proc-list ()
1285 (let ((proc (do-shell "ps x -o pid=" nil t))
1286 (proc-list nil))
1287 (loop for line = (read-line proc nil nil)
1288 while line
1289 do (push line proc-list))
1290 (dbg proc-list)
1291 proc-list))
1293 (defun run-other-window-manager ()
1294 (let ((proc-start (get-proc-list)))
1295 (do-shell *other-window-manager* nil t :terminal)
1296 (let* ((proc-end (get-proc-list))
1297 (proc-diff (set-difference proc-end proc-start :test #'equal)))
1298 (dbg 'killing-sigterm proc-diff)
1299 (do-shell (format nil "kill ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1300 (dbg 'killing-sigkill proc-diff)
1301 (do-shell (format nil "kill -9 ~{ ~A ~} 2> /dev/null" proc-diff) nil t :terminal)
1302 (sleep 1))
1303 (setf *other-window-manager* nil)))
1306 (defun do-run-other-window-manager (window-manager)
1307 (setf *other-window-manager* window-manager)
1308 (throw 'exit-main-loop nil))
1310 (defmacro def-run-other-window-manager (name &optional definition)
1311 (let ((definition (or definition name)))
1312 `(defun ,(create-symbol "run-" name) ()
1313 ,(format nil "Run ~A" definition)
1314 (do-run-other-window-manager ,(format nil "~A" name)))))
1316 (def-run-other-window-manager "xterm")
1317 (def-run-other-window-manager "icewm")
1318 (def-run-other-window-manager "twm")
1319 (def-run-other-window-manager "gnome-session" "Gnome")
1320 (def-run-other-window-manager "startkde" "KDE")
1321 (def-run-other-window-manager "xfce4-session" "XFCE")
1323 (defun run-lxde ()
1324 "Run LXDE"
1325 (do-run-other-window-manager "( lxsession & ); xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1327 (defun run-xfce4 ()
1328 "Run LXDE (xterm)"
1329 (do-run-other-window-manager "( xfce4-session &) ; xterm -e \"echo ' /----------------------------------\\' ; echo ' | CLFSWM Note: |' ; echo ' | Close this window when done. |' ; echo ' \\----------------------------------/'; echo; echo; $SHELL\""))
1332 (defun run-prompt-wm ()
1333 "Prompt for an other window manager"
1334 (let ((wm (query-string "Run an other window manager:" "icewm")))
1335 (do-run-other-window-manager wm)))
1338 ;;; Hide or show unmanaged windows utility.
1339 (defun set-hide-unmanaged-window ()
1340 "Hide unmanaged windows when frame is not selected"
1341 (when (frame-p *current-child*)
1342 (setf (frame-data-slot *current-child* :unmanaged-window-action) :hide)
1343 (leave-second-mode)))
1345 (defun set-show-unmanaged-window ()
1346 "Show unmanaged windows when frame is not selected"
1347 (when (frame-p *current-child*)
1348 (setf (frame-data-slot *current-child* :unmanaged-window-action) :show)
1349 (leave-second-mode)))
1351 (defun set-default-hide-unmanaged-window ()
1352 "Set default behaviour to hide or not unmanaged windows when frame is not selected"
1353 (when (frame-p *current-child*)
1354 (setf (frame-data-slot *current-child* :unmanaged-window-action) nil)
1355 (leave-second-mode)))
1357 (defun set-globally-hide-unmanaged-window ()
1358 "Hide unmanaged windows by default. This is overriden by functions above"
1359 (setf *hide-unmanaged-window* t)
1360 (leave-second-mode))
1362 (defun set-globally-show-unmanaged-window ()
1363 "Show unmanaged windows by default. This is overriden by functions above"
1364 (setf *hide-unmanaged-window* nil)
1365 (leave-second-mode))
1368 ;;; Speed mouse movement.
1369 (let (minx miny maxx maxy history lx ly)
1370 (labels ((middle (x1 x2)
1371 (round (/ (+ x1 x2) 2)))
1372 (reset-if-moved (x y)
1373 (when (or (/= x (or lx x)) (/= y (or ly y)))
1374 (speed-mouse-reset)))
1375 (add-in-history (x y)
1376 (push (list x y) history)))
1377 (defun speed-mouse-reset ()
1378 "Reset speed mouse coordinates"
1379 (setf minx nil miny nil maxx nil maxy nil history nil lx nil ly nil))
1380 (defun speed-mouse-left ()
1381 "Speed move mouse to left"
1382 (with-x-pointer
1383 (reset-if-moved x y)
1384 (setf maxx x)
1385 (add-in-history x y)
1386 (setf lx (middle (or minx 0) maxx))
1387 (xlib:warp-pointer *root* lx y)))
1388 (defun speed-mouse-right ()
1389 "Speed move mouse to right"
1390 (with-x-pointer
1391 (reset-if-moved x y)
1392 (setf minx x)
1393 (add-in-history x y)
1394 (setf lx (middle minx (or maxx (xlib:screen-width *screen*))))
1395 (xlib:warp-pointer *root* lx y)))
1396 (defun speed-mouse-up ()
1397 "Speed move mouse to up"
1398 (with-x-pointer
1399 (reset-if-moved x y)
1400 (setf maxy y)
1401 (add-in-history x y)
1402 (setf ly (middle (or miny 0) maxy))
1403 (xlib:warp-pointer *root* x ly)))
1404 (defun speed-mouse-down ()
1405 "Speed move mouse to down"
1406 (with-x-pointer
1407 (reset-if-moved x y)
1408 (setf miny y)
1409 (add-in-history x y)
1410 (setf ly (middle miny (or maxy (xlib:screen-height *screen*))))
1411 (xlib:warp-pointer *root* x ly)))
1412 (defun speed-mouse-undo ()
1413 "Undo last speed mouse move"
1414 (when history
1415 (let ((h (pop history)))
1416 (when h
1417 (destructuring-bind (bx by) h
1418 (setf lx bx ly by
1419 minx nil maxx nil
1420 miny nil maxy nil)
1421 (xlib:warp-pointer *root* lx ly))))))
1422 (defun speed-mouse-first-history ()
1423 "Revert to the first speed move mouse"
1424 (when history
1425 (let ((h (first (last history))))
1426 (when h
1427 (setf lx (first h)
1428 ly (second h))
1429 (xlib:warp-pointer *root* lx ly)))))))
1433 ;;; Notify window functions
1434 (let (font
1435 window
1437 width height
1438 text
1439 current-child)
1440 (labels ((text-string (tx)
1441 (typecase tx
1442 (cons (first tx))
1443 (t tx)))
1444 (text-color (tx)
1445 (get-color (typecase tx
1446 (cons (second tx))
1447 (t *notify-window-foreground*)))))
1448 (defun is-notify-window-p (win)
1449 (xlib:window-equal win window))
1451 (defun refresh-notify-window ()
1452 (add-timer 0.1 #'refresh-notify-window :refresh-notify-window)
1453 (raise-window window)
1454 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1455 (loop for tx in text
1456 for i from 1 do
1457 (setf (xlib:gcontext-foreground gc) (text-color tx))
1458 (xlib:draw-glyphs window gc
1459 (truncate (/ (- width (* (xlib:max-char-width font) (length (text-string tx)))) 2))
1460 (* text-height i 2)
1461 (text-string tx)))))
1463 (defun close-notify-window ()
1464 (erase-timer :refresh-notify-window)
1465 (setf *never-managed-window-list*
1466 (remove (list #'equal #'is-notify-window-p t) *never-managed-window-list* :test #'equal))
1467 (when gc
1468 (xlib:free-gcontext gc))
1469 (when window
1470 (xlib:destroy-window window))
1471 (when font
1472 (xlib:close-font font))
1473 (xlib:display-finish-output *display*)
1474 (setf window nil
1475 gc nil
1476 font nil))
1478 (defun open-notify-window (text-list)
1479 (close-notify-window)
1480 (setf font (xlib:open-font *display* *notify-window-font-string*))
1481 (let ((text-height (- (xlib:font-ascent font) (xlib:font-descent font))))
1482 (setf text text-list)
1483 (setf width (* (xlib:max-char-width font) (+ (loop for tx in text-list
1484 maximize (length (text-string tx))) 2))
1485 height (+ (* text-height (length text-list) 2) text-height))
1486 (with-placement (*notify-window-placement* x y width height)
1487 (setf window (xlib:create-window :parent *root*
1488 :x x
1489 :y y
1490 :width width
1491 :height height
1492 :background (get-color *notify-window-background*)
1493 :border-width 1
1494 :border (get-color *notify-window-border*)
1495 :colormap (xlib:screen-default-colormap *screen*)
1496 :event-mask '(:exposure :key-press))
1497 gc (xlib:create-gcontext :drawable window
1498 :foreground (get-color *notify-window-foreground*)
1499 :background (get-color *notify-window-background*)
1500 :font font
1501 :line-style :solid))
1502 (when (frame-p *current-child*)
1503 (setf current-child *current-child*)
1504 (push (list #'equal #'is-notify-window-p t) *never-managed-window-list*))
1505 (map-window window)
1506 (refresh-notify-window)
1507 (xlib:display-finish-output *display*))))))
1510 (defun display-hello-window ()
1511 (open-notify-window '(("Welcome to CLFSWM" "yellow")
1512 "Press Alt+F1 for help"))
1513 (add-timer *notify-window-delay* #'close-notify-window))