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

36c3ed767b7239d88c96fab0b4fa2b5ddeb49a81
[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
12 local escape_f     = require("awful.util").escape
13 local naughty      = require("naughty")
14 local wibox        = require("wibox")
15
16 local io           = { popen    = io.popen }
17 local os           = { execute  = os.execute,
18                        getenv   = os.getenv }
19 local string       = { format   = string.format,
20                        gmatch   = string.gmatch }
21
22 local setmetatable = setmetatable
23
24 -- MPD infos
25 -- lain.widgets.mpd
26 local mpd = {}
27
28 local function worker(args)
29     local args        = args or {}
30     local timeout     = args.timeout or 2
31     local password    = args.password or ""
32     local host        = args.host or "127.0.0.1"
33     local port        = args.port or "6600"
34     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
35     local cover_size  = args.cover_size or 100
36     local default_art = args.default_art or ""
37     local settings    = args.settings or function() end
38
39     local mpdcover = helpers.scripts_dir .. "mpdcover"
40     local mpdh = "telnet://" .. host .. ":" .. port
41
42     local echo = nil
43
44     if password == "" then
45         echo = "(echo -e 'status'; sleep 0.1;" ..
46                "echo -e 'currentsong'; sleep 0.1;" ..
47                "echo -e 'close')"
48     else
49         echo = "(echo -e 'password " .. password .. "'" ..
50                "echo -e 'status'; sleep 0.1;" ..
51                "echo -e 'currentsong'; sleep 0.1;" ..
52                "echo -e 'close')"
53     end
54
55     mpd.widget = wibox.widget.textbox('')
56
57     mpd_notification_preset = {
58         title   = "Now playing",
59         timeout = 6
60     }
61
62     helpers.set_map("current mpd track", nil)
63
64     function mpd.update()
65         mpd_now = {
66             state  = "N/A",
67             file   = "N/A",
68             artist = "N/A",
69             title  = "N/A",
70             album  = "N/A",
71             date   = "N/A"
72         }
73
74         local f = io.popen(echo .. " | curl --connect-timeout 1 -fsm 1 " .. mpdh)
75
76         for line in f:lines() do
77             for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
78                 if     k == "state"  then mpd_now.state  = v
79                 elseif k == "file"   then mpd_now.file   = v
80                 elseif k == "Artist" then mpd_now.artist = escape_f(v)
81                 elseif k == "Title"  then mpd_now.title  = escape_f(v)
82                 elseif k == "Album"  then mpd_now.album  = escape_f(v)
83                 elseif k == "Date"   then mpd_now.date   = escape_f(v)
84                 end
85             end
86         end
87
88         f:close()
89
90         mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
91                                        mpd_now.album, mpd_now.date, mpd_now.title)
92         widget = mpd.widget
93         settings()
94
95         if mpd_now.state == "play"
96         then
97             if mpd_now.title ~= helpers.get_map("current mpd track")
98             then
99                 helpers.set_map("current mpd track", mpd_now.title)
100
101                 os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
102                            mpd_now.file, cover_size, default_art))
103
104                 mpd.id = naughty.notify({
105                     preset = mpd_notification_preset,
106                     icon = "/tmp/mpdcover.png",
107                     replaces_id = mpd.id
108                 }).id
109             end
110         elseif mpd_now.state ~= "pause"
111         then
112             helpers.set_map("current mpd track", nil)
113               end
114     end
115
116     helpers.newtimer("mpd", timeout, mpd.update)
117
118     return setmetatable(mpd, { __index = mpd.widget })
119 end
120
121 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })