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.
This commit is contained in:
Adrian C. (anrxc) 2010-03-12 21:56:17 +01:00
parent dda51b1e34
commit 8482b5407c
2 changed files with 45 additions and 31 deletions

20
README
View File

@ -131,7 +131,6 @@ great for saving power.
Security
--------
At the moment only one widget type (Gmail) requires auth. information
in order to get to the data. In the future there could be more, and
you should give some thought to the issue of protecting your data. The
@ -299,11 +298,13 @@ vicious.widgets.pkg
- returns 1st value as the count of available updates
vicious.widgets.mpd
- provides the currently playing song in MPD
- takes an (optional) argument, if it's a number song name will be
truncated, if a table, with 1st field as maximum lenght and 2nd
the widget name (i.e. "mpdwidget"), scrolling will be used
- returns 1st value as the currently playing song
- provides Music Player Daemon information
- takes a table as an argument, 1st field should be the password (or
nil), 2nd the hostname (or nil) and 3rd port (or nil) - if no
argument is provided connection attempt will be made to localhost
port 6600 with no password
- returns a table with string keys: {volume}, {state}, {Artist},
{Title}, {Album}, {Genre}
vicious.widgets.volume
- provides volume levels and state of requested ALSA mixers
@ -413,11 +414,12 @@ second argument, and will return the text/data to be used for the
widget.
Example
mpdwidget = widget({ type = 'textbox' })
mpdwidget = widget({ type = "textbox" })
vicious.register(mpdwidget, vicious.widgets.mpd,
function (widget, args)
if args[1] == 'Stopped' then return ''
else return '<span color="white">MPD:</span> '..args[1]
if args["{state}"] == "Stop" then return ""
else return '<span color="white">MPD:</span> '..
args["{Artist}"]..' - '.. args["{Title}"]
end
end)

60
mpd.lua
View File

@ -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")
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()
-- 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)
end
end
return {helpers.escape(np)}
return mpd_state
end
-- }}}