Debian Codegraph @

obvious/volume_alsa/init.lua

raw symbols SCIP
1 --------------------------------
2 -- Author: Gregor Best --
3 -- Copyright 2009 Gregor Best --
4 --------------------------------
5
6 local setmetatable = setmetatable
7 local tonumber = tonumber
8 local pairs = pairs
9 local io = {
10 popen = io.popen
11 }
12 local string = {
13 match = string.match,
14 find = string.find,
15 format = string.format
16 }
17 local table = {
18 insert = table.insert
19 }
20 local awful = require("awful")
21 local wibox = require("wibox")
22 local lib = {
23 hooks = require("obvious.lib.hooks"),
24 markup = require("obvious.lib.markup")
25 }
26
27 local volume_alsa = {}
28
29 local objects = { }
30
31 function volume_alsa.get_data(cardid, channel)
32 local rv = { }
33 local fd = io.popen("amixer -c " .. cardid .. " -- sget " .. channel)
34 if not fd then return end
35 local status = fd:read("*all")
36 fd:close()
37
38 rv.volume = tonumber(string.match(status, "(%d?%d?%d)%%"))
39 if not rv.volume then
40 rv.volume = ""
41 end
42
43 status = string.match(status, "%[(o[^%]]*)%]")
44 if not status then status = "on" end
45 if string.find(status, "on", 1, true) then
46 rv.mute = false
47 else
48 rv.mute = true
49 end
50
51 return rv
52 end
53
54 local function update(obj)
55 local status = volume_alsa.get_data(obj.cardid, obj.channel) or { mute = true, volume = 0 }
56
57 local color = "#900000"
58 if not status.mute then
59 color = "#009000"
60 end
61
62 local format = " %03d%%"
63 if status.volume == "" then
64 format = ""
65 end
66
67 obj.widget:set_markup(lib.markup.fg.color(color, "" .. obj.abrv .. "" ) .. string.format(format, status.volume))
68 end
69
70 local function update_by_values(cardid, channel)
71 for i, v in pairs(objects) do
72 if v.channel == channel and v.cardid == cardid then
73 update(v)
74 end
75 end
76 end
77
78 function volume_alsa.raise(cardid, channel, v)
79 v = v or 1
80 awful.util.spawn("amixer -q -c " .. cardid .. " sset " .. channel .. " " .. v .. "+", false)
81 update_by_values(cardid, channel)
82 end
83
84 function volume_alsa.lower(cardid, channel, v)
85 v = v or 1
86 awful.util.spawn("amixer -q -c " .. cardid .. " sset " .. channel .. " " .. v .. "-", false)
87 update_by_values(cardid, channel)
88 end
89
90 function volume_alsa.mute(cardid, channel)
91 awful.util.spawn("amixer -c " .. cardid .. " sset " .. channel .. " toggle", false)
92 update_by_values(cardid, channel)
93 end
94
95 local function mixer(term, cardid)
96 awful.util.spawn(term .. " -e 'alsamixer -c " .. cardid .. "'")
97 end
98
99 local function create(_, cardid, channel, abrv)
100 local cardid = cardid or 0
101 local channel = channel or "Master"
102 local abrv = abrv or "M"
103
104 local obj = {
105 cardid = cardid,
106 channel = channel,
107 abrv = abrv,
108 term = "x-terminal-emulator -T Mixer"
109 }
110
111 local widget = wibox.widget.textbox()
112 obj.widget = widget
113 obj[1] = widget
114 obj.update = function() update(obj) end
115
116 widget:buttons(awful.util.table.join(
117 awful.button({ }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 1) obj.update() end),
118 awful.button({ }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 1) obj.update() end),
119 awful.button({ "Shift" }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 10) obj.update() end),
120 awful.button({ "Shift" }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 10) obj.update() end),
121 awful.button({ "Control" }, 4, function () volume_alsa.raise(obj.cardid, obj.channel, 5) obj.update() end),
122 awful.button({ "Control" }, 5, function () volume_alsa.lower(obj.cardid, obj.channel, 5) obj.update() end),
123 awful.button({ }, 1, function () volume_alsa.mute(obj.cardid, obj.channel) obj.update() end),
124 awful.button({ }, 3, function () mixer(obj.term, obj.cardid) obj.update() end)
125 ))
126
127 obj.set_layout = function(obj, layout) obj.layout = layout return obj end
128 obj.set_cardid = function(obj, id) obj.cardid = id obj.update() return obj end
129 obj.set_channel = function(obj, id) obj.channel = id obj.update() return obj end
130 obj.set_abrv = function(obj, id) obj.abrv = id obj.update() return obj end
131 obj.set_term = function(obj, term) obj.term = term return obj end
132 obj.raise = function(obj, v) volume_alsa.raise(obj.cardid, obj.channel, v) return obj end
133 obj.lower = function(obj, v) volume_alsa.lower(obj.cardid, obj.channel, v) return obj end
134 obj.mute = function(obj, v) volume_alsa.mute(obj.cardid, obj.channel, v) return obj end
135
136 obj.update()
137 lib.hooks.timer.register(10, 30, obj.update, "Update for the volume widget")
138 lib.hooks.timer.start(obj.update)
139
140 table.insert(objects, obj)
141 return widget
142 end
143
144 return setmetatable(volume_alsa, { __call = create })
145
146 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et