]> git.sesse.net Git - vlc/blob - src/misc/vlm.c
260602011fec5a55fb3bcd32cc1a88b67094d5ca
[vlc] / src / misc / vlm.c
1 /*****************************************************************************
2  * vlm.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000, 2001 VideoLAN
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *          Gildas Bazin <gbazin@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <stdlib.h>                                      /* malloc(), free() */
30 #include <ctype.h>                                              /* tolower() */
31
32 #include <vlc/vlc.h>
33
34 #ifdef ENABLE_VLM
35
36 #include <vlc/intf.h>
37 #include <vlc/input.h>
38
39 #ifdef HAVE_TIME_H
40 #   include <time.h>                                              /* ctime() */
41 #   include <sys/timeb.h>                                         /* ftime() */
42 #endif
43
44 #include "vlc_vlm.h"
45 #include "vlc_vod.h"
46
47 #define FREE( p ) \
48         if( p ) { free( p ); (p) = NULL; }
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static vlm_message_t *vlm_Show( vlm_t *, vlm_media_t *, vlm_schedule_t *, char * );
54 static vlm_message_t *vlm_Help( vlm_t *, char * );
55
56 static vlm_media_t *vlm_MediaSearch ( vlm_t *, char * );
57 static vlm_media_instance_t *vlm_MediaInstanceSearch( vlm_t *, vlm_media_t *, char * );
58
59 static vlm_message_t *vlm_MessageNew( char *, const char *, ... );
60 static vlm_message_t *vlm_MessageAdd( vlm_message_t *, vlm_message_t * );
61
62 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *, char * );
63
64 static char *Save( vlm_t * );
65 static int Load( vlm_t *, char * );
66 static int ExecuteCommand( vlm_t *, char *, vlm_message_t ** );
67 static int Manage( vlc_object_t * );
68
69 /*****************************************************************************
70  * vlm_New:
71  *****************************************************************************/
72 vlm_t *__vlm_New ( vlc_object_t *p_this )
73 {
74     vlc_value_t lockval;
75     vlm_t *p_vlm = NULL;
76     char *psz_vlmconf;
77
78     /* to be sure to avoid multiple creation */
79     var_Create( p_this->p_libvlc, "vlm_mutex", VLC_VAR_MUTEX );
80     var_Get( p_this->p_libvlc, "vlm_mutex", &lockval );
81     vlc_mutex_lock( lockval.p_address );
82
83     if( !(p_vlm = vlc_object_find( p_this, VLC_OBJECT_VLM, FIND_ANYWHERE )) )
84     {
85         msg_Info( p_this, "creating vlm" );
86         if( ( p_vlm = vlc_object_create( p_this, VLC_OBJECT_VLM ) ) == NULL )
87         {
88             vlc_mutex_unlock( lockval.p_address );
89             return NULL;
90         }
91
92         vlc_mutex_init( p_this->p_vlc, &p_vlm->lock );
93         p_vlm->i_media      = 0;
94         p_vlm->media        = NULL;
95         p_vlm->i_vod        = 0;
96         p_vlm->i_schedule   = 0;
97         p_vlm->schedule     = NULL;
98
99         vlc_object_yield( p_vlm );
100         vlc_object_attach( p_vlm, p_this->p_vlc );
101     }
102     vlc_mutex_unlock( lockval.p_address );
103
104     if( vlc_thread_create( p_vlm, "vlm thread",
105                            Manage, VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
106     {
107         vlc_mutex_destroy( &p_vlm->lock );
108         vlc_object_destroy( p_vlm );
109         return NULL;
110     }
111
112     /* Try loading the vlm conf file given by --vlm-conf */
113     psz_vlmconf = config_GetPsz( p_vlm, "vlm-conf" );
114
115     if( psz_vlmconf && *psz_vlmconf )
116     {
117         vlm_message_t *p_message = NULL;
118         char *psz_buffer = NULL;
119
120         msg_Dbg( p_this, "loading vlm conf ..." );
121         asprintf(&psz_buffer, "load %s", psz_vlmconf );
122         if( psz_buffer )
123         {
124             msg_Dbg( p_this, psz_buffer);
125             if( vlm_ExecuteCommand( p_vlm, psz_buffer, &p_message ) ){
126                 msg_Warn( p_this, "error while loading the vlm conf file" );
127             }
128             free(p_message);
129             free(psz_buffer);
130         }
131    }
132    free(psz_vlmconf);
133
134    return p_vlm;
135 }
136
137 /*****************************************************************************
138  * vlm_Delete:
139  *****************************************************************************/
140 void vlm_Delete( vlm_t *p_vlm )
141 {
142     vlc_value_t lockval;
143
144     var_Get( p_vlm->p_libvlc, "vlm_mutex", &lockval );
145     vlc_mutex_lock( lockval.p_address );
146
147     vlc_object_release( p_vlm );
148
149     if( p_vlm->i_refcount > 0 )
150     {
151         vlc_mutex_unlock( lockval.p_address );
152         return;
153     }
154
155     p_vlm->b_die = VLC_TRUE;
156     vlc_thread_join( p_vlm );
157
158     vlc_mutex_destroy( &p_vlm->lock );
159
160     while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0], NULL );
161     FREE( p_vlm->media );
162
163     while( p_vlm->i_schedule ) vlm_ScheduleDelete( p_vlm,
164                                                    p_vlm->schedule[0], NULL );
165     FREE( p_vlm->schedule );
166
167     vlc_object_detach( p_vlm );
168     vlc_object_destroy( p_vlm );
169     vlc_mutex_unlock( lockval.p_address );
170 }
171
172 /*****************************************************************************
173  * vlm_ExecuteCommand:
174  *****************************************************************************/
175 int vlm_ExecuteCommand( vlm_t *p_vlm, char *psz_command,
176                         vlm_message_t **pp_message)
177 {
178     int i_result;
179
180     vlc_mutex_lock( &p_vlm->lock );
181     i_result = ExecuteCommand( p_vlm, psz_command, pp_message );
182     vlc_mutex_unlock( &p_vlm->lock );
183
184     return i_result;
185 }
186
187 /*****************************************************************************
188  * vlm_Save:
189  *****************************************************************************/
190 int vlm_Save( vlm_t *p_vlm, char *psz_file )
191 {
192     FILE *file;
193     char *psz_save;
194
195     if( !p_vlm || !psz_file ) return 1;
196
197     file = fopen( psz_file, "wt" );
198     if( file == NULL ) return 1;
199
200     psz_save = Save( p_vlm );
201     if( psz_save == NULL )
202     {
203         fclose( file );
204         return 1;
205     }
206     fwrite( psz_save, strlen( psz_save ), 1, file );
207     fclose( file );
208     free( psz_save );
209
210     return 0;
211 }
212
213 /*****************************************************************************
214  * vlm_Load:
215  *****************************************************************************/
216 int vlm_Load( vlm_t *p_vlm, char *psz_file )
217 {
218     FILE *file;
219     int64_t i_size;
220     char *psz_buffer;
221
222     if( !p_vlm || !psz_file ) return 1;
223
224     file = fopen( psz_file, "r" );
225     if( file == NULL ) return 1;
226
227     if( fseek( file, 0, SEEK_END) != 0 )
228     {
229         fclose( file );
230         return 2;
231     }
232
233     i_size = ftell( file );
234     fseek( file, 0, SEEK_SET );
235     psz_buffer = malloc( i_size + 1 );
236     if( !psz_buffer )
237     {
238         fclose( file );
239         return 2;
240     }
241     fread( psz_buffer, 1, i_size, file );
242     psz_buffer[ i_size ] = '\0';
243     if( Load( p_vlm, psz_buffer ) )
244     {
245         fclose( file );
246         free( psz_buffer );
247         return 3;
248     }
249
250     free( psz_buffer );
251     fclose( file );
252
253     return 0;
254 }
255
256 /*****************************************************************************
257  * FindEndCommand
258  *****************************************************************************/
259 static char *FindEndCommand( char *psz )
260 {
261     char *psz_sent = psz;
262
263     switch( *psz_sent )
264     {
265     case '\"':
266         psz_sent++;
267
268         while( ( *psz_sent != '\"' ) && ( *psz_sent != '\0' ) )
269         {
270             if( *psz_sent == '\'' )
271             {
272                 psz_sent = FindEndCommand( psz_sent );
273                 if( psz_sent == NULL ) return NULL;
274             }
275             else psz_sent++;
276         }
277
278         if( *psz_sent == '\"' )
279         {
280             psz_sent++;
281             return psz_sent;
282         }
283
284         /* *psz_sent == '\0' -> number of " is incorrect */
285         else return NULL;
286
287         break;
288
289     case '\'':
290         psz_sent++;
291
292         while( ( *psz_sent != '\'' ) && ( *psz_sent != '\0' ) )
293         {
294             if( *psz_sent == '\"' )
295             {
296                 psz_sent = FindEndCommand( psz_sent );
297                 if( psz_sent == NULL ) return NULL;
298             }
299             else psz_sent++;
300         }
301
302         if( *psz_sent == '\'' )
303         {
304             psz_sent++;
305             return psz_sent;
306         }
307
308         /* *psz_sent == '\0' -> number of " is incorrect */
309         else return NULL;
310
311         break;
312
313     default: /* now we can look for spaces */
314         while( ( *psz_sent != ' ' ) && ( *psz_sent != '\0' ) )
315         {
316             if( ( *psz_sent == '\'' ) || ( *psz_sent == '\"' ) )
317             {
318                 psz_sent = FindEndCommand( psz_sent );
319             }
320             else psz_sent++;
321         }
322
323         return psz_sent;
324     }
325 }
326
327 /*****************************************************************************
328  * ExecuteCommand: The main state machine
329  *****************************************************************************
330  * Execute a command which ends with '\0' (string)
331  *****************************************************************************/
332 static int ExecuteCommand( vlm_t *p_vlm, char *psz_command,
333                            vlm_message_t **pp_message )
334 {
335     int i_command = 0;
336     char **ppsz_command = NULL;
337     char *psz_cmd = psz_command;
338     vlm_message_t *p_message = NULL;
339     int i, j;
340
341     /* First, parse the line and cut it */
342     while( *psz_cmd != '\0' )
343     {
344
345         if( *psz_cmd == ' ' || *psz_cmd == '\t' )
346         {
347             psz_cmd++;
348         }
349         else
350         {
351             char *psz_temp;
352             int   i_temp;
353
354             /* support for comments */
355             if( i_command == 0 && *psz_cmd == '#')
356             {
357                 p_message = vlm_MessageNew( "", NULL );
358                 goto success;
359             }
360
361             psz_temp = FindEndCommand( psz_cmd );
362
363             if( psz_temp == NULL ) goto error;
364
365             i_temp = psz_temp - psz_cmd;
366
367             ppsz_command = realloc( ppsz_command, (i_command + 1) *
368                                     sizeof(char*) );
369             ppsz_command[ i_command ] = malloc( (i_temp + 1) * sizeof(char) );
370             strncpy( ppsz_command[ i_command ], psz_cmd, i_temp );
371             ppsz_command[ i_command ][ i_temp ] = '\0';
372
373             i_command++;
374
375             psz_cmd = psz_temp;
376         }
377     }
378
379     /*
380      * And then Interpret it
381      */
382
383     if( i_command == 0 )
384     {
385         p_message = vlm_MessageNew( "", NULL );
386         goto success;
387     }
388
389     if( !strcmp(ppsz_command[0], "new") )
390     {
391         int i_type;
392
393         /* Check the number of arguments */
394         if( i_command < 3 ) goto syntax_error;
395
396         /* Get type */
397         if( !strcmp(ppsz_command[2], "vod") )
398         {
399             i_type = VOD_TYPE;
400         }
401         else if( !strcmp(ppsz_command[2], "broadcast") )
402         {
403             i_type = BROADCAST_TYPE;
404         }
405         else if( !strcmp(ppsz_command[2], "schedule") )
406         {
407             i_type = SCHEDULE_TYPE;
408         }
409         else
410         {
411             p_message =
412                 vlm_MessageNew( "new", "%s: Choose between vod, "
413                                 "broadcast or schedule", ppsz_command[1] );
414             goto error;
415         }
416
417         /* Check for forbidden media names */
418         if( !strcmp(ppsz_command[1], "all") ||
419             !strcmp(ppsz_command[1], "media") ||
420             !strcmp(ppsz_command[1], "schedule") )
421         {
422             p_message = vlm_MessageNew( "new", "\"all\", \"media\" and "
423                                         "\"schedule\" are reserved names" );
424             goto error;
425         }
426
427         /* Check the name is not already in use */
428         if( vlm_ScheduleSearch( p_vlm, ppsz_command[1] ) ||
429             vlm_MediaSearch( p_vlm, ppsz_command[1] ) )
430         {
431             p_message = vlm_MessageNew( "new", "%s: Name already in use",
432                                         ppsz_command[1] );
433             goto error;
434         }
435
436         /* Schedule */
437         if( i_type == SCHEDULE_TYPE )
438         {
439             vlm_schedule_t *p_schedule;
440             p_schedule = vlm_ScheduleNew( p_vlm, ppsz_command[1] );
441             if( !p_schedule )
442             {
443                 p_message = vlm_MessageNew( "new", "could not create schedule" );
444                 goto error;
445             }
446         }
447
448         /* Media */
449         else
450         {
451             vlm_media_t *p_media;
452             p_media = vlm_MediaNew( p_vlm, ppsz_command[1], i_type );
453             if( !p_media )
454             {
455                 p_message = vlm_MessageNew( "new", "could not create media" );
456                 goto error;
457             }
458         }
459
460         if( i_command <= 3 )
461         {
462             p_message = vlm_MessageNew( "new", NULL );
463             goto success;
464         }
465
466         /* Properties will be dealt with later on */
467     }
468
469     else if( !strcmp(ppsz_command[0], "setup") )
470     {
471         if( i_command < 2 ) goto syntax_error;
472
473         /* Properties will be dealt with later on */
474     }
475
476     else if( !strcmp(ppsz_command[0], "del") )
477     {
478         vlm_media_t *p_media;
479         vlm_schedule_t *p_schedule;
480
481         if( i_command < 2 ) goto syntax_error;
482
483         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
484         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
485
486         if( p_schedule != NULL )
487         {
488             vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
489         }
490         else if( p_media != NULL )
491         {
492             vlm_MediaDelete( p_vlm, p_media, NULL );
493         }
494         else if( !strcmp(ppsz_command[1], "media") )
495         {
496             while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
497                                                      NULL );
498         }
499         else if( !strcmp(ppsz_command[1], "schedule") )
500         {
501             while( p_vlm->i_schedule )
502                 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
503         }
504         else if( !strcmp(ppsz_command[1], "all") )
505         {
506             while( p_vlm->i_media ) vlm_MediaDelete( p_vlm, p_vlm->media[0],
507                                                      NULL );
508
509             while( p_vlm->i_schedule )
510                 vlm_ScheduleDelete( p_vlm, p_vlm->schedule[0], NULL );
511         }
512         else
513         {
514             p_message = vlm_MessageNew( "del", "%s: media unknown",
515                                       ppsz_command[1] );
516             goto error;
517         }
518
519         p_message = vlm_MessageNew( "del", NULL );
520         goto success;
521     }
522
523     else if( !strcmp(ppsz_command[0], "show") )
524     {
525         vlm_media_t *p_media;
526         vlm_schedule_t *p_schedule;
527
528         if( i_command == 1 )
529         {
530             p_message = vlm_Show( p_vlm, NULL, NULL, NULL );
531             goto success;
532         }
533         else if( i_command > 2 ) goto syntax_error;
534
535         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
536         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
537
538         if( p_schedule != NULL )
539         {
540             p_message = vlm_Show( p_vlm, NULL, p_schedule, NULL );
541         }
542         else if( p_media != NULL )
543         {
544             p_message = vlm_Show( p_vlm, p_media, NULL, NULL );
545         }
546         else
547         {
548             p_message = vlm_Show( p_vlm, NULL, NULL, ppsz_command[1] );
549         }
550
551         goto success;
552     }
553
554     else if( !strcmp(ppsz_command[0], "help") )
555     {
556         if( i_command != 1 ) goto syntax_error;
557
558         p_message = vlm_Help( p_vlm, NULL );
559         goto success;
560     }
561
562     else if( !strcmp(ppsz_command[0], "control") )
563     {
564         vlm_media_t *p_media;
565
566         if( i_command < 3 ) goto syntax_error;
567
568         if( !(p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] ) ) )
569         {
570             p_message = vlm_MessageNew( "control", "%s: media unknown",
571                                       ppsz_command[1] );
572             goto error;
573         }
574         else
575         {
576             char *psz_command, *psz_arg = 0, *psz_instance = 0;
577             int i_index = 2;
578
579             if( strcmp( ppsz_command[2], "play" ) &&
580                 strcmp( ppsz_command[2], "stop" ) &&
581                 strcmp( ppsz_command[2], "pause" ) &&
582                 strcmp( ppsz_command[2], "seek" ) )
583             {
584                 i_index++;
585                 psz_instance = ppsz_command[2];
586
587                 if( i_command < 4 ) goto syntax_error;
588             }
589
590             psz_command = ppsz_command[i_index];
591
592             if( i_command >= i_index + 2 ) psz_arg = ppsz_command[i_index + 1];
593
594             vlm_MediaControl( p_vlm, p_media, psz_instance, psz_command,
595                              psz_arg );
596             p_message = vlm_MessageNew( "control", NULL );
597             goto success;
598         }
599     }
600
601     else if( !strcmp(ppsz_command[0], "save") )
602     {
603         if( i_command != 2 ) goto syntax_error;
604
605         if( vlm_Save( p_vlm, ppsz_command[1] ) )
606         {
607             p_message = vlm_MessageNew( "save", "Unable to save to file" );
608             goto error;
609         }
610         else
611         {
612             p_message = vlm_MessageNew( "save", NULL );
613             goto success;
614         }
615     }
616
617     else if( !strcmp(ppsz_command[0], "load") )
618     {
619         if( i_command != 2 ) goto syntax_error;
620
621         switch( vlm_Load( p_vlm, ppsz_command[1] ) )
622         {
623             case 0:
624                 p_message = vlm_MessageNew( "load", NULL );
625                 goto success;
626             case 2:
627                 p_message = vlm_MessageNew( "load", "read file error" );
628                 goto error;
629             case 3:
630                 p_message =
631                     vlm_MessageNew( "load", "error while loading file" );
632                 goto error;
633             default:
634                 p_message =
635                     vlm_MessageNew( "load", "Unable to load from file" );
636                 goto error;
637         }
638     }
639
640     else
641     {
642         p_message = vlm_MessageNew( ppsz_command[0], "Unknown command" );
643         goto error;
644     }
645
646     /* Common code between "new" and "setup" */
647     if( !strcmp(ppsz_command[0], "new") ||
648         !strcmp(ppsz_command[0], "setup") )
649     {
650         int i_command_start = strcmp(ppsz_command[0], "new") ? 2 : 3;
651         vlm_media_t *p_media;
652         vlm_schedule_t *p_schedule;
653
654         if( i_command < i_command_start ) goto syntax_error;
655
656         p_media = vlm_MediaSearch( p_vlm, ppsz_command[1] );
657         p_schedule = vlm_ScheduleSearch( p_vlm, ppsz_command[1] );
658
659         if( !p_media && !p_schedule )
660         {
661             p_message = vlm_MessageNew( ppsz_command[0], "%s unknown",
662                                         ppsz_command[1] );
663             goto error;
664         }
665
666         if( p_schedule != NULL )
667         {
668             for( i = i_command_start ; i < i_command ; i++ )
669             {
670                 if( !strcmp( ppsz_command[i], "enabled" ) ||
671                     !strcmp( ppsz_command[i], "disabled" ) )
672                 {
673                     vlm_ScheduleSetup( p_schedule, ppsz_command[i], NULL );
674                 }
675
676                 /* Beware: everything behind append is considered as
677                  * command line */
678                 else if( !strcmp( ppsz_command[i], "append" ) )
679                 {
680                     if( ++i >= i_command ) break;
681
682                     for( j = i + 1; j < i_command; j++ )
683                     {
684                         ppsz_command[i] =
685                             realloc( ppsz_command[i], strlen(ppsz_command[i]) +
686                                      strlen(ppsz_command[j]) + 1 + 1 );
687                         strcat( ppsz_command[i], " " );
688                         strcat( ppsz_command[i], ppsz_command[j] );
689                     }
690
691                     vlm_ScheduleSetup( p_schedule, ppsz_command[i - 1],
692                                        ppsz_command[i] );
693                     break;
694                 }
695                 else
696                 {
697                     if( i + 1 >= i_command && !strcmp(ppsz_command[0], "new") )
698                     {
699                         vlm_ScheduleDelete( p_vlm, p_schedule, NULL );
700                         p_message =
701                             vlm_MessageNew( ppsz_command[0],
702                                             "Wrong properties syntax" );
703                         goto error;
704                     }
705                     else if( i + 1 >= i_command )
706                     {
707                         p_message =
708                             vlm_MessageNew( ppsz_command[0],
709                                             "Wrong properties syntax" );
710                         goto error;
711                     }
712
713                     vlm_ScheduleSetup( p_schedule, ppsz_command[i],
714                                        ppsz_command[i+1] );
715                     i++;
716                 }
717             }
718         }
719
720         else if( p_media != NULL )
721         {
722             for( i = i_command_start ; i < i_command ; i++ )
723             {
724                 if( !strcmp( ppsz_command[i], "enabled" ) ||
725                     !strcmp( ppsz_command[i], "disabled" ) )
726                 {
727                     vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
728                 }
729                 else if( i + 1 >= i_command &&
730                          !strcmp( ppsz_command[i], "mux") )
731                 {
732                     if( p_media->i_type != VOD_TYPE )
733                     {
734                         p_message = vlm_MessageNew( ppsz_command[0],
735                                   "mux only available for broadcast" );
736                     }
737                     else
738                     {
739                         vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
740                                         ppsz_command[i+1] );
741                         i++;
742                     }
743                 }
744                 else if( !strcmp( ppsz_command[i], "loop" ) ||
745                          !strcmp( ppsz_command[i], "unloop" ) )
746                 {
747                     if( p_media->i_type != BROADCAST_TYPE )
748                     {
749                         p_message = vlm_MessageNew( ppsz_command[0],
750                                   "loop only available for broadcast" );
751                     }
752                     else
753                     {
754                         vlm_MediaSetup( p_vlm, p_media, ppsz_command[i], NULL );
755                     }
756                 }
757                 else
758                 {
759                     if( i + 1 >= i_command &&
760                         !strcmp(ppsz_command[0], "new") )
761                     {
762                         vlm_MediaDelete( p_vlm, p_media, NULL );
763                         p_message =
764                             vlm_MessageNew( ppsz_command[0],
765                                             "Wrong properties syntax" );
766                         goto error;
767                     }
768                     else if( i + 1 >= i_command )
769                     {
770                         p_message =
771                             vlm_MessageNew( ppsz_command[0],
772                                             "Wrong properties syntax" );
773                         goto error;
774                     }
775
776                     vlm_MediaSetup( p_vlm, p_media, ppsz_command[i],
777                                     ppsz_command[i+1] );
778                     i++;
779                 }
780             }
781         }
782
783         p_message = vlm_MessageNew( ppsz_command[0], NULL );
784         goto success;
785     }
786
787 success:
788     for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
789     FREE( ppsz_command );
790     *pp_message = p_message;
791
792     return VLC_SUCCESS;
793
794 syntax_error:
795     p_message = vlm_MessageNew( ppsz_command[0], "Wrong command syntax" );
796
797 error:
798     for( i = 0 ; i < i_command ; i++ ) FREE( ppsz_command[i] );
799     FREE( ppsz_command );
800     *pp_message = p_message;
801
802     return VLC_EGENERIC;
803 }
804
805 static vlm_media_t *vlm_MediaSearch( vlm_t *vlm, char *psz_name )
806 {
807     int i;
808
809     for( i = 0; i < vlm->i_media; i++ )
810     {
811         if( strcmp( psz_name, vlm->media[i]->psz_name ) == 0 )
812         {
813             return vlm->media[i];
814         }
815     }
816
817     return NULL;
818 }
819
820 /*****************************************************************************
821  * Media handling
822  *****************************************************************************/
823 static vlm_media_instance_t *
824 vlm_MediaInstanceSearch( vlm_t *vlm, vlm_media_t *media, char *psz_name )
825 {
826     int i;
827
828     for( i = 0; i < media->i_instance; i++ )
829     {
830         if( ( !psz_name && !media->instance[i]->psz_name ) ||
831             ( psz_name && media->instance[i]->psz_name &&
832               !strcmp( psz_name, media->instance[i]->psz_name ) ) )
833         {
834             return media->instance[i];
835         }
836     }
837
838     return NULL;
839 }
840
841 vlm_media_t *vlm_MediaNew( vlm_t *vlm, char *psz_name, int i_type )
842 {
843     vlm_media_t *media = malloc( sizeof( vlm_media_t ) );
844
845     if( !media )
846     {
847         msg_Err( vlm, "out of memory" );
848         return NULL;
849     }
850
851     /* Check if we need to load the VOD server */
852     if( i_type == VOD_TYPE && !vlm->i_vod )
853     {
854         vlm->vod = vlc_object_create( vlm, VLC_OBJECT_VOD );
855         vlc_object_attach( vlm->vod, vlm );
856         vlm->vod->p_module = module_Need( vlm->vod, "vod server", 0, 0 );
857         if( !vlm->vod->p_module )
858         {
859             msg_Err( vlm, "cannot find vod server" );
860             vlc_object_detach( vlm->vod );
861             vlc_object_destroy( vlm->vod );
862             vlm->vod = 0;
863             free( media );
864             return NULL;
865         }
866
867         vlm->vod->p_data = vlm;
868         vlm->vod->pf_media_control = vlm_MediaVodControl;
869     }
870
871     if( i_type == VOD_TYPE ) vlm->i_vod++;
872
873     media->psz_name = strdup( psz_name );
874     media->b_enabled = VLC_FALSE;
875     media->b_loop = VLC_FALSE;
876     media->vod_media = NULL;
877     media->psz_vod_output = NULL;
878     media->psz_mux = NULL;
879     media->i_input = 0;
880     media->input = NULL;
881     media->psz_output = NULL;
882     media->i_option = 0;
883     media->option = NULL;
884     media->i_type = i_type;
885     media->i_instance = 0;
886     media->instance = NULL;
887
888     media->item.psz_uri = strdup( psz_name );
889     vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
890
891     TAB_APPEND( vlm->i_media, vlm->media, media );
892
893     return media;
894 }
895
896 /* for now, simple delete. After, del with options (last arg) */
897 void vlm_MediaDelete( vlm_t *vlm, vlm_media_t *media, char *psz_name )
898 {
899     if( media == NULL ) return;
900
901     while( media->i_instance )
902     {
903         vlm_media_instance_t *p_instance = media->instance[0];
904         vlm_MediaControl( vlm, media, p_instance->psz_name, "stop", 0 );
905     }
906
907     TAB_REMOVE( vlm->i_media, vlm->media, media );
908
909     if( media->i_type == VOD_TYPE )
910     {
911         vlm_MediaSetup( vlm, media, "disabled", 0 );
912         vlm->i_vod--;
913     }
914
915     /* Check if we need to unload the VOD server */
916     if( media->i_type == VOD_TYPE && !vlm->i_vod )
917     {
918         module_Unneed( vlm->vod, vlm->vod->p_module );
919         vlc_object_detach( vlm->vod );
920         vlc_object_destroy( vlm->vod );
921         vlm->vod = 0;
922     }
923
924     if( vlm->i_media == 0 && vlm->media ) free( vlm->media );
925
926     free( media->psz_name );
927
928     while( media->i_input-- ) free( media->input[media->i_input] );
929     if( media->input ) free( media->input );
930
931     if( media->psz_output ) free( media->psz_output );
932     if( media->psz_mux ) free( media->psz_mux );
933
934     while( media->i_option-- ) free( media->option[media->i_option] );
935     if( media->option ) free( media->option );
936
937     vlc_input_item_Clean( &media->item );
938
939     free( media );
940 }
941
942 int vlm_MediaSetup( vlm_t *vlm, vlm_media_t *media, char *psz_cmd,
943                     char *psz_value )
944 {
945     if( !psz_cmd) return VLC_EGENERIC;
946
947     if( !strcmp( psz_cmd, "loop" ) )
948     {
949         media->b_loop = VLC_TRUE;
950     }
951     else if( !strcmp( psz_cmd, "unloop" ) )
952     {
953         media->b_loop = VLC_FALSE;
954     }
955     else if( !strcmp( psz_cmd, "enabled" ) )
956     {
957         media->b_enabled = VLC_TRUE;
958     }
959     else if( !strcmp( psz_cmd, "disabled" ) )
960     {
961         media->b_enabled = VLC_FALSE;
962     }
963     else if( !strcmp( psz_cmd, "mux" ) )
964     {
965         if( media->psz_mux ) free( media->psz_mux );
966         media->psz_mux = NULL;
967         if( psz_value ) media->psz_mux = strdup( psz_value );
968     }
969     else if( !strcmp( psz_cmd, "input" ) )
970     {
971         char *input;
972
973         if( psz_value != NULL && strlen(psz_value) > 1 &&
974             ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
975             ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
976               psz_value[ strlen(psz_value) - 1 ] == '\"' )  )
977         {
978             input = malloc( strlen(psz_value) - 1 );
979
980             memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
981             input[ strlen(psz_value) - 2 ] = '\0';
982         }
983         else
984         {
985             input = strdup( psz_value );
986         }
987
988         TAB_APPEND( media->i_input, media->input, input );
989     }
990     else if( !strcmp( psz_cmd, "inputdel" ) && !strcmp( psz_value, "all" ) )
991     {
992         while( media->i_input > 0 )
993         {
994             TAB_REMOVE( media->i_input, media->input, media->input[0] );
995         }
996     }
997     else if( !strcmp( psz_cmd, "inputdel" ) )
998     {
999         char *input;
1000         int i;
1001
1002         if( psz_value != NULL && strlen(psz_value) > 1 &&
1003             ( psz_value[0] == '\'' || psz_value[0] == '\"' ) &&
1004             ( psz_value[ strlen(psz_value) - 1 ] == '\'' ||
1005               psz_value[ strlen(psz_value) - 1 ] == '\"' )  )
1006         {
1007             input = malloc( strlen(psz_value) - 1 );
1008
1009             memcpy( input, psz_value + 1, strlen(psz_value) - 2 );
1010             input[ strlen(psz_value) - 2 ] = '\0';
1011         }
1012         else
1013         {
1014             input = strdup( psz_value );
1015         }
1016
1017         for( i = 0; i < media->i_input; i++ )
1018         {
1019             if( !strcmp( input, media->input[i] ) )
1020             {
1021                 TAB_REMOVE( media->i_input, media->input, media->input[i] );
1022                 break;
1023             }
1024         }
1025     }
1026     else if( !strcmp( psz_cmd, "inputdeln" ) )
1027     {
1028         int index = atoi( psz_value );
1029         if( index > 0 && index <= media->i_input )
1030         {
1031             TAB_REMOVE( media->i_input, media->input, media->input[index-1] );
1032         }
1033     }
1034     else if( !strcmp( psz_cmd, "output" ) )
1035     {
1036         if( media->psz_output != NULL )
1037         {
1038             free( media->psz_output );
1039         }
1040         media->psz_output = strdup( psz_value );
1041     }
1042     else if( !strcmp( psz_cmd, "option" ) )
1043     {
1044         char *psz_option;
1045         psz_option = strdup( psz_value );
1046
1047         TAB_APPEND( media->i_option, media->option, psz_option );
1048     }
1049     else
1050     {
1051         return VLC_EGENERIC;
1052     }
1053
1054     /* Check if we need to create/delete a vod media */
1055     if( media->i_type == VOD_TYPE )
1056     {
1057         if( !media->b_enabled && media->vod_media )
1058         {
1059             vlm->vod->pf_media_del( vlm->vod, media->vod_media );
1060             media->vod_media = 0;
1061         }
1062         else if( media->b_enabled && !media->vod_media && media->i_input )
1063         {
1064             /* Pre-parse the input */
1065             input_thread_t *p_input;
1066             char *psz_output;
1067             int i;
1068
1069             vlc_input_item_Clean( &media->item );
1070             vlc_input_item_Init( VLC_OBJECT(vlm), &media->item );
1071
1072             if( media->psz_output )
1073                 asprintf( &psz_output, "%s:description", media->psz_output );
1074             else
1075                 asprintf( &psz_output, "#description" );
1076
1077             media->item.psz_uri = strdup( media->input[0] );
1078             media->item.ppsz_options = malloc( sizeof( char* ) );
1079             asprintf( &media->item.ppsz_options[0], "sout=%s", psz_output);
1080             media->item.i_options = 1;
1081             for( i = 0; i < media->i_option; i++ )
1082             {
1083                 media->item.i_options++;
1084                 media->item.ppsz_options =
1085                     realloc( media->item.ppsz_options,
1086                              media->item.i_options * sizeof( char* ) );
1087                 media->item.ppsz_options[ media->item.i_options - 1 ] =
1088                     strdup( media->option[i] );
1089             }
1090
1091             if( (p_input = input_CreateThread( vlm, &media->item ) ) )
1092             {
1093                 while( !p_input->b_eof && !p_input->b_error ) msleep( 100000 );
1094
1095                 input_StopThread( p_input );
1096                 input_DestroyThread( p_input );
1097                 vlc_object_detach( p_input );
1098                 vlc_object_destroy( p_input );
1099             }
1100             free( psz_output );
1101
1102             if( media->psz_mux )
1103             {
1104                 input_item_t item;
1105                 es_format_t es, *p_es = &es;
1106                 char fourcc[5];
1107
1108                 sprintf( fourcc, "%4.4s", media->psz_mux );
1109                 fourcc[0] = tolower(fourcc[0]); fourcc[1] = tolower(fourcc[1]);
1110                 fourcc[2] = tolower(fourcc[2]); fourcc[3] = tolower(fourcc[3]);
1111
1112                 item = media->item;
1113                 item.i_es = 1;
1114                 item.es = &p_es;
1115                 es_format_Init( &es, VIDEO_ES, *((int *)fourcc) );
1116
1117                 media->vod_media =
1118                   vlm->vod->pf_media_new( vlm->vod, media->psz_name, &item );
1119                 return VLC_SUCCESS;
1120             }
1121
1122             media->vod_media =
1123                 vlm->vod->pf_media_new( vlm->vod, media->psz_name,
1124                                         &media->item );
1125         }
1126     }
1127
1128     return VLC_SUCCESS;
1129 }
1130
1131 int vlm_MediaControl( vlm_t *vlm, vlm_media_t *media, char *psz_id,
1132                       char *psz_command, char *psz_args )
1133 {
1134     vlm_media_instance_t *p_instance;
1135     int i;
1136
1137     p_instance = vlm_MediaInstanceSearch( vlm, media, psz_id );
1138
1139     if( !strcmp( psz_command, "play" ) )
1140     {
1141         if( !media->b_enabled || media->i_input == 0 ) return 0;
1142
1143         if( !p_instance )
1144         {
1145             p_instance = malloc( sizeof(vlm_media_instance_t) );
1146             if( !p_instance ) return VLC_EGENERIC;
1147             memset( p_instance, 0, sizeof(vlm_media_instance_t) );
1148             vlc_input_item_Init( VLC_OBJECT(vlm), &p_instance->item );
1149             p_instance->p_input = NULL;
1150
1151             if( media->psz_output != NULL || media->psz_vod_output != NULL )
1152             {
1153                 p_instance->item.ppsz_options = malloc( sizeof( char* ) );
1154                 asprintf( &p_instance->item.ppsz_options[0], "sout=%s%s%s",
1155                           media->psz_output ? media->psz_output : "",
1156                           (media->psz_output && media->psz_vod_output) ?
1157                           ":" : media->psz_vod_output ? "#" : "",
1158                           media->psz_vod_output ? media->psz_vod_output : "" );
1159                 p_instance->item.i_options = 1;
1160             }
1161
1162             for( i = 0; i < media->i_option; i++ )
1163             {
1164                 p_instance->item.i_options++;
1165                 p_instance->item.ppsz_options =
1166                     realloc( p_instance->item.ppsz_options,
1167                              p_instance->item.i_options * sizeof( char* ) );
1168                 p_instance->item.ppsz_options[p_instance->item.i_options - 1] =
1169                     strdup( media->option[i] );
1170             }
1171
1172             p_instance->psz_name = psz_id ? strdup( psz_id ) : NULL;
1173             TAB_APPEND( media->i_instance, media->instance, p_instance );
1174         }
1175
1176         if( psz_args && sscanf(psz_args, "%d", &i) == 1 && i < media->i_input )
1177         {
1178             p_instance->i_index = i;
1179         }
1180
1181         if( p_instance->item.psz_uri ) free( p_instance->item.psz_uri );
1182         p_instance->item.psz_uri =
1183             strdup( media->input[p_instance->i_index] );
1184
1185         if( p_instance->p_input )
1186         {
1187             input_StopThread( p_instance->p_input );
1188             input_DestroyThread( p_instance->p_input );
1189             vlc_object_detach( p_instance->p_input );
1190             vlc_object_destroy( p_instance->p_input );
1191         }
1192
1193         p_instance->p_input = input_CreateThread( vlm, &p_instance->item );
1194         if( !p_instance->p_input )
1195         {
1196             TAB_REMOVE( media->i_instance, media->instance, p_instance );
1197             vlc_input_item_Clean( &p_instance->item );
1198             if( p_instance->psz_name ) free( p_instance->psz_name );
1199         }
1200
1201         return VLC_SUCCESS;
1202     }
1203
1204     if( !p_instance ) return VLC_EGENERIC;
1205
1206     if( !strcmp( psz_command, "seek" ) )
1207     {
1208         vlc_value_t val;
1209         float f_percentage;
1210
1211         if( psz_args && sscanf( psz_args, "%f", &f_percentage ) == 1 )
1212         {
1213             val.f_float = f_percentage / 100.0 ;
1214             var_Set( p_instance->p_input, "position", val );
1215             return VLC_SUCCESS;
1216         }
1217     }
1218     else if( !strcmp( psz_command, "stop" ) )
1219     {
1220         TAB_REMOVE( media->i_instance, media->instance, p_instance );
1221
1222         if( p_instance->p_input )
1223         {
1224             input_StopThread( p_instance->p_input );
1225             input_DestroyThread( p_instance->p_input );
1226             vlc_object_detach( p_instance->p_input );
1227             vlc_object_destroy( p_instance->p_input );
1228         }
1229
1230         vlc_input_item_Clean( &p_instance->item );
1231         if( p_instance->psz_name ) free( p_instance->psz_name );
1232         free( p_instance );
1233
1234         return VLC_SUCCESS;
1235     }
1236     else if( !strcmp( psz_command, "pause" ) )
1237     {
1238         vlc_value_t val;
1239
1240         if( !p_instance->p_input ) return VLC_SUCCESS;
1241
1242         var_Get( p_instance->p_input, "state", &val );
1243
1244         if( val.i_int == PAUSE_S ) val.i_int = PLAYING_S;
1245         else val.i_int = PAUSE_S;
1246         var_Set( p_instance->p_input, "state", val );
1247
1248         return VLC_SUCCESS;
1249     }
1250
1251     return VLC_EGENERIC;
1252 }
1253
1254 /*****************************************************************************
1255  * Schedule handling
1256  *****************************************************************************/
1257 static int64_t vlm_Date()
1258 {
1259 #ifdef WIN32
1260     struct timeb tm;
1261     ftime( &tm );
1262     return ((int64_t)tm.time) * 1000000 + ((int64_t)tm.millitm) * 1000;
1263 #else
1264     return mdate();
1265 #endif
1266 }
1267
1268 vlm_schedule_t *vlm_ScheduleNew( vlm_t *vlm, char *psz_name )
1269 {
1270     vlm_schedule_t *p_sched = malloc( sizeof( vlm_schedule_t ) );
1271
1272     if( !p_sched )
1273     {
1274         return NULL;
1275     }
1276
1277     if( !psz_name )
1278     {
1279         return NULL;
1280     }
1281
1282     p_sched->psz_name = strdup( psz_name );
1283     p_sched->b_enabled = VLC_FALSE;
1284     p_sched->i_command = 0;
1285     p_sched->command = NULL;
1286     p_sched->i_date = 0;
1287     p_sched->i_period = 0;
1288     p_sched->i_repeat = -1;
1289
1290     TAB_APPEND( vlm->i_schedule, vlm->schedule, p_sched );
1291
1292     return p_sched;
1293 }
1294
1295 /* for now, simple delete. After, del with options (last arg) */
1296 void vlm_ScheduleDelete( vlm_t *vlm, vlm_schedule_t *sched,
1297                          char *psz_name )
1298 {
1299     if( sched == NULL ) return;
1300
1301     TAB_REMOVE( vlm->i_schedule, vlm->schedule, sched );
1302
1303     if( vlm->i_schedule == 0 && vlm->schedule ) free( vlm->schedule );
1304     free( sched->psz_name );
1305     while( sched->i_command-- ) free( sched->command[sched->i_command] );
1306     free( sched );
1307 }
1308
1309 static vlm_schedule_t *vlm_ScheduleSearch( vlm_t *vlm, char *psz_name )
1310 {
1311     int i;
1312
1313     for( i = 0; i < vlm->i_schedule; i++ )
1314     {
1315         if( strcmp( psz_name, vlm->schedule[i]->psz_name ) == 0 )
1316         {
1317             return vlm->schedule[i];
1318         }
1319     }
1320
1321     return NULL;
1322 }
1323
1324 /* Ok, setup schedule command will be able to support only one (argument value) at a time  */
1325 int vlm_ScheduleSetup( vlm_schedule_t *schedule, char *psz_cmd,
1326                        char *psz_value )
1327 {
1328     if( !strcmp( psz_cmd, "enabled" ) )
1329     {
1330         schedule->b_enabled = VLC_TRUE;
1331     }
1332     else if( !strcmp( psz_cmd, "disabled" ) )
1333     {
1334         schedule->b_enabled = VLC_FALSE;
1335     }
1336 #if !defined( UNDER_CE )
1337     else if( !strcmp( psz_cmd, "date" ) )
1338     {
1339         struct tm time;
1340         char *p;
1341         time_t date;
1342
1343         time.tm_sec = 0;         /* seconds */
1344         time.tm_min = 0;         /* minutes */
1345         time.tm_hour = 0;        /* hours */
1346         time.tm_mday = 0;        /* day of the month */
1347         time.tm_mon = 0;         /* month */
1348         time.tm_year = 0;        /* year */
1349         time.tm_wday = 0;        /* day of the week */
1350         time.tm_yday = 0;        /* day in the year */
1351         time.tm_isdst = 0;       /* daylight saving time */
1352
1353         /* date should be year/month/day-hour:minutes:seconds */
1354         p = strchr( psz_value, '-' );
1355
1356         if( !strcmp( psz_value, "now" ) )
1357         {
1358             schedule->i_date = 0;
1359         }
1360         else if( p == NULL && sscanf( psz_value, "%d:%d:%d", &time.tm_hour, &time.tm_min, &time.tm_sec ) != 3 ) /* it must be a hour:minutes:seconds */
1361         {
1362             return 1;
1363         }
1364         else
1365         {
1366             int i,j,k;
1367
1368             switch( sscanf( p + 1, "%d:%d:%d", &i, &j, &k ) )
1369             {
1370                 case 1:
1371                     time.tm_sec = i;
1372                     break;
1373                 case 2:
1374                     time.tm_min = i;
1375                     time.tm_sec = j;
1376                     break;
1377                 case 3:
1378                     time.tm_hour = i;
1379                     time.tm_min = j;
1380                     time.tm_sec = k;
1381                     break;
1382                 default:
1383                     return 1;
1384             }
1385
1386             *p = '\0';
1387
1388             switch( sscanf( psz_value, "%d/%d/%d", &i, &j, &k ) )
1389             {
1390                 case 1:
1391                     time.tm_mday = i;
1392                     break;
1393                 case 2:
1394                     time.tm_mon = i - 1;
1395                     time.tm_mday = j;
1396                     break;
1397                 case 3:
1398                     time.tm_year = i - 1900;
1399                     time.tm_mon = j - 1;
1400                     time.tm_mday = k;
1401                     break;
1402                 default:
1403                     return 1;
1404             }
1405
1406             date = mktime( &time );
1407             schedule->i_date = ((mtime_t) date) * 1000000;
1408         }
1409     }
1410     else if( !strcmp( psz_cmd, "period" ) )
1411     {
1412         struct tm time;
1413         char *p;
1414         char *psz_time = NULL, *psz_date = NULL;
1415         time_t date;
1416         int i,j,k;
1417
1418         /* First, if date or period are modified, repeat should be equal to -1 */
1419         schedule->i_repeat = -1;
1420
1421         time.tm_sec = 0;         /* seconds */
1422         time.tm_min = 0;         /* minutes */
1423         time.tm_hour = 0;        /* hours */
1424         time.tm_mday = 0;        /* day of the month */
1425         time.tm_mon = 0;         /* month */
1426         time.tm_year = 0;        /* year */
1427         time.tm_wday = 0;        /* day of the week */
1428         time.tm_yday = 0;        /* day in the year */
1429         time.tm_isdst = 0;       /* daylight saving time */
1430
1431         /* date should be year/month/day-hour:minutes:seconds */
1432         p = strchr( psz_value, '-' );
1433         if( p )
1434         {
1435             psz_date = psz_value;
1436             psz_time = p + 1;
1437
1438             *p = '\0';
1439         }
1440         else
1441         {
1442             psz_time = psz_value;
1443         }
1444
1445
1446         switch( sscanf( psz_time, "%d:%d:%d", &i, &j, &k ) )
1447         {
1448             case 1:
1449                 time.tm_sec = i;
1450                 break;
1451             case 2:
1452                 time.tm_min = i;
1453                 time.tm_sec = j;
1454                 break;
1455             case 3:
1456                 time.tm_hour = i;
1457                 time.tm_min = j;
1458                 time.tm_sec = k;
1459                 break;
1460             default:
1461                 return 1;
1462         }
1463         if( psz_date )
1464         {
1465             switch( sscanf( psz_date, "%d/%d/%d", &i, &j, &k ) )
1466             {
1467                 case 1:
1468                     time.tm_mday = i;
1469                     break;
1470                 case 2:
1471                     time.tm_mon = i;
1472                     time.tm_mday = j;
1473                     break;
1474                 case 3:
1475                     time.tm_year = i;
1476                     time.tm_mon = j;
1477                     time.tm_mday = k;
1478                     break;
1479                 default:
1480                     return 1;
1481             }
1482         }
1483
1484         /* ok, that's stupid... who is going to schedule streams every 42 years ? */
1485         date = (((( time.tm_year * 12 + time.tm_mon ) * 30 + time.tm_mday ) * 24 + time.tm_hour ) * 60 + time.tm_min ) * 60 + time.tm_sec ;
1486         schedule->i_period = ((mtime_t) date) * 1000000;
1487     }
1488 #endif /* UNDER_CE */
1489     else if( !strcmp( psz_cmd, "repeat" ) )
1490     {
1491         int i;
1492
1493         if( sscanf( psz_value, "%d", &i ) == 1 )
1494         {
1495             schedule->i_repeat = i;
1496         }
1497         else
1498         {
1499             return 1;
1500         }
1501     }
1502     else if( !strcmp( psz_cmd, "append" ) )
1503     {
1504         char *command = strdup( psz_value );
1505
1506         TAB_APPEND( schedule->i_command, schedule->command, command );
1507     }
1508     else
1509     {
1510         return 1;
1511     }
1512     return 0;
1513 }
1514
1515 /*****************************************************************************
1516  * Message handling functions
1517  *****************************************************************************/
1518 static vlm_message_t *vlm_MessageNew( char *psz_name,
1519                                       const char *psz_format, ... )
1520 {
1521     vlm_message_t *p_message;
1522     va_list args;
1523
1524     if( !psz_name ) return 0;
1525
1526     p_message = malloc( sizeof(vlm_message_t) );
1527     if( !p_message)
1528     {
1529         return NULL;
1530     }
1531
1532     p_message->psz_value = 0;
1533
1534     if( psz_format )
1535     {
1536         va_start( args, psz_format );
1537         if( vasprintf( &p_message->psz_value, psz_format, args ) < 0 )
1538         {
1539             va_end( args );
1540             free( p_message );
1541             return 0;
1542         }
1543         va_end( args );
1544     }
1545
1546     p_message->psz_name = strdup( psz_name );
1547     p_message->i_child = 0;
1548     p_message->child = NULL;
1549
1550     return p_message;
1551 }
1552
1553 void vlm_MessageDelete( vlm_message_t *p_message )
1554 {
1555     if( p_message->psz_name ) free( p_message->psz_name );
1556     if( p_message->psz_value ) free( p_message->psz_value );
1557     while( p_message->i_child-- )
1558         vlm_MessageDelete( p_message->child[p_message->i_child] );
1559     if( p_message->child ) free( p_message->child );
1560     free( p_message );
1561 }
1562
1563 /* Add a child */
1564 static vlm_message_t *vlm_MessageAdd( vlm_message_t *p_message,
1565                                       vlm_message_t *p_child )
1566 {
1567     if( p_message == NULL ) return NULL;
1568
1569     if( p_child )
1570     {
1571         TAB_APPEND( p_message->i_child, p_message->child, p_child );
1572     }
1573
1574     return p_child;
1575 }
1576
1577 /*****************************************************************************
1578  * Misc utility functions
1579  *****************************************************************************/
1580 static vlm_message_t *vlm_Show( vlm_t *vlm, vlm_media_t *media,
1581                                 vlm_schedule_t *schedule, char *psz_filter )
1582 {
1583     if( media != NULL )
1584     {
1585         int i;
1586         vlm_message_t *msg;
1587         vlm_message_t *msg_media;
1588         vlm_message_t *msg_child;
1589
1590         msg = vlm_MessageNew( "show", NULL );
1591         msg_media = vlm_MessageAdd( msg, vlm_MessageNew( media->psz_name, 0 ));
1592
1593         vlm_MessageAdd( msg_media,
1594                         vlm_MessageNew( "type", media->i_type == VOD_TYPE ?
1595                                         "vod" : "broadcast" ) );
1596         vlm_MessageAdd( msg_media,
1597                         vlm_MessageNew( "enabled", media->b_enabled ?
1598                                         "yes" : "no" ) );
1599
1600         vlm_MessageAdd( msg_media,
1601                         vlm_MessageNew( "loop", media->b_loop ?
1602                                         "yes" : "no" ) );
1603
1604         if( media->i_type == VOD_TYPE && media->psz_mux )
1605             vlm_MessageAdd( msg_media,
1606                             vlm_MessageNew( "mux", media->psz_mux ) );
1607
1608         msg_child = vlm_MessageAdd( msg_media,
1609                                     vlm_MessageNew( "inputs", NULL ) );
1610
1611         for( i = 0; i < media->i_input; i++ )
1612         {
1613             vlm_MessageAdd( msg_child,
1614                             vlm_MessageNew( media->input[i], NULL ) );
1615         }
1616
1617         vlm_MessageAdd( msg_media,
1618                         vlm_MessageNew( "output", media->psz_output ?
1619                                         media->psz_output : "" ) );
1620
1621         msg_child = vlm_MessageAdd( msg_media, vlm_MessageNew( "options", 0 ));
1622
1623         for( i = 0; i < media->i_option; i++ )
1624         {
1625             vlm_MessageAdd( msg_child, vlm_MessageNew( media->option[i], 0 ) );
1626         }
1627
1628         msg_child = vlm_MessageAdd( msg_media,
1629                                     vlm_MessageNew( "instances", NULL ) );
1630
1631         for( i = 0; i < media->i_instance; i++ )
1632         {
1633             vlm_media_instance_t *p_instance = media->instance[i];
1634             vlc_value_t val;
1635
1636             if( !p_instance->p_input ) val.i_int = END_S;
1637             else var_Get( p_instance->p_input, "state", &val );
1638
1639             vlm_MessageAdd( msg_child,
1640                 vlm_MessageNew( p_instance->psz_name ?
1641                                 p_instance->psz_name : "default",
1642                                 val.i_int == PLAYING_S ? "playing" :
1643                                 val.i_int == PAUSE_S ? "paused" :
1644                                 "stopped" ) );
1645         }
1646
1647         return msg;
1648
1649     }
1650
1651     else if( schedule != NULL )
1652     {
1653         int i;
1654         vlm_message_t *msg;
1655         vlm_message_t *msg_schedule;
1656         vlm_message_t *msg_child;
1657         char buffer[100];
1658
1659         msg = vlm_MessageNew( "show", NULL );
1660         msg_schedule =
1661             vlm_MessageAdd( msg, vlm_MessageNew( schedule->psz_name, 0 ) );
1662
1663         vlm_MessageAdd( msg_schedule, vlm_MessageNew("type", "schedule") );
1664
1665         vlm_MessageAdd( msg_schedule,
1666                         vlm_MessageNew( "enabled", schedule->b_enabled ?
1667                                         "yes" : "no" ) );
1668
1669 #if !defined( UNDER_CE )
1670         if( schedule->i_date != 0 )
1671         {
1672             struct tm date;
1673             time_t i_time = (time_t)( schedule->i_date / 1000000 );
1674             char *psz_date;
1675
1676 #ifdef HAVE_LOCALTIME_R
1677             localtime_r( &i_time, &date);
1678 #else
1679             struct tm *p_date = localtime( &i_time );
1680             date = *p_date;
1681 #endif
1682
1683             asprintf( &psz_date, "%d/%d/%d-%d:%d:%d",
1684                       date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
1685                       date.tm_hour, date.tm_min, date.tm_sec );
1686
1687             vlm_MessageAdd( msg_schedule,
1688                             vlm_MessageNew( "date", psz_date ) );
1689             free( psz_date );
1690         }
1691         else
1692         {
1693             vlm_MessageAdd( msg_schedule, vlm_MessageNew("date", "now") );
1694         }
1695
1696         if( schedule->i_period != 0 )
1697         {
1698             time_t i_time = (time_t) ( schedule->i_period / 1000000 );
1699             struct tm date;
1700
1701             date.tm_sec = (int)( i_time % 60 );
1702             i_time = i_time / 60;
1703             date.tm_min = (int)( i_time % 60 );
1704             i_time = i_time / 60;
1705             date.tm_hour = (int)( i_time % 24 );
1706             i_time = i_time / 24;
1707             date.tm_mday = (int)( i_time % 30 );
1708             i_time = i_time / 30;
1709             /* okay, okay, months are not always 30 days long */
1710             date.tm_mon = (int)( i_time % 12 );
1711             i_time = i_time / 12;
1712             date.tm_year = (int)i_time;
1713
1714             sprintf( buffer, "%d/%d/%d-%d:%d:%d", date.tm_year, date.tm_mon,
1715                      date.tm_mday, date.tm_hour, date.tm_min, date.tm_sec);
1716
1717             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", buffer) );
1718         }
1719         else
1720         {
1721             vlm_MessageAdd( msg_schedule, vlm_MessageNew("period", "0") );
1722         }
1723 #endif /* UNDER_CE */
1724
1725         sprintf( buffer, "%d", schedule->i_repeat );
1726         vlm_MessageAdd( msg_schedule, vlm_MessageNew( "repeat", buffer ) );
1727
1728         msg_child =
1729             vlm_MessageAdd( msg_schedule, vlm_MessageNew("commands", 0) );
1730
1731         for( i = 0; i < schedule->i_command; i++ )
1732         {
1733            vlm_MessageAdd( msg_child,
1734                            vlm_MessageNew( schedule->command[i], NULL ) );
1735         }
1736
1737         return msg;
1738
1739     }
1740
1741     else if( psz_filter && !strcmp( psz_filter, "media" ) )
1742     {
1743         int i, j;
1744         vlm_message_t *msg;
1745         vlm_message_t *msg_child;
1746         int i_vod = 0, i_broadcast = 0;
1747         char *psz_count;
1748
1749         for( i = 0; i < vlm->i_media; i++ )
1750         {
1751             if( vlm->media[i]->i_type == VOD_TYPE )
1752                 i_vod ++;
1753             else
1754                 i_broadcast ++;
1755         }
1756
1757         asprintf( &psz_count, "( %d broadcast - %d vod )", i_broadcast, i_vod);
1758
1759         msg = vlm_MessageNew( "show", NULL );
1760         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "media", psz_count ) );
1761         free( psz_count );
1762
1763         for( i = 0; i < vlm->i_media; i++ )
1764         {
1765             vlm_media_t *m = vlm->media[i];
1766             vlm_message_t *msg_media, *msg_instance;
1767
1768             msg_media = vlm_MessageAdd( msg_child,
1769                                         vlm_MessageNew( m->psz_name, 0 ) );
1770
1771             vlm_MessageAdd( msg_media,
1772                             vlm_MessageNew( "type", m->i_type == VOD_TYPE ?
1773                                             "vod" : "broadcast" ) );
1774
1775             vlm_MessageAdd( msg_media,
1776                             vlm_MessageNew( "enabled", m->b_enabled ?
1777                                             "yes" : "no" ) );
1778
1779             if( m->i_type == VOD_TYPE && m->psz_mux )
1780                 vlm_MessageAdd( msg_media,
1781                                 vlm_MessageNew( "mux", m->psz_mux ) );
1782
1783             msg_instance = vlm_MessageAdd( msg_media,
1784                                            vlm_MessageNew( "instances", 0 ) );
1785
1786             for( j = 0; j < m->i_instance; j++ )
1787             {
1788                 vlm_media_instance_t *p_instance = m->instance[j];
1789                 vlc_value_t val;
1790
1791                 if( !p_instance->p_input ) val.i_int = END_S;
1792                 else var_Get( p_instance->p_input, "state", &val );
1793
1794                 vlm_MessageAdd( msg_instance,
1795                     vlm_MessageNew( p_instance->psz_name ?
1796                                     p_instance->psz_name : "default",
1797                                     val.i_int == PLAYING_S ? "playing" :
1798                                     val.i_int == PAUSE_S ? "paused" :
1799                                     "stopped" ) );
1800             }
1801         }
1802
1803         return msg;
1804     }
1805
1806     else if( psz_filter && !strcmp( psz_filter, "schedule" ) )
1807     {
1808         int i;
1809         vlm_message_t *msg;
1810         vlm_message_t *msg_child;
1811
1812         msg = vlm_MessageNew( "show", NULL );
1813         msg_child = vlm_MessageAdd( msg, vlm_MessageNew( "schedule", NULL ) );
1814
1815         for( i = 0; i < vlm->i_schedule; i++ )
1816         {
1817             vlm_schedule_t *s = vlm->schedule[i];
1818             vlm_message_t *msg_schedule;
1819             mtime_t i_time, i_next_date;
1820
1821             msg_schedule = vlm_MessageAdd( msg_child,
1822                                            vlm_MessageNew( s->psz_name, 0 ) );
1823             vlm_MessageAdd( msg_schedule,
1824                             vlm_MessageNew( "enabled", s->b_enabled ?
1825                                             "yes" : "no" ) );
1826
1827             if( !s->b_enabled ) return msg;
1828
1829
1830             vlm_MessageAdd( msg_schedule,
1831                             vlm_MessageNew( "enabled", "yes" ) );
1832
1833             /* calculate next date */
1834             i_time = vlm_Date();
1835             i_next_date = s->i_date;
1836
1837             if( s->i_period != 0 )
1838             {
1839                 int j = 0;
1840                 while( s->i_date + j * s->i_period <= i_time &&
1841                        s->i_repeat > j )
1842                 {
1843                     j++;
1844                 }
1845
1846                 i_next_date = s->i_date + j * s->i_period;
1847             }
1848
1849             if( i_next_date > i_time )
1850             {
1851                 time_t i_date = (time_t) (i_next_date / 1000000) ;
1852
1853 #if !defined( UNDER_CE )
1854 #ifdef HAVE_CTIME_R
1855                 char psz_date[500];
1856                 ctime_r( &i_date, psz_date );
1857 #else
1858                 char *psz_date = ctime( &i_date );
1859 #endif
1860
1861                 vlm_MessageAdd( msg_schedule,
1862                                 vlm_MessageNew( "next launch", psz_date ) );
1863 #endif
1864             }
1865         }
1866
1867         return msg;
1868     }
1869
1870     else if( psz_filter == NULL && media == NULL && schedule == NULL )
1871     {
1872         vlm_message_t *show1 = vlm_Show( vlm, NULL, NULL, "media" );
1873         vlm_message_t *show2 = vlm_Show( vlm, NULL, NULL, "schedule" );
1874
1875         vlm_MessageAdd( show1, show2->child[0] );
1876
1877         /* We must destroy the parent node "show" of show2
1878          * and not the children */
1879         free( show2->psz_name );
1880         free( show2 );
1881
1882         return show1;
1883     }
1884
1885     else
1886     {
1887         return vlm_MessageNew( "show", NULL );
1888     }
1889 }
1890
1891 static vlm_message_t *vlm_Help( vlm_t *vlm, char *psz_filter )
1892 {
1893     vlm_message_t *message, *message_child;
1894
1895 #define MessageAdd( a ) \
1896         vlm_MessageAdd( message, vlm_MessageNew( a, NULL ) );
1897 #define MessageAddChild( a ) \
1898         vlm_MessageAdd( message_child, vlm_MessageNew( a, NULL ) );
1899
1900     if( psz_filter == NULL )
1901     {
1902         message = vlm_MessageNew( "help", NULL );
1903
1904         message_child = MessageAdd( "Commands Syntax:" );
1905         MessageAddChild( "new (name) vod|broadcast|schedule [properties]" );
1906         MessageAddChild( "setup (name) (properties)" );
1907         MessageAddChild( "show [(name)|media|schedule]" );
1908         MessageAddChild( "del (name)|all|media|schedule" );
1909         MessageAddChild( "control (name) [instance_name] (command)" );
1910         MessageAddChild( "save (config_file)" );
1911         MessageAddChild( "load (config_file)" );
1912
1913         message_child = MessageAdd( "Media Proprieties Syntax:" );
1914         MessageAddChild( "input (input_name)" );
1915         MessageAddChild( "inputdel (input_name)|all" );
1916         MessageAddChild( "inputdeln input_number" );
1917         MessageAddChild( "output (output_name)" );
1918         MessageAddChild( "option (option_name)[=value]" );
1919         MessageAddChild( "enabled|disabled" );
1920         MessageAddChild( "loop|unloop (broadcast only)" );
1921         MessageAddChild( "mux (mux_name)" );
1922
1923         message_child = MessageAdd( "Schedule Proprieties Syntax:" );
1924         MessageAddChild( "enabled|disabled" );
1925         MessageAddChild( "append (command_until_rest_of_the_line)" );
1926         MessageAddChild( "date (year)/(month)/(day)-(hour):(minutes):"
1927                          "(seconds)|now" );
1928         MessageAddChild( "period (years_aka_12_months)/(months_aka_30_days)/"
1929                          "(days)-(hours):(minutes):(seconds)" );
1930         MessageAddChild( "repeat (number_of_repetitions)" );
1931
1932         message_child = MessageAdd( "Control Commands Syntax:" );
1933         MessageAddChild( "play" );
1934         MessageAddChild( "pause" );
1935         MessageAddChild( "stop" );
1936         MessageAddChild( "seek (percentage)" );
1937
1938         return message;
1939     }
1940
1941     return vlm_MessageNew( "help", NULL );
1942 }
1943
1944 /*****************************************************************************
1945  * Config handling functions
1946  *****************************************************************************/
1947 static int Load( vlm_t *vlm, char *file )
1948 {
1949     char *pf = file;
1950
1951     while( *pf != '\0' )
1952     {
1953         vlm_message_t *message = NULL;
1954         int i_end = 0;
1955
1956         while( pf[i_end] != '\n' && pf[i_end] != '\0' && pf[i_end] != '\r' )
1957         {
1958             i_end++;
1959         }
1960
1961         if( pf[i_end] == '\r' || pf[i_end] == '\n' )
1962         {
1963             pf[i_end] = '\0';
1964             i_end++;
1965             if( pf[i_end] == '\n' ) i_end++;
1966         }
1967
1968         if( *pf && ExecuteCommand( vlm, pf, &message ) )
1969         {
1970             if( message ) free( message );
1971             return 1;
1972         }
1973         if( message ) free( message );
1974
1975         pf += i_end;
1976     }
1977
1978     return 0;
1979 }
1980
1981 static char *Save( vlm_t *vlm )
1982 {
1983     char *save = NULL;
1984     char *p;
1985     int i,j;
1986     int i_length = 0;
1987
1988     for( i = 0; i < vlm->i_media; i++ )
1989     {
1990         vlm_media_t *media = vlm->media[i];
1991
1992         if( media->i_type == VOD_TYPE )
1993         {
1994             i_length += strlen( "new  vod " ) + strlen(media->psz_name);
1995         }
1996         else
1997         {
1998             i_length += strlen( "new  broadcast " ) + strlen(media->psz_name);
1999         }
2000
2001         if( media->b_enabled == VLC_TRUE )
2002         {
2003             i_length += strlen( "enabled" );
2004         }
2005         else
2006         {
2007             i_length += strlen( "disabled" );
2008         }
2009
2010         if( media->b_loop == VLC_TRUE )
2011         {
2012             i_length += strlen( " loop\n" );
2013         }
2014         else
2015         {
2016             i_length += strlen( "\n" );
2017         }
2018
2019         for( j = 0; j < media->i_input; j++ )
2020         {
2021             i_length += strlen( "setup  input \"\"\n" ) +
2022                 strlen( media->psz_name ) + strlen( media->input[j] );
2023         }
2024
2025         if( media->psz_output != NULL )
2026         {
2027             i_length += strlen(media->psz_name) + strlen(media->psz_output) +
2028                 strlen( "setup  output \n" );
2029         }
2030
2031         for( j=0 ; j < media->i_option ; j++ )
2032         {
2033             i_length += strlen(media->psz_name) + strlen(media->option[j]) +
2034                 strlen("setup  option \n");
2035         }
2036     }
2037
2038     for( i = 0; i < vlm->i_schedule; i++ )
2039     {
2040         vlm_schedule_t *schedule = vlm->schedule[i];
2041
2042         i_length += strlen( "new  schedule " ) + strlen( schedule->psz_name );
2043
2044         if( schedule->b_enabled == VLC_TRUE )
2045         {
2046             i_length += strlen( "date //-:: enabled\n" ) + 14;
2047         }
2048         else
2049         {
2050             i_length += strlen( "date //-:: disabled\n" ) + 14;
2051         }
2052
2053
2054         if( schedule->i_period != 0 )
2055         {
2056             i_length += strlen( "setup  " ) + strlen( schedule->psz_name ) +
2057                 strlen( "period //-::\n" ) + 14;
2058         }
2059
2060         if( schedule->i_repeat >= 0 )
2061         {
2062             char buffer[12];
2063
2064             sprintf( buffer, "%d", schedule->i_repeat );
2065             i_length += strlen( "setup  repeat \n" ) +
2066                 strlen( schedule->psz_name ) + strlen( buffer );
2067         }
2068         else
2069         {
2070             i_length++;
2071         }
2072
2073         for( j = 0; j < schedule->i_command; j++ )
2074         {
2075             i_length += strlen( "setup  append \n" ) +
2076                 strlen( schedule->psz_name ) + strlen( schedule->command[j] );
2077         }
2078
2079     }
2080
2081     /* Don't forget the '\0' */
2082     i_length++;
2083     /* now we have the length of save */
2084
2085     p = save = malloc( i_length );
2086     *save = '\0';
2087
2088     /* finally we can write in it */
2089     for( i = 0; i < vlm->i_media; i++ )
2090     {
2091         vlm_media_t *media = vlm->media[i];
2092
2093         if( media->i_type == VOD_TYPE )
2094         {
2095             p += sprintf( p, "new %s vod ", media->psz_name);
2096         }
2097         else
2098         {
2099             p += sprintf( p, "new %s broadcast ", media->psz_name);
2100         }
2101
2102         if( media->b_enabled == VLC_TRUE )
2103         {
2104             p += sprintf( p, "enabled" );
2105         }
2106         else
2107         {
2108             p += sprintf( p, "disabled" );
2109         }
2110
2111         if( media->b_loop == VLC_TRUE )
2112         {
2113             p += sprintf( p, " loop\n" );
2114         }
2115         else
2116         {
2117             p += sprintf( p, "\n" );
2118         }
2119
2120         for( j = 0; j < media->i_input; j++ )
2121         {
2122             p += sprintf( p, "setup %s input \"%s\"\n", media->psz_name,
2123                           media->input[j] );
2124         }
2125
2126         if( media->psz_output != NULL )
2127         {
2128             p += sprintf( p, "setup %s output %s\n", media->psz_name,
2129                           media->psz_output );
2130         }
2131
2132         for( j = 0; j < media->i_option; j++ )
2133         {
2134             p += sprintf( p, "setup %s option %s\n", media->psz_name,
2135                           media->option[j] );
2136         }
2137     }
2138
2139     /* and now, the schedule scripts */
2140 #if !defined( UNDER_CE )
2141     for( i = 0; i < vlm->i_schedule; i++ )
2142     {
2143         vlm_schedule_t *schedule = vlm->schedule[i];
2144         struct tm date;
2145         time_t i_time = (time_t) ( schedule->i_date / 1000000 );
2146
2147 #ifdef HAVE_LOCALTIME_R
2148         localtime_r( &i_time, &date);
2149 #else
2150         struct tm *p_date = localtime( &i_time );
2151         date = *p_date;
2152 #endif
2153
2154         p += sprintf( p, "new %s schedule ", schedule->psz_name);
2155
2156         if( schedule->b_enabled == VLC_TRUE )
2157         {
2158             p += sprintf( p, "date %d/%d/%d-%d:%d:%d enabled\n",
2159                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2160                           date.tm_hour, date.tm_min, date.tm_sec );
2161         }
2162         else
2163         {
2164             p += sprintf( p, "date %d/%d/%d-%d:%d:%d disabled\n",
2165                           date.tm_year + 1900, date.tm_mon + 1, date.tm_mday,
2166                           date.tm_hour, date.tm_min, date.tm_sec);
2167         }
2168
2169         if( schedule->i_period != 0 )
2170         {
2171             p += sprintf( p, "setup %s ", schedule->psz_name );
2172
2173             i_time = (time_t) ( schedule->i_period / 1000000 );
2174
2175             date.tm_sec = (int)( i_time % 60 );
2176             i_time = i_time / 60;
2177             date.tm_min = (int)( i_time % 60 );
2178             i_time = i_time / 60;
2179             date.tm_hour = (int)( i_time % 24 );
2180             i_time = i_time / 24;
2181             date.tm_mday = (int)( i_time % 30 );
2182             i_time = i_time / 30;
2183             /* okay, okay, months are not always 30 days long */
2184             date.tm_mon = (int)( i_time % 12 );
2185             i_time = i_time / 12;
2186             date.tm_year = (int)i_time;
2187
2188             p += sprintf( p, "period %d/%d/%d-%d:%d:%d\n",
2189                           date.tm_year, date.tm_mon, date.tm_mday,
2190                           date.tm_hour, date.tm_min, date.tm_sec);
2191         }
2192
2193         if( schedule->i_repeat >= 0 )
2194         {
2195             p += sprintf( p, "setup %s repeat %d\n",
2196                           schedule->psz_name, schedule->i_repeat );
2197         }
2198         else
2199         {
2200             p += sprintf( p, "\n" );
2201         }
2202
2203         for( j = 0; j < schedule->i_command; j++ )
2204         {
2205             p += sprintf( p, "setup %s append %s\n",
2206                           schedule->psz_name, schedule->command[j] );
2207         }
2208
2209     }
2210 #endif /* UNDER_CE */
2211
2212     return save;
2213 }
2214
2215 /*****************************************************************************
2216  * Manage:
2217  *****************************************************************************/
2218 int vlm_MediaVodControl( void *p_private, vod_media_t *p_vod_media,
2219                          char *psz_id, int i_query, va_list args )
2220 {
2221     vlm_t *vlm = (vlm_t *)p_private;
2222     int i, i_ret = VLC_EGENERIC;
2223
2224     if( !p_private || !p_vod_media ) return VLC_EGENERIC;
2225
2226     vlc_mutex_lock( &vlm->lock );
2227
2228     /* Find media */
2229     for( i = 0; i < vlm->i_media; i++ )
2230     {
2231         if( p_vod_media == vlm->media[i]->vod_media ) break;
2232     }
2233
2234     if( i == vlm->i_media )
2235     {
2236         vlc_mutex_unlock( &vlm->lock );
2237         return VLC_EGENERIC;
2238     }
2239
2240     switch( i_query )
2241     {
2242     case VOD_MEDIA_PLAY:
2243         vlm->media[i]->psz_vod_output = (char *)va_arg( args, char * );
2244         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "play", 0 );
2245         vlm->media[i]->psz_vod_output = 0;
2246         break;
2247
2248     case VOD_MEDIA_PAUSE:
2249         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "pause", 0 );
2250         break;
2251
2252     case VOD_MEDIA_STOP:
2253         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "stop", 0 );
2254         break;
2255
2256     case VOD_MEDIA_SEEK:
2257     {
2258         double f_pos = (double)va_arg( args, double );
2259         char psz_pos[50];
2260
2261         sprintf( psz_pos, "%f", f_pos );
2262         i_ret = vlm_MediaControl( vlm, vlm->media[i], psz_id, "seek", psz_pos);
2263         break;
2264     }
2265
2266     default:
2267         break;
2268     }
2269
2270     vlc_mutex_unlock( &vlm->lock );
2271
2272     return i_ret;
2273 }
2274
2275 /*****************************************************************************
2276  * Manage:
2277  *****************************************************************************/
2278 static int Manage( vlc_object_t* p_object )
2279 {
2280     vlm_t *vlm = (vlm_t*)p_object;
2281     int i, j;
2282     mtime_t i_lastcheck;
2283     mtime_t i_time;
2284
2285     i_lastcheck = vlm_Date();
2286
2287     msleep( 100000 );
2288
2289     while( !vlm->b_die )
2290     {
2291         vlc_mutex_lock( &vlm->lock );
2292
2293         /* destroy the inputs that wants to die, and launch the next input */
2294         for( i = 0; i < vlm->i_media; i++ )
2295         {
2296             vlm_media_t *p_media = vlm->media[i];
2297
2298             for( j = 0; j < p_media->i_instance; j++ )
2299             {
2300                 vlm_media_instance_t *p_instance = p_media->instance[j];
2301
2302                 if( !p_instance->p_input ||
2303                     ( !p_instance->p_input->b_eof &&
2304                       !p_instance->p_input->b_error ) ) continue;
2305
2306                 input_StopThread( p_instance->p_input );
2307                 input_DestroyThread( p_instance->p_input );
2308                 vlc_object_detach( p_instance->p_input );
2309                 vlc_object_destroy( p_instance->p_input );
2310
2311                 p_instance->i_index++;
2312                 if( p_instance->i_index == p_media->i_input &&
2313                     p_media->b_loop ) p_instance->i_index = 0;
2314
2315                 if( p_instance->i_index < p_media->i_input )
2316                 {
2317                     /* FIXME, find a way to select the right instance */
2318                     char buffer[12];
2319                     sprintf( buffer, "%d", p_instance->i_index );
2320                     vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2321                                       "play", buffer );
2322                 }
2323                 else
2324                 {
2325                     if( vlm_MediaControl( vlm, p_media, p_instance->psz_name,
2326                                           "stop", 0 ) == VLC_SUCCESS ) i--;
2327                 }
2328             }
2329         }
2330
2331         /* scheduling */
2332         i_time = vlm_Date();
2333
2334         for( i = 0; i < vlm->i_schedule; i++ )
2335         {
2336             mtime_t i_real_date = vlm->schedule[i]->i_date;
2337
2338             if( vlm->schedule[i]->b_enabled == VLC_TRUE )
2339             {
2340                 if( vlm->schedule[i]->i_date == 0 ) // now !
2341                 {
2342                     vlm->schedule[i]->i_date = (i_time / 1000000) * 1000000 ;
2343                     i_real_date = i_time;
2344                 }
2345                 else if( vlm->schedule[i]->i_period != 0 )
2346                 {
2347                     int j = 0;
2348                     while( vlm->schedule[i]->i_date + j *
2349                            vlm->schedule[i]->i_period <= i_lastcheck &&
2350                            ( vlm->schedule[i]->i_repeat > j ||
2351                              vlm->schedule[i]->i_repeat == -1 ) )
2352                     {
2353                         j++;
2354                     }
2355
2356                     i_real_date = vlm->schedule[i]->i_date + j *
2357                         vlm->schedule[i]->i_period;
2358                 }
2359
2360                 if( i_real_date <= i_time && i_real_date > i_lastcheck )
2361                 {
2362                     for( j = 0; j < vlm->schedule[i]->i_command; j++ )
2363                     {
2364                         vlm_message_t *message = NULL;
2365
2366                         ExecuteCommand( vlm, vlm->schedule[i]->command[j],
2367                                         &message );
2368
2369                         /* for now, drop the message */
2370                         free( message );
2371                     }
2372                 }
2373             }
2374         }
2375
2376         i_lastcheck = i_time;
2377
2378         vlc_mutex_unlock( &vlm->lock );
2379
2380         msleep( 100000 );
2381     }
2382
2383     return VLC_SUCCESS;
2384 }
2385
2386 #else /* ENABLE_VLM */
2387
2388 /* We just define an empty wrapper */
2389 vlm_t *__vlm_New( vlc_object_t *a )
2390 {
2391     msg_Err( a, "VideoLAN manager support is disabled" );
2392     return 0;
2393 }
2394 void vlm_Delete( vlm_t *a ){}
2395 int vlm_ExecuteCommand( vlm_t *a, char *b, vlm_message_t **c ){ return -1; }
2396 void vlm_MessageDelete( vlm_message_t *a ){}
2397 vlm_media_t *vlm_MediaNew( vlm_t *a, char *b, int c ){ return NULL; }
2398 void vlm_MediaDelete( vlm_t *a, vlm_media_t *b, char *c ){}
2399 int vlm_MediaSetup( vlm_t *a, vlm_media_t *b, char *c, char *d ){ return -1; }
2400 int vlm_MediaControl( vlm_t *a, vlm_media_t *b, char *c, char *d, char *e )
2401     { return -1; }
2402 vlm_schedule_t * vlm_ScheduleNew( vlm_t *a, char *b ){ return NULL; }
2403 void  vlm_ScheduleDelete( vlm_t *a, vlm_schedule_t *b, char *c ){}
2404 int vlm_ScheduleSetup( vlm_schedule_t *a, char *b, char *c ){ return -1; }
2405 int vlm_MediaVodControl( void *a, vod_media_t *b, char *c, int d, va_list e )
2406     { return -1; }
2407 int vlm_Save( vlm_t *a, char *b ){ return -1; }
2408 int vlm_Load( vlm_t *a, char *b ){ return -1; }
2409
2410 #endif /* ENABLE_VLM */