From 20fae489af0ff9d111dd6a161465305da7395c93 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 17 Sep 2017 23:15:40 +0200 Subject: widget/volume: port to 4.0 --- widgets/volume.lua | 80 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 32 deletions(-) (limited to 'widgets/volume.lua') diff --git a/widgets/volume.lua b/widgets/volume.lua index 122714a..172931d 100644 --- a/widgets/volume.lua +++ b/widgets/volume.lua @@ -5,67 +5,83 @@ local io = io local string = string local tonumber = tonumber local capi = { widget = widget } -local awful = require ("awful") -local beautiful = require ("beautiful") +local awful = require('awful') +local wibox = require('wibox') +local beautiful = require('beautiful') -module("widgets.volume") +module('widgets.volume') local cardid = nil -local channel = "Master" +local channel = 'Master' -- command must start with a space! local function mixercommand(w, command) - cmd = "amixer" + local cmd = 'amixer' if cardid then - cmd = cmd .. " -c " .. cardid + cmd = cmd .. ' -c ' .. cardid end local fd = io.popen(cmd .. command) - local status = fd:read("*all") + local status = fd:read('*all') fd:close() if status == '' then - w.widget.visible = false + w.visible = false else - local volume = string.match(status, "(%d?%d?%d)%%") - status = string.match(status, "%[(o[^%]]*)%]") - w:set_value(tonumber(volume)) - if string.find(status, "on", 1, true) then - w:set_border_color(beautiful.fg_normal) + 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 - w:set_border_color(beautiful.bg_normal) + p.border_color = beautiful.bg_normal end end end local function update(w) - mixercommand(w, " sget " .. channel) + mixercommand(w, ' sget ' .. channel) end -local function up() - mixercommand(w, " sset " .. channel .. " 10%+") +local function up(w) + mixercommand(w, ' sset ' .. channel .. ' 10%+') end -local function down() - mixercommand(w, " sset " .. channel .. " 10%-") +local function down(w) + mixercommand(w, ' sset ' .. channel .. ' 10%-') end -local function toggle() - mixercommand(w, " sset " .. channel .. " toggle") +local function toggle(w) + mixercommand(w, ' sset ' .. channel .. ' toggle') end -function new(args) +local function new(args) local args = args or {} - args.width = args.width or 8 - args.height = args.height or beautiful.wibox_height or 16 - w = awful.widget.progressbar(args) - w:set_vertical(true) - w:set_background_color(beautiful.bg_normal) - w:set_color(beautiful.fg_normal) - w:set_max_value(100) - w:set_value(50) - w.widget:buttons(awful.util.table.join( + 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.widget + return w end setmetatable(_M, { __call = function(_, ...) return new(...) end }) -- cgit v1.2.3