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