]> git.wh0rd.org - chrome-ext/music-player-client.git/blob - js/mpc.js
dfcd5fd304278d8d520e637ad98f560dd06c25a1
[chrome-ext/music-player-client.git] / js / mpc.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
2
3 function Mpc(socket, cb_update_state, debug_enabled) {
4 this._socket = socket;
5 this._cb_update_state = cb_update_state;
6 this._debug_enabled = debug_enabled;
7 this._queue = ['init'];
8 this._recv_buffer = [];
9 this._recv_buffer_last = 0;
10 this.state = {};
11 }
12
13 Mpc.prototype.log = function(lvl, msg, obj) {
14 if (this._debug_enabled & lvl)
15 console.log('mpc: ' + msg, obj);
16 }
17
18 Mpc.prototype.err = function(msg, obj) {
19 console.error('mpc: ' + msg, obj);
20 }
21
22 Mpc.prototype.set_debug = function(val) {
23 this._debug_enabled = val;
24 }
25
26 Mpc.prototype.send = function(msg) {
27 var _this = this;
28 this._queue.push(msg);
29 this._socket.send(msg, function(x) {
30 _this.log(0x1, 'send: ' + msg + ':', x);
31 });
32 }
33
34 Mpc.prototype.recv_msg = function(lines) {
35 curr = this._queue.shift();
36 this.log(0x2, 'recv: [' + curr + ']:', lines.join('\n'));
37 curr = curr.split(' ');
38
39 switch (curr[0]) {
40 // Needs to return a list of dicts (see above for dicts).
41 //case 'playlistinfo':
42 case 'currentsong':
43 case 'stats':
44 case 'status':
45 state = {};
46 keys = [];
47 lines.forEach(function(line) {
48 i = line.indexOf(':');
49 if (i == -1)
50 return; // Ignores the OK line
51 key = line.substr(0, i);
52 keys.push(key);
53 val = line.substr(i + 2);
54 state[key] = val;
55 });
56
57 // When mpd is stopped, it gives us back crap values for some things.
58 if ('state' in state && state.state == 'stop') {
59 if ('volume' in state && state.volume == '-1')
60 keys.splice(keys.indexOf('volume'), 1);
61 }
62 // Now merge the current state with the previous one so that we don't
63 // lose information like volume or song position.
64 curr_state = this.state;
65 keys.forEach(function(key) {
66 curr_state[key] = state[key];
67 });
68
69 this._cb_update_state(curr_state);
70 break;
71 default:
72 this._cb_update_state(lines, curr);
73 break;
74 }
75 }
76
77 Mpc.prototype.recv = function(msg) {
78 /* We can get back a bunch of responses in a row, so parse them out */
79 var lines = this._recv_buffer = this._recv_buffer.concat(msg.split('\n'));
80 var i = 0;
81 while (i < lines.length) {
82 if (lines[i] == 'OK' || lines[i].substr(0, 3) == 'OK ') {
83 this.recv_msg(lines.splice(0, i + 1));
84 i = 0;
85 } else
86 ++i;
87 }
88
89 if (lines.length && this._recv_buffer_last != lines.length) {
90 // Keep sucking in data so long as more exists.
91 this._recv_buffer_last = lines.length;
92 this._socket.poll();
93 } else
94 this._recv_buffer_last = lines.length;
95 }
96
97 /*
98 * Command generator helpers.
99 */
100
101 Mpc.__make_send_void = function(cmd) {
102 return function() { this.send(cmd); }
103 }
104
105 Mpc.__make_send_arg1 = function(cmd) {
106 return function(a1) {
107 if (a1 === undefined)
108 this.err(cmd + ': function requires one argument');
109 else
110 this.send(cmd + ' ' + a1);
111 }
112 }
113
114 Mpc.__make_send_arg2 = function(cmd) {
115 return function(a1, a2) {
116 if (a1 === undefined || a2 === undefined)
117 this.err(cmd + ': function requires two arguments');
118 else
119 this.send(cmd + ' ' + a1 + ' ' + a2);
120 }
121 }
122
123 Mpc.__make_send_arg3 = function(cmd) {
124 return function(a1, a2, a3) {
125 if (a1 === undefined || a2 === undefined || a3 == undefined)
126 this.err(cmd + ': function requires three arguments');
127 else
128 this.send(cmd + ' ' + a1 + ' ' + a2 + ' ' + a3);
129 }
130 }
131
132 Mpc.__make_send_opt = function(cmd) {
133 return function(arg) {
134 if (arg === undefined)
135 arg = '';
136 this.send(cmd + ' ' + arg);
137 };
138 }
139
140 Mpc.__make_send_range = function(cmd, min, max, def) {
141 return function(arg) {
142 if (arg === undefined)
143 arg = def;
144 if (arg >= min && arg <= max)
145 this.send(cmd + ' ' + arg);
146 else
147 this.err(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"');
148 };
149 }
150
151 /*
152 * Querying MPD's status
153 * http://www.musicpd.org/doc/protocol/ch03.html#idp118752
154 */
155
156 // clearerror
157 Mpc.prototype.clearerror = Mpc.__make_send_void('clearerror');
158 // currentsong
159 Mpc.prototype.currentsong = Mpc.__make_send_void('currentsong');
160 // idle [SUBSYSTEMS...]
161 // TODO
162 // status
163 Mpc.prototype.status = Mpc.__make_send_void('status');
164 // stats
165 Mpc.prototype.stats = Mpc.__make_send_void('stats');
166
167 /*
168 * Playback options
169 * http://www.musicpd.org/doc/protocol/ch03s02.html
170 */
171
172 // consume {STATE}
173 Mpc.prototype.consume = Mpc.__make_send_range('consume', 0, 1, 1);
174 // crossfade {SECONDS}
175 Mpc.prototype.crossfade = Mpc.__make_send_arg1('crossfade');
176 // mixrampdb {deciBels}
177 Mpc.prototype.mixrampdb = Mpc.__make_send_arg1('mixrampdb');
178 // mixrampdelay {SECONDS|nan}
179 // Note: Probably should handle javascript NaN here.
180 Mpc.prototype.mixrampdelay = Mpc.__make_send_arg1('mixrampdelay');
181 // random {STATE}
182 Mpc.prototype.random = Mpc.__make_send_range('random', 0, 1, 1);
183 // repeat {STATE}
184 Mpc.prototype.repeat = Mpc.__make_send_range('repeat', 0, 1, 1);
185 // setvol {VOL}
186 Mpc.prototype.setvol = Mpc.__make_send_range('setvol', 0, 100);
187 // single {STATE}
188 Mpc.prototype.single = Mpc.__make_send_range('single', 0, 1, 1);
189 // replay_gain_mode {MODE}
190 Mpc.prototype.replay_gain_mode = Mpc.__make_send_arg1('replay_gain_mode');
191 // replay_gain_status
192
193 /*
194 * Controlling playback
195 * http://www.musicpd.org/doc/protocol/ch03s03.html
196 */
197
198 // next
199 Mpc.prototype.next = Mpc.__make_send_void('next');
200 // pause {PAUSE}
201 Mpc.prototype.pause = Mpc.__make_send_range('pause', 0, 1, 1);
202 // play [SONGPOS]
203 Mpc.prototype.play = Mpc.__make_send_opt('play');
204 // playid [SONGID]
205 Mpc.prototype.playid = Mpc.__make_send_opt('playid');
206 // previous
207 Mpc.prototype.previous = Mpc.__make_send_void('previous');
208 // seek {SONGPOS} {TIME}
209 Mpc.prototype.seek = Mpc.__make_send_arg2('seek');
210 // seekid {SONGID} {TIME}
211 Mpc.prototype.seekid = Mpc.__make_send_arg2('seekid');
212 // seekcur {TIME}
213 Mpc.prototype.seekcur = Mpc.__make_send_arg1('seek');
214 // stop
215 Mpc.prototype.stop = Mpc.__make_send_void('stop');
216
217 /*
218 * The current playlist
219 * http://www.musicpd.org/doc/protocol/ch03s04.html
220 */
221
222 // add {URI}
223 Mpc.prototype.add = Mpc.__make_send_arg1('add');
224 // addid {URI} [POSITION]
225 // TODO: handle position
226 Mpc.prototype.addid = Mpc.__make_send_arg1('addid');
227 // clear
228 Mpc.prototype.clear = Mpc.__make_send_void('clear');
229 // delete [{POS} | {START:END}]
230 Mpc.prototype.delete = Mpc.__make_send_arg1('delete');
231 // deleteid {SONGID}
232 Mpc.prototype.deleteid = Mpc.__make_send_arg1('deleteid');
233 // move [{FROM} | {START:END}] {TO}
234 Mpc.prototype.move = Mpc.__make_send_arg2('move');
235 // moveid {FROM} {TO}
236 Mpc.prototype.moveid = Mpc.__make_send_arg2('moveid');
237 // playlist
238 Mpc.prototype.playlist = Mpc.__make_send_void('playlist');
239 // playlistfind {TAG} {NEEDLE}
240 Mpc.prototype.playlistfind = Mpc.__make_send_arg2('playlistfind');
241 // playlistid {SONGID}
242 Mpc.prototype.playlistid = Mpc.__make_send_arg1('playlistid');
243 // playlistinfo [[SONGPOS] | [START:END]]
244 Mpc.prototype.playlistinfo = Mpc.__make_send_opt('playlistinfo');
245 // playlistsearch {TAG} {NEEDLE}
246 Mpc.prototype.playlistsearch = Mpc.__make_send_arg2('playlistsearch');
247 // plchanges {VERSION}
248 Mpc.prototype.plchanges = Mpc.__make_send_arg1('plchanges');
249 // plchangesposid {VERSION}
250 Mpc.prototype.plchangesposid = Mpc.__make_send_arg1('plchangesposid');
251 // prio {PRIORITY} {START:END...}
252 Mpc.prototype.prio = Mpc.__make_send_arg2('prio');
253 // prioid {PRIORITY} {ID...}
254 Mpc.prototype.prioid = Mpc.__make_send_arg2('prioid');
255 // shuffle [START:END]
256 Mpc.prototype.shuffle = Mpc.__make_send_opt('shuffle');
257 // swap {SONG1} {SONG2}
258 Mpc.prototype.swap = Mpc.__make_send_arg2('swap');
259 // swapid {SONG1} {SONG2}
260 Mpc.prototype.swapid = Mpc.__make_send_arg2('swapid');
261
262 /*
263 * Stored playlists
264 * http://www.musicpd.org/doc/protocol/ch03s05.html
265 */
266
267 // listplaylist {NAME}
268 Mpc.prototype.listplaylist = Mpc.__make_send_arg1('listplaylist');
269 // listplaylistinfo {NAME}
270 Mpc.prototype.listplaylistinfo = Mpc.__make_send_arg1('listplaylistinfo');
271 // listplaylists
272 Mpc.prototype.listplaylists = Mpc.__make_send_void('listplaylists');
273 // load {NAME} [START:END]
274 // TODO: handle optional start:end
275 Mpc.prototype.load = Mpc.__make_send_arg1('load');
276 // playlistadd {NAME} {URI}
277 Mpc.prototype.playlistadd = Mpc.__make_send_arg2('playlistadd');
278 // playlistclear {NAME}
279 Mpc.prototype.playlistclear = Mpc.__make_send_arg1('playlistclear');
280 // playlistdelete {NAME} {SONGPOS}
281 Mpc.prototype.playlistdelete = Mpc.__make_send_arg2('playlistdelete');
282 // playlistmove {NAME} {SONGID} {SONGPOS}
283 Mpc.prototype.playlistmove = Mpc.__make_send_arg3('playlistmove');
284 // rename {NAME} {NEW_NAME}
285 Mpc.prototype.rename = Mpc.__make_send_arg2('rename');
286 // rm {NAME}
287 Mpc.prototype.rm = Mpc.__make_send_arg1('rm');
288 // save {NAME}
289 Mpc.prototype.save = Mpc.__make_send_arg1('save');
290
291 /*
292 * The music database
293 * http://www.musicpd.org/doc/protocol/ch03s06.html
294 */
295
296 /*
297 * Connection settings
298 * http://www.musicpd.org/doc/protocol/ch03s08.html
299 */
300
301 // close
302 Mpc.prototype.close = Mpc.__make_send_void('close');
303 // kill
304 Mpc.prototype.kill = Mpc.__make_send_void('kill');
305 // password {PASSWORD}
306 Mpc.prototype.password = Mpc.__make_send_arg1('password');
307 // ping
308 Mpc.prototype.ping = Mpc.__make_send_void('ping');
309
310 /*
311 * Audio output devices
312 * http://www.musicpd.org/doc/protocol/ch03s09.html
313 */
314
315 // disableoutput {ID}
316 Mpc.prototype.disableoutput = Mpc.__make_send_arg1('disableoutput');
317 // enableoutput {ID}
318 Mpc.prototype.enableoutput = Mpc.__make_send_arg1('enableoutput');
319 // outputs
320 Mpc.prototype.outputs = Mpc.__make_send_void('outputs');
321
322 /*
323 * Reflection
324 * http://www.musicpd.org/doc/protocol/ch03s10.html
325 */
326
327 // config
328 Mpc.prototype.config = Mpc.__make_send_void('config');
329 // commands
330 Mpc.prototype.commands = Mpc.__make_send_void('commands');
331 // notcommands
332 Mpc.prototype.notcommands = Mpc.__make_send_void('notcommands');
333 // tagtypes
334 Mpc.prototype.tagtypes = Mpc.__make_send_void('tagtypes');
335 // urlhandlers
336 Mpc.prototype.urlhandlers = Mpc.__make_send_void('urlhandlers');
337 // decoders
338 Mpc.prototype.decoders = Mpc.__make_send_void('decoders');
339
340 /*
341 * Client to client
342 * http://www.musicpd.org/doc/protocol/ch03s11.html
343 */
344
345 // subscribe {NAME}
346 Mpc.prototype.subscribe = Mpc.__make_send_arg1('subscribe');
347 // unsubscribe {NAME}
348 Mpc.prototype.unsubscribe = Mpc.__make_send_arg1('unsubscribe');
349 // channels
350 Mpc.prototype.channels = Mpc.__make_send_void('channels');
351 // readmessages
352 Mpc.prototype.readmessages = Mpc.__make_send_void('readmessages');
353 // sendmessage {CHANNEL} {TEXT}
354 Mpc.prototype.sendmessage = Mpc.__make_send_arg2('sendmessage');