From: Samir Benmendil Date: Sun, 2 Aug 2020 20:36:13 +0000 (+0100) Subject: awesome: new mpd widget using mpc.lua X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/4264968ad117b6d08217808f72d32032a3f6b253 awesome: new mpd widget using mpc.lua --- diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index c8967c7..f357f18 100644 --- a/awesome/widgets/mpc.lua +++ b/awesome/widgets/mpc.lua @@ -203,6 +203,17 @@ function mpc:toggle_play() end) end +function clamp(x, min, max) + return math.min(math.max(x, min), max) +end + +function mpc:change_volume(change) + self:send("status", function(_, status) + new_vol = clamp(tonumber(status.volume) + change, 0, 100) + self:send("setvol " .. new_vol) + end) +end + --[[ -- Example on how to use this (standalone) diff --git a/awesome/widgets/mpd_widget.lua b/awesome/widgets/mpd_widget.lua new file mode 100644 index 0000000..692e9a2 --- /dev/null +++ b/awesome/widgets/mpd_widget.lua @@ -0,0 +1,48 @@ +local mpc = require("widgets/mpc") +local textbox = require("wibox.widget.textbox") +local timer = require("gears.timer") + +mpd_widget = textbox() + +local function update_widget() + local text = "" + if state == "pause" then + text = "|| " + end + text = text .. tostring(artist or "") .. " - " .. tostring(title or "") + mpd_widget.text = text +end + +local connection +local function error_handler(err) + mpd_widget:set_text("Error: " .. tostring(err)) + timer.start_new(10, function() + connection:send("ping") + end) +end + +connection = mpc.new(nil, nil, nil, error_handler, + "status", function(_, result) + state = result.state + end, + "currentsong", function(_, result) + title, artist, file = result.title, result.artist, result.file + pcall(update_widget) + end +) + +mpd_widget:buttons(gears.table.join( + awful.button({}, 1, function() connection:toggle_play() end) +, awful.button({}, 4, function() connection:change_volume(5) end) +, awful.button({}, 5, function() connection:change_volume(-5) end)) +) + +globalkeys = gears.table.join(globalkeys, + awful.key({ modkey }, "p", function() connection:toggle_play() end, + { description = "toogle play", group = "mpd" }), + awful.key({ modkey }, ",", function() connection:send("previous") end, + { description = "previous track", group = "mpd" }), + awful.key({ modkey }, ".", function() connection:send("next") end, + { description = "next track", group = "mpd" }) +) +return mpd_widget