diff options
| -rwxr-xr-x | vol | 121 |
1 files changed, 64 insertions, 57 deletions
@@ -5,7 +5,7 @@ # (c) 2020 B. Watson <urchlay@slackware.uk> # Released under the WTFPL, see http://www.wtfpl.net/txt/copying/ -# Requires xosd. +# Requires aosd_cat (on Slackware, libaosd from SBo). # Intended for use with xbindkeys, like so: #$ cat ~/.xbindkeysrc @@ -20,15 +20,11 @@ # "Next" and "Prior" are PageDown and PageUp. Replace with "Up" and "Down" # to use arrow keys. If you have multimedia keys, you can probably use -# them. See xbindkeys(1). +# them (try XF86AudioRaiseVolume, XF86AudioLowerVolume, and +# XF86AudioMute). See xbindkeys(1). # Can also be used from the command line. Run with no args for help. -### TODO: -# aosd_cat -R LightGreen -n 'Normal 40' -p 7 -t 0 -# looks promising (the "40" is points; could be "40px" instead). -# Font string spec: https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html - # -------------------- # Configurable options # -------------------- @@ -38,33 +34,23 @@ CHANNEL=Master # Each up/down adjustment is by this much. Use the dB suffix if you -# prefer decibels, % for percentage, or leave it off for raw units. +# prefer decibels, % percentage, no suffix for raw units (on one of my +# sound cards, the raw unit range is 0 to 87; on another, 0-127). ADJ=1% -# osd_cat seems a bit picky about what fonts it'll use. If you give a -# nonexistent font, it won't run at all. If you give a font it "doesn't -# like", you'll see the percentage bar, but no "Volume" text. -FONT=12x24 +# Pango font string, see: +# https://docs.gtk.org/Pango/type_func.FontDescription.from_string.html +# The size is in points; 30px would be pixels. +FONT="Mono Normal 30" -# What color is the OSD bar and text? See /usr/share/X11/rgb.txt. +# What color is the OSD bar and text? See /usr/share/X11/rgb.txt, +# or use hex escapes (with #). # COLOR is for when we're unmuted, MUTECOLOR is for muted. -COLOR=green -MUTECOLOR=red - -# Text drop shadow, 0 to disable. -SHADOW=3 - -# Where does the OSD show up on screen (vert)? One of: top middle bottom -POSITION=bottom - -# Where does the OSD show up on the screen (horiz)? One of: left right center -ALIGN=center - -# Where does the OSD show up (offset from top or bottom of screen)? -OFFSET=40 +COLOR='#00ff00' +MUTECOLOR='#ff0000' -# How long does the OSD persist? In seconds, integer only. -DELAY=2 +# How long does the OSD persist? In milliseconds. +DELAY=2000 # amixer options. Could use -c, -D here. Don't use -q. # default is "-M", see amixer(1). @@ -78,23 +64,43 @@ SELF="$( basename $0 )" PIDFILE="$HOME/.$SELF.osd.pid" -# osd() will kill any previously-spawned osd_cat process. Ideally this +BLOCKS="██████████████████████████████████████████████████" +BLANKS="──────────────────────────────────────────────────" +PARTIAL="▌" + +# progress() prints a UTF-8 progress bar with 50 characters. +# takes a percentage, 0 to 100 +progress() { + local text="$1" + local count="$2" + local solidblocks + local partial + local trailing + + solidblocks="$(( $count / 2 ))" + partial="$(( $count % 2 ))" + trailing="$(( 50 - ( $solidblocks + $partial) ))" + + echo -n "$text${BLOCKS:0:$solidblocks}" + [ "$partial" = "1" ] && echo -n $PARTIAL + echo -n "${BLANKS:0:$trailing}" +} + +# osd() will kill any previously-spawned aosd_cat process. Ideally this # means rapid multiple keypresses won't "step on" each other. In practice # it seems to work OK, but if you press the key really fast on a slow # system, or hold it down with your key-repeat rate cranked up (on any # system), the OSD bar will never get a chance to display. - osd() { local got="$( amixer $AMIXER_OPTS get $CHANNEL | tail -1 )" local muted="$( echo "$got" | grep '\[off\]$' )" local volpct="$( echo "$got" | cut -d'[' -f2 | cut -d% -f1 )" local db="$( echo "$got" | cut -d'[' -f3 | cut -d']' -f1 )" - local text="Volume $db ($volpct%)" local color="$COLOR" + local text="$( printf "%8s" $db) $( printf "%-3s" $volpct% ) " local oldpid if [ -n "$muted" ]; then - text="$text [Muted]" color="$MUTECOLOR" fi @@ -102,20 +108,29 @@ osd() { # files whose PIDs have been recycled. if [ -e $PIDFILE ]; then oldpid="$( cat $PIDFILE )" - grep -q '^osd_cat' /proc/$oldpid/cmdline 2>/dev/null && \ - kill "$oldpid" 2>/dev/null + grep -q '^aosd_cat' /proc/$oldpid/cmdline 2>/dev/null && \ + kill -9 "$oldpid" 2>/dev/null + rm -f $PIDFILE fi - osd_cat -b percentage \ - -o $OFFSET \ - -A $ALIGN \ - -d $DELAY \ - -P $volpct \ - -p $POSITION \ - -c $color \ - -s $SHADOW \ - -f $FONT \ - -T "$text" & + # TODO: make these variables in the config section rather than + # hardcoding them. + progress "$text" "$volpct" | \ + aosd_cat \ + --fore-color $color \ + --back-color '#808080' \ + --font "$FONT" \ + --output 1 \ + --x-offset 159 \ + --y-offset 0 \ + --position 7 \ + --transparency 1 \ + --back-opacity 128 \ + --padding 7 \ + --alignment 0 \ + --fade-in 0 \ + --fade-out 0 \ + --fade-full $DELAY & echo "$!" > $PIDFILE } @@ -134,17 +149,17 @@ mute() { usage() { cat <<EOF -$SELF [up|down|mute|<nnn>] +$SELF [up|down|mute|<nn>] -<nnn> is a numeric volume, possibly followed by "dB" and/or "+" or "-". - It will be passed to 'amixer $AMIXER_OPTS set $CHANNEL' as-is. +<nn> is a numeric volume, possibly followed by "dB" or "%", and/or + "+" or "-". It will be passed to 'amixer $AMIXER_OPTS set $CHANNEL' as-is. EOF exit 1 } check_deps() { local missing="no" - for dep in osd_cat amixer; do + for dep in aosd_cat amixer; do if ! type -p "$dep" > /dev/null; then echo "$SELF: missing required executable '$dep'" 1>&2 missing="yes" @@ -170,11 +185,3 @@ esac osd exit 0 - -# For reference: -#$ amixer get Master -#Simple mixer control 'Master',0 -# Capabilities: pvolume pvolume-joined pswitch pswitch-joined -# Playback channels: Mono -# Limits: Playback 0 - 87 -# Mono: Playback 31 [36%] [-42.00dB] [on] |
