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
|
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
-- @author Pavel Makhov, Aurélien Lajoie
|
2018-10-05 17:02:05 +02:00
|
|
|
-- @copyright 2018 Pavel Makhov
|
2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
|
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")
|
2019-09-18 01:21:34 +02:00
|
|
|
local naughty = require("naughty")
|
2019-12-25 02:37:59 +01:00
|
|
|
local gfs = require("gears.filesystem")
|
2019-09-18 01:21:34 +02:00
|
|
|
local dpi = require('beautiful').xresources.apply_dpi
|
|
|
|
|
2019-12-25 02:37:59 +01:00
|
|
|
local PATH_TO_ICONS = "/usr/share/icons/Arc/status/symbolic/"
|
2020-02-08 20:39:49 +01:00
|
|
|
local volume_icon_name="audio-volume-high-symbolic"
|
|
|
|
local GET_VOLUME_CMD = 'amixer sget Master'
|
2018-10-05 17:02:05 +02:00
|
|
|
|
2020-11-01 22:37:11 +01:00
|
|
|
local volume = {device = '', display_notification = false, display_notification_onClick = true, notification = nil, delta = 5}
|
2019-08-27 09:20:08 +02:00
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
function volume:toggle()
|
|
|
|
volume:_cmd('amixer ' .. volume.device .. ' sset Master toggle')
|
|
|
|
end
|
|
|
|
|
|
|
|
function volume:raise()
|
2020-05-23 15:30:22 +02:00
|
|
|
volume:_cmd('amixer ' .. volume.device .. ' sset Master ' .. tostring(volume.delta) .. '%+')
|
2020-02-08 20:39:49 +01:00
|
|
|
end
|
|
|
|
function volume:lower()
|
2020-05-23 15:30:22 +02:00
|
|
|
volume:_cmd('amixer ' .. volume.device .. ' sset Master ' .. tostring(volume.delta) .. '%-')
|
2020-02-08 20:39:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
--{{{ Icon and notification update
|
|
|
|
|
|
|
|
--------------------------------------------------
|
|
|
|
-- Set the icon and return the message to display
|
|
|
|
-- base on sound level and mute
|
|
|
|
--------------------------------------------------
|
|
|
|
local function parse_output(stdout)
|
|
|
|
local level = string.match(stdout, "(%d?%d?%d)%%")
|
|
|
|
if stdout:find("%[off%]") then
|
|
|
|
volume_icon_name="audio-volume-muted-symbolic_red"
|
|
|
|
return level.."% <span color=\"red\"><b>Mute</b></span>"
|
|
|
|
end
|
|
|
|
level = tonumber(string.format("% 3d", level))
|
|
|
|
|
|
|
|
if (level >= 0 and level < 25) then
|
|
|
|
volume_icon_name="audio-volume-muted-symbolic"
|
|
|
|
elseif (level < 50) then
|
|
|
|
volume_icon_name="audio-volume-low-symbolic"
|
|
|
|
elseif (level < 75) then
|
|
|
|
volume_icon_name="audio-volume-medium-symbolic"
|
|
|
|
else
|
|
|
|
volume_icon_name="audio-volume-high-symbolic"
|
|
|
|
end
|
|
|
|
return level.."%"
|
|
|
|
end
|
2019-09-04 03:57:24 +02:00
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
--------------------------------------------------------
|
|
|
|
--Update the icon and the notification if needed
|
|
|
|
--------------------------------------------------------
|
|
|
|
local function update_graphic(widget, stdout, _, _, _)
|
|
|
|
local txt = parse_output(stdout)
|
|
|
|
widget.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
|
2020-11-01 22:37:11 +01:00
|
|
|
if (volume.display_notification or volume.display_notification_onClick) then
|
2020-02-08 20:39:49 +01:00
|
|
|
volume.notification.iconbox.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
|
|
|
|
naughty.replace_text(volume.notification, "Volume", txt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function notif(msg, keep)
|
2020-11-01 22:37:11 +01:00
|
|
|
if (volume.display_notification or (keep and volume.display_notification_onClick)) then
|
2020-02-08 20:39:49 +01:00
|
|
|
naughty.destroy(volume.notification)
|
|
|
|
volume.notification= naughty.notify{
|
|
|
|
text = msg,
|
|
|
|
icon=PATH_TO_ICONS .. volume_icon_name .. ".svg",
|
|
|
|
icon_size = dpi(16),
|
|
|
|
title = "Volume",
|
|
|
|
position = volume.position,
|
|
|
|
timeout = keep and 0 or 2, hover_timeout = 0.5,
|
|
|
|
width = 200,
|
2020-04-15 19:00:10 +02:00
|
|
|
screen = mouse.screen
|
2020-02-08 20:39:49 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--}}}
|
|
|
|
|
|
|
|
local function worker(args)
|
|
|
|
--{{{ Args
|
2019-09-04 03:57:24 +02:00
|
|
|
local args = args or {}
|
|
|
|
|
|
|
|
local volume_audio_controller = args.volume_audio_controller or 'pulse'
|
2020-04-04 18:44:46 +02:00
|
|
|
volume.display_notification = args.display_notification or false
|
2020-11-01 22:37:11 +01:00
|
|
|
volume.display_notification_onClick = args.display_notification_onClick or true
|
2020-02-08 20:39:49 +01:00
|
|
|
volume.position = args.notification_position or "top_right"
|
2019-09-04 03:57:24 +02:00
|
|
|
if volume_audio_controller == 'pulse' then
|
2020-02-08 20:39:49 +01:00
|
|
|
volume.device = '-D pulse'
|
2017-05-30 02:09:44 +02:00
|
|
|
end
|
2020-05-23 15:30:22 +02:00
|
|
|
volume.delta = args.delta or 5
|
2020-02-08 20:39:49 +01:00
|
|
|
GET_VOLUME_CMD = 'amixer ' .. volume.device.. ' sget Master'
|
|
|
|
--}}}
|
|
|
|
--{{{ Check for icon path
|
2019-12-25 02:37:59 +01:00
|
|
|
if not gfs.dir_readable(PATH_TO_ICONS) then
|
|
|
|
naughty.notify{
|
|
|
|
title = "Volume Widget",
|
|
|
|
text = "Folder with icons doesn't exist: " .. PATH_TO_ICONS,
|
|
|
|
preset = naughty.config.presets.critical
|
|
|
|
}
|
2020-02-08 20:39:49 +01:00
|
|
|
return
|
2019-12-25 02:37:59 +01:00
|
|
|
end
|
2020-02-08 20:39:49 +01:00
|
|
|
--}}}
|
|
|
|
--{{{ Widget creation
|
|
|
|
volume.widget = wibox.widget {
|
2019-09-04 03:57:24 +02:00
|
|
|
{
|
|
|
|
id = "icon",
|
2019-12-25 02:37:59 +01:00
|
|
|
image = PATH_TO_ICONS .. "audio-volume-muted-symbolic.svg",
|
2019-09-04 03:57:24 +02:00
|
|
|
resize = false,
|
|
|
|
widget = wibox.widget.imagebox,
|
|
|
|
},
|
|
|
|
layout = wibox.container.margin(_, _, _, 3),
|
|
|
|
set_image = function(self, path)
|
|
|
|
self.icon.image = path
|
|
|
|
end
|
|
|
|
}
|
2020-02-08 20:39:49 +01:00
|
|
|
--}}}
|
|
|
|
--{{{ Spawn functions
|
|
|
|
function volume:_cmd(cmd)
|
|
|
|
notif("")
|
|
|
|
spawn.easy_async(cmd, function(stdout, stderr, exitreason, exitcode)
|
|
|
|
update_graphic(volume.widget, stdout, stderr, exitreason, exitcode)
|
|
|
|
end)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
local function show()
|
2020-11-01 22:37:11 +01:00
|
|
|
spawn.easy_async(GET_VOLUME_CMD, function(stdout, _, _, _)
|
|
|
|
txt = parse_output(stdout)
|
|
|
|
notif(txt, true)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-02-08 20:39:49 +01:00
|
|
|
--}}}
|
|
|
|
--{{{ Mouse event
|
2019-09-04 03:57:24 +02:00
|
|
|
--[[ allows control volume level by:
|
|
|
|
- clicking on the widget to mute/unmute
|
|
|
|
- scrolling when cursor is over the widget
|
|
|
|
]]
|
2020-02-08 20:39:49 +01:00
|
|
|
volume.widget:connect_signal("button::press", function(_,_,_,button)
|
|
|
|
if (button == 4) then volume.raise()
|
|
|
|
elseif (button == 5) then volume.lower()
|
|
|
|
elseif (button == 1) then volume.toggle()
|
2019-09-04 03:57:24 +02:00
|
|
|
end
|
2017-05-30 02:09:44 +02:00
|
|
|
end)
|
2020-02-08 20:39:49 +01:00
|
|
|
if volume.display_notification then
|
|
|
|
volume.widget:connect_signal("mouse::enter", function() show() end)
|
|
|
|
volume.widget:connect_signal("mouse::leave", function() naughty.destroy(volume.notification) end)
|
2020-11-01 22:37:11 +01:00
|
|
|
elseif volume.display_notification_onClick then
|
|
|
|
volume.widget:connect_signal("button::press", function(_,_,_,button)
|
|
|
|
if (button == 3) then show() end
|
|
|
|
end)
|
|
|
|
volume.widget:connect_signal("mouse::leave", function() naughty.destroy(volume.notification) end)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
2020-02-08 20:39:49 +01:00
|
|
|
--}}}
|
2020-05-18 22:48:45 +02:00
|
|
|
|
|
|
|
--{{{ Set initial icon
|
|
|
|
spawn.easy_async(GET_VOLUME_CMD, function(stdout, stderr, exitreason, exitcode)
|
|
|
|
parse_output(stdout)
|
|
|
|
volume.widget.image = PATH_TO_ICONS .. volume_icon_name .. ".svg"
|
|
|
|
end)
|
|
|
|
--}}}
|
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
return volume.widget
|
2019-09-04 03:57:24 +02:00
|
|
|
end
|
2017-12-09 20:39:46 +01:00
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
return setmetatable(volume, { __call = function(_, ...) return worker(...) end })
|