-- Inspired by https://awesome.naquadah.org/wiki/Farhavens_volume_widget local setmetatable = setmetatable local io = io local string = string local tonumber = tonumber local capi = { widget = widget } local awful = require('awful') local wibox = require('wibox') local beautiful = require('beautiful') module('widgets.volume') local cardid = nil local channel = 'Master' -- command must start with a space! local function mixercommand(w, command) local cmd = 'amixer' if cardid then cmd = cmd .. ' -c ' .. cardid end local fd = io.popen(cmd .. command) local status = fd:read('*all') fd:close() if status == '' then w.visible = false else local p = w:get_children_by_id('p')[1] local volume = string.match(status, '(%d?%d?%d)%%') status = string.match(status, '%[(o[^%]]*)%]') p.value = tonumber(volume) if string.find(status, 'on', 1, true) then p.border_color = beautiful.fg_normal else p.border_color = beautiful.bg_normal end end end local function update(w) mixercommand(w, ' sget ' .. channel) end local function up(w) mixercommand(w, ' sset ' .. channel .. ' 10%+') end local function down(w) mixercommand(w, ' sset ' .. channel .. ' 10%-') end local function toggle(w) mixercommand(w, ' sset ' .. channel .. ' toggle') end local function new(args) local args = args or {} local w = wibox.widget{ { image = beautiful.widget_vol, widget = wibox.widget.imagebox, }, { { id = 'p', forced_height = args.width or 8, forced_width = args.height or beautiful.wibox_height or 16, border_width = 1, color = beautiful.fg_normal, background_color = beautiful.bg_normal, value = 10, max_value = 100, widget = wibox.widget.progressbar, }, direction = 'east', layout = wibox.container.rotate, }, layout = wibox.layout.fixed.horizontal, } w:buttons(awful.util.table.join( awful.button({}, 1, nil, function () toggle(w) end), awful.button({}, 4, nil, function () up(w) end), awful.button({}, 5, nil, function () down(w) end) )) update(w) return w end setmetatable(_M, { __call = function(_, ...) return new(...) end })