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