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