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

#198 fix attempt 3: patch on DST check
[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 -- MOC audio player
25 -- lain.widgets.contrib.moc
26 local moc = {}
27
28 local function worker(args)
29     local args        = args or {}
30     local timeout     = args.timeout or 2
31     local music_dir   = args.music_dir or os.getenv("HOME") .. "/Music"
32     local cover_size  = args.cover_size or 100
33     local default_art = args.default_art or ""
34     local followmouse = args.followmouse or false
35     local settings    = args.settings or function() end
36
37     local mpdcover = helpers.scripts_dir .. "mpdcover"
38
39     moc.widget = wibox.widget.textbox('')
40
41     moc_notification_preset = {
42         title   = "Now playing",
43         timeout = 6
44     }
45
46     helpers.set_map("current moc track", nil)
47
48     function moc.update()
49         -- mocp -i will produce output like:
50         -- Artist: Travis
51         -- Album: The Man Who
52         -- etc.
53         async.request("mocp -i", function(f)
54             moc_now = {
55                 state   = "N/A",
56                 file    = "N/A",
57                 artist  = "N/A",
58                 title   = "N/A",
59                 album   = "N/A",
60                 elapsed = "N/A",
61                 total   = "N/A"
62             }
63
64             for line in string.gmatch(f, "[^\n]+") do
65                 for k, v in string.gmatch(line, "([%w]+):[%s](.*)$") do
66                     if k == "State" then moc_now.state = v
67                     elseif k == "File" then moc_now.file = v
68                     elseif k == "Artist" then moc_now.artist = escape_f(v)
69                     elseif k == "SongTitle" then moc_now.title = escape_f(v)
70                     elseif k == "Album" then moc_now.album = escape_f(v)
71                     elseif k == "CurrentTime" then moc_now.elapsed = escape_f(v)
72                     elseif k == "TotalTime" then moc_now.total = escape_f(v)
73                     end
74                 end
75             end
76
77             moc_notification_preset.text = string.format("%s (%s) - %s\n%s", moc_now.artist,
78                                            moc_now.album, moc_now.total, moc_now.title)
79             widget = moc.widget
80             settings()
81
82             if moc_now.state == "PLAY" then
83                 if moc_now.title ~= helpers.get_map("current moc track") then
84                     helpers.set_map("current moc track", moc_now.title)
85                     os.execute(string.format("%s %q %q %d %q", mpdcover, "",
86                                moc_now.file, cover_size, default_art))
87
88                     if followmouse then
89                         moc_notification_preset.screen = mouse.screen
90                     end
91
92                     moc.id = naughty.notify({
93                         preset = moc_notification_preset,
94                         icon = "/tmp/mpdcover.png",
95                         replaces_id = moc.id,
96                     }).id
97                 end
98             elseif  moc_now.state ~= "PAUSE" then
99                 helpers.set_map("current moc track", nil)
100             end
101         end)
102     end
103
104     helpers.newtimer("moc", timeout, moc.update)
105
106     return setmetatable(moc, { __index = moc.widget })
107 end
108
109 return setmetatable(moc, { __call = function(_, ...) return worker(...) end })