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

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