This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
/
init.lua
148 lines (123 loc) · 4.65 KB
/
init.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
145
146
147
148
--------------------------------
-- Author: Gregor Best --
-- Copyright 2009 Gregor Best --
--------------------------------
local setmetatable = setmetatable
local tonumber = tonumber
local pairs = pairs
local io = {
popen = io.popen
}
local string = {
match = string.match,
find = string.find,
format = string.format
}
local table = {
insert = table.insert
}
local awful = require("awful")
local wibox = require("wibox")
local lib = {
hooks = require("obvious.lib.hooks"),
markup = require("obvious.lib.markup")
}
local volume_alsa = {}
local objects = { }
function volume_alsa.get_data(cardid, channel)
local rv = { }
local fd = io.popen("amixer -c " .. cardid .. " -- sget " .. channel)
if not fd then return end
local status = fd:read("*all")
fd:close()
rv.volume = tonumber(string.match(status, "(%d?%d?%d)%%"))
if not rv.volume then
rv.volume = ""
end
status = string.match(status, "%[(o[^%]]*)%]")
if not status then status = "on" end
if string.find(status, "on", 1, true) then
rv.mute = false
else
rv.mute = true
end
return rv
end
local function update(obj)
local status = volume_alsa.get_data(obj.cardid, obj.channel) or { mute = true, volume = 0 }
local color = "#900000"
if not status.mute then
color = "#009000"
end
local format = " %03d%%"
if status.volume == "" then
format = ""
end
obj.widget:set_markup(lib.markup.fg.color(color, "" .. obj.abrv .. "" ) .. string.format(format, status.volume))
end
local function update_by_values(cardid, channel)
for i, v in pairs(objects) do
if v.channel == channel and v.cardid == cardid then
update(v)
end
end
end
function volume_alsa.raise(cardid, channel, v)
v = v or 1
awful.util.spawn("amixer -q -c " .. cardid .. " sset " .. channel .. " " .. v .. "+", false)
update_by_values(cardid, channel)
end
function volume_alsa.lower(cardid, channel, v)
v = v or 1
awful.util.spawn("amixer -q -c " .. cardid .. " sset " .. channel .. " " .. v .. "-", false)
update_by_values(cardid, channel)
end
function volume_alsa.mute(cardid, channel)
awful.util.spawn("amixer -c " .. cardid .. " sset " .. channel .. " toggle", false)
update_by_values(cardid, channel)
end
local function mixer(term, cardid)
awful.util.spawn(term .. " -e 'alsamixer -c " .. cardid .. "'")
end
local function create(_, cardid, channel, abrv)
local cardid = cardid or 0
local channel = channel or "Master"
local abrv = abrv or "M"
local obj = {
cardid = cardid,
channel = channel,
abrv = abrv,
term = "x-terminal-emulator -T Mixer"
}
local widget = wibox.widget.textbox()
obj.widget = widget
obj[1] = widget
obj.update = function() update(obj) end
widget:buttons(awful.util.table.join(
awful.button({ }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 1) obj.update() end),
awful.button({ }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 1) obj.update() end),
awful.button({ "Shift" }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 10) obj.update() end),
awful.button({ "Shift" }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 10) obj.update() end),
awful.button({ "Control" }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 5) obj.update() end),
awful.button({ "Control" }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 5) obj.update() end),
awful.button({ }, 1, function () volume_alsa.mute(obj.cardid, obj.channel) obj.update() end),
awful.button({ }, 3, function () mixer(obj.term, obj.cardid) obj.update() end)
))
obj.set_layout = function(obj, layout) obj.layout = layout return obj end
obj.set_cardid = function(obj, id) obj.cardid = id obj.update() return obj end
obj.set_channel = function(obj, id) obj.channel = id obj.update() return obj end
obj.set_abrv = function(obj, id) obj.abrv = id obj.update() return obj end
obj.set_term = function(obj, term) obj.term = term return obj end
obj.raise = function(obj, v) volume_alsa.raise(obj.cardid, obj.channel, v) return obj end
obj.lower = function(obj, v) volume_alsa.lower(obj.cardid, obj.channel, v) return obj end
obj.mute = function(obj, v) volume_alsa.mute(obj.cardid, obj.channel, v) return obj end
obj.update()
lib.hooks.timer.register(10, 30, obj.update, "Update for the volume widget")
lib.hooks.timer.start(obj.update)
table.insert(objects, obj)
return widget
end
local volume_alsa = setmetatable(volume_alsa, { __call = create })
require('obvious.widget_registry').volume_alsa = volume_alsa
return volume_alsa
-- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et