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

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