1 local mpc = require("widgets/mpc")
2 local textbox = require("wibox.widget.textbox")
3 local gears = require("gears")
4 local awful = require("awful")
5 local naughty = require("naughty")
6 local timer = require("gears.timer")
7 local wibox = require("wibox")
10 local dbg = require("gears.debug")
13 widget._textbox = textbox()
14 widget._currentsong = {}
16 widget.scroll = wibox.widget {
17 layout = wibox.container.scroll.horizontal,
20 step_function = wibox.container.scroll.step_functions.linear_increase,
23 widget = widget._textbox,
29 function widget:update_widget()
31 local artist = self._currentsong.artist
32 local title = self._currentsong.title
33 local file = self._currentsong.file
34 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
36 text = string.format("%s", title or file )
38 text = string.format("%s - %s", artist, title)
40 self._textbox.text = text
43 local function sec_to_min(sec)
44 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 function widget:get_info(callback)
54 self._connection:send("status", function(_, s) status = s end)
57 self._connection:send("currentsong", function(_, s) song = s end)
59 self._connection:send("ping", function()
62 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
63 info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
64 .. tostring(song.album or "")
70 function widget:popup_show()
71 self:get_info(function(table)
72 if self._notification then return end
73 self._notification = naughty.notify(
78 , destroy = function() self._timer:stop() end
84 function widget:popup_hide(delay)
85 if self._hide_timer and self._hide_timer.started then
86 self._hide_timer:again()
88 self._hide_timer = timer(
92 , callback = function()
93 if self._hover then return end
94 if not self._notification then return end
95 naughty.destroy(self._notification)
96 self._notification = nil
102 function widget:popup_update()
103 self:get_info(function(table)
104 if not self._notification then return end
105 naughty.replace_text(self._notification, table.title, table.text)
109 function widget:popup_oneshot(timeout)
110 if self._notification then
119 function widget:error_handler(err)
120 self._textbox:set_text("Error: " .. tostring(err))
121 timer.start_new(10, function()
122 self._connection:send("ping")
126 function widget:run()
127 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
128 "status", function(_, result)
129 if not self._status or result.state ~= self._status.state then
130 self._status = result
132 self:popup_oneshot(5)
135 "currentsong", function(_, result)
136 if not self._currentsong or result.id ~= self._currentsong.id then
137 self._currentsong = result
139 self:popup_oneshot(5)
144 self._timer = timer({ timeout = 1 })
145 self._timer:connect_signal("timeout", function() self:popup_update() end)
148 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
149 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
154 widget.scroll:buttons(gears.table.join(
155 awful.button({}, 1, function() widget._connection:toggle_play() end)
156 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
157 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
160 globalkeys = gears.table.join(globalkeys,
161 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
162 { description = "toogle play", group = "mpd" }),
163 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
164 { description = "show more info", group = "mpd" }),
165 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
166 { description = "previous track", group = "mpd" }),
167 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
168 { description = "next track", group = "mpd" })