2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
-- Volume Widget for Awesome Window Manager
|
|
|
|
-- Shows the current volume level
|
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/volume-widget
|
|
|
|
|
|
|
|
-- @author Pavel Makhov
|
2018-10-05 17:02:05 +02:00
|
|
|
-- @copyright 2018 Pavel Makhov
|
2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
|
2017-01-23 17:01:16 +01:00
|
|
|
local awful = require("awful")
|
2017-02-04 04:15:16 +01:00
|
|
|
local wibox = require("wibox")
|
|
|
|
local watch = require("awful.widget.watch")
|
2017-05-30 02:09:44 +02:00
|
|
|
local spawn = require("awful.spawn")
|
2017-01-23 17:01:16 +01:00
|
|
|
|
2017-02-04 04:15:16 +01:00
|
|
|
local path_to_icons = "/usr/share/icons/Arc/status/symbolic/"
|
2018-10-05 17:02:05 +02:00
|
|
|
|
|
|
|
local GET_VOLUME_CMD = 'amixer -D pulse sget Master'
|
|
|
|
local INC_VOLUME_CMD = 'amixer -D pulse sset Master 5%+'
|
|
|
|
local DEC_VOLUME_CMD = 'amixer -D pulse sset Master 5%-'
|
|
|
|
local TOG_VOLUME_CMD = 'amixer -D pulse sset Master toggle'
|
2017-01-23 17:01:16 +01:00
|
|
|
|
2017-12-09 20:39:46 +01:00
|
|
|
local volume_widget = wibox.widget {
|
2017-02-04 04:15:16 +01:00
|
|
|
{
|
|
|
|
id = "icon",
|
2017-06-06 03:22:43 +02:00
|
|
|
image = path_to_icons .. "audio-volume-muted-symbolic.svg",
|
|
|
|
resize = false,
|
2017-02-04 04:15:16 +01:00
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
},
|
2017-06-06 03:22:43 +02:00
|
|
|
layout = wibox.container.margin(_, _, _, 3),
|
2017-02-04 04:15:16 +01:00
|
|
|
set_image = function(self, path)
|
|
|
|
self.icon.image = path
|
|
|
|
end
|
|
|
|
}
|
2017-01-23 17:01:16 +01:00
|
|
|
|
2017-06-06 03:22:43 +02:00
|
|
|
local update_graphic = function(widget, stdout, _, _, _)
|
2017-05-30 02:09:44 +02:00
|
|
|
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
|
2017-06-09 21:45:44 +02:00
|
|
|
if mute == "off" then volume_icon_name="audio-volume-muted-symbolic_red"
|
2017-05-30 02:09:44 +02:00
|
|
|
elseif (volume >= 0 and volume < 25) then volume_icon_name="audio-volume-muted-symbolic"
|
2017-06-09 21:45:44 +02:00
|
|
|
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"
|
2017-05-30 02:09:44 +02:00
|
|
|
end
|
|
|
|
widget.image = path_to_icons .. volume_icon_name .. ".svg"
|
|
|
|
end
|
|
|
|
|
2017-02-13 03:05:14 +01:00
|
|
|
--[[ allows control volume level by:
|
2017-02-10 03:56:09 +01:00
|
|
|
- clicking on the widget to mute/unmute
|
2017-06-09 15:17:07 +02:00
|
|
|
- scrolling when cursor is over the widget
|
2017-02-10 03:56:09 +01:00
|
|
|
]]
|
|
|
|
volume_widget:connect_signal("button::press", function(_,_,_,button)
|
2018-10-05 17:02:05 +02:00
|
|
|
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)
|
2017-02-10 03:56:09 +01:00
|
|
|
end
|
2017-06-06 03:22:43 +02:00
|
|
|
|
2018-10-05 17:02:05 +02:00
|
|
|
spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode)
|
2017-05-30 02:09:44 +02:00
|
|
|
update_graphic(volume_widget, stdout, stderr, exitreason, exitcode)
|
|
|
|
end)
|
2017-02-10 03:56:09 +01:00
|
|
|
end)
|
|
|
|
|
2018-10-05 17:02:05 +02:00
|
|
|
watch(GET_VOLUME_CMD, 1, update_graphic, volume_widget)
|
2017-12-09 20:39:46 +01:00
|
|
|
|
|
|
|
return volume_widget
|