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