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()
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 )
36 self._textbox.text = text
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))
44 local function song_duration(elapsed, duration)
45 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
48 local dbg = require("gears.debug")
49 function widget:get_albumart()
50 local art = self._albumart
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')
60 return beautiful.mpd_default_album
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
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)
74 info.text = string.format("%s - %s", song.artist, song.title)
77 info.text = info.text .. "\n" .. tostring(song.album or "")
83 function widget:popup_show()
84 if self._notification then return end
86 local table = self:get_info()
87 if not table then return end
90 self._notification = naughty.notify(
92 , icon = self:get_albumart()
96 , destroy = function() self._timer:stop(); self._notification = nil end
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
113 if self._hide_timer and self._hide_timer.started then
114 self._hide_timer.timeout = delay
115 self._hide_timer:again()
117 self._hide_timer = timer(
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 naughty.replace_text(self._notification, table.title, table.text)
136 function widget:popup_oneshot(timeout)
137 if self._notification then
146 function widget:error_handler(err)
147 self._textbox:set_text("Error: " .. tostring(err))
151 function widget:run()
154 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
155 "player", function(conn)
156 conn:send("status", function(err, status)
157 local songchanged = self._status.songid ~= status.songid
158 local statechanged = self._status.state ~= status.state
159 self._status = status
160 if not songchanged then
163 self:popup_oneshot(5)
168 conn:send("currentsong", function(_, song)
169 self._currentsong = song
174 conn:albumart(song.file, function(_, art)
177 self:popup_oneshot(5)
184 self._keep_alive_timer = timer {
187 , callback = function() self._connection:connect() end
191 self._timer = timer({ timeout = 1 })
192 self._timer:connect_signal("timeout", function() self:popup_update() end)
195 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
196 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
201 widget.scroll:buttons(gears.table.join(
202 awful.button({}, 1, function() widget._connection:toggle_play() end)
203 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
204 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
207 globalkeys = gears.table.join(globalkeys,
208 --TODO headphone support
209 awful.key({ }, "XF86AudioPlay", function() widget._connection:play() end,
210 { description = "play", group = "mpd" }),
211 awful.key({ }, "XF86AudioPause", function() widget._connection:pause() end,
212 { description = "pause", group = "mpd" }),
213 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
214 { description = "toogle play", group = "mpd" }),
215 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
216 { description = "show more info", group = "mpd" }),
217 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
218 { description = "previous track", group = "mpd" }),
219 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
220 { description = "next track", group = "mpd" })
223 layout = wibox.layout.align.horizontal,