diff --git a/rc/sharetags.lua b/lib/sharetags.lua similarity index 100% rename from rc/sharetags.lua rename to lib/sharetags.lua diff --git a/lib/volume.lua b/lib/volume.lua new file mode 100644 index 0000000..6507136 --- /dev/null +++ b/lib/volume.lua @@ -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 diff --git a/rc.lua b/rc.lua index 711ea2f..f847f56 100644 --- a/rc.lua +++ b/rc.lua @@ -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 diff --git a/rc/bindings.lua b/rc/bindings.lua index 8d24386..c156658 100644 --- a/rc/bindings.lua +++ b/rc/bindings.lua @@ -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( diff --git a/rc/tags.lua b/rc/tags.lua index 0f5fa4b..6a7c671 100644 --- a/rc/tags.lua +++ b/rc/tags.lua @@ -1,6 +1,6 @@ -- Tags -loadrc("sharetags") +sharetags = loadrc("sharetags", "sharetags") local otags = config.tags config.tags = {} diff --git a/rc/widgets.lua b/rc/widgets.lua index 006aff1..4ae44e5 100644 --- a/rc/widgets.lua +++ b/rc/widgets.lua @@ -105,12 +105,12 @@ local volwidget = widget({ type = "textbox" }) vicious.register(volwidget, vicious.widgets.volume, '$2 $1%', 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",