mpd before change to lmpdc

This commit is contained in:
crater2150 2012-06-20 09:55:53 +02:00
parent e37b2d3403
commit 7b5f343a55

35
mpd.lua
View file

@ -4,7 +4,7 @@ local M = {}
local type = "" local type = ""
-- local functions -- local functions
local show, mpc_play_search, notify local show, mpc_play_search, notify, idlecall
local conn = nil local conn = nil
@ -23,6 +23,7 @@ end
M.connect = function () M.connect = function ()
print("Connecting to mpd") print("Connecting to mpd")
pcall(function() if conn == nil then conn:close() end end)
conn = luampd:new({ conn = luampd:new({
hostname = settings.hostname, hostname = settings.hostname,
port = settings.port, port = settings.port,
@ -36,8 +37,17 @@ M.disconnect = function()
end end
M.ensure_connection = function() M.ensure_connection = function()
-- luampd throws SOCKET_ERRORs all the time. catch them and reconnect -- connect on first call and go into idle mode
if conn == nil or not pcall(conn:status()) then M.connect() end if conn == nil then M.connect() conn:idle() end
end
idlecall = function(command)
M.ensure_connection()
-- unidle, send commands and go back to idling
conn:noidle()
command()
conn:idle()
end end
-- }}} -- }}}
@ -47,36 +57,32 @@ end
M.ctrl = {} M.ctrl = {}
M.ctrl.toggle = function () M.ctrl.toggle = function ()
M.ensure_connection() idlecall(function()
local status = conn:status() local status = conn:status()
if status["state"] == "pause" or status["state"] == "stop" then if status["state"] == "pause" or status["state"] == "stop" then
conn:play() conn:play()
else else
conn:pause() conn:pause()
end end
end)
end end
M.ctrl.play = function () M.ctrl.play = function ()
M.ensure_connection() idlecall(function() conn:play() end)
conn:play()
-- TODO widget updating -- TODO widget updating
end end
M.ctrl.pause = function () M.ctrl.pause = function ()
M.ensure_connection() idlecall(function() conn:pause() end)
conn:pause()
-- TODO widget updating
end end
M.ctrl.next = function () M.ctrl.next = function ()
M.ensure_connection() idlecall(function() conn:next_() end)
conn:next_()
-- TODO widget updating -- TODO widget updating
end end
M.ctrl.prev = function () M.ctrl.prev = function ()
M.ensure_connection() idlecall(function() conn:previous() end)
conn:previous()
-- TODO widget updating -- TODO widget updating
end end
@ -126,12 +132,13 @@ function show()
end end
function mpc_play_search(s) function mpc_play_search(s)
M.ensure_connection() idlecall(function()
if clear_before then conn:clear() end if clear_before then conn:clear() end
local result, num = conn:isearch(type, s) local result, num = conn:isearch(type, s)
notify("Found " .. (num) .. " matches"); notify("Found " .. (num) .. " matches");
conn:iadd(result) conn:iadd(result)
conn:play() conn:play()
end)
end end
-- }}} -- }}}