]> git.sesse.net Git - vlc/blobdiff - src/libvlc-common.c
new (failing) test for libvlc
[vlc] / src / libvlc-common.c
index bfe525ab8f002d195d6007a6b0e2011ac7984716..7a29a70c4e899db3366d4305b9176534a5527fa6 100644 (file)
@@ -42,6 +42,7 @@
 
 #include "modules/modules.h"
 #include "config/configuration.h"
+#include "interface/interface.h"
 
 #include <errno.h>                                                 /* ENOMEM */
 #include <stdio.h>                                              /* sprintf() */
@@ -75,8 +76,6 @@
 #   include <hal/libhal.h>
 #endif
 
-#include "vlc_os_specific.h"
-
 #include <vlc_playlist.h>
 #include <vlc_interface.h>
 
@@ -94,6 +93,8 @@
 
 #include "playlist/playlist_internal.h"
 
+#include <vlc_vlm.h>
+
 /*****************************************************************************
  * The evil global variable. We handle it with care, don't worry.
  *****************************************************************************/
@@ -105,7 +106,8 @@ static volatile unsigned int i_instances = 0;
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-#if defined (__APPLE__) || defined (WIN32)
+#if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
+    ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 static void SetLanguage   ( char const * );
 #endif
 static inline int LoadMessages (void);
@@ -128,7 +130,7 @@ static void InitDeviceValues( libvlc_int_t * );
 
 libvlc_global_data_t *vlc_global( void )
 {
-    return &libvlc_global;
+    return p_libvlc_global;
 }
 
 /*****************************************************************************
@@ -151,39 +153,29 @@ libvlc_int_t * libvlc_InternalCreate( void )
 {
     int i_ret;
     libvlc_int_t * p_libvlc = NULL;
-    vlc_value_t lockval;
     char *psz_env = NULL;
 
-#if 0
-    /* &libvlc_global never changes,
-     * so we can safely call this multiple times. */
-    p_libvlc_global = &libvlc_global;
-#endif
-
     /* vlc_threads_init *must* be the first internal call! No other call is
      * allowed before the thread system has been initialized. */
     i_ret = vlc_threads_init( p_libvlc_global );
     if( i_ret < 0 ) return NULL;
 
     /* Now that the thread system is initialized, we don't have much, but
-     * at least we have var_Create */
-    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
-    var_Get( p_libvlc_global, "libvlc", &lockval );
-    vlc_mutex_lock( lockval.p_address );
+     * at least we have variables */
+    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
 
     i_instances++;
 
-    if( !libvlc_global.b_ready )
+    if( !p_libvlc_global->b_ready )
     {
         /* Guess what CPU we have */
         cpu_flags = CPUCapabilities();
        /* The module bank will be initialized later */
-        libvlc_global.p_module_bank = NULL;
+        p_libvlc_global->p_module_bank = NULL;
 
-        libvlc_global.b_ready = VLC_TRUE;
+        p_libvlc_global->b_ready = VLC_TRUE;
     }
-    vlc_mutex_unlock( lockval.p_address );
-    var_Destroy( p_libvlc_global, "libvlc" );
+    vlc_mutex_unlock( lock );
 
     /* Allocate a libvlc instance object */
     p_libvlc = vlc_object_create( p_libvlc_global, VLC_OBJECT_LIBVLC );
@@ -193,6 +185,8 @@ libvlc_int_t * libvlc_InternalCreate( void )
         return NULL;
     }
     p_libvlc->p_playlist = NULL;
+    p_libvlc->p_interaction = NULL;
+    p_libvlc->p_vlm = NULL;
     p_libvlc->psz_object_name = "libvlc";
 
     /* Initialize message queue */
@@ -215,6 +209,7 @@ libvlc_int_t * libvlc_InternalCreate( void )
     msg_Dbg( p_libvlc, "libvlc was configured with %s", CONFIGURE_LINE );
 
     /* Initialize mutexes */
+    vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
     vlc_mutex_init( p_libvlc, &p_libvlc->config_lock );
 #ifdef __APPLE__
     vlc_mutex_init( p_libvlc, &p_libvlc->quicktime_lock );
@@ -316,7 +311,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     /* Check for plugins cache options */
     if( config_GetInt( p_libvlc, "reset-plugins-cache" ) > 0 )
     {
-        libvlc_global.p_module_bank->b_cache_delete = VLC_TRUE;
+        p_libvlc_global->p_module_bank->b_cache_delete = VLC_TRUE;
     }
 
     /* Will be re-done properly later on */
@@ -326,7 +321,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
 #ifndef WIN32
     if( config_GetInt( p_libvlc, "daemon" ) )
     {
-#if HAVE_DAEMON
+#ifdef HAVE_DAEMON
         char *psz_pidfile = NULL;
 
         if( daemon( 1, 0) != 0 )
@@ -334,7 +329,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             msg_Err( p_libvlc, "Unable to fork vlc to daemon mode" );
             b_exit = VLC_TRUE;
         }
-        libvlc_global.b_daemon = VLC_TRUE;
+        p_libvlc_global->b_daemon = VLC_TRUE;
 
         /* lets check if we need to write the pidfile */
         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
@@ -381,7 +376,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             close( STDOUT_FILENO );
             close( STDERR_FILENO );
 
-            libvlc_global.b_daemon = VLC_TRUE;
+            p_libvlc_global->b_daemon = VLC_TRUE;
         }
 #endif
     }
@@ -406,7 +401,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     psz_language = config_GetPsz( p_libvlc, "language" );
     if( psz_language && *psz_language && strcmp( psz_language, "auto" ) )
     {
-        vlc_bool_t b_cache_delete = libvlc_global.p_module_bank->b_cache_delete;
+        vlc_bool_t b_cache_delete = p_libvlc_global->p_module_bank->b_cache_delete;
 
         /* Reset the default domain */
         SetLanguage( psz_language );
@@ -418,9 +413,9 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         module_InitBank( p_libvlc );
         config_LoadConfigFile( p_libvlc, "main" );
         config_LoadCmdLine( p_libvlc, &i_argc, ppsz_argv, VLC_TRUE );
-        libvlc_global.p_module_bank->b_cache_delete = b_cache_delete;
+        p_libvlc_global->p_module_bank->b_cache_delete = b_cache_delete;
     }
-    if( psz_language ) free( psz_language );
+    free( psz_language );
 # endif
 #endif
 
@@ -438,7 +433,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     }
 
     msg_Dbg( p_libvlc, "module bank initialized, found %i modules",
-                    libvlc_global.p_module_bank->i_children );
+             p_libvlc_global->p_module_bank->i_children );
 
     /* Check for help on modules */
     if( (p_tmp = config_GetPsz( p_libvlc, "module" )) )
@@ -723,15 +718,34 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
     p_libvlc->b_stats = config_GetInt( p_libvlc, "stats" ) > 0;
     p_libvlc->i_timers = 0;
     p_libvlc->pp_timers = NULL;
-    vlc_mutex_init( p_libvlc, &p_libvlc->timer_lock );
+
+    /* Init stats */
+    p_libvlc->p_stats = (global_stats_t *)malloc( sizeof( global_stats_t ) );
+    if( !p_libvlc->p_stats )
+    {
+        vlc_object_release( p_libvlc );
+        return VLC_ENOMEM;
+    }
+    vlc_mutex_init( p_libvlc, &p_libvlc->p_stats->lock );
+    p_libvlc->p_stats_computer = NULL;
+
+    /* Init the array that holds every input item */
+    ARRAY_INIT( p_libvlc->input_items );
+    p_libvlc->i_last_input_id = 0;
 
     /*
      * Initialize hotkey handling
      */
     var_Create( p_libvlc, "key-pressed", VLC_VAR_INTEGER );
+    var_Create( p_libvlc, "key-action", VLC_VAR_INTEGER );
     p_libvlc->p_hotkeys = malloc( libvlc_hotkeys_size );
     /* Do a copy (we don't need to modify the strings) */
     memcpy( p_libvlc->p_hotkeys, libvlc_hotkeys, libvlc_hotkeys_size );
+    var_AddCallback( p_libvlc, "key-pressed", vlc_key_to_action,
+                     p_libvlc->p_hotkeys );
+
+    /* Initialize interaction */
+    p_libvlc->p_interaction = interaction_Init( p_libvlc );
 
     /* Initialize playlist and get commandline files */
     playlist_ThreadCreate( p_libvlc );
@@ -753,7 +767,17 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         /* Add service discovery modules */
         playlist_ServicesDiscoveryAdd( p_playlist, psz_modules );
     }
-    if( psz_modules ) free( psz_modules );
+    free( psz_modules );
+
+    /* Initialize VLM if vlm-conf is specified */
+    psz_parser = config_GetPsz( p_libvlc, "vlm-conf" );
+    if( psz_parser && *psz_parser )
+    {
+        p_libvlc->p_vlm = vlm_New( p_libvlc );
+        if( !p_libvlc->p_vlm )
+            msg_Err( p_libvlc, "VLM initialization failed" );
+    }
+    free( psz_parser );
 
     /*
      * Load background interfaces
@@ -857,12 +881,12 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
             if( psz_morecodecs )
             {
                 config_PutPsz( p_libvlc, "codec", psz_morecodecs);
-                free(psz_morecodecs);
+                free( psz_morecodecs );
             }
         }
         else
             config_PutPsz( p_libvlc, "codec", "dmo,quicktime");
-        free(psz_codecs);
+        free( psz_codecs );
     }
 #endif
 
@@ -897,7 +921,7 @@ int libvlc_InternalInit( libvlc_int_t *p_libvlc, int i_argc,
         VLC_AddTarget( p_libvlc->i_object_id, val.psz_string, NULL, 0,
                        PLAYLIST_INSERT, 0 );
     }
-    if ( val.psz_string != NULL ) free( val.psz_string );
+    free( val.psz_string );
 
     return VLC_SUCCESS;
 }
@@ -946,6 +970,16 @@ int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
         aout_Delete( p_aout );
     }
 
+    /* Destroy VLM if created in libvlc_InternalInit */
+    if( p_libvlc->p_vlm )
+    {
+        vlm_Delete( p_libvlc->p_vlm );
+    }
+
+    /* Free interaction */
+    msg_Dbg( p_libvlc, "removing interaction" );
+    vlc_object_release( p_libvlc->p_interaction );
+
     stats_TimersDumpAll( p_libvlc );
     stats_TimersClean( p_libvlc );
 
@@ -958,6 +992,19 @@ int libvlc_InternalCleanup( libvlc_int_t *p_libvlc )
         vlc_object_release( p_announce );
         announce_HandlerDestroy( p_announce );
     }
+
+    vlc_bool_t b_clean = VLC_TRUE;
+    FOREACH_ARRAY( input_item_t *p_del, p_libvlc->input_items )
+        msg_Err( p_libvlc, "input item %p has not been deleted properly: refcount %d", p_del, p_del->i_gc_refcount );
+        b_clean = VLC_FALSE;
+    FOREACH_END();
+    assert( b_clean );
+    ARRAY_RESET( p_libvlc->input_items );
+
+    msg_Dbg( p_libvlc, "removing stats" );
+    vlc_mutex_destroy( &p_libvlc->p_stats->lock );
+    FREENULL( p_libvlc->p_stats );
+
     return VLC_SUCCESS;
 }
 
@@ -979,7 +1026,6 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
 #ifndef WIN32
     char* psz_pidfile = NULL;
 
-    if( libvlc_global.p_module_bank )
     if( config_GetInt( p_libvlc, "daemon" ) > 0 )
     {
         psz_pidfile = config_GetPsz( p_libvlc, "pidfile" );
@@ -992,7 +1038,7 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
                         psz_pidfile );
             }
         }
-        free ( psz_pidfile );
+        free( psz_pidfile );
     }
 #endif
 
@@ -1010,11 +1056,11 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
     FREENULL( p_libvlc->psz_datadir );
     FREENULL( p_libvlc->psz_cachedir );
     FREENULL( p_libvlc->psz_configfile );
+    var_DelCallback( p_libvlc, "key-pressed", vlc_key_to_action,
+                     p_libvlc->p_hotkeys );
     FREENULL( p_libvlc->p_hotkeys );
 
-    var_Create( p_libvlc_global, "libvlc", VLC_VAR_MUTEX );
-    var_Get( p_libvlc_global, "libvlc", &lockval );
-    vlc_mutex_lock( lockval.p_address );
+    vlc_mutex_t *lock = var_AcquireMutex( "libvlc" );
     i_instances--;
 
     if( i_instances == 0 )
@@ -1022,8 +1068,7 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
         /* System specific cleaning code */
         system_End( p_libvlc );
     }
-    vlc_mutex_unlock( lockval.p_address );
-    var_Destroy( p_libvlc_global, "libvlc" );
+    vlc_mutex_unlock( lock );
 
     msg_Flush( p_libvlc );
     msg_Destroy( p_libvlc );
@@ -1033,7 +1078,7 @@ int libvlc_InternalDestroy( libvlc_int_t *p_libvlc, vlc_bool_t b_release )
     vlc_mutex_destroy( &p_libvlc->timer_lock );
 
     if( b_release ) vlc_object_release( p_libvlc );
-    vlc_object_destroy( p_libvlc );
+    vlc_object_release( p_libvlc );
     p_libvlc = NULL;
 
     /* Stop thread system: last one out please shut the door!
@@ -1067,13 +1112,13 @@ int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
     }
 
 #ifndef WIN32
-    if( libvlc_global.b_daemon && b_block && !psz_module )
+    if( p_libvlc_global->b_daemon && b_block && !psz_module )
     {
         /* Daemon mode hack.
          * We prefer the dummy interface if none is specified. */
         char *psz_interface = config_GetPsz( p_libvlc, "intf" );
         if( !psz_interface || !*psz_interface ) psz_module = "dummy";
-        if( psz_interface ) free( psz_interface );
+        free( psz_interface );
     }
 #endif
 
@@ -1117,7 +1162,8 @@ int libvlc_InternalAddIntf( libvlc_int_t *p_libvlc,
     return VLC_SUCCESS;
 };
 
-#if defined (__APPLE__) || defined (WIN32)
+#if defined( ENABLE_NLS ) && (defined (__APPLE__) || defined (WIN32)) && \
+    ( defined( HAVE_GETTEXT ) || defined( HAVE_INCLUDED_GETTEXT ) )
 /*****************************************************************************
  * SetLanguage: set the interface language.
  *****************************************************************************
@@ -1158,7 +1204,7 @@ static inline int LoadMessages (void)
 #else
     char psz_path[1024];
     if (snprintf (psz_path, sizeof (psz_path), "%s/%s",
-                  libvlc_global.psz_vlcpath, "locale")
+                  p_libvlc_global->psz_vlcpath, "locale")
                      >= (int)sizeof (psz_path))
         return -1;
 
@@ -1317,6 +1363,9 @@ static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
     psz_spaces_text[PADDING_SPACES+LINE_START] = '\0';
     memset( psz_spaces_longtext, ' ', LINE_START+2 );
     psz_spaces_longtext[LINE_START+2] = '\0';
+#ifdef WIN32
+    b_color = VLC_FALSE; // don't put color control codes in a .txt file
+#endif
 
     if( b_color )
     {
@@ -1691,9 +1740,15 @@ static void Usage( libvlc_int_t *p_this, char const *psz_module_name )
         }
     }
 
-    if( b_has_advanced )
-        utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
-        _( "add --advanced to your command line to see advanced options." ) );
+    if( b_has_advanced ) 
+    {
+        if( b_color )
+            utf8_fprintf( stdout, "\n" WHITE "%s" GRAY " %s\n", _( "Note:" ),
+           _( "add --advanced to your command line to see advanced options."));
+        else
+            utf8_fprintf( stdout, "\n %s %s\n", _( "Note:" ),
+           _( "add --advanced to your command line to see advanced options."));
+    }
 
     /* Release the module list */
     vlc_list_release( p_list );
@@ -1801,7 +1856,7 @@ static void Version( void )
              VLC_CompileBy(), VLC_CompileHost(), VLC_CompileDomain() );
     utf8_fprintf( stdout, _("Compiler: %s\n"), VLC_Compiler() );
     if( strcmp( VLC_Changeset(), "exported" ) )
-        utf8_fprintf( stdout, _("Based upon svn changeset [%s]\n"),
+        utf8_fprintf( stdout, _("Based upon Git commit [%s]\n"),
                  VLC_Changeset() );
     utf8_fprintf( stdout, LICENSE_MSG );
 
@@ -1824,6 +1879,11 @@ static void ShowConsole( vlc_bool_t b_dofile )
     if( getenv( "PWD" ) && getenv( "PS1" ) ) return; /* cygwin shell */
 
     AllocConsole();
+    /* Use the ANSI code page (e.g. Windows-1252) as expected by the LibVLC
+     * Unicode/locale subsystem. By default, we have the obsolecent OEM code
+     * page (e.g. CP437 or CP850). */
+    SetConsoleOutputCP (GetACP ());
+    SetConsoleTitle ("VLC media player version "PACKAGE_VERSION);
 
     freopen( "CONOUT$", "w", stderr );
     freopen( "CONIN$", "r", stdin );
@@ -1868,34 +1928,21 @@ static void PauseConsole( void )
  *****************************************************************************/
 static int ConsoleWidth( void )
 {
-    int i_width = 80;
+    unsigned i_width = 80;
 
 #ifndef WIN32
-    char buf[20];
-    char *psz_parser = NULL;
-    FILE *file = NULL;
-    int i_ret;
-
-    file = popen( "stty size 2>/dev/null", "r" );
-    if( file )
+    FILE *file = popen( "stty size 2>/dev/null", "r" );
+    if (file != NULL)
     {
-        i_ret = fread( buf, 1, 20, file );
-        if( i_ret > 0 )
-        {
-            buf[19] = '\0';
-            psz_parser = strchr( buf, ' ' );
-            if( psz_parser )
-            {
-                i_ret = atoi( psz_parser + 1 );
-                if( i_ret >= 80 )
-                {
-                    i_width = i_ret;
-                }
-            }
-        }
-
+        if (fscanf (file, "%*u %u", &i_width) <= 0)
+            i_width = 80;
         pclose( file );
     }
+#else
+    CONSOLE_SCREEN_BUFFER_INFO buf;
+
+    if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &buf))
+        i_width = buf.dwSize.X;
 #endif
 
     return i_width;