aboutsummaryrefslogtreecommitdiff
path: root/widgets/mpd.lua
diff options
context:
space:
mode:
authorAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
committerAdrian C. (anrxc) <anrxc@sysphere.org>2010-03-14 01:55:33 +0100
commit237470c8f45190b213e3a173ce6ae1a74b3e11fe (patch)
tree7f53c8144761947d4bde20715bcad34f4be0d6c0 /widgets/mpd.lua
parent9a82d4113a8271b7dfc7506f2b07379e3ede89a8 (diff)
downloadvicious-legacy-237470c8f45190b213e3a173ce6ae1a74b3e11fe.tar.xz
API: transform widgets namespace table to a directory
Diffstat (limited to 'widgets/mpd.lua')
-rw-r--r--widgets/mpd.lua59
1 files changed, 59 insertions, 0 deletions
diff --git a/widgets/mpd.lua b/widgets/mpd.lua
new file mode 100644
index 0000000..009ae7d
--- /dev/null
+++ b/widgets/mpd.lua
@@ -0,0 +1,59 @@
+---------------------------------------------------
+-- Licensed under the GNU General Public License v2
+-- * (c) 2010, Adrian C. <anrxc@sysphere.org>
+---------------------------------------------------
+
+-- {{{ Grab environment
+local tonumber = tonumber
+local io = { popen = io.popen }
+local setmetatable = setmetatable
+local string = { gmatch = string.gmatch }
+local helpers = require("vicious.helpers")
+-- }}}
+
+
+-- Mpd: provides Music Player Daemon information
+module("vicious.widgets.mpd")
+
+
+-- {{{ MPD widget type
+local function worker(format, 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 mpd_state
+end
+-- }}}
+
+setmetatable(_M, { __call = function(_, ...) return worker(...) end })