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