]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpd_widget.lua
7f96b2408c22dfb6d9321f05a13ac16530fe4533
[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 self._connection:send("currentsong", function(_, s) song = s end)
52
53 self._connection:send("ping", function()
54
55 local info = {}
56 info.title = status.state .. " " .. status.song .. "/" .. status.playlistlength .. " " .. song_duration(status.elapsed, status.duration)
57 if not song.artist then
58 info.text = string.format("%s", song.title or song.file)
59 else
60 info.text = string.format("%s - %s", song.artist, song.title)
61 end
62 if song.album then
63 info.text = info.text .. "\n" .. tostring(song.album or "")
64 end
65 callback(info)
66 end)
67 end
68
69 function widget:popup_show()
70 self:get_info(function(table)
71 if self._notification then return end
72 self._notification = naughty.notify(
73 { title = table.title
74 -- , icon = img
75 , text = table.text
76 , timeout = 0
77 , destroy = function() self._timer:stop() end
78 })
79 self._timer:start()
80 end)
81 end
82
83 function widget:popup_hide(delay)
84 if self._hide_timer and self._hide_timer.started then
85 self._hide_timer:again()
86 else
87 self._hide_timer = timer(
88 { timeout = delay
89 , autostart = true
90 , single_shot = true
91 , callback = function()
92 if self._hover then return end
93 if not self._notification then return end
94 naughty.destroy(self._notification)
95 self._notification = nil
96 end
97 })
98 end
99 end
100
101 function widget:popup_update()
102 self:get_info(function(table)
103 if not self._notification then return end
104 naughty.replace_text(self._notification, table.title, table.text)
105 end)
106 end
107
108 function widget:popup_oneshot(timeout)
109 if self._notification then
110 self:popup_update()
111 self:popup_hide(5)
112 else
113 self:popup_show()
114 self:popup_hide(5)
115 end
116 end
117
118 function widget:error_handler(err)
119 self._textbox:set_text("Error: " .. tostring(err))
120 timer.start_new(10, function()
121 self._connection:send("ping")
122 end)
123 end
124
125 function widget:run()
126 self._connection = mpc.new(nil, nil, nil, function(err) self:error_handler(err) end,
127 "status", function(_, result)
128 if not self._status or result.state ~= self._status.state then
129 self._status = result
130 self:update_widget()
131 self:popup_oneshot(5)
132 end
133 end,
134 "currentsong", function(_, result)
135 if not self._currentsong or result.id ~= self._currentsong.id then
136 self._currentsong = result
137 self:update_widget()
138 self:popup_oneshot(5)
139 end
140 end
141 )
142
143 self._timer = timer({ timeout = 1 })
144 self._timer:connect_signal("timeout", function() self:popup_update() end)
145
146 self._hover = false;
147 self.scroll:connect_signal("mouse::enter", function() self._hover = true; self:popup_show() end)
148 self.scroll:connect_signal("mouse::leave", function() self._hover = false; self:popup_hide(2) end)
149 end
150
151 widget:run()
152
153 widget.scroll:buttons(gears.table.join(
154 awful.button({}, 1, function() widget._connection:toggle_play() end)
155 , awful.button({}, 4, function() widget._connection:change_volume(5) end)
156 , awful.button({}, 5, function() widget._connection:change_volume(-5) end))
157 )
158
159 globalkeys = gears.table.join(globalkeys,
160 awful.key({ modkey }, "p", function() widget._connection:toggle_play() end,
161 { description = "toogle play", group = "mpd" }),
162 awful.key({ modkey }, "'", function() widget:popup_oneshot(5) end,
163 { description = "show more info", group = "mpd" }),
164 awful.key({ modkey }, ",", function() widget._connection:send("previous") end,
165 { description = "previous track", group = "mpd" }),
166 awful.key({ modkey }, ".", function() widget._connection:send("next") end,
167 { description = "next track", group = "mpd" })
168 )
169 return {
170 layout = wibox.layout.align.horizontal,
171 forced_width = 200,
172 expand = "outside",
173 nil,
174 widget.scroll,
175 nil,
176 }