]> git.sesse.net Git - vlc/blobdiff - src/osd/osd.c
Clean up a bit subpicture region API.
[vlc] / src / osd / osd.c
index ac289b8e26980903b2d6ceb1e7902603f96017de..7e1f78e1404737adf3b1924e89a348be0a332f83 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * osd.c - The OSD Menu core code.
  *****************************************************************************
- * Copyright (C) 2005-2007 M2X
+ * Copyright (C) 2005-2008 M2X
  * $Id$
  *
  * Authors: Jean-Paul Saman <jpsaman #_at_# m2x dot nl>
  * Preamble
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
 #include <vlc_keys.h>
 #include <vlc_osd.h>
 #include <vlc_image.h>
@@ -34,8 +38,6 @@
 
 #undef OSD_MENU_DEBUG
 
-static const char *ppsz_button_states[] = { "unselect", "select", "pressed" };
-
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -43,11 +45,11 @@ static const char *ppsz_button_states[] = { "unselect", "select", "pressed" };
 static void osd_UpdateState( osd_menu_state_t *, int, int, int, int, picture_t * );
 static inline osd_state_t *osd_VolumeStateChange( osd_state_t *, int );
 static int osd_VolumeStep( vlc_object_t *, int, int );
-static vlc_bool_t osd_isVisible( osd_menu_t *p_osd );
-static osd_menu_t *osd_ParserLoad( vlc_object_t *, const char * );
-static void osd_ParserUnload( vlc_object_t *, osd_menu_t * );
+static bool osd_isVisible( osd_menu_t *p_osd );
+static bool osd_ParserLoad( osd_menu_t *, const char * );
+static void osd_ParserUnload( osd_menu_t * );
 
-static vlc_bool_t osd_isVisible( osd_menu_t *p_osd )
+static bool osd_isVisible( osd_menu_t *p_osd )
 {
     vlc_value_t val;
 
@@ -58,27 +60,15 @@ static vlc_bool_t osd_isVisible( osd_menu_t *p_osd )
 /*****************************************************************************
  * Wrappers for loading and unloading osd parser modules.
  *****************************************************************************/
-static osd_menu_t *osd_ParserLoad( vlc_object_t *p_this, const char *psz_file )
+static bool osd_ParserLoad( osd_menu_t *p_menu, const char *psz_file )
 {
-    osd_menu_t *p_menu;
-
-    p_menu = vlc_object_create( p_this, VLC_OBJECT_OSDMENU );
-    if( !p_menu )
-    {
-        msg_Err( p_this, "out of memory" );
-        return NULL;
-    }
-    vlc_object_yield( p_menu );
-    vlc_object_attach( p_menu, p_this->p_libvlc );
-
     /* Stuff needed for Parser */
     p_menu->psz_file = strdup( psz_file );
-    p_menu->p_image = image_HandlerCreate( p_this );
+    p_menu->p_image = image_HandlerCreate( p_menu );
     if( !p_menu->p_image || !p_menu->psz_file )
     {
-        msg_Err( p_this, "unable to load images, aborting .." );
-        osd_ParserUnload( p_this, p_menu );
-        return NULL;
+        msg_Err( p_menu, "unable to load images, aborting .." );
+        return true;
     }
     else
     {
@@ -86,39 +76,35 @@ static osd_menu_t *osd_ParserLoad( vlc_object_t *p_this, const char *psz_file )
         char *psz_ext = strrchr( p_menu->psz_file, '.' );
 
         if( psz_ext && !strcmp( psz_ext, ".cfg") )
-            psz_type = "import-osd";
+            psz_type = (char*)"import-osd";
         else
-            psz_type = "import-osd-xml";
+            psz_type = (char*)"import-osd-xml";
 
         p_menu->p_parser = module_Need( p_menu, "osd parser",
-                                        psz_type, VLC_TRUE );
+                                        psz_type, true );
         if( !p_menu->p_parser )
         {
-            osd_ParserUnload( p_this, p_menu );
-            return NULL;
+            return false;
         }
     }
-    return p_menu;
+    return true;
 }
 
-static void osd_ParserUnload( vlc_object_t *p_this, osd_menu_t *p_menu )
+static void osd_ParserUnload( osd_menu_t *p_menu )
 {
     if( p_menu->p_image )
         image_HandlerDelete( p_menu->p_image );
-    if( p_menu->psz_file )
-        free( p_menu->psz_file );
 
     if( p_menu->p_parser )
         module_Unneed( p_menu, p_menu->p_parser );
 
-    vlc_object_detach( p_menu );
-    vlc_object_destroy( p_menu );
+    free( p_menu->psz_file );
 }
 
 /**
  * Change state on an osd_button_t.
  *
- * This function selects the specified state and returns a pointer to it. The
+ * This function selects the specified state and returns a pointer vlc_custom_createto it. The
  * following states are currently supported:
  * \see OSD_BUTTON_UNSELECT
  * \see OSD_BUTTON_SELECT
@@ -164,11 +150,21 @@ osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
     p_osd = vlc_object_find( p_this, VLC_OBJECT_OSDMENU, FIND_ANYWHERE );
     if( p_osd == NULL )
     {
+        static const char osdmenu_name[] = "osd menu";
         vlc_value_t val;
 
-        /* Parse configuration file */
-        p_osd = osd_ParserLoad( p_this, psz_file );
+        p_osd = vlc_custom_create( p_this, sizeof( *p_osd ), VLC_OBJECT_OSDMENU,
+                                    osdmenu_name );
         if( !p_osd )
+            return NULL;
+
+        p_osd->p_parser = NULL;
+        vlc_object_attach( p_osd, p_this->p_libvlc );
+
+        /* Parse configuration file */
+        if ( !osd_ParserLoad( p_osd, psz_file ) )
+            goto error;
+        if( !p_osd->p_state )
             goto error;
 
         /* Setup default button (first button) */
@@ -194,7 +190,7 @@ osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
         var_Create( p_osd, "osd-menu-update", VLC_VAR_BOOL );
         var_Create( p_osd, "osd-menu-visible", VLC_VAR_BOOL );
 
-        val.b_bool = VLC_FALSE;
+        val.b_bool = false;
         var_Set( p_osd, "osd-menu-update", val );
         var_Set( p_osd, "osd-menu-visible", val );
     }
@@ -202,16 +198,8 @@ osd_menu_t *__osd_MenuCreate( vlc_object_t *p_this, const char *psz_file )
     return p_osd;
 
 error:
-    msg_Err( p_this, "creating OSD menu object failed" );
-
-    if( p_osd->p_image )
-        image_HandlerDelete( p_osd->p_image );
-    if( p_osd->psz_file )
-        free( p_osd->psz_file );
-
-    vlc_object_detach( p_osd );
-    vlc_object_destroy( p_osd );
     vlc_mutex_unlock( lockval.p_address );
+    __osd_MenuDelete( p_this, p_osd );
     return NULL;
 }
 
@@ -224,17 +212,19 @@ void __osd_MenuDelete( vlc_object_t *p_this, osd_menu_t *p_osd )
     var_Get( p_this->p_libvlc, "osd_mutex", &lockval );
     vlc_mutex_lock( lockval.p_address );
 
+    if( vlc_internals( VLC_OBJECT(p_osd) )->i_refcount == 1 )
+    {
+        var_Destroy( p_osd, "osd-menu-visible" );
+        var_Destroy( p_osd, "osd-menu-update" );
+        osd_ParserUnload( p_osd );
+    }
+
     vlc_object_release( p_osd );
-    if( p_osd->p_internals->i_refcount > 0 )
+    if( vlc_internals( VLC_OBJECT(p_osd) )->i_refcount > 0 )
     {
         vlc_mutex_unlock( lockval.p_address );
         return;
     }
-
-    var_Destroy( p_osd, "osd-menu-visible" );
-    var_Destroy( p_osd, "osd-menu-update" );
-
-    osd_ParserUnload( p_this, p_osd );
     p_osd = NULL;
     vlc_mutex_unlock( lockval.p_address );
 }
@@ -305,9 +295,9 @@ void __osd_MenuShow( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
     }
-    osd_SetMenuVisible( p_osd, VLC_TRUE );
+    osd_SetMenuVisible( p_osd, true );
 
     vlc_object_release( (vlc_object_t*) p_osd );
     vlc_mutex_unlock( lockval.p_address );
@@ -334,7 +324,7 @@ void __osd_MenuHide( vlc_object_t *p_this )
     osd_UpdateState( p_osd->p_state,
                 p_osd->p_state->i_x, p_osd->p_state->i_y,
                 0, 0, NULL );
-    osd_SetMenuUpdate( p_osd, VLC_TRUE );
+    osd_SetMenuUpdate( p_osd, true );
 
     vlc_object_release( (vlc_object_t*) p_osd );
     vlc_mutex_unlock( lockval.p_address );
@@ -353,7 +343,7 @@ void __osd_MenuActivate( vlc_object_t *p_this )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -392,8 +382,8 @@ void __osd_MenuActivate( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_button->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
-        osd_SetMenuVisible( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
+        osd_SetMenuVisible( p_osd, true );
         osd_SetKeyPressed( VLC_OBJECT(p_osd->p_libvlc), config_GetInt( p_osd, p_button->psz_action ) );
 #if defined(OSD_MENU_DEBUG)
         msg_Dbg( p_osd, "select (%d, %s)", config_GetInt( p_osd, p_button->psz_action ), p_button->psz_action );
@@ -416,7 +406,7 @@ void __osd_MenuNext( vlc_object_t *p_this )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -444,7 +434,7 @@ void __osd_MenuNext( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
     }
 #if defined(OSD_MENU_DEBUG)
     msg_Dbg( p_osd, "direction right [button %s]", p_osd->p_state->p_visible->psz_action );
@@ -467,7 +457,7 @@ void __osd_MenuPrev( vlc_object_t *p_this )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -495,7 +485,7 @@ void __osd_MenuPrev( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
     }
 #if defined(OSD_MENU_DEBUG)
     msg_Dbg( p_osd, "direction left [button %s]", p_osd->p_state->p_visible->psz_action );
@@ -520,7 +510,7 @@ void __osd_MenuUp( vlc_object_t *p_this )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -556,7 +546,7 @@ void __osd_MenuUp( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
         /* If this is a range style action with associated images of only one state,
             * then perform "menu select" on every menu navigation
             */
@@ -592,7 +582,7 @@ void __osd_MenuDown( vlc_object_t *p_this )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -628,7 +618,7 @@ void __osd_MenuDown( vlc_object_t *p_this )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
         /* If this is a range style action with associated images of only one state,
          * then perform "menu select" on every menu navigation
          */
@@ -698,12 +688,12 @@ void __osd_Volume( vlc_object_t *p_this )
                     p_button->p_current_state->i_width,
                     p_button->p_current_state->i_height,
                     p_button->p_current_state->p_pic );
-            osd_SetMenuUpdate( p_osd, VLC_TRUE );
-            osd_SetMenuVisible( p_osd, VLC_TRUE );
+            osd_SetMenuUpdate( p_osd, true );
+            osd_SetMenuVisible( p_osd, true );
         }
-        vlc_object_release( (vlc_object_t*) p_osd );
         vlc_mutex_unlock( lockval.p_address );
     }
+    vlc_object_release( p_osd );
 }
 
 osd_button_t *__osd_ButtonFind( vlc_object_t *p_this, int i_x, int i_y,
@@ -721,9 +711,9 @@ osd_button_t *__osd_ButtonFind( vlc_object_t *p_this, int i_x, int i_y,
         return NULL;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
-        vlc_object_release( (vlc_object_t*) p_osd );
+        vlc_object_release( p_osd );
         return NULL;
     }
 
@@ -776,13 +766,13 @@ osd_button_t *__osd_ButtonFind( vlc_object_t *p_this, int i_x, int i_y,
         if( ( i_x >= i_x_offset ) && ( i_x <= i_x_offset + i_width ) &&
             ( i_y >= i_y_offset ) && ( i_y <= i_y_offset + i_height ) )
         {
-            vlc_object_release( (vlc_object_t*) p_osd );
+            vlc_object_release( p_osd );
             vlc_mutex_unlock( lockval.p_address );
             return p_button;
         }
     }
 
-    vlc_object_release( (vlc_object_t*) p_osd );
+    vlc_object_release( p_osd );
     vlc_mutex_unlock( lockval.p_address );
     return NULL;
 }
@@ -803,7 +793,7 @@ void __osd_ButtonSelect( vlc_object_t *p_this, osd_button_t *p_button )
         return;
     }
 
-    if( osd_isVisible( p_osd ) == VLC_FALSE )
+    if( osd_isVisible( p_osd ) == false )
     {
         vlc_object_release( (vlc_object_t*) p_osd );
         return;
@@ -828,12 +818,12 @@ void __osd_ButtonSelect( vlc_object_t *p_this, osd_button_t *p_button )
                 p_osd->p_state->p_visible->p_current_state->i_width,
                 p_osd->p_state->p_visible->p_current_state->i_height,
                 p_osd->p_state->p_visible->p_current_state->p_pic );
-        osd_SetMenuUpdate( p_osd, VLC_TRUE );
+        osd_SetMenuUpdate( p_osd, true );
     }
 #if defined(OSD_MENU_DEBUG)
     msg_Dbg( p_osd, "button selected is [button %s]", p_osd->p_state->p_visible->psz_action );
 #endif
 
-    vlc_object_release( (vlc_object_t*) p_osd );
+    vlc_object_release( p_osd );
     vlc_mutex_unlock( lockval.p_address );
 }