]> git.madduck.net Git - etc/awesome.git/blob - widget/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:

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