From b92b07f74745c3c01dda15cdcd1fc8eae51eef30 Mon Sep 17 00:00:00 2001 From: Timo Wilken Date: Fri, 21 Oct 2022 00:40:50 +0200 Subject: Handle volume keys in i3 --- home-configuration.scm | 90 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) (limited to 'home-configuration.scm') diff --git a/home-configuration.scm b/home-configuration.scm index 3d54e558..124dbf0f 100644 --- a/home-configuration.scm +++ b/home-configuration.scm @@ -263,14 +263,94 @@ replacement spec (to which `regexp-substitute/global' is applied)." ,(local-file #.(string-append "catppuccin/zathura/src/catppuccin-" catppuccin-theme-variant))))) - (service - home-files-service-type - `((".icons/default/index.theme" ,(local-file "cursors.ini")) - ;; https://sw.kovidgoyal.net/kitty/kittens/diff/ + (simple-service + 'scripts home-files-service-type + `(;; https://sw.kovidgoyal.net/kitty/kittens/diff/ (".local/bin/kdiff" ,(program-file "kdiff" #~(apply execl #$(package-binary "kitty") "kitty" "+kitten" "diff" (cdr (command-line))))) - ;; GnuPG config files must be in ~/.local/share/gnupg, not ~/.config, + (".local/bin/volume" + ,(program-file + "volume" + (with-imported-modules '((ice-9 format) + (ice-9 match) + (ice-9 popen) + (ice-9 regex) + (ice-9 textual-ports)) + #~(begin + (use-modules (ice-9 format) + (ice-9 match) + (ice-9 popen) + (ice-9 regex) + (ice-9 textual-ports)) + + (define (read-from-process . command) + (let* ((port (apply open-pipe* OPEN_READ command)) + (output (get-string-all port))) + (close-pipe port) + (string-trim-right output #\newline))) + + (define %default-sink + (read-from-process #$(package-binary "pulseaudio" "pactl") + "get-default-sink")) + + (define* (sink-muted? #:optional (sink %default-sink)) + (match (read-from-process #$(package-binary "pulseaudio" "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-process + #$(package-binary "pulseaudio" "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* "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")))) + + (define (main) + (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)) + + (main))))))) + + (simple-service + 'gui-files home-files-service-type + `((".icons/default/index.theme" ,(local-file "cursors.ini")))) + + (simple-service + 'terminal-files home-files-service-type + `(;; GnuPG config files must be in ~/.local/share/gnupg, not ~/.config, ;; so we can't use `home-xdg-configuration-files-service-type'. (".local/share/gnupg/gpg.conf" ,(local-file "gpg.conf")) (".local/share/gnupg/gpg-agent.conf" -- cgit v1.2.3