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

0b9a4c9e54fdb05ce3114c864a8a53b82bbc4a4a
[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 markup       = require("lain.util.markup")
11 local helpers      = require("lain.helpers")
12
13 local awful        = require("awful")
14 local beautiful    = require("beautiful")
15 local naughty      = require("naughty")
16 local wibox        = require("wibox")
17
18 local io           = io
19 local os           = { execute  = os.execute,
20                        getenv   = os.getenv }
21 local string       = { gmatch   = string.gmatch }
22
23 local setmetatable = setmetatable
24
25 -- MPD infos
26 -- lain.widgets.mpd
27 local mpd = { id = nil }
28
29 function worker(args)
30     local args = args or {}
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 refresh_timeout = args.refresh_timeout or 1
36     local color_artist = args.color_artist or beautiful.fg_normal or "#FFFFFF"
37     local color_song = args.color_song or beautiful.fg_focus or "#FFFFFF"
38     local spr = args.spr or " "
39     local app = args.app or "ncmpcpp"
40     local shadow = args.shadow or false
41
42     local mpdcover = helpers.scripts_dir .. "mpdcover"
43     local mpdh = "telnet://"..host..":"..port
44     local echo = "echo 'password "..password.."\nstatus\ncurrentsong\nclose'"
45
46     local mympd = wibox.widget.textbox()
47
48     helpers.set_map("current mpd track", nil)
49
50     local mympdupdate = function()
51         local function set_nompd()
52             if shadow
53             then
54                 mympd:set_text('')
55             else
56                 mympd:set_markup(markup(color_artist, " mpd "), markup(color_song , "off "))
57             end
58         end
59
60         local mpd_state  = {
61             ["{state}"]  = "N/A",
62             ["{file}"]   = "N/A",
63             ["{Artist}"] = "N/A",
64             ["{Title}"]  = "N/A",
65             ["{Album}"]  = "N/A",
66             ["{Date}"]   = "N/A"
67         }
68
69         -- Get data from MPD server
70         local f = io.popen(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh)
71
72         for line in f:lines() do
73             for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
74                 if     k == "state"  then mpd_state["{"..k.."}"] = v
75                 elseif k == "file"   then mpd_state["{"..k.."}"] = v
76                 elseif k == "Artist" then mpd_state["{"..k.."}"] = awful.util.escape(v)
77                 elseif k == "Title"  then mpd_state["{"..k.."}"] = awful.util.escape(v)
78                 elseif k == "Album"  then mpd_state["{"..k.."}"] = awful.util.escape(v)
79                 elseif k == "Date"   then mpd_state["{"..k.."}"] = awful.util.escape(v)
80                 end
81             end
82         end
83
84         f:close()
85
86         if mpd_state["{state}"] == "play"
87         then
88             if mpd_state["{Title}"] ~= helpers.get_map("current mpd track")
89             then
90                 helpers.set_map("current mpd track", mpd_state["{Title}"])
91                 os.execute(mpdcover .. " '" .. music_dir .. "' '"
92                            .. mpd_state["{file}"] .. "'")
93                 mpd.id = naughty.notify({
94                     title = "Now playing",
95                     text = mpd_state["{Artist}"] .. " ("   ..
96                            mpd_state["{Album}"]  .. ") - " ..
97                            mpd_state["{Date}"]   .. "\n"   ..
98                            mpd_state["{Title}"],
99                     icon = "/tmp/mpdcover.png",
100                     fg = beautiful.fg_focus or "#FFFFFF",
101                     bg = beautiful.bg_normal or "#000000" ,
102                     timeout = 6, 
103                     replaces_id = mpd.id
104                 }).id
105             end
106             mympd:set_markup(markup(color_artist, " " .. mpd_state["{Artist}"])
107                              .. spr ..
108                              markup(color_song, mpd_state["{Title}"] .. " "))
109         elseif mpd_state["{state}"] == "pause"
110         then
111             mympd:set_markup(markup(color_artist, " mpd")
112                              .. spr ..
113                              markup(color_song, "paused "))
114         else
115             helpers.set_map("current mpd track", nil)
116                         set_nompd()
117               end
118     end
119
120     local mympdtimer = timer({ timeout = refresh_timeout })
121     mympdtimer:connect_signal("timeout", mympdupdate)
122     mympdtimer:start()
123     mympdtimer:emit_signal("timeout")
124
125     mympd:buttons(awful.util.table.join(
126         awful.button({}, 0,
127             function()
128                 helpers.run_in_terminal(app)
129             end)
130     ))
131
132     local mpd_out = { widget = mympd, notify = mympdupdate }
133
134     return setmetatable(mpd_out, { __index = mpd_out.widget })
135 end
136
137 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })