]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
zsh/motd: print both --system and --user failed units
[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 local 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 if not status.song then return nil end
68
69 local info = {}
70 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
71 if not song.artist then
72 info.text = string.format("%s", song.title or song.file)
73 else
74 info.text = string.format("%s - %s", song.artist, song.title)
75 end
76 if song.album then
77 info.text = info.text .. "\n" .. tostring(song.album or "")
78 end
79
80 return info
81 end
82
83 function widget:popup_show()
84 if self._notification then return end
85
86 local table = self:get_info()
87 if not table then return end
88
89 self._timer:start()
90 self._notification = naughty.notify(
91 { title = table.title
92 , icon = self:get_albumart()
93 , icon_size = 64
94 , text = table.text
95 , timeout = 0
96 , destroy = function() self._timer:stop(); self._notification = nil end
97 })
98 end
99
100 function widget:popup_hide(delay)
101 local function destroy()
102 if self._hover then return end
103 if not self._notification then return end
104 naughty.destroy(self._notification)
105 self._notification = nil
106 end
107
108 if not delay then
109 destroy()
110 return
111 end
112
113 if self._hide_timer and self._hide_timer.started then
114 self._hide_timer.timeout = delay
115 self._hide_timer:again()
116 else
117 self._hide_timer = timer(
118 { timeout = delay
119 , autostart = true
120 , single_shot = true
121 , callback = destroy
122 })
123 end
124 end
125
126 function widget:popup_update()
127 if not self._notification then return end
128 self._connection:send("status", function(_, status)
129 self._status = status
130 local table = self:get_info()
131 if not self._notification then return end
132 if not table then return end
133 naughty.replace_text(self._notification, table.title, table.text)
134 end)
135 end
136
137 function widget:popup_oneshot(timeout)
138 if self._notification then
139 self:popup_hide()
140 self:popup_show()
141 else
142 self:popup_show()
143 end
144 self:popup_hide(5)
145 end
146
147 function widget:error_handler(err)
148 self._textbox:set_text("Error: " .. tostring(err))
149 self._status = {}
150 end
151
152 function widget:run()
153 self._status = {}
154
155 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
156 "player", function(conn)
157 conn:send("status", function(err, status)
158 local songchanged = self._status.songid ~= status.songid
159 local statechanged = self._status.state ~= status.state
160 self._status = status
161 if not songchanged then
162 self:update_widget()
163 if statechanged then
164 self:popup_oneshot(5)
165 end
166 return
167 end
168
169 conn:send("currentsong", function(_, song)
170 self._currentsong = song
171 if not song then
172 self:update_widget()
173 return
174 end
175 conn:albumart(song.file, function(_, art)
176 self._albumart = art
177 self:update_widget()
178 self:popup_oneshot(5)
179 end)
180 end)
181 end)
182 end
183 )
184
185 self._keep_alive_timer = timer {
186 timeout = 1
187 , autostart = true
188 , callback = function() self._connection:connect() end
189 , call_now = true
190 }
191
192 self._timer = timer({ timeout = 1 })
193 self._timer:connect_signal("timeout", function() self:popup_update() end)
194
195 self._hover = false;
196 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
197 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
198 end
199
200 widget:run()
201
202 widget.scroll:buttons(gears.table.join(
203 awful.button({}, 1, function() widget._connection:toggle_play() end)
204 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
205 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
206 )
207
208 globalkeys = gears.table.join(globalkeys,
209 --TODO headphone support
210 awful.key({ }, "XF86AudioPlay", function() widget._connection:play() end,
211 { description = "play", group = "mpd" }),
212 awful.key({ }, "XF86AudioPause", function() widget._connection:pause() end,
213 { description = "pause", group = "mpd" }),
214 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
215 { description = "toogle play", group = "mpd" }),
216 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
217 { description = "show more info", group = "mpd" }),
218 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
219 { description = "previous track", group = "mpd" }),
220 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
221 { description = "next track", group = "mpd" })
222 )
223 return {
224 layout = wibox.layout.align.horizontal,
225 forced_width = 200,
226 expand = "outside",
227 nil,
228 widget.scroll,
229 nil,
230 }