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

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