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 --- bindings.lua | 1 + utils.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ workspace.lua | 1 + 3 files changed, 56 insertions(+) diff --git a/bindings.lua b/bindings.lua index 8aa9d48..ebaf864 100644 --- a/bindings.lua +++ b/bindings.lua @@ -35,6 +35,7 @@ function M.create(workspace) awful.key({ modkey }, "Prior", function () utils.vol_control(1) end), awful.key({ modkey }, "Next", function () utils.vol_control(-1) end), awful.key({ modkey }, "End", utils.vol_mute_toggle), + awful.key({ modkey }, "Delete", function () utils.auto_mute_toggle(workspace.notify_tbl, 'auto_mute') end), awful.key({ modkey }, "grave", function () awful.spawn("mpc toggle") end), awful.key({ modkey }, "b", function () awful.spawn("mpc next") end), awful.key({ modkey }, "p", function () awful.spawn("mpc prev") end), 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 diff --git a/workspace.lua b/workspace.lua index db9973a..732caef 100644 --- a/workspace.lua +++ b/workspace.lua @@ -195,6 +195,7 @@ function Workspace:new(layouts) o.desktops = {} o.screen_state = {} o.layouts = layouts + o.notify_tbl = {} o.signals = object() -- cgit v1.2.3