]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome: support widget for missing title and file
[dotfiles.git] / awesome / widgets / mpd_widget.lua
1 local awful = require("awful")
2 local gears = require("gears")
3 local mpc = require("widgets/mpc")
4 local naughty = require("naughty")
5 local textbox = require("wibox.widget.textbox")
6 local timer = require("gears.timer")
7 local wibox = require("wibox")
8
9 local widget = {}
10 widget._textbox = textbox()
11 widget._currentsong = {}
12
13 widget.scroll = wibox.widget {
14 layout = wibox.container.scroll.horizontal,
15 max_size = 200,
16 extra_space = 10,
17 step_function = wibox.container.scroll.step_functions.linear_increase,
18 speed = 20,
19 {
20 widget = widget._textbox,
21 }
22 }
23
24 function widget:update_widget()
25 local text = ""
26 local artist = self._currentsong.artist
27 local title = self._currentsong.title
28 local file = self._currentsong.file
29 if artist and title then
30 text = string.format("%s - %s", artist, title)
31 elseif title or file then
32 text = string.format("%s", title or file )
33 else
34 text = "-"
35 end
36 self._textbox.text = text
37 end
38
39 local function sec_to_min(sec)
40 m, s = math.modf((sec or 0.0) / 60)
41 return string.format('%d:%02d', m, math.floor(s * 60))
42 end
43
44 local function song_duration(elapsed, duration)
45 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
46 end
47
48 local dbg = require("gears.debug")
49 function widget:get_albumart()
50 local art = self._albumart
51 -- dbg.dump(art)
52 -- dbg.dump(self._albumart.binary)
53 if art and art.binary then
54 local path = '/tmp/test.jpg'
55 local f = io.open(path, 'w')
56 f:write(art.binary)
57 f:close()
58 return path
59 else
60 return beautiful.mpd_default_album
61 end
62 end
63
64 function widget:get_info()
65 local status, song = self._status, self._currentsong
66 if not status.state then return nil end
67
68 local info = {}
69 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
70 if not song.artist then
71 info.text = string.format("%s", song.title or song.file)
72 else
73 info.text = string.format("%s - %s", song.artist, song.title)
74 end
75 if song.album then
76 info.text = info.text .. "\n" .. tostring(song.album or "")
77 end
78
79 return info
80 end
81
82 function widget:popup_show()
83 if self._notification then return end
84
85 local table = self:get_info()
86 if not table then return end
87
88 self._timer:start()
89 self._notification = naughty.notify(
90 { title = table.title
91 , icon = self:get_albumart()
92 , icon_size = 64
93 , text = table.text
94 , timeout = 0
95 , destroy = function() self._timer:stop(); self._notification = nil end
96 })
97 end
98
99 function widget:popup_hide(delay)
100 local function destroy()
101 if self._hover then return end
102 if not self._notification then return end
103 naughty.destroy(self._notification)
104 self._notification = nil
105 end
106
107 if not delay then
108 destroy()
109 return
110 end
111
112 if self._hide_timer and self._hide_timer.started then
113 self._hide_timer.timeout = delay
114 self._hide_timer:again()
115 else
116 self._hide_timer = timer(
117 { timeout = delay
118 , autostart = true
119 , single_shot = true
120 , callback = destroy
121 })
122 end
123 end
124
125 function widget:popup_update()
126 if not self._notification then return end
127 self._connection:send("status", function(_, status)
128 self._status = status
129 local table = self:get_info()
130 if not self._notification then return end
131 naughty.replace_text(self._notification, table.title, table.text)
132 end)
133 end
134
135 function widget:popup_oneshot(timeout)
136 if self._notification then
137 self:popup_hide()
138 self:popup_show()
139 else
140 self:popup_show()
141 end
142 self:popup_hide(5)
143 end
144
145 function widget:error_handler(err)
146 self._textbox:set_text("Error: " .. tostring(err))
147 self._status = {}
148 end
149
150 function widget:run()
151 self._status = {}
152
153 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
154 "player", function(conn)
155 conn:send("status", function(err, status)
156 local songchanged = self._status.songid ~= status.songid
157 local statechanged = self._status.state ~= status.state
158 self._status = status
159 if not songchanged then
160 self:update_widget()
161 if statechanged then
162 self:popup_oneshot(5)
163 end
164 return
165 end
166
167 conn:send("currentsong", function(_, song)
168 self._currentsong = song
169 conn:albumart(song.file, function(_, art)
170 self._albumart = art
171 self:update_widget()
172 self:popup_oneshot(5)
173 end)
174 end)
175 end)
176 end
177 )
178
179 self._keep_alive_timer = timer {
180 timeout = 1
181 , autostart = true
182 , callback = function() self._connection:connect() end
183 , call_now = true
184 }
185
186 self._timer = timer({ timeout = 1 })
187 self._timer:connect_signal("timeout", function() self:popup_update() end)
188
189 self._hover = false;
190 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
191 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
192 end
193
194 widget:run()
195
196 widget.scroll:buttons(gears.table.join(
197 awful.button({}, 1, function() widget._connection:toggle_play() end)
198 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
199 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
200 )
201
202 globalkeys = gears.table.join(globalkeys,
203 --TODO headphone support
204 awful.key({ }, "XF86AudioPlay", function() widget._connection:play() end,
205 { description = "play", group = "mpd" }),
206 awful.key({ }, "XF86AudioPause", function() widget._connection:pause() end,
207 { description = "pause", group = "mpd" }),
208 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
209 { description = "toogle play", group = "mpd" }),
210 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
211 { description = "show more info", group = "mpd" }),
212 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
213 { description = "previous track", group = "mpd" }),
214 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
215 { description = "next track", group = "mpd" })
216 )
217 return {
218 layout = wibox.layout.align.horizontal,
219 forced_width = 200,
220 expand = "outside",
221 nil,
222 widget.scroll,
223 nil,
224 }