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

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