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

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