X-Git-Url: https://git.rvb.name/mpd-lua.git/blobdiff_plain/7e7b0d2b9ec3b3c62370de418cf9b0a4a667b1fb..55262d4cf0ac84eb33fafca62bdcae22a4d478b3:/mpd.lua diff --git a/mpd.lua b/mpd.lua index 0200a8a..31275b6 100755 --- a/mpd.lua +++ b/mpd.lua @@ -1,5 +1,9 @@ #!/usr/bin/lua +require "uci" +require("socket") +json=require("json") + function url_decode(str) if not str then return nil end str = string.gsub (str, "+", " ") @@ -65,9 +69,6 @@ function process_directory(directory) return res end -require "uci" -require("socket") - function mpd_new(settings) local client = {} if settings == nil then settings = {} end @@ -82,7 +83,6 @@ function mpd_new(settings) return client end - function mpd_send(mpd,action,raw) local command = string.format("%s\n", action) @@ -160,13 +160,15 @@ function mpd_send(mpd,action,raw) return values end - -json=require("json") - x = uci.cursor() + settings = {} settings['host'] = x.get("mpd","server","host") or "localhost" settings['port'] = x.get("mpd","server","port") or 6600 +settings['timeout'] = x.get("mpd","server","timeout") or 1 + +volstep = x.get("mpd","control","volume_step") or 3 + password = x.get("mpd","server","password") if password then settings["password"] = password @@ -177,62 +179,89 @@ m = mpd_new(settings) command = url_decode(os.getenv('QUERY_STRING')) if not command or command=="" then - command="status" + command="idle" end if command=="play" or command=="pause" or command=="stop" or command=="previous" or command=="next" then + res=mpd_send(m,command) + +elseif command=="idle" then + + m.timeout=30 + res=mpd_send(m,command) + elseif command=="vold" then + status=mpd_send(m,"status") volume=tonumber(status["volume"]) - res=mpd_send(m,"setvol "..(volume-3)) + res=mpd_send(m,"setvol "..(volume-volstep)) + elseif command=="volu" then + status=mpd_send(m,"status") volume=tonumber(status["volume"]) - res=mpd_send(m,"setvol "..(volume+3)) + res=mpd_send(m,"setvol "..(volume+volstep)) + elseif command=="status" then + res=mpd_send(m,"status") song=res["song"] playlist=mpd_send(m,"playlist",1) pl=process_playlist(playlist) + if song then res['current_playing']=pl[song]['name'] else res['current_playing']="No songs selected" end + elseif command=="playlist" then + playlist=mpd_send(m,"playlist",1) res=process_playlist(playlist) + elseif command=="repeat" then + status=mpd_send(m,"status") rep=1-status["repeat"] res=mpd_send(m,"repeat "..rep) + elseif string.starts(command,"cpl") then + cmd=split(command,"|") id=cmd[3] command=cmd[2] + if command=="playitem" then command="play "..id res=mpd_send(m,command) end + if command=="clear" then res=mpd_send(m,"clear") end + if command=="remove" then command="delete "..id res=mpd_send(m,command) end + if command=="moveup" then command="swap "..id.." "..(id-1) res=mpd_send(m,command) end + if command=="movedown" then command="swap "..id.." "..(id+1) res=mpd_send(m,command) end + elseif string.starts(command,"lists") then + cmd=split(command,"|") command=cmd[2] + if command=="load" then if not cmd[3] then lists=mpd_send(m,"listplaylists",1) @@ -241,12 +270,15 @@ elseif string.starts(command,"lists") then res=mpd_send(m,"load "..cmd[3],1) end end + if command=="save" and cmd[3] then res=mpd_send(m,"save "..cmd[3],1) end + if command=="delete" and cmd[3] then res=mpd_send(m,"rm "..cmd[3],1) end + if command=="edit" then if cmd[3] then dir=mpd_send(m,"lsinfo \""..cmd[3].."\"",1) @@ -255,12 +287,14 @@ elseif string.starts(command,"lists") then end res=process_directory(dir) end + if command=="add" then res={} if cmd[3] then res=mpd_send(m,"add \""..cmd[3].."\"") end end + end if not res then