All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
4 Licensed under GNU General Public License v2
5 * (c) 2013, Luke Bonham
6 * (c) 2010, Adrian C. <anrxc@sysphere.org>
10 local helpers = require("lain.helpers")
11 local async = require("lain.asyncshell")
13 local escape_f = require("awful.util").escape
14 local naughty = require("naughty")
15 local wibox = require("wibox")
17 local io = { popen = io.popen }
18 local os = { execute = os.execute,
20 local string = { format = string.format,
21 gmatch = string.gmatch }
23 local setmetatable = setmetatable
29 local function worker(args)
30 local args = args or {}
31 local timeout = args.timeout or 2
32 local password = args.password or ""
33 local host = args.host or "127.0.0.1"
34 local port = args.port or "6600"
35 local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
36 local cover_size = args.cover_size or 100
37 local default_art = args.default_art or ""
38 local settings = args.settings or function() end
40 local mpdcover = helpers.scripts_dir .. "mpdcover"
41 local mpdh = "telnet://" .. host .. ":" .. port
42 local echo = "echo 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
44 mpd.widget = wibox.widget.textbox('')
46 mpd_notification_preset = {
47 title = "Now playing",
51 helpers.set_map("current mpd track", nil)
54 async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
64 for line in f:lines() do
65 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
66 if k == "state" then mpd_now.state = v
67 elseif k == "file" then mpd_now.file = v
68 elseif k == "Artist" then mpd_now.artist = escape_f(v)
69 elseif k == "Title" then mpd_now.title = escape_f(v)
70 elseif k == "Album" then mpd_now.album = escape_f(v)
71 elseif k == "Date" then mpd_now.date = escape_f(v)
76 mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
77 mpd_now.album, mpd_now.date, mpd_now.title)
81 if mpd_now.state == "play"
83 if mpd_now.title ~= helpers.get_map("current mpd track")
85 helpers.set_map("current mpd track", mpd_now.title)
87 os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
88 mpd_now.file, cover_size, default_art))
90 mpd.id = naughty.notify({
91 preset = mpd_notification_preset,
92 icon = "/tmp/mpdcover.png",
94 screen = client.focus and client.focus.screen or 1
97 elseif mpd_now.state ~= "pause"
99 helpers.set_map("current mpd track", nil)
104 helpers.newtimer("mpd", timeout, mpd.update)
106 return setmetatable(mpd, { __index = mpd.widget })
109 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })