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