]> 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:

#126 extended to every supported widget
[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 mouse        = mouse
21 local string       = { format   = string.format,
22                        match    = string.match,
23                        gmatch   = string.gmatch }
24
25 local setmetatable = setmetatable
26
27 -- MPD infos
28 -- lain.widgets.mpd
29 local mpd = {}
30
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 followmouse = args.followmouse or false
41     local settings    = args.settings or function() end
42
43     local mpdcover = helpers.scripts_dir .. "mpdcover"
44     local mpdh = "telnet://" .. host .. ":" .. port
45     local echo = "/bin/echo -e 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
46
47     mpd.widget = wibox.widget.textbox('')
48
49     mpd_notification_preset = {
50         title   = "Now playing",
51         timeout = 6
52     }
53
54     helpers.set_map("current mpd track", nil)
55
56     function mpd.update()
57         async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
58             mpd_now = {
59                 state   = "N/A",
60                 file    = "N/A",
61                 artist  = "N/A",
62                 title   = "N/A",
63                 album   = "N/A",
64                 date    = "N/A",
65                 time    = "N/A",
66                 elapsed = "N/A"
67             }
68
69             for line in f:lines() do
70                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
71                     if     k == "state"   then mpd_now.state   = v
72                     elseif k == "file"    then mpd_now.file    = v
73                     elseif k == "Artist"  then mpd_now.artist  = escape_f(v)
74                     elseif k == "Title"   then mpd_now.title   = escape_f(v)
75                     elseif k == "Album"   then mpd_now.album   = escape_f(v)
76                     elseif k == "Date"    then mpd_now.date    = escape_f(v)
77                     elseif k == "Time"    then mpd_now.time    = v
78                     elseif k == "elapsed" then mpd_now.elapsed = string.match(v, "%d+")
79                     end
80                 end
81             end
82
83             mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
84                                            mpd_now.album, mpd_now.date, mpd_now.title)
85             widget = mpd.widget
86             settings()
87
88             if mpd_now.state == "play"
89             then
90                 if mpd_now.title ~= helpers.get_map("current mpd track")
91                 then
92                     helpers.set_map("current mpd track", mpd_now.title)
93
94                     if string.match(mpd_now.file, "http.*://") == nil
95                     then -- local file
96                         os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
97                                    mpd_now.file, cover_size, default_art))
98                         current_icon = "/tmp/mpdcover.png"
99                     else -- http stream
100                         current_icon = default_art
101                     end
102
103                     if followmouse then
104                         mpd_notification_preset.screen = mouse.screen
105                     end
106
107                     mpd.id = naughty.notify({
108                         preset = mpd_notification_preset,
109                         icon = current_icon,
110                         replaces_id = mpd.id,
111                     }).id
112                 end
113             elseif mpd_now.state ~= "pause"
114             then
115                 helpers.set_map("current mpd track", nil)
116             end
117         end)
118     end
119
120     helpers.newtimer("mpd", timeout, mpd.update)
121
122     return setmetatable(mpd, { __index = mpd.widget })
123 end
124
125 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })