]> git.wh0rd.org Git - chrome-ext/music-player-client.git/blob - js/mpc.js
mpc: split up log levels
[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.state = {};
9 }
10
11 Mpc.prototype.log = function(lvl, msg, obj) {
12         if (this._debug_enabled & lvl)
13                 console.log('mpc: ' + msg, obj);
14 }
15
16 Mpc.prototype.err = function(msg, obj) {
17         console.error('mpc: ' + msg, obj);
18 }
19
20 Mpc.prototype.set_debug = function(val) {
21         this._debug_enabled = val;
22 }
23
24 Mpc.prototype.send = function(msg) {
25         var _this = this;
26         this._queue.push(msg);
27         this._socket.send(msg, function(x) {
28                 _this.log(0x1, 'send: ' + msg + ':', x);
29         });
30 }
31
32 Mpc.prototype.recv_msg = function(lines) {
33         curr = this._queue.shift();
34         this.log(0x2, 'recv: [' + curr + ']:', lines.join('\n'));
35         curr = curr.split(' ');
36
37         switch (curr[0]) {
38         // Needs to return a list of dicts (see above for dicts).
39         //case 'playlistinfo':
40         case 'currentsong':
41         case 'stats':
42         case 'status':
43                 state = {};
44                 lines.forEach(function(line) {
45                         i = line.indexOf(':');
46                         if (i == -1)
47                                 return; // Ignores the OK line
48                         key = line.substr(0, i);
49                         val = line.substr(i + 2);
50                         state[key] = val;
51                 });
52                 this.state = state;
53                 this._cb_update_state(state);
54                 break;
55         default:
56                 this._cb_update_state(lines, curr);
57                 break;
58         }
59 }
60
61 Mpc.prototype.recv = function(msg) {
62         /* We can get back a bunch of responses in a row, so parse them out */
63         /* XXX: Do we have to handle partial reads ?  like long playlists ... */
64         lines = msg.split('\n');
65         var i = 0;
66         while (i < lines.length) {
67                 if (lines[i] == 'OK' || lines[i].substr(0, 3) == 'OK ') {
68                         this.recv_msg(lines.splice(0, i + 1));
69                         i = 0;
70                 } else
71                         ++i;
72         }
73 }
74
75 /*
76  * Command generator helpers.
77  */
78
79 Mpc.__make_send_void = function(cmd) {
80         return function() { this.send(cmd); }
81 }
82
83 Mpc.__make_send_arg1 = function(cmd) {
84         return function(a1) {
85                 if (a1 === undefined)
86                         this.err(cmd + ': function requires one argument');
87                 else
88                         this.send(cmd + ' ' + a1);
89         }
90 }
91
92 Mpc.__make_send_arg2 = function(cmd) {
93         return function(a1, a2) {
94                 if (a1 === undefined || a2 === undefined)
95                         this.err(cmd + ': function requires two arguments');
96                 else
97                         this.send(cmd + ' ' + a1 + ' ' + a2);
98         }
99 }
100
101 Mpc.__make_send_opt = function(cmd) {
102         return function(arg) {
103                 if (arg === undefined)
104                         arg = '';
105                 this.send(cmd + ' ' + arg);
106         };
107 }
108
109 Mpc.__make_send_range = function(cmd, min, max, def) {
110         return function(arg) {
111                 if (arg === undefined)
112                         arg = def;
113                 if (arg >= min && arg <= max)
114                         this.send(cmd + ' ' + arg);
115                 else
116                         this.err(cmd + ': arg must be [' + min + ',' + max + '] but got "' + arg + '"');
117         };
118 }
119
120 /*
121  * Querying MPD's status
122  * http://www.musicpd.org/doc/protocol/ch03.html#idp118752
123  */
124
125 // clearerror
126 Mpc.prototype.clearerror          = Mpc.__make_send_void('clearerror');
127 // currentsong
128 Mpc.prototype.currentsong         = Mpc.__make_send_void('currentsong');
129 // idle [SUBSYSTEMS...]
130 // TODO
131 // status
132 Mpc.prototype.status              = Mpc.__make_send_void('status');
133 // stats
134 Mpc.prototype.stats               = Mpc.__make_send_void('stats');
135
136 /*
137  * Playback options
138  * http://www.musicpd.org/doc/protocol/ch03s02.html
139  */
140
141 // consume {STATE}
142 Mpc.prototype.consume             = Mpc.__make_send_range('consume', 0, 1, 1);
143 // crossfade {SECONDS}
144 Mpc.prototype.crossfade           = Mpc.__make_send_arg1('crossfade');
145 // mixrampdb {deciBels}
146 Mpc.prototype.mixrampdb           = Mpc.__make_send_arg1('mixrampdb');
147 // mixrampdelay {SECONDS|nan}
148 // Note: Probably should handle javascript NaN here.
149 Mpc.prototype.mixrampdelay        = Mpc.__make_send_arg1('mixrampdelay');
150 // random {STATE}
151 Mpc.prototype.random              = Mpc.__make_send_range('random', 0, 1, 1);
152 // repeat {STATE}
153 Mpc.prototype.repeat              = Mpc.__make_send_range('repeat', 0, 1, 1);
154 // setvol {VOL}
155 Mpc.prototype.setvol              = Mpc.__make_send_range('setvol', 0, 100);
156 // single {STATE}
157 Mpc.prototype.single              = Mpc.__make_send_range('single', 0, 1, 1);
158 // replay_gain_mode {MODE}
159 Mpc.prototype.replay_gain_mode    = Mpc.__make_send_arg1('replay_gain_mode');
160 // replay_gain_status
161
162 /*
163  * Controlling playback
164  * http://www.musicpd.org/doc/protocol/ch03s03.html
165  */
166
167 // next
168 Mpc.prototype.next                = Mpc.__make_send_void('next');
169 // pause {PAUSE}
170 Mpc.prototype.pause               = Mpc.__make_send_range('pause', 0, 1, 1);
171 // play [SONGPOS]
172 Mpc.prototype.play                = Mpc.__make_send_opt('play');
173 // playid [SONGID]
174 Mpc.prototype.playid              = Mpc.__make_send_opt('playid');
175 // previous
176 Mpc.prototype.previous            = Mpc.__make_send_void('previous');
177 // seek {SONGPOS} {TIME}
178 Mpc.prototype.seek                = Mpc.__make_send_arg2('seek');
179 // seekid {SONGID} {TIME}
180 Mpc.prototype.seekid              = Mpc.__make_send_arg2('seekid');
181 // seekcur {TIME}
182 Mpc.prototype.seekcur             = Mpc.__make_send_arg1('seek');
183 // stop
184 Mpc.prototype.stop                = Mpc.__make_send_void('stop');
185
186 /*
187  * Connection settings
188  * http://www.musicpd.org/doc/protocol/ch03s08.html
189  */
190
191 // close
192 Mpc.prototype.close               = Mpc.__make_send_void('close');
193 // kill
194 Mpc.prototype.kill                = Mpc.__make_send_void('kill');
195 // password {PASSWORD}
196 Mpc.prototype.password            = Mpc.__make_send_arg1('password');
197 // ping
198 Mpc.prototype.ping                = Mpc.__make_send_void('ping');
199
200 /*
201  * Audio output devices
202  * http://www.musicpd.org/doc/protocol/ch03s09.html
203  */
204
205 // disableoutput {ID}
206 Mpc.prototype.disableoutput       = Mpc.__make_send_arg1('disableoutput');
207 // enableoutput {ID}
208 Mpc.prototype.enableoutput        = Mpc.__make_send_arg1('enableoutput');
209 // outputs
210 Mpc.prototype.outputs             = Mpc.__make_send_void('outputs');
211
212 /*
213  * Reflection
214  * http://www.musicpd.org/doc/protocol/ch03s10.html
215  */
216
217 // config
218 Mpc.prototype.config              = Mpc.__make_send_void('config');
219 // commands
220 Mpc.prototype.commands            = Mpc.__make_send_void('commands');
221 // notcommands
222 Mpc.prototype.notcommands         = Mpc.__make_send_void('notcommands');
223 // tagtypes
224 Mpc.prototype.tagtypes            = Mpc.__make_send_void('tagtypes');
225 // urlhandlers
226 Mpc.prototype.urlhandlers         = Mpc.__make_send_void('urlhandlers');
227 // decoders
228 Mpc.prototype.decoders            = Mpc.__make_send_void('decoders');
229
230 /*
231  * Client to client
232  * http://www.musicpd.org/doc/protocol/ch03s11.html
233  */
234
235 // subscribe {NAME}
236 Mpc.prototype.subscribe           = Mpc.__make_send_arg1('subscribe');
237 // unsubscribe {NAME}
238 Mpc.prototype.unsubscribe         = Mpc.__make_send_arg1('unsubscribe');
239 // channels
240 Mpc.prototype.channels            = Mpc.__make_send_void('channels');
241 // readmessages
242 Mpc.prototype.readmessages        = Mpc.__make_send_void('readmessages');
243 // sendmessage {CHANNEL} {TEXT}
244 Mpc.prototype.sendmessage         = Mpc.__make_send_arg2('sendmessage');