]> git.sesse.net Git - vlc/blob - src/interface/interface.c
3636431bd628c5cd5655a7abf5117d92fc07e5aa
[vlc] / src / interface / interface.c
1 /*****************************************************************************
2  * interface.c: interface access for other threads
3  * This library provides basic functions for threads to interact with user
4  * interface, such as command line.
5  *****************************************************************************
6  * Copyright (C) 1998, 1999, 2000 VideoLAN
7  *
8  * Authors:
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public
21  * License along with this program; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <stdio.h>                                              /* sprintf() */
31 #include <stdlib.h>                                      /* free(), strtol() */
32 #include <string.h>                                            /* strerror() */
33 #include <sys/types.h>                        /* on BSD, uio.h needs types.h */
34 #include <sys/uio.h>                                          /* for input.h */
35
36 #include <dlfcn.h>                                                /* plugins */
37
38 #include "config.h"
39 #include "common.h"
40 #include "mtime.h"
41 #include "threads.h"
42 #include "input.h"
43
44 #include "intf_msg.h"
45 #include "interface.h"
46 #include "intf_cmd.h"
47 #include "intf_console.h"
48
49 #include "video.h"
50 #include "video_output.h"
51
52 #include "main.h"
53
54 /*****************************************************************************
55  * intf_channel_t: channel description
56  *****************************************************************************
57  * A 'channel' is a descriptor of an input method. It is used to switch easily
58  * from source to source without having to specify the whole input thread
59  * configuration. The channels array, stored in the interface thread object, is
60  * loaded in intf_Create, and unloaded in intf_Destroy.
61  *****************************************************************************/
62 typedef struct intf_channel_s
63 {
64     /* Channel description */
65     int         i_channel;            /* channel number, -1 for end of array */
66     char *      psz_description;              /* channel description (owned) */
67
68     /* Input configuration */
69     int         i_input_method;                   /* input method descriptor */
70     char *      psz_input_source;                   /* source string (owned) */
71     int         i_input_port;                                        /* port */
72     int         i_input_vlan;                                        /* vlan */
73 } intf_channel_t;
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int      LoadChannels    ( intf_thread_t *p_intf, char *psz_filename );
79 static void     UnloadChannels  ( intf_thread_t *p_intf );
80 static int      ParseChannel    ( intf_channel_t *p_channel, char *psz_str );
81
82 /*****************************************************************************
83  * intf_Create: prepare interface before main loop
84  *****************************************************************************
85  * This function opens output devices and creates specific interfaces. It sends
86  * its own error messages.
87  *****************************************************************************/
88 intf_thread_t* intf_Create( void )
89 {
90     intf_thread_t *p_intf;
91     char * psz_method;
92     char * psz_plugin;
93
94     /* Allocate structure */
95     p_intf = malloc( sizeof( intf_thread_t ) );
96     if( !p_intf )
97     {
98         intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );
99         return( NULL );
100     }
101
102     /* Initialize method-dependent functions */
103     psz_method = main_GetPszVariable( VOUT_METHOD_VAR, VOUT_DEFAULT_METHOD );
104
105     psz_plugin = malloc( sizeof("./interface/intf_.so") + strlen(psz_method) );
106     sprintf( psz_plugin, "./interface/intf_%s.so", psz_method );
107
108     p_intf->p_intf_plugin = dlopen( psz_plugin, RTLD_NOW | RTLD_GLOBAL );
109
110     if( p_intf->p_intf_plugin == NULL )
111     {
112         intf_ErrMsg( "error: could not open interface plugin %s\n", psz_plugin );
113         free( psz_plugin );
114         free( p_intf );
115         return( NULL );
116     }
117     free( psz_plugin );
118
119     /* Get plugins */
120     p_intf->p_sys_create =  dlsym(p_intf->p_intf_plugin, "intf_SysCreate");
121     p_intf->p_sys_manage =  dlsym(p_intf->p_intf_plugin, "intf_SysManage");
122     p_intf->p_sys_destroy = dlsym(p_intf->p_intf_plugin, "intf_SysDestroy");
123
124     /* Initialize structure */
125     p_intf->b_die =     0;
126     p_intf->p_vout =    NULL;
127     p_intf->p_input =   NULL;
128
129     /* Load channels - the pointer will be set to NULL on failure. The
130      * return value is ignored since the program can work without
131      * channels */
132     LoadChannels( p_intf, main_GetPszVariable( INTF_CHANNELS_VAR, INTF_CHANNELS_DEFAULT ));
133
134     /* Start interfaces */
135     p_intf->p_console = intf_ConsoleCreate();
136     if( p_intf->p_console == NULL )
137     {
138         intf_ErrMsg("error: can't create control console\n");
139         dlclose( p_intf->p_intf_plugin );
140         free( p_intf );
141         return( NULL );
142     }
143     if( p_intf->p_sys_create( p_intf ) )
144     {
145         intf_ErrMsg("error: can't create interface\n");
146         intf_ConsoleDestroy( p_intf->p_console );
147         dlclose( p_intf->p_intf_plugin );
148         free( p_intf );
149         return( NULL );
150     }
151
152     intf_Msg("Interface initialized\n");
153     return( p_intf );
154 }
155
156 /*****************************************************************************
157  * intf_Run
158  *****************************************************************************
159  * Initialization script and main interface loop.
160  *****************************************************************************/
161 void intf_Run( intf_thread_t *p_intf )
162 {
163     /* Execute the initialization script - if a positive number is returned,
164      * the script could be executed but failed */
165     if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
166     {
167         intf_ErrMsg("warning: error(s) during startup script\n");
168     }
169
170     /* Main loop */
171     while(!p_intf->b_die)
172     {
173         /* Flush waiting messages */
174         intf_FlushMsg();
175
176         /* Manage specific interface */
177         p_intf->p_sys_manage( p_intf );
178
179         /* Check attached threads status */
180         if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
181         {
182             /* FIXME: add aout error detection ?? */
183             p_intf->b_die = 1;
184         }
185         if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
186         {
187             input_DestroyThread( p_intf->p_input, NULL );
188             p_intf->p_input = NULL;
189             intf_DbgMsg("Input thread destroyed\n");
190         }
191
192         /* Sleep to avoid using all CPU - since some interfaces needs to access
193          * keyboard events, a 100ms delay is a good compromise */
194         msleep( INTF_IDLE_SLEEP );
195     }
196 }
197
198 /*****************************************************************************
199  * intf_Destroy: clean interface after main loop
200  *****************************************************************************
201  * This function destroys specific interfaces and close output devices.
202  *****************************************************************************/
203 void intf_Destroy( intf_thread_t *p_intf )
204 {
205     /* Destroy interfaces */
206     p_intf->p_sys_destroy( p_intf );
207     intf_ConsoleDestroy( p_intf->p_console );
208
209     /* Unload channels */
210     UnloadChannels( p_intf );
211
212     /* Close plugin */
213     dlclose( p_intf->p_intf_plugin );
214
215     /* Free structure */
216     free( p_intf );
217 }
218
219 /*****************************************************************************
220  * intf_SelectChannel: change channel
221  *****************************************************************************
222  * Kill existing input, if any, and try to open a new one, using an input
223  * configuration table.
224  *****************************************************************************/
225 int intf_SelectChannel( intf_thread_t * p_intf, int i_channel )
226 {
227     intf_channel_t *    p_channel;                                /* channel */
228
229     /* Look for channel in array */
230     if( p_intf->p_channel != NULL )
231     {
232         for( p_channel = p_intf->p_channel; p_channel->i_channel != -1; p_channel++ )
233         {
234             if( p_channel->i_channel == i_channel )
235             {
236                 /*
237                  * Change channel
238                  */
239
240                 /* Kill existing input, if any */
241                 if( p_intf->p_input != NULL )
242                 {
243                     input_DestroyThread( p_intf->p_input, NULL );
244                 }
245
246                 intf_Msg("Channel %d: %s\n", i_channel, p_channel->psz_description );
247
248                 /* Open a new input */
249                 p_intf->p_input = input_CreateThread( p_channel->i_input_method, p_channel->psz_input_source,
250                                                       p_channel->i_input_port, p_channel->i_input_vlan,
251                                                       p_intf->p_vout, p_main->p_aout, NULL );
252                 return( p_intf->p_input == NULL );
253             }
254         }
255     }
256
257     /* Channel does not exist */
258     intf_Msg("Channel %d does not exist\n", i_channel );
259     return( 1 );
260 }
261
262 /*****************************************************************************
263  * intf_ProcessKey: process standard keys
264  *****************************************************************************
265  * This function will process standard keys and return non 0 if the key was
266  * unknown.
267  *****************************************************************************/
268 int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
269 {
270     switch( i_key )
271     {
272     case 'Q':                                                  /* quit order */
273     case 'q':
274     case 27:                                                   /* escape key */
275     case 3:                                                            /* ^C */
276         p_intf->b_die = 1;
277         break;
278     case '0':                                               /* source change */
279     case '1':
280     case '2':
281     case '3':
282     case '4':
283     case '5':
284     case '6':
285     case '7':
286     case '8':
287     case '9':
288         /* Change channel - return code is ignored since SelectChannel displays
289          * its own error messages */
290         intf_SelectChannel( p_intf, i_key - '0' );
291         break;
292     case '+':                                                    /* volume + */
293         /* XXX?? */
294         break;
295     case '-':                                                    /* volume - */
296         /* XXX?? */
297         break;
298     case 'M':                                                 /* toggle mute */
299     case 'm':
300         /* XXX?? */
301         break;
302     case 'g':                                                     /* gamma - */
303         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
304         {
305             vlc_mutex_lock( &p_intf->p_vout->change_lock );
306             p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;
307             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
308             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
309         }
310         break;
311     case 'G':                                                     /* gamma + */
312         if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
313         {
314             vlc_mutex_lock( &p_intf->p_vout->change_lock );
315             p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
316             p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
317             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
318         }
319         break;
320     case 'c':                                            /* toggle grayscale */
321         if( p_intf->p_vout != NULL )
322         {
323             vlc_mutex_lock( &p_intf->p_vout->change_lock );
324             p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;
325             p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;
326             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
327         }
328         break;
329     case ' ':                                            /* toggle interface */
330         if( p_intf->p_vout != NULL )
331         {
332             vlc_mutex_lock( &p_intf->p_vout->change_lock );
333             p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;
334             p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;
335             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
336         }
337         break;
338     case 'i':                                                 /* toggle info */
339         if( p_intf->p_vout != NULL )
340         {
341             vlc_mutex_lock( &p_intf->p_vout->change_lock );
342             p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;
343             p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;
344             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
345         }
346         break;
347     case 's':                                              /* toggle scaling */
348         if( p_intf->p_vout != NULL )
349         {
350             vlc_mutex_lock( &p_intf->p_vout->change_lock );
351             p_intf->p_vout->b_scale    = !p_intf->p_vout->b_scale;
352             p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;
353             vlc_mutex_unlock( &p_intf->p_vout->change_lock );
354         }
355         break;
356    default:                                                   /* unknown key */
357         return( 1 );
358     }
359
360     return( 0 );
361 }
362
363 /* following functions are local */
364
365 /*****************************************************************************
366  * LoadChannels: load channels description from a file
367  *****************************************************************************
368  * This structe describes all interface-specific data of the main (interface)
369  * thread.
370  * Each line of the file is a semicolon separated list of the following
371  * fields :
372  *      integer         channel number
373  *      string          channel description
374  *      integer         input method (see input.h)
375  *      string          input source
376  *      integer         input port
377  *      integer         input vlan
378  * The last field must end with a semicolon.
379  * Comments and empty lines are not explicitely allowed, but lines with parsing
380  * errors are ignored without warning.
381  *****************************************************************************/
382 static int LoadChannels( intf_thread_t *p_intf, char *psz_filename )
383 {
384     FILE *              p_file;                                      /* file */
385     intf_channel_t *    p_channel;                        /* current channel */
386     char                psz_line[INTF_MAX_CMD_SIZE];          /* line buffer */
387     int                 i_index;                   /* channel or field index */
388
389     /* Set default value */
390     p_intf->p_channel = NULL;
391
392     /* Open file */
393     p_file = fopen( psz_filename, "r" );
394     if( p_file == NULL )
395     {
396         intf_ErrMsg("error: can't open %s (%s)\n", psz_filename, strerror(errno));
397         return( 1 );
398     }
399
400     /* First pass: count number of lines */
401     for( i_index = 0; fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL; i_index++ )
402     {
403         ;
404     }
405
406     if( i_index != 0 )
407     {
408         /* Allocate array and rewind - some of the lines may be invalid, and the
409          * array will probably be larger than the actual number of channels, but
410          * it has no consequence. */
411         p_intf->p_channel = malloc( sizeof( intf_channel_t ) * i_index );
412         if( p_intf->p_channel == NULL )
413         {
414             intf_ErrMsg("error: %s\n", strerror(ENOMEM));
415             fclose( p_file );
416             return( 1 );
417         }
418         p_channel = p_intf->p_channel;
419         rewind( p_file );
420
421         /* Second pass: read channels descriptions */
422         while( fgets( psz_line, INTF_MAX_CMD_SIZE, p_file ) != NULL )
423         {
424             if( !ParseChannel( p_channel, psz_line ) )
425             {
426                 intf_DbgMsg("channel [%d] %s : method %d (%s:%d vlan %d)\n",
427                             p_channel->i_channel, p_channel->psz_description,
428                             p_channel->i_input_method, p_channel->psz_input_source,
429                             p_channel->i_input_port, p_channel->i_input_vlan );
430                 p_channel++;
431             }
432         }
433
434         /* Add marker at the end of the array */
435         p_channel->i_channel = -1;
436     }
437
438     /* Close file */
439     fclose( p_file );
440     return( 0 );
441 }
442
443 /*****************************************************************************
444  * UnloadChannels: unload channels description
445  *****************************************************************************
446  * This function free all resources allocated by LoadChannels, if any.
447  *****************************************************************************/
448 static void UnloadChannels( intf_thread_t *p_intf )
449 {
450     int i_channel;                                          /* channel index */
451
452     if( p_intf->p_channel != NULL )
453     {
454         /* Free allocated strings */
455         for( i_channel = 0;
456              p_intf->p_channel[ i_channel ].i_channel != -1;
457              i_channel++ )
458         {
459             if( p_intf->p_channel[ i_channel ].psz_description != NULL )
460             {
461                 free( p_intf->p_channel[ i_channel ].psz_description );
462             }
463             if( p_intf->p_channel[ i_channel ].psz_input_source != NULL )
464             {
465                 free( p_intf->p_channel[ i_channel ].psz_input_source );
466             }
467         }
468
469         /* Free array */
470         free( p_intf->p_channel );
471         p_intf->p_channel = NULL;
472     }
473 }
474
475
476 /*****************************************************************************
477  * ParseChannel: parse a channel description line
478  *****************************************************************************
479  * See LoadChannels. This function return non 0 on parsing error.
480  *****************************************************************************/
481 static int ParseChannel( intf_channel_t *p_channel, char *psz_str )
482 {
483     char *      psz_index;                              /* current character */
484     char *      psz_end;                           /* end pointer for strtol */
485     int         i_field;                        /* field number, -1 on error */
486     int         i_field_length;             /* field length, for text fields */
487
488     /* Set some default fields */
489     p_channel->i_channel =              0;
490     p_channel->psz_description =        NULL;
491     p_channel->i_input_method =         0;
492     p_channel->psz_input_source =       NULL;
493     p_channel->i_input_port =           0;
494     p_channel->i_input_vlan =           0;
495
496     /* Parse string */
497     i_field = 0;
498     for( psz_index = psz_str; (i_field != -1) && (*psz_index != '\0'); psz_index++ )
499     {
500         if( *psz_index == ';' )
501         {
502             /* Mark end of field */
503             *psz_index = '\0';
504
505             /* Parse field */
506             switch( i_field++ )
507             {
508             case 0:                                        /* channel number */
509                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
510                 if( (*psz_str == '\0') || (*psz_end != '\0') )
511                 {
512                     i_field = -1;
513                 }
514                 break;
515             case 1:                                   /* channel description */
516                 i_field_length = strlen( psz_str );
517                 if( i_field_length != 0 )
518                 {
519                     p_channel->psz_description = malloc( i_field_length + 1 );
520                     if( p_channel->psz_description == NULL )
521                     {
522                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
523                         i_field = -1;
524                     }
525                     else
526                     {
527                         strcpy( p_channel->psz_description, psz_str );
528                     }
529                 }
530                 break;
531             case 2:                                          /* input method */
532                 p_channel->i_input_method = strtol( psz_str, &psz_end, 0);
533                 if( (*psz_str == '\0') || (*psz_end != '\0') )
534                 {
535                     i_field = -1;
536                 }
537                 break;
538             case 3:                                          /* input source */
539                 i_field_length = strlen( psz_str );
540                 if( i_field_length != 0 )
541                 {
542                     p_channel->psz_input_source = malloc( i_field_length + 1 );
543                     if( p_channel->psz_input_source == NULL )
544                     {
545                         intf_ErrMsg("error: %s\n", strerror( ENOMEM ));
546                         i_field = -1;
547                     }
548                     else
549                     {
550                         strcpy( p_channel->psz_input_source, psz_str );
551                     }
552                 }
553                 break;
554             case 4:                                            /* input port */
555                 p_channel->i_input_port = strtol( psz_str, &psz_end, 0);
556                 if( (*psz_str == '\0') || (*psz_end != '\0') )
557                 {
558                     i_field = -1;
559                 }
560                 break;
561             case 5:                                            /* input vlan */
562                 p_channel->i_channel = strtol( psz_str, &psz_end, 0);
563                 if( (*psz_str == '\0') || (*psz_end != '\0') )
564                 {
565                     i_field = -1;
566                 }
567                 break;
568                 /* ... following fields are ignored */
569             }
570
571             /* Set new beginning of field */
572             psz_str = psz_index + 1;
573         }
574     }
575
576     /* At least the first three fields must be parsed sucessfully for function
577      * success. Other parsing errors are returned using i_field = -1. */
578     if( i_field < 3 )
579     {
580         /* Function fails. Free allocated strings */
581         if( p_channel->psz_description != NULL )
582         {
583             free( p_channel->psz_description );
584         }
585         if( p_channel->psz_input_source != NULL )
586         {
587             free( p_channel->psz_input_source );
588         }
589         return( 1 );
590     }
591
592     /* Return success */
593     return( 0 );
594 }