-
Notifications
You must be signed in to change notification settings - Fork 1
/
volume.lua
144 lines (115 loc) · 3.53 KB
/
volume.lua
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
--[[
-- Volume widget for awesomewm 3.5
-- @author miraleung
--]]
local wibox = require("wibox")
local awful = require("awful")
local volume_icon = wibox.widget.imagebox()
local audio_jack_icon = wibox.widget.imagebox()
local volume_text = wibox.widget.textbox()
volume_text:set_align("right")
local spacer = wibox.widget.textbox()
spacer:set_text(" ")
volume_widget = wibox.layout.fixed.horizontal()
volume_widget:add(volume_text)
volume_widget:add(spacer)
volume_widget:add(audio_jack_icon)
volume_widget:add(spacer)
volume_widget:add(volume_icon)
function get_current_path()
local path = debug.getinfo(2, "S").source:sub(2)
return path:match("(.*/)")
end
function isempty(str)
return str == nil or str == ""
end
-- Returns the amixer command for checking the headphone jack.
function get_headphone_query()
local num_cards_cmd = 'grep -nr "[0-9]\\+ \\[" /proc/asound/cards | wc -l'
local fd = io.popen(num_cards_cmd)
local num_cards_str = fd:read("*all")
fd:close()
local num_cards = tonumber(num_cards_str)
local curr_card_id = 0
local control_id = ""
for i = 0, num_cards, 1 do
find_controls_cmd = 'amixer -c ' .. tostring(i) .. ' controls | grep "Headphone Jack"'
fd = io.popen(find_controls_cmd)
control_id = fd:read("*all")
fd:close()
if not isempty(control_id) then
curr_card_id = tostring(i)
break
end
end
local amixer_headphones_cmd = ""
if not isempty(control_id) then
amixer_headphones_cmd = "amixer -c " .. curr_card_id .. " cget " .. control_id
end
return amixer_headphones_cmd
end
local current_path = get_current_path()
local query_headphones_cmd = get_headphone_query()
-- Returns true if headphones are plugged in
function is_audio_jack_in()
if isempty(query_headphones_cmd) then
return false
end
local fd = io.popen(query_headphones_cmd)
local headphone_status = fd:read("*all")
fd:close()
headphone_status = headphone_status:match("values=(o%w+)")
return headphone_status == "on"
end
function get_volume_icon(volume_int)
if volume_int == 0 then
icon = "volume_icon_mute.png"
elseif volume_int < 33 then
icon = "volume_icon_low.png"
elseif volume_int < 66 then
icon = "volume_icon_med.png"
else
icon = "volume_icon_high.png"
end
return icon
end
function update_volume()
local fd = io.popen("amixer -D pulse sget Master")
local status = fd:read("*all")
local icons_dir = "icons"
fd:close()
local volume = status:match("(%d?%d?%d)%%")
local volume_int = tonumber(volume)
local volume_str = string.format("% 3d", volume)
status = status:match("%[(o[^%]]*)%]")
if status:find("on", 1, true) then
-- For the volume numbers
volume_str = volume_str .. "%"
else
-- For the mute button
volume_int = 0
volume_str = volume_str .. "M"
end
local icon = get_volume_icon(volume_int)
if is_audio_jack_in() then
local aj_icon = "volume_icon_headphones.png"
audio_jack_icon:set_image(current_path .. "/" .. icons_dir .. "/" .. aj_icon)
else
audio_jack_icon:set_image(nil)
end
volume_text:set_markup(volume_str)
volume_icon:set_image(current_path .. "/" .. icons_dir .. "/" .. icon)
end
function volume_up()
awful.util.spawn("amixer -D pulse sset Master 5%+")
end
function volume_down()
awful.util.spawn("amixer -D pulse sset Master 5%-")
end
function volume_mute()
awful.util.spawn("amixer -D pulse sset Master toggle")
end
update_volume(volume_text, volume_icon)
local mytimer = timer({ timeout = 0.2 })
mytimer:connect_signal("timeout", function () update_volume() end)
mytimer:start()