1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
-------------------------------------------------
-- A purely pactl-based volume widget based on the original Volume widget
-- More details could be found here:
-- https://github.com/streetturtle/awesome-wm-widgets/tree/master/pactl-widget
-- @author Stefan Huber
-- @copyright 2023 Stefan Huber
-------------------------------------------------
local awful = require("awful")
local wibox = require("wibox")
local spawn = require("awful.spawn")
local gears = require("gears")
local beautiful = require("beautiful")
local pactl = require("awesome-wm-widgets.pactl-widget.pactl")
local utils = require("awesome-wm-widgets.pactl-widget.utils")
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.description .. ' · ' .. utils.split(device.ports[device.active_port], " ")[1]
else
return 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 {
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()
pactl.set_default(device_type, device.name)
on_checkbox_click()
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
},
margins = 4,
layout = wibox.container.margin
},
bg = beautiful.bg_normal,
widget = wibox.container.background
}
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()
pactl.set_default(device_type, device.name)
on_checkbox_click()
end)
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()
for i = 0, #rows do
rows[i]=nil
end
local sinks, sources = pactl.get_sinks_and_sources()
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
local function worker(user_args)
local args = user_args or {}
local mixer_cmd = args.mixer_cmd or 'pavucontrol'
local widget_type = args.widget_type
local refresh_rate = args.refresh_rate or 1
local step = args.step or 5
local device = args.device or '@DEFAULT_SINK@'
if widget_types[widget_type] == nil then
volume.widget = widget_types['icon_and_text'].get_widget(args.icon_and_text_args)
else
volume.widget = widget_types[widget_type].get_widget(args)
end
local function update_graphic(widget)
local vol = pactl.get_volume(device)
if vol ~= nil then
widget:set_volume_level(vol)
end
if pactl.get_mute(device) then
widget:mute()
else
widget:unmute()
end
end
function volume:inc(s)
pactl.volume_increase(device, s or step)
update_graphic(volume.widget)
end
function volume:dec(s)
pactl.volume_decrease(device, s or step)
update_graphic(volume.widget)
end
function volume:toggle()
pactl.mute_toggle(device)
update_graphic(volume.widget)
end
function volume:popup()
if popup.visible then
popup.visible = not popup.visible
else
rebuild_popup()
popup:move_next_to(mouse.current_widget_geometry)
end
end
function volume:mixer()
if mixer_cmd then
spawn(mixer_cmd)
end
end
volume.widget:buttons(
awful.util.table.join(
awful.button({}, 1, function() volume:toggle() end),
awful.button({}, 2, function() volume:mixer() end),
awful.button({}, 3, function() volume:popup() end),
awful.button({}, 4, function() volume:inc() end),
awful.button({}, 5, function() volume:dec() end)
)
)
gears.timer {
timeout = refresh_rate,
call_now = true,
autostart = true,
callback = function()
update_graphic(volume.widget)
end
}
return volume.widget
end
return setmetatable(volume, { __call = function(_, ...) return worker(...) end })
|