functions.js 34.6 KB
Newer Older
Antoine Cellerier's avatar
Antoine Cellerier committed
1 2 3
/*****************************************************************************
 * functions.js: VLC media player web interface
 *****************************************************************************
4
 * Copyright (C) 2005-2006 the VideoLAN team
5
 * $Id$
Antoine Cellerier's avatar
Antoine Cellerier committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
Antoine Cellerier's avatar
Antoine Cellerier committed
22 23
 *****************************************************************************/

24 25 26
/**********************************************************************
 * Global variables
 *********************************************************************/
27

28
var old_time = 0;
29
var pl_cur_id;
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/**********************************************************************
 * Slider functions
 *********************************************************************/
 
var slider_mouse_down = 0;
var slider_dx = 0;

/* findPosX() from http://www.quirksmode.rg/js/indpos.html */
function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function slider_seek( e, bar )
{
    seek(Math.floor(( e.clientX + document.body.scrollLeft - findPosX( bar )) / 4)+"%25");
}
function slider_down( e, point )
{
    slider_mouse_down = 1;
    slider_dx = e.clientX - findPosX( point );
}
function slider_up( e, bar )
{
    slider_mouse_down = 0;
    /* slider_seek( e, bar ); */
}
function slider_move( e, bar )
{
    if( slider_mouse_down == 1 )
    {
        var slider_position  = Math.floor( e.clientX - slider_dx + document.body.scrollLeft - findPosX( bar ));
        document.getElementById( 'main_slider_point' ).style.left = slider_position+"px";
        slider_seek( e, bar );
    }
}

79 80 81 82 83
/**********************************************************************
 * Misc utils
 *********************************************************************/

/* XMLHttpRequest wrapper */
Antoine Cellerier's avatar
Antoine Cellerier committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
function loadXMLDoc( url, callback )
{
  // branch for native XMLHttpRequest object
  if ( window.XMLHttpRequest )
  {
    req = new XMLHttpRequest();
    req.onreadystatechange = callback;
    req.open( "GET", url, true );
    req.send( null );
  // branch for IE/Windows ActiveX version
  }
  else if ( window.ActiveXObject )
  {
    req = new ActiveXObject( "Microsoft.XMLHTTP" );
    if ( req )
    {
      req.onreadystatechange = callback;
      req.open( "GET", url, true );
      req.send();
    }
  }
}

107
/* fomat time in second as hh:mm:ss */
Antoine Cellerier's avatar
Antoine Cellerier committed
108 109
function format_time( s )
{
110 111
    var hours = Math.floor(s/3600);
    var minutes = Math.floor((s/60)%60);
112
    var seconds = Math.floor(s%60);
Antoine Cellerier's avatar
Antoine Cellerier committed
113 114 115 116 117 118
    if( hours < 10 ) hours = "0"+hours;
    if( minutes < 10 ) minutes = "0"+minutes;
    if( seconds < 10 ) seconds = "0"+seconds;
    return hours+":"+minutes+":"+seconds;
}

119 120 121
/* delete all a tag's children and add a text child node */
function set_text( id, val )
{
122
    var elt = document.getElementById( id );
123 124 125 126 127 128 129 130
    while( elt.hasChildNodes() )
        elt.removeChild( elt.firstChild );
    elt.appendChild( document.createTextNode( val ) );
}

/* set item's 'element' attribute to value */
function set_css( item, element, value )
{
131
    for( var j = 0; j < document.styleSheets.length; j++ )
132
    {
133 134
        var cssRules = document.styleSheets[j].cssRules;
        if( !cssRules ) cssRules = document.styleSheets[j].rules;
135
        for( var i = 0; i < cssRules.length; i++)
136 137 138
        {
            if( cssRules[i].selectorText == item )
            {
139 140 141 142
                if( cssRules[i].style.setProperty )
                    cssRules[i].style.setProperty( element, value, null );
                else
                    cssRules[i].style.setAttribute( toCamelCase( element ), value );
143 144 145 146 147 148 149 150 151
                return;
            }
        }
    }
}

/* get item's 'element' attribute */
function get_css( item, element )
{
152
    for( var j = 0; j < document.styleSheets.length; j++ )
153
    {
154 155
        var cssRules = document.styleSheets[j].cssRules;
        if( !cssRules ) cssRules = document.styleSheets[j].rules;
156
        for( var i = 0; i < cssRules.length; i++)
157 158 159
        {
            if( cssRules[i].selectorText == item )
            {
160 161 162 163
                if( cssRules[i].style.getPropertyValue )
                    return cssRules[i].style.getPropertyValue( element );
                else
                    return cssRules[i].style.getAttribute( toCamelCase( element ) );
164 165 166 167 168 169 170
            }
        }
    }
}

function toggle_show( id )
{
171
    var element = document.getElementById( id );
172 173 174 175 176 177 178 179 180 181 182
    if( element.style.display == 'block' || element.style.display == '' )
    {
        element.style.display = 'none';
    }
    else
    {
        element.style.display = 'block';
    }
}
function toggle_show_node( id )
{
183 184
    var element = document.getElementById( 'pl_'+id );
    var img = document.getElementById( 'pl_img_'+id );
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    if( element.style.display == 'block' || element.style.display == '' )
    {
        element.style.display = 'none';
        img.setAttribute( 'src', 'images/plus.png' );
        img.setAttribute( 'alt', '[+]' );
    }
    else
    {
        element.style.display = 'block';
        img.setAttribute( 'src', 'images/minus.png' );
        img.setAttribute( 'alt', '[-]' );
    }
}

function show( id ){ document.getElementById( id ).style.display = 'block'; }

function hide( id ){ document.getElementById( id ).style.display = 'none'; }

function checked( id ){ return document.getElementById( id ).checked; }

function value( id ){ return document.getElementById( id ).value; }

function radio_value( name )
{
209 210
    var radio = document.getElementsByName( name );
    for( var i = 0; i < radio.length; i++ )
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
    {
        if( radio[i].checked )
        {
            return radio[i].value;
        }
    }
    return "";
}

function check_and_replace_int( id, val )
{
    var objRegExp = /^\d\d*$/;
    if( value( id ) != ''
        && ( !objRegExp.test( value( id ) )
             || parseInt( value( id ) ) < 1 ) )
Antoine Cellerier's avatar
Antoine Cellerier committed
226 227
        return document.getElementById( id ).value = val;
    return document.getElementById( id ).value;
228
}
Antoine Cellerier's avatar
Antoine Cellerier committed
229

230
function addslashes( str ){ return str.replace(/\'/g, '\\\''); }
231
function escapebackslashes( str ){ return str.replace(/\\[^']/g, '\\\\'); }
232

233 234 235 236 237 238 239 240 241
function toCamelCase( str )
{
    str = str.split( '-' );
    var cml = str[0];
    for( var i=1; i<str.length; i++)
        cml += str[i].charAt(0).toUpperCase()+str[i].substring(1);
    return cml;
}

242 243 244 245
function disable( id ){ document.getElementById( id ).disabled = true; }

function enable( id ){ document.getElementById( id ).disabled = false; }

246
function button_over( element ){ element.style.border = "1px solid #000"; }
247

248
function button_out( element ){ element.style.border = "1px solid #fff"; }
249 250 251 252
function button_out_menu( element ){ element.style.border = "1px solid transparent"; }

function show_menu( id ){ document.getElementById(id).style.display = 'block'; }
function hide_menu( id ){ document.getElementById(id).style.display = 'none'; }
253 254 255 256 257 258 259 260 261 262 263 264 265 266

/* toggle show help under the buttons */
function toggle_btn_text()
{
    if( get_css( '.btn_text', 'display' ) == 'none' )
    {
        set_css( '.btn_text', 'display', 'block' );
    }
    else
    {
        set_css( '.btn_text', 'display', 'none' );
    }
}

267 268 269 270 271 272 273
function clear_children( elt )
{   
    if( elt )
        while( elt.hasChildNodes() )
            elt.removeChild( elt.firstChild );
}

274 275 276 277
/**********************************************************************
 * Interface actions
 *********************************************************************/
/* input actions */
Antoine Cellerier's avatar
Antoine Cellerier committed
278 279
function in_play()
{
280
    var input = value('input_mrl');
Antoine Cellerier's avatar
Antoine Cellerier committed
281
    if( value('sout_mrl') != '' )
282 283
        input += ' '+value('sout_mrl');
    var url = 'requests/status.xml?command=in_play&input='+escape( input );
Antoine Cellerier's avatar
Antoine Cellerier committed
284
    loadXMLDoc( url, parse_status );
285
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
286 287 288
}
function in_enqueue()
{
289
    var input = value('input_mrl');
Antoine Cellerier's avatar
Antoine Cellerier committed
290
    if( value('sout_mrl') != '' )
291 292
        input += ' '+value('sout_mrl');
    var url = 'requests/status.xml?command=in_enqueue&input='+escape( input );
Antoine Cellerier's avatar
Antoine Cellerier committed
293
    loadXMLDoc( url, parse_status );
294
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
295
}
296 297

/* playlist actions */
Antoine Cellerier's avatar
Antoine Cellerier committed
298 299 300
function pl_play( id )
{
    loadXMLDoc( 'requests/status.xml?command=pl_play&id='+id, parse_status );
301
    pl_cur_id = id;
302
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
303 304 305
}
function pl_pause()
{
306
    loadXMLDoc( 'requests/status.xml?command=pl_pause&id='+pl_cur_id, parse_status );
Antoine Cellerier's avatar
Antoine Cellerier committed
307 308 309 310
}
function pl_stop()
{
    loadXMLDoc( 'requests/status.xml?command=pl_stop', parse_status );
311
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
312 313 314 315
}
function pl_next()
{
    loadXMLDoc( 'requests/status.xml?command=pl_next', parse_status );
316
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
317 318 319 320
}
function pl_previous()
{
    loadXMLDoc( 'requests/status.xml?command=pl_previous', parse_status );
321
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
322 323 324 325
}
function pl_delete( id )
{
    loadXMLDoc( 'requests/status.xml?command=pl_delete&id='+id, parse_status );
326
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
327 328 329 330
}
function pl_empty()
{
    loadXMLDoc( 'requests/status.xml?command=pl_empty', parse_status );
331
    setTimeout( 'update_playlist()', 1000 );
Antoine Cellerier's avatar
Antoine Cellerier committed
332
}
333
function pl_sort( sort, order )
334
{
335
    loadXMLDoc( 'requests/status.xml?command=pl_sort&id='+order+'&val='+sort, parse_status );
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
    setTimeout( 'update_playlist()', 1000 );
}
function pl_shuffle()
{
    loadXMLDoc( 'requests/status.xml?command=pl_random', parse_status );
    setTimeout( 'update_playlist()', 1000 );
}
function pl_loop()
{
    loadXMLDoc( 'requests/status.xml?command=pl_loop', parse_status );
}
function pl_repeat()
{
    loadXMLDoc( 'requests/status.xml?command=pl_repeat', parse_status );
}
351 352 353 354
function pl_sd( value )
{
    loadXMLDoc( 'requests/status.xml?command=pl_sd&val='+value, parse_status );
}
355 356

/* misc actions */
Antoine Cellerier's avatar
Antoine Cellerier committed
357 358 359 360 361 362 363 364
function volume_down()
{
    loadXMLDoc( 'requests/status.xml?command=volume&val=-20', parse_status );
}
function volume_up()
{
    loadXMLDoc( 'requests/status.xml?command=volume&val=%2B20', parse_status );
}
365 366 367 368
function seek( pos )
{
    loadXMLDoc( 'requests/status.xml?command=seek&val='+pos, parse_status );
}
Antoine Cellerier's avatar
Antoine Cellerier committed
369 370 371 372 373 374 375 376 377 378 379 380 381
function fullscreen()
{
    loadXMLDoc( 'requests/status.xml?command=fullscreen', parse_status );
}
function update_status()
{
    loadXMLDoc( 'requests/status.xml', parse_status );
}
function update_playlist()
{
    loadXMLDoc( 'requests/playlist.xml', parse_playlist );
}

382 383 384 385
/**********************************************************************
 * Parse xml replies to XMLHttpRequests
 *********************************************************************/
/* parse request/status.xml */
Antoine Cellerier's avatar
Antoine Cellerier committed
386 387 388 389 390 391
function parse_status()
{
    if( req.readyState == 4 )
    {
        if( req.status == 200 )
        {
392
            var status = req.responseXML.documentElement;
393 394 395 396 397 398
            var timetag = status.getElementsByTagName( 'time' );
            if( timetag.length > 0 )
            {
                var new_time = timetag[0].firstChild.data;
            }
            else
399
            {
400 401 402 403 404 405 406
                new_time = old_time;
            }
            var lengthtag = status.getElementsByTagName( 'length' );
            var length;
            if( lengthtag.length > 0 )
            {
                length = lengthtag[0].firstChild.data;
407 408
            }
            else
409 410 411 412 413 414 415 416 417 418
            {
                length = 0;
            }
            var slider_position;
            positiontag = status.getElementsByTagName( 'position' );
            if( length < 100 && positiontag.length > 0 )
            {
                slider_position = ( positiontag[0].firstChild.data * 4 ) + "px";
            }
            else if( length > 0 )
419 420 421 422
            {
                /* this is more precise if length > 100 */
                slider_position = Math.floor( ( new_time * 400 ) / length ) + "px";
            }
423 424 425 426
            else
            {
                slider_position = 0;
            }
427 428 429 430
            if( old_time > new_time )
                setTimeout('update_playlist()',50);
            old_time = new_time;
            set_text( 'time', format_time( new_time ) );
431
            set_text( 'length', format_time( length ) );
432 433
            if( status.getElementsByTagName( 'volume' ).length != 0 )
                set_text( 'volume', Math.floor(status.getElementsByTagName( 'volume' )[0].firstChild.data/5.12)+'%' );
434 435 436 437 438 439 440 441 442
            var statetag = status.getElementsByTagName( 'state' );
            if( statetag.length > 0 )
            {
            	set_text( 'state', statetag[0].firstChild.data );
            }
            else
            {
                set_text( 'state', '(?)' );
            }
443 444 445 446
            if( slider_mouse_down == 0 )
            {
                document.getElementById( 'main_slider_point' ).style.left = slider_position;
            }
447 448
            var statustag = status.getElementsByTagName( 'state' );
            if( statustag.length > 0 ? statustag[0].firstChild.data == "playing" : 0 )
Antoine Cellerier's avatar
Antoine Cellerier committed
449 450
            {
                document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/pause.png' );
451 452
                document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Pause' );
                document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Pause' );
Antoine Cellerier's avatar
Antoine Cellerier committed
453 454 455 456
            }
            else
            {
                document.getElementById( 'btn_pause_img' ).setAttribute( 'src', 'images/play.png' );
457 458
                document.getElementById( 'btn_pause_img' ).setAttribute( 'alt', 'Play' );
                document.getElementById( 'btn_pause' ).setAttribute( 'title', 'Play' );
Antoine Cellerier's avatar
Antoine Cellerier committed
459
            }
460

461 462
            var randomtag = status.getElementsByTagName( 'random' );
            if( randomtag.length > 0 ? randomtag[0].firstChild.data == "1" : 0)
463 464 465
                document.getElementById( 'btn_shuffle').setAttribute( 'class', 'on' );
            else
                document.getElementById( 'btn_shuffle').setAttribute( 'class', 'off' );
466 467 468
               
            var looptag = status.getElementsByTagName( 'loop' );
            if( looptag.length > 0 ? looptag[0].firstChild.data == "1" : 0)
469 470 471
                document.getElementById( 'btn_loop').setAttribute( 'class', 'on' );
            else
                document.getElementById( 'btn_loop').setAttribute( 'class', 'off' );
472 473 474

            var repeattag = status.getElementsByTagName( 'repeat' );
            if( repeattag.length > 0 ? repeattag[0].firstChild.data == "1" : 0 )
475 476 477
                document.getElementById( 'btn_repeat').setAttribute( 'class', 'on' );
            else
                document.getElementById( 'btn_repeat').setAttribute( 'class', 'off' );
478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
            var tree = document.createElement( "ul" );
            var categories = status.getElementsByTagName( 'category' );
            var i;
            for( i = 0; i < categories.length; i++ )
            {
                var item = document.createElement( "li" );
                item.appendChild( document.createTextNode( categories[i].getAttribute( 'name' ) ) );
                var subtree = document.createElement( "dl" );
                var infos = categories[i].getElementsByTagName( 'info' );
                var j;
                for( j = 0; j < infos.length; j++ )
                {
                    var subitem = document.createElement( "dt" );
                    subitem.appendChild( document.createTextNode( infos[j].getAttribute( 'name' ) ) );
                    subtree.appendChild( subitem );
                    if( infos[j].hasChildNodes() )
                    {
                        var subitem = document.createElement( "dd" );
                        subitem.appendChild( document.createTextNode( infos[j].firstChild.data ) );
                        subtree.appendChild( subitem );
                    }
                }
                item.appendChild( subtree );
                tree.appendChild( item );
            }
            var infotree = document.getElementById('infotree' );
            clear_children( infotree );
            infotree.appendChild( tree );
            
Antoine Cellerier's avatar
Antoine Cellerier committed
508 509 510 511 512 513 514 515
        }
        else
        {
            /*alert( 'Error! HTTP server replied: ' + req.status );*/
        }
    }
}

516
/* parse playlist.xml */
Antoine Cellerier's avatar
Antoine Cellerier committed
517 518 519 520 521 522
function parse_playlist()
{
    if( req.readyState == 4 )
    {
        if( req.status == 200 )
        {
523 524 525 526 527
            var answer = req.responseXML.documentElement;
            var playtree = document.getElementById( 'playtree' );
            var pos = document.createElement( "div" );
            var pos_top = pos;
            var elt = answer.firstChild;
528 529 530
            
            pl_cur_id = 0;  /* changed to the current id is there actually
                             * is a current id */
Antoine Cellerier's avatar
Antoine Cellerier committed
531 532 533 534
            while( elt )
            {
                if( elt.nodeName == "node" )
                {
535 536
                    if( pos.hasChildNodes() )
                        pos.appendChild( document.createElement( "br" ) );
537
                    var nda = document.createElement( 'a' );
538
                    nda.setAttribute( 'href', 'javascript:toggle_show_node(\''+elt.getAttribute( 'id' )+'\');' );
539
                    var ndai = document.createElement( 'img' );
540 541 542
                    ndai.setAttribute( 'src', 'images/minus.png' );
                    ndai.setAttribute( 'alt', '[-]' );
                    ndai.setAttribute( 'id', 'pl_img_'+elt.getAttribute( 'id' ) );
543 544
                    nda.appendChild( ndai );
                    pos.appendChild( nda );
545
                    pos.appendChild( document.createTextNode( ' ' + elt.getAttribute( 'name' ) ) );
546 547 548 549 550 551 552 553 554 555 556 557 558

                    if( elt.getAttribute( 'ro' ) == 'rw' )
                    {
                        pos.appendChild( document.createTextNode( ' ' ) );
                        var del = document.createElement( "a" );
                        del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
                            var delimg = document.createElement( "img" );
                            delimg.setAttribute( 'src', 'images/delete_small.png' );
                            delimg.setAttribute( 'alt', '(delete)' );
                        del.appendChild( delimg );
                        pos.appendChild( del );
                    }

559
                    var nd = document.createElement( "div" );
560 561
                    nd.setAttribute( 'class', 'pl_node' );
                    nd.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
562
                    pos.appendChild( nd );
Antoine Cellerier's avatar
Antoine Cellerier committed
563 564 565
                }
                else if( elt.nodeName == "leaf" )
                {
566 567
                    if( pos.hasChildNodes() )
                    pos.appendChild( document.createElement( "br" ) );
568
                    var pl = document.createElement( "a" );
Antoine Cellerier's avatar
Antoine Cellerier committed
569 570 571 572
                    pl.setAttribute( 'class', 'pl_leaf' );
                    pl.setAttribute( 'href', 'javascript:pl_play('+elt.getAttribute( 'id' )+');' );
                    pl.setAttribute( 'id', 'pl_'+elt.getAttribute( 'id' ) );
                    if( elt.getAttribute( 'current' ) == 'current' )
573
                    {
Antoine Cellerier's avatar
Antoine Cellerier committed
574
                        pl.setAttribute( 'style', 'font-weight: bold;' );
575 576 577 578
                        var nowplaying = document.getElementById( 'nowplaying' );
                        clear_children( nowplaying );
                        nowplaying.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
                        pl.appendChild( document.createTextNode( '* '));
579
                        pl_cur_id = elt.getAttribute( 'id' );
580
                    }
Antoine Cellerier's avatar
Antoine Cellerier committed
581 582
                    pl.setAttribute( 'title', elt.getAttribute( 'uri' ));
                    pl.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
583 584 585
                    var duration = elt.getAttribute( 'duration' );
                    if( duration > 0 )
                        pl.appendChild( document.createTextNode( " (" + format_time( elt.getAttribute( 'duration' ) / 1000000 ) + ")" ) );
586
                    pos.appendChild( pl );
587 588 589 590 591 592 593 594 595 596 597 598

                    if( elt.getAttribute( 'ro' ) == 'rw' )
                    {
                        pos.appendChild( document.createTextNode( ' ' ) );
                        var del = document.createElement( "a" );
                        del.setAttribute( 'href', 'javascript:pl_delete('+elt.getAttribute( 'id' )+')' );
                            var delimg = document.createElement( "img" );
                            delimg.setAttribute( 'src', 'images/delete_small.png' );
                            delimg.setAttribute( 'alt', '(delete)' );
                        del.appendChild( delimg );
                        pos.appendChild( del );
                    }
Antoine Cellerier's avatar
Antoine Cellerier committed
599 600 601 602 603 604 605 606 607 608 609 610 611
                }
                if( elt.firstChild )
                {
                    elt = elt.firstChild;
                    pos = pos.lastChild;
                }
                else if( elt.nextSibling )
                {
                    elt = elt.nextSibling;
                    pos = pos;
                }
                else
                {
612 613 614 615 616 617
                    while( ! elt.parentNode.nextSibling )
                    {
                        elt = elt.parentNode;
                        if( ! elt.parentNode ) break;
                    }
                    if( ! elt.parentNode ) break;
Antoine Cellerier's avatar
Antoine Cellerier committed
618 619 620 621
                    elt = elt.parentNode.nextSibling;
                    pos = pos.parentNode;
                }
            }
622
            clear_children( playtree );
623
            playtree.appendChild( pos_top );
Antoine Cellerier's avatar
Antoine Cellerier committed
624 625 626 627 628 629 630 631
        }
        else
        {
            /*alert( 'Error! HTTP server replied: ' + req.status );*/
        }
    }
}

632 633
/* parse browse.xml */
function parse_browse_dir( )
Antoine Cellerier's avatar
Antoine Cellerier committed
634
{
635
    if( req.readyState == 4 )
Antoine Cellerier's avatar
Antoine Cellerier committed
636
    {
637
        if( req.status == 200 )
Antoine Cellerier's avatar
Antoine Cellerier committed
638
        {
639
            var answer = req.responseXML.documentElement;
640
            if( !answer ) return;
641 642 643
            var browser = document.getElementById( 'browser' );
            var pos = document.createElement( "div" );
            var elt = answer.firstChild;
644
            while( elt )
Antoine Cellerier's avatar
Antoine Cellerier committed
645
            {
646 647
                if( elt.nodeName == "element" )
                {
648 649
                    var item = document.createElement( "a" );
                    item.setAttribute( 'class', 'browser' );
650 651
                    if( elt.getAttribute( 'type' ) == 'directory' )
                    {
652
                        item.setAttribute( 'href', 'javascript:browse_dir(\''+escapebackslashes(addslashes(elt.getAttribute( 'path' )))+'\');');
653 654 655
                    }
                    else
                    {
656
                        item.setAttribute( 'href', 'javascript:browse_path(\''+escapebackslashes(addslashes(elt.getAttribute( 'path' )))+'\');' );
657
                    }
658 659
                    item.appendChild( document.createTextNode( elt.getAttribute( 'name' ) ) );
                    pos.appendChild( item );
660 661 662
                    if( elt.getAttribute( 'type' ) == 'directory' )
                    {
                        pos.appendChild( document.createTextNode( ' ' ) );
663 664 665 666 667
                        var item = document.createElement( "a" );
                        item.setAttribute( 'class', 'browser' );
                        item.setAttribute( 'href', 'javascript:browse_path(\''+escapebackslashes(addslashes(elt.getAttribute( 'path' )))+'\');');
                        item.appendChild( document.createTextNode( '(select)' ) );
                        pos.appendChild( item );
668 669 670 671
                    }
                    pos.appendChild( document.createElement( "br" ) );
                }
                elt = elt.nextSibling;
Antoine Cellerier's avatar
Antoine Cellerier committed
672
            }
673
            clear_children( browser );
674
            browser.appendChild( pos );
Antoine Cellerier's avatar
Antoine Cellerier committed
675
        }
676
        else
Antoine Cellerier's avatar
Antoine Cellerier committed
677
        {
678
            /*alert( 'Error! HTTP server replied: ' + req.status );*/
Antoine Cellerier's avatar
Antoine Cellerier committed
679 680 681 682
        }
    }
}

683 684 685
/**********************************************************************
 * Input dialog functions
 *********************************************************************/
Antoine Cellerier's avatar
Antoine Cellerier committed
686 687 688 689 690 691 692 693 694 695 696
function hide_input( )
{
    document.getElementById( 'input_file' ).style.display = 'none';
    document.getElementById( 'input_disc' ).style.display = 'none';
    document.getElementById( 'input_network' ).style.display = 'none';
}

/* update the input MRL using data from the input file helper */
/* FIXME ... subs support */
function update_input_file()
{
697
    var mrl = document.getElementById( 'input_mrl' );
Antoine Cellerier's avatar
Antoine Cellerier committed
698 699 700 701 702 703 704

    mrl.value = value( 'input_file_filename' );
}

/* update the input MRL using data from the input disc helper */
function update_input_disc()
{
Antoine Cellerier's avatar
Antoine Cellerier committed
705 706 707
    var mrl     = document.getElementById( 'input_mrl' );
    var type    = radio_value( "input_disc_type" );
    var device  = value( "input_disc_dev" );
Antoine Cellerier's avatar
Antoine Cellerier committed
708

Antoine Cellerier's avatar
Antoine Cellerier committed
709 710 711 712
    var title   = check_and_replace_int( 'input_disc_title', 0 );
    var chapter = check_and_replace_int( 'input_disc_chapter', 0 );
    var subs    = check_and_replace_int( 'input_disc_subtrack', '' );
    var audio   = check_and_replace_int( 'input_disc_audiotrack', 0 );
Antoine Cellerier's avatar
Antoine Cellerier committed
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

    mrl.value = "";

    if( type == "dvd" )
    {
        mrl.value += "dvd://";
    }
    else if( type == "dvdsimple" )
    {
        mrl.value += "dvdsimple://";
    }
    else if( type == "vcd" )
    {
        mrl.value += "vcd://";
    }
    else if( type == "cdda" )
    {
        mrl.value += "cdda://";
    }

    mrl.value += device;

    if( title )
    {
        mrl.value += "@"+title;
        if( chapter && type != "cdda" )
            mrl.value += ":"+chapter;
    }

    if( type != "cdda" )
    {
        if( subs != '' )
            mrl.value += " :sub-track="+subs;
        if( audio != '' )
            mrl.value += " :audio-track="+audio;
    }

}

/* update the input MRL using data from the input network helper */
function update_input_net()
{
755 756
    var mrl = document.getElementById( 'input_mrl' );
    var type = radio_value( "input_net_type" );
Antoine Cellerier's avatar
Antoine Cellerier committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    
    check_and_replace_int( 'input_net_udp_port', 1234 );
    check_and_replace_int( 'input_net_udpmcast_port', 1234 );

    mrl.value = "";

    if( type == "udp" )
    {
        mrl.value += "udp://";
        if( checked( 'input_net_udp_forceipv6' ) )
            mrl.value += "[::]";
        if( value( 'input_net_udp_port' ) )
            mrl.value += ":"+value( 'input_net_udp_port' );
    }
    else if( type == "udpmcast" )
    {
        mrl.value += "udp://@"+value( 'input_net_udpmcast_address');
        if( value( 'input_net_udpmcast_port' ) )
            mrl.value += ":"+value( 'input_net_udpmcast_port' );
    }
    else if( type == "http" )
    {
779
        var url = value( 'input_net_http_url' );
Antoine Cellerier's avatar
Antoine Cellerier committed
780 781 782 783 784 785 786 787 788 789
        if( url.substring(0,7) != "http://"
            && url.substring(0,8) != "https://"
            && url.substring(0,6) != "ftp://"
            && url.substring(0,6) != "mms://"
            && url.substring(0,7) != "mmsh://" )
            mrl.value += "http://";
        mrl.value += url;
    }
    else if( type == "rtsp" )
    {
790
        var url = value( 'input_net_rtsp_url' );
Antoine Cellerier's avatar
Antoine Cellerier committed
791 792 793 794 795 796 797 798 799
        if( url.substring(0,7) != "rtsp://" )
            mrl.value += "rtsp://";
        mrl.value += url;
    }

    if( checked( "input_net_timeshift" ) )
        mrl.value += " :access-filter=timeshift";
}

800 801 802 803 804 805
/**********************************************************************
 * Sout dialog functions
 *********************************************************************/
/* toggle show the full sout interface */
function toggle_show_sout_helper()
{
806 807
    var element = document.getElementById( "sout_helper" );
    if( element.style.display == 'block' )
808 809 810 811 812 813 814 815 816 817 818
    {
        element.style.display = 'none';
        document.getElementById( "sout_helper_toggle" ).value = 'Full sout interface';
    }
    else
    {
        element.style.display = 'block';
        document.getElementById( "sout_helper_toggle" ).value = 'Hide sout interface';
    }
}

Antoine Cellerier's avatar
Antoine Cellerier committed
819 820 821
/* update the sout MRL using data from the sout_helper */
function update_sout()
{
822
    var mrl = document.getElementById( 'sout_mrl' );
Antoine Cellerier's avatar
Antoine Cellerier committed
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    mrl.value = "";

    check_and_replace_int( 'sout_http_port', 8080 );
    check_and_replace_int( 'sout_mmsh_port', 8080 );
    check_and_replace_int( 'sout_rtp_port', 1234 );
    check_and_replace_int( 'sout_udp_port', 1234 );
    check_and_replace_int( 'sout_ttl', 1 );

    if( checked( 'sout_soverlay' ) )
    {
        disable( 'sout_scodec' );
        disable( 'sout_sub' );
    }
    else
    {
        enable( 'sout_scodec' );
        enable( 'sout_sub' );
    }

842 843
    var transcode =  checked( 'sout_vcodec_s' ) || checked( 'sout_acodec_s' )
                  || checked( 'sout_sub' )      || checked( 'sout_soverlay' );
Antoine Cellerier's avatar
Antoine Cellerier committed
844 845 846 847

    if( transcode )
    {
        mrl.value += ":sout=#transcode{";
848
        var alot = false; /* alot == at least one transcode */
Antoine Cellerier's avatar
Antoine Cellerier committed
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
        if( checked( 'sout_vcodec_s' ) )
        {
            mrl.value += "vcodec="+value( 'sout_vcodec' )+",vb="+value( 'sout_vb' )+",scale="+value( 'sout_scale' );
            alot = true;
        }
        if( checked( 'sout_acodec_s' ) )
        {
            if( alot ) mrl.value += ",";
            mrl.value += "acodec="+value( 'sout_acodec' )+",ab="+value( 'sout_ab' );
            if( value( 'sout_channels' ) )
                mrl.value += ",channels="+value( 'sout_channels' );
            alot = true;
        }
        if( checked( 'sout_soverlay' ) )
        {
            if( alot ) mrl.value += ",";
            mrl.value += "soverlay";
            alot = true;
        }
        else if( checked( 'sout_sub' ) )
        {
            if( alot ) mrl.value += ",";
            mrl.value += "scodec="+value( 'sout_scodec' );
            alot = true;
        }
Antoine Cellerier's avatar
Antoine Cellerier committed
874 875
        mrl.value += value( 'sout_extra' );
            
Antoine Cellerier's avatar
Antoine Cellerier committed
876 877 878
        mrl.value += "}";
    }

879 880 881
    var output = checked( 'sout_display' ) + checked( 'sout_file' )
               + checked( 'sout_http' )    + checked( 'sout_mmsh' )
               + checked( 'sout_rtp' )     + checked( 'sout_udp' );
Antoine Cellerier's avatar
Antoine Cellerier committed
882 883 884 885 886 887 888

    if( output )
    {
        if( transcode )
            mrl.value += ":";
        else
            mrl.value += ":sout=#";
889 890 891
        var aloo = false; /* aloo == at least one output */
        var mux = radio_value( 'sout_mux' );
        var ttl = parseInt( value( 'sout_ttl' ) );
Antoine Cellerier's avatar
Antoine Cellerier committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
        if( output > 1 ) mrl.value += "duplicate{";
        if( checked( 'sout_display' ) )
        {
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "display";
            aloo = true;
        }
        if( checked( 'sout_file' ) )
        {
            if( aloo ) mrl.value += ",";
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "std{access=file,mux="+mux+",dst="+value( 'sout_file_filename' )+"}";
            aloo = true;
        }
        if( checked( 'sout_http' ) )
        {
            if( aloo ) mrl.value += ",";
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "std{access=http,mux="+mux+",dst="+value( 'sout_http_addr' );
            if( value( 'sout_http_port' ) )
                mrl.value += ":"+value( 'sout_http_port' );
            mrl.value += "}";
            aloo = true;
        }
        if( checked( 'sout_mmsh' ) )
        {
            if( aloo ) mrl.value += ",";
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "std{access=mmsh,mux="+mux+",dst="+value( 'sout_mmsh_addr' );
            if( value( 'sout_mmsh_port' ) )
                mrl.value += ":"+value( 'sout_mmsh_port' );
            mrl.value += "}";
            aloo = true;
        }
        if( checked( 'sout_rtp' ) )
        {
            if( aloo ) mrl.value += ",";
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "std{access=rtp";
            if( ttl ) mrl.value += "{ttl="+ttl+"}";
            mrl.value += ",mux="+mux+",dst="+value( 'sout_rtp_addr' );
            if( value( 'sout_rtp_port' ) )
                mrl.value += ":"+value( 'sout_rtp_port' );
            if( checked( 'sout_sap' ) )
            {
                mrl.value += ",sap";
                if( value( 'sout_sap_group' ) != '' )
                {
                    mrl.value += ",group=\""+value( 'sout_sap_group' )+"\"";
                }
                mrl.value += ",name=\""+value( 'sout_sap_name' )+"\"";
            }
            mrl.value += "}";
            aloo = true;
        }
        if( checked( 'sout_udp' ) )
        {
            if( aloo ) mrl.value += ",";
            if( output > 1 ) mrl.value += "dst="
            mrl.value += "std{access=udp";
            if( ttl ) mrl.value += "{ttl="+ttl+"}";
            mrl.value += ",mux="+mux+",dst="+value( 'sout_udp_addr' );
            if( value('sout_udp_port' ) )
                mrl.value += ":"+value( 'sout_udp_port' );
            if( checked( 'sout_sap' ) )
            {
                mrl.value += ",sap";
                if( value( 'sout_sap_group' ) != '' )
                {
                    mrl.value += ",group=\""+value( 'sout_sap_group' )+"\"";
                }
                mrl.value += ",name=\""+value( 'sout_sap_name' )+"\"";
            }
            mrl.value += "}";
            aloo = true;
        }
        if( output > 1 ) mrl.value += "}";
    }

    if( ( transcode || output ) && checked( 'sout_all' ) )
        mrl.value += " :sout-all";
}

/* reset sout mrl value */
function reset_sout()
{
    document.getElementById('sout_mrl').value = value('sout_old_mrl');
}

981
/* save sout mrl value */
Antoine Cellerier's avatar
Antoine Cellerier committed
982 983 984 985 986
function save_sout()
{
    document.getElementById('sout_old_mrl').value = value('sout_mrl');
}

987 988 989 990
/**********************************************************************
 * Browser dialog functions
 *********************************************************************/
/* only browse() should be called directly */
991 992 993
function browse( dest )
{
    document.getElementById( 'browse_dest' ).value = dest;
994
    document.getElementById( 'browse_lastdir' ).value;
995 996 997 998 999 1000
    browse_dir( document.getElementById( 'browse_lastdir' ).value );
    show( 'browse' );
}
function browse_dir( dir )
{
    document.getElementById( 'browse_lastdir' ).value = dir;
1001
    loadXMLDoc( 'requests/browse.xml?dir='+escape(dir), parse_browse_dir );
1002 1003 1004
}
function browse_path( p )
{
1005
    document.getElementById( value( 'browse_dest' ) ).value = p;
1006
    hide( 'browse' );
1007
    document.getElementById( value( 'browse_dest' ) ).focus();
1008
}
1009 1010 1011 1012 1013

/**********************************************************************
 * Periodically update stuff in the interface
 *********************************************************************/
function loop_refresh_status()
1014
{
1015 1016
    setTimeout( 'loop_refresh_status()', 1000 );
    update_status();
1017
}
1018 1019
function loop_refresh_playlist()
{
1020
    /* setTimeout( 'loop_refresh_playlist()', 10000 ); */
1021 1022 1023 1024
    update_playlist();
}
function loop_refresh()
{
1025 1026
    setTimeout( 'loop_refresh_status()', 1 );
    setTimeout( 'loop_refresh_playlist()', 1 );
1027 1028
}