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