diff options
author | Anton Khirnov <anton@khirnov.net> | 2021-10-26 21:05:31 +0200 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2021-10-26 21:05:31 +0200 |
commit | 43470e3b516b49e7bdb7559e9ed0af0809d16212 (patch) | |
tree | 831733644250aeb53110e00b38934c2a37fc8636 /utils.lua | |
parent | 3d1e5438ada61e406de6dccc0b44e7491bf6561d (diff) |
bindings: add a binding for toggling auto-mute
Diffstat (limited to 'utils.lua')
-rw-r--r-- | utils.lua | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -40,6 +40,60 @@ function M.vol_mute_toggle() awful.spawn("amixer -q set Master toggle") end +function M.auto_mute_toggle(notify_tbl, notify_tbl_key) + local control = "'Auto-Mute Mode'" + awful.spawn.easy_async("amixer sget " .. control, + function(stdout, stderr, exitreason, exitcode) + if exitcode ~= 0 then + naughty.notify({ preset = naughty.config.presets.warn, + title = "Error getting the auto-mute mode", + text = stderr }) + return + end + + -- parse valid auto-mute items + local items_raw = string.match(stdout, "\n%s*Items:%s*([^\n]+)\n") + local items = {} + for item in string.gmatch(items_raw, "'[^']+'") do + table.insert(items, item) + end + + -- parse current item + local cur_item = string.match(stdout, "\n%s*Item0:%s*('[^'\n]+')\n") + + -- find the next item + local next_item = nil + for i, v in ipairs(items) do + if v == cur_item then + next_item = items[(i % #items) + 1] + break + end + end + if next_item == nil then return end + + -- set the next item + awful.spawn.easy_async('amixer -q sset ' .. control .. ' ' .. next_item, + function(stdout, stderr, exitreason, exitcode) + if exitcode ~= 0 then + naughty.notify({ preset = naughty.config.presets.warn, + title = "Error setting the auto-mute mode", + text = stderr }) + return + end + + local args = { title = "Auto-mute", text = next_item } + + local prev_notify = notify_tbl[notify_tbl_key] + if prev_notify and prev_notify.box.visible then + args.replaces_id = prev_notify.id + end + + notify_tbl[notify_tbl_key] = naughty.notify(args) + end + ) + end) +end + function M.spawn_current(command) awful.spawn(command, {tag = mouse.screen.selected_tag}) end |