]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome/mpd_widget: use ping command to sync state
[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 connection:send("ping", function()
63 dbg.dump(status)
64 dbg.dump(song)
65
66 local info = {}
67 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
68 info.text = tostring(song.artist or "") .. " - " .. tostring(song.title or "") .. "\n"
69 .. tostring(song.album or "")
70 callback(info)
71 return false
72 end)
73 end
74
75 local popup = {}
76 function popup:show()
77 get_info(function(table)
78 if self._notification ~= nil then return end
79 self._notification = naughty.notify(
80 { title = table.title
81 -- , icon = img
82 , text = table.text
83 , timeout = 0
84 })
85 end)
86 end
87
88 function popup:hide()
89 if self._notification ~= nil then
90 naughty.destroy(self._notification)
91 self._notification = nil
92 end
93 end
94
95 function popup:update()
96 get_info(function(table)
97 if self._notification == nil then return end
98 naughty.replace_text(self._notification, table.title, table.text)
99 end)
100 end
101
102 function popup:run()
103 self._timer = timer({ timeout = 1 })
104 self._timer:connect_signal("start", function() self:show() end)
105 self._timer:connect_signal("timeout", function() self:update() end)
106 self._timer:connect_signal("stop", function() self:hide() end) --TODO delay
107
108 mpd_widget:connect_signal("mouse::enter", function() self._timer:again() end)
109 mpd_widget:connect_signal("mouse::leave", function() self._timer:stop() end)
110 end
111
112 popup:run()
113
114 globalkeys = gears.table.join(globalkeys,
115 awful.key({ modkey }, "p", function() connection:toggle_play() end,
116 { description = "toogle play", group = "mpd" }),
117 awful.key({ modkey }, ",", function() connection:send("previous") end,
118 { description = "previous track", group = "mpd" }),
119 awful.key({ modkey }, ".", function() connection:send("next") end,
120 { description = "next track", group = "mpd" })
121 )
122 return mpd_widget