lain/widgets/alsabar.lua

176 lines
5.0 KiB
Lua
Raw Normal View History

2013-09-07 12:06:42 +02:00
--[[
Licensed under GNU General Public License v2
* (c) 2013, Luke Bonham
* (c) 2013, Rman
2013-09-12 17:18:08 +02:00
2013-09-07 12:06:42 +02:00
--]]
2013-09-10 23:02:11 +02:00
local newtimer = require("lain.helpers").newtimer
2013-09-07 12:06:42 +02:00
local awful = require("awful")
local beautiful = require("beautiful")
local naughty = require("naughty")
2013-09-12 17:18:08 +02:00
local io = { popen = io.popen }
local math = { modf = math.modf }
2013-09-13 18:15:25 +02:00
local string = { format = string.format,
2013-09-12 17:18:08 +02:00
match = string.match,
rep = string.rep }
2013-09-07 12:06:42 +02:00
local tonumber = tonumber
local setmetatable = setmetatable
-- ALSA volume bar
-- lain.widgets.alsabar
local alsabar =
{
channel = "Master",
2013-09-12 17:18:08 +02:00
step = "5%",
2013-09-07 12:06:42 +02:00
colors =
{
background = beautiful.bg_normal,
2013-09-12 17:18:08 +02:00
mute = "#EB8F8F",
unmute = "#A4CE8A"
2013-09-07 12:06:42 +02:00
},
2013-09-10 23:02:11 +02:00
terminal = terminal or "xterm",
2013-09-12 17:18:08 +02:00
mixer = terminal .. " -e alsamixer",
2013-09-07 12:06:42 +02:00
notifications =
{
2013-09-12 17:18:08 +02:00
font = beautiful.font:sub(beautiful.font:find(""), beautiful.font:find(" ")),
2013-09-07 12:06:42 +02:00
font_size = "11",
2013-09-20 03:04:17 +02:00
color = beautiful.fg_normal,
2013-09-13 18:15:25 +02:00
bar_size = 18
2013-09-07 12:06:42 +02:00
},
_current_level = 0,
2013-09-12 17:18:08 +02:00
_muted = false
2013-09-07 12:06:42 +02:00
}
2013-09-12 17:18:08 +02:00
function alsabar.notify()
alsabar.update()
2013-09-07 12:06:42 +02:00
local preset =
{
2013-09-12 17:18:08 +02:00
title = "",
text = "",
timeout = 4,
font = alsabar.notifications.font .. " " ..
alsabar.notifications.font_size,
fg = alsabar.notifications.color
2013-09-07 12:06:42 +02:00
}
2013-09-12 17:18:08 +02:00
if alsabar._muted
then
2013-09-07 12:06:42 +02:00
preset.title = alsabar.channel .. " - Muted"
else
preset.title = alsabar.channel .. " - " .. alsabar._current_level * 100 .. "%"
end
2013-09-12 17:18:08 +02:00
int = math.modf(alsabar._current_level * alsabar.notifications.bar_size)
preset.text = "["
.. string.rep("|", int)
.. string.rep(" ", alsabar.notifications.bar_size - int)
.. "]"
2013-09-07 12:06:42 +02:00
if alsabar._notify ~= nil then
2013-09-12 17:18:08 +02:00
alsabar._notify = naughty.notify ({
replaces_id = alsabar._notify.id,
preset = preset
})
2013-09-07 12:06:42 +02:00
else
2013-09-12 17:18:08 +02:00
alsabar._notify = naughty.notify ({
preset = preset
})
2013-09-07 12:06:42 +02:00
end
end
2013-09-10 23:02:11 +02:00
local function worker(args)
2013-09-07 12:06:42 +02:00
local args = args or {}
2013-09-12 17:18:08 +02:00
local timeout = args.timeout or 4
2013-09-20 03:01:04 +02:00
local settings = args.settings or function() end
2013-09-07 12:06:42 +02:00
local width = args.width or 63
local height = args.heigth or 1
local ticks = args.ticks or false
2013-09-07 12:06:42 +02:00
local ticks_size = args.ticks_size or 7
local vertical = args.vertical or false
2013-09-12 17:18:08 +02:00
2013-09-07 12:06:42 +02:00
alsabar.channel = args.channel or alsabar.channel
alsabar.step = args.step or alsabar.step
alsabar.colors = args.colors or alsabar.colors
alsabar.notifications = args.notifications or alsabar.notifications
alsabar.bar = awful.widget.progressbar()
2013-09-10 23:02:11 +02:00
2013-09-07 12:06:42 +02:00
alsabar.bar:set_background_color(alsabar.colors.background)
alsabar.bar:set_color(alsabar.colors.unmute)
alsabar.tooltip = awful.tooltip({ objects = { alsabar.bar } })
alsabar.bar:set_width(width)
alsabar.bar:set_height(height)
alsabar.bar:set_ticks(ticks)
alsabar.bar:set_ticks_size(ticks_size)
alsabar.bar:set_vertical(vertical)
2013-09-07 12:06:42 +02:00
2013-09-12 17:18:08 +02:00
function alsabar.update()
2013-09-07 12:06:42 +02:00
-- Get mixer control contents
local f = io.popen("amixer get " .. alsabar.channel)
local mixer = f:read("*all")
f:close()
-- Capture mixer control state: [5%] ... ... [on]
2013-09-13 18:15:25 +02:00
local volu, mute = string.match(mixer, "([%d]+)%%.*%[([%l]*)")
2013-09-10 23:02:11 +02:00
2013-09-07 12:06:42 +02:00
if volu == nil then
2013-09-10 23:02:11 +02:00
volu = 0
mute = "off"
2013-09-07 12:06:42 +02:00
end
alsabar._current_level = tonumber(volu) / 100
alsabar.bar:set_value(alsabar._current_level)
2013-09-12 17:18:08 +02:00
if not mute and tonumber(volu) == 0 or mute == "off"
2013-09-07 12:06:42 +02:00
then
alsabar._muted = true
alsabar.tooltip:set_text (" [Muted] ")
alsabar.bar:set_color(alsabar.colors.mute)
else
alsabar._muted = false
2013-09-12 17:18:08 +02:00
alsabar.tooltip:set_text(string.format(" %s:%s ", alsabar.channel, volu))
2013-09-07 12:06:42 +02:00
alsabar.bar:set_color(alsabar.colors.unmute)
end
2013-09-20 03:01:04 +02:00
volume_now = {}
volume_now.level = tonumber(volu)
volume_now.status = mute
settings()
2013-09-07 12:06:42 +02:00
end
2013-09-12 17:18:08 +02:00
newtimer("alsabar", timeout, alsabar.update)
2013-09-07 12:06:42 +02:00
alsabar.bar:buttons (awful.util.table.join (
awful.button ({}, 1, function()
awful.util.spawn(alsabar.mixer)
end),
awful.button ({}, 3, function()
2013-09-12 17:18:08 +02:00
awful.util.spawn(string.format("amixer set %s toggle", alsabar.channel))
alsabar.update()
2013-09-07 12:06:42 +02:00
end),
awful.button ({}, 4, function()
2013-09-12 17:18:08 +02:00
awful.util.spawn(string.format("amixer set %s %s+", alsabar.channel, alsabar.step))
alsabar.update()
2013-09-07 12:06:42 +02:00
end),
awful.button ({}, 5, function()
2013-09-12 17:18:08 +02:00
awful.util.spawn(string.format("amixer set %s %s-", alsabar.channel, alsabar.step))
alsabar.update()
2013-09-07 12:06:42 +02:00
end)
))
2013-09-12 17:18:08 +02:00
return alsabar
2013-09-07 12:06:42 +02:00
end
return setmetatable(alsabar, { __call = function(_, ...) return worker(...) end })