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

Stopped assuming all clients use theme.border_width
[etc/awesome.git] / widgets / contrib / moc.lua
1
2 --[[
3                                                                   
4      Licensed under GNU General Public License v2                 
5       * (c) 2014, anticlockwise <http://github.com/anticlockwise> 
6                                                                   
7 --]]
8
9 local helpers = require("lain.helpers")
10 local async   = require("lain.asyncshell")
11
12 local escape_f = require("awful.util").escape
13 local naughty  = require("naughty")
14 local wibox    = require("wibox")
15
16 local io     = { popen   = io.popen }
17 local os     = { execute = os.execute,
18                  getenv  = os.getenv }
19 local string = { format  = string.format,
20                  gmatch  = string.gmatch }
21
22 local setmetatable = setmetatable
23
24 local moc = {}
25
26 local function worker(args)
27     local args        = args or {}
28     local timeout     = args.timeout or 2
29     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
30     local cover_size  = args.cover_size or 100
31     local default_art = args.default_art or ""
32     local followmouse = args.followmouse or false
33     local settings    = args.settings or function() end
34
35     local mpdcover = helpers.scripts_dir .. "mpdcover"
36
37     moc.widget = wibox.widget.textbox('')
38
39     moc_notification_preset = {
40         title   = "Now playing",
41         timeout = 6
42     }
43
44     helpers.set_map("current moc track", nil)
45
46     function moc.update()
47         -- mocp -i will produce output like:
48         -- Artist: Travis
49         -- Album: The Man Who
50         -- etc.
51         async.request("mocp -i", function(f)
52             moc_now = {
53                 state   = "N/A",
54                 file    = "N/A",
55                 artist  = "N/A",
56                 title   = "N/A",
57                 album   = "N/A",
58                 elapsed = "N/A",
59                 total   = "N/A"
60             }
61
62             for line in string.gmatch(f, "[^\n]+") do
63                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
64                     if k == "State" then moc_now.state = v
65                     elseif k == "File" then moc_now.file = v
66                     elseif k == "Artist" then moc_now.artist = escape_f(v)
67                     elseif k == "SongTitle" then moc_now.title = escape_f(v)
68                     elseif k == "Album" then moc_now.album = escape_f(v)
69                     elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
70                     elseif k == "TotalTime" then moc_now.total = escape_f(v)
71                     end
72                 end
73             end
74
75             moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
76                                            moc_now.album, moc_now.total, moc_now.title)
77             widget = moc.widget
78             settings()
79
80             if moc_now.state == "PLAY" then
81                 if moc_now.title ~= helpers.get_map("current moc track") then
82                     helpers.set_map("current moc track", moc_now.title)
83                     os.execute(string.format("%s %q %q %d %q", mpdcover, "",
84                                moc_now.file, cover_size, default_art))
85
86                     if followmouse then
87                         moc_notification_preset.screen = mouse.screen
88                     end
89
90                     moc.id = naughty.notify({
91                         preset = moc_notification_preset,
92                         icon = "/tmp/mpdcover.png",
93                         replaces_id = moc.id,
94                     }).id
95                 end
96             elseif  moc_now.state ~= "PAUSE" then
97                 helpers.set_map("current moc track", nil)
98             end
99         end)
100     end
101
102     helpers.newtimer("moc", timeout, moc.update)
103
104     return setmetatable(moc, { __index = moc.widget })
105 end
106
107 return setmetatable(moc, { __call = function(_, ...) return worker(...) end })