From 0999de2bfd2e9f7ccd5d0e04eb1f63c3803449ae Mon Sep 17 00:00:00 2001 From: streetturtle Date: Tue, 3 Sep 2019 21:57:24 -0400 Subject: externalize config of volume widget --- volume-widget/volume.lua | 110 +++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 51 deletions(-) (limited to 'volume-widget/volume.lua') diff --git a/volume-widget/volume.lua b/volume-widget/volume.lua index 04f1c26..6172f5e 100644 --- a/volume-widget/volume.lua +++ b/volume-widget/volume.lua @@ -13,64 +13,72 @@ local wibox = require("wibox") local watch = require("awful.widget.watch") local spawn = require("awful.spawn") -local secrets = require("awesome-wm-widgets.secrets") - local path_to_icons = "/usr/share/icons/Arc/status/symbolic/" -if secrets.volume_audio_controller == 'pulse' then - device_arg = '-D pulse' -else - device_arg = '' -end +local volume_widget = {} -local GET_VOLUME_CMD = 'amixer ' .. device_arg .. ' sget Master' -local INC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%+' -local DEC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%-' -local TOG_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master toggle' - - -local volume_widget = wibox.widget { - { - id = "icon", - image = path_to_icons .. "audio-volume-muted-symbolic.svg", - resize = false, - widget = wibox.widget.imagebox, - }, - layout = wibox.container.margin(_, _, _, 3), - set_image = function(self, path) - self.icon.image = path - end -} - -local update_graphic = function(widget, stdout, _, _, _) - local mute = string.match(stdout, "%[(o%D%D?)%]") - local volume = string.match(stdout, "(%d?%d?%d)%%") - volume = tonumber(string.format("% 3d", volume)) - local volume_icon_name - if mute == "off" then volume_icon_name="audio-volume-muted-symbolic_red" - elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic" - elseif (volume < 50) then volume_icon_name="audio-volume-low-symbolic" - elseif (volume < 75) then volume_icon_name="audio-volume-medium-symbolic" - elseif (volume <= 100) then volume_icon_name="audio-volume-high-symbolic" +local function worker(args) + + local args = args or {} + + local volume_audio_controller = args.volume_audio_controller or 'pulse' + + local device_arg = '' + if volume_audio_controller == 'pulse' then + device_arg = '-D pulse' end - widget.image = path_to_icons .. volume_icon_name .. ".svg" -end ---[[ allows control volume level by: -- clicking on the widget to mute/unmute -- scrolling when cursor is over the widget -]] -volume_widget:connect_signal("button::press", function(_,_,_,button) - if (button == 4) then awful.spawn(INC_VOLUME_CMD, false) - elseif (button == 5) then awful.spawn(DEC_VOLUME_CMD, false) - elseif (button == 1) then awful.spawn(TOG_VOLUME_CMD, false) + local GET_VOLUME_CMD = 'amixer ' .. device_arg .. ' sget Master' + local INC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%+' + local DEC_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master 5%-' + local TOG_VOLUME_CMD = 'amixer ' .. device_arg .. ' sset Master toggle' + + + volume_widget = wibox.widget { + { + id = "icon", + image = path_to_icons .. "audio-volume-muted-symbolic.svg", + resize = false, + widget = wibox.widget.imagebox, + }, + layout = wibox.container.margin(_, _, _, 3), + set_image = function(self, path) + self.icon.image = path + end + } + + local update_graphic = function(widget, stdout, _, _, _) + local mute = string.match(stdout, "%[(o%D%D?)%]") + local volume = string.match(stdout, "(%d?%d?%d)%%") + volume = tonumber(string.format("% 3d", volume)) + local volume_icon_name + if mute == "off" then volume_icon_name="audio-volume-muted-symbolic_red" + elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic" + elseif (volume < 50) then volume_icon_name="audio-volume-low-symbolic" + elseif (volume < 75) then volume_icon_name="audio-volume-medium-symbolic" + elseif (volume <= 100) then volume_icon_name="audio-volume-high-symbolic" + end + widget.image = path_to_icons .. volume_icon_name .. ".svg" end - spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode) - update_graphic(volume_widget, stdout, stderr, exitreason, exitcode) + --[[ allows control volume level by: + - clicking on the widget to mute/unmute + - scrolling when cursor is over the widget + ]] + volume_widget:connect_signal("button::press", function(_,_,_,button) + if (button == 4) then awful.spawn(INC_VOLUME_CMD, false) + elseif (button == 5) then awful.spawn(DEC_VOLUME_CMD, false) + elseif (button == 1) then awful.spawn(TOG_VOLUME_CMD, false) + end + + spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode) + update_graphic(volume_widget, stdout, stderr, exitreason, exitcode) + end) end) -end) -watch(GET_VOLUME_CMD, 1, update_graphic, volume_widget) + watch(GET_VOLUME_CMD, 1, update_graphic, volume_widget) + + return volume_widget +end -return volume_widget +return setmetatable(volume_widget, { __call = function(_, ...) return worker(...) end }) -- cgit v1.2.3