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
30 text = string.format("%s", title or file )
32 text = string.format("%s - %s", artist, title)
34 self._textbox.text = text
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))
42 local function song_duration(elapsed, duration)
43 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
46 local dbg = require("gears.debug")
47 function widget:get_albumart()
48 local art = self._albumart
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')
58 return beautiful.mpd_default_album
62 function widget:get_info()
64 local status, song = self._status, self._currentsong
65 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
66 if not song.artist then
67 info.text = string.format("%s", song.title or song.file)
69 info.text = string.format("%s - %s", song.artist, song.title)
72 info.text = info.text .. "\n" .. tostring(song.album or "")
78 function widget:popup_show()
79 if self._notification then return end
80 local table = self:get_info()
82 self._notification = naughty.notify(
84 , icon = self:get_albumart()
88 , destroy = function() self._timer:stop(); self._notification = nil end
92 function widget:popup_hide(delay)
93 local function destroy()
94 if self._hover then return end
95 if not self._notification then return end
96 naughty.destroy(self._notification)
97 self._notification = nil
105 if self._hide_timer and self._hide_timer.started then
106 self._hide_timer.timeout = delay
107 self._hide_timer:again()
109 self._hide_timer = timer(
118 function widget:popup_update()
119 if not self._notification then return end
120 self._connection:send("status", function(_, status)
121 self._status = status
122 local table = self:get_info()
123 if not self._notification then return end
124 naughty.replace_text(self._notification, table.title, table.text)
128 function widget:popup_oneshot(timeout)
129 if self._notification then
138 function widget:error_handler(err)
139 self._textbox:set_text("Error: " .. tostring(err))
140 timer.start_new(10, function()
141 self._connection:send("ping")
145 function widget:run()
147 self._status.songid = nil
149 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
150 "player", function(conn)
151 conn:send("status", function(err, status)
152 local songchanged = self._status.songid ~= status.songid
153 self._status = status
154 if not songchanged then
159 conn:send("currentsong", function(_, song)
160 self._currentsong = song
161 conn:albumart(song.file, function(_, art)
164 self:popup_oneshot(5)
171 self._timer = timer({ timeout = 1 })
172 self._timer:connect_signal("timeout", function() self:popup_update() end)
175 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
176 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
181 widget.scroll:buttons(gears.table.join(
182 awful.button({}, 1, function() widget._connection:toggle_play() end)
183 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
184 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
187 globalkeys = gears.table.join(globalkeys,
188 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
189 { description = "toogle play", group = "mpd" }),
190 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
191 { description = "show more info", group = "mpd" }),
192 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
193 { description = "previous track", group = "mpd" }),
194 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
195 { description = "next track", group = "mpd" })
198 layout = wibox.layout.align.horizontal,