summaryrefslogtreecommitdiff
path: root/widgets/volume.lua
blob: 80d170ff45ad1d9edf624a85a0b3ebf1f84ecbe8 (plain)
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
-- Inspired by https://awesome.naquadah.org/wiki/Farhavens_volume_widget

local setmetatable = setmetatable
local io = io
local string = string
local tonumber = tonumber
local capi = { widget = widget }
local awful = require ("awful")
local beautiful = require ("beautiful")

module("widgets.volume")

local cardid = 0
local channel = "Master"

-- command must start with a space!
local function mixercommand(w, command)
	local fd = io.popen("amixer -c " .. cardid .. command)
	local status = fd:read("*all")
	fd:close()
	local volume = string.match(status, "(%d?%d?%d)%%")
	status = string.match(status, "%[(o[^%]]*)%]")
	w:set_value(tonumber(volume))
	if string.find(status, "on", 1, true) then
		w:set_border_color(beautiful.fg_normal)
	else   
		w:set_border_color(beautiful.bg_normal)
	end
end

local function update(w)
	mixercommand(w, " sget " .. channel)
end
local function up()
	mixercommand(w, " sset " .. channel .. " 10%+")
end
local function down()
	mixercommand(w, " sset " .. channel .. " 10%-")
end
local function toggle()
	mixercommand(w, " sset " .. channel .. " toggle")
end

function new(args)
	local args = args or {}
	args.width = args.width or 8
	args.height = args.height or beautiful.wibox_height or 16
	w = awful.widget.progressbar(args)
	w:set_vertical(true)
	w:set_background_color(beautiful.bg_normal)
	w:set_color(beautiful.fg_normal)
	w:set_max_value(100)
	w:set_value(50)
	w.widget:buttons(awful.util.table.join(
		awful.button({}, 1, nil, function () toggle(w) end),
		awful.button({}, 4, nil, function () up(w) end),
		awful.button({}, 5, nil, function () down(w) end)
	))
	update(w)
	return w.widget
end

setmetatable(_M, { __call = function(_, ...) return new(...) end })