summaryrefslogtreecommitdiff
path: root/utils.lua
diff options
context:
space:
mode:
Diffstat (limited to 'utils.lua')
-rw-r--r--utils.lua54
1 files changed, 54 insertions, 0 deletions
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