X-Git-Url: https://git.rmz.io/dotfiles.git/blobdiff_plain/8144bf204ceb16f19e1d5a2943acd940e80e3704..f2430c7c2d870fbdd8aa3b056a3acfcb09498b13:/awesome/widgets/mpc.lua?ds=sidebyside diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index c8967c7..4da4a81 100644 --- a/awesome/widgets/mpc.lua +++ b/awesome/widgets/mpc.lua @@ -95,7 +95,6 @@ function mpc:_connect() self._output, self._input = nil, nil self:_error(err) else - do_read() line = tostring(line) if line == "OK" or line:match("^ACK ") then local success = line == "OK" @@ -112,9 +111,22 @@ function mpc:_connect() else local _, _, key, value = string.find(line, "([^:]+):%s(.+)") if key then - self._pending_reply[string.lower(key)] = value + if key == "binary" then + value = tonumber(value) + local data = {} + while value > 0 do + local b = assert(obj:read_bytes(value)) + table.insert(data, b.data) + value = value - #b + end + local w = obj:read_bytes(1) -- read newline at end of binary + self._pending_reply[string.lower(key)] = table.concat(data) + else + self._pending_reply[string.lower(key)] = value + end end end + do_read() end end) end @@ -203,6 +215,26 @@ 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 + +function mpc:currentsong() + local currentsong + self:send("currentsong", function(err, song) + if err then error(err) end + currentsong = song + end) + return currentsong +end + --[[ -- Example on how to use this (standalone)