]> git.madduck.net Git - etc/awesome.git/blob - widgets/mpd.lua.orig

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:

merging with upstream
[etc/awesome.git] / widgets / mpd.lua.orig
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 async        = require("lain.asyncshell")
12
13 local escape_f     = require("awful.util").escape
14 local naughty      = require("naughty")
15 local wibox        = require("wibox")
16
17 local os           = { execute  = os.execute,
18                        getenv   = os.getenv }
19 local math         = { floor    = math.floor }
20 <<<<<<< HEAD
21 =======
22 local mouse        = mouse
23 >>>>>>> upstream/master
24 local string       = { format   = string.format,
25                        match    = string.match,
26                        gmatch   = string.gmatch }
27
28 local setmetatable = setmetatable
29
30 -- MPD infos
31 -- lain.widgets.mpd
32 local mpd = {}
33
34 local function worker(args)
35     local args        = args or {}
36     local timeout     = args.timeout or 2
37     local password    = args.password or ""
38     local host        = args.host or "127.0.0.1"
39     local port        = args.port or "6600"
40     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
41     local cover_size  = args.cover_size or 100
42     local default_art = args.default_art or ""
43 <<<<<<< HEAD
44 =======
45     local followmouse = args.followmouse or false
46     local echo_cmd    = args.echo_cmd or "echo"
47 >>>>>>> upstream/master
48     local settings    = args.settings or function() end
49
50     local mpdcover = helpers.scripts_dir .. "mpdcover"
51     local mpdh = "telnet://" .. host .. ":" .. port
52 <<<<<<< HEAD
53     local echo = "echo 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
54 =======
55     local echo = echo_cmd .. " 'password " .. password .. "\nstatus\ncurrentsong\nclose'"
56 >>>>>>> upstream/master
57
58     mpd.widget = wibox.widget.textbox('')
59
60     mpd_notification_preset = {
61         title   = "Now playing",
62         timeout = 6
63     }
64
65     helpers.set_map("current mpd track", nil)
66
67     function mpd.update()
68         async.request(echo .. " | curl --connect-timeout 1 -fsm 3 " .. mpdh, function (f)
69             mpd_now = {
70                 state   = "N/A",
71                 file    = "N/A",
72                 artist  = "N/A",
73                 title   = "N/A",
74                 album   = "N/A",
75                 date    = "N/A",
76                 time    = "N/A",
77                 elapsed = "N/A"
78             }
79
80 <<<<<<< HEAD
81             for line in f:lines() do
82 =======
83             for line in string.gmatch(f, "[^\n]+") do
84 >>>>>>> upstream/master
85                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
86                     if     k == "state"   then mpd_now.state   = v
87                     elseif k == "file"    then mpd_now.file    = v
88                     elseif k == "Artist"  then mpd_now.artist  = escape_f(v)
89                     elseif k == "Title"   then mpd_now.title   = escape_f(v)
90                     elseif k == "Album"   then mpd_now.album   = escape_f(v)
91                     elseif k == "Date"    then mpd_now.date    = escape_f(v)
92                     elseif k == "Time"    then mpd_now.time    = v
93                     elseif k == "elapsed" then mpd_now.elapsed = string.match(v, "%d+")
94                     end
95                 end
96             end
97
98             mpd_notification_preset.text = string.format("%s (%s) - %s\n%s", mpd_now.artist,
99                                            mpd_now.album, mpd_now.date, mpd_now.title)
100             widget = mpd.widget
101             settings()
102
103             if mpd_now.state == "play"
104             then
105                 if mpd_now.title ~= helpers.get_map("current mpd track")
106                 then
107                     helpers.set_map("current mpd track", mpd_now.title)
108
109 <<<<<<< HEAD
110                     if string.match(mpd_now.file, "http://") == nil
111 =======
112                     if string.match(mpd_now.file, "http.*://") == nil
113 >>>>>>> upstream/master
114                     then -- local file
115                         os.execute(string.format("%s %q %q %d %q", mpdcover, music_dir,
116                                    mpd_now.file, cover_size, default_art))
117                         current_icon = "/tmp/mpdcover.png"
118                     else -- http stream
119                         current_icon = default_art
120                     end
121
122 <<<<<<< HEAD
123 =======
124                     if followmouse then
125                         mpd_notification_preset.screen = mouse.screen
126                     end
127
128 >>>>>>> upstream/master
129                     mpd.id = naughty.notify({
130                         preset = mpd_notification_preset,
131                         icon = current_icon,
132                         replaces_id = mpd.id,
133                     }).id
134                 end
135             elseif mpd_now.state ~= "pause"
136             then
137                 helpers.set_map("current mpd track", nil)
138             end
139         end)
140     end
141
142     helpers.newtimer("mpd", timeout, mpd.update)
143
144     return setmetatable(mpd, { __index = mpd.widget })
145 end
146
147 return setmetatable(mpd, { __call = function(_, ...) return worker(...) end })