contrib/toolbar.lisp (clock): Add a clock module.
[clfswm.git] / contrib / volume-mode.lisp
blob752663cab612ad4d5c720866dd2a14e26a648211
1 ;;; --------------------------------------------------------------------------
2 ;;; CLFSWM - FullScreen Window Manager
3 ;;;
4 ;;; --------------------------------------------------------------------------
5 ;;; Documentation: Volume mode
6 ;;; --------------------------------------------------------------------------
7 ;;;
8 ;;; (C) 2011 Desmond O. Chang <dochang@gmail.com>
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: A volume mode.
25 ;;; If you want to use this file, just add this line in
26 ;;; your configuration file:
27 ;;;
28 ;;; (load-contrib "volume-mode.lisp")
29 ;;; And with the alsa mixer:
30 ;;; (load-contrib "amixer.lisp")
31 ;;;
32 ;;; This mode is inspired by the emms volume package. When you change the
33 ;;; volume in main mode or second mode, clfswm will enter volume mode and
34 ;;; set a timer to leave this mode. Changing volume in volume mode will
35 ;;; reset the timer. You can also leave volume mode manually by return,
36 ;;; escape or control-g.
37 ;;;
38 ;;; Special variable *VOLUME-MODE-TIMEOUT* controls the timeout in
39 ;;; seconds. If it's positive, volume mode will exit when timeout occurs;
40 ;;; if it's 0, volume mode will exit right now; if it's negative, volume
41 ;;; will not exit even if timeout occurs. Default timeout is 3 seconds.
42 ;;;
43 ;;; Volume mode uses three special variables to control the mixer:
44 ;;; *VOLUME-MUTE-FUNCTION*, *VOLUME-LOWER-FUNCTION* and
45 ;;; *VOLUME-RAISE-FUNCTION*. Their values are functions which must accept
46 ;;; no arguments and return two values indicating the mixer state. The
47 ;;; first value is the volume ratio whose type must be (real 0 1). If the
48 ;;; mixer is mute, the second value should be true, otherwise it should be
49 ;;; false. If volume controller cannot get the mixer state, it must
50 ;;; return NIL.
51 ;;;
52 ;;; Volume mode shows a mute sign, a percentage and a ratio bar on the
53 ;;; screen. A plus sign '+' means it's unmute and a minus sign '-' means
54 ;;; it's mute now. If volume mode doesn't know the mixer state, a message
55 ;;; "unknown" will be shown.
56 ;;;
57 ;;; contrib/amixer.lisp shows how to use volume mode with alsa.
58 ;;;
59 ;;; --------------------------------------------------------------------------
61 (in-package :clfswm)
63 (format t "Loading Volume mode code... ")
65 (defparameter *volume-keys* nil)
66 (defconfig *volume-mode-placement* 'bottom-middle-root-placement
67 'Placement "Volume mode window placement")
70 (defvar *volume-window* nil)
71 (defvar *volume-font* nil)
72 (defvar *volume-gc* nil)
73 (defvar *in-volume-mode* nil)
74 (defvar *leave-volume-mode* nil)
76 (defvar *volume-ratio* nil)
77 (defvar *volume-mute* nil)
79 (defvar *volume-mode-timeout* 3
80 "Volume mode timeout in seconds:
81 > 0 means volume mode will exit when timeout occurs;
82 = 0 means exit right now;
83 < 0 means exit manually.")
86 ;;; CONFIG - Volume mode
87 (defconfig *volume-font-string* *default-font-string*
88 'Volume-mode "Volume string window font string")
89 (defconfig *volume-background* "black"
90 'Volume-mode "Volume string window background color")
91 (defconfig *volume-foreground* "green"
92 'Volume-mode "Volume string window foreground color")
93 (defconfig *volume-border* "red"
94 'Volume-mode "Volume string window border color")
95 (defconfig *volume-width* 400
96 'Volume-mode "Volume mode window width")
97 (defconfig *volume-height* 15
98 'Volume-mode "Volume mode window height")
99 (defconfig *volume-text-limit* 30
100 'Volume-mode "Maximum text limit in the volume window")
101 (defconfig *volume-external-mixer-cmd* "/usr/bin/gnome-alsamixer"
102 'Volume-mode "Command to start an external mixer program")
104 (define-init-hash-table-key *volume-keys* "Volume mode keys")
105 (define-define-key "volume" *volume-keys*)
107 (add-hook *binding-hook* 'init-*volume-keys*)
109 (defun set-default-volume-keys ()
110 (define-volume-key ("XF86AudioMute") 'volume-mute)
111 (define-volume-key ("XF86AudioLowerVolume") 'volume-lower)
112 (define-volume-key ("XF86AudioRaiseVolume") 'volume-raise)
113 (define-volume-key (#\/) 'volume-mute)
114 (define-volume-key (#\,) 'volume-lower)
115 (define-volume-key (#\.) 'volume-raise)
116 (define-volume-key ("m") 'volume-mute)
117 (define-volume-key ("l") 'volume-lower)
118 (define-volume-key ("r") 'volume-raise)
119 (define-volume-key ("Down") 'volume-lower)
120 (define-volume-key ("Up") 'volume-raise)
121 (define-volume-key ("Left") 'volume-lower)
122 (define-volume-key ("Right") 'volume-raise)
123 (define-volume-key ("PageUp") 'volume-lower)
124 (define-volume-key ("PageDown") 'volume-raise)
125 (define-volume-key ("Return") 'leave-volume-mode)
126 (define-volume-key ("Escape") 'leave-volume-mode)
127 (define-volume-key ("g" :control) 'leave-volume-mode)
128 (define-volume-key ("e") 'run-external-volume-mixer)
129 ;;; Main mode
130 (define-main-key ("XF86AudioMute") 'volume-mute)
131 (define-main-key ("XF86AudioLowerVolume") 'volume-lower)
132 (define-main-key ("XF86AudioRaiseVolume") 'volume-raise)
133 ;;; Second mode
134 (define-second-key ("XF86AudioMute") 'volume-mute)
135 (define-second-key ("XF86AudioLowerVolume") 'volume-lower)
136 (define-second-key ("XF86AudioRaiseVolume") 'volume-raise))
138 (add-hook *binding-hook* 'set-default-volume-keys)
140 (defun volume-mode-window-message (width)
141 (if *volume-ratio*
142 (let* ((mute (if *volume-mute* #\- #\+))
143 (percentage (round (* 100 *volume-ratio*)))
144 (n (round (* width *volume-ratio*))))
145 (format nil "[~A] ~3@A% ~A~A" mute percentage
146 (repeat-chars n #\#) (repeat-chars (- width n) #\.)))
147 "unknown"))
149 (defun draw-volume-mode-window ()
150 (raise-window *volume-window*)
151 (clear-pixmap-buffer *volume-window* *volume-gc*)
152 (let* ((text (limit-length (volume-mode-window-message 20) *volume-text-limit*))
153 (len (length text)))
154 (xlib:draw-glyphs *pixmap-buffer* *volume-gc*
155 (truncate (/ (- *volume-width* (* (xlib:max-char-width *volume-font*) len)) 2))
156 (truncate (/ (+ *volume-height* (- (xlib:font-ascent *volume-font*) (xlib:font-descent *volume-font*))) 2))
157 text))
158 (copy-pixmap-buffer *volume-window* *volume-gc*))
160 (defun leave-volume-mode ()
161 "Leave the volume mode"
162 (throw 'exit-volume-loop nil))
164 (defun update-volume-mode ()
165 (draw-volume-mode-window)
166 (cond ((plusp *volume-mode-timeout*)
167 (erase-timer :volume-mode-timer)
168 (with-timer (*volume-mode-timeout* :volume-mode-timer)
169 (setf *leave-volume-mode* t)))
170 ((zerop *volume-mode-timeout*)
171 (erase-timer :volume-mode-timer)
172 (setf *leave-volume-mode* t))
173 ((minusp *volume-mode-timeout*)
174 (erase-timer :volume-mode-timer))))
176 (defun volume-enter-function ()
177 (with-placement (*volume-mode-placement* x y *volume-width* *volume-height*)
178 (setf *volume-font* (xlib:open-font *display* *volume-font-string*)
179 *volume-window* (xlib:create-window :parent *root*
180 :x x
181 :y y
182 :width *volume-width*
183 :height *volume-height*
184 :background (get-color *volume-background*)
185 :border-width 1
186 :border (get-color *volume-border*)
187 :colormap (xlib:screen-default-colormap *screen*)
188 :event-mask '(:exposure :key-press))
189 *volume-gc* (xlib:create-gcontext :drawable *volume-window*
190 :foreground (get-color *volume-foreground*)
191 :background (get-color *volume-background*)
192 :font *volume-font*
193 :line-style :solid))
194 (map-window *volume-window*))
195 (setf *in-volume-mode* t
196 *leave-volume-mode* nil)
197 (update-volume-mode))
199 (defun volume-loop-function ()
200 (when *leave-volume-mode*
201 (leave-volume-mode)))
203 (defun volume-leave-function ()
204 (when *volume-gc*
205 (xlib:free-gcontext *volume-gc*))
206 (when *volume-window*
207 (xlib:destroy-window *volume-window*))
208 (when *volume-font*
209 (xlib:close-font *volume-font*))
210 (xlib:display-finish-output *display*)
211 (erase-timer :volume-mode-timer)
212 (setf *volume-window* nil
213 *volume-gc* nil
214 *volume-font* nil
215 *in-volume-mode* nil
216 *leave-volume-mode* nil))
218 (define-handler volume-mode :key-press (code state)
219 (funcall-key-from-code *volume-keys* code state))
221 (defun volume-mode ()
222 (let ((grab-keyboard-p (xgrab-keyboard-p))
223 (grab-pointer-p (xgrab-pointer-p)))
224 (xgrab-pointer *root* 92 93)
225 (unless grab-keyboard-p
226 (ungrab-main-keys)
227 (xgrab-keyboard *root*))
228 (generic-mode 'volume-mode 'exit-volume-loop
229 :enter-function 'volume-enter-function
230 :loop-function 'volume-loop-function
231 :leave-function 'volume-leave-function
232 :original-mode '(main-mode))
233 (unless grab-keyboard-p
234 (xungrab-keyboard)
235 (grab-main-keys))
236 (if grab-pointer-p
237 (xgrab-pointer *root* 66 67)
238 (xungrab-pointer))))
240 (defun volume-set (fn)
241 (when fn
242 (setf (values *volume-ratio* *volume-mute*) (funcall fn))
243 (if *in-volume-mode*
244 (update-volume-mode)
245 (volume-mode))))
247 (defvar *volume-mute-function* nil)
248 (defvar *volume-lower-function* nil)
249 (defvar *volume-raise-function* nil)
251 (defun volume-mute ()
252 "Toggle mute."
253 (volume-set *volume-mute-function*))
255 (defun volume-lower ()
256 "Lower volume."
257 (volume-set *volume-lower-function*))
259 (defun volume-raise ()
260 "Raise volume."
261 (volume-set *volume-raise-function*))
263 (defun run-external-volume-mixer ()
264 "Start an external volume mixer"
265 (do-shell *volume-external-mixer-cmd*))
267 (format t "done~%")