vlm.c 62.8 KB
Newer Older
1
/*****************************************************************************
2
 * vlm.c: VLM interface plugin
3 4
 *****************************************************************************
 * Copyright (C) 2000, 2001 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Authors: Simon Latapie <garf@videolan.org>
 *          Laurent Aimar <fenrir@videolan.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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/intf.h>
#include <vlc/input.h>

34 35 36 37
#ifdef HAVE_TIME_H
#   include <time.h>                                              /* ctime() */
#endif

38
#include "vlc_vlm.h"
39

40 41 42
/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
43 44
static char          *vlm_Save( vlm_t * );
static int            vlm_Load( vlm_t *, char *);
45 46 47
static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
static vlm_message_t *vlm_Help( vlm_t *, char * );

48 49 50 51 52 53
static vlm_media_t *vlm_MediaNew    ( vlm_t *, char *, int );
static int          vlm_MediaDelete ( vlm_t *, vlm_media_t *, char * );
static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
static int          vlm_MediaSetup  ( vlm_media_t *, char *, char * );
static int          vlm_MediaControl( vlm_t *, vlm_media_t *, char *, char * );

54 55
static vlm_message_t* vlm_MessageNew( char * , char * );
static vlm_message_t* vlm_MessageAdd( vlm_message_t*, vlm_message_t* );
56 57

static vlm_schedule_t *vlm_ScheduleNew( vlm_t *, char *);
58 59
static int             vlm_ScheduleDelete( vlm_t *, vlm_schedule_t *, char *);
static int             vlm_ScheduleSetup( vlm_schedule_t *, char *, char *);
60 61
static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char *);

62

63
static int ExecuteCommand( vlm_t *, char * , vlm_message_t **);
64 65 66

static int Manage( vlc_object_t* );

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/*****************************************************************************
 * vlm_New:
 *****************************************************************************/
vlm_t *__vlm_New ( vlc_object_t *p_this )
{
    vlc_value_t lockval;
    vlm_t *vlm = NULL;

    /* to be sure to avoid multiple creation */
    var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
    var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );

    if( !(vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
    {
        msg_Info( p_this, "creating vlm" );
        if( ( vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
        {
            vlc_mutex_unlock( lockval.p_address );
            return NULL;
        }

        vlc_mutex_init( p_this->p_vlc, &vlm->lock );
        vlm->i_media      = 0;
        vlm->media        = NULL;
        vlm->i_schedule   = 0;
        vlm->schedule     = NULL;

        vlc_object_yield( vlm );
        vlc_object_attach( vlm, p_this->p_vlc );
    }
    vlc_mutex_unlock( lockval.p_address );


    if( vlc_thread_create( vlm, "vlm thread",
                           Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
        vlc_mutex_destroy( &vlm->lock );
        vlc_object_destroy( vlm );
        return NULL;
    }
    return vlm;
}

/*****************************************************************************
 * vlm_Delete:
 *****************************************************************************/
void vlm_Delete( vlm_t *vlm )
{
    vlc_value_t lockval;
    int i;

    var_Get( vlm->p_libvlc, "vlm_mutex", &lockval );
    vlc_mutex_lock( lockval.p_address );

    vlc_object_release( vlm );

    if( vlm->i_refcount > 0 )
    {
        vlc_mutex_unlock( lockval.p_address );
        return;
    }

    vlm->b_die = VLC_TRUE;
    vlc_thread_join( vlm );

    vlc_mutex_destroy( &vlm->lock );

    for( i = 0; i < vlm->i_media; i++ )
    {
        vlm_media_t *media = vlm->media[i];

        vlm_MediaDelete( vlm, media, NULL );
    }

    if( vlm->media ) free( vlm->media );

    for( i = 0; i < vlm->i_schedule; i++ )
    {
        vlm_ScheduleDelete( vlm, vlm->schedule[i], NULL );
    }

    if( vlm->schedule ) free( vlm->schedule );

    vlc_object_detach( vlm );
    vlc_object_destroy( vlm );
    vlc_mutex_unlock( lockval.p_address );
}

/*****************************************************************************
 * vlm_ExecuteCommand:
 *****************************************************************************/
int vlm_ExecuteCommand( vlm_t *vlm, char *command, vlm_message_t **message)
{
    int result;

    vlc_mutex_lock( &vlm->lock );
    result = ExecuteCommand( vlm, command, message );
    vlc_mutex_unlock( &vlm->lock );

    return result;
}

/*****************************************************************************
 *
 *****************************************************************************/
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
#if 1
static char *FindEndCommand( char *psz )
{
    char *s_sent = psz;

    switch( *s_sent )
    {
        case '\"':
        {
            s_sent++;

            while( ( *s_sent != '\"' ) && ( *s_sent != '\0' ) )
            {
                if( *s_sent == '\'' )
                {
                    s_sent = FindEndCommand( s_sent );

                    if( s_sent == NULL )
                    {
                        return NULL;
                    }
                }
                else
                {
                    s_sent++;
                }
            }

            if( *s_sent == '\"' )
            {
                s_sent++;
                return s_sent;
            }
            else  /* *s_sent == '\0' , which means the number of " is incorrect */
            {
                return NULL;
            }
            break;
        }
        case '\'':
        {
            s_sent++;

            while( ( *s_sent != '\'' ) && ( *s_sent != '\0' ) )
            {
                if( *s_sent == '\"' )
                {
                    s_sent = FindEndCommand( s_sent );

                    if( s_sent == NULL )
                    {
                        return NULL;
                    }
                }
                else
                {
                    s_sent++;
                }
            }

            if( *s_sent == '\'' )
            {
                s_sent++;
                return s_sent;
            }
            else  /* *s_sent == '\0' , which means the number of ' is incorrect */
            {
                return NULL;
            }
            break;
        }
        default: /* now we can look for spaces */
        {
            while( ( *s_sent != ' ' ) && ( *s_sent != '\0' ) )
            {
                if( ( *s_sent == '\'' ) || ( *s_sent == '\"' ) )
                {
                    s_sent = FindEndCommand( s_sent );
                }
                else
                {
                    s_sent++;
                }
            }
            return s_sent;
        }
    }
}
#else
static char *FindEndCommand( char *psz )
{
    char *s_sent = psz;

    while( *psz &&
            *psz != ' ' && *psz != '\t' &&
            *psz != '\n' && *psz != '\r' )
    {
        if( *psz == '\'' || *psz == '"' )
        {
            char d = *psz++;

            while( *psz && *psz != d && *psz != *psz != '\n' && *psz != '\r' )
            {
                if( ( d == '\'' && *psz == '"' ) ||
                    ( d == '"' && *psz == '\'' ) )
                {
                    psz = FindEndCommand( psz );
                }
            }
            if( psz != d )
            {
                return NULL;
            }
        }
        psz++;
    }
}
#endif


/* Execute a command which ends by '\0' (string) */
294
static int ExecuteCommand( vlm_t *vlm, char *command , vlm_message_t **p_message)
295 296 297 298 299 300
{
    int i_return = 0;
    int i_command = 0;
    char **p_command = NULL;
    char *cmd = command;
    int i;
301
    vlm_message_t * message = NULL;
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

    /* First, parse the line and cut it */
    while( *cmd != '\0' )
    {

        if( *cmd == ' ' )
        {
            cmd++;
        }
        else
        {
            char *p_temp;
            int   i_temp;

            p_temp = FindEndCommand( cmd );

            if( p_temp == NULL )
            {
                i_return = 1;
                goto end_seq;
            }

            i_temp = p_temp - cmd;

            p_command = realloc( p_command , (i_command + 1) * sizeof( char* ) );
            p_command[ i_command ] = malloc( (i_temp + 1) * sizeof( char ) ); // don't forget the '\0'
            strncpy( p_command[ i_command ] , cmd , i_temp );
            (p_command[ i_command ])[ i_temp ] = '\0';

            i_command++;
            cmd = p_temp;
        }
    }

    /* And then Interpret it */

    if( i_command == 0 )
    {
340
        message = vlm_MessageNew( "" , NULL );
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        i_return = 0;
        goto end_seq;
    }

    if( strcmp(p_command[0] , "segfault") == 0 )
    {
        /* the only command we really need */
        *((int *)NULL) = 42;
    }
    else if( strcmp(p_command[0] , "new") == 0 )
    {
        if( i_command >= 3 )
        {
            int i_type;
            vlm_media_t *media;
            vlm_schedule_t *schedule;

            if( strcmp(p_command[2] , "schedule") == 0 )
            {
                /* new vlm_schedule */
                if( vlm_ScheduleSearch( vlm , p_command[1] ) != NULL || strcmp(p_command[1] , "schedule") == 0 )
                {
363 364 365 366
                    char *error_message = malloc( strlen(p_command[1]) + strlen(" is already used") + 1 );
                    sprintf( error_message, "%s is already used" , p_command[1] );
                    message = vlm_MessageNew( "new" , error_message );
                    free( error_message );
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
                    i_return = 1;
                    goto end_seq;
                }

                schedule = vlm_ScheduleNew( vlm , p_command[1] );


                if( i_command > 3 ) // hey, there are properties
                {
                    for( i = 3 ; i < i_command ; i++ )
                    {
                        if( strcmp( p_command[i] , "enabled" ) == 0 || strcmp( p_command[i] , "disabled" ) == 0 )
                        {
                            vlm_ScheduleSetup( schedule, p_command[i], NULL );
                        }
                        /* Beware: evrything behind append is considered as command line */
                        else if( strcmp( p_command[i] , "append" ) == 0 )
                        {
                            i++;

                            if( i < i_command )
                            {
                                int j;
                                for( j = (i + 1); j < i_command; j++ )
                                {
                                    p_command[i] = realloc( p_command[i], strlen(p_command[i]) + strlen(p_command[j]) + 1 + 1);
                                    strcat( p_command[i], " " );
                                    strcat( p_command[i], p_command[j] );
                                }

                                vlm_ScheduleSetup( schedule, p_command[i - 1], p_command[i] );
                            }
                            i = i_command;
                        }
                        else
                        {
                            if( (i+1) < i_command )
                            {
                                vlm_ScheduleSetup( schedule, p_command[i], p_command[i+1] );
                                i++;
                            }
                            else
                            {
                                vlm_ScheduleDelete( vlm, schedule, NULL );
411
                                message = vlm_MessageNew( p_command[i], "Wrong properties syntax" );
412 413 414 415 416 417
                                i_return = 1;
                                goto end_seq;
                            }
                        }
                    }

418
                    message = vlm_MessageNew( "new" , NULL );
419 420 421 422
                    i_return = 0;
                    goto end_seq;
                }

423
                message = vlm_MessageNew( "new" , NULL );
424 425 426 427 428 429 430 431 432 433 434 435 436 437
                i_return = 0;
                goto end_seq;
            }

            if( strcmp(p_command[2] , "vod") == 0 )
            {
                i_type = VOD_TYPE;
            }
            else if( strcmp(p_command[2] , "broadcast") == 0 )
            {
                i_type = BROADCAST_TYPE;
            }
            else
            {
438 439 440 441
                char *error_message = malloc( strlen(p_command[1]) + strlen(": Choose between vod or broadcast") + 1 );
                sprintf( error_message, "%s: Choose between vod or broadcast" , p_command[1] );
                message = vlm_MessageNew( "new" , error_message );
                free( error_message );
442 443 444 445 446 447 448
                i_return = 1;
                goto end_seq;
            }

            /* new vlm_media */
            if( vlm_MediaSearch( vlm , p_command[1] ) != NULL || strcmp(p_command[1] , "media") == 0 )
            {
449 450 451 452
                char *error_message = malloc( strlen(p_command[1]) + strlen(" is already used") + 1 );
                sprintf( error_message, "%s is already used" , p_command[1] );
                message = vlm_MessageNew( "new" , error_message );
                free( error_message );
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
                i_return = 1;
                goto end_seq;
            }

            media = vlm_MediaNew( vlm , p_command[1] , i_type );

            if( i_command > 3 ) // hey, there are properties
            {
                for( i = 3 ; i < i_command ; i++ )
                {
                    if( strcmp( p_command[i] , "enabled" ) == 0 || strcmp( p_command[i] , "disabled" ) == 0 )
                    {
                        vlm_MediaSetup( media, p_command[i], NULL );
                    }
                    else
                    {
                        if( (i+1) < i_command )
                        {
                            vlm_MediaSetup( media, p_command[i], p_command[i+1] );
                            i++;
                        }
                        else
                        {
                            vlm_MediaDelete( vlm, media, NULL );
477
                            message = vlm_MessageNew( p_command[i] , "Wrong properties syntax" );
478 479 480 481 482 483
                            i_return = 1;
                            goto end_seq;
                        }
                    }
                }

484
                message = vlm_MessageNew( "new" , NULL );
485 486 487 488
                i_return = 0;
                goto end_seq;
            }

489
            message = vlm_MessageNew( "new" , NULL );
490 491 492 493 494
            i_return = 0;
            goto end_seq;
        }
        else
        {
495
            message = vlm_MessageNew( "new" , "Wrong command syntax" );
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "del") == 0 )
    {
        if( i_command >= 2 )
        {
            vlm_media_t *media;
            vlm_schedule_t *schedule;

            media = vlm_MediaSearch( vlm , p_command[1] );
            schedule = vlm_ScheduleSearch( vlm , p_command[1] );

            if( schedule != NULL )
            {
                vlm_ScheduleDelete( vlm, schedule, NULL );
513
                message = vlm_MessageNew( "del" , NULL );
514 515 516 517 518 519
                i_return = 0;
                goto end_seq;
            }
            else if( media != NULL )
            {
                vlm_MediaDelete( vlm, media, NULL );
520
                message = vlm_MessageNew( "del" , NULL );
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
                i_return = 0;
                goto end_seq;
            }
            else if( strcmp(p_command[1] , "media") == 0 )
            {
                for( i = 0; i < vlm->i_media; i++ )
                {
                    vlm_MediaDelete( vlm, vlm->media[i], NULL );
                }
            }
            else if( strcmp(p_command[1] , "schedule") == 0 )
            {
                for( i = 0; i < vlm->i_schedule; i++ )
                {
                    vlm_ScheduleDelete( vlm, vlm->schedule[i], NULL );
                }
            }
            else if( strcmp(p_command[1] , "all") == 0 )
            {
                for( i = 0; i < vlm->i_media; i++ )
                {
                    vlm_MediaDelete( vlm, vlm->media[i], NULL );
                }

                for( i = 0; i < vlm->i_schedule; i++ )
                {
                    vlm_ScheduleDelete( vlm, vlm->schedule[i], NULL );
                }
549 550
                i_return = 0;
                goto end_seq;
551 552 553
            }
            else
            {
554 555 556 557
                char *error_message = malloc( strlen(p_command[1]) + strlen(": media unknown") + 1 );
                sprintf( error_message, "%s: media unknown" , p_command[1] );
                message = vlm_MessageNew( "del" , error_message );
                free( error_message );
558 559 560 561 562 563
                i_return = 1;
                goto end_seq;
            }
        }
        else
        {
564
            message = vlm_MessageNew( "setup" , "Wrong command syntax" );
565 566 567 568 569 570 571 572
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "show") == 0 )
    {
        if( i_command == 1 )
        {
573
            message = vlm_Show( vlm, NULL , NULL, NULL );
574 575 576 577 578 579 580 581 582 583 584 585 586
            i_return = 0;
            goto end_seq;
        }
        else if( i_command == 2 )
        {
            vlm_media_t *media;
            vlm_schedule_t *schedule;

            media = vlm_MediaSearch( vlm , p_command[1] );
            schedule = vlm_ScheduleSearch( vlm , p_command[1] );

            if( schedule != NULL )
            {
587
                message = vlm_Show( vlm, NULL, schedule, NULL );
588 589 590
            }
            else if( media != NULL )
            {
591
                message = vlm_Show( vlm, media, NULL, NULL );
592 593 594
            }
            else
            {
595
                message = vlm_Show( vlm, NULL, NULL, p_command[1] );
596 597 598 599 600 601 602
            }

            i_return = 0;
            goto end_seq;
        }
        else
        {
603
            message = vlm_MessageNew( "show" , "Wrong command syntax" );
604 605 606 607 608 609 610 611
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "help") == 0 )
    {
        if( i_command == 1 )
        {
612
            message = vlm_Help( vlm, NULL );
613 614 615 616 617
            i_return = 0;
            goto end_seq;
        }
        else
        {
618
            message = vlm_MessageNew( "help" , "Wrong command syntax" );
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "setup") == 0 )
    {
        if( i_command >= 2 )
        {
            vlm_media_t *media;
            vlm_schedule_t *schedule;

            media = vlm_MediaSearch( vlm , p_command[1] );
            schedule = vlm_ScheduleSearch( vlm , p_command[1] );

            if( schedule != NULL )
            {
                for( i = 2 ; i < i_command ; i++ )
                {
                    if( strcmp( p_command[i] , "enabled" ) == 0 || strcmp( p_command[i] , "disabled" ) == 0 )
                    {
                        vlm_ScheduleSetup( schedule, p_command[i], NULL );
                    }
                    /* Beware: evrything behind append is considered as command line */
                    else if( strcmp( p_command[i] , "append" ) == 0 )
                    {
                        i++;

                        if( i < i_command )
                        {
                            int j;
                            for( j = (i + 1); j < i_command; j++ )
                            {
                                p_command[i] = realloc( p_command[i], strlen(p_command[i]) + strlen(p_command[j]) + 1 + 1);
                                strcat( p_command[i], " " );
                                strcat( p_command[i], p_command[j] );
                            }

                            vlm_ScheduleSetup( schedule, p_command[i - 1], p_command[i] );
                        }
                        i = i_command;
                    }
                    else
                    {
                        if( (i+1) < i_command )
                        {
                            vlm_ScheduleSetup( schedule, p_command[i], p_command[i+1] );
                            i++;
                        }
                        else
                        {
                            vlm_ScheduleDelete( vlm, schedule, NULL );
670
                            message = vlm_MessageNew( "setup" , "Wrong properties syntax" );
671 672 673 674 675 676
                            i_return = 1;
                            goto end_seq;
                        }
                    }
                }

677
                message = vlm_MessageNew( "setup" , NULL );
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
                i_return = 0;
                goto end_seq;
            }
            else if( media != NULL )
            {
                for( i = 2 ; i < i_command ; i++ )
                {
                    if( strcmp( p_command[i] , "enabled" ) == 0 || strcmp( p_command[i] , "disabled" ) == 0 )
                    {   /* only one argument */
                        vlm_MediaSetup( media, p_command[i], NULL );
                    }
                    else if( strcmp( p_command[i] , "loop" ) == 0 || strcmp( p_command[i] , "unloop" ) == 0 )
                    {
                        if( media->i_type != BROADCAST_TYPE )
                        {
693
                            message = vlm_MessageNew( "setup" , "lool only available for broadcast" );
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
                            i_return = 1;
                            goto end_seq;
                        }
                        else
                        {
                            vlm_MediaSetup( media, p_command[i], NULL );
                        }
                    }
                    else
                    {
                        if( (i+1) < i_command )
                        {
                            vlm_MediaSetup( media, p_command[i], p_command[i+1] );
                            i++;
                        }
                        else
                        {
                            vlm_MediaDelete( vlm, media, NULL );
712
                            message = vlm_MessageNew( "setup" , "Wrong properties syntax" );
713 714 715 716 717 718
                            i_return = 1;
                            goto end_seq;
                        }
                    }
                }

719
                message = vlm_MessageNew( "setup" , NULL );
720 721 722 723 724
                i_return = 0;
                goto end_seq;
            }
            else
            {
725 726 727 728
                char *error_message = malloc( strlen(p_command[1]) + strlen(" unknown") + 1 );
                sprintf( error_message, "%s unknown" , p_command[1] );
                message = vlm_MessageNew( "setup" , error_message );
                free( error_message );
729 730 731 732 733 734 735
                i_return = 1;
                goto end_seq;
            }

        }
        else
        {
736
            message = vlm_MessageNew( "setup" , "Wrong command syntax" );
737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "control") == 0 )
    {

        if( i_command >= 3 )
        {
            vlm_media_t *media;

            media = vlm_MediaSearch( vlm , p_command[1] );

            if( media == NULL )
            {
752 753 754 755
                char *error_message = malloc( strlen(p_command[1]) + strlen(": media unknown") + 1 );
                sprintf( error_message, "%s: media unknown" , p_command[1] );
                message = vlm_MessageNew( "control" , error_message );
                free( error_message );
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
                i_return = 1;
                goto end_seq;
            }
            else
            {
                char *psz_args;

                if( i_command >= 4 )
                {
                    psz_args = p_command[3];
                }
                else
                {
                    psz_args = NULL;
                }

                /* for now */
                vlm_MediaControl( vlm, media, p_command[2], psz_args );
774
                message = vlm_MessageNew( "control" , NULL );
775 776 777 778 779 780
                i_return = 0;
                goto end_seq;
            }
        }
        else
        {
781
            message = vlm_MessageNew( "control" , "Wrong command syntax" );
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "save") == 0 )
    {
        if( i_command == 2 )
        {
            FILE *file;


            file = fopen( p_command[1], "w" );

            if( file == NULL )
            {
797
                message = vlm_MessageNew( "save" , "Unable to save file" );
798 799 800 801 802 803 804 805 806 807 808 809
                i_return = 1;
                goto end_seq;
            }
            else
            {
                char *save;

                save = vlm_Save( vlm );

                fwrite( save, strlen( save ) , 1 , file );
                fclose( file );
                free( save );
810
                message = vlm_MessageNew( "save" , NULL );
811 812 813 814 815 816
                i_return = 0;
                goto end_seq;
            }
        }
        else
        {
817
            message = vlm_MessageNew( "save" , "Wrong command" );
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
            i_return = 1;
            goto end_seq;
        }
    }
    else if( strcmp(p_command[0] , "load") == 0 )
    {

        if( i_command == 2 )
        {
            FILE *file;

            file = fopen( p_command[1], "r" );

            if( file == NULL )
            {
833
                message = vlm_MessageNew( "load" , "Unable to load file" );
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
                i_return = 1;
                goto end_seq;
            }
            else
            {
                int64_t size;
                char *buffer;

                if( fseek( file, 0, SEEK_END) == 0 )
                {
                    size = ftell( file );
                    fseek( file, 0, SEEK_SET);
                    buffer = malloc( size + 1 );
                    fread( buffer, 1, size, file);
                    buffer[ size ] = '\0';
                    if( vlm_Load( vlm, buffer ) )
                    {
                        free( buffer );
852
                        message = vlm_MessageNew( "load" , "error while loading file" );
853 854 855 856 857 858 859
                        i_return = 1;
                        goto end_seq;
                    }
                    free( buffer );
                }
                else
                {
860
                    message = vlm_MessageNew( "load" , "read file error" );
861 862 863 864
                    i_return = 1;
                    goto end_seq;
                }
                fclose( file );
865
                message = vlm_MessageNew( "load" , NULL );
866 867 868 869 870 871
                i_return = 0;
                goto end_seq;
            }
        }
        else
        {
872
            message = vlm_MessageNew( "load" , "Wrong command" );
873 874 875 876 877 878
            i_return = 1;
            goto end_seq;
        }
    }
    else
    {
879
        message = vlm_MessageNew( p_command[0] , "Unknown comand" );
880 881 882 883 884 885 886 887 888 889 890
        i_return = 1;
        goto end_seq;
    }

end_seq:

    for( i = 0 ; i < i_command ; i++ )
    {
        free( p_command[i] );
    }

891
    *p_message = message;
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

    return i_return;
}


static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
{
    int i;

    for( i = 0; i < vlm->i_media; i++ )
    {
        if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
        {
            return vlm->media[i];
        }
    }

    return NULL;
}


static vlm_media_t *vlm_MediaNew( vlm_t *vlm , char *psz_name, int i_type )
{
    vlm_media_t *media= malloc( sizeof( vlm_media_t ));

    media->psz_name = strdup( psz_name );
    media->b_enabled = VLC_FALSE;
    media->b_loop = VLC_FALSE;
    media->i_input = 0;
    media->input = NULL;
    media->i_index = 0;
    media->psz_output = NULL;
    media->i_option = 0;
    media->option = NULL;
    media->i_type = i_type;
    media->p_input = NULL;

929 930 931 932 933 934 935 936 937 938
    media->item.psz_uri = strdup( psz_name );
    media->item.psz_name = NULL;
    media->item.i_duration = -1;
    media->item.ppsz_options = NULL;
    media->item.i_options = 0;
    media->item.i_categories = 0;
    media->item.pp_categories = NULL;
    vlc_mutex_init( vlm, &media->item.lock );

    TAB_APPEND( vlm->i_media, vlm->media, media );
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 981 982

    return media;
}


/* for now, simple delete. After, del with options (last arg) */
static int vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
{
    int i;

    if( media == NULL )
    {
        return 1;
    }

    if( media->p_input )
    {
        input_StopThread( media->p_input );

        input_DestroyThread( media->p_input );
        vlc_object_detach( media->p_input );
        vlc_object_destroy( media->p_input );
    }

    TAB_REMOVE( vlm->i_media, vlm->media , media );

    if( vlm->i_media == 0 && vlm->media ) free( vlm->media );

    free( media->psz_name );

    for( i = 0; i < media->i_input; i++ )
    {
        free( media->input[i] );
    }
    if( media->input ) free( media->input );

    if( media->psz_output ) free( media->psz_output );

    for( i = 0; i < media->i_option; i++ )
    {
        free( media->option[i] );
    }
    if(media->option) free( media->option );

983 984 985 986
    if( media->item.psz_uri ) free( media->item.psz_uri );
    if( media->item.psz_name ) free( media->item.psz_name );
    vlc_mutex_destroy( &media->item.lock );
    for( i = 0; i < media->item.i_options; i++ )
987
    {
988
        free( media->item.ppsz_options[i] );
989
    }
990 991
    if( media->item.ppsz_options ) free( media->item.ppsz_options );
    /* FIXME: free the info categories. */
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057

    free( media );

    return 0;
}


static int vlm_MediaSetup( vlm_media_t *media, char *psz_cmd, char *psz_value )
{
    if( strcmp( psz_cmd, "loop" ) == 0 )
    {
        media->b_loop = VLC_TRUE;
    }
    else if( strcmp( psz_cmd, "unloop" ) == 0 )
    {
        media->b_loop = VLC_FALSE;
    }
    else if( strcmp( psz_cmd, "enabled" ) == 0 )
    {
        media->b_enabled = VLC_TRUE;
    }
    else if( strcmp( psz_cmd, "disabled" ) == 0 )
    {
        media->b_enabled = VLC_FALSE;
    }
    else if( strcmp( psz_cmd, "input" ) == 0 )
    {
        char *input;

        if( psz_value != NULL && strlen(psz_value) > 1 && ( psz_value[0] == '\'' || psz_value[0] == '\"' ) 
            && ( psz_value[ strlen(psz_value) - 1 ] == '\'' || psz_value[ strlen(psz_value) - 1 ] == '\"' )  )
        {
            input = malloc( strlen(psz_value) - 1 );

            memcpy( input , psz_value + 1 , strlen(psz_value) - 2 );
            input[ strlen(psz_value) - 2 ] = '\0';
        }
        else
        {
            input = strdup( psz_value );
        }

        TAB_APPEND( media->i_input, media->input, input );
    }
    else if( strcmp( psz_cmd, "output" ) == 0 )
    {
        if( media->psz_output != NULL )
        {
            free( media->psz_output );
        }
        media->psz_output = strdup( psz_value );
    }
    else if( strcmp( psz_cmd, "option" ) == 0 )
    {
        char *option;
        option = strdup( psz_value );

        TAB_APPEND( media->i_option, media->option, option );
    }
    else
    {
        return 1;
    }
    return 0;
}

1058 1059
static int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_name,
                             char *psz_args )
1060 1061 1062 1063 1064
{
    if( strcmp( psz_name, "play" ) == 0 )
    {
        int i;

Laurent Aimar's avatar
Laurent Aimar committed
1065
        if( media->b_enabled == VLC_TRUE && media->i_input > 0 )
1066
        {
1067 1068
            if( psz_args != NULL && sscanf( psz_args, "%d", &i ) == 1 &&
                i < media->i_input )
1069 1070 1071 1072 1073 1074 1075 1076
            {
                media->i_index = i;
            }
            else
            {
                media->i_index = 0;
            }

1077 1078
            /* FIXME!!! we need an input_item_t per input spawned */
            //input_ItemNew( &media->item );
1079 1080
            if( media->psz_output != NULL )
            {
1081 1082 1083 1084 1085 1086
                media->item.ppsz_options = malloc( sizeof( char* ) );
                media->item.ppsz_options[0] =
                    malloc( strlen(media->psz_output) + sizeof("sout=") );
                sprintf( media->item.ppsz_options[0], "sout=%s",
                         media->psz_output );
                media->item.i_options = 1;
1087 1088 1089
            }
            else
            {
1090 1091
                media->item.ppsz_options = NULL;
                media->item.i_options = 0;
1092 1093
            }

1094
            for( i = 0; i < media->i_option; i++ )
1095
            {
1096 1097 1098 1099 1100 1101
                media->item.i_options++;
                media->item.ppsz_options =
                    realloc( media->item.ppsz_options,
                             media->item.i_options * sizeof( char* ) );
                media->item.ppsz_options[ media->item.i_options - 1 ] =
                    strdup( media->option[i] );
1102 1103
            }

1104
            media->p_input = input_CreateThread( vlm, &media->item );
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126

            return 0;
        }
        else
        {
            return 1;
        }
    }
    else if( strcmp( psz_name, "seek" ) == 0 )
    {
        vlc_value_t val;
        float f_percentage;

        if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
        {
            val.f_float = f_percentage / 100.0 ;
            var_Set( media->p_input, "position", val );
            return 0;
        }
    }
    else if( strcmp( psz_name, "stop" ) == 0 )
    {
1127
        /* FIXME!!! we need an input_item_t per input spawned */
Laurent Aimar's avatar
Laurent Aimar committed
1128
        if( media->p_input )
1129
        {
1130 1131 1132 1133 1134
            input_StopThread( media->p_input );
            input_DestroyThread( media->p_input );
            vlc_object_detach( media->p_input );
            vlc_object_destroy( media->p_input );
            media->p_input = NULL;
1135

1136
            //input_ItemDelete( &media->item );
1137
        }
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

        return 0;
    }
    else if( strcmp( psz_name, "pause" ) == 0 )
    {
        vlc_value_t val;

        val.i_int = 0;

        if( media->p_input != NULL )
        {
            var_Get( media->p_input, "state", &val );
        }

        if( val.i_int == PAUSE_S )
        {
            if( media->p_input )
            {
                val.i_int = PLAYING_S;
                var_Set( media->p_input, "state", val );
            }
        }
        else
        {
            if( media->p_input )
            {
                val.i_int = PAUSE_S;
                var_Set( media->p_input, "state", val );
            }
        }
        return 0;
    }

    return 1;
}

1174
static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media, vlm_schedule_t *schedule, char *psz_filter )
1175 1176 1177 1178 1179
{

    if( media != NULL )
    {
        int i;
1180 1181 1182
        vlm_message_t *message;
        vlm_message_t *message_media;
        vlm_message_t *message_child;
1183

1184 1185
        message = vlm_MessageNew( "show" , NULL );
        message_media = vlm_MessageAdd( message , vlm_MessageNew( media->psz_name , NULL ) );
1186 1187 1188

        if( media->i_type == VOD_TYPE )
        {
1189
            vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "vod" ) );
1190 1191 1192
        }
        else
        {
1193
            vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "broadcast" ) );
1194 1195
        }

Laurent Aimar's avatar
Laurent Aimar committed
1196
        vlm_MessageAdd( message_media , vlm_MessageNew( "enabled" , media->b_enabled ? "yes" : "no" ) );
1197

Laurent Aimar's avatar
Laurent Aimar committed
1198
        vlm_MessageAdd( message_media, vlm_MessageNew( "loop" , media->b_loop ? "yes" : "no" ) );
1199

1200
        message_child = vlm_MessageAdd( message_media , vlm_MessageNew( "inputs" , NULL ) );
1201 1202 1203

        for( i=0 ; i < (media->i_input) ; i++ )
        {
1204
            vlm_MessageAdd( message_child , vlm_MessageNew( media->input[i] , NULL ) );
1205 1206
        }

Laurent Aimar's avatar
Laurent Aimar committed
1207
        vlm_MessageAdd( message_media , vlm_MessageNew( "output" , media->psz_output ? media->psz_output : "" ) );
1208

1209
        message_child = vlm_MessageAdd( message_media , vlm_MessageNew( "options" , NULL ) );
1210 1211 1212

        for( i=0 ; i < (media->i_option) ; i++ )
        {
1213
            vlm_MessageAdd( message_child , vlm_MessageNew( media->option[i] , NULL ) );
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
        }

        if( media->p_input != NULL )
        {
            vlc_value_t val;

            var_Get( media->p_input, "state", &val );

            if( val.i_int == PLAYING_S )
            {
1224
                vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "playing" ) );
1225 1226 1227
            }
            else if( val.i_int == PAUSE_S )
            {
1228
                vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "paused" ) );
1229 1230 1231
            }
            else
            {
1232
                vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "stop" ) );
1233 1234 1235 1236
            }
        }
        else
        {
1237
            vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "stop" ) );
1238 1239
        }

1240
        return message;
1241 1242 1243 1244 1245

    }
    else if( schedule != NULL )
    {
        int i;
1246 1247 1248
        vlm_message_t *message;
        vlm_message_t *message_schedule;
        vlm_message_t *message_child;
1249
        char buffer[100];
1250

1251 1252
        message = vlm_MessageNew( "show" , NULL );
        message_schedule = vlm_MessageAdd( message , vlm_MessageNew( schedule->psz_name , NULL ) );
1253

1254
        vlm_MessageAdd( message_schedule , vlm_MessageNew( "type" , "schedule" ) );
1255 1256 1257

        if( schedule->b_enabled == VLC_TRUE )
        {
1258
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "enabled" , "yes" ) );
1259 1260 1261
        }
        else
        {
1262
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "enabled" , "no" ) );
1263 1264 1265 1266
        }

        if( schedule->i_date != 0 )
        {
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
            struct tm date;
            time_t i_time = (time_t) ( schedule->i_date / 1000000 );
            char *psz_date = malloc( strlen( "//-::" ) + 14 );

#ifdef HAVE_LOCALTIME_R
            localtime_r( &i_time, &date);
#else
            struct tm *p_date = localtime( &i_time );
            date = *p_date;
#endif

            sprintf( psz_date, "%d/%d/%d-%d:%d:%d",
                          date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
                          date.tm_hour, date.tm_min, date.tm_sec );

/*
1283 1284
            time_t i_time = schedule->i_date / (int64_t)1000000;

1285 1286
#ifdef HAVE_CTIME_R
            char psz_date[500];
1287
            ctime_r( &i_time, psz_date );
1288 1289 1290
#else
            char *psz_date = ctime( &i_time );
#endif
1291
*/
1292
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "date" , psz_date ) );
1293
            free( psz_date );
1294 1295 1296
        }
        else
        {
1297
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "date" , "now" ) );
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
        }

        if( schedule->i_period != 0 )
        {
            time_t i_time = (time_t) ( schedule->i_period / 1000000 );
            struct tm date;

            date.tm_sec = (int)( i_time % 60 );
            i_time = i_time / 60;
            date.tm_min = (int)( i_time % 60 );
            i_time = i_time / 60;
            date.tm_hour = (int)( i_time % 24 );
            i_time = i_time / 24;
            date.tm_mday = (int)( i_time % 30 );
            i_time = i_time / 30;
            /* okay, okay, months are not always 30 days long */
            date.tm_mon = (int)( i_time % 12 );
            i_time = i_time / 12;
            date.tm_year = (int)i_time;

1318 1319
            sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
                     date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1320

1321
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "period" , buffer ) );
1322 1323 1324 1325
        }
        else
        {
            vlm_MessageAdd( message_schedule , vlm_MessageNew( "period" , "0" ) );
1326 1327
        }

1328 1329 1330
        sprintf( buffer, "%d" , schedule->i_repeat );
        vlm_MessageAdd( message_schedule , vlm_MessageNew( "repeat" , buffer ) );

1331
        message_child = vlm_MessageAdd( message_schedule , vlm_MessageNew( "commands" , NULL ) );
1332 1333 1334

        for( i=0 ; i < (schedule->i_command) ; i++ )
        {
1335
           vlm_MessageAdd( message_child , vlm_MessageNew( schedule->command[i] , NULL ) );
1336 1337
        }

1338
        return message;
1339 1340 1341 1342 1343

    }
    else if( psz_filter && strcmp( psz_filter , "media") == 0 )
    {
        int i;
1344 1345
        vlm_message_t *message;
        vlm_message_t *message_child;
1346

1347 1348
        message = vlm_MessageNew( "show" , NULL );
        message_child = vlm_MessageAdd( message , vlm_MessageNew( "media" , NULL ) );
1349 1350 1351 1352

        for( i = 0; i < vlm->i_media; i++ )
        {
            vlm_media_t *m = vlm->media[i];
1353
            vlm_message_t *message_media = vlm_MessageAdd( message_child , vlm_MessageNew( m->psz_name , NULL ) );
1354 1355 1356

            if( m->i_type == VOD_TYPE )
            {
1357
                vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "vod" ) );
1358 1359 1360
            }
            else
            {
1361
                vlm_MessageAdd( message_media , vlm_MessageNew( "type" , "broadcast" ) );
1362 1363 1364 1365
            }

            if( m->b_enabled == VLC_TRUE )
            {
1366
                vlm_MessageAdd( message_media , vlm_MessageNew( "enabled" , "yes" ) );
1367 1368 1369
            }
            else
            {
1370
                vlm_MessageAdd( message_media , vlm_MessageNew( "enabled" , "no" ) );
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
            }

            if( m->p_input != NULL )
            {
                vlc_value_t val;

                var_Get( m->p_input, "state", &val );

                if( val.i_int == PLAYING_S )
                {
1381
                    vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "playing" ) );
1382 1383 1384
                }
                else if( val.i_int == PAUSE_S )
                {
1385
                    vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "paused" ) );
1386 1387 1388
                }
                else
                {
1389
                    vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "stop" ) );
1390 1391 1392 1393
                }
            }
            else
            {
1394
                vlm_MessageAdd( message_media , vlm_MessageNew( "state" , "stop" ) );
1395 1396 1397
            }
        }

1398
        return message;
1399 1400 1401 1402
    }
    else if( psz_filter && strcmp( psz_filter , "schedule") == 0 )
    {
        int i;
1403 1404
        vlm_message_t *message;
        vlm_message_t *message_child;
1405

1406 1407
        message = vlm_MessageNew( "show" , NULL );
        message_child = vlm_MessageAdd( message , vlm_MessageNew( "schedule" , NULL ) );
1408 1409 1410 1411

        for( i = 0; i < vlm->i_schedule; i++ )
        {
            vlm_schedule_t *s = vlm->schedule[i];
1412
            vlm_message_t *message_schedule = vlm_MessageAdd( message_child , vlm_MessageNew( s->psz_name , NULL ) ); 
1413 1414 1415 1416 1417 1418

            if( s->b_enabled == VLC_TRUE )
            {
                mtime_t i_time;
                mtime_t i_next_date;

1419
                vlm_MessageAdd( message_schedule , vlm_MessageNew( "enabled" , "yes" ) );
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440

                /* calculate next date */
                i_time = mdate();

                i_next_date = s->i_date;

                if( s->i_period != 0 )
                {
                    int j = 0;
                    while( s->i_date + j * s->i_period <= i_time && s->i_repeat > j )
                    {
                        j++;
                    }

                    i_next_date = s->i_date + j * s->i_period;
                }

                if( i_next_date > i_time )
                {
                    time_t i_date = (time_t) (i_next_date / 1000000) ;

1441 1442
#ifdef HAVE_CTIME_R
                    char psz_date[500];
1443
                    ctime_r( &i_date, psz_date );
1444 1445 1446
#else
                    char *psz_date = ctime( &i_date );
#endif
1447

1448
                    vlm_MessageAdd( message_schedule , vlm_MessageNew( "next launch" , psz_date ) );
1449 1450 1451 1452
                }
            }
            else
            {
1453
                vlm_MessageAdd( message_schedule , vlm_MessageNew( "enabled" , "no" ) );
1454 1455 1456
            }
        }

1457
        return message;
1458 1459 1460
    }
    else if( psz_filter == NULL && media == NULL && schedule == NULL )
    {
1461 1462
        vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
        vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1463