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 function widget:get_info(callback)
48 self._connection:send("status", function(_, s) status = s end)
52 self._connection:send("currentsong", function(_, s)
54 self._connection:albumart(song.file, function(_, art)
56 if art and art.binary then
57 info.icon = '/tmp/test.jpg'
58 local f = io.open(info.icon, 'w')
63 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
64 if not song.artist then
65 info.text = string.format("%s", song.title or song.file)
67 info.text = string.format("%s - %s", song.artist, song.title)
70 info.text = info.text .. "\n" .. tostring(song.album or "")
77 function widget:popup_show()
78 self:get_info(function(table)
79 if self._notification then return end
80 self._notification = naughty.notify(
86 , 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:again()
108 self._hide_timer = timer(
117 function widget:popup_update()
118 self:get_info(function(table)
119 if not self._notification then return end
120 naughty.replace_text(self._notification, table.title, table.text)
124 function widget:popup_oneshot(timeout)
125 if self._notification then
134 function widget:error_handler(err)
135 self._textbox:set_text("Error: " .. tostring(err))
136 timer.start_new(10, function()
137 self._connection:send("ping")
141 function widget:run()
142 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
143 "status", function(_, result)
144 if not self._status or result.state ~= self._status.state then
145 self._status = result
147 self:popup_oneshot(5)
150 "currentsong", function(_, result)
151 if not self._currentsong or result.id ~= self._currentsong.id then
152 self._currentsong = result
154 self:popup_oneshot(5)
159 self._timer = timer({ timeout = 1 })
160 self._timer:connect_signal("timeout", function() self:popup_update() end)
163 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
164 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
169 widget.scroll:buttons(gears.table.join(
170 awful.button({}, 1, function() widget._connection:toggle_play() end)
171 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
172 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
175 globalkeys = gears.table.join(globalkeys,
176 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
177 { description = "toogle play", group = "mpd" }),
178 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
179 { description = "show more info", group = "mpd" }),
180 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
181 { description = "previous track", group = "mpd" }),
182 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
183 { description = "next track", group = "mpd" })
186 layout = wibox.layout.align.horizontal,