]> git.rmz.io Git - dotfiles.git/blobdiff - awesome/widgets/mpc.lua
mpv: don't ignore vp9 codecs
[dotfiles.git] / awesome / widgets / mpc.lua
index 932a4a5f7b28180471d04d917509e80a8ba63561..ef56c43ed7ba7f2bb0076e09250dd304e7ca22a8 100644 (file)
@@ -32,7 +32,7 @@ function mpc.new(host, port, password, error_handler, ...)
         _try_reconnect = false,
         _idle_subsystems = { ... }
     }, { __index = mpc })
-    self:_connect()
+    self:connect()
     return self
 end
 
@@ -46,13 +46,9 @@ end
 function mpc:_error(err)
     self._connected = false
     self._error_handler(err)
-    self._try_reconnect = not self._try_reconnect
-    if self._try_reconnect then
-        self:_connect()
-    end
 end
 
-function mpc:_connect()
+function mpc:connect()
     if self._connected then return end
     -- Reset all of our state
     self._reply_handlers = {}
@@ -103,51 +99,52 @@ function mpc:_connect()
             if type(err) ~= "number" then
                 self._output, self._input = nil, nil
                 self:_error(err)
-            else
-                line = tostring(line)
-                if line == "OK" or line:match("^ACK ") then
-                    local success = line == "OK"
-                    local arg
-                    if success then
-                        arg = self._pending_reply
-                    else
-                        arg = { line }
-                    end
-                    local handler = self._reply_handlers[1]
-                    table.remove(self._reply_handlers, 1)
-                    self._pending_reply = {}
-                    handler(success, arg)
+                return
+            end
 
-                    if next(self._reply_handlers) == nil then
-                        self:_start_idle()
-                    end
+            line = tostring(line)
+            if line == "OK" or line:match("^ACK ") then
+                local success = line == "OK"
+                local arg
+                if success then
+                    arg = self._pending_reply
                 else
-                    local _, _, key, value = string.find(line, "([^:]+):%s(.+)")
-                    if key then
-                        local k = string.lower(key)
-                        if k == "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[k] = table.concat(data)
-                        elseif k == "changed" then
-                            if not self._pending_reply[k] then
-                                self._pending_reply[k] = {}
-                            end
-
-                            self._pending_reply[k][value] = true
-                        else
-                            self._pending_reply[k] = value
+                    arg = { line }
+                end
+                local handler = self._reply_handlers[1]
+                table.remove(self._reply_handlers, 1)
+                self._pending_reply = {}
+                handler(success, arg)
+
+                if next(self._reply_handlers) == nil then
+                    self:_start_idle()
+                end
+            else
+                local _, _, key, value = string.find(line, "([^:]+):%s(.+)")
+                if key then
+                    local k = string.lower(key)
+                    if k == "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[k] = table.concat(data)
+                    elseif k == "changed" then
+                        if not self._pending_reply[k] then
+                            self._pending_reply[k] = {}
                         end
+
+                        self._pending_reply[k][value] = true
+                    else
+                        self._pending_reply[k] = value
                     end
                 end
-                do_read()
             end
+            do_read()
         end)
     end
     do_read()
@@ -196,7 +193,7 @@ function mpc:_send(command, callback)
 end
 
 function mpc:send(...)
-    self:_connect()
+    self:connect()
     if not self._connected then
         return
     end
@@ -219,6 +216,14 @@ function mpc:toggle_play()
     end)
 end
 
+function mpc:play()
+    self:send("play")
+end
+
+function mpc:pause()
+    self:send("pause")
+end
+
 function clamp(x, min, max)
     return math.min(math.max(x, min), max)
 end