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")
9 local dbg = require("gears.debug")
12 widget._textbox = textbox()
13 widget._currentsong = {}
15 function widget:update_widget()
17 local artist = self._currentsong.artist
18 local title = self._currentsong.title
19 local file = self._currentsong.file
20 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
22 text = string.format("%s", title or file )
24 text = string.format("%s - %s", artist, title)
26 self._textbox.text = text
29 local function sec_to_min(sec)
30 m, s = math.modf((sec or 0.0) / 60)
31 return string.format('%d:%02d', m, math.floor(s * 60))
34 local function song_duration(elapsed, duration)
35 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
38 function widget:get_info(callback)
40 self._connection:send("status", function(_, s) status = s end)
43 self._connection:send("currentsong", function(_, s) song = s end)
45 self._connection:send("ping", function()
48 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
49 info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
50 .. tostring(song.album or "")
56 function widget:popup_show()
57 self:get_info(function(table)
58 if self._notification then return end
59 self._notification = naughty.notify(
64 , destroy = function() self._timer:stop() end
70 function widget:popup_hide(delay)
71 if self._hide_timer and self._hide_timer.started then
72 self._hide_timer:again()
74 self._hide_timer = timer(
78 , callback = function()
79 if self._hover then return end
80 if not self._notification then return end
81 naughty.destroy(self._notification)
82 self._notification = nil
88 function widget:popup_update()
89 self:get_info(function(table)
90 if not self._notification then return end
91 naughty.replace_text(self._notification, table.title, table.text)
95 function widget:popup_oneshot(timeout)
96 if self._notification then
105 function widget:error_handler(err)
106 self._textbox:set_text("Error: " .. tostring(err))
107 timer.start_new(10, function()
108 self._connection:send("ping")
112 function widget:run()
113 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
114 "status", function(_, result)
115 if not self._status or result.state ~= self._status.state then
116 self._status = result
118 self:popup_oneshot(5)
121 "currentsong", function(_, result)
122 if not self._currentsong or result.id ~= self._currentsong.id then
123 self._currentsong = result
125 self:popup_oneshot(5)
130 self._timer = timer({ timeout = 1 })
131 self._timer:connect_signal("timeout", function() self:popup_update() end)
134 self._textbox:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
135 self._textbox:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
140 widget._textbox:buttons(gears.table.join(
141 awful.button({}, 1, function() widget._connection:toggle_play() end)
142 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
143 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
146 globalkeys = gears.table.join(globalkeys,
147 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
148 { description = "toogle play", group = "mpd" }),
149 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
150 { description = "show more info", group = "mpd" }),
151 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
152 { description = "previous track", group = "mpd" }),
153 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
154 { description = "next track", group = "mpd" })
156 return widget._textbox