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

refresh widget fix
[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 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 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     mpd.widget = wibox.widget.textbox('')
42
43     notification_preset = {
44         title   = "Now playing",
45         timeout = 6
46     }
47
48     helpers.set_map("current mpd track", nil)
49
50     function mpd.update()
51         mpd_now = {
52             state  = "N/A",
53             file   = "N/A",
54             artist = "N/A",
55             title  = "N/A",
56             album  = "N/A",
57             date   = "N/A"
58         }
59
60         local f = io.popen(echo .. " | curl --connect-timeout 1 -fsm 1 " .. mpdh)
61
62         for line in f:lines() do
63             for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
64                 if     k == "state"  then mpd_now.state  = v
65                 elseif k == "file"   then mpd_now.file   = v
66                 elseif k == "Artist" then mpd_now.artist = util.escape(v)
67                 elseif k == "Title"  then mpd_now.title  = util.escape(v)
68                 elseif k == "Album"  then mpd_now.album  = util.escape(v)
69                 elseif k == "Date"   then mpd_now.date   = util.escape(v)
70                 end
71             end
72         end
73
74         f:close()
75
76         notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
77                                    mpd_now.album, mpd_now.date, mpd_now.title)
78         widget = mpd.widget
79         settings()
80
81         if mpd_now.state == "play"
82         then
83             if mpd_now.title ~= helpers.get_map("current mpd track")
84             then
85                 helpers.set_map("current mpd track", mpd_now.title)
86
87                 os.execute(string.format("%s %q %q", mpdcover, music_dir, mpd_now.file))
88
89                 mpd.id = naughty.notify({
90                     preset = notification_preset,
91                     icon = "/tmp/mpdcover.png",
92                     replaces_id = mpd.id
93                 }).id
94             end
95         elseif mpd_now.state ~= "pause"
96         then
97             helpers.set_map("current mpd track", nil)
98               end
99     end
100
101     helpers.newtimer("mpd", timeout, mpd.update)
102
103     return setmetatable(mpd, { __index = mpd.widget })
104 end
105
106 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })