From 22a270eba9568af1394a46429f1a379200496cf9 Mon Sep 17 00:00:00 2001 From: Amin Bandali Date: Mon, 1 Apr 2024 15:21:11 -0400 Subject: Break pulse volume code out of bar and into its own script In part so that it could easily be used in Emacs as well. Also rename pacify-pulseaudio to b-pacify (as in PAcify). --- .config/sway/config | 4 +- .emacs.d/init.el | 113 ++++--------------------------------------- .local/bin/b-bar | 13 +++++ .local/bin/b-pacify | 28 +++++++++++ .local/bin/b-pavols | 23 +++++++++ .local/bin/my-bar | 20 -------- .local/bin/pacify-pulseaudio | 28 ----------- 7 files changed, 76 insertions(+), 153 deletions(-) create mode 100755 .local/bin/b-bar create mode 100755 .local/bin/b-pacify create mode 100755 .local/bin/b-pavols delete mode 100755 .local/bin/my-bar delete mode 100755 .local/bin/pacify-pulseaudio diff --git a/.config/sway/config b/.config/sway/config index 5e1334f..c251a25 100644 --- a/.config/sway/config +++ b/.config/sway/config @@ -155,7 +155,7 @@ bar { position bottom # status_command while date +'%a, %d %b %Y %T %z'; do sleep 1; done - status_command my-bar + status_command b-bar colors { statusline #ffffff @@ -191,7 +191,7 @@ for_window [app_id="firefox" title="Sharing Indicator"] \ exec mako >/tmp/mako.log 2>&1 exec dbus-update-activation-environment DISPLAY WAYLAND_DISPLAY SWAYSOCK exec gsettings set 'org.gnome.desktop.interface' cursor-theme 'DMZ-White' -exec pacify-pulseaudio +exec b-pacify exec [ -x "$(command -v nm-applet)" ] && nm-applet include ~/.config/sway/"$(hostname)" diff --git a/.emacs.d/init.el b/.emacs.d/init.el index 9286e0c..f4bb031 100644 --- a/.emacs.d/init.el +++ b/.emacs.d/init.el @@ -219,66 +219,16 @@ plain variables. This means that `setopt' will execute any (setopt battery-mode-line-format " [%b%p%% %t]") (display-battery-mode)) -(progn ; display system volume in mode-line - (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 +(progn ; display system volume and mute states in mode-line + (defsubst b/pavols () + "Get volume and mute state of default pulseaudio sink and source." + ;; , + ;; e.g. 46+,89- + (string-trim (shell-command-to-string "b-pavols"))) + (setopt 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 "-" "+"))))))) + `(,@mode-line-format + (:eval (format " [%s]" (b/pavols)))))) ;; (with-eval-after-load 'fringe ;; ;; smaller fringe @@ -502,42 +452,6 @@ for all frames." (completing-read "Find recent file: " recentf-list))) (b/keymap-global-set "C-c f r" #'b/recentf-open)) -;; (define-key minibuffer-local-completion-map -;; "\t" #'minibuffer-force-complete) - -;; (with-eval-after-load 'icomplete - -;; (setq icomplete-on-del-error-function #'abort-recursive-edit) - -;; (defun b/icomplete-fido-backward-updir () -;; "Delete char before or go up directory, like `ido-mode'." -;; (interactive) -;; (if (and (eq (char-before) ?/) -;; (eq (icomplete--category) 'file)) -;; (save-excursion -;; (goto-char (1- (point))) -;; (when (search-backward "/" (point-min) t) -;; (delete-region (1+ (point)) (point-max)))) -;; (condition-case nil -;; (call-interactively #'delete-backward-char) -;; (error -;; (when icomplete-on-del-error-function -;; (funcall icomplete-on-del-error-function)))))) - -;; (define-key icomplete-fido-mode-map -;; (kbd "DEL") #'b/icomplete-fido-backward-updir)) - -;; (fido-mode 1) -;; (defun b/icomplete--fido-mode-setup () -;; "Customizations to `fido-mode''s minibuffer." -;; (when (and icomplete-mode (icomplete-simple-completing-p)) -;; (setq-local -;; ;; icomplete-compute-delay 0.1 -;; ;; icomplete-hide-common-prefix t -;; icomplete-separator " ยท " -;; completion-styles '(basic substring partial-completion flex)))) -;; (add-hook 'minibuffer-setup-hook #'b/icomplete--fido-mode-setup 1) - (with-eval-after-load 'eshell (setopt eshell-hist-ignoredups t @@ -702,13 +616,6 @@ for all frames." (b/keymap-set m "b" #'help-go-back) (b/keymap-set m "f" #'help-go-forward))) -(with-eval-after-load 'tramp - (setopt tramp-auto-save-directory (b/emacs.d "tramp-auto-save/")) - (add-to-list 'tramp-default-proxies-alist '(nil "\\`root\\'" "/ssh:%h:")) - (add-to-list 'tramp-default-proxies-alist '("localhost" nil nil)) - (add-to-list 'tramp-default-proxies-alist - (list (regexp-quote (system-name)) nil nil))) - (with-eval-after-load 'doc-view (b/keymap-set doc-view-mode-map "M-RET" #'image-previous-line)) @@ -1121,7 +1028,7 @@ for all frames." (setopt message-elide-ellipsis "[...]\n" - message-citation-line-format "%N writes:\n" + message-citation-line-format "%N wrote:\n" message-citation-line-function #'message-insert-formatted-citation-line message-confirm-send t diff --git a/.local/bin/b-bar b/.local/bin/b-bar new file mode 100755 index 0000000..e326673 --- /dev/null +++ b/.local/bin/b-bar @@ -0,0 +1,13 @@ +#!/bin/sh +while true +do + b0="/sys/class/power_supply/BAT0" + bat="$(cat $b0/capacity)" + # batl="$(cat $b0/capacity_level)" + vols="$(b-pavols)" + load="$(cat /proc/loadavg | cut -d' ' -f1)" + time="$(date +'%a, %d %b %Y %R %z')" + printf "%s%% | %s | %s | %s\n" \ + "$bat" "$vols" "$load" "$time" + sleep 5 +done diff --git a/.local/bin/b-pacify b/.local/bin/b-pacify new file mode 100755 index 0000000..18d6ee5 --- /dev/null +++ b/.local/bin/b-pacify @@ -0,0 +1,28 @@ +#!/bin/sh + +# Copyright (c) 2024 Amin Bandali +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. This file is offered as-is, +# without any warranty. + +# pulseaudio is trash and insists on using machine-id as a prefix for +# its database filenames under ~/.config/pulse. So, on systems with +# randomized machine-ids like Devuan GNU/Linux it shits the bed and +# can't find its own database files. So we do its job for it and +# rename its database files on each boot. Ditto for libcanberra's +# event sound cache database. + +cur_id_path=/var/lib/dbus/machine-id +prev_id_path="$XDG_CACHE_HOME/tmp-prevmid" +cur_id="$(cat $cur_id_path)" +prev_id="$(cat $prev_id_path)" + +for f in $XDG_CONFIG_HOME/pulse/$prev_id-* \ + $XDG_CACHE_HOME/event-sound-cache.tdb.$prev_id.*; do + fnew="$(echo $f | sed "s/$prev_id/$cur_id/")" + mv $f $fnew +done + +cp -p $cur_id_path $prev_id_path diff --git a/.local/bin/b-pavols b/.local/bin/b-pavols new file mode 100755 index 0000000..76606e2 --- /dev/null +++ b/.local/bin/b-pavols @@ -0,0 +1,23 @@ +#!/bin/sh + +# default sink (output) +sink_volume="$(pactl get-sink-volume @DEFAULT_SINK@ | grep -Po '\d+(?=%)' | head -n1)" +sink_mute="$(pactl get-sink-mute @DEFAULT_SINK@ | grep -o 'no\|yes')" +# default source (input) +source_volume="$(pactl get-source-volume @DEFAULT_SOURCE@ | grep -Po '\d+(?=%)' | head -n1)" +source_mute="$(pactl get-source-mute @DEFAULT_SOURCE@ | grep -o 'no\|yes')" + +if [ "$sink_mute" = "no" ]; then + sink_mute='+' +else + sink_mute='-' +fi + +if [ "$source_mute" = "no" ]; then + source_mute='+' +else + source_mute='-' +fi + +printf "%s%s,%s%s\n" \ + "$sink_volume" "$sink_mute" "$source_volume" "$source_mute" diff --git a/.local/bin/my-bar b/.local/bin/my-bar deleted file mode 100755 index 8a37d2f..0000000 --- a/.local/bin/my-bar +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -while true -do - b0="/sys/class/power_supply/BAT0" - bat="$(cat $b0/capacity)" - # batl="$(cat $b0/capacity_level)" - sinkv="$(pactl get-sink-volume @DEFAULT_SINK@ | grep -Po '\d+(?=%)' | head -n1)" - sinkm="$(pactl get-sink-mute @DEFAULT_SINK@ | grep -o 'no\|yes')" - srcv="$(pactl get-source-volume @DEFAULT_SOURCE@ | grep -Po '\d+(?=%)' | head -n1)" - srcm="$(pactl get-source-mute @DEFAULT_SOURCE@ | grep -o 'no\|yes')" - [ "$sinkm" = "no" ] && sinkm='+' || sinkm='-' - [ "$srcm" = "no" ] && srcm='+' || srcm='-' - load="$(cat /proc/loadavg | cut -d' ' -f1)" - time="$(date +'%a, %d %b %Y %R %z')" - printf "%s%% | %s%s,%s%s | %s | %s\n" \ - "$bat" \ - "$sinkv" "$sinkm" "$srcv" "$srcm" \ - "$load" "$time" - sleep 5 -done diff --git a/.local/bin/pacify-pulseaudio b/.local/bin/pacify-pulseaudio deleted file mode 100755 index 18d6ee5..0000000 --- a/.local/bin/pacify-pulseaudio +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh - -# Copyright (c) 2024 Amin Bandali -# -# Copying and distribution of this file, with or without modification, -# are permitted in any medium without royalty provided the copyright -# notice and this notice are preserved. This file is offered as-is, -# without any warranty. - -# pulseaudio is trash and insists on using machine-id as a prefix for -# its database filenames under ~/.config/pulse. So, on systems with -# randomized machine-ids like Devuan GNU/Linux it shits the bed and -# can't find its own database files. So we do its job for it and -# rename its database files on each boot. Ditto for libcanberra's -# event sound cache database. - -cur_id_path=/var/lib/dbus/machine-id -prev_id_path="$XDG_CACHE_HOME/tmp-prevmid" -cur_id="$(cat $cur_id_path)" -prev_id="$(cat $prev_id_path)" - -for f in $XDG_CONFIG_HOME/pulse/$prev_id-* \ - $XDG_CACHE_HOME/event-sound-cache.tdb.$prev_id.*; do - fnew="$(echo $f | sed "s/$prev_id/$cur_id/")" - mv $f $fnew -done - -cp -p $cur_id_path $prev_id_path -- cgit v1.2.3-60-g2f50