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 os = { execute = os.execute,
19 local math = { floor = math.floor }
21 local string = { format = string.format,
23 gmatch = string.gmatch }
25 local setmetatable = setmetatable
31 local function worker(args)
32 local args = args or {}
33 local timeout = args.timeout or 2
34 local password = args.password or ""
35 local host = args.host or "127.0.0.1"
36 local port = args.port or "6600"
37 local music_dir = args.music_dir or os.getenv("HOME") .. "/Music"
38 local cover_size = args.cover_size or 100
39 local default_art = args.default_art or ""
40 local notify = args.notify or "on"
41 local followmouse = args.followmouse or false
42 local echo_cmd = args.echo_cmd or "echo"
43 local settings = args.settings or function() end
45 local mpdcover = helpers.scripts_dir .. "mpdcover"
46 local mpdh = "telnet://" .. host .. ":" .. port
47 local echo = echo_cmd .. " 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
49 mpd.widget = wibox.widget.textbox('')
51 mpd_notification_preset = {
52 title = "Now playing",
56 helpers.set_map("current mpd track", nil)
59 async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
80 for line in string.gmatch(f, "[^\n]+") do
81 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
82 if k == "state" then mpd_now.state = v
83 elseif k == "file" then mpd_now.file = v
84 elseif k == "Name" then mpd_now.name = escape_f(v)
85 elseif k == "Artist" then mpd_now.artist = escape_f(v)
86 elseif k == "Title" then mpd_now.title = escape_f(v)
87 elseif k == "Album" then mpd_now.album = escape_f(v)
88 elseif k == "Genre" then mpd_now.genre = escape_f(v)
89 elseif k == "Track" then mpd_now.track = escape_f(v)
90 elseif k == "Date" then mpd_now.date = escape_f(v)
91 elseif k == "Time" then mpd_now.time = v
92 elseif k == "elapsed" then mpd_now.elapsed = string.match(v, "%d+")
93 elseif k == "song" then mpd_now.pls_pos = v
94 elseif k == "playlistlength" then mpd_now.pls_len = v
95 elseif k == "repeat" then mpd_now.repeat_mode = v ~= "0"
96 elseif k == "single" then mpd_now.single_mode = v ~= "0"
97 elseif k == "random" then mpd_now.random_mode = v ~= "0"
98 elseif k == "consume" then mpd_now.consume_mode = v ~= "0"
103 mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
104 mpd_now.album, mpd_now.date, mpd_now.title)
108 if mpd_now.state == "play"
110 if notify == "on" and mpd_now.title ~= helpers.get_map("current mpd track")
112 helpers.set_map("current mpd track", mpd_now.title)
114 if string.match(mpd_now.file, "http.*://") == nil
116 os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
117 mpd_now.file, cover_size, default_art))
118 current_icon = "/tmp/mpdcover.png"
120 current_icon = default_art
124 mpd_notification_preset.screen = mouse.screen
127 mpd.id = naughty.notify({
128 preset = mpd_notification_preset,
130 replaces_id = mpd.id,
133 elseif mpd_now.state ~= "pause"
135 helpers.set_map("current mpd track", nil)
140 helpers.newtimer("mpd", timeout, mpd.update)
142 return setmetatable(mpd, { __index = mpd.widget })
145 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })