2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
-- The Ultimate Volume Widget for Awesome Window Manager
|
2017-12-09 20:39:46 +01:00
|
|
|
-- More details could be found here:
|
|
|
|
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/volume-widget
|
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
-- @author Pavel Makhov
|
|
|
|
-- @copyright 2020 Pavel Makhov
|
2017-12-09 20:39:46 +01:00
|
|
|
-------------------------------------------------
|
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
local awful = require("awful")
|
2017-02-04 04:15:16 +01:00
|
|
|
local wibox = require("wibox")
|
2017-05-30 02:09:44 +02:00
|
|
|
local spawn = require("awful.spawn")
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
local gears = require("gears")
|
|
|
|
local beautiful = require("beautiful")
|
|
|
|
local watch = require("awful.widget.watch")
|
|
|
|
local utils = require("awesome-wm-widgets.volume-widget.utils")
|
2019-08-27 09:20:08 +02:00
|
|
|
|
2020-02-08 20:39:49 +01:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
local LIST_DEVICES_CMD = [[sh -c "pacmd list-sinks; pacmd list-sources"]]
|
2021-11-30 20:19:56 +01:00
|
|
|
local function GET_VOLUME_CMD(device) return 'amixer -D ' .. device .. ' sget Master' end
|
2021-12-26 12:39:36 +01:00
|
|
|
local function INC_VOLUME_CMD(device, step) return 'amixer -D ' .. device .. ' sset Master ' .. step .. '%+' end
|
|
|
|
local function DEC_VOLUME_CMD(device, step) return 'amixer -D ' .. device .. ' sset Master ' .. step .. '%-' end
|
2021-11-30 20:19:56 +01:00
|
|
|
local function TOG_VOLUME_CMD(device) return 'amixer -D ' .. device .. ' sset Master toggle' end
|
2020-02-08 20:39:49 +01:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
|
|
|
|
local widget_types = {
|
|
|
|
icon_and_text = require("awesome-wm-widgets.volume-widget.widgets.icon-and-text-widget"),
|
|
|
|
icon = require("awesome-wm-widgets.volume-widget.widgets.icon-widget"),
|
|
|
|
arc = require("awesome-wm-widgets.volume-widget.widgets.arc-widget"),
|
|
|
|
horizontal_bar = require("awesome-wm-widgets.volume-widget.widgets.horizontal-bar-widget"),
|
|
|
|
vertical_bar = require("awesome-wm-widgets.volume-widget.widgets.vertical-bar-widget")
|
|
|
|
}
|
|
|
|
local volume = {}
|
|
|
|
|
|
|
|
local rows = { layout = wibox.layout.fixed.vertical }
|
|
|
|
|
|
|
|
local popup = awful.popup{
|
|
|
|
bg = beautiful.bg_normal,
|
|
|
|
ontop = true,
|
|
|
|
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
|
|
|
|
return device.properties.device_description .. ' · ' .. device.ports[device.active_port]
|
2020-02-08 20:39:49 +01:00
|
|
|
else
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
return device.properties.device_description
|
2020-02-08 20:39:49 +01:00
|
|
|
end
|
|
|
|
end
|
2019-09-04 03:57:24 +02:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
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 {
|
|
|
|
checked = device.is_default,
|
|
|
|
color = beautiful.bg_normal,
|
|
|
|
paddings = 2,
|
|
|
|
shape = gears.shape.circle,
|
|
|
|
forced_width = 20,
|
|
|
|
forced_height = 20,
|
|
|
|
check_color = beautiful.fg_urgent,
|
|
|
|
widget = wibox.widget.checkbox
|
|
|
|
}
|
|
|
|
|
|
|
|
checkbox:connect_signal("button::press", function()
|
|
|
|
spawn.easy_async(string.format([[sh -c 'pacmd set-default-%s "%s"']], device_type, device.name), function()
|
|
|
|
on_checkbox_click()
|
|
|
|
end)
|
|
|
|
end)
|
2020-02-08 20:39:49 +01:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
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
|
|
|
|
},
|
|
|
|
margins = 4,
|
|
|
|
layout = wibox.container.margin
|
|
|
|
},
|
|
|
|
bg = beautiful.bg_normal,
|
|
|
|
widget = wibox.container.background
|
2020-02-08 20:39:49 +01:00
|
|
|
}
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +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
|
|
|
|
row:connect_signal("mouse::enter", function()
|
|
|
|
local wb = mouse.current_wibox
|
|
|
|
old_cursor, old_wibox = wb.cursor, wb
|
|
|
|
wb.cursor = "hand1"
|
|
|
|
end)
|
|
|
|
row:connect_signal("mouse::leave", function()
|
|
|
|
if old_wibox then
|
|
|
|
old_wibox.cursor = old_cursor
|
|
|
|
old_wibox = nil
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
row:connect_signal("button::press", function()
|
|
|
|
spawn.easy_async(string.format([[sh -c 'pacmd set-default-%s "%s"']], device_type, device.name), function()
|
|
|
|
on_checkbox_click()
|
|
|
|
end)
|
|
|
|
end)
|
|
|
|
|
|
|
|
table.insert(device_rows, row)
|
2020-02-08 20:39:49 +01:00
|
|
|
end
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
|
|
|
|
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)
|
2020-02-08 20:39:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-02 15:24:05 +01:00
|
|
|
local function worker(user_args)
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
|
2020-12-02 15:24:05 +01:00
|
|
|
local args = user_args or {}
|
2019-09-04 03:57:24 +02:00
|
|
|
|
2021-05-16 00:19:21 +02:00
|
|
|
local mixer_cmd = args.mixer_cmd or 'pavucontrol'
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
local widget_type = args.widget_type
|
|
|
|
local refresh_rate = args.refresh_rate or 1
|
2021-07-24 15:14:28 +02:00
|
|
|
local step = args.step or 5
|
2021-11-30 20:19:56 +01:00
|
|
|
local device = args.device or 'pulse'
|
2021-07-24 15:14:28 +02:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
if widget_types[widget_type] == nil then
|
2021-04-12 02:31:58 +02:00
|
|
|
volume.widget = widget_types['icon_and_text'].get_widget(args.icon_and_text_args)
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
else
|
|
|
|
volume.widget = widget_types[widget_type].get_widget(args)
|
2019-12-25 02:37:59 +01:00
|
|
|
end
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +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()
|
2019-09-04 03:57:24 +02:00
|
|
|
end
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
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)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
|
|
|
|
2021-09-23 23:04:26 +02:00
|
|
|
function volume:inc(s)
|
2021-11-30 20:19:56 +01:00
|
|
|
spawn.easy_async(INC_VOLUME_CMD(device, s or step), function(stdout) update_graphic(volume.widget, stdout) end)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
|
2021-09-23 23:04:26 +02:00
|
|
|
function volume:dec(s)
|
2021-11-30 20:19:56 +01:00
|
|
|
spawn.easy_async(DEC_VOLUME_CMD(device, s or step), function(stdout) update_graphic(volume.widget, stdout) end)
|
2019-09-18 01:21:34 +02:00
|
|
|
end
|
2020-05-18 22:48:45 +02:00
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
function volume:toggle()
|
2021-11-30 20:19:56 +01:00
|
|
|
spawn.easy_async(TOG_VOLUME_CMD(device), function(stdout) update_graphic(volume.widget, stdout) end)
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
end
|
|
|
|
|
2021-05-16 00:19:21 +02:00
|
|
|
function volume:mixer()
|
|
|
|
if mixer_cmd then
|
|
|
|
spawn.easy_async(mixer_cmd)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
volume.widget:buttons(
|
|
|
|
awful.util.table.join(
|
|
|
|
awful.button({}, 3, function()
|
|
|
|
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),
|
2021-05-16 00:19:21 +02:00
|
|
|
awful.button({}, 2, function() volume:mixer() end),
|
[volume] BREAKING CHANGE - new widget instead of old ones
Having three widgets for volume led to a problem of code duplication -
same logic was duplicated three times. However when an issue was
discovered and fixed, it was fixed in only one of three widgets.
So I decided to create a volume widget from scratch, adding new
features, such as selecting input/output, better responsiveness,
easily customizable widget ui (bar, text, icon, icon and text, arc).
Should close #199, #198, #185, #182, #47, #122, #183.
2021-03-20 01:49:00 +01:00
|
|
|
awful.button({}, 1, function() volume:toggle() end)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2021-11-30 20:19:56 +01:00
|
|
|
watch(GET_VOLUME_CMD(device), refresh_rate, update_graphic, volume.widget)
|
2020-05-18 22:48:45 +02:00
|
|
|
|
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 })
|