]> git.sesse.net Git - vlc/blobdiff - src/interface/interface.c
* ./modules/access/dvdplay/demux.c: the dvdplay plugin no longer sets the
[vlc] / src / interface / interface.c
index 9e22ba2d024ccc6b51f056cc599962961ee94241..57c6f88621a4dd981fea1cceeaa675b052d50ff6 100644 (file)
-/*******************************************************************************
+/*****************************************************************************
  * interface.c: interface access for other threads
- * (c)1998 VideoLAN
- *******************************************************************************
  * This library provides basic functions for threads to interact with user
  * interface, such as command line.
- *******************************************************************************/
-
-/*******************************************************************************
+ *****************************************************************************
+ * Copyright (C) 1998-2001 VideoLAN
+ * $Id: interface.c,v 1.102 2003/02/06 23:59:40 sam Exp $
+ *
+ * Authors: Vincent Seguin <seguin@via.ecp.fr>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
  * Preamble
- *******************************************************************************/
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/uio.h>                                        /* for input.h */
+ *****************************************************************************/
+#include <stdlib.h>                                      /* free(), strtol() */
+#include <stdio.h>                                                   /* FILE */
+#include <string.h>                                            /* strerror() */
+
+#include <vlc/vlc.h>
+
+#include "stream_control.h"
+#include "input_ext-intf.h"
+
+#include "audio_output.h"
 
-#include "config.h"
-#include "common.h"
-#include "mtime.h"
-#include "vlc_thread.h"
-#include "input.h"
-#include "intf_msg.h"
 #include "interface.h"
-#include "intf_cmd.h"
-#include "intf_console.h"
-#include "main.h"
+
 #include "video.h"
 #include "video_output.h"
 
-#include "intf_sys.h"
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static void Manager( intf_thread_t *p_intf );
 
-/*******************************************************************************
+/*****************************************************************************
  * intf_Create: prepare interface before main loop
- *******************************************************************************
- * This function opens output devices and create specific interfaces. It send
- * it's own error messages.
- *******************************************************************************/
-intf_thread_t* intf_Create( void )
-{    
-    intf_thread_t *p_intf;                                        
+ *****************************************************************************
+ * This function opens output devices and creates specific interfaces. It sends
+ * its own error messages.
+ *****************************************************************************/
+intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
+{
+    intf_thread_t * p_intf;
+    char *psz_intf;
 
     /* Allocate structure */
-    p_intf = malloc( sizeof( intf_thread_t ) );
+    p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
     if( !p_intf )
     {
-        intf_ErrMsg("error: %s\n", strerror( ENOMEM ) );        
-       return( NULL );
+        msg_Err( p_this, "out of memory" );
+        return NULL;
     }
 
-    /* Initialize structure */
-    p_intf->b_die =     0;    
-    p_intf->p_vout =    NULL;
-    p_intf->p_input =   NULL;   
-
-    /* Start interfaces */
-    p_intf->p_console = intf_ConsoleCreate();
-    if( p_intf->p_console == NULL )
+    /* XXX: workaround for a bug in VLC 0.5.0 where the dvdplay plugin was
+     * registering itself in $interface, which we do not want to happen. */
+    psz_intf = config_GetPsz( p_intf, "interface" );
+    if( psz_intf )
     {
-        intf_ErrMsg("error: can't create control console\n");
-       free( p_intf );
-        return( NULL );
+        if( !strcasecmp( psz_intf, "dvdplay" ) )
+        {
+            config_PutPsz( p_intf, "interface", "" );
+        }
+        free( psz_intf );
     }
-    if( intf_SysCreate( p_intf ) )
-    {
-       intf_ErrMsg("error: can't create interface\n");
-       intf_ConsoleDestroy( p_intf->p_console );
-       free( p_intf );
-       return( NULL );
-    }   
 
-    intf_Msg("Interface initialized\n");    
-    return( p_intf );
-}
+    /* Choose the best module */
+    p_intf->p_module = module_Need( p_intf, "interface", psz_module );
 
-/*******************************************************************************
- * intf_Run
- *******************************************************************************
- * Initialization script and main interface loop.
- *******************************************************************************/
-void intf_Run( intf_thread_t *p_intf )
-{ 
-    /* Execute the initialization script - if a positive number is returned, 
-     * the script could be executed but failed */
-    if( intf_ExecScript( main_GetPszVariable( INTF_INIT_SCRIPT_VAR, INTF_INIT_SCRIPT_DEFAULT ) ) > 0 )
+    if( p_intf->p_module == NULL )
     {
-       intf_ErrMsg("warning: error(s) during startup script\n");
+        msg_Err( p_intf, "no suitable intf module" );
+        vlc_object_destroy( p_intf );
+        return NULL;
     }
 
-    /* Main loop */
-    while(!p_intf->b_die)
-    {
-        /* Flush waiting messages */
-        intf_FlushMsg();
+    /* Initialize structure */
+    p_intf->b_menu        = VLC_FALSE;
+    p_intf->b_menu_change = VLC_FALSE;
+
+    /* Initialize mutexes */
+    vlc_mutex_init( p_intf, &p_intf->change_lock );
 
-        /* Manage specific interface */
-        intf_SysManage( p_intf );
+    msg_Dbg( p_intf, "interface initialized" );
 
-        /* Check attached threads status */
-        if( (p_intf->p_vout != NULL) && p_intf->p_vout->b_error )
+    /* Attach interface to its parent object */
+    vlc_object_attach( p_intf, p_this );
+
+    return p_intf;
+}
+
+/*****************************************************************************
+ * intf_RunThread: launch the interface thread
+ *****************************************************************************
+ * This function either creates a new thread and runs the interface in it,
+ * or runs the interface in the current thread, depending on b_block.
+ *****************************************************************************/
+int intf_RunThread( intf_thread_t *p_intf )
+{
+    if( p_intf->b_block )
+    {
+        /* Run a manager thread, launch the interface, kill the manager */
+        if( vlc_thread_create( p_intf, "manager", Manager,
+                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
         {
-            //?? add aout error detection
-            p_intf->b_die = 1;            
-        }    
-        if( (p_intf->p_input != NULL) && p_intf->p_input->b_error )
+            msg_Err( p_intf, "cannot spawn manager thread" );
+            return VLC_EGENERIC;
+        }
+
+        p_intf->pf_run( p_intf );
+
+        p_intf->b_die = VLC_TRUE;
+
+        /* Do not join the thread... intf_StopThread will do it for us */
+    }
+    else
+    {
+        /* Run the interface in a separate thread */
+        if( vlc_thread_create( p_intf, "interface", p_intf->pf_run,
+                               VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
         {
-            input_DestroyThread( p_intf->p_input, NULL );            
-            p_intf->p_input = NULL;            
-            intf_DbgMsg("Input thread destroyed\n");            
+            msg_Err( p_intf, "cannot spawn interface thread" );
+            return VLC_EGENERIC;
         }
+    }
 
-        /* Sleep to avoid using all CPU - since some interfaces needs to access 
-         * keyboard events, a 100ms delay is a good compromise */
-        msleep( INTF_IDLE_SLEEP );
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * intf_StopThread: end the interface thread
+ *****************************************************************************
+ * This function asks the interface thread to stop.
+ *****************************************************************************/
+void intf_StopThread( intf_thread_t *p_intf )
+{
+    /* Tell the interface to die */
+    if( !p_intf->b_block )
+    {
+        p_intf->b_die = VLC_TRUE;
     }
+
+    /* Wait for the thread to exit */
+    vlc_thread_join( p_intf );
 }
 
-/*******************************************************************************
+/*****************************************************************************
  * intf_Destroy: clean interface after main loop
- *******************************************************************************
+ *****************************************************************************
  * This function destroys specific interfaces and close output devices.
- *******************************************************************************/
+ *****************************************************************************/
 void intf_Destroy( intf_thread_t *p_intf )
 {
-    /* Destroy interfaces */
-    intf_SysDestroy( p_intf );
-    intf_ConsoleDestroy( p_intf->p_console );
+    /* Unlock module */
+    module_Unneed( p_intf, p_intf->p_module );
+
+    vlc_mutex_destroy( &p_intf->change_lock );
 
     /* Free structure */
-    free( p_intf );
+    vlc_object_destroy( p_intf );
 }
 
-/*******************************************************************************
- * intf_SelectInput: change input stream
- *******************************************************************************
- * Kill existing input, if any, and try to open a new one, using an input
- * configuration table.
- *******************************************************************************/
-int intf_SelectInput( intf_thread_t * p_intf, int i_index )
-{
-    intf_DbgMsg("\n");
-
-    /* If VLANs are not active, return with an error */
-    if( !p_main->b_vlans )
-    {
-        intf_ErrMsg("error: VLANs are not activated\n");
-        return( 1 );        
-    }    
-    
-    /* Kill existing input, if any */
-    if( p_intf->p_input != NULL )
-    {        
-        input_DestroyThread( p_intf->p_input, NULL );
-    }
+/* Following functions are local */
 
-    /* Open a new input */
-    intf_Msg("Switching to channel %d\n", i_index );    
-    p_intf->p_input = input_CreateThread( INPUT_METHOD_TS_VLAN_BCAST, NULL, 0, i_index, 
-                                          p_intf->p_vout, p_main->p_aout, NULL );        
-    return( p_intf->p_input == NULL );    
-}
-
-/*******************************************************************************
- * intf_ProcessKey: process standard keys
- *******************************************************************************
- * This function will process standard keys and return non 0 if the key was
- * unknown.
- *******************************************************************************/
-int intf_ProcessKey( intf_thread_t *p_intf, int i_key )
+/*****************************************************************************
+ * Manager: helper thread for blocking interfaces
+ *****************************************************************************
+ * If the interface is launched in the main thread, it will not listen to
+ * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
+ * This thread takes care of the matter.
+ *****************************************************************************/
+static void Manager( intf_thread_t *p_intf )
 {
-    switch( i_key )
+    while( !p_intf->b_die )
     {
-    case 'Q':                                                    /* quit order */
-    case 'q':
-    case 27: /* escape key */
-        p_intf->b_die = 1;
-        break;  
-    case '0':                                                 /* source change */
-    case '1':
-    case '2':
-    case '3':
-    case '4':
-    case '5':
-    case '6':
-    case '7':
-    case '8':
-    case '9':                    
-        if( intf_SelectInput( p_intf, i_key - '0' ) )
-        {
-            intf_ErrMsg("error: can not open channel %d\n", i_key - '0');            
-        }        
-        break;
-    case '+':                                                      /* volume + */
-        // ??
-        break;
-    case '-':                                                      /* volume - */
-        // ??
-        break;
-    case 'M':                                                   /* toggle mute */
-    case 'm':                    
-        // ??
-        break;     
-    case 'g':                                                       /* gamma - */
-        if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma > -INTF_GAMMA_LIMIT) )
-        {
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );
-            p_intf->p_vout->f_gamma   -= INTF_GAMMA_STEP;                        
-            p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );
-        }                    
-        break;                                        
-    case 'G':                                                       /* gamma + */
-        if( (p_intf->p_vout != NULL) && (p_intf->p_vout->f_gamma < INTF_GAMMA_LIMIT) )
-        {       
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );
-            p_intf->p_vout->f_gamma   += INTF_GAMMA_STEP;
-            p_intf->p_vout->i_changes |= VOUT_GAMMA_CHANGE;
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );
-        }                    
-        break;  
-    case 'c':                                              /* toggle grayscale */
-        if( p_intf->p_vout != NULL )
-        {
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );                        
-            p_intf->p_vout->b_grayscale = !p_intf->p_vout->b_grayscale;                    
-            p_intf->p_vout->i_changes  |= VOUT_GRAYSCALE_CHANGE;                        
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );      
-        }
-        break;  
-    case ' ':                                              /* toggle interface */
-        if( p_intf->p_vout != NULL )
-        {
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );                        
-            p_intf->p_vout->b_interface     = !p_intf->p_vout->b_interface;                    
-            p_intf->p_vout->i_changes |= VOUT_INTF_CHANGE;                        
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );      
-        }
-        break;                                
-    case 'i':                                                   /* toggle info */
-        if( p_intf->p_vout != NULL )
-        {
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );                        
-            p_intf->p_vout->b_info     = !p_intf->p_vout->b_info;                    
-            p_intf->p_vout->i_changes |= VOUT_INFO_CHANGE;                        
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );      
-        }
-        break;                                
-    case 's':                                                 /* toggle scaling */
-        if( p_intf->p_vout != NULL )
+        msleep( INTF_IDLE_SLEEP );
+
+        if( p_intf->p_vlc->b_die )
         {
-            vlc_mutex_lock( &p_intf->p_vout->change_lock );                        
-            p_intf->p_vout->b_scale     = !p_intf->p_vout->b_scale;                    
-            p_intf->p_vout->i_changes |= VOUT_SCALE_CHANGE;                        
-            vlc_mutex_unlock( &p_intf->p_vout->change_lock );      
+            p_intf->b_die = VLC_TRUE;
+            return;
         }
-        break;                                
-   default:                                                    /* unknown key */
-        return( 1 );        
     }
-
-    return( 0 );    
 }
 
-    
-