]> git.wh0rd.org - chrome-ext/music-player-client.git/blame - main.js
images: update screenshot
[chrome-ext/music-player-client.git] / main.js
CommitLineData
50dafc4c
MF
1// Written by Mike Frysinger <vapier@gmail.com>. Released into the public domain. Suck it.
2
3/* Globals to allow easy manipulation via javascript console */
4var mpc;
5var tcpclient;
0c661219 6var refresh_id = NaN;
50dafc4c
MF
7
8function TcpClientSender(tcpclient) {
9 this.tcpclient = tcpclient;
10}
11TcpClientSender.prototype.send = function(data, cb) {
12 this.tcpclient.sendMessage(data, cb);
13}
0a3a435e
MF
14TcpClientSender.prototype.poll = function() {
15 this.tcpclient.poll();
16}
8d64e317
MF
17TcpClientSender.prototype.reconnect = function() {
18 this.tcpclient.disconnect();
19 this.tcpclient.connect();
20}
50dafc4c
MF
21
22function tramp_mpc_recv(data) {
23 mpc.recv(data);
24}
25
26function sync_storage(sync) {
27 return sync ? chrome.storage.sync : chrome.storage.local;
28}
29
30window.onload = function() {
31 var local_keys = [
32 'sync',
33 ];
34 var sync_keys = [
0c661219 35 'host', 'port', 'refresh',
50dafc4c
MF
36 ];
37 var options = {
38 'host': '192.168.0.2',
39 'port': 6600,
40 'sync': true,
0c661219 41 'refresh': 5,
50dafc4c
MF
42 };
43
44 chrome.storage.local.get(local_keys, function(settings) {
45 local_keys.forEach(function(key) {
46 if (key in settings)
47 options[key] = settings[key]
48 });
49
50 var storage = sync_storage(options['sync']);
51 storage.get(sync_keys, function(settings) {
52 sync_keys.forEach(function(key) {
53 if (key in settings)
54 options[key] = settings[key];
55 });
56
57 init_ui(local_keys, sync_keys, options);
58 mpc_connect();
59 });
60 });
61};
62
ee26ebe4
MF
63window.onkeypress = function(e) {
64 switch (e.keyCode) {
65 case 49: // 1
66 show_page('controls');
67 break;
68 case 50: // 1
69 show_page('metadata');
70 break;
71 case 51: // 1
72 show_page('playlist');
73 break;
74 case 52: // 1
75 show_page('options');
76 break;
77 }
78};
79
50dafc4c
MF
80function mpc_refresh() {
81 mpc.status();
50dafc4c
MF
82}
83
84function mpc_connect(host, port) {
85 if (typeof(host) != 'string') {
86 host = window['opts_host'].value;
87 port = parseInt(window['opts_port'].value);
88 }
89
90 if (mpc != undefined) {
91 console.log('disconnecting');
92 update_ui('disconnect');
93 delete mpc;
94 tcpclient.disconnect();
95 delete tcpclient;
96 }
97
98 update_ui('init');
99 tcpclient = new TcpClient(host, port);
100 tcpclient.connect(function() {
101 var mpc_sender = new TcpClientSender(tcpclient);
102 tcpclient.addResponseListener(tramp_mpc_recv);
103 mpc = new Mpc(mpc_sender, update_ui);
104 console.log('connected to ' + host + ':' + port);
4ed31ba8
MF
105 console.log('protip: use the "mpc" object to poke mpd directly.\n' +
106 'you can also do mpc.set_debug(3) to see traffic');
50dafc4c 107 mpc_refresh();
0c661219 108 update_refresh_timer();
50dafc4c
MF
109 });
110}
111
112function tramp_mpc_consume() {
113 var val = zo(!getToggleButton(this));
114 mpc.consume(val);
115 setToggleButton(this, val);
116}
9cb957ed 117function tramp_mpc_deleteid() { mpc.deleteid(this.title); }
50dafc4c
MF
118function tramp_mpc_next() { mpc.next(); }
119function tramp_mpc_pause() { mpc.pause(); }
120function tramp_mpc_play() { mpc.play(); }
121function tramp_mpc_previous() { mpc.previous(); }
122function tramp_mpc_random() {
123 var val = zo(!getToggleButton(this));
124 mpc.random(val);
125 setToggleButton(this, val);
126}
127function tramp_mpc_repeat() {
128 var val = zo(!getToggleButton(this));
129 mpc.repeat(val);
130 setToggleButton(this, val);
131}
132function tramp_mpc_seekcur() { mpc.seekcur(this.value); }
133function tramp_mpc_setvol() { mpc.setvol(this.value); }
134function tramp_mpc_single() {
135 var val = zo(!getToggleButton(this));
136 mpc.single(val);
137 setToggleButton(this, val);
138}
139function tramp_mpc_stop() { mpc.stop(); }
140
141function zo(val) {
142 return val ? 1 : 0;
143}
144function szo(val) {
145 return val == '0' ? 0 : 1;
146}
147function getToggleButton(btn) {
148 return btn.style.borderStyle == 'inset';
149}
150function setToggleButton(btn, val) {
151 if (val === undefined)
152 val = !getToggleButton(btn);
153 btn.style.borderStyle = val ? 'inset' : '';
154}
155
156function show_page(page) {
157 if (typeof(page) != 'string')
158 page = this.id.split('.')[1];
159
ea38f3ed
MF
160 switch (page) {
161 case 'playlist':
162 mpc.playlistinfo();
163 // Fallthrough.
164 case 'metadata':
165 mpc.currentsong();
166 break;
167 }
168
50dafc4c
MF
169 var eles = document.getElementsByClassName('main');
170 for (var i = 0; i < eles.length; ++i) {
171 var ele = eles[i];
172 var dis = 'none';
173 var cls = '';
174 if (ele.id == 'main.' + page) {
175 dis = '';
176 cls = 'selected';
177 }
178 ele.style.display = dis;
179 document.getElementById('tab.' + ele.id.split('.')[1]).className = cls;
180 }
181}
182
0c661219
MF
183function do_refresh() {
184 mpc_refresh();
185 refresh_id = window.setTimeout(do_refresh, window['opts_refresh'].value * 1000);
186}
187
188function update_refresh_timer() {
2ff2c6c1 189 if (!isNaN(refresh_id))
0c661219
MF
190 window.clearTimeout(refresh_id);
191 var rate = window['opts_refresh'].value * 1000;
192 if (rate > 0)
193 refresh_id = window.setTimeout(do_refresh, rate);
194}
195
50dafc4c
MF
196function update_local_settings() {
197 var setting = {};
198 setting[this.id] = this.checked;
199 chrome.storage.local.set(setting);
200}
201
202function update_sync_settings() {
203 var setting = {};
204 setting[this.id] = this.value;
205 var storage = sync_storage(window['opts_sync'].checked);
206 storage.set(setting);
0c661219
MF
207
208 switch (this.id) {
209 case 'refresh':
210 update_refresh_timer();
211 break;
212 }
50dafc4c
MF
213}
214
215function init_ui(local_keys, sync_keys, options) {
ee26ebe4
MF
216 var ele, i;
217
50dafc4c 218 /* Setup footer */
ee26ebe4 219 i = 1;
50dafc4c 220 [
ee26ebe4 221 'controls', 'metadata', 'playlist', 'options',
50dafc4c 222 ].forEach(function(id) {
ee26ebe4
MF
223 var ele = document.getElementById('tab.' + id);
224 ele.onclick = show_page;
225 ele.title = id + ' [' + i + ']';
226 ++i;
50dafc4c
MF
227 });
228
229 /* Setup control tab */
230 ui_mpc_status = document.getElementById('status');
50dafc4c
MF
231 [
232 'consume', 'next', 'pause', 'play', 'previous', 'random', 'repeat',
233 'seekcur', 'setvol', 'single', 'stop',
234 ].forEach(function(id) {
235 var ele = window['ui_mpc_' + id] = document.getElementById(id);
ea13f13a 236 ele.onchange = ele.onclick = window['tramp_mpc_' + id];
50dafc4c 237 ele.title = id;
ee26ebe4
MF
238 if (ele.accessKey)
239 ele.title += ' [' + ele.accessKey + ']'
50dafc4c 240 });
ea38f3ed 241 window['ui_mpc_currtime'] = document.getElementById('currtime');
50dafc4c 242
e9aee305
MF
243 /* Setup metadata tab */
244 [
245 'album', 'artist', 'date', 'file', 'title',
246 ].forEach(function(id) {
247 window['ui_mpc_metadata_' + id] = document.getElementById('metadata.' + id);
248 });
249
ea38f3ed
MF
250 /* Setup playlist tab */
251 window['ui_mpc_playlist'] = document.getElementById('playlist');
252
50dafc4c
MF
253 /* Setup options tab */
254 document.getElementById('connect').onclick = mpc_connect;
255 local_keys.forEach(function(id) {
256 var ele = window['opts_' + id] = document.getElementById(id);
257 ele.checked = options[id];
258 ele.onchange = update_local_settings;
259 });
260 sync_keys.forEach(function(id) {
261 var ele = window['opts_' + id] = document.getElementById(id);
262 ele.value = options[id];
263 ele.oninput = update_sync_settings;
264 });
265}
266
ea38f3ed
MF
267function pretty_time(time) {
268 var sec, min, hrs, ret = '';
269 time = parseInt(time);
270 sec = time % 60;
271 min = parseInt((time / 60) % 60);
272 hrs = parseInt((time / 3600) % 3600);
273 if (hrs)
274 ret = hrs + ':' + ("00" + min).substr(-2) + ':';
275 else
276 ret = min + ':';
277 return ret + ("00" + sec).substr(-2);
278}
279
9cb957ed 280function playlist_del() {
17ac93ed 281 mpc.deleteid(this.song_id);
9cb957ed
MF
282 this.parentNode.remove();
283}
284
17ac93ed
MF
285function playlist_play() {
286 mpc.playid(this.song_id);
287 this.parentNode.style.fontWeight = 'bold';
288}
289
50dafc4c
MF
290function update_ui(state, cmd) {
291 if (typeof(state) == 'string') {
292 ui_mpc_status.innerText = ({
293 'disconnect': 'Disconnecting...',
294 'init': 'Connecting...',
295 })[state];
296 return;
297 }
298
299 if (Array.isArray(state)) {
300 /*
301 switch (cmd[0]) {
302 case 'setvol':
303 case 'seekcur':
304 break;
305 default:
306 mpc_refresh();
307 }
308 */
309 return;
310 }
311
ea38f3ed
MF
312 /* Update the metadata tab. */
313 var currentsong = {};
314 if ('Currentsong' in state)
315 currentsong = state.Currentsong;
316 ui_mpc_metadata_album.innerText = currentsong.Album;
317 ui_mpc_metadata_artist.innerText = currentsong.Artist;
318 ui_mpc_metadata_title.innerText = currentsong.Title;
319 ui_mpc_metadata_date.innerText = currentsong.Date;
320 ui_mpc_metadata_file.innerText = currentsong.file;
321
322 /* Update the playlist tab. */
323 var playlist = [];
324 if ('Playlist' in state)
325 playlist = state.Playlist;
326 ui_mpc_playlist.innerHTML = '';
327 playlist.forEach(function(song) {
328 var cell, row = ui_mpc_playlist.insertRow(-1);
329 if (song.Pos == currentsong.Pos)
330 row.style.fontWeight = 'bold';
331
9cb957ed
MF
332 cell = row.insertCell(-1);
333 cell.id = 'playlist_del';
334 cell.innerHTML = '&#164;';
17ac93ed
MF
335 cell.song_id = song.Id;
336 cell.title = 'delete';
9cb957ed
MF
337 cell.onclick = playlist_del;
338
ea38f3ed
MF
339 cell = row.insertCell(-1);
340 cell.innerText = song.Pos;
341 cell.style.textAlign = 'right';
17ac93ed
MF
342 cell.song_id = song.Id;
343 cell.title = 'play';
344 cell.onclick = playlist_play;
ea38f3ed
MF
345
346 if ('Artist' in song) {
347 row.insertCell(-1).innerText = song.Artist;
348 row.insertCell(-1).innerText = song.Album;
349 row.insertCell(-1).innerText = song.Title;
350 } else {
351 cell = row.insertCell(-1);
352 cell.innerText = song.file;
353 cell.colSpan = 3;
354 }
355 row.insertCell(-1).innerText = pretty_time(song.Time);
356 });
50dafc4c 357
ea38f3ed 358 /* Update the status tab. */
a7204fc7
MF
359 var time;
360 if ('time' in state)
361 // When stopped, there is no time field at all.
362 time = state.time.split(':');
363 else
364 time = [0, 0];
ea38f3ed
MF
365 ui_mpc_seekcur.max = time[1];
366 ui_mpc_seekcur.value = time[0];
92ef8630 367 percent = Math.floor((0.0 + time[0]) * 100 / (0.0 + time[1]));
ea38f3ed
MF
368 ui_mpc_seekcur.title = 'seekcur (' + percent + '%)';
369 ui_mpc_currtime.innerText = [pretty_time(time[0]), pretty_time(time[1]), percent + '%'].join(' / ');
92ef8630 370
ea38f3ed
MF
371 ui_mpc_setvol.value = state.volume;
372 ui_mpc_setvol.title = 'setvol (' + state.volume + '%)';
50dafc4c 373
50dafc4c
MF
374 [
375 'consume', 'random', 'repeat', 'single',
376 ].forEach(function(id) {
377 setToggleButton(window['ui_mpc_' + id], szo(state[id]));
378 });
379
380 ui_mpc_status.innerText = ({
381 'play': 'Playing',
382 'pause': 'Paused',
383 'stop': 'Stopped',
384 })[state.state];
385}