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)
--- /dev/null
+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