awesome-wm-widgets/experiments/volume/volume.lua

219 lines
7.3 KiB
Lua
Raw Normal View History

2020-10-31 19:54:15 +01:00
-------------------------------------------------
-- The Ultimate Volume Widget for Awesome Window Manager
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/volume-widget
-- @author Pavel Makhov
-- @copyright 2020 Pavel Makhov
-------------------------------------------------
local awful = require("awful")
local wibox = require("wibox")
local spawn = require("awful.spawn")
local gears = require("gears")
local beautiful = require("beautiful")
local watch = require("awful.widget.watch")
2020-10-31 19:54:15 +01:00
local utils = require("awesome-wm-widgets.experiments.volume.utils")
local LIST_DEVICES_CMD = [[sh -c "pacmd list-sinks; pacmd list-sources"]]
local GET_VOLUME_CMD = 'amixer -D pulse sget Master'
2020-12-17 03:44:37 +01:00
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'
2020-10-31 19:54:15 +01:00
2020-11-09 21:54:33 +01:00
local widget_types = {
icon_and_text = require("awesome-wm-widgets.experiments.volume.widgets.icon-and-text-widget"),
icon = require("awesome-wm-widgets.experiments.volume.widgets.icon-widget"),
2020-12-15 04:33:56 +01:00
arc = require("awesome-wm-widgets.experiments.volume.widgets.arc-widget"),
horizontal_bar = require("awesome-wm-widgets.experiments.volume.widgets.horizontal-bar-widget"),
vertical_bar = require("awesome-wm-widgets.experiments.volume.widgets.vertical-bar-widget")
2020-11-09 21:54:33 +01:00
}
local volume = {}
2020-10-31 19:54:15 +01:00
local rows = { layout = wibox.layout.fixed.vertical }
2020-10-31 19:54:15 +01:00
local popup = awful.popup{
2020-12-26 20:43:13 +01:00
bg = beautiful.bg_normal,
ontop = true,
2020-10-31 19:54:15 +01:00
visible = false,
shape = gears.shape.rounded_rect,
border_width = 1,
border_color = beautiful.bg_focus,
maximum_width = 400,
offset = { y = 5 },
widget = {}
}
local function build_main_line(device)
if device.active_port ~= nil and device.ports[device.active_port] ~= nil then
2020-11-09 16:53:07 +01:00
return device.properties.device_description .. ' · ' .. device.ports[device.active_port]
2020-10-31 19:54:15 +01:00
else
return device.properties.device_description
end
end
local function build_rows(devices, on_checkbox_click, device_type)
local device_rows = { layout = wibox.layout.fixed.vertical }
for _, device in pairs(devices) do
local checkbox = wibox.widget {
2020-12-15 04:33:56 +01:00
checked = device.is_default,
color = beautiful.bg_normal,
paddings = 2,
shape = gears.shape.circle,
2020-10-31 19:54:15 +01:00
forced_width = 20,
forced_height = 20,
check_color = beautiful.fg_urgent,
2020-12-15 04:33:56 +01:00
widget = wibox.widget.checkbox
2020-10-31 19:54:15 +01:00
}
2020-12-06 20:47:40 +01:00
checkbox:connect_signal("button::press", function()
2020-10-31 19:54:15 +01:00
spawn.easy_async(string.format([[sh -c 'pacmd set-default-%s "%s"']], device_type, device.name), function()
on_checkbox_click()
end)
end)
local row = wibox.widget {
{
{
{
checkbox,
valign = 'center',
layout = wibox.container.place,
},
{
{
text = build_main_line(device),
align = 'left',
widget = wibox.widget.textbox
},
left = 10,
layout = wibox.container.margin
},
spacing = 8,
layout = wibox.layout.align.horizontal
},
2020-11-09 16:53:07 +01:00
margins = 4,
2020-10-31 19:54:15 +01:00
layout = wibox.container.margin
},
bg = beautiful.bg_normal,
widget = wibox.container.background
}
2020-11-09 16:53:07 +01:00
row:connect_signal("mouse::enter", function(c) c:set_bg(beautiful.bg_focus) end)
row:connect_signal("mouse::leave", function(c) c:set_bg(beautiful.bg_normal) end)
local old_cursor, old_wibox
2020-12-06 20:47:40 +01:00
row:connect_signal("mouse::enter", function()
2020-11-09 16:53:07 +01:00
local wb = mouse.current_wibox
old_cursor, old_wibox = wb.cursor, wb
wb.cursor = "hand1"
end)
2020-12-06 20:47:40 +01:00
row:connect_signal("mouse::leave", function()
2020-11-09 16:53:07 +01:00
if old_wibox then
old_wibox.cursor = old_cursor
old_wibox = nil
end
end)
2020-12-06 20:47:40 +01:00
row:connect_signal("button::press", function()
2020-11-09 16:53:07 +01:00
spawn.easy_async(string.format([[sh -c 'pacmd set-default-%s "%s"']], device_type, device.name), function()
on_checkbox_click()
end)
end)
2020-10-31 19:54:15 +01:00
table.insert(device_rows, row)
end
return device_rows
end
local function build_header_row(text)
return wibox.widget{
{
markup = "<b>" .. text .. "</b>",
align = 'center',
widget = wibox.widget.textbox
},
bg = beautiful.bg_normal,
widget = wibox.container.background
}
end
local function rebuild_popup()
spawn.easy_async(LIST_DEVICES_CMD, function(stdout)
local sinks, sources = utils.extract_sinks_and_sources(stdout)
for i = 0, #rows do rows[i]=nil end
table.insert(rows, build_header_row("SINKS"))
table.insert(rows, build_rows(sinks, function() rebuild_popup() end, "sink"))
table.insert(rows, build_header_row("SOURCES"))
table.insert(rows, build_rows(sources, function() rebuild_popup() end, "source"))
popup:setup(rows)
end)
end
2020-12-06 20:47:40 +01:00
local function worker(user_args)
2020-10-31 19:54:15 +01:00
2020-12-06 20:47:40 +01:00
local args = user_args or {}
2020-11-09 21:54:33 +01:00
local widget_type = args.widget_type
2020-12-15 04:33:56 +01:00
local refresh_rate = args.refresh_rate or 1
2020-11-09 21:54:33 +01:00
if widget_types[widget_type] == nil then
volume.widget = widget_types['icon_and_text'].get_widget(user_args.icon_and_text_args)
2020-11-09 21:54:33 +01:00
else
volume.widget = widget_types[widget_type].get_widget(args)
2020-11-09 21:54:33 +01:00
end
2020-12-17 03:44:37 +01:00
local function update_graphic(widget, stdout)
local mute = string.match(stdout, "%[(o%D%D?)%]") -- \[(o\D\D?)\] - [on] or [off]
if mute == 'off' then widget:mute()
elseif mute == 'on' then widget:unmute()
end
local volume_level = string.match(stdout, "(%d?%d?%d)%%") -- (\d?\d?\d)\%)
volume_level = string.format("% 3d", volume_level)
widget:set_volume_level(volume_level)
end
function volume:inc()
spawn.easy_async(INC_VOLUME_CMD, function(stdout) update_graphic(volume.widget, stdout) end)
end
function volume:dec()
spawn.easy_async(DEC_VOLUME_CMD, function(stdout) update_graphic(volume.widget, stdout) end)
2020-12-17 03:44:37 +01:00
end
function volume:toggle()
spawn.easy_async(TOG_VOLUME_CMD, function(stdout) update_graphic(volume.widget, stdout) end)
end
volume.widget:buttons(
2020-10-31 19:54:15 +01:00
awful.util.table.join(
awful.button({}, 3, function()
2020-10-31 19:54:15 +01:00
if popup.visible then
popup.visible = not popup.visible
else
rebuild_popup()
popup:move_next_to(mouse.current_widget_geometry)
end
end),
awful.button({}, 4, function() volume:inc() end),
awful.button({}, 5, function() volume:dec() end),
awful.button({}, 1, function() volume:toggle() end)
2020-10-31 19:54:15 +01:00
)
)
watch(GET_VOLUME_CMD, refresh_rate, update_graphic, volume.widget)
return volume.widget
2020-10-31 19:54:15 +01:00
end
return setmetatable(volume, { __call = function(_, ...) return worker(...) end })