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")
11 mpd_widget = textbox()
13 local function update_widget()
15 if state == "pause" then
18 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
19 mpd_widget.text = text
23 local function error_handler(err)
24 mpd_widget:set_text("Error: " .. tostring(err))
25 timer.start_new(10, function()
26 connection:send("ping")
30 connection = mpc.new(nil, nil, nil, error_handler,
31 "status", function(_, result)
34 "currentsong", function(_, result)
35 title, artist, file = result.title, result.artist, result.file
40 mpd_widget:buttons(gears.table.join(
41 awful.button({}, 1, function() connection:toggle_play() end)
42 , awful.button({}, 4, function() connection:change_volume(5) end)
43 , awful.button({}, 5, function() connection:change_volume(-5) end))
46 local function sec_to_min(sec)
47 m, s = math.modf((sec or 0.0) / 60)
48 return string.format('%d:%02d', m, math.floor(s * 60))
51 local function song_duration(elapsed, duration)
52 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
55 local function get_info(callback)
57 connection:send("status", function(_, s) status = s end)
60 connection:send("currentsong", function(_, s) song = s end)
62 timer.start_new(0.10, function()
63 if status == nil or song == nil then return true end
69 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
70 info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
71 .. tostring(song.album or "")
79 get_info(function(table)
80 if self._notification ~= nil then return end
81 self._notification = naughty.notify(
91 if self._notification ~= nil then
92 naughty.destroy(self._notification)
93 self._notification = nil
97 function popup:update()
98 get_info(function(table)
99 if self._notification == nil then return end
100 naughty.replace_text(self._notification, table.title, table.text)
105 self._timer = timer({ timeout = 1 })
106 self._timer:connect_signal("start", function() self:show() end)
107 self._timer:connect_signal("timeout", function() self:update() end)
108 self._timer:connect_signal("stop", function() self:hide() end) --TODO delay
110 mpd_widget:connect_signal("mouse::enter", function() self._timer:again() end)
111 mpd_widget:connect_signal("mouse::leave", function() self._timer:stop() end)
116 globalkeys = gears.table.join(globalkeys,
117 awful.key({ modkey }, "p", function() connection:toggle_play() end,
118 { description = "toogle play", group = "mpd" }),
119 awful.key({ modkey }, ",", function() connection:send("previous") end,
120 { description = "previous track", group = "mpd" }),
121 awful.key({ modkey }, ".", function() connection:send("next") end,
122 { description = "next track", group = "mpd" })