From f9728c117d5c6bebb8bbf917fad07b0fb8343416 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Sun, 11 Dec 2022 12:55:18 +0100 Subject: Extract volume script into separate file --- home-configuration.scm | 65 +------------------------------------------------- volume | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 64 deletions(-) create mode 100755 volume diff --git a/home-configuration.scm b/home-configuration.scm index b00b08be..4acc4f8c 100644 --- a/home-configuration.scm +++ b/home-configuration.scm @@ -325,70 +325,7 @@ replacement spec (to which `regexp-substitute/global' is applied)." ;; With #:recursive? #t, Guix keeps the files' permission bits, i.e. makes them executable. (".local/bin/sessionmenu" ,(local-file "sessionmenu" #:recursive? #t)) ; rofi logout/reboot menu (".local/bin/passmenu" ,(local-file "passmenu" #:recursive? #t)) ; rofi passwords menu - (".local/bin/volume" - ,(program-file - "volume" - #~(begin - (use-modules (ice-9 format) - (ice-9 match) - (ice-9 popen) - (ice-9 regex) - (ice-9 textual-ports)) - - (define (read-from-pactl . args) - (let* ((port (apply open-pipe* OPEN_READ - #$(package-binary "pulseaudio" "pactl") args)) - (output (get-string-all port))) - (close-pipe port) - (string-trim-right output #\newline))) - - (define %default-sink - (read-from-pactl "get-default-sink")) - - (define* (sink-muted? #:optional (sink %default-sink)) - (match (read-from-pactl "get-sink-mute" sink) - ("Mute: yes" #t) - ("Mute: no" #f) - (output (error "Unknown `pactl get-sink-mute' output" output)))) - - (define* (sink-volume #:optional (sink %default-sink)) - (match-let ((`#(,match (,start . ,end)) - (string-match "[0-9]+%" (read-from-pactl "get-sink-volume" sink)))) - (string->number (substring match start (1- end))))) ; trim trailing "%" - - (define* (set-sink-mute! #:optional (state "toggle") (sink %default-sink)) - (status:exit-val - (system* #$(package-binary "pulseaudio" "pactl") "set-sink-mute" sink state))) - - (define* (increment-sink-volume! increment-percent #:optional (sink %default-sink)) - (status:exit-val - (system* #$(package-binary "pulseaudio" "pactl") - "set-sink-volume" sink - ;; A percentage with a sign is an increment. - (format #f "~@d%" increment-percent)))) - - (define* (notify #:optional (muted? (sink-muted?)) (volume (sink-volume))) - (status:exit-val - (system* #$(package-binary "dunst" "dunstify") - "-a" "volume" "-u" "low" - "-i" (cond (muted? "audio-volume-muted") - ((< volume 50) "audio-volume-low") - (#t "audio-volume-high")) - "-h" "string:x-canonical-private-synchronous:volume" - "-h" (format #f "int:value:~d" (if muted? 0 volume)) - (if muted? "Volume muted" "Volume")))) - - (match (cdr (program-arguments)) - (() 0) ; notify only - (("mute") (set-sink-mute! "true")) - (("unmute") (set-sink-mute! "false")) - (("toggle-mute") (set-sink-mute! "toggle")) - (((? string->number increment-percent)) - (and (set-sink-mute! "false") - (increment-sink-volume! - (string->number increment-percent)))) - (args (error "Could not parse command-line arguments:" args))) - (notify)))))))) + (".local/bin/volume" ,(local-file "volume" #:recursive? #t)))))) ; set volume on key press (home-environment (packages diff --git a/volume b/volume new file mode 100755 index 00000000..96398553 --- /dev/null +++ b/volume @@ -0,0 +1,60 @@ +#!/usr/bin/env -S guile --no-auto-compile +!# +(use-modules (ice-9 format) + (ice-9 match) + (ice-9 popen) + (ice-9 regex) + (ice-9 textual-ports)) + +(define (read-from-pactl . args) + (let* ((port (apply open-pipe* OPEN_READ "pactl" args)) + (output (get-string-all port))) + (close-pipe port) + (string-trim-right output #\newline))) + +(define %default-sink + (read-from-pactl "get-default-sink")) + +(define* (sink-muted? #:optional (sink %default-sink)) + (match (read-from-pactl "get-sink-mute" sink) + ("Mute: yes" #t) + ("Mute: no" #f) + (output (error "Unknown `pactl get-sink-mute' output" output)))) + +(define* (sink-volume #:optional (sink %default-sink)) + (match-let ((`#(,match (,start . ,end)) + (string-match "[0-9]+%" (read-from-pactl "get-sink-volume" sink)))) + (string->number (substring match start (1- end))))) ; trim trailing "%" + +(define* (set-sink-mute! #:optional (state "toggle") (sink %default-sink)) + (status:exit-val + (system* "pactl" "set-sink-mute" sink state))) + +(define* (increment-sink-volume! increment-percent #:optional (sink %default-sink)) + (status:exit-val + (system* "pactl" "set-sink-volume" sink + ;; A percentage with a sign is an increment. + (format #f "~@d%" increment-percent)))) + +(define* (notify #:optional (muted? (sink-muted?)) (volume (sink-volume))) + (status:exit-val + (system* "dunstify" "-a" "volume" "-u" "low" + "-i" (cond (muted? "audio-volume-muted") + ((< volume 50) "audio-volume-low") + (#t "audio-volume-high")) + "-h" "string:x-canonical-private-synchronous:volume" + "-h" (format #f "int:value:~d" (if muted? 0 volume)) + (if muted? "Volume muted" "Volume")))) + +(match (cdr (program-arguments)) + (() 0) ; notify only + (("mute") (set-sink-mute! "true")) + (("unmute") (set-sink-mute! "false")) + (("toggle-mute") (set-sink-mute! "toggle")) + (((? string->number increment-percent)) + (and (set-sink-mute! "false") + (increment-sink-volume! + (string->number increment-percent)))) + (args (error "Could not parse command-line arguments:" args))) + +(notify) -- cgit v1.2.3