]> git.madduck.net Git - etc/awesome.git/blob - widgets/mpd.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge pull request #96 from worron/master
[etc/awesome.git] / widgets / mpd.lua
1
2 --[[
3                                                   
4      Licensed under GNU General Public License v2 
5       * (c) 2013, Luke Bonham                     
6       * (c) 2010, Adrian C. <anrxc@sysphere.org>  
7                                                   
8 --]]
9
10 local helpers      = require("lain.helpers")
11 local async        = require("lain.asyncshell")
12
13 local escape_f     = require("awful.util").escape
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local os           = { execute  = os.execute,
18                        getenv   = os.getenv }
19 local math         = { floor    = math.floor }
20 local string       = { format   = string.format,
21                        match    = string.match,
22                        gmatch   = string.gmatch }
23
24 local setmetatable = setmetatable
25
26 -- MPD infos
27 -- lain.widgets.mpd
28 local mpd = {}
29
30 local function worker(args)
31     local args        = args or {}
32     local timeout     = args.timeout or 2
33     local password    = args.password or ""
34     local host        = args.host or "127.0.0.1"
35     local port        = args.port or "6600"
36     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
37     local cover_size  = args.cover_size or 100
38     local default_art = args.default_art or ""
39     local settings    = args.settings or function() end
40
41     local mpdcover = helpers.scripts_dir .. "mpdcover"
42     local mpdh = "telnet://" .. host .. ":" .. port
43     local echo = "echo 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
44
45     mpd.widget = wibox.widget.textbox('')
46
47     mpd_notification_preset = {
48         title   = "Now playing",
49         timeout = 6
50     }
51
52     helpers.set_map("current mpd track", nil)
53
54     function mpd.update()
55         async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
56             mpd_now = {
57                 state   = "N/A",
58                 file    = "N/A",
59                 artist  = "N/A",
60                 title   = "N/A",
61                 album   = "N/A",
62                 date    = "N/A",
63                 time    = "N/A",
64                 elapsed = "N/A"
65             }
66
67             for line in f:lines() do
68                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
69                     if     k == "state"   then mpd_now.state   = v
70                     elseif k == "file"    then mpd_now.file    = v
71                     elseif k == "Artist"  then mpd_now.artist  = escape_f(v)
72                     elseif k == "Title"   then mpd_now.title   = escape_f(v)
73                     elseif k == "Album"   then mpd_now.album   = escape_f(v)
74                     elseif k == "Date"    then mpd_now.date    = escape_f(v)
75                     elseif k == "Time"    then mpd_now.time    = v
76                     elseif k == "elapsed" then mpd_now.elapsed = string.match(v, "%d+")
77                     end
78                 end
79             end
80
81             mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
82                                            mpd_now.album, mpd_now.date, mpd_now.title)
83             widget = mpd.widget
84             settings()
85
86             if mpd_now.state == "play"
87             then
88                 if mpd_now.title ~= helpers.get_map("current mpd track")
89                 then
90                     helpers.set_map("current mpd track", mpd_now.title)
91
92                     os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
93                                mpd_now.file, cover_size, default_art))
94
95                     mpd.id = naughty.notify({
96                         preset = mpd_notification_preset,
97                         icon = "/tmp/mpdcover.png",
98                         replaces_id = mpd.id,
99                     }).id
100                 end
101             elseif mpd_now.state ~= "pause"
102             then
103                 helpers.set_map("current mpd track", nil)
104             end
105         end)
106     end
107
108     helpers.newtimer("mpd", timeout, mpd.update)
109
110     return setmetatable(mpd, { __index = mpd.widget })
111 end
112
113 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })