]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome/mpd_widget: add persistent popup
[dotfiles.git] / awesome / widgets / mpd_widget.lua
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
8 --TODO remove
9 local dbg = require("gears.debug")
10
11 mpd_widget = textbox()
12
13 local function update_widget()
14 local text = ""
15 if state == "pause" then
16 text = "|| "
17 end
18 text = text .. tostring(artist or "") .. " - " .. tostring(title or "")
19 mpd_widget.text = text
20 end
21
22 local connection
23 local function error_handler(err)
24 mpd_widget:set_text("Error: " .. tostring(err))
25 timer.start_new(10, function()
26 connection:send("ping")
27 end)
28 end
29
30 connection = mpc.new(nil, nil, nil, error_handler,
31 "status", function(_, result)
32 state = result.state
33 end,
34 "currentsong", function(_, result)
35 title, artist, file = result.title, result.artist, result.file
36 pcall(update_widget)
37 end
38 )
39
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))
44 )
45
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))
49 end
50
51 local function song_duration(elapsed, duration)
52 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
53 end
54
55 local function get_info(callback)
56 local status = nil
57 connection:send("status", function(_, s) status = s end)
58
59 local song = nil
60 connection:send("currentsong", function(_, s) song = s end)
61
62 timer.start_new(0.10, function()
63 if status == nil or song == nil then return true end
64
65 dbg.dump(status)
66 dbg.dump(song)
67
68 local info = {}
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 "")
72 callback(info)
73 return false
74 end)
75 end
76
77 local popup = {}
78 function popup:show()
79 get_info(function(table)
80 if self._notification ~= nil then return end
81 self._notification = naughty.notify(
82 { title = table.title
83 -- , icon = img
84 , text = table.text
85 , timeout = 0
86 })
87 end)
88 end
89
90 function popup:hide()
91 if self._notification ~= nil then
92 naughty.destroy(self._notification)
93 self._notification = nil
94 end
95 end
96
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)
101 end)
102 end
103
104 function popup:run()
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
109
110 mpd_widget:connect_signal("mouse::enter", function() self._timer:again() end)
111 mpd_widget:connect_signal("mouse::leave", function() self._timer:stop() end)
112 end
113
114 popup:run()
115
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" })
123 )
124 return mpd_widget