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