]>
git.wh0rd.org - chrome-ext/music-player-client.git/blob - js/mpc.js
1 // Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
3 function Mpc(socket
, cb_update_state
) {
5 this._cb_update_state
= cb_update_state
;
6 this._queue
= ['init'];
10 Mpc
.log = function(msg
, obj
) {
11 console
.log('mpc: ' + msg
, obj
);
14 Mpc
.prototype.send = function(msg
) {
15 this._queue
.push(msg
);
16 this._socket
.send(msg
, function(x
) {
17 Mpc
.log('send: ' + msg
+ ':', x
);
21 Mpc
.prototype.recv_msg = function(lines
) {
22 curr
= this._queue
.shift();
23 Mpc
.log('recv: [' + curr
+ ']:', lines
.join('\n'));
24 curr
= curr
.split(' ');
27 // Needs to return a list of dicts (see above for dicts).
28 //case 'playlistinfo':
33 lines
.forEach(function(line
) {
34 i
= line
.indexOf(':');
36 return; // Ignores the OK line
37 key
= line
.substr(0, i
);
38 val
= line
.substr(i
+ 2);
42 this._cb_update_state(state
);
45 this._cb_update_state(lines
, curr
);
50 Mpc
.prototype.recv = function(msg
) {
51 /* We can get back a bunch of responses in a row, so parse them out */
52 /* XXX: Do we have to handle partial reads ? like long playlists ... */
53 lines
= msg
.split('\n');
55 while (i
< lines
.length
) {
56 if (lines
[i
] == 'OK' || lines
[i
].substr(0, 3) == 'OK ') {
57 this.recv_msg(lines
.splice(0, i
+ 1));
65 * Command generator helpers.
68 Mpc
.__make_send_void = function(cmd
) {
69 return function() { this.send(cmd
); }
72 Mpc
.__make_send_arg1 = function(cmd
) {
75 Mpc
.log(cmd
+ ': function requires one argument');
77 this.send(cmd
+ ' ' + a1
);
81 Mpc
.__make_send_arg2 = function(cmd
) {
82 return function(a1
, a2
) {
83 if (a1
=== undefined || a2
=== undefined)
84 Mpc
.log(cmd
+ ': function requires two arguments');
86 this.send(cmd
+ ' ' + a1
+ ' ' + a2
);
90 Mpc
.__make_send_opt = function(cmd
) {
91 return function(arg
) {
92 if (arg
=== undefined)
94 this.send(cmd
+ ' ' + arg
);
98 Mpc
.__make_send_range = function(cmd
, min
, max
, def
) {
99 return function(arg
) {
100 if (arg
=== undefined)
102 if (arg
>= min
&& arg
<= max
)
103 this.send(cmd
+ ' ' + arg
);
105 Mpc
.log(cmd
+ ': arg must be [' + min
+ ',' + max
+ '] but got "' + arg
+ '"');
110 * Querying MPD's status
111 * http://www.musicpd.org/doc/protocol/ch03.html#idp118752
115 Mpc
.prototype.clearerror
= Mpc
.__make_send_void('clearerror');
117 Mpc
.prototype.currentsong
= Mpc
.__make_send_void('currentsong');
118 // idle [SUBSYSTEMS...]
121 Mpc
.prototype.status
= Mpc
.__make_send_void('status');
123 Mpc
.prototype.stats
= Mpc
.__make_send_void('stats');
127 * http://www.musicpd.org/doc/protocol/ch03s02.html
131 Mpc
.prototype.consume
= Mpc
.__make_send_range('consume', 0, 1, 1);
132 // crossfade {SECONDS}
133 Mpc
.prototype.consume
= Mpc
.__make_send_arg1('crossfade');
134 // mixrampdb {deciBels}
135 Mpc
.prototype.mixrampdb
= Mpc
.__make_send_arg1('mixrampdb');
136 // mixrampdelay {SECONDS|nan}
137 // Note: Probably should handle javascript NaN here.
138 Mpc
.prototype.mixrampdelay
= Mpc
.__make_send_arg1('mixrampdelay');
140 Mpc
.prototype.random
= Mpc
.__make_send_range('random', 0, 1, 1);
142 Mpc
.prototype.repeat
= Mpc
.__make_send_range('repeat', 0, 1, 1);
144 Mpc
.prototype.setvol
= Mpc
.__make_send_range('setvol', 0, 100);
146 Mpc
.prototype.single
= Mpc
.__make_send_range('single', 0, 1, 1);
147 // replay_gain_mode {MODE}
148 Mpc
.prototype.replay_gain_mode
= Mpc
.__make_send_arg1('replay_gain_mode');
149 // replay_gain_status
152 * Controlling playback
153 * http://www.musicpd.org/doc/protocol/ch03s03.html
157 Mpc
.prototype.next
= Mpc
.__make_send_void('next');
159 Mpc
.prototype.pause
= Mpc
.__make_send_range('pause', 0, 1, 1);
161 Mpc
.prototype.play
= Mpc
.__make_send_opt('play');
163 Mpc
.prototype.playid
= Mpc
.__make_send_opt('playid');
165 Mpc
.prototype.previous
= Mpc
.__make_send_void('previous');
166 // seek {SONGPOS} {TIME}
167 Mpc
.prototype.seek
= Mpc
.__make_send_arg2('seek');
168 // seekid {SONGID} {TIME}
169 Mpc
.prototype.seekid
= Mpc
.__make_send_arg2('seekid');
171 Mpc
.prototype.seekcur
= Mpc
.__make_send_arg1('seek');
173 Mpc
.prototype.stop
= Mpc
.__make_send_void('stop');
176 * Connection settings
177 * http://www.musicpd.org/doc/protocol/ch03s08.html
181 Mpc
.prototype.close
= Mpc
.__make_send_void('close');
183 Mpc
.prototype.kill
= Mpc
.__make_send_void('kill');
184 // password {PASSWORD}
185 Mpc
.prototype.password
= Mpc
.__make_send_arg1('password');
187 Mpc
.prototype.ping
= Mpc
.__make_send_void('ping');