contrib/toolbar.lisp (remove-toolbar): New function.
[clfswm.git] / contrib / toolbar.lisp
blob0e2b27b90fa5f59ef7534c0e0286f41c64160ab5
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Toolbar
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2012 Philippe Brochard <pbrochard@common-lisp.net>
9 ;;;
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 3 of the License, or
13 ;;; (at your option) any later version.
14 ;;;
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
19 ;;;
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software
22 ;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 ;;;
24 ;;; Documentation: If you want to use this file, just add this line in
25 ;;; your configuration file:
26 ;;;
27 ;;; (load-contrib "toolbar.lisp")
28 ;;;
29 ;;; You can add a toolbar with the function add-toolbar in your configuration
30 ;;; file. You can obtain modules list with the list-toolbar-modules function.
31 ;;;
32 ;;; For convenience, here is the add-toolbar documentation:
33 ;;;
34 ;;; add-toolbar (root-x root-y direction size placement modules
35 ;;; &key (autohide *toolbar-default-autohide*)
36 ;;; (thickness *toolbar-default-thickness*)
37 ;;; (refresh-delay *toolbar-default-refresh-delay*)
38 ;;; (border-size *toolbar-default-border-size*))
39 ;;; "Add a new toolbar.
40 ;;; root-x, root-y: root coordinates or if root-y is nil, root-x is the nth root in root-list.
41 ;;; direction: one of :horiz or :vert
42 ;;; placement: same argument as with-placement macro
43 ;;; modules: list of modules: a list of module name, position in percent and arguments.
44 ;;; 0%=left/up <-> 100%=right/down.
45 ;;; Example: '((clock 1) (label 50 \"My label\") (clickable-clock 90))
46 ;;; size: toolbar size in percent of root size
47 ;;; thickness: toolbar height for horizontal toolbar or width for vertical one
48 ;;; autohide: one of nil, :click, or :motion
49 ;;; refresh-delay: refresh delay for toolbar in seconds
50 ;;; border-size: toolbar window border size"
51 ;;;
52 ;;; Here are some examples:
53 ;;; (load-contrib "toolbar.lisp")
54 ;;;
55 ;;; ;; Add an horizontal toolbar on root at coordinates 0,0 pixels
56 ;;; ;; with default modules
57 ;;;
58 ;;; (add-toolbar 0 0 :horiz 80 'top-middle-root-placement *default-toolbar*)
59 ;;;
60 ;;;
61 ;;; ;; Add an horizontal toolbar on root at coordinates 0,0 pixels
62 ;;;
63 ;;; (add-toolbar 0 0 :horiz 90 'top-middle-root-placement
64 ;;; '((clock 1) (label 50 "Plop") (clock-second 25) (clickable-clock 99))
65 ;;; :autohide :click
66 ;;; :refresh-delay 1)
67 ;;;
68 ;;;
69 ;;; ;; Add an horizontal toolbar on root at coordinates 0,0 pixels
70 ;;;
71 ;;; (add-toolbar 0 0 :horiz 70 'bottom-middle-root-placement '((clock 1) (label 50 "Paf) (clock 99))
72 ;;; :autohide :motion)
73 ;;;
74 ;;;
75 ;;; ;; Add a vertical toolbar on root 0
76 ;;;
77 ;;; (add-toolbar 0 nil :vert 60 'middle-left-root-placement '((clock 1) (label 50 "My label") (clock 90)))
78 ;;;
79 ;;;
80 ;;; ;; Add a vertical toolbar on root 1
81 ;;;
82 ;;; (add-toolbar 1 nil :vert 70 'bottom-right-root-placement '((clock 1) (label 50) (clickable-clock 99)))
83 ;;; --------------------------------------------------------------------------
85 (in-package :clfswm)
87 (format t "Loading Toolbar code... ")
89 (defstruct toolbar root-x root-y root direction size thickness placement refresh-delay
90 autohide modules clickable hide-state font window gc border-size
91 exposure-hook button-press-hook motion-notify-hook leave-notify-hook)
93 (defstruct toolbar-module name pos display-fun click-fun args rect)
95 (defparameter *toolbar-list* nil)
96 (defparameter *toolbar-module-list* nil)
98 (defconfig *default-toolbar* '((clfswm-menu 1)
99 (system-usage 90)
100 (clickable-clock 99))
101 'Toolbar "Default toolbar modules")
104 ;;; CONFIG - Toolbar window string colors
105 (defconfig *toolbar-window-font-string* *default-font-string*
106 'Toolbar "Toolbar window font string")
107 (defconfig *toolbar-window-background* "black"
108 'Toolbar "Toolbar Window background color")
109 (defconfig *toolbar-window-foreground* "green"
110 'Toolbar "Toolbar Window foreground color")
111 (defconfig *toolbar-window-border* "red"
112 'Toolbar "Toolbar Window border color")
113 (defconfig *toolbar-default-border-size* 0
114 'Toolbar "Toolbar Window border size")
115 (defconfig *toolbar-window-transparency* *default-transparency*
116 'Toolbar "Toolbar window background transparency")
117 (defconfig *toolbar-default-thickness* 20
118 'Toolbar "Toolbar default thickness")
119 (defconfig *toolbar-default-refresh-delay* 30
120 'Toolbar "Toolbar default refresh delay")
121 (defconfig *toolbar-default-autohide* nil
122 'Toolbar "Toolbar default autohide value")
123 (defconfig *toolbar-sensibility* 3
124 'Toolbar "Toolbar sensibility in pixels")
126 (defconfig *toolbar-window-placement* 'top-left-placement
127 'Placement "Toolbar window placement")
129 (defun toolbar-symbol-fun (name &optional (type 'display))
130 (create-symbol-in-package :clfswm 'toolbar- name '-module- type))
132 (defun toolbar-adjust-root-size (toolbar)
133 (unless (toolbar-autohide toolbar)
134 (let ((root (toolbar-root toolbar))
135 (placement-name (symbol-name (toolbar-placement toolbar)))
136 (thickness (+ (toolbar-thickness toolbar) (* 2 (toolbar-border-size toolbar)))))
137 (when (root-p root)
138 (case (toolbar-direction toolbar)
139 (:horiz (cond ((search "TOP" placement-name)
140 (incf (root-y root) thickness)
141 (decf (root-h root) thickness))
142 ((search "BOTTOM" placement-name)
143 (decf (root-h root) thickness))))
144 (t (cond ((search "LEFT" placement-name)
145 (incf (root-x root) thickness)
146 (decf (root-w root) thickness))
147 ((search "RIGHT" placement-name)
148 (decf (root-w root) thickness)))))))))
151 (defun toolbar-draw-text (toolbar pos1 pos2 text color)
152 "pos1: percent of toolbar, pos2: pixels in toolbar"
153 (labels ((horiz-text ()
154 (let* ((height (- (xlib:font-ascent (toolbar-font toolbar)) (xlib:font-descent (toolbar-font toolbar))))
155 (dy (truncate (+ pos2 (/ height 2))))
156 (width (xlib:text-width (toolbar-font toolbar) text))
157 (pos (truncate (/ (* (- (xlib:drawable-width (toolbar-window toolbar)) width) pos1) 100))))
158 (xlib:draw-glyphs *pixmap-buffer* (toolbar-gc toolbar) pos dy text)
159 (values (+ pos (xlib:drawable-x (toolbar-window toolbar)))
160 (xlib:drawable-y (toolbar-window toolbar))
161 width
162 (xlib:drawable-height (toolbar-window toolbar)))))
163 (vert-text ()
164 (let* ((width (xlib:max-char-width (toolbar-font toolbar)))
165 (dx (truncate (- pos2 (/ width 2))))
166 (dpos (xlib:max-char-ascent (toolbar-font toolbar)))
167 (height (* dpos (length text)))
168 (pos (+ (truncate (/ (* (- (xlib:drawable-height (toolbar-window toolbar)) height
169 (xlib:max-char-descent (toolbar-font toolbar)))
170 pos1) 100))
171 (xlib:font-ascent (toolbar-font toolbar)))))
172 (loop for c across text
173 for i from 0
174 do (xlib:draw-glyphs *pixmap-buffer* (toolbar-gc toolbar) dx (+ pos (* i dpos)) (string c)))
175 (values (xlib:drawable-x (toolbar-window toolbar))
176 (+ (- pos dpos) (xlib:drawable-y (toolbar-window toolbar)))
177 (xlib:drawable-width (toolbar-window toolbar))
178 height))))
179 (xlib:with-gcontext ((toolbar-gc toolbar) :foreground (get-color color))
180 (case (toolbar-direction toolbar)
181 (:horiz (horiz-text))
182 (:vert (vert-text))))))
185 (defun toolbar-module-text (toolbar module color formatter &rest text)
186 "Print a formatted text at module position centered in toolbar"
187 (toolbar-draw-text toolbar (toolbar-module-pos module) (/ *toolbar-default-thickness* 2)
188 (apply #'format nil formatter text)
189 color))
191 (defun is-valid-toolbar (toolbar)
192 (member toolbar *toolbar-list*))
195 (defun refresh-toolbar (toolbar)
196 (when (is-valid-toolbar toolbar)
197 (unless (toolbar-hide-state toolbar)
198 (add-timer (toolbar-refresh-delay toolbar)
199 (lambda ()
200 (refresh-toolbar toolbar))
201 :refresh-toolbar)
202 (clear-pixmap-buffer (toolbar-window toolbar) (toolbar-gc toolbar))
203 (dolist (module (toolbar-modules toolbar))
204 (when (fboundp (toolbar-module-display-fun module))
205 (apply (toolbar-module-display-fun module) toolbar module (toolbar-module-args module))))
206 (copy-pixmap-buffer (toolbar-window toolbar) (toolbar-gc toolbar)))))
208 (defun toolbar-in-sensibility-zone-p (toolbar root-x root-y)
209 (let* ((tb-win (toolbar-window toolbar))
210 (win-x (xlib:drawable-x tb-win))
211 (win-y (xlib:drawable-y tb-win))
212 (width (xlib:drawable-width tb-win))
213 (height (xlib:drawable-height tb-win))
214 (tb-dir (toolbar-direction toolbar) )
215 (placement-name (symbol-name (toolbar-placement toolbar))))
216 (or (and (equal tb-dir :horiz) (search "TOP" placement-name)
217 (<= root-y win-y (+ root-y *toolbar-sensibility*))
218 (<= win-x root-x (+ win-x width)) (toolbar-autohide toolbar))
219 (and (equal tb-dir :horiz) (search "BOTTOM" placement-name)
220 (<= (+ win-y height (- *toolbar-sensibility*)) root-y (+ win-y height))
221 (<= win-x root-x (+ win-x width)) (toolbar-autohide toolbar))
222 (and (equal tb-dir :vert) (search "LEFT" placement-name)
223 (<= root-x win-x (+ root-x *toolbar-sensibility*))
224 (<= win-y root-y (+ win-y height)) (toolbar-autohide toolbar))
225 (and (equal tb-dir :vert) (search "RIGHT" placement-name)
226 (<= (+ win-x width (- *toolbar-sensibility*)) root-x (+ win-x width))
227 (<= win-y root-y (+ win-y height)) (toolbar-autohide toolbar)))))
229 (use-event-hook :exposure)
230 (use-event-hook :button-press)
231 (use-event-hook :motion-notify)
232 (use-event-hook :leave-notify)
235 (defun toolbar-add-exposure-hook (toolbar)
236 (push (define-event-hook :exposure (window)
237 (when (and (is-valid-toolbar toolbar)
238 (xlib:window-p window)
239 (xlib:window-equal (toolbar-window toolbar) window))
240 (refresh-toolbar toolbar)))
241 (toolbar-exposure-hook toolbar)))
244 (defun toolbar-add-hide-button-press-hook (toolbar)
245 (push (define-event-hook :button-press (code root-x root-y)
246 (when (and (is-valid-toolbar toolbar) (= code 1))
247 (let* ((tb-win (toolbar-window toolbar)))
248 (when (toolbar-in-sensibility-zone-p toolbar root-x root-y)
249 (if (toolbar-hide-state toolbar)
250 (progn
251 (setf (toolbar-hide-state toolbar) nil)
252 (map-window tb-win)
253 (raise-window tb-win)
254 (refresh-toolbar toolbar))
255 (progn
256 (hide-window tb-win)
257 (setf (toolbar-hide-state toolbar) t)))
258 (wait-mouse-button-release)
259 (stop-button-event)
260 (exit-handle-event)))))
261 (toolbar-button-press-hook toolbar)))
263 (defun toolbar-add-hide-motion-hook (toolbar)
264 (push (define-event-hook :motion-notify (root-x root-y)
265 (unless (compress-motion-notify)
266 (when (and (is-valid-toolbar toolbar)
267 (toolbar-hide-state toolbar)
268 (toolbar-in-sensibility-zone-p toolbar root-x root-y))
269 (map-window (toolbar-window toolbar))
270 (raise-window (toolbar-window toolbar))
271 (refresh-toolbar toolbar)
272 (setf (toolbar-hide-state toolbar) nil)
273 (exit-handle-event))))
274 (toolbar-motion-notify-hook toolbar)))
276 (defun toolbar-add-hide-leave-hook (toolbar)
277 (push (define-event-hook :leave-notify (root-x root-y)
278 (when (and (is-valid-toolbar toolbar)
279 (not (toolbar-hide-state toolbar))
280 (not (in-window (toolbar-window toolbar) root-x root-y)))
281 (hide-window (toolbar-window toolbar))
282 (setf (toolbar-hide-state toolbar) t)
283 (exit-handle-event)))
284 (toolbar-leave-notify-hook toolbar)))
287 (defun toolbar-add-clickable-module-hook (toolbar)
288 (push (define-event-hook :button-press (code state root-x root-y)
289 (when (and (is-valid-toolbar toolbar)
290 (in-window (toolbar-window toolbar) root-x root-y)
291 (not (toolbar-hide-state toolbar)))
292 (dolist (module (toolbar-modules toolbar))
293 (when (and (in-rectangle root-x root-y (toolbar-module-rect module))
294 (fboundp (toolbar-module-click-fun module)))
295 (apply (toolbar-module-click-fun module) toolbar module code state
296 (toolbar-module-args module))
297 (stop-button-event)
298 (exit-handle-event)))))
299 (toolbar-button-press-hook toolbar)))
302 (defun define-toolbar-hooks (toolbar)
303 (toolbar-add-exposure-hook toolbar)
304 (when (toolbar-clickable toolbar)
305 (toolbar-add-clickable-module-hook toolbar))
306 (case (toolbar-autohide toolbar)
307 (:click (toolbar-add-hide-button-press-hook toolbar))
308 (:motion (toolbar-add-hide-motion-hook toolbar)
309 (toolbar-add-hide-leave-hook toolbar))))
311 (defun set-clickable-toolbar (toolbar)
312 (dolist (module (toolbar-modules toolbar))
313 (when (fboundp (toolbar-module-click-fun module))
314 (setf (toolbar-clickable toolbar) t))))
317 (defmacro remove-toolbar-hook (toolbar keyword)
318 (let ((fun (create-symbol 'toolbar- keyword '-hook)))
319 `(dolist (hook (,fun ,toolbar))
320 (remove-event-hook ,keyword hook))))
324 (let ((windows-list nil))
325 (defun is-toolbar-window-p (win)
326 (and (xlib:window-p win) (member win windows-list :test 'xlib:window-equal)))
328 (defun close-toolbar (toolbar)
329 (erase-timer :refresh-toolbar-window)
330 (remove-toolbar-hook toolbar :exposure)
331 (remove-toolbar-hook toolbar :button-press)
332 (remove-toolbar-hook toolbar :leave-notify)
333 (remove-toolbar-hook toolbar :motion-notify)
334 (setf *never-managed-window-list*
335 (remove (list #'is-toolbar-window-p nil)
336 *never-managed-window-list* :test #'equal))
337 (awhen (toolbar-gc toolbar)
338 (xlib:free-gcontext it))
339 (awhen (toolbar-window toolbar)
340 (xlib:destroy-window it))
341 (awhen (toolbar-font toolbar)
342 (xlib:close-font it))
343 (xlib:display-finish-output *display*)
344 (setf (toolbar-window toolbar) nil
345 (toolbar-gc toolbar) nil
346 (toolbar-font toolbar) nil))
348 (defun open-toolbar (toolbar)
349 (let ((root (root (toolbar-root-x toolbar) (toolbar-root-y toolbar))))
350 (when (root-p root)
351 (setf (toolbar-root toolbar) root)
352 (let ((*get-current-root-fun* (lambda () root)))
353 (setf (toolbar-font toolbar) (xlib:open-font *display* *toolbar-window-font-string*))
354 (let* ((width (if (equal (toolbar-direction toolbar) :horiz)
355 (round (/ (* (root-w root) (toolbar-size toolbar)) 100))
356 (toolbar-thickness toolbar)))
357 (height (if (equal (toolbar-direction toolbar) :horiz)
358 (toolbar-thickness toolbar)
359 (round (/ (* (root-h root) (toolbar-size toolbar)) 100)))))
360 (with-placement ((toolbar-placement toolbar) x y width height (toolbar-border-size toolbar))
361 (setf (toolbar-window toolbar) (xlib:create-window :parent *root*
362 :x x
363 :y y
364 :width width
365 :height height
366 :background (get-color *toolbar-window-background*)
367 :border-width (toolbar-border-size toolbar)
368 :border (when (plusp (toolbar-border-size toolbar))
369 (get-color *toolbar-window-border*))
370 :colormap (xlib:screen-default-colormap *screen*)
371 :event-mask '(:exposure :key-press :leave-window
372 :pointer-motion))
373 (toolbar-gc toolbar) (xlib:create-gcontext :drawable (toolbar-window toolbar)
374 :foreground (get-color *toolbar-window-foreground*)
375 :background (get-color *toolbar-window-background*)
376 :font (toolbar-font toolbar)
377 :line-style :solid))
378 (push (toolbar-window toolbar) windows-list)
379 (setf (window-transparency (toolbar-window toolbar)) *toolbar-window-transparency*)
380 (push (list #'is-toolbar-window-p nil) *never-managed-window-list*)
381 (map-window (toolbar-window toolbar))
382 (raise-window (toolbar-window toolbar))
383 (refresh-toolbar toolbar)
384 (when (toolbar-autohide toolbar)
385 (hide-window (toolbar-window toolbar))
386 (setf (toolbar-hide-state toolbar) t))
387 (xlib:display-finish-output *display*)
388 (set-clickable-toolbar toolbar)
389 (define-toolbar-hooks toolbar))))))))
392 (defun open-all-toolbars ()
393 "Open all toolbars"
394 (dolist (toolbar *toolbar-list*)
395 (open-toolbar toolbar))
396 (dolist (toolbar *toolbar-list*)
397 (toolbar-adjust-root-size toolbar)))
399 (defun close-all-toolbars ()
400 (dolist (toolbar *toolbar-list*)
401 (close-toolbar toolbar))
402 (stop-system-poll))
404 (defun create-toolbar-modules (modules)
405 (loop for mod in modules
406 collect (make-toolbar-module :name (first mod)
407 :pos (second mod)
408 :display-fun (toolbar-symbol-fun (first mod))
409 :click-fun (toolbar-symbol-fun (first mod) 'click)
410 :args (cddr mod)
411 :rect nil)))
414 (defun add-toolbar (root-x root-y direction size placement modules
415 &key (autohide *toolbar-default-autohide*)
416 (thickness *toolbar-default-thickness*)
417 (refresh-delay *toolbar-default-refresh-delay*)
418 (border-size *toolbar-default-border-size*))
419 "Add a new toolbar.
420 root-x, root-y: root coordinates or if root-y is nil, root-x is the nth root in root-list.
421 direction: one of :horiz or :vert
422 placement: same argument as with-placement macro
423 modules: list of modules: a list of module name, position in percent and arguments.
424 0%=left/up <-> 100%=right/down.
425 Example: '((clock 1) (label 50 \"My label\") (clickable-clock 90))
426 size: toolbar size in percent of root size
427 thickness: toolbar height for horizontal toolbar or width for vertical one
428 autohide: one of nil, :click, or :motion
429 refresh-delay: refresh delay for toolbar in seconds
430 border-size: toolbar window border size"
431 (let ((toolbar (make-toolbar :root-x root-x :root-y root-y
432 :direction direction :size size
433 :thickness thickness
434 :placement placement
435 :autohide autohide
436 :refresh-delay refresh-delay
437 :border-size border-size
438 :modules (create-toolbar-modules modules))))
439 (push toolbar *toolbar-list*)
440 toolbar))
442 (defun remove-toolbar (toolbar)
443 (close-toolbar toolbar)
444 (setf *toolbar-list* (remove toolbar *toolbar-list* :test #'equal)))
447 (add-hook *init-hook* 'open-all-toolbars)
448 (add-hook *close-hook* 'close-all-toolbars)
451 (defun set-toolbar-module-rectangle (module x y width height)
452 (unless (toolbar-module-rect module)
453 (setf (toolbar-module-rect module) (make-rectangle)))
454 (setf (rectangle-x (toolbar-module-rect module)) x
455 (rectangle-y (toolbar-module-rect module)) y
456 (rectangle-width (toolbar-module-rect module)) width
457 (rectangle-height (toolbar-module-rect module)) height))
459 (defmacro with-set-toolbar-module-rectangle ((module) &body body)
460 (let ((x (gensym)) (y (gensym)) (width (gensym)) (height (gensym)))
461 `(multiple-value-bind (,x ,y ,width ,height)
462 ,@body
463 (set-toolbar-module-rectangle ,module ,x ,y ,width ,height))))
467 (defmacro define-toolbar-module ((name &rest args) &body body)
468 (let ((symbol-fun (toolbar-symbol-fun name)))
469 `(progn
470 (pushnew ',name *toolbar-module-list*)
471 (defun ,symbol-fun (toolbar module ,@(when args `(&optional ,@args)))
472 ,@body))))
474 (defmacro define-toolbar-module-click ((name &rest args) &body body)
475 (let ((symbol-fun (toolbar-symbol-fun name 'click)))
476 `(progn
477 (pushnew ',name *toolbar-module-list*)
478 (defun ,symbol-fun (toolbar module code state ,@(when args `(&optional ,@args)))
479 ,@body))))
482 (defun list-toolbar-modules (&optional (stream t))
483 "List all toolbar modules"
484 (format stream "Toolbar modules availables:~%")
485 (dolist (module (reverse *toolbar-module-list*))
486 (format stream " Module: ~A~%" module)
487 (when (fboundp (toolbar-symbol-fun module))
488 (format stream " ~A~%" (documentation (toolbar-symbol-fun module) 'function)))
489 (when (fboundp (toolbar-symbol-fun module 'click))
490 (format stream " On click: ~A~%" (documentation (toolbar-symbol-fun module 'click) 'function)))))
493 (defmacro define-toolbar-color (name doc-string &optional (value *toolbar-window-foreground*))
494 (let ((symbol-name (create-symbol '*toolbar- name '-color*)))
495 `(defconfig ,symbol-name ,value 'Toolbar ,doc-string)))
497 (defmacro tb-color (name)
498 (let ((symbol-name (create-symbol '*toolbar- name '-color*)))
499 symbol-name))
502 ;;; Modules definitions
506 ;;; Clock module
508 (define-toolbar-color clock "Clock color")
510 (define-toolbar-module (clock)
511 "A clock module"
512 (multiple-value-bind (s m h)
513 (get-decoded-time)
514 (declare (ignore s))
515 (toolbar-module-text toolbar module (tb-color clock) "~2,'0D:~2,'0D" h m)))
518 ;;; Clock module with seconds
520 (define-toolbar-module (clock-second)
521 "A clock module with seconds"
522 (multiple-value-bind (s m h)
523 (get-decoded-time)
524 (toolbar-module-text toolbar module (tb-color clock) "~2,'0D:~2,'0D:~2,'0D" h m s)))
528 ;;; Label module
530 (define-toolbar-color label "Label color")
532 (define-toolbar-module (label text)
533 "(text) - Display a text in toolbar"
534 (toolbar-module-text toolbar module (tb-color label) (or text "Empty")))
537 ;;; Clickable label module
539 (define-toolbar-color clickable-label "Clickable label color")
541 (define-toolbar-module (clickable-label text action)
542 "(text action) - Display a clickable text in toolbar"
543 (declare (ignore action))
544 (with-set-toolbar-module-rectangle (module)
545 (toolbar-module-text toolbar module (tb-color clickable-label) (or text "Empty"))))
547 (define-toolbar-module-click (clickable-label text action)
548 "Call the function 'action'"
549 (declare (ignore text))
550 (when action
551 (funcall action toolbar module code state )))
554 ;;; Clickable clock module
556 (define-toolbar-color clickable-clock "Clickable clock color")
558 (define-toolbar-module (clickable-clock)
559 "A clickable clock module"
560 (multiple-value-bind (s m h)
561 (get-decoded-time)
562 (declare (ignore s))
563 (with-set-toolbar-module-rectangle (module)
564 (toolbar-module-text toolbar module (tb-color clickable-clock) "~2,'0D:~2,'0D" h m))))
567 (defconfig *toolbar-clock-action* "xclock -analog"
568 'toolbar "Toolbar clickable clock module action on click")
570 (define-toolbar-module-click (clickable-clock)
571 "Start an external clock"
572 (declare (ignore toolbar module state))
573 (when (= code 1)
574 (do-shell *toolbar-clock-action*)))
577 (format t "done~%")
581 ;;; CLFSWM menu module
583 (define-toolbar-color clfswm-menu "CLFSWM menu color")
585 (define-toolbar-module (clfswm-menu text placement)
586 "(text placement) - Display an entry for the CLFSWM menu"
587 (declare (ignore placement))
588 (with-set-toolbar-module-rectangle (module)
589 (toolbar-module-text toolbar module (tb-color clfswm-menu) (or text "CLFSWM"))))
591 (define-toolbar-module-click (clfswm-menu text placement)
592 "Open the CLFSWM main menu"
593 (declare (ignore text code state toolbar module))
594 (let ((*info-mode-placement* (or placement *info-mode-placement*)))
595 (open-menu)))
598 ;;; CPU usage
600 (define-toolbar-color cpu "CPU color")
602 (define-toolbar-module (cpu)
603 "Display the CPU usage (slow methode)"
604 (toolbar-module-text toolbar module (tb-color cpu) "CPU:~A%" (cpu-usage)))
608 ;;; Memory usage
610 (define-toolbar-color mem "Memory color")
612 (define-toolbar-module (mem)
613 "Display the memory usage (slow methode)"
614 (multiple-value-bind (used total)
615 (memory-usage)
616 (toolbar-module-text toolbar module (tb-color mem) "Mem:~A%" (round (* (/ used total) 100.0)))))
621 ;;; Battery usage
623 (define-toolbar-color system-info "System information colors (CPU+Mem+Battery)")
624 (define-toolbar-color system-info-low "System information colors (CPU+Mem+Battery)" "Yellow")
625 (define-toolbar-color system-info-alert "System information colors (CPU+Mem+Battery)" "Magenta")
626 (define-toolbar-color system-info-urgent "System information colors (CPU+Mem+Battery)" "Red")
628 (defun toolbar-battery-color (bat)
629 (if (numberp bat)
630 (cond ((<= bat 5) (tb-color system-info-urgent))
631 ((<= bat 10) (tb-color system-info-alert))
632 ((<= bat 25) (tb-color system-info-low))
633 (t (tb-color system-info)))
634 (tb-color system-info)))
636 (define-toolbar-module (bat)
637 "Display the battery usage (slow methode)"
638 (let* ((bat (battery-usage)))
639 (toolbar-module-text toolbar module
640 (toolbar-battery-color bat)
641 "Bat:~A%" bat)))
646 ;;; System usage - Battery, CPU and Memory usage all in one
648 (define-toolbar-module (system-usage (poll-delay 10))
649 "Display system usage: CPU, Memory and Battery (poll methode)"
650 (multiple-value-bind (cpu used total bat)
651 (system-usage-poll poll-delay)
652 (toolbar-module-text toolbar module (toolbar-battery-color bat)
653 "Bat:~A% CPU:~A% Mem:~A%"
654 bat cpu
655 (round (* (/ used total) 100)))))
658 ;;; CPU and Memory usage - CPU and Memory usage
660 (define-toolbar-module (system-cpu-mem (poll-delay 10))
661 "Display system usage: CPU and Memory (poll methode)"
662 (multiple-value-bind (cpu used total)
663 (system-usage-poll poll-delay)
664 (toolbar-module-text toolbar module (tb-color cpu)
665 "CPU:~A% Mem:~A%"
667 (round (* (/ used total) 100)))))