From 20b58f4d603c55b1aba42cc375a97b8708f725ba Mon Sep 17 00:00:00 2001 From: Amin Bandali Date: Sun, 8 Jan 2023 10:28:25 -0500 Subject: [PATCH] * .emacs.d/init.el: Show system volumes in mode-line. Uses pamixer to get or set the output (speakers or headphones) and input (microphone) volumes. --- .emacs.d/init.el | 55 +++++++++++++++++++++++++++++++++++ .emacs.d/lisp/bandali-exwm.el | 47 +++++++++++++++++++++++++----- 2 files changed, 95 insertions(+), 7 deletions(-) diff --git a/.emacs.d/init.el b/.emacs.d/init.el index d9ec432..94f1ccb 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -210,6 +210,61 @@ (setq battery-mode-line-format " [%p%% %t]") (display-battery-mode)) +(progn ; display system volume in mode-line + (defun b/get-volume-level (&optional mic) + "Returns the current system sound volume. +If MIC is non-nil, it returns the microphone volume instead." + (string-to-number + (string-trim + (shell-command-to-string + (mapconcat + #'identity + `("pamixer" ,(when mic "--default-source") "--get-volume") + " "))))) + + (defun b/set-volume-level (level &optional mic) + "Set the system sound volume to LEVEL. +If MIC is non-nil, set the volume level for the microphone +instead." + (let ((v (if mic 'b/volume-level-mic 'b/volume-level))) + (eval `(setq ,v ,level)))) + + (defun b/get-volume-mute (&optional mic) + "Returns t if system sound is currently muted. +If MIC is non-nil, it instead returns t if the microphone is +muted." + (string= + "true" + (string-trim + (shell-command-to-string + (mapconcat + #'identity + `("pamixer" ,(when mic "--default-source") "--get-mute") + " "))))) + + (defun b/set-volume-mute (mute &optional mic) + "Set the system sound mutedness to MUTE. +If MIC is non-nil, set the mutedness for the microphone instead." + (let ((v (if mic 'b/volume-mute-mic 'b/volume-mute))) + (eval `(setq ,v ,mute)))) + + (defvar b/volume-level (b/get-volume-level)) + (defvar b/volume-mute (b/get-volume-mute)) + (defvar b/volume-level-mic (b/get-volume-level 'mic)) + (defvar b/volume-mute-mic (b/get-volume-mute 'mic)) + + (setq-default + mode-line-format + (append + mode-line-format + '((:eval + (format + " [%s%%%%%s %s%%%%%s]" + (number-to-string b/volume-level) + (if b/volume-mute "-" "+") + (number-to-string b/volume-level-mic) + (if b/volume-mute-mic "-" "+"))))))) + ;; (with-eval-after-load 'fringe ;; ;; smaller fringe ;; (fringe-mode '(3 . 1))) diff --git a/.emacs.d/lisp/bandali-exwm.el b/.emacs.d/lisp/bandali-exwm.el index 3072765..4cd2d1c 100644 --- a/.emacs.d/lisp/bandali-exwm.el +++ b/.emacs.d/lisp/bandali-exwm.el @@ -181,30 +181,63 @@ around if needed." ([?\s-\;] . (lambda () (interactive) (start-process-shell-command - "dmneu-pamixer" nil "dmenu-pamixer"))) + "dmneu-pamixer" nil "dmenu-pamixer") + (b/set-volume-level (b/get-volume-level)) + (force-mode-line-update))) ([XF86AudioMute] . ; borken on my X200 :-( (lambda () (interactive) - (start-process "" nil "pamixer" "--toggle-mute"))) + (start-process "" nil "pamixer" "--toggle-mute") + (b/set-volume-mute (b/get-volume-mute)) + (force-mode-line-update))) + ([\s-XF86AudioMute] . ; toggle mic mute + (lambda () + (interactive) + (start-process + "" nil "pamixer" "--default-source" "--toggle-mute") + (b/set-volume-mute (b/get-volume-mute 'mic) 'mic) + (force-mode-line-update))) ([XF86Launch1] . (lambda () (interactive) - (start-process "" nil "pamixer" "--toggle-mute"))) + (start-process "" nil "pamixer" "--toggle-mute") + (b/set-volume-mute (b/get-volume-mute)) + (force-mode-line-update))) ([\s-XF86Launch1] . ; toggle mic mute (lambda () (interactive) (start-process - "" nil "pamixer" "--default-source" "--toggle-mute"))) + "" nil "pamixer" "--default-source" "--toggle-mute") + (b/set-volume-mute (b/get-volume-mute 'mic) 'mic) + (force-mode-line-update))) ([XF86AudioLowerVolume] . (lambda () (interactive) (start-process - "" nil "pamixer" "--allow-boost" "--decrease" "5"))) + "" nil "pamixer" "--allow-boost" "--decrease" "5") + (b/set-volume-level (b/get-volume-level)) + (force-mode-line-update))) ([XF86AudioRaiseVolume] . (lambda () (interactive) (start-process - "" nil "pamixer" "--allow-boost" "--increase" "5"))) + "" nil "pamixer" "--allow-boost" "--increase" "5") + (b/set-volume-level (b/get-volume-level)) + (force-mode-line-update))) + ([\s-XF86AudioLowerVolume] . + (lambda () + (interactive) + (start-process + "" nil "pamixer" "--default-source" "--decrease" "5") + (b/set-volume-level (b/get-volume-level 'mic) 'mic) + (force-mode-line-update))) + ([\s-XF86AudioRaiseVolume] . + (lambda () + (interactive) + (start-process + "" nil "pamixer" "--default-source" "--increase" "5") + (b/set-volume-level (b/get-volume-level 'mic) 'mic) + (force-mode-line-update))) ([XF86AudioPlay] . (lambda () (interactive) @@ -319,7 +352,7 @@ around if needed." '((:eval (format " [%s]" (number-to-string - exwm-workspace-current-index))))))) + exwm-workspace-current-index))))))) (with-eval-after-load 'exwm-layout (setq exwm-layout-show-all-buffers t)) -- 2.20.1