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

port to awesome 4.0 completed; #248
[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 focused      = require("awful.screen").focused
15 local naughty      = require("naughty")
16 local wibox        = require("wibox")
17
18 local os           = { execute = os.execute,
19                        getenv  = os.getenv }
20 local math         = { floor   = math.floor }
21 local string       = { format  = string.format,
22                        match   = string.match,
23                        gmatch  = string.gmatch }
24
25 local setmetatable = setmetatable
26
27 -- MPD infos
28 -- lain.widgets.mpd
29 local mpd = helpers.make_widget_textbox()
30
31 local function worker(args)
32     local args        = args or {}
33     local timeout     = args.timeout or 2
34     local password    = args.password or ""
35     local host        = args.host or "127.0.0.1"
36     local port        = args.port or "6600"
37     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
38     local cover_size  = args.cover_size or 100
39     local default_art = args.default_art or ""
40     local notify      = args.notify or "on"
41     local followtag   = args.followtag or false
42     local echo_cmd    = args.echo_cmd or "echo"
43     local settings    = args.settings or function() end
44
45     local mpdcover = helpers.scripts_dir .. "mpdcover"
46     local mpdh = "telnet://" .. host .. ":" .. port
47     local echo = echo_cmd .. " 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
48
49     mpd_notification_preset = {
50         title   = "Now playing",
51         timeout = 6
52     }
53
54     helpers.set_map("current mpd track", nil)
55
56     function mpd.update()
57         async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
58             mpd_now = {
59                 random_mode  = false,
60                 single_mode  = false,
61                 repeat_mode  = false,
62                 consume_mode = false,
63                 pls_pos      = "N/A",
64                 pls_len      = "N/A",
65                 state        = "N/A",
66                 file         = "N/A",
67                 name         = "N/A",
68                 artist       = "N/A",
69                 title        = "N/A",
70                 album        = "N/A",
71                 genre        = "N/A",
72                 track        = "N/A",
73                 date         = "N/A",
74                 time         = "N/A",
75                 elapsed      = "N/A"
76             }
77
78             for line in string.gmatch(f, "[^\n]+") do
79                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
80                     if     k == "state"          then mpd_now.state        = v
81                     elseif k == "file"           then mpd_now.file         = v
82                     elseif k == "Name"           then mpd_now.name         = escape_f(v)
83                     elseif k == "Artist"         then mpd_now.artist       = escape_f(v)
84                     elseif k == "Title"          then mpd_now.title        = escape_f(v)
85                     elseif k == "Album"          then mpd_now.album        = escape_f(v)
86                     elseif k == "Genre"          then mpd_now.genre        = escape_f(v)
87                     elseif k == "Track"          then mpd_now.track        = escape_f(v)
88                     elseif k == "Date"           then mpd_now.date         = escape_f(v)
89                     elseif k == "Time"           then mpd_now.time         = v
90                     elseif k == "elapsed"        then mpd_now.elapsed      = string.match(v, "%d+")
91                     elseif k == "song"           then mpd_now.pls_pos      = v
92                     elseif k == "playlistlength" then mpd_now.pls_len      = v
93                     elseif k == "repeat"         then mpd_now.repeat_mode  = v ~= "0"
94                     elseif k == "single"         then mpd_now.single_mode  = v ~= "0"
95                     elseif k == "random"         then mpd_now.random_mode  = v ~= "0"
96                     elseif k == "consume"        then mpd_now.consume_mode = v ~= "0"
97                     end
98                 end
99             end
100
101             mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
102                                            mpd_now.album, mpd_now.date, mpd_now.title)
103             widget = mpd.widget
104             settings()
105
106             if mpd_now.state == "play"
107             then
108                 if notify == "on" and mpd_now.title ~= helpers.get_map("current mpd track")
109                 then
110                     helpers.set_map("current mpd track", mpd_now.title)
111
112                     if string.match(mpd_now.file, "http.*://") == nil
113                     then -- local file
114                         os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
115                                    mpd_now.file, cover_size, default_art))
116                         current_icon = "/tmp/mpdcover.png"
117                     else -- http stream
118                         current_icon = default_art
119                     end
120
121                     if followtag then
122                         mpd_notification_preset.screen = focused()
123                     end
124
125                     mpd.id = naughty.notify({
126                         preset = mpd_notification_preset,
127                         icon = os.execute(string.format("ls %s &> /dev/null", current_icon))
128                                and current_icon,
129                         replaces_id = mpd.id,
130                     }).id
131                 end
132             elseif mpd_now.state ~= "pause"
133             then
134                 helpers.set_map("current mpd track", nil)
135             end
136         end)
137     end
138
139     helpers.newtimer("mpd", timeout, mpd.update)
140
141     return mpd
142 end
143
144 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })