]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome: set tooltip colours to same as normal
[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 local dbg = require("gears.debug")
47 function widget:get_albumart()
48 local art = self._albumart
49 -- dbg.dump(art)
50 -- dbg.dump(self._albumart.binary)
51 if art and art.binary then
52 local path = '/tmp/test.jpg'
53 local f = io.open(path, 'w')
54 f:write(art.binary)
55 f:close()
56 return path
57 else
58 return beautiful.mpd_default_album
59 end
60 end
61
62 function widget:get_info()
63 local info = {}
64 local status, song = self._status, self._currentsong
65 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
66 if not song.artist then
67 info.text = string.format("%s", song.title or song.file)
68 else
69 info.text = string.format("%s - %s", song.artist, song.title)
70 end
71 if song.album then
72 info.text = info.text .. "\n" .. tostring(song.album or "")
73 end
74
75 return info
76 end
77
78 function widget:popup_show()
79 if self._notification then return end
80 local table = self:get_info()
81 self._timer:start()
82 self._notification = naughty.notify(
83 { title = table.title
84 , icon = self:get_albumart()
85 , icon_size = 64
86 , text = table.text
87 , timeout = 0
88 , destroy = function() self._timer:stop(); self._notification = nil end
89 })
90 end
91
92 function widget:popup_hide(delay)
93 local function destroy()
94 if self._hover then return end
95 if not self._notification then return end
96 naughty.destroy(self._notification)
97 self._notification = nil
98 end
99
100 if not delay then
101 destroy()
102 return
103 end
104
105 if self._hide_timer and self._hide_timer.started then
106 self._hide_timer.timeout = delay
107 self._hide_timer:again()
108 else
109 self._hide_timer = timer(
110 { timeout = delay
111 , autostart = true
112 , single_shot = true
113 , callback = destroy
114 })
115 end
116 end
117
118 function widget:popup_update()
119 if not self._notification then return end
120 self._connection:send("status", function(_, status)
121 self._status = status
122 local table = self:get_info()
123 if not self._notification then return end
124 naughty.replace_text(self._notification, table.title, table.text)
125 end)
126 end
127
128 function widget:popup_oneshot(timeout)
129 if self._notification then
130 self:popup_hide()
131 self:popup_show()
132 else
133 self:popup_show()
134 end
135 self:popup_hide(5)
136 end
137
138 function widget:error_handler(err)
139 self._textbox:set_text("Error: " .. tostring(err))
140 timer.start_new(10, function()
141 self._connection:send("ping")
142 end)
143 end
144
145 function widget:run()
146 self._status = {}
147 self._status.songid = nil
148
149 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
150 "player", function(conn)
151 conn:send("status", function(err, status)
152 local songchanged = self._status.songid ~= status.songid
153 local statechanged = self._status.state ~= status.state
154 self._status = status
155 if not songchanged then
156 self:update_widget()
157 if statechanged then
158 self:popup_oneshot(5)
159 end
160 return
161 end
162
163 conn:send("currentsong", function(_, song)
164 self._currentsong = song
165 conn:albumart(song.file, function(_, art)
166 self._albumart = art
167 self:update_widget()
168 self:popup_oneshot(5)
169 end)
170 end)
171 end)
172 end
173 )
174
175 self._timer = timer({ timeout = 1 })
176 self._timer:connect_signal("timeout", function() self:popup_update() end)
177
178 self._hover = false;
179 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
180 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
181 end
182
183 widget:run()
184
185 widget.scroll:buttons(gears.table.join(
186 awful.button({}, 1, function() widget._connection:toggle_play() end)
187 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
188 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
189 )
190
191 globalkeys = gears.table.join(globalkeys,
192 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
193 { description = "toogle play", group = "mpd" }),
194 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
195 { description = "show more info", group = "mpd" }),
196 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
197 { description = "previous track", group = "mpd" }),
198 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
199 { description = "next track", group = "mpd" })
200 )
201 return {
202 layout = wibox.layout.align.horizontal,
203 forced_width = 200,
204 expand = "outside",
205 nil,
206 widget.scroll,
207 nil,
208 }