volume: move volume functions into a lib

Also add some notification when changing the volume. Also move
sharetags to the `lib/` directory. And modify `loadrc` to be able to
load modules.
This commit is contained in:
Vincent Bernat 2012-07-14 16:29:19 +02:00
parent 5d55609657
commit 3415d39992
6 changed files with 79 additions and 11 deletions

52
lib/volume.lua Normal file
View file

@ -0,0 +1,52 @@
-- Handle volume (through pulseaudio)
local awful = require("awful")
local naughty = require("naughty")
local tonumber = tonumber
local string = string
local os = os
local setmetatable = setmetatable
module("volume")
local volid = nil
local function change(what)
os.execute("amixer -q sset Master " .. what, false)
-- Read the current value
local out = awful.util.pread("amixer sget Master")
local vol, mute = out:match("([%d]+)%%.*%[([%l]*)")
if not mute or not vol then return end
vol = tonumber(vol)
local icon = "high"
if mute ~= "on" or vol == 0 then
icon = "muted"
elseif vol < 30 then
icon = "low"
elseif vol < 60 then
icon = "medium"
end
icon = "/usr/share/icons/gnome/32x32/status/audio-volume-" .. icon .. ".png"
volid = naughty.notify({ text = string.format("%3d %%", vol),
icon = icon,
font = "Free Sans Bold 24",
replaces_id = volid }).id
end
function increase()
change("5%+")
end
function decrease()
change("5%-")
end
function toggle()
change("toggle")
end
-- run pavucontrol
function mixer()
awful.util.spawn("pavucontrol", false)
end

19
rc.lua
View file

@ -5,10 +5,19 @@ require("beautiful")
require("naughty")
-- Simple function to load additional LUA files from rc/.
function loadrc(name)
function loadrc(name, mod)
local success
local result
local path = awful.util.getdir("config") .. "/rc/" .. name .. ".lua"
-- Which file? In rc/ or in lib/?
local path = awful.util.getdir("config") .. "/" ..
(mod and "lib" or "rc") ..
"/" .. name .. ".lua"
-- If the module is already loaded, don't load it again
if mod and package.loaded[mod] then return package.loaded[mod] end
-- Execute the RC/module file
success, result = pcall(function() return dofile(path) end)
if not success then
naughty.notify({ title = "Error while loading an RC file",
@ -18,6 +27,12 @@ function loadrc(name)
})
return print("E: error loading RC file '" .. name .. "': " .. result)
end
-- Is it a module?
if mod then
return package.loaded[mod]
end
return result
end

View file

@ -1,5 +1,6 @@
config.keys = {}
config.mouse = {}
local volume = loadrc("volume", "volume")
local function client_info()
local v = ""
@ -79,9 +80,9 @@ config.keys.global = awful.util.table.join(
awful.key({ modkey, "Control" }, "r", awesome.restart),
-- Multimedia keys
awful.key({ }, "XF86AudioRaiseVolume", function () awful.util.spawn("amixer -q -c 0 set Master 2dB+", false) end),
awful.key({ }, "XF86AudioLowerVolume", function () awful.util.spawn("amixer -q -c 0 set Master 2dB-", false) end),
awful.key({ }, "XF86AudioMute", function () awful.util.spawn("amixer -q -c 0 set Master toggle", false) end)
awful.key({ }, "XF86AudioRaiseVolume", volume.increase),
awful.key({ }, "XF86AudioLowerVolume", volume.decrease),
awful.key({ }, "XF86AudioMute", volume.toggle)
)
config.keys.client = awful.util.table.join(

View file

@ -1,6 +1,6 @@
-- Tags
loadrc("sharetags")
sharetags = loadrc("sharetags", "sharetags")
local otags = config.tags
config.tags = {}

View file

@ -105,12 +105,12 @@ local volwidget = widget({ type = "textbox" })
vicious.register(volwidget, vicious.widgets.volume,
'<span font="Terminus 8" color="' .. beautiful.fg_widget_value .. '">$2 $1%</span>',
2, "Master")
volume = loadrc("volume", "volume")
volwidget:buttons(awful.util.table.join(
awful.button({ }, 1, function () awful.util.spawn("pavucontrol", false) end),
awful.button({ }, 3, function () awful.util.spawn("amixer -q sset Master toggle", false) end),
awful.button({ }, 4, function () awful.util.spawn("amixer -q sset Master 5%+", false) end),
awful.button({ }, 5, function () awful.util.spawn("amixer -q sset Master 5%-", false) end)
))
awful.button({ }, 1, volume.mixer),
awful.button({ }, 3, volume.toggle),
awful.button({ }, 4, volume.increase),
awful.button({ }, 5, volume.decrease)))
-- File systems
local fs = { ["/"] = "root",