]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome/mpd_widget: request albumart when getting info
[dotfiles.git] / awesome / widgets / mpd_widget.lua
1 local awful = require("awful")
2 local gears = require("gears")
3 local mpc = require("widgets/mpc")
4 local naughty = require("naughty")
5 local textbox = require("wibox.widget.textbox")
6 local timer = require("gears.timer")
7 local wibox = require("wibox")
8
9 local widget = {}
10 widget._textbox = textbox()
11 widget._currentsong = {}
12
13 widget.scroll = wibox.widget {
14 layout = wibox.container.scroll.horizontal,
15 max_size = 200,
16 extra_space = 10,
17 step_function = wibox.container.scroll.step_functions.linear_increase,
18 speed = 20,
19 {
20 widget = widget._textbox,
21 }
22 }
23
24 function widget:update_widget()
25 local text = ""
26 local artist = self._currentsong.artist
27 local title = self._currentsong.title
28 local file = self._currentsong.file
29 if not artist then
30 text = string.format("%s", title or file )
31 else
32 text = string.format("%s - %s", artist, title)
33 end
34 self._textbox.text = text
35 end
36
37 local function sec_to_min(sec)
38 m, s = math.modf((sec or 0.0) / 60)
39 return string.format('%d:%02d', m, math.floor(s * 60))
40 end
41
42 local function song_duration(elapsed, duration)
43 return sec_to_min(elapsed) .. "/" .. sec_to_min(duration)
44 end
45
46 function widget:get_info(callback)
47 local status = nil
48 self._connection:send("status", function(_, s) status = s end)
49
50 local song = nil
51
52 self._connection:send("currentsong", function(_, s)
53 song = s
54 self._connection:albumart(song.file, function(_, art)
55 local info = {}
56 if art and art.binary then
57 info.icon = '/tmp/test.jpg'
58 local f = io.open(info.icon, 'w')
59 f:write(art.binary)
60 f:close()
61 end
62
63 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
64 if not song.artist then
65 info.text = string.format("%s", song.title or song.file)
66 else
67 info.text = string.format("%s - %s", song.artist, song.title)
68 end
69 if song.album then
70 info.text = info.text .. "\n" .. tostring(song.album or "")
71 end
72 callback(info)
73 end)
74 end)
75 end
76
77 function widget:popup_show()
78 self:get_info(function(table)
79 if self._notification then return end
80 self._notification = naughty.notify(
81 { title = table.title
82 , icon = table.icon
83 , icon_size = 64
84 , text = table.text
85 , timeout = 0
86 , destroy = function() self._timer:stop(); self._notification = nil end
87 })
88 self._timer:start()
89 end)
90 end
91
92 function widget:popup_hide(delay)
93 if self._hide_timer and self._hide_timer.started then
94 self._hide_timer:again()
95 else
96 self._hide_timer = timer(
97 { timeout = delay
98 , autostart = true
99 , single_shot = true
100 , callback = function()
101 if self._hover then return end
102 if not self._notification then return end
103 naughty.destroy(self._notification)
104 self._notification = nil
105 end
106 })
107 end
108 end
109
110 function widget:popup_update()
111 self:get_info(function(table)
112 if not self._notification then return end
113 naughty.replace_text(self._notification, table.title, table.text)
114 end)
115 end
116
117 function widget:popup_oneshot(timeout)
118 if self._notification then
119 self:popup_update()
120 self:popup_hide(5)
121 else
122 self:popup_show()
123 self:popup_hide(5)
124 end
125 end
126
127 function widget:error_handler(err)
128 self._textbox:set_text("Error: " .. tostring(err))
129 timer.start_new(10, function()
130 self._connection:send("ping")
131 end)
132 end
133
134 function widget:run()
135 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
136 "status", function(_, result)
137 if not self._status or result.state ~= self._status.state then
138 self._status = result
139 self:update_widget()
140 self:popup_oneshot(5)
141 end
142 end,
143 "currentsong", function(_, result)
144 if not self._currentsong or result.id ~= self._currentsong.id then
145 self._currentsong = result
146 self:update_widget()
147 self:popup_oneshot(5)
148 end
149 end
150 )
151
152 self._timer = timer({ timeout = 1 })
153 self._timer:connect_signal("timeout", function() self:popup_update() end)
154
155 self._hover = false;
156 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
157 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
158 end
159
160 widget:run()
161
162 widget.scroll:buttons(gears.table.join(
163 awful.button({}, 1, function() widget._connection:toggle_play() end)
164 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
165 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
166 )
167
168 globalkeys = gears.table.join(globalkeys,
169 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
170 { description = "toogle play", group = "mpd" }),
171 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
172 { description = "show more info", group = "mpd" }),
173 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
174 { description = "previous track", group = "mpd" }),
175 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
176 { description = "next track", group = "mpd" })
177 )
178 return {
179 layout = wibox.layout.align.horizontal,
180 forced_width = 200,
181 expand = "outside",
182 nil,
183 widget.scroll,
184 nil,
185 }