]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
awesome/battery_widget: remove module
[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 or beautiful.mpd_default_album
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 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 self:get_info(function(table)
120 if not self._notification then return end
121 naughty.replace_text(self._notification, table.title, table.text)
122 end)
123 end
124
125 function widget:popup_oneshot(timeout)
126 if self._notification then
127 self:popup_hide()
128 self:popup_show()
129 else
130 self:popup_show()
131 end
132 self:popup_hide(5)
133 end
134
135 function widget:error_handler(err)
136 self._textbox:set_text("Error: " .. tostring(err))
137 timer.start_new(10, function()
138 self._connection:send("ping")
139 end)
140 end
141
142 function widget:run()
143 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
144 "status", function(_, result)
145 if not self._status or result.state ~= self._status.state then
146 self._status = result
147 self:update_widget()
148 self:popup_oneshot(5)
149 end
150 end,
151 "currentsong", function(_, result)
152 if not self._currentsong or result.id ~= self._currentsong.id then
153 self._currentsong = result
154 self:update_widget()
155 self:popup_oneshot(5)
156 end
157 end
158 )
159
160 self._timer = timer({ timeout = 1 })
161 self._timer:connect_signal("timeout", function() self:popup_update() end)
162
163 self._hover = false;
164 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
165 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
166 end
167
168 widget:run()
169
170 widget.scroll:buttons(gears.table.join(
171 awful.button({}, 1, function() widget._connection:toggle_play() end)
172 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
173 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
174 )
175
176 globalkeys = gears.table.join(globalkeys,
177 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
178 { description = "toogle play", group = "mpd" }),
179 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
180 { description = "show more info", group = "mpd" }),
181 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
182 { description = "previous track", group = "mpd" }),
183 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
184 { description = "next track", group = "mpd" })
185 )
186 return {
187 layout = wibox.layout.align.horizontal,
188 forced_width = 200,
189 expand = "outside",
190 nil,
191 widget.scroll,
192 nil,
193 }