X-Git-Url: https://git.rmz.io/dotfiles.git/blobdiff_plain/4264968ad117b6d08217808f72d32032a3f6b253..382e25f7d8c569293f3182f10c3b15ffb93b5e90:/awesome/widgets/mpc.lua diff --git a/awesome/widgets/mpc.lua b/awesome/widgets/mpc.lua index f357f18..a3c5b87 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 @@ -214,6 +226,39 @@ function mpc:change_volume(change) 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 + +local function escape(str) + return "\"" .. str .. "\"" +end + +function mpc:albumart(uri, handler) + local image_table = {} + local get_art_at + get_art_at = function(off) + self:send("albumart " .. escape(uri) .. " " .. tostring(off), function(success, data) + if not success then + handler(success, data) + end + table.insert(image_table, data.binary) + if data.binary and #data.binary > 0 then + get_art_at(off + #data.binary) + else + data.binary = table.concat(image_table) + handler(success, data) + end + end) + end + get_art_at(0) +end + --[[ -- Example on how to use this (standalone)