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")
10 widget._textbox = textbox()
11 widget._currentsong = {}
13 widget.scroll = wibox.widget {
14 layout = wibox.container.scroll.horizontal,
17 step_function = wibox.container.scroll.step_functions.linear_increase,
20 widget = widget._textbox,
24 function widget:update_widget()
25 if not self._currentsong then
26 self._textbox.text = ""
30 local artist = self._currentsong.artist
31 local title = self._currentsong.title
32 local file = self._currentsong.file
33 if artist and title then
34 text = string.format("%s - %s", artist, title)
35 elseif title or file then
36 text = string.format("%s", title or file )
40 self._textbox.text = text
43 local function sec_to_min(sec)
44 local m, s = math.modf((sec or 0.0) / 60)
45 return string.format('%d:%02d', m, math.floor(s * 60))
48 local function song_duration(elapsed, duration)
49 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
52 local dbg = require("gears.debug")
53 function widget:get_albumart()
54 local art = self._albumart
56 -- dbg.dump(self._albumart.binary)
57 if art and art.binary then
58 local path = '/tmp/test.jpg'
59 local f = io.open(path, 'w')
64 return beautiful.mpd_default_album
68 function widget:get_info()
69 local status, song = self._status, self._currentsong
70 if not status.state then return nil end
71 if not status.song then return nil end
74 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
75 if not song.artist then
76 info.text = string.format("%s", song.title or song.file)
78 info.text = string.format("%s - %s", song.artist, song.title)
81 info.text = info.text .. "\n" .. tostring(song.album or "")
87 function widget:popup_show()
88 if self._notification then return end
90 local table = self:get_info()
91 if not table then return end
94 self._notification = naughty.notify(
96 , icon = self:get_albumart()
100 , destroy = function() self._timer:stop(); self._notification = nil end
104 function widget:popup_hide(delay)
105 local function destroy()
106 if self._hover then return end
107 if not self._notification then return end
108 naughty.destroy(self._notification)
109 self._notification = nil
117 if self._hide_timer and self._hide_timer.started then
118 self._hide_timer.timeout = delay
119 self._hide_timer:again()
121 self._hide_timer = timer(
130 function widget:popup_update()
131 if not self._notification then return end
132 self._connection:send("status", function(_, status)
133 self._status = status
134 local table = self:get_info()
135 if not self._notification then return end
136 if not table then return end
137 naughty.replace_text(self._notification, table.title, table.text)
141 function widget:popup_oneshot(timeout)
142 if self._notification then
151 function widget:error_handler(err)
152 self._textbox:set_text("Error: " .. tostring(err))
156 function widget:run()
159 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
160 "player", function(conn)
161 conn:send("status", function(err, status)
162 local songchanged = self._status.songid ~= status.songid
163 local statechanged = self._status.state ~= status.state
164 self._status = status
165 if not songchanged then
168 self:popup_oneshot(5)
173 conn:send("currentsong", function(_, song)
174 self._currentsong = song
179 conn:albumart(song.file, function(_, art)
182 self:popup_oneshot(5)
189 self._keep_alive_timer = timer {
192 , callback = function() self._connection:connect() end
196 self._timer = timer({ timeout = 1 })
197 self._timer:connect_signal("timeout", function() self:popup_update() end)
200 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
201 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
206 widget.scroll:buttons(gears.table.join(
207 awful.button({}, 1, function() widget._connection:toggle_play() end)
208 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
209 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
212 globalkeys = gears.table.join(globalkeys,
213 --TODO headphone support
214 awful.key({ }, "XF86AudioPlay", function() widget._connection:play() end,
215 { description = "play", group = "mpd" }),
216 awful.key({ }, "XF86AudioPause", function() widget._connection:pause() end,
217 { description = "pause", group = "mpd" }),
218 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
219 { description = "toogle play", group = "mpd" }),
220 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
221 { description = "show more info", group = "mpd" }),
222 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
223 { description = "previous track", group = "mpd" }),
224 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
225 { description = "next track", group = "mpd" })
228 layout = wibox.layout.align.horizontal,