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

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