]> git.rmz.io Git - dotfiles.git/blob - awesome/widgets/mpc.lua
awesome: set tooltip colours to same as normal
[dotfiles.git] / awesome / widgets / mpc.lua
1 -- Originally from https://awesomewm.org/recipes/mpc/
2
3 local lgi = require "lgi"
4 local GLib = lgi.GLib
5 local Gio = lgi.Gio
6
7 local trace_mpc = false
8
9 local mpc = {}
10
11 local function parse_password(host)
12 -- This function is based on mpd_parse_host_password() from libmpdclient
13 local position = string.find(host, "@")
14 if not position then
15 return host
16 end
17 return string.sub(host, position + 1), string.sub(host, 1, position - 1)
18 end
19
20 function mpc.new(host, port, password, error_handler, ...)
21 host = host or os.getenv("MPD_HOST") or "localhost"
22 port = port or os.getenv("MPD_PORT") or 6600
23 if not password then
24 host, password = parse_password(host)
25 end
26 local self = setmetatable({
27 _host = host,
28 _port = port,
29 _password = password,
30 _error_handler = error_handler or function() end,
31 _connected = false,
32 _try_reconnect = false,
33 _idle_subsystems = { ... }
34 }, { __index = mpc })
35 self:_connect()
36 return self
37 end
38
39 function mpc:_write(command)
40 if trace_mpc then
41 print("write: " .. command)
42 end
43 self._output:write(command .. "\n")
44 end
45
46 function mpc:_error(err)
47 self._connected = false
48 self._error_handler(err)
49 self._try_reconnect = not self._try_reconnect
50 if self._try_reconnect then
51 self:_connect()
52 end
53 end
54
55 function mpc:_connect()
56 if self._connected then return end
57 -- Reset all of our state
58 self._reply_handlers = {}
59 self._pending_reply = {}
60 self._idle_commands_pending = false
61 self._idle = false
62 self._connected = true
63
64 -- Set up a new connection
65 local address
66 if string.sub(self._host, 1, 1) == "/" then
67 -- It's a unix socket
68 address = Gio.UnixSocketAddress.new(self._host)
69 else
70 -- Do a TCP connection
71 address = Gio.NetworkAddress.new(self._host, self._port)
72 end
73 local client = Gio.SocketClient()
74 local conn, err = client:connect(address)
75
76 if not conn then
77 self:_error(err)
78 return false
79 end
80
81 local input, output = conn:get_input_stream(), conn:get_output_stream()
82 self._conn, self._output, self._input = conn, output, Gio.DataInputStream.new(input)
83
84 -- Read the welcome message
85 self._input:read_line()
86
87 if self._password and self._password ~= "" then
88 self:_send("password " .. self._password)
89 end
90
91 -- Set up the reading loop. This will asynchronously read lines by
92 -- calling itself.
93 local do_read
94 do_read = function()
95 self._input:read_line_async(GLib.PRIORITY_DEFAULT, nil, function(obj, res)
96 local line, err = obj:read_line_finish(res)
97 -- Ugly API. On success we get string, length-of-string
98 -- and on error we get nil, error. Other versions of lgi
99 -- behave differently.
100 if line == nil or tostring(line) == "" then
101 err = "Connection closed"
102 end
103 if type(err) ~= "number" then
104 self._output, self._input = nil, nil
105 self:_error(err)
106 else
107 line = tostring(line)
108 if line == "OK" or line:match("^ACK ") then
109 local success = line == "OK"
110 local arg
111 if success then
112 arg = self._pending_reply
113 else
114 arg = { line }
115 end
116 local handler = self._reply_handlers[1]
117 table.remove(self._reply_handlers, 1)
118 self._pending_reply = {}
119 handler(success, arg)
120
121 if next(self._reply_handlers) == nil then
122 self:_start_idle()
123 end
124 else
125 local _, _, key, value = string.find(line, "([^:]+):%s(.+)")
126 if key then
127 local k = string.lower(key)
128 if k == "binary" then
129 value = tonumber(value)
130 local data = {}
131 while value > 0 do
132 local b = assert(obj:read_bytes(value))
133 table.insert(data, b.data)
134 value = value - #b
135 end
136 local w = obj:read_bytes(1) -- read newline at end of binary
137 self._pending_reply[k] = table.concat(data)
138 elseif k == "changed" then
139 if not self._pending_reply[k] then
140 self._pending_reply[k] = {}
141 end
142
143 self._pending_reply[k][value] = true
144 else
145 self._pending_reply[k] = value
146 end
147 end
148 end
149 do_read()
150 end
151 end)
152 end
153 do_read()
154
155 -- To synchronize the state on startup, send the idle commands now.
156 for i = 1, #self._idle_subsystems, 2 do
157 self._idle_subsystems[i+1](self)
158 end
159
160 return self
161 end
162
163 function mpc:_start_idle()
164 if self._idle then
165 error("start_idle but already idle")
166 end
167 self:_send("idle", function(success, reply)
168 self._idle = false
169 if reply.changed then
170 -- idle mode was disabled by mpd
171 for i = 1, #self._idle_subsystems, 2 do
172 local subsys = self._idle_subsystems[i]
173 if reply.changed[subsys] then
174 self._idle_subsystems[i+1](self)
175 end
176 end
177 end
178 end)
179 self._idle = true
180 end
181
182 function mpc:_stop_idle()
183 if not self._idle then
184 error("stop_idle but not idle")
185 end
186 self:_write("noidle")
187 self._idle = false
188 end
189
190 function mpc:_send(command, callback)
191 if self._idle then
192 error("Still idle in send()?!")
193 end
194 self:_write(command)
195 table.insert(self._reply_handlers, callback or function() end)
196 end
197
198 function mpc:send(...)
199 self:_connect()
200 if not self._connected then
201 return
202 end
203 if self._idle then
204 self:_stop_idle()
205 end
206 local args = { ... }
207 for i = 1, #args, 2 do
208 self:_send(args[i], args[i+1])
209 end
210 end
211
212 function mpc:toggle_play()
213 self:send("status", function(success, status)
214 if status.state == "stop" then
215 self:send("play")
216 else
217 self:send("pause")
218 end
219 end)
220 end
221
222 function clamp(x, min, max)
223 return math.min(math.max(x, min), max)
224 end
225
226 function mpc:change_volume(change)
227 self:send("status", function(_, status)
228 new_vol = clamp(tonumber(status.volume) + change, 0, 100)
229 self:send("setvol " .. new_vol)
230 end)
231 end
232
233 function mpc:currentsong()
234 local currentsong
235 self:send("currentsong", function(err, song)
236 if err then error(err) end
237 currentsong = song
238 end)
239 return currentsong
240 end
241
242 local function escape(str)
243 return "\"" .. str .. "\""
244 end
245
246 function mpc:albumart(uri, handler)
247 local image_table = {}
248 local get_art_at
249 get_art_at = function(off)
250 self:send("albumart " .. escape(uri) .. " " .. tostring(off), function(success, data)
251 if not success then
252 handler(success, data)
253 end
254 table.insert(image_table, data.binary)
255 if data.binary and #data.binary > 0 then
256 get_art_at(off + #data.binary)
257 else
258 data.binary = table.concat(image_table)
259 handler(success, data)
260 end
261 end)
262 end
263 get_art_at(0)
264 end
265
266 --[[
267
268 -- Example on how to use this (standalone)
269
270 local host, port, password = nil, nil, nil
271 local m = mpc.new(host, port, password, error,
272 "status", function(success, status) print("status is", status.state) end)
273
274 GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, function()
275 -- Test command submission
276 m:send("status", function(_, s) print(s.state) end,
277 "currentsong", function(_, s) print(s.title) end)
278 m:send("status", function(_, s) print(s.state) end)
279 -- Force a reconnect
280 GLib.timeout_add(GLib.PRIORITY_DEFAULT, 1000, function()
281 m._conn:close()
282 end)
283 end)
284
285 GLib.MainLoop():run()
286 --]]
287
288 return mpc