aboutsummaryrefslogtreecommitdiff
path: root/mpd.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-12 21:56:17 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-12 21:56:17 +0100
commit8482b5407cf1a17b39ce7d2b992acd13a893da25 (patch)
tree45b8e28779e692e26ddc0282c685068cde9e396f /mpd.lua
parentdda51b1e34c3c3e5f8fe7f08616e125812da1d5b (diff)
downloadvicious-legacy-8482b5407cf1a17b39ce7d2b992acd13a893da25.tar.xz
mpd: rewritten and now uses curl not mpc
Widget type uses curl now, like all other types accessing network resources (until, if ever, we switch to luasocket). Where previously only the currently playing song was returned now you can access these keys: {volume}, {state}, {Artist}, {Title}, {Album}, {Genre}. You can provide an optional table argument to change password, host or port.
Diffstat (limited to 'mpd.lua')
-rw-r--r--mpd.lua58
1 files changed, 35 insertions, 23 deletions
diff --git a/mpd.lua b/mpd.lua
index 2bd176a..0764e53 100644
--- a/mpd.lua
+++ b/mpd.lua
@@ -1,46 +1,58 @@
---------------------------------------------------
-- Licensed under the GNU General Public License v2
-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
--- * (c) 2009, Lucas de Vries <lucas@glacicle.com>
---------------------------------------------------
-- {{{ Grab environment
-local type = type
+local tonumber = tonumber
local io = { popen = io.popen }
local setmetatable = setmetatable
-local string = { find = string.find }
+local string = { gmatch = string.gmatch }
local helpers = require("vicious.helpers")
-- }}}
--- Mpd: provides the currently playing song in MPD
+-- Mpd: provides Music Player Daemon information
module("vicious.mpd")
-- {{{ MPD widget type
local function worker(format, warg)
- -- Get data from mpc
- local f = io.popen("mpc")
- local np = f:read("*line")
- f:close()
-
- -- Not installed,
- if np == nil or -- off or stoppped.
- (string.find(np, "MPD_HOST") or string.find(np, "volume:"))
- then
- return {"Stopped"}
- end
-
- -- Check if we should scroll, or maybe truncate
- if warg then
- if type(warg) == "table" then
- np = helpers.scroll(np, warg[1], warg[2])
- else
- np = helpers.truncate(np, warg)
+ local mpd_state = {
+ ["{volume}"] = 0,
+ ["{state}"] = "N/A",
+ ["{Artist}"] = "N/A",
+ ["{Title}"] = "N/A",
+ ["{Album}"] = "N/A",
+ ["{Genre}"] = "N/A"
+ }
+
+ -- Fallback to MPD defaults
+ local pass = warg and warg[1] or "\"\""
+ local host = warg and warg[2] or "127.0.0.1"
+ local port = warg and warg[3] or "6600"
+
+ -- Construct MPD client options
+ local mpdh = "telnet://"..host..":"..port
+ local echo = "echo 'password "..pass.."\nstatus\ncurrentsong\nclose'"
+
+ -- Get data from MPD server
+ local f = io.popen(echo.." | curl --connect-timeout 1 -fsm 3 "..mpdh)
+
+ for line in f:lines() do
+ for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
+ if k == "volume" then mpd_state["{"..k.."}"] = v and tonumber(v)
+ elseif k == "state" then mpd_state["{"..k.."}"] = helpers.capitalize(v)
+ elseif k == "Artist" then mpd_state["{"..k.."}"] = helpers.escape(v)
+ elseif k == "Title" then mpd_state["{"..k.."}"] = helpers.escape(v)
+ elseif k == "Album" then mpd_state["{"..k.."}"] = helpers.escape(v)
+ elseif k == "Genre" then mpd_state["{"..k.."}"] = helpers.escape(v)
+ end
end
end
+ f:close()
- return {helpers.escape(np)}
+ return mpd_state
end
-- }}}