luampd lib
This commit is contained in:
parent
7b5f343a55
commit
c420f3581f
28
luampd.lua
28
luampd.lua
|
@ -584,12 +584,28 @@ function luampd:isearch(stype, swhat)
|
||||||
local found = self:search(stype, swhat)
|
local found = self:search(stype, swhat)
|
||||||
local count = 0
|
local count = 0
|
||||||
local size = table.maxn(found)
|
local size = table.maxn(found)
|
||||||
return function()
|
return reverse_iterator(function()
|
||||||
count = count + 1
|
count = count + 1
|
||||||
if count <= size then
|
if count <= size then
|
||||||
return found[count]
|
return found[count]
|
||||||
end
|
end
|
||||||
end, size
|
end, size), size
|
||||||
|
end
|
||||||
|
|
||||||
|
function reverse_iterator(iter, size)
|
||||||
|
local reverse = {}
|
||||||
|
i=0
|
||||||
|
for elem in iter do
|
||||||
|
reverse[size - i] = elem
|
||||||
|
i = i+1
|
||||||
|
end
|
||||||
|
local count = 0
|
||||||
|
return function()
|
||||||
|
count = count + 1
|
||||||
|
if count <= size then
|
||||||
|
return reverse[count]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function luampd:iadd(file_iterator)
|
function luampd:iadd(file_iterator)
|
||||||
|
@ -598,4 +614,12 @@ function luampd:iadd(file_iterator)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function luampd:idle()
|
||||||
|
self.socket:send('idle\n')
|
||||||
|
end
|
||||||
|
|
||||||
|
function luampd:noidle()
|
||||||
|
self.socket:send('noidle\n')
|
||||||
|
end
|
||||||
|
|
||||||
return luampd
|
return luampd
|
||||||
|
|
50
mpd.lua
50
mpd.lua
|
@ -1,10 +1,10 @@
|
||||||
local luampd = require("luampd")
|
--assert(package.loadlib(MY_PATH .. "./mpdc.so", "luaopen_mpdc"))
|
||||||
|
require "mpdc"
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local type = ""
|
local type = ""
|
||||||
|
|
||||||
-- local functions
|
-- local functions
|
||||||
local show, mpc_play_search, notify, idlecall
|
local show, mpc_play_search, notify
|
||||||
|
|
||||||
local conn = nil
|
local conn = nil
|
||||||
|
|
||||||
|
@ -23,12 +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 = mpdc.open(settings.host, settings.port)
|
||||||
conn = luampd:new({
|
|
||||||
hostname = settings.hostname,
|
|
||||||
port = settings.port,
|
|
||||||
debug = false
|
|
||||||
})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.disconnect = function()
|
M.disconnect = function()
|
||||||
|
@ -37,17 +32,8 @@ M.disconnect = function()
|
||||||
end
|
end
|
||||||
|
|
||||||
M.ensure_connection = function()
|
M.ensure_connection = function()
|
||||||
-- connect on first call and go into idle mode
|
-- connect on first call
|
||||||
if conn == nil then M.connect() conn:idle() end
|
if conn == nil then M.connect() end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
idlecall = function(command)
|
|
||||||
M.ensure_connection()
|
|
||||||
-- unidle, send commands and go back to idling
|
|
||||||
conn:noidle()
|
|
||||||
command()
|
|
||||||
conn:idle()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
@ -57,32 +43,30 @@ end
|
||||||
M.ctrl = {}
|
M.ctrl = {}
|
||||||
|
|
||||||
M.ctrl.toggle = function ()
|
M.ctrl.toggle = function ()
|
||||||
idlecall(function()
|
M.ensure_connection()
|
||||||
local status = conn:status()
|
conn:toggle()
|
||||||
if status["state"] == "pause" or status["state"] == "stop" then
|
|
||||||
conn:play()
|
|
||||||
else
|
|
||||||
conn:pause()
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
M.ctrl.play = function ()
|
M.ctrl.play = function ()
|
||||||
idlecall(function() conn:play() end)
|
M.ensure_connection()
|
||||||
|
conn:play()
|
||||||
-- TODO widget updating
|
-- TODO widget updating
|
||||||
end
|
end
|
||||||
|
|
||||||
M.ctrl.pause = function ()
|
M.ctrl.pause = function ()
|
||||||
idlecall(function() conn:pause() end)
|
M.ensure_connection()
|
||||||
|
conn:pause()
|
||||||
end
|
end
|
||||||
|
|
||||||
M.ctrl.next = function ()
|
M.ctrl.next = function ()
|
||||||
idlecall(function() conn:next_() end)
|
M.ensure_connection()
|
||||||
|
conn:next()
|
||||||
-- TODO widget updating
|
-- TODO widget updating
|
||||||
end
|
end
|
||||||
|
|
||||||
M.ctrl.prev = function ()
|
M.ctrl.prev = function ()
|
||||||
idlecall(function() conn:previous() end)
|
M.ensure_connection()
|
||||||
|
conn:prev()
|
||||||
-- TODO widget updating
|
-- TODO widget updating
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -132,13 +116,11 @@ function show()
|
||||||
end
|
end
|
||||||
|
|
||||||
function mpc_play_search(s)
|
function mpc_play_search(s)
|
||||||
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
|
||||||
|
|
||||||
-- }}}
|
-- }}}
|
||||||
|
|
Loading…
Reference in a new issue