X-Git-Url: https://git.wh0rd.org/?p=chrome-ext%2Fmusic-player-client.git;a=blobdiff_plain;f=js%2Fmpc.js;h=37db8cdcc391388c1fc2e0fe9bb0fe4399f38abb;hp=c12a7482afbd85556a82d650ad29c5179ece1168;hb=0a3a435e6ad8fcd1232675e09a07baa73be09497;hpb=024ea28421ec5d9c88f80ac11403da6c43a40e6a diff --git a/js/mpc.js b/js/mpc.js index c12a748..37db8cd 100644 --- a/js/mpc.js +++ b/js/mpc.js @@ -8,11 +8,15 @@ function Mpc(socket, cb_update_state, debug_enabled) { this.state = {}; } -Mpc.prototype.log = function(msg, obj) { - if (this._debug_enabled) +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; } @@ -21,13 +25,13 @@ Mpc.prototype.send = function(msg) { var _this = this; this._queue.push(msg); this._socket.send(msg, function(x) { - _this.log('send: ' + msg + ':', x); + _this.log(0x1, 'send: ' + msg + ':', x); }); } Mpc.prototype.recv_msg = function(lines) { curr = this._queue.shift(); - this.log('recv: [' + curr + ']:', lines.join('\n')); + this.log(0x2, 'recv: [' + curr + ']:', lines.join('\n')); curr = curr.split(' '); switch (curr[0]) { @@ -37,16 +41,30 @@ Mpc.prototype.recv_msg = function(lines) { case 'stats': case 'status': state = {}; + keys = []; lines.forEach(function(line) { i = line.indexOf(':'); if (i == -1) return; // Ignores the OK line key = line.substr(0, i); + keys.push(key); val = line.substr(i + 2); state[key] = val; }); - this.state = state; - this._cb_update_state(state); + + // When mpd is stopped, it gives us back crap values for some things. + if ('state' in state && state.state == 'stop') { + if ('volume' in state && state.volume == '-1') + keys.splice(keys.indexOf('volume'), 1); + } + // Now merge the current state with the previous one so that we don't + // lose information like volume or song position. + curr_state = this.state; + keys.forEach(function(key) { + curr_state[key] = state[key]; + }); + + this._cb_update_state(curr_state); break; default: this._cb_update_state(lines, curr); @@ -79,7 +97,7 @@ Mpc.__make_send_void = function(cmd) { Mpc.__make_send_arg1 = function(cmd) { return function(a1) { if (a1 === undefined) - this.log(cmd + ': function requires one argument'); + this.err(cmd + ': function requires one argument'); else this.send(cmd + ' ' + a1); } @@ -88,7 +106,7 @@ Mpc.__make_send_arg1 = function(cmd) { Mpc.__make_send_arg2 = function(cmd) { return function(a1, a2) { if (a1 === undefined || a2 === undefined) - this.log(cmd + ': function requires two arguments'); + this.err(cmd + ': function requires two arguments'); else this.send(cmd + ' ' + a1 + ' ' + a2); } @@ -109,7 +127,7 @@ Mpc.__make_send_range = function(cmd, min, max, def) { if (arg >= min && arg <= max) this.send(cmd + ' ' + arg); else - this.log(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"'); + this.err(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"'); }; }