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

layout: fixed useless_gap retrieval
[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 async        = require("lain.asyncshell")
11 local helpers      = require("lain.helpers")
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           = { 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 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 echo_cmd      = args.echo_cmd or "echo"
42     local settings      = args.settings or function() end
43
44     local mpdh = string.format("telnet://%s:%s", host, port)
45     local echo = string.format("%s 'password %s\nstatus\ncurrentsong\nclose'", echo_cmd, password)
46
47     mpd_notification_preset = {
48         title   = "Now playing",
49         timeout = 6
50     }
51
52     helpers.set_map("current mpd track", nil)
53
54     function mpd.update()
55         async.request(string.format("%s | curl --connect-timeout 1 -fsm 3 %s", echo, mpdh), function (f)
56             mpd_now = {
57                 random_mode  = false,
58                 single_mode  = false,
59                 repeat_mode  = false,
60                 consume_mode = false,
61                 pls_pos      = "N/A",
62                 pls_len      = "N/A",
63                 state        = "N/A",
64                 file         = "N/A",
65                 name         = "N/A",
66                 artist       = "N/A",
67                 title        = "N/A",
68                 album        = "N/A",
69                 genre        = "N/A",
70                 track        = "N/A",
71                 date         = "N/A",
72                 time         = "N/A",
73                 elapsed      = "N/A"
74             }
75
76             for line in string.gmatch(f, "[^\n]+") do
77                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
78                     if     k == "state"          then mpd_now.state        = v
79                     elseif k == "file"           then mpd_now.file         = v
80                     elseif k == "Name"           then mpd_now.name         = escape_f(v)
81                     elseif k == "Artist"         then mpd_now.artist       = escape_f(v)
82                     elseif k == "Title"          then mpd_now.title        = escape_f(v)
83                     elseif k == "Album"          then mpd_now.album        = escape_f(v)
84                     elseif k == "Genre"          then mpd_now.genre        = escape_f(v)
85                     elseif k == "Track"          then mpd_now.track        = escape_f(v)
86                     elseif k == "Date"           then mpd_now.date         = escape_f(v)
87                     elseif k == "Time"           then mpd_now.time         = v
88                     elseif k == "elapsed"        then mpd_now.elapsed      = string.match(v, "%d+")
89                     elseif k == "song"           then mpd_now.pls_pos      = v
90                     elseif k == "playlistlength" then mpd_now.pls_len      = v
91                     elseif k == "repeat"         then mpd_now.repeat_mode  = v ~= "0"
92                     elseif k == "single"         then mpd_now.single_mode  = v ~= "0"
93                     elseif k == "random"         then mpd_now.random_mode  = v ~= "0"
94                     elseif k == "consume"        then mpd_now.consume_mode = v ~= "0"
95                     end
96                 end
97             end
98
99             mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
100                                            mpd_now.album, mpd_now.date, mpd_now.title)
101             widget = mpd.widget
102             settings()
103
104             if mpd_now.state == "play" then
105                 if notify == "on" and mpd_now.title ~= helpers.get_map("current mpd track") then
106                     helpers.set_map("current mpd track", mpd_now.title)
107
108                     local current icon = default_art
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'", path, cover_pattern)
113                         current_icon = helpers.read_pipe(cover):gsub("\n", "")
114                     end
115
116                     if followtag then mpd_notification_preset.screen = focused() end
117
118                     mpd.id = naughty.notify({
119                         preset      = mpd_notification_preset,
120                         icon        = current_icon,
121                         icon_size   = cover_size,
122                         replaces_id = mpd.id,
123                     }).id
124                 end
125             elseif mpd_now.state ~= "pause" then
126                 helpers.set_map("current mpd track", nil)
127             end
128         end)
129     end
130
131     helpers.newtimer("mpd", timeout, mpd.update)
132
133     return mpd
134 end
135
136 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })