-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpctl.lisp
173 lines (135 loc) · 5.36 KB
/
wpctl.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
(in-package :wpctl)
;; formatters.
(add-screen-mode-line-formatter #\P 'modeline)
;; (add-screen-mode-line-formatter #\M 'source-modeline)
(defparameter *step* 5)
(defparameter *modeline-fmt* "%b(%v)"
"The default value for displaying wpctl information on the modeline")
(defparameter *source-modeline-fmt* "%b(%v)"
"The default value for displaying wpctl source information on the modeline")
(defparameter *formatters-alist*
'((#\b ml-bar)
(#\v ml-volume)))
(defparameter *wpctl-path* "/usr/bin/wpctl")
(defparameter *mixer-command* "pavucontrol")
(defparameter *default-sink-id* "@DEFAULT_AUDIO_SINK@")
(defparameter *default-source-id* "@DEFAULT_AUDIO_SOURCE@")
(defvar *volume-regex* (ppcre:create-scanner "Volume: (\\d+\\.\\d+)"))
(defvar *mute-regex* (ppcre:create-scanner "Volume: \\d+\\.\\d+ \\[MUTED\\]"))
(defun run (args &optional (wait-output nil))
(if wait-output
(with-output-to-string (s)
(sb-ext:run-program *wpctl-path* args :wait t :output s))
(sb-ext:run-program *wpctl-path* args :wait nil)))
(defun volume-up (device-id step)
(run (list "set-volume" device-id (format nil "~D%+" step))))
(defun volume-down (device-id step)
(run (list "set-volume" device-id (format nil "~D%-" step))))
(defun set-volume (device-id value)
(run (list "set-volume" device-id (format nil "~D%" value))))
(defun get-volume (device-id)
(truncate (* 100 (parse-float (aref (nth-value 1
(ppcre:scan-to-strings *volume-regex*
(run (list "get-volume" device-id) t)))
0)))))
(defun get-mute (device-id)
(and (ppcre:scan *mute-regex*
(run (list "get-volume" device-id) t))
t))
(defun unmute (device-id)
(run (list "set-mute" device-id "0")))
(defun mute (device-id)
(run (list "set-mute" device-id "1")))
(defun toggle-mute (device-id)
(run (list "set-mute" device-id "toggle")))
(defun open-mixer ()
(run-shell-command *mixer-command*))
(defun ml-bar (volume muted)
(concat "\["
(stumpwm:bar (if muted 0 (min 100 volume)) 5 #\X #\=)
"\]"))
(defun ml-volume (volume muted)
(if muted "MUT" (format nil "~a\%" volume)))
(defun modeline (ml)
(declare (ignore ml))
(let ((ml-str (format-expand *formatters-alist*
*modeline-fmt*
(get-volume *default-sink-id*) (get-mute *default-sink-id*))))
(if (fboundp 'stumpwm::format-with-on-click-id) ;check in case of old stumpwm version
(format-with-on-click-id ml-str :ml-wpctl-on-click nil)
ml-str)))
(defun source-modeline (ml)
(declare (ignore ml))
(let ((ml-str (format-expand *formatters-alist*
*source-modeline-fmt*
(get-volume *default-source-id*) (get-mute *default-source-id*))))
(if (fboundp 'stumpwm::format-with-on-click-id) ;check in case of old stumpwm version
(format-with-on-click-id ml-str :ml-wpctl-source-on-click nil)
ml-str)))
(defun ml-on-click (code id &rest rest)
(declare (ignore rest))
(declare (ignore id))
(let ((button (stumpwm::decode-button-code code)))
(case button
((:left-button)
(toggle-mute *default-sink-id*))
((:right-button)
(open-mixer))
((:wheel-up)
(volume-up *default-sink-id* *step*))
((:wheel-down)
(volume-down *default-sink-id* *step*))))
(stumpwm::update-all-mode-lines))
(defun source-ml-on-click (code id &rest rest)
(declare (ignore rest))
(declare (ignore id))
(let ((button (stumpwm::decode-button-code code)))
(case button
((:left-button)
(toggle-mute *default-source-id*))
((:right-button)
(open-mixer))
((:wheel-up)
(volume-up *default-source-id* *step*))
((:wheel-down)
(volume-down *default-source-id* *step*))))
(stumpwm::update-all-mode-lines))
(when (fboundp 'stumpwm::register-ml-on-click-id) ;check in case of old stumpwm version
(register-ml-on-click-id :ml-wpctl-on-click #'ml-on-click)
(register-ml-on-click-id :ml-wpctl-source-on-click #'source-ml-on-click))
(defcommand wpctl-volume-up () ()
"Increase the volume by N points"
(volume-up *default-sink-id* *step*))
(defcommand wpctl-volume-down () ()
"Decrease the volume by N points"
(volume-down *default-sink-id* *step*))
(defcommand wpctl-mute () ()
"Mute"
(mute *default-sink-id*))
(defcommand wpctl-unmute () ()
"Unmute"
(unmute *default-sink-id*))
(defcommand wpctl-toggle-mute () ()
"Toggle Mute"
(toggle-mute *default-sink-id*))
(defcommand wpctl-set-volume (value) ((:string "Volume percentage:"))
"Set volume"
(set-volume *default-sink-id* value))
(defcommand wpctl-source-volume-up () ()
"Increase the volume by N points"
(volume-up *default-source-id* *step*))
(defcommand wpctl-source-volume-down () ()
"Decrease the volume by N points"
(volume-down *default-source-id* *step*))
(defcommand wpctl-source-mute () ()
"Source mute"
(mute *default-source-id*))
(defcommand wpctl-source-unmute () ()
"Source unmute"
(unmute *default-source-id*))
(defcommand wpctl-source-toggle-mute () ()
"Toggle source Mute"
(toggle-mute *default-source-id*))
(defcommand wpctl-source-set-volume (value) ((:string "Volume percentage:"))
"Set source volume"
(set-volume *default-source-id* value))