]> git.rmz.io Git - dotfiles.git/blobdiff - awesome/widgets/mpc.lua
qutebrowser: add bindings for qute-pass
[dotfiles.git] / awesome / widgets / mpc.lua
index c8967c743a91290933fec3e66582945b3c0acf1a..a3c5b879cb967183977b607ab0eb2ca4d3e0a2f4 100644 (file)
@@ -95,7 +95,6 @@ function mpc:_connect()
                 self._output, self._input = nil, nil
                 self:_error(err)
             else
                 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"
                 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
                 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
                     end
                 end
+                do_read()
             end
         end)
     end
             end
         end)
     end
@@ -203,6 +215,50 @@ function mpc:toggle_play()
     end)
 end
 
     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
+
+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)
 --[[
 
 -- Example on how to use this (standalone)