* Core: add var_Command function. This is used to execute a command registered on...
authorAntoine Cellerier <dionoea@videolan.org>
Wed, 4 Apr 2007 21:58:51 +0000 (21:58 +0000)
committerAntoine Cellerier <dionoea@videolan.org>
Wed, 4 Apr 2007 21:58:51 +0000 (21:58 +0000)
* rc.c: use var_Command (instead of own code)
* telnet.c: add support for object commands (like in the rc interface)

include/vlc_variables.h
modules/control/rc.c
modules/control/telnet.c
src/misc/variables.c

index 78cf0e6..991781e 100644 (file)
@@ -123,6 +123,9 @@ VLC_EXPORT( int, __var_Get, ( vlc_object_t *, const char *, vlc_value_t * ) );
 #define var_OptionParse(a,b) __var_OptionParse( VLC_OBJECT( a ) , b )
 VLC_EXPORT( void, __var_OptionParse, ( vlc_object_t *, const char * ) );
 
 #define var_OptionParse(a,b) __var_OptionParse( VLC_OBJECT( a ) , b )
 VLC_EXPORT( void, __var_OptionParse, ( vlc_object_t *, const char * ) );
 
+#define var_Command(a,b,c,d,e) __var_Command( VLC_OBJECT( a ), b, c, d, e )
+VLC_EXPORT( int, __var_Command, ( vlc_object_t *, const char *, const char *, const char *, char ** ) );
+
 /**
  * __var_Create() with automatic casting.
  */
 /**
  * __var_Create() with automatic casting.
  */
index 97acec3..2dec506 100644 (file)
@@ -601,56 +601,20 @@ static void Run( intf_thread_t *p_intf )
             char *psz_alias = psz_cmd + 1;
             char *psz_mycmd = strdup( psz_arg );
             char *psz_myarg = strchr( psz_mycmd, ' ' );
             char *psz_alias = psz_cmd + 1;
             char *psz_mycmd = strdup( psz_arg );
             char *psz_myarg = strchr( psz_mycmd, ' ' );
-            int i_ret = VLC_EGENERIC;
+            char *psz_msg;
 
             *psz_myarg = '\0';
             psz_myarg ++;
 
             *psz_myarg = '\0';
             psz_myarg ++;
-            vlc_object_t *p_obj =
-                vlc_object_find_name( p_input->p_libvlc, psz_alias,
-                                      FIND_CHILD );
 
 
-            if( !p_obj )
-                msg_rc( "Unknown destination object!" );
-            else
+            var_Command( p_input, psz_alias, psz_mycmd, psz_myarg, &psz_msg );
+
+
+            if( psz_msg )
             {
             {
-                int i_type;
-                if( (i_type = var_Type( p_obj, psz_mycmd )) & VLC_VAR_ISCOMMAND )
-                {
-                    i_type &= 0xf0;
-                    if( i_type == VLC_VAR_INTEGER )
-                    {
-                        i_ret = var_SetInteger( p_obj, psz_mycmd,
-                                                atoi( psz_myarg ) );
-                    }
-                    else if( i_type == VLC_VAR_FLOAT )
-                    {
-                        i_ret = var_SetFloat( p_obj, psz_mycmd,
-                                              atof( psz_myarg ) );
-                    }
-                    else if( i_type == VLC_VAR_STRING )
-                    {
-                        i_ret = var_SetString( p_obj, psz_mycmd,
-                                               psz_myarg );
-                    }
-                    else if( i_type == VLC_VAR_BOOL )
-                    {
-                        i_ret = var_SetBool( p_obj, psz_mycmd,
-                                             atoi( psz_myarg ) );
-                    }
-                    else
-                    {
-                        msg_rc( "Unhandled command type. Fix the code!" );
-                    }
-                }
-                else
-                {
-                    msg_rc( "Unknown command! %d", i_type );
-                }
-                vlc_object_release( p_obj );
+                msg_rc( psz_msg );
+                free( psz_msg );
             }
             free( psz_mycmd );
             }
             free( psz_mycmd );
-            msg_rc( "%s on object %s: returned %i (%s)",
-                    psz_mycmd, psz_alias, i_ret, vlc_error( i_ret ) );
         }
         /* If the user typed a registered local command, try it */
         else if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
         }
         /* If the user typed a registered local command, try it */
         else if( var_Type( p_intf, psz_cmd ) & VLC_VAR_ISCOMMAND )
index e390a13..6266d70 100644 (file)
@@ -434,6 +434,38 @@ static void Run( intf_thread_t *p_intf )
                     msg_Err( p_intf, "shutdown requested" );
                     p_intf->p_libvlc->b_die = VLC_TRUE;
                 }
                     msg_Err( p_intf, "shutdown requested" );
                     p_intf->p_libvlc->b_die = VLC_TRUE;
                 }
+                else if( *cl->buffer_read == '@'
+                          && strchr( cl->buffer_read, ' ' ) )
+                {
+                    /* Module specific commands (use same syntax as in the
+                     * rc interface) */
+                    char *psz_name = cl->buffer_read + 1;
+                    char *psz_cmd, *psz_arg, *psz_msg;
+                    int i_ret;
+
+                    psz_cmd = strchr( cl->buffer_read, ' ' );
+                    *psz_cmd = '\0';  psz_cmd++;
+                    if( ( psz_arg = strchr( psz_cmd, '\n' ) ) ) *psz_arg = '\0';
+                    if( ( psz_arg = strchr( psz_cmd, '\r' ) ) ) *psz_arg = '\0';
+                    if( ( psz_arg = strchr( psz_cmd, ' ' ) )
+                        && *psz_arg )
+                    {
+                        *psz_arg = '\0';
+                        psz_arg++;
+                    }
+
+                    i_ret = var_Command( p_intf, psz_name, psz_cmd, psz_arg,
+                                         &psz_msg );
+
+                    if( psz_msg )
+                    {
+                        vlm_message_t *message;
+                        message = vlm_MessageNew( "Module command", psz_msg );
+                        Write_message( cl, message, NULL, WRITE_MODE_CMD );
+                        vlm_MessageDelete( message );
+                        free( psz_msg );
+                    }
+                }
                 else
                 {
                     vlm_message_t *message;
                 else
                 {
                     vlm_message_t *message;
@@ -451,6 +483,9 @@ static void Run( intf_thread_t *p_intf )
                             vlm_MessageNew( "logout, quit, exit" , NULL ) );
                         vlm_MessageAdd( p_my_help,
                             vlm_MessageNew( "shutdown" , NULL ) );
                             vlm_MessageNew( "logout, quit, exit" , NULL ) );
                         vlm_MessageAdd( p_my_help,
                             vlm_MessageNew( "shutdown" , NULL ) );
+                        vlm_MessageAdd( p_my_help,
+                            vlm_MessageNew( "@moduleinstance command argument",
+                                             NULL) );
                         vlm_MessageAdd( message, p_my_help );
                     }
                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
                         vlm_MessageAdd( message, p_my_help );
                     }
                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
index 520604c..b7910d3 100644 (file)
@@ -1428,3 +1428,61 @@ static int InheritValue( vlc_object_t *p_this, const char *psz_name,
 
     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
 }
 
     return InheritValue( p_this->p_parent, psz_name, p_val, i_type );
 }
+
+/**********************************************************************
+ * Execute a var command on an object identified by its name
+ **********************************************************************/
+int __var_Command( vlc_object_t *p_this, const char *psz_name,
+                   const char *psz_cmd, const char *psz_arg, char **psz_msg )
+{
+    vlc_object_t *p_obj = vlc_object_find_name( p_this->p_libvlc,
+                                                psz_name, FIND_CHILD );
+    int i_type, i_ret;
+
+    if( !p_obj )
+    {
+        if( psz_msg )
+            *psz_msg = strdup( "Unknown destination object." );
+        return VLC_ENOOBJ;
+    }
+
+    i_type = var_Type( p_obj, psz_cmd );
+    if( !( i_type&VLC_VAR_ISCOMMAND ) )
+    {
+        vlc_object_release( p_obj );
+        if( psz_msg )
+            *psz_msg = strdup( "Variable doesn't exist or isn't a command." );
+        return VLC_EGENERIC;
+    }
+
+    i_type &= 0xf0;
+    switch( i_type )
+    {
+        case VLC_VAR_INTEGER:
+            i_ret = var_SetInteger( p_obj, psz_cmd, atoi( psz_arg ) );
+            break;
+        case VLC_VAR_FLOAT:
+            i_ret = var_SetFloat( p_obj, psz_cmd, atof( psz_arg ) );
+            break;
+        case VLC_VAR_STRING:
+            i_ret = var_SetString( p_obj, psz_cmd, psz_arg );
+            break;
+        case VLC_VAR_BOOL:
+            i_ret = var_SetBool( p_obj, psz_cmd, atoi( psz_arg ) );
+            break;
+        default:
+            i_ret = VLC_EGENERIC;
+            break;
+    }
+
+    vlc_object_release( p_obj );
+
+    if( psz_msg )
+    {
+        *psz_msg = (char*)malloc( 80 );
+        sprintf( *psz_msg, "%s on object %s returned %i (%s)",
+                 psz_cmd, psz_name, i_ret, vlc_error( i_ret ) );
+    }
+
+    return i_ret;
+}