]> git.rmz.io Git - dotfiles.git/commitdiff
awesome: new mpd widget using mpc.lua
authorSamir Benmendil <me@rmz.io>
Sun, 2 Aug 2020 20:36:13 +0000 (21:36 +0100)
committerSamir Benmendil <me@rmz.io>
Sat, 8 Aug 2020 10:48:16 +0000 (11:48 +0100)
awesome/widgets/mpc.lua
awesome/widgets/mpd_widget.lua [new file with mode: 0644]

index c8967c743a91290933fec3e66582945b3c0acf1a..f357f18b48b54dd85a70241b5a41dc7e2534e818 100644 (file)
@@ -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 (file)
index 0000000..692e9a2
--- /dev/null
@@ -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