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