X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fmusic-player-client.git;a=blobdiff_plain;f=js%2Fmpc.js;h=e005eaf42557c9f8b28ddbf4eca5f29f56f87ad1;hp=bf2d9697d3e639521efa131fb1667e0ce45ee67f;hb=e4657cab6d590463265d26d2d4dac639c4e76527;hpb=50dafc4c60cfd4183bdedcd86fcd9b6b932c6a0b diff --git a/js/mpc.js b/js/mpc.js index bf2d969..e005eaf 100644 --- a/js/mpc.js +++ b/js/mpc.js @@ -1,26 +1,37 @@ // Written by Mike Frysinger . Released into the public domain. Suck it. -function Mpc(socket, cb_update_state) { +function Mpc(socket, cb_update_state, debug_enabled) { this._socket = socket; this._cb_update_state = cb_update_state; + this._debug_enabled = debug_enabled; this._queue = ['init']; this.state = {}; } -Mpc.log = function(msg, obj) { - console.log('mpc: ' + msg, obj); +Mpc.prototype.log = function(lvl, msg, obj) { + if (this._debug_enabled & lvl) + console.log('mpc: ' + msg, obj); +} + +Mpc.prototype.err = function(msg, obj) { + console.error('mpc: ' + msg, obj); +} + +Mpc.prototype.set_debug = function(val) { + this._debug_enabled = val; } Mpc.prototype.send = function(msg) { + var _this = this; this._queue.push(msg); this._socket.send(msg, function(x) { - Mpc.log('send: ' + msg + ':', x); + _this.log(0x1, 'send: ' + msg + ':', x); }); } Mpc.prototype.recv_msg = function(lines) { curr = this._queue.shift(); - Mpc.log('recv: [' + curr + ']:', lines.join('\n')); + this.log(0x2, 'recv: [' + curr + ']:', lines.join('\n')); curr = curr.split(' '); switch (curr[0]) { @@ -72,7 +83,7 @@ Mpc.__make_send_void = function(cmd) { Mpc.__make_send_arg1 = function(cmd) { return function(a1) { if (a1 === undefined) - Mpc.log(cmd + ': function requires one argument'); + this.err(cmd + ': function requires one argument'); else this.send(cmd + ' ' + a1); } @@ -81,7 +92,7 @@ Mpc.__make_send_arg1 = function(cmd) { Mpc.__make_send_arg2 = function(cmd) { return function(a1, a2) { if (a1 === undefined || a2 === undefined) - Mpc.log(cmd + ': function requires two arguments'); + this.err(cmd + ': function requires two arguments'); else this.send(cmd + ' ' + a1 + ' ' + a2); } @@ -102,7 +113,7 @@ Mpc.__make_send_range = function(cmd, min, max, def) { if (arg >= min && arg <= max) this.send(cmd + ' ' + arg); else - Mpc.log(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"'); + this.err(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"'); }; } @@ -130,7 +141,7 @@ Mpc.prototype.stats = Mpc.__make_send_void('stats'); // consume {STATE} Mpc.prototype.consume = Mpc.__make_send_range('consume', 0, 1, 1); // crossfade {SECONDS} -Mpc.prototype.consume = Mpc.__make_send_arg1('crossfade'); +Mpc.prototype.crossfade = Mpc.__make_send_arg1('crossfade'); // mixrampdb {deciBels} Mpc.prototype.mixrampdb = Mpc.__make_send_arg1('mixrampdb'); // mixrampdelay {SECONDS|nan} @@ -185,3 +196,49 @@ Mpc.prototype.kill = Mpc.__make_send_void('kill'); Mpc.prototype.password = Mpc.__make_send_arg1('password'); // ping Mpc.prototype.ping = Mpc.__make_send_void('ping'); + +/* + * Audio output devices + * http://www.musicpd.org/doc/protocol/ch03s09.html + */ + +// disableoutput {ID} +Mpc.prototype.disableoutput = Mpc.__make_send_arg1('disableoutput'); +// enableoutput {ID} +Mpc.prototype.enableoutput = Mpc.__make_send_arg1('enableoutput'); +// outputs +Mpc.prototype.outputs = Mpc.__make_send_void('outputs'); + +/* + * Reflection + * http://www.musicpd.org/doc/protocol/ch03s10.html + */ + +// config +Mpc.prototype.config = Mpc.__make_send_void('config'); +// commands +Mpc.prototype.commands = Mpc.__make_send_void('commands'); +// notcommands +Mpc.prototype.notcommands = Mpc.__make_send_void('notcommands'); +// tagtypes +Mpc.prototype.tagtypes = Mpc.__make_send_void('tagtypes'); +// urlhandlers +Mpc.prototype.urlhandlers = Mpc.__make_send_void('urlhandlers'); +// decoders +Mpc.prototype.decoders = Mpc.__make_send_void('decoders'); + +/* + * Client to client + * http://www.musicpd.org/doc/protocol/ch03s11.html + */ + +// subscribe {NAME} +Mpc.prototype.subscribe = Mpc.__make_send_arg1('subscribe'); +// unsubscribe {NAME} +Mpc.prototype.unsubscribe = Mpc.__make_send_arg1('unsubscribe'); +// channels +Mpc.prototype.channels = Mpc.__make_send_void('channels'); +// readmessages +Mpc.prototype.readmessages = Mpc.__make_send_void('readmessages'); +// sendmessage {CHANNEL} {TEXT} +Mpc.prototype.sendmessage = Mpc.__make_send_arg2('sendmessage');