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