|
| 1 | +--[[ Authors: Pavel Karnaukhov <[email protected]> |
| 2 | +License: GPLv3 |
| 3 | +Shows volume using wpctl - WirePlumber, PipeWire session manager |
| 4 | +
|
| 5 | +Add to template section of your cfg_statusbar.lua: |
| 6 | +%wpvol_volume -- shows volume of @DEFAULT_SINK@ in % |
| 7 | +%wpvol_state -- shows 'M' if muted and ' ' if unmuted |
| 8 | +
|
| 9 | +You can edit device name and update interval in local defaults |
| 10 | +]] |
| 11 | + |
| 12 | + |
| 13 | +local defaults = { |
| 14 | + update_interval = 1*1000, |
| 15 | + important = 33, |
| 16 | + critical = 66, |
| 17 | + device = '@DEFAULT_SINK@', |
| 18 | + unknown = '?' |
| 19 | +} |
| 20 | + |
| 21 | +local settings = table.join(statusd.get_config('wpvol'), defaults) |
| 22 | + |
| 23 | +local function get_volume() |
| 24 | + local f=io.popen('wpctl get-volume '..settings.device) |
| 25 | + local s=f:read('*all') |
| 26 | + f:close() |
| 27 | + local _, _, volume, state = string.find(s, 'Volume: ([0-9].[0-9]*)(.*)') |
| 28 | + |
| 29 | + if not volume then |
| 30 | + return settings.unknown, settings.unknown, critical, critical |
| 31 | + else |
| 32 | + volume = math.floor(volume * 100) |
| 33 | + if volume >= 0 and volume < settings.important then |
| 34 | + volume_hint = 'normal' |
| 35 | + elseif volume >= settings.important and volume < settings.critical then |
| 36 | + volume_hint = 'important' |
| 37 | + elseif volume >= settings.critical then |
| 38 | + volume_hint = 'critical' |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + if state == ' [MUTED]\n' then |
| 43 | + state = 'M' |
| 44 | + state_hint = 'critical' |
| 45 | + else |
| 46 | + state = '.' |
| 47 | + state_hint = 'normal' |
| 48 | + end |
| 49 | + |
| 50 | + return volume, state, volume_hint, state_hint |
| 51 | +end |
| 52 | + |
| 53 | + |
| 54 | +local function inform(key, value) |
| 55 | + statusd.inform('wpvol_'..key, value) |
| 56 | +end |
| 57 | + |
| 58 | + |
| 59 | +local volume_timer = statusd.create_timer() |
| 60 | + |
| 61 | +local function update_volume() |
| 62 | + local volume, state, volume_hint, state_hint = get_volume() |
| 63 | + inform('volume', volume..'%') |
| 64 | + inform('state', state) |
| 65 | + inform('volume_hint', volume_hint) |
| 66 | + inform('state_hint', state_hint) |
| 67 | + |
| 68 | + -- update interval |
| 69 | + volume_timer:set(settings.update_interval, update_volume) |
| 70 | +end |
| 71 | + |
| 72 | +update_volume() |
0 commit comments