]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - js/mpc.js
dfcd5fd304278d8d520e637ad98f560dd06c25a1
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
3 function Mpc(socket
, cb_update_state
, debug_enabled
) {
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;
13 Mpc
.prototype.log = function(lvl
, msg
, obj
) {
14 if (this._debug_enabled
& lvl
)
15 console
.log('mpc: ' + msg
, obj
);
18 Mpc
.prototype.err = function(msg
, obj
) {
19 console
.error('mpc: ' + msg
, obj
);
22 Mpc
.prototype.set_debug = function(val
) {
23 this._debug_enabled
= val
;
26 Mpc
.prototype.send = function(msg
) {
28 this._queue
.push(msg
);
29 this._socket
.send(msg
, function(x
) {
30 _this
.log(0x1, 'send: ' + msg
+ ':', x
);
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(' ');
40 // Needs to return a list of dicts (see above for dicts).
41 //case 'playlistinfo':
47 lines
.forEach(function(line
) {
48 i
= line
.indexOf(':');
50 return; // Ignores the OK line
51 key
= line
.substr(0, i
);
53 val
= line
.substr(i
+ 2);
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);
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
];
69 this._cb_update_state(curr_state
);
72 this._cb_update_state(lines
, curr
);
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'));
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));
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
;
94 this._recv_buffer_last
= lines
.length
;
98 * Command generator helpers.
101 Mpc
.__make_send_void = function(cmd
) {
102 return function() { this.send(cmd
); }
105 Mpc
.__make_send_arg1 = function(cmd
) {
106 return function(a1
) {
107 if (a1
=== undefined)
108 this.err(cmd
+ ': function requires one argument');
110 this.send(cmd
+ ' ' + a1
);
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');
119 this.send(cmd
+ ' ' + a1
+ ' ' + a2
);
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');
128 this.send(cmd
+ ' ' + a1
+ ' ' + a2
+ ' ' + a3
);
132 Mpc
.__make_send_opt = function(cmd
) {
133 return function(arg
) {
134 if (arg
=== undefined)
136 this.send(cmd
+ ' ' + arg
);
140 Mpc
.__make_send_range = function(cmd
, min
, max
, def
) {
141 return function(arg
) {
142 if (arg
=== undefined)
144 if (arg
>= min
&& arg
<= max
)
145 this.send(cmd
+ ' ' + arg
);
147 this.err(cmd
+ ': arg must be [' + min
+ ',' + max
+ '] but got "' + arg
+ '"');
152 * Querying MPD's status
153 * http://www.musicpd.org/doc/protocol/ch03.html#idp118752
157 Mpc
.prototype.clearerror
= Mpc
.__make_send_void('clearerror');
159 Mpc
.prototype.currentsong
= Mpc
.__make_send_void('currentsong');
160 // idle [SUBSYSTEMS...]
163 Mpc
.prototype.status
= Mpc
.__make_send_void('status');
165 Mpc
.prototype.stats
= Mpc
.__make_send_void('stats');
169 * http://www.musicpd.org/doc/protocol/ch03s02.html
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');
182 Mpc
.prototype.random
= Mpc
.__make_send_range('random', 0, 1, 1);
184 Mpc
.prototype.repeat
= Mpc
.__make_send_range('repeat', 0, 1, 1);
186 Mpc
.prototype.setvol
= Mpc
.__make_send_range('setvol', 0, 100);
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
194 * Controlling playback
195 * http://www.musicpd.org/doc/protocol/ch03s03.html
199 Mpc
.prototype.next
= Mpc
.__make_send_void('next');
201 Mpc
.prototype.pause
= Mpc
.__make_send_range('pause', 0, 1, 1);
203 Mpc
.prototype.play
= Mpc
.__make_send_opt('play');
205 Mpc
.prototype.playid
= Mpc
.__make_send_opt('playid');
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');
213 Mpc
.prototype.seekcur
= Mpc
.__make_send_arg1('seek');
215 Mpc
.prototype.stop
= Mpc
.__make_send_void('stop');
218 * The current playlist
219 * http://www.musicpd.org/doc/protocol/ch03s04.html
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');
228 Mpc
.prototype.clear
= Mpc
.__make_send_void('clear');
229 // delete [{POS} | {START:END}]
230 Mpc
.prototype.delete = Mpc
.__make_send_arg1('delete');
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');
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');
264 * http://www.musicpd.org/doc/protocol/ch03s05.html
267 // listplaylist {NAME}
268 Mpc
.prototype.listplaylist
= Mpc
.__make_send_arg1('listplaylist');
269 // listplaylistinfo {NAME}
270 Mpc
.prototype.listplaylistinfo
= Mpc
.__make_send_arg1('listplaylistinfo');
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');
287 Mpc
.prototype.rm
= Mpc
.__make_send_arg1('rm');
289 Mpc
.prototype.save
= Mpc
.__make_send_arg1('save');
293 * http://www.musicpd.org/doc/protocol/ch03s06.html
297 * Connection settings
298 * http://www.musicpd.org/doc/protocol/ch03s08.html
302 Mpc
.prototype.close
= Mpc
.__make_send_void('close');
304 Mpc
.prototype.kill
= Mpc
.__make_send_void('kill');
305 // password {PASSWORD}
306 Mpc
.prototype.password
= Mpc
.__make_send_arg1('password');
308 Mpc
.prototype.ping
= Mpc
.__make_send_void('ping');
311 * Audio output devices
312 * http://www.musicpd.org/doc/protocol/ch03s09.html
315 // disableoutput {ID}
316 Mpc
.prototype.disableoutput
= Mpc
.__make_send_arg1('disableoutput');
318 Mpc
.prototype.enableoutput
= Mpc
.__make_send_arg1('enableoutput');
320 Mpc
.prototype.outputs
= Mpc
.__make_send_void('outputs');
324 * http://www.musicpd.org/doc/protocol/ch03s10.html
328 Mpc
.prototype.config
= Mpc
.__make_send_void('config');
330 Mpc
.prototype.commands
= Mpc
.__make_send_void('commands');
332 Mpc
.prototype.notcommands
= Mpc
.__make_send_void('notcommands');
334 Mpc
.prototype.tagtypes
= Mpc
.__make_send_void('tagtypes');
336 Mpc
.prototype.urlhandlers
= Mpc
.__make_send_void('urlhandlers');
338 Mpc
.prototype.decoders
= Mpc
.__make_send_void('decoders');
342 * http://www.musicpd.org/doc/protocol/ch03s11.html
346 Mpc
.prototype.subscribe
= Mpc
.__make_send_arg1('subscribe');
347 // unsubscribe {NAME}
348 Mpc
.prototype.unsubscribe
= Mpc
.__make_send_arg1('unsubscribe');
350 Mpc
.prototype.channels
= Mpc
.__make_send_void('channels');
352 Mpc
.prototype.readmessages
= Mpc
.__make_send_void('readmessages');
353 // sendmessage {CHANNEL} {TEXT}
354 Mpc
.prototype.sendmessage
= Mpc
.__make_send_arg2('sendmessage');