Same mute button for lua http interface
[vlc/asuraparaju-public.git] / share / lua / http / js / functions.js
blobd751716c0fc4ebb880fb2a17200d97c17b390557
1 /*****************************************************************************
2  * functions.js: VLC media player web interface
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id: functions.js 21264 2007-08-19 17:48:28Z dionoea $
6  *
7  * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
24 /**********************************************************************
25  * Global variables
26  *********************************************************************/
28 var old_time = 0;
29 var pl_cur_id;
30 var albumart_id = -1;
32 /**********************************************************************
33  * Slider functions
34  *********************************************************************/
36 var slider_mouse_down = 0;
37 var slider_dx = 0;
39 var input_options = new Array();
41 /* findPosX() from http://www.quirksmode.rg/js/indpos.html */
42 function findPosX(obj)
44     var curleft = 0;
45     if (obj.offsetParent)
46     {
47         while (obj.offsetParent)
48         {
49             curleft += obj.offsetLeft
50             obj = obj.offsetParent;
51         }
52     }
53     else if (obj.x)
54         curleft += obj.x;
55     return curleft;
58 function slider_seek( e, bar )
60     seek(Math.floor(( e.clientX + document.body.scrollLeft - findPosX( bar )) / 4)+"%25");
62 function slider_down( e, point )
64     slider_mouse_down = 1;
65     slider_dx = e.clientX - findPosX( point );
67 function slider_up( e, bar )
69     slider_mouse_down = 0;
70     /* slider_seek( e, bar ); */
72 function slider_move( e, bar )
74     if( slider_mouse_down == 1 )
75     {
76         var slider_position  = Math.floor( e.clientX - slider_dx + document.body.scrollLeft - findPosX( bar ));
77         document.getElementById( 'main_slider_point' ).style.left = slider_position+"px";
78         slider_seek( e, bar );
79     }
82 /**********************************************************************
83  * Misc utils
84  *********************************************************************/
86 /* XMLHttpRequest wrapper */
87 function loadXMLDoc( url, callback )
89   // branch for native XMLHttpRequest object
90   if ( window.XMLHttpRequest )
91   {
92     req = new XMLHttpRequest();
93     req.onreadystatechange = callback;
94     req.open( "GET", url, true );
95     req.send( null );
96   // branch for IE/Windows ActiveX version
97   }
98   else if ( window.ActiveXObject )
99   {
100     req = new ActiveXObject( "Microsoft.XMLHTTP" );
101     if ( req )
102     {
103       req.onreadystatechange = callback;
104       req.open( "GET", url, true );
105       req.send();
106     }
107   }
110 /* fomat time in second as hh:mm:ss */
111 function format_time( s )
113     var hours = Math.floor(s/3600);
114     var minutes = Math.floor((s/60)%60);
115     var seconds = Math.floor(s%60);
116     if( hours < 10 ) hours = "0"+hours;
117     if( minutes < 10 ) minutes = "0"+minutes;
118     if( seconds < 10 ) seconds = "0"+seconds;
119     return hours+":"+minutes+":"+seconds;
122 /* delete all a tag's children and add a text child node */
123 function set_text( id, val )
125     var elt = document.getElementById( id );
126     while( elt.hasChildNodes() )
127         elt.removeChild( elt.firstChild );
128     elt.appendChild( document.createTextNode( val ) );
131 /* set item's 'element' attribute to value */
132 function set_css( item, element, value )
134     for( var j = 0; j < document.styleSheets.length; j++ )
135     {
136         var cssRules = document.styleSheets[j].cssRules;
137         if( !cssRules ) cssRules = document.styleSheets[j].rules;
138         for( var i = 0; i < cssRules.length; i++)
139         {
140             if( cssRules[i].selectorText == item )
141             {
142                 if( cssRules[i].style.setProperty )
143                     cssRules[i].style.setProperty( element, value, null );
144                 else
145                     cssRules[i].style.setAttribute( toCamelCase( element ), value );
146                 return;
147             }
148         }
149     }
152 /* get item's 'element' attribute */
153 function get_css( item, element )
155     for( var j = 0; j < document.styleSheets.length; j++ )
156     {
157         var cssRules = document.styleSheets[j].cssRules;
158         if( !cssRules ) cssRules = document.styleSheets[j].rules;
159         for( var i = 0; i < cssRules.length; i++)
160         {
161             if( cssRules[i].selectorText == item )
162             {
163                 if( cssRules[i].style.getPropertyValue )
164                     return cssRules[i].style.getPropertyValue( element );
165                 else
166                     return cssRules[i].style.getAttribute( toCamelCase( element ) );
167             }
168         }
169     }
172 function toggle_show( id )
174     var element = document.getElementById( id );
175     if( element.style.display == 'block' || element.style.display == '' )
176     {
177         element.style.display = 'none';
178     }
179     else
180     {
181         element.style.display = 'block';
182     }
184 function toggle_show_node( id )
186     var element = document.getElementById( 'pl_'+id );
187     var img = document.getElementById( 'pl_img_'+id );
188     if( element.style.display == 'block' || element.style.display == '' )
189     {
190         element.style.display = 'none';
191         img.setAttribute( 'src', 'images/plus.png' );
192         img.setAttribute( 'alt', '[+]' );
193     }
194     else
195     {
196         element.style.display = 'block';
197         img.setAttribute( 'src', 'images/minus.png' );
198         img.setAttribute( 'alt', '[-]' );
199     }
202 function show( id ){ document.getElementById( id ).style.display = 'block'; }
203 function showinline( id ){ document.getElementById( id ).style.display = 'inline'; }
205 function hide( id ){ document.getElementById( id ).style.display = 'none'; }
207 function checked( id ){ return document.getElementById( id ).checked; }
209 function value( id ){ return document.getElementById( id ).value; }
211 function setclass( obj, value )
213     obj.setAttribute( 'class', value ); /* Firefox */
214     obj.setAttribute( 'className', value ); /* IE */
217 function radio_value( name )
219     var radio = document.getElementsByName( name );
220     for( var i = 0; i < radio.length; i++ )
221     {
222         if( radio[i].checked )
223         {
224             return radio[i].value;
225         }
226     }
227     return "";
230 function check_and_replace_int( id, val )
232     var objRegExp = /^\d+$/;
233     if( value( id ) != ''
234         && ( !objRegExp.test( value( id ) )
235              || parseInt( value( id ) ) < 1 ) )
236         return document.getElementById( id ).value = val;
237     return document.getElementById( id ).value;
240 function addslashes( str ){ return str.replace(/\'/g, '\\\''); }
241 function escapebackslashes( str ){ return str.replace(/\\/g, '\\\\'); }
243 function toCamelCase( str )
245     str = str.split( '-' );
246     var cml = str[0];
247     for( var i=1; i<str.length; i++)
248         cml += str[i].charAt(0).toUpperCase()+str[i].substring(1);
249     return cml;
252 function disable( id ){ document.getElementById( id ).disabled = true; }
254 function enable( id ){ document.getElementById( id ).disabled = false; }
256 function button_over( element ){ element.style.border = "1px solid #000"; }
258 function button_out( element ){ element.style.border = "1px solid #fff"; }
259 function button_out_menu( element ){ element.style.border = "1px solid transparent"; }
261 function show_menu( id ){ document.getElementById(id).style.display = 'block'; }
262 function hide_menu( id ){ document.getElementById(id).style.display = 'none'; }
264 /* toggle show help under the buttons */
265 function toggle_btn_text()
267     if( get_css( '.btn_text', 'display' ) == 'none' )
268     {
269         set_css( '.btn_text', 'display', 'block' );
270     }
271     else
272     {
273         set_css( '.btn_text', 'display', 'none' );
274     }
277 function clear_children( elt )
278 {   
279     if( elt )
280         while( elt.hasChildNodes() )
281             elt.removeChild( elt.firstChild );
284 /**********************************************************************
285  * Interface actions
286  *********************************************************************/
287 /* input actions */
288 function in_playenqueue( cmd )
290     var input = value('input_mrl');
291     var url = 'requests/status.xml?command=in_'+cmd+'&input='+encodeURIComponent( addslashes(escapebackslashes(input)) );
292     for( i in input_options )
293         if( input_options[i] != ':option=value' )
294             url += '&option='+encodeURIComponent( addslashes(escapebackslashes(input_options[i]) ));
295     loadXMLDoc( url, parse_status );
296     setTimeout( 'update_playlist()', 1000 );
299 function in_play()
301     in_playenqueue( 'play' );
304 function in_enqueue()
306     in_playenqueue( 'enqueue' );
309 /* playlist actions */
310 function pl_play( id )
312     loadXMLDoc( 'requests/status.xml?command=pl_play&id='+id, parse_status );
313     pl_cur_id = id;
314     setTimeout( 'update_playlist()', 1000 );
316 function pl_pause()
318     loadXMLDoc( 'requests/status.xml?command=pl_pause&id='+pl_cur_id, parse_status );
320 function pl_stop()
322     loadXMLDoc( 'requests/status.xml?command=pl_stop', parse_status );
323     setTimeout( 'update_playlist()', 1000 );
325 function pl_next()
327     loadXMLDoc( 'requests/status.xml?command=pl_next', parse_status );
328     setTimeout( 'update_playlist()', 1000 );
330 function pl_previous()
332     loadXMLDoc( 'requests/status.xml?command=pl_previous', parse_status );
333     setTimeout( 'update_playlist()', 1000 );
335 function pl_delete( id )
337     loadXMLDoc( 'requests/status.xml?command=pl_delete&id='+id, parse_status );
338     setTimeout( 'update_playlist()', 1000 );
340 function pl_empty()
342     loadXMLDoc( 'requests/status.xml?command=pl_empty', parse_status );
343     setTimeout( 'update_playlist()', 1000 );
345 function pl_sort( sort, order )
347     loadXMLDoc( 'requests/status.xml?command=pl_sort&id='+order+'&val='+sort, parse_status );
348     setTimeout( 'update_playlist()', 1000 );
350 function pl_shuffle()
352     loadXMLDoc( 'requests/status.xml?command=pl_random', parse_status );
353     setTimeout( 'update_playlist()', 1000 );
355 function pl_loop()
357     loadXMLDoc( 'requests/status.xml?command=pl_loop', parse_status );
359 function pl_repeat()
361     loadXMLDoc( 'requests/status.xml?command=pl_repeat', parse_status );
363 function pl_sd( value )
365     loadXMLDoc( 'requests/status.xml?command=pl_sd&val='+value, parse_status );
368 /* misc actions */
369 function volume_down()
371     loadXMLDoc( 'requests/status.xml?command=volume&val=-20', parse_status );
373 function volume_up()
375     loadXMLDoc( 'requests/status.xml?command=volume&val=%2B20', parse_status );
377 function volume_mute()
379     loadXMLDoc( 'requests/status.xml?command=volume&val=0', parse_status );
381 function seek( pos )
383     loadXMLDoc( 'requests/status.xml?command=seek&val='+pos, parse_status );
385 function fullscreen()
387     loadXMLDoc( 'requests/status.xml?command=fullscreen', parse_status );
389 function snapshot()
391     loadXMLDoc( 'requests/status.xml?command=snapshot', parse_status );
393 function hotkey( str )
395     /* Use hotkey name (without the "key-" part) as the argument to simulate a hotkey press */
396     loadXMLDoc( 'requests/status.xml?command=key&val='+str, parse_status );
398 function update_status()
400     loadXMLDoc( 'requests/status.xml', parse_status );
402 function update_playlist()
404     loadXMLDoc( 'requests/playlist.xml', parse_playlist );
406 function update_playlist_search(key)
408     loadXMLDoc( 'requests/playlist.xml?search='+encodeURIComponent(key), parse_playlist )
410 function reset_search()
412     var search = document.getElementById('search')
413     if( search )
414     {
415         search.value = '<search>'
416         update_playlist_search('')
417     }
420 /**********************************************************************
421  * Parse xml replies to XMLHttpRequests
422  *********************************************************************/
423 /* parse request/status.xml */
424 function parse_status()
426     if( req.readyState == 4 )
427     {
428         if( req.status == 200 )
429         {
430             var status = req.responseXML.documentElement;
431             var timetag = status.getElementsByTagName( 'time' );
432             if( timetag.length > 0 )
433             {
434                 var new_time = timetag[0].firstChild.data;
435             }
436             else
437             {
438                 new_time = old_time;
439             }
440             var lengthtag = status.getElementsByTagName( 'length' );
441             var length;
442             if( lengthtag.length > 0 )
443             {
444                 length = lengthtag[0].firstChild.data;
445             }
446             else
447             {
448                 length = 0;
449             }
450             var slider_position;
451             positiontag = status.getElementsByTagName( 'position' );
452             if( length < 100 && positiontag.length > 0 )
453             {
454                 slider_position = ( positiontag[0].firstChild.data * 4 ) + "px";
455             }
456             else if( length > 0 )
457             {
458                 /* this is more precise if length > 100 */
459                 slider_position = Math.floor( ( new_time * 400 ) / length ) + "px";
460             }
461             else
462             {
463                 slider_position = 0;
464             }
465             if( old_time > new_time )
466                 setTimeout('update_playlist()',50);
467             old_time = new_time;
468             set_text( 'time', format_time( new_time ) );
469             set_text( 'length', format_time( length ) );
470             if( status.getElementsByTagName( 'volume' ).length != 0 )
471                 set_text( 'volume', Math.floor(status.getElementsByTagName( 'volume' )[0].firstChild.data/5.12)+'%' );
472             var statetag = status.getElementsByTagName( 'state' );
473             if( statetag.length > 0 )
474             {
475                 set_text( 'state', statetag[0].firstChild.data );
476             }
477             else
478             {
479                 set_text( 'state', '(?)' );
480             }
481             if( slider_mouse_down == 0 )
482             {
483                 document.getElementById( 'main_slider_point' ).style.left = slider_position;
484             }
485             var statustag = status.getElementsByTagName( 'state' );
486             if( statustag.length > 0 ? statustag[0].firstChild.data == "playing" : 0 )
487             {
488                 document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/pause.png' );
489                 document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Pause' );
490                 document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Pause' );
491             }
492             else
493             {
494                 document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/play.png' );
495                 document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Play' );
496                 document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Play' );
497             }
499             var randomtag = status.getElementsByTagName( 'random' );
500             if( randomtag.length > 0 ? randomtag[0].firstChild.data == "1" : 0)
501                 setclass( document.getElementById( 'btn_shuffle'), 'on' );
502             else
503                 setclass( document.getElementById( 'btn_shuffle'), 'off' );
504                
505             var looptag = status.getElementsByTagName( 'loop' );
506             if( looptag.length > 0 ? looptag[0].firstChild.data == "1" : 0)
507                 setclass( document.getElementById( 'btn_loop'), 'on' );
508             else
509                 setclass( document.getElementById( 'btn_loop'), 'off' );
511             var repeattag = status.getElementsByTagName( 'repeat' );
512             if( repeattag.length > 0 ? repeattag[0].firstChild.data == "1" : 0 )
513                 setclass( document.getElementById( 'btn_repeat'), 'on' );
514             else
515                 setclass( document.getElementById( 'btn_repeat'), 'off' );
517             var tree = document.createElement( "ul" );
518             var categories = status.getElementsByTagName( 'category' );
519             var i;
520             for( i = 0; i < categories.length; i++ )
521             {
522                 var item = document.createElement( "li" );
523                 item.appendChild( document.createTextNode( categories[i].getAttribute( 'name' ) ) );
524                 var subtree = document.createElement( "dl" );
525                 var infos = categories[i].getElementsByTagName( 'info' );
526                 var j;
527                 for( j = 0; j < infos.length; j++ )
528                 {
529                     var subitem = document.createElement( "dt" );
530                     subitem.appendChild( document.createTextNode( infos[j].getAttribute( 'name' ) ) );
531                     subtree.appendChild( subitem );
532                     if( infos[j].hasChildNodes() )
533                     {
534                         var subitem = document.createElement( "dd" );
535                         subitem.appendChild( document.createTextNode( infos[j].firstChild.data ) );
536                         subtree.appendChild( subitem );
537                     }
538                 }
539                 item.appendChild( subtree );
540                 tree.appendChild( item );
541             }
542             var infotree = document.getElementById('infotree' );
543             clear_children( infotree );
544             infotree.appendChild( tree );
545             
546         }
547         else
548         {
549             /*alert( 'Error! HTTP server replied: ' + req.status );*/
550         }
551     }
554 /* parse playlist.xml */
555 function parse_playlist()
557     if( req.readyState == 4 )
558     {
559         if( req.status == 200 )
560         {
561             var answer = req.responseXML.documentElement;
562             var playtree = document.getElementById( 'playtree' );
563             var pos = document.createElement( "div" );
564             var pos_top = pos;
565             var elt = answer.firstChild;
566             
567             pl_cur_id = 0;  /* changed to the current id is there actually
568                              * is a current id */
569             while( elt )
570             {
571                 if( elt.nodeName == "node" )
572                 {
573                     if( pos.hasChildNodes() )
574                         pos.appendChild( document.createElement( "br" ) );
575                     var nda = document.createElement( 'a' );
576                     nda.setAttribute( 'href', 'javascript:toggle_show_node(\''+elt.getAttribute( 'id' )+'\');' );
577                     var ndai = document.createElement( 'img' );
578                     ndai.setAttribute( 'src', 'images/minus.png' );
579                     ndai.setAttribute( 'alt', '[-]' );
580                     ndai.setAttribute( 'id', 'pl_img_'+elt.getAttribute( 'id' ) );
581                     nda.appendChild( ndai );
582                     pos.appendChild( nda );
583                     pos.appendChild( document.createTextNode( ' ' + elt.getAttribute( 'name' ) ) );
585                     if( elt.getAttribute( 'ro' ) == 'rw' )
586                     {
587                         pos.appendChild( document.createTextNode( ' ' ) );
588                         var del = document.createElement( "a" );
589                         del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
590                             var delimg = document.createElement( "img" );
591                             delimg.setAttribute( 'src', 'images/delete_small.png' );
592                             delimg.setAttribute( 'alt', '(delete)' );
593                         del.appendChild( delimg );
594                         pos.appendChild( del );
595                     }
597                     var nd = document.createElement( "div" );
598                     setclass( nd, 'pl_node' );
599                     nd.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
600                     pos.appendChild( nd );
601                 }
602                 else if( elt.nodeName == "leaf" )
603                 {
604                     if( pos.hasChildNodes() )
605                     pos.appendChild( document.createElement( "br" ) );
606                     var pl = document.createElement( "a" );
607                     setclass( pl, 'pl_leaf' );
608                     pl.setAttribute( 'href', 'javascript:pl_play('+elt.getAttribute( 'id' )+');' );
609                     pl.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
610                     if( elt.getAttribute( 'current' ) == 'current' )
611                     {
612                         pl.style.fontWeight = 'bold';
613                         var nowplaying = document.getElementById( 'nowplaying' );
614                         clear_children( nowplaying );
615                         nowplaying.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
616                         pl.appendChild( document.createTextNode( '* '));
617                         pl_cur_id = elt.getAttribute( 'id' );
618                     }
619                     pl.setAttribute( 'title', elt.getAttribute( 'uri' ));
620                     pl.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
621                     var duration = elt.getAttribute( 'duration' );
622                     if( duration > 0 )
623                         pl.appendChild( document.createTextNode( " (" + format_time( elt.getAttribute( 'duration' ) ) + ")" ) );
624                     pos.appendChild( pl );
626                     if( elt.getAttribute( 'ro' ) == 'rw' )
627                     {
628                         pos.appendChild( document.createTextNode( ' ' ) );
629                         var del = document.createElement( "a" );
630                         del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
631                             var delimg = document.createElement( "img" );
632                             delimg.setAttribute( 'src', 'images/delete_small.png' );
633                             delimg.setAttribute( 'alt', '(delete)' );
634                         del.appendChild( delimg );
635                         pos.appendChild( del );
636                     }
637                 }
638                 if( elt.firstChild )
639                 {
640                     elt = elt.firstChild;
641                     pos = pos.lastChild;
642                 }
643                 else if( elt.nextSibling )
644                 {
645                     elt = elt.nextSibling;
646                     pos = pos;
647                 }
648                 else
649                 {
650                     while( ! elt.parentNode.nextSibling )
651                     {
652                         elt = elt.parentNode;
653                         if( ! elt.parentNode ) break;
654                         pos = pos.parentNode;
655                     }
656                     if( ! elt.parentNode ) break;
657                     elt = elt.parentNode.nextSibling;
658                     pos = pos.parentNode;
659                 }
660             }
661             clear_children( playtree );
662             playtree.appendChild( pos_top );
663         }
664         else
665         {
666             /*alert( 'Error! HTTP server replied: ' + req.status );*/
667         }
668     }
671 /* parse browse.xml */
672 function parse_browse_dir( )
674     if( req.readyState == 4 )
675     {
676         if( req.status == 200 )
677         {
678             var answer = req.responseXML.documentElement;
679             if( !answer ) return;
680             var browser = document.getElementById( 'browser' );
681             var pos = document.createElement( "div" );
682             var elt = answer.firstChild;
683             while( elt )
684             {
685                 if( elt.nodeName == "element" )
686                 {
687                     var item = document.createElement( "a" );
688                     setclass( item, 'browser' );
689                     if( elt.getAttribute( 'type' ) == 'dir' )
690                     {
691                         item.setAttribute( 'href', 'javascript:browse_dir(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');');
692                     }
693                     else
694                     {
695                         item.setAttribute( 'href', 'javascript:browse_path(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');' );
696                     }
697                     item.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
698                     pos.appendChild( item );
699                     if( elt.getAttribute( 'type' ) == 'dir' )
700                     {
701                         pos.appendChild( document.createTextNode( ' ' ) );
702                         var item = document.createElement( "a" );
703                         setclass( item, 'browser' );
704                         item.setAttribute( 'href', 'javascript:browse_path(\''+addslashes(escapebackslashes(elt.getAttribute( 'path' )))+'\');');
705                         item.appendChild( document.createTextNode( '(select)' ) );
706                         pos.appendChild( item );
707                     }
708                     pos.appendChild( document.createElement( "br" ) );
709                 }
710                 elt = elt.nextSibling;
711             }
712             clear_children( browser );
713             browser.appendChild( pos );
714         }
715         else
716         {
717             /*alert( 'Error! HTTP server replied: ' + req.status );*/
718         }
719     }
722 /**********************************************************************
723  * Input dialog functions
724  *********************************************************************/
725 function hide_input( )
727     document.getElementById( 'input_file' ).style.display = 'none';
728     document.getElementById( 'input_disc' ).style.display = 'none';
729     document.getElementById( 'input_network' ).style.display = 'none';
730     document.getElementById( 'input_fake' ).style.display = 'none';
733 /* update the input MRL using data from the input file helper */
734 /* FIXME ... subs support */
735 function update_input_file()
737     var mrl = document.getElementById( 'input_mrl' );
739     mrl.value = value( 'input_file_filename' );
742 /* update the input MRL using data from the input disc helper */
743 function update_input_disc()
745     var mrl     = document.getElementById( 'input_mrl' );
746     var type    = radio_value( "input_disc_type" );
747     var device  = value( "input_disc_dev" );
749     var title   = check_and_replace_int( 'input_disc_title', 0 );
750     var chapter = check_and_replace_int( 'input_disc_chapter', 0 );
751     var subs    = check_and_replace_int( 'input_disc_subtrack', '' );
752     var audio   = check_and_replace_int( 'input_disc_audiotrack', 0 );
754     mrl.value = "";
756     if( type == "dvd" )
757     {
758         mrl.value += "dvd://";
759     }
760     else if( type == "dvdsimple" )
761     {
762         mrl.value += "dvdsimple://";
763     }
764     else if( type == "vcd" )
765     {
766         mrl.value += "vcd://";
767     }
768     else if( type == "cdda" )
769     {
770         mrl.value += "cdda://";
771     }
773     mrl.value += device;
775     if( title )
776     {
777         mrl.value += "@"+title;
778         if( chapter && type != "cdda" )
779             mrl.value += ":"+chapter;
780     }
782     remove_input_options( ':sub-track' );
783     remove_input_options( ':audio-track' );
785     if( type != "cdda" )
786     {
787         if( subs != '' )
788             add_input_option( ":sub-track="+subs );
789         if( audio != '' )
790             add_input_option( ":audio-track="+audio );
791     }
795 /* update the input MRL using data from the input network helper */
796 function update_input_net()
798     var mrl = document.getElementById( 'input_mrl' );
799     var type = radio_value( "input_net_type" );
800     
801     check_and_replace_int( 'input_net_udp_port', 1234 );
802     check_and_replace_int( 'input_net_udpmcast_port', 1234 );
804     mrl.value = "";
806     if( type == "udp" )
807     {
808         mrl.value += "udp://";
809         if( checked( 'input_net_udp_forceipv6' ) )
810             mrl.value += "[::]";
811         if( value( 'input_net_udp_port' ) )
812             mrl.value += ":"+value( 'input_net_udp_port' );
813     }
814     else if( type == "udpmcast" )
815     {
816         mrl.value += "udp://@"+value( 'input_net_udpmcast_address');
817         if( value( 'input_net_udpmcast_port' ) )
818             mrl.value += ":"+value( 'input_net_udpmcast_port' );
819     }
820     else if( type == "http" )
821     {
822         var url = value( 'input_net_http_url' );
823         if( url.substring(0,7) != "http://"
824             && url.substring(0,8) != "https://"
825             && url.substring(0,6) != "ftp://"
826             && url.substring(0,6) != "mms://"
827             && url.substring(0,7) != "mmsh://" )
828             mrl.value += "http://";
829         mrl.value += url;
830     }
831     else if( type == "rtsp" )
832     {
833         var url = value( 'input_net_rtsp_url' );
834         if( url.substring(0,7) != "rtsp://" )
835             mrl.value += "rtsp://";
836         mrl.value += url;
837     }
839     remove_input_options( ':access-filter' );
840     if( checked( "input_net_timeshift" ) )
841         add_input_option( ":access-filter=timeshift" );
844 /* update the input MRL using data from the input fake helper */
845 function update_input_fake()
847     remove_input_options( ":fake" );
848     var mrl = document.getElementById( 'input_mrl' );
850     mrl.value = "fake://";
852     add_input_option( ":fake-file=" + value( "input_fake_filename" ) );
854     if( value( "input_fake_width" ) )
855         add_input_option( ":fake-width=" + value( "input_fake_width" ) );
856     if( value( "input_fake_height" ) )
857         add_input_option( ":fake-height=" + value( "input_fake_height" ) );
858     if( value( "input_fake_ar" ) )
859         add_input_option( ":fake-ar=" + value( "input_fake_ar" ) );
862 /**********************************************************************
863  * Sout dialog functions
864  *********************************************************************/
865 /* toggle show the full sout interface */
866 function toggle_show_sout_helper()
868     var element = document.getElementById( "sout_helper" );
869     if( element.style.display == 'block' )
870     {
871         element.style.display = 'none';
872         document.getElementById( "sout_helper_toggle" ).value = 'Full sout interface';
873     }
874     else
875     {
876         element.style.display = 'block';
877         document.getElementById( "sout_helper_toggle" ).value = 'Hide sout interface';
878     }
881 /* update the sout MRL using data from the sout_helper */
882 function update_sout()
884     var option = "";
885     /* Remove all options starting with :sout since we're going to write them
886      * again. */
887     remove_input_options( ":sout" );
889     check_and_replace_int( 'sout_http_port', 8080 );
890     check_and_replace_int( 'sout_mmsh_port', 8080 );
891     check_and_replace_int( 'sout_rtp_port', 1234 );
892     check_and_replace_int( 'sout_udp_port', 1234 );
893     check_and_replace_int( 'sout_ttl', 1 );
895     if( checked( 'sout_soverlay' ) )
896     {
897         disable( 'sout_scodec' );
898         disable( 'sout_sub' );
899     }
900     else
901     {
902         enable( 'sout_scodec' );
903         enable( 'sout_sub' );
904     }
906     var transcode =  checked( 'sout_vcodec_s' ) || checked( 'sout_acodec_s' )
907                   || checked( 'sout_sub' )      || checked( 'sout_soverlay' );
909     if( transcode )
910     {
911         option = ":sout=#transcode{";
912         var alot = false; /* alot == at least one transcode */
913         if( checked( 'sout_vcodec_s' ) )
914         {
915             option += "vcodec="+value( 'sout_vcodec' )+",vb="+value( 'sout_vb' )+",scale="+value( 'sout_scale' );
916             alot = true;
917         }
918         if( checked( 'sout_acodec_s' ) )
919         {
920             if( alot ) option += ",";
921             option += "acodec="+value( 'sout_acodec' )+",ab="+value( 'sout_ab' );
922             if( value( 'sout_channels' ) )
923                 option += ",channels="+value( 'sout_channels' );
924             alot = true;
925         }
926         if( checked( 'sout_soverlay' ) )
927         {
928             if( alot ) option += ",";
929             option += "soverlay";
930             alot = true;
931         }
932         else if( checked( 'sout_sub' ) )
933         {
934             if( alot ) option += ",";
935             option += "scodec="+value( 'sout_scodec' );
936             alot = true;
937         }
938         option += value( 'sout_transcode_extra' );
939             
940         option += "}";
942     }
944     var output = checked( 'sout_display' ) + checked( 'sout_file' )
945                + checked( 'sout_http' )    + checked( 'sout_mmsh' )
946                + checked( 'sout_rtp' )     + checked( 'sout_udp' );
948     if( output )
949     {
950         if( transcode )
951             option += ":";
952         else
953             option += ":sout=#";
954         var aloo = false; /* aloo == at least one output */
955         var mux = radio_value( 'sout_mux' );
956         var ttl = parseInt( value( 'sout_ttl' ) );
957         if( output > 1 ) option += "duplicate{";
958         if( checked( 'sout_display' ) )
959         {
960             if( output > 1 ) option += "dst="
961             option += "display";
962             aloo = true;
963         }
964         if( checked( 'sout_file' ) )
965         {
966             if( aloo ) option += ",";
967             if( output > 1 ) option += "dst="
968             option += "std{access=file,mux="+mux+",dst="+value( 'sout_file_filename' )+"}";
969             aloo = true;
970         }
971         if( checked( 'sout_http' ) )
972         {
973             if( aloo ) option += ",";
974             if( output > 1 ) option += "dst="
975             option += "std{access=http,mux="+mux+",dst="+value( 'sout_http_addr' );
976             if( value( 'sout_http_port' ) )
977                 option += ":"+value( 'sout_http_port' );
978             option += "}";
979             aloo = true;
980         }
981         if( checked( 'sout_mmsh' ) )
982         {
983             if( aloo ) option += ",";
984             if( output > 1 ) option += "dst="
985             option += "std{access=mmsh,mux="+mux+",dst="+value( 'sout_mmsh_addr' );
986             if( value( 'sout_mmsh_port' ) )
987                 option += ":"+value( 'sout_mmsh_port' );
988             option += "}";
989             aloo = true;
990         }
991         if( checked( 'sout_rtp' ) )
992         {
993             if( aloo ) option += ",";
994             if( output > 1 ) option += "dst="
995             option += "std{access=rtp";
996             if( ttl ) option += "{ttl="+ttl+"}";
997             option += ",mux="+mux+",dst="+value( 'sout_rtp_addr' );
998             if( value( 'sout_rtp_port' ) )
999                 option += ":"+value( 'sout_rtp_port' );
1000             if( checked( 'sout_sap' ) )
1001             {
1002                 option += ",sap";
1003                 if( value( 'sout_sap_group' ) != '' )
1004                 {
1005                     option += ",group=\""+value( 'sout_sap_group' )+"\"";
1006                 }
1007                 option += ",name=\""+value( 'sout_sap_name' )+"\"";
1008             }
1009             option += "}";
1010             aloo = true;
1011         }
1012         if( checked( 'sout_udp' ) )
1013         {
1014             if( aloo ) option += ",";
1015             if( output > 1 ) option += "dst="
1016             option += "std{access=udp";
1017             if( ttl ) option += "{ttl="+ttl+"}";
1018             option += ",mux="+mux+",dst="+value( 'sout_udp_addr' );
1019             if( value('sout_udp_port' ) )
1020                 option += ":"+value( 'sout_udp_port' );
1021             if( checked( 'sout_sap' ) )
1022             {
1023                 option += ",sap";
1024                 if( value( 'sout_sap_group' ) != '' )
1025                 {
1026                     option += ",group=\""+value( 'sout_sap_group' )+"\"";
1027                 }
1028                 option += ",name=\""+value( 'sout_sap_name' )+"\"";
1029             }
1030             option += "}";
1031             aloo = true;
1032         }
1033         if( output > 1 ) option += "}";
1034     }
1036     if( option != "" )
1037         input_options.push( option );
1039     if( ( transcode || output ) && checked( 'sout_all' ) )
1040         input_options.push( ":sout-all" );
1042     var mrl = document.getElementById( 'sout_mrl' );
1043     mrl.value = option;
1045     refresh_input_options_list();
1048 /* reset sout mrl value */
1049 function reset_sout()
1051     document.getElementById('sout_mrl').value = value('sout_old_mrl');
1054 /* save sout mrl value */
1055 function save_sout()
1057     document.getElementById('sout_old_mrl').value = value('sout_mrl');
1060 function refresh_input_options_list()
1062     var iol = document.getElementById( 'input_options_list' );
1063     clear_children( iol );
1064     input_options.sort();
1065     for( i in input_options )
1066     {
1067         var o = document.createElement( 'div' );
1068         var ot = document.createElement( 'input' );
1069         ot.setAttribute( 'type', 'text' );
1070         ot.setAttribute( 'size', '60' );
1071         ot.setAttribute( 'value', input_options[i] );
1072         ot.setAttribute( 'id', 'input_option_item_'+i );
1073         ot.setAttribute( 'onchange', 'javascript:save_input_option('+i+',this.value);' );
1074         ot.setAttribute( 'onfocus', 'if( this.value == ":option=value" ) this.value = ":";' );
1075         ot.setAttribute( 'onblur', 'if( this.value == ":" ) this.value = ":option=value";' );
1076         o.appendChild( ot );
1077         var od = document.createElement( 'a' );
1078         od.setAttribute( 'href', 'javascript:delete_input_option('+i+');' );
1079         var delimg = document.createElement( "img" );
1080         delimg.setAttribute( 'src', 'images/delete_small.png' );
1081         delimg.setAttribute( 'alt', '(delete)' );
1082         od.appendChild( delimg );
1083         o.appendChild( od );
1084         iol.appendChild( o );
1085     }
1088 function delete_input_option( i )
1090     input_options.splice(i,1);
1091     refresh_input_options_list();
1094 function save_input_option( i, value )
1096     input_options[i] = value;
1097     refresh_input_options_list();
1100 function add_input_option( value )
1102     input_options.push( value );
1103     refresh_input_options_list();
1106 function remove_input_options( prefix )
1108     for( i in input_options )
1109         if( input_options[i].substring( 0, prefix.length ) == prefix )
1110         {
1111             delete input_options[i];
1112             i--;
1113         }
1117 /**********************************************************************
1118  * Browser dialog functions
1119  *********************************************************************/
1120 /* only browse() should be called directly */
1121 function browse( dest )
1123     document.getElementById( 'browse_dest' ).value = dest;
1124     document.getElementById( 'browse_lastdir' ).value;
1125     browse_dir( document.getElementById( 'browse_lastdir' ).value );
1126     show( 'browse' );
1128 function browse_dir( dir )
1130     document.getElementById( 'browse_lastdir' ).value = dir;
1131     loadXMLDoc( 'requests/browse.xml?dir='+encodeURIComponent(dir), parse_browse_dir );
1133 function browse_path( p )
1135     document.getElementById( value( 'browse_dest' ) ).value = p;
1136     hide( 'browse' );
1137     document.getElementById( value( 'browse_dest' ) ).focus();
1139 function refresh_albumart( force )
1141     if( albumart_id != pl_cur_id || force )
1142     {
1143         var now = new Date();
1144         var albumart = document.getElementById( 'albumart' );
1145         albumart.src = '/art?timestamp=' + now.getTime();
1146         albumart_id = pl_cur_id;
1147     }
1149 /**********************************************************************
1150  * Periodically update stuff in the interface
1151  *********************************************************************/
1152 function loop_refresh_status()
1154     setTimeout( 'loop_refresh_status()', 1000 );
1155     update_status();
1157 function loop_refresh_playlist()
1159     /* setTimeout( 'loop_refresh_playlist()', 10000 ); */
1160     update_playlist();
1162 function loop_refresh_albumart()
1164     setTimeout( 'loop_refresh_albumart()', 1000 );
1165     refresh_albumart( false );
1167 function loop_refresh()
1169     setTimeout( 'loop_refresh_status()', 1 );
1170     setTimeout( 'loop_refresh_playlist()', 1 );
1171     setTimeout( 'loop_refresh_albumart()', 1 );