| 1 |
|
| 2 |
|
| 3 |
|
| 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 capi = { |
| 21 |
widget = widget, |
| 22 |
} |
| 23 |
local awful = require("awful") |
| 24 |
local lib = { |
| 25 |
hooks = require("obvious.lib.hooks"), |
| 26 |
markup = require("obvious.lib.markup") |
| 27 |
} |
| 28 |
|
| 29 |
local objects = { } |
| 30 |
|
| 31 |
local function get_data(channel) |
| 32 |
local rv = { } |
| 33 |
local fd = io.popen("mixer " .. channel) |
| 34 |
if not fd then return end |
| 35 |
local status = fd:read("*all") |
| 36 |
fd:close() |
| 37 |
|
| 38 |
rv.volume = tonumber(status:match(" +(%d?%d?%d)")) |
| 39 |
rv.mute = false |
| 40 |
|
| 41 |
local fd = io.popen("sysctl dev.acpi_ibm.0.mute") |
| 42 |
if fd then |
| 43 |
if tonumber(fd:read("*all"):match(": (%d)")) == 1 then |
| 44 |
rv.mute = true |
| 45 |
end |
| 46 |
end |
| 47 |
return rv |
| 48 |
end |
| 49 |
|
| 50 |
local function update(obj) |
| 51 |
local status = get_data(obj.channel) or { volume = 0, mute = true } |
| 52 |
|
| 53 |
local color = "#009000" |
| 54 |
if status.mute then |
| 55 |
color = "#900000" |
| 56 |
end |
| 57 |
obj.widget.text = lib.markup.fg.color(color, "☊") .. string.format(" %03d%%", status.volume) |
| 58 |
end |
| 59 |
|
| 60 |
local function update_by_values(channel) |
| 61 |
for i, v in pairs(objects) do |
| 62 |
if v.channel == channel then |
| 63 |
update(v) |
| 64 |
end |
| 65 |
end |
| 66 |
end |
| 67 |
|
| 68 |
local function raise(channel, v) |
| 69 |
v = v or 1 |
| 70 |
awful.util.spawn("mixer " .. channel .. " +" .. v, false) |
| 71 |
update_by_values(channel) |
| 72 |
end |
| 73 |
|
| 74 |
local function lower(channel, v) |
| 75 |
v = v or 1 |
| 76 |
awful.util.spawn("mixer " .. channel .. " -" .. v, false) |
| 77 |
update_by_values(channel) |
| 78 |
end |
| 79 |
|
| 80 |
local function create(_, channel) |
| 81 |
local channel = channel or "vol" |
| 82 |
|
| 83 |
local obj = { channel = channel } |
| 84 |
|
| 85 |
local widget = capi.widget({ type = "textbox" }) |
| 86 |
obj.widget = widget |
| 87 |
obj[1] = widget |
| 88 |
obj.update = function() update(obj) end |
| 89 |
|
| 90 |
widget:buttons(awful.util.table.join( |
| 91 |
awful.button({ }, 4, function () raise(obj.channel, 1) obj.update() end), |
| 92 |
awful.button({ }, 5, function () lower(obj.channel, 1) obj.update() end), |
| 93 |
awful.button({ "Shift" }, 4, function () raise(obj.channel, 10) obj.update() end), |
| 94 |
awful.button({ "Shift" }, 5, function () lower(obj.channel, 10) obj.update() end), |
| 95 |
awful.button({ "Control" }, 4, function () raise(obj.channel, 5) obj.update() end), |
| 96 |
awful.button({ "Control" }, 5, function () lower(obj.channel, 5) obj.update() end) |
| 97 |
)) |
| 98 |
|
| 99 |
obj.set_layout = function(obj, layout) obj.layout = layout return obj end |
| 100 |
obj.set_channel = function(obj, id) obj.channel = id obj.update() return obj end |
| 101 |
obj.raise = function(obj, v) raise(obj.channel, v) return obj end |
| 102 |
obj.lower = function(obj, v) lower(obj.channel, v) return obj end |
| 103 |
|
| 104 |
obj.update() |
| 105 |
lib.hooks.timer.register(10, 30, obj.update, "Update for the volume widget") |
| 106 |
lib.hooks.timer.start(obj.update) |
| 107 |
|
| 108 |
table.insert(objects, obj) |
| 109 |
return obj |
| 110 |
end |
| 111 |
|
| 112 |
return setmetatable({ |
| 113 |
raise = raise, |
| 114 |
lower = lower, |
| 115 |
}, { __call = create }) |
| 116 |
|
| 117 |
|