From: Amin Bandali Date: Mon, 9 Jan 2023 06:49:21 +0000 (-0500) Subject: Clean up the system volume in mode-line display code X-Git-Url: https://git.shemshak.org/~bandali/configs/commitdiff_plain/81b33a828705837b380db3147fbff3e313da4e27?hp=20b58f4d603c55b1aba42cc375a97b8708f725ba Clean up the system volume in mode-line display code --- diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 94f1ccb..ad9b091 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -1,6 +1,6 @@ ;;; init.el --- bandali's emacs configuration -*- lexical-binding: t -*- -;; Copyright (C) 2018-2022 Amin Bandali +;; Copyright (c) 2018-2023 Amin Bandali ;; This program is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by @@ -211,47 +211,53 @@ (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)) + (defvar b/volume-level 0) + (defvar b/volume-mute nil) + (defvar b/volume-level-mic 0) + (defvar b/volume-mute-mic nil) + + (defun b/volume-get (&optional use-default-source) + "Get the default sink volume and mute state. +If USE-DEFAULT-SOURCE is non-nill, use the default source (e.g. a +microphone) instead of the default sink." + (pcase (split-string ; expecting: (e.g. true 15) + (string-trim + (shell-command-to-string + (mapconcat + #'identity + `("pamixer" + ,(when use-default-source "--default-source") + "--get-volume" + "--get-mute") + " ")))) + (`(,m ,v) + (let ((mute (string= "true" m)) + (volume (string-to-number v))) + `(,mute ,volume))))) + + (defun b/volume-update () + "Update system sound volume as displayed in mode-line." + (let ((changed)) + (pcase (b/volume-get) + (`(,mute ,volume) + (unless (eq mute b/volume-mute) + (setq b/volume-mute mute + changed t)) + (unless (= volume b/volume-level) + (setq b/volume-level volume + changed t)))) + (pcase (b/volume-get 'mic) + (`(,mute ,volume) + (unless (eq mute b/volume-mute-mic) + (setq b/volume-mute-mic mute + changed t)) + (unless (= volume b/volume-level-mic) + (setq b/volume-level-mic volume + changed t)))) + (when changed + (force-mode-line-update)))) + + (defvar b/volume-timer (run-at-time nil 5 #'b/volume-update)) (setq-default mode-line-format diff --git a/.emacs.d/lisp/bandali-exwm.el b/.emacs.d/lisp/bandali-exwm.el index 4cd2d1c..e7f83ce 100644 --- a/.emacs.d/lisp/bandali-exwm.el +++ b/.emacs.d/lisp/bandali-exwm.el @@ -1,6 +1,6 @@ ;;; bandali-exwm.el --- bandali's EXWM configuration -*- lexical-binding: t; -*- -;; Copyright (C) 2018-2022 Amin Bandali +;; Copyright (c) 2018-2023 Amin Bandali ;; Author: Amin Bandali ;; Keywords: tools @@ -182,62 +182,53 @@ around if needed." (interactive) (start-process-shell-command "dmneu-pamixer" nil "dmenu-pamixer") - (b/set-volume-level (b/get-volume-level)) - (force-mode-line-update))) + (b/volume-update))) ([XF86AudioMute] . ; borken on my X200 :-( (lambda () (interactive) (start-process "" nil "pamixer" "--toggle-mute") - (b/set-volume-mute (b/get-volume-mute)) - (force-mode-line-update))) + (b/volume-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))) + (b/volume-update))) ([XF86Launch1] . (lambda () (interactive) (start-process "" nil "pamixer" "--toggle-mute") - (b/set-volume-mute (b/get-volume-mute)) - (force-mode-line-update))) + (b/volume-update))) ([\s-XF86Launch1] . ; 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))) + (b/volume-update))) ([XF86AudioLowerVolume] . (lambda () (interactive) (start-process "" nil "pamixer" "--allow-boost" "--decrease" "5") - (b/set-volume-level (b/get-volume-level)) - (force-mode-line-update))) + (b/volume-update))) ([XF86AudioRaiseVolume] . (lambda () (interactive) (start-process "" nil "pamixer" "--allow-boost" "--increase" "5") - (b/set-volume-level (b/get-volume-level)) - (force-mode-line-update))) + (b/volume-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))) + (b/volume-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))) + (b/volume-update))) ([XF86AudioPlay] . (lambda () (interactive)