Skip to content

Commit 7f0bc88

Browse files
authored
Merge pull request #297 from catap/sndioctl-in-out
sndioctl: support change input level
2 parents 6bfed60 + 40a76a8 commit 7f0bc88

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

media/stumpwm-sndioctl/stumpwm-sndioctl.lisp

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,81 @@
2424
(defvar *terse* nil "If t, this will supress messages that get shown on changes to volume.")
2525
(defvar *doas* nil "If t, this will use doas to run sndioctl.")
2626

27-
(defun call-sndioctl (args captive)
27+
(defun call-sndioctl (args)
2828
"Calls sndioctl with the required arguments."
2929
(run-shell-command
3030
(format nil "~asndioctl ~a" (if *doas* "doas " "") args)
31-
captive))
32-
33-
(defun make-step-cmd (direction)
34-
"Helpful wrapper for generating command strings for incrementing/decrementing volume."
35-
(cond
36-
((string= "+" direction)
37-
(format nil "-q output.level=+~a" *step*))
38-
((string= "-" direction)
39-
(format nil "-q output.level=-~a" *step*))
40-
(t (error "Not a valid step direction!"))))
31+
t))
4132

4233
(defun get-mute-state-string ()
4334
"Returns a nicely formatted string containing the current mute state."
44-
(let ((state (aref (call-sndioctl "-n output.mute" t) 0)))
45-
(cond
46-
((string= "0" state)
47-
(format nil "Muted: No"))
48-
((string= "1" state)
49-
(format nil "Muted: Yes"))
50-
(t (error "Unknown output from sndioctl -n output.mute")))))
35+
(let* ((code-to-str
36+
(lambda (c)
37+
(cond
38+
((string= "0" c) "No")
39+
((string= "1" c) "Yes")
40+
(t "Err"))))
41+
(get-state
42+
(lambda (key)
43+
(handler-case
44+
(funcall code-to-str
45+
(aref (call-sndioctl (format nil "-n ~a" key)) 0))
46+
(error (c) (declare (ignore c)) "N/A"))
47+
))
48+
(in-state (funcall get-state "input.mute"))
49+
(out-state (funcall get-state "output.mute")))
50+
(format nil "Muted in: ~a Muted out: ~a" in-state out-state)))
5151

5252
(defun get-volume-level-string ()
5353
"Returns a nicely formatted string containing the current volume level."
54-
(let ((level
55-
(with-input-from-string (vol-str (call-sndioctl "-n output.level" t))
56-
(read vol-str))))
57-
(format nil "Volume: ~2$%" (* 100.0 level))))
54+
(let* ((get-volume
55+
(lambda (key)
56+
(handler-case
57+
(format nil "~2$%"
58+
(* 100
59+
(with-input-from-string
60+
(vol-str (call-sndioctl (format nil "-n ~a" key)))
61+
(read vol-str))))
62+
(error (c) (declare (ignore c)) "N/A"))))
63+
(in-volume (funcall get-volume "input.level"))
64+
(out-volume (funcall get-volume "output.level")))
65+
(format nil "Volume in: ~a Volume out: ~a" in-volume out-volume)))
5866

5967
(defun sndioctl-status-message ()
6068
"Returns a string indicating currrent sndioctl state."
6169
(format nil "~a ~a" (get-volume-level-string) (get-mute-state-string)))
6270

6371
(defcommand volume-up () ()
6472
"Volume goes up"
65-
(call-sndioctl (make-step-cmd "+") nil)
73+
(call-sndioctl (format nil "-q input.level=+~a" *step*))
74+
(call-sndioctl (format nil "-q output.level=+~a" *step*))
6675
(if (not *terse*)
6776
(message (sndioctl-status-message))))
6877

6978
(defcommand volume-down () ()
7079
"Volume goes down"
71-
(call-sndioctl (make-step-cmd "-") nil)
80+
(call-sndioctl (format nil "-q input.level=-~a" *step*))
81+
(call-sndioctl (format nil "-q output.level=-~a" *step*))
7282
(if (not *terse*)
7383
(message (sndioctl-status-message))))
7484

7585
(defcommand toggle-mute () ()
7686
"Toggles mute"
77-
(call-sndioctl "-q output.mute=!" nil)
87+
(call-sndioctl "-n input.mute=!")
88+
(call-sndioctl "-n output.mute=!")
7889
(if (not *terse*)
7990
(message (sndioctl-status-message))))
8091

8192
(defcommand set-mute () ()
8293
"Force sets mute to ON"
83-
(call-sndioctl "-q output.mute=1" nil)
94+
(call-sndioctl "-q input.mute=1")
95+
(call-sndioctl "-q output.mute=1")
8496
(if (not *terse*)
8597
(message (sndioctl-status-message))))
8698

8799
(defcommand unset-mute () ()
88100
"Force sets mute to OFF"
89-
(call-sndioctl "-q output.mute=0" nil)
101+
(call-sndioctl "-q inout.mute=0")
102+
(call-sndioctl "-q output.mute=0")
90103
(if (not *terse*)
91104
(message (sndioctl-status-message))))

0 commit comments

Comments
 (0)