From 43470e3b516b49e7bdb7559e9ed0af0809d16212 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 26 Oct 2021 21:05:31 +0200 Subject: bindings: add a binding for toggling auto-mute --- utils.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'utils.lua') diff --git a/utils.lua b/utils.lua index 3a02c24..6fc40c5 100644 --- a/utils.lua +++ b/utils.lua @@ -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 -- cgit v1.2.3