summaryrefslogtreecommitdiff
path: root/widgets/volume.lua
blob: e73ad37b374b72e8d657749be0f516d19abaa024 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local awful = require('awful')
local wibox = require('wibox')
local beautiful = require('beautiful')

local cardid = nil
local channel = 'Master'

local function mixercommand(w, command, arg)
    local cmd = {'amixer'}
    if cardid then
        table.insert(cmd, '-c')
        table.insert(cmd, cardid)
    end
    table.insert(cmd, command)
    table.insert(cmd, channel)
    if arg then
        table.insert(cmd, arg)
    end
    awful.spawn.easy_async(cmd, function(out)
        if out == '' then
            w.visible = false
        else
            local p = w:get_children_by_id('p')[1]
            local volume = string.match(out, '(%d?%d?%d)%%')
            local status = string.match(out, '%[(o[^%]]*)%]')
            p.value = tonumber(volume)
            if status == 'on' then
                p.border_color = beautiful.fg_normal
            else
                p.border_color = beautiful.bg_normal
            end
        end

    end)
end

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

local function new(args)
    local args = args or {}
    local w = wibox.widget{
        {
            image = beautiful.widget_vol,
            widget = wibox.widget.imagebox,
        },
        {
            {
                id = 'p',
                forced_height = args.width or 8,
                forced_width = args.height or beautiful.wibox_height or 16,
                border_width = 1,
                color = beautiful.fg_normal,
                background_color = beautiful.bg_normal,
                value = 10,
                max_value = 100,
                widget = wibox.widget.progressbar,
            },
            direction = 'east',
            layout = wibox.container.rotate,
        },
        layout = wibox.layout.fixed.horizontal,
    }
    w: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
end

return new