]> git.sesse.net Git - vlc/commitdiff
An interface to run extern programs (such as shutdown)
authorSylvain Cadhillac <sylv@videolan.org>
Fri, 8 Apr 2005 20:22:45 +0000 (20:22 +0000)
committerSylvain Cadhillac <sylv@videolan.org>
Fri, 8 Apr 2005 20:22:45 +0000 (20:22 +0000)
configure.ac
modules/misc/dummy/input.c
modules/misc/externrun/Modules.am [new file with mode: 0644]
modules/misc/externrun/externrun.c [new file with mode: 0644]

index 6b30883a9ff9b16c4261e44bcccbb72af77037d1..e9319dca2951be832387f9f009bcf65e6b83f254 100644 (file)
@@ -953,7 +953,7 @@ test "${enable_cprof}" != "yes" && enable_cprof="no"
 dnl
 dnl  default modules
 dnl
-VLC_ADD_PLUGINS([dummy logger memcpy])
+VLC_ADD_PLUGINS([dummy logger memcpy externrun])
 VLC_ADD_PLUGINS([mpgv mpga m4v m4a h264 ps pva avi asf aac mp4 rawdv nsv real aiff mjpeg demuxdump flac])
 VLC_ADD_PLUGINS([cvdsub svcdsub spudec subsdec dvbsub mpeg_audio lpcm a52 dts cinepak flacdec])
 VLC_ADD_PLUGINS([deinterlace invert adjust transform distort motionblur])
@@ -4373,6 +4373,7 @@ AC_CONFIG_FILES([
   modules/gui/wince/Makefile
   modules/misc/Makefile
   modules/misc/dummy/Makefile
+  modules/misc/externrun/Makefile
   modules/misc/memcpy/Makefile
   modules/misc/network/Makefile
   modules/misc/testsuite/Makefile
index 714b295dee31a259ce12491e076e7660d80c6254..fe13e7e0ae6e2d21eef8098cfa2f73fb3aaf1b49 100644 (file)
@@ -115,6 +115,9 @@ struct demux_sys_t
 
     /* Used for the pause command */
     mtime_t expiration;
+    
+    /* The command to run */
+    char* psz_command;
 };
 enum
 {
@@ -122,6 +125,7 @@ enum
     COMMAND_QUIT = 1,
     COMMAND_LOOP = 2,
     COMMAND_PAUSE= 3,
+    COMMAND_RUN  = 4,
 };
 
 static int Demux( demux_t * );
@@ -177,6 +181,16 @@ int E_(OpenDemux) ( vlc_object_t *p_this )
         p_sys->expiration = mdate() + (mtime_t)i_arg * (mtime_t)1000000;
         return VLC_SUCCESS;
     }
+    
+    /* Check for a "vlc:run:***" command */
+    if ( i_len > 4 && !strncasecmp( psz_name, "run:", 4 ) )
+    {
+       p_sys->psz_command = malloc( i_len - 4 );
+       strcpy( p_sys->psz_command, psz_name + 4 );
+       msg_Info( p_demux, "command `run program %s'", p_sys->psz_command );
+       p_sys->i_command = COMMAND_RUN;
+       return VLC_SUCCESS;
+    } 
 
     msg_Err( p_demux, "unknown command `%s'", psz_name );
 
@@ -227,11 +241,17 @@ static int Demux( demux_t *p_demux )
             else
                 msleep( 10000 );
             break;
+        
+        case COMMAND_RUN:
+            var_SetString( p_playlist, "run-program-command", p_sys->psz_command );
+            free( p_sys->psz_command );
+            b_eof = VLC_TRUE;
+            break;
 
         case COMMAND_NOP:
         default:
             b_eof = VLC_TRUE;
-            break;
+            break;       
     }
 
     vlc_object_release( p_playlist );
diff --git a/modules/misc/externrun/Modules.am b/modules/misc/externrun/Modules.am
new file mode 100644 (file)
index 0000000..131b5cc
--- /dev/null
@@ -0,0 +1,4 @@
+SOURCES_externrun = \
+       externrun.c \
+       $(NULL)
+
diff --git a/modules/misc/externrun/externrun.c b/modules/misc/externrun/externrun.c
new file mode 100644 (file)
index 0000000..a545a74
--- /dev/null
@@ -0,0 +1,280 @@
+/*****************************************************************************
+ * externrun.c: Run extern programs (vlc:run:"theprogram":delayinseconds)
+ *****************************************************************************
+ * Copyright (C) 2004 VideoLAN
+ *
+ * Authors: Sylvain Cadilhac <sylv@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 <stdlib.h>                                      /* malloc(), free() */
+
+#include <vlc/vlc.h>
+#include <vlc/intf.h>
+#include <string.h>
+
+#if defined( WIN32 )
+#include <windows.h>
+#endif
+
+
+/*****************************************************************************
+ * run_command_t: a command and its delay
+ *****************************************************************************/
+struct run_command_t
+{
+    int                                i_delay;
+    char                       *psz_torun;
+    struct run_command_t       *p_next;
+};
+typedef struct run_command_t run_command_t;
+
+/*****************************************************************************
+ * intf_sys_t: description and status of FB interface
+ *****************************************************************************/
+struct intf_sys_t
+{
+    vlc_mutex_t                change_lock;
+    mtime_t                    next_check;
+    run_command_t              *p_first_command;
+    run_command_t              *p_last_command;
+};
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  Open    ( vlc_object_t * );
+static void Close   ( vlc_object_t * );
+static void Run     ( intf_thread_t * );
+static int  AddRunCommand( vlc_object_t *, char const *,
+                      vlc_value_t, vlc_value_t, void * );
+
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+vlc_module_begin();
+    set_category( CAT_INTERFACE );
+    set_description( _("Execution of extern programs interface function") );
+    set_capability( "interface", 0 );
+    set_callbacks( Open, Close );
+vlc_module_end();
+
+/*****************************************************************************
+ * Open: initialize interface
+ *****************************************************************************/
+static int Open( vlc_object_t *p_this )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)p_this;
+    
+    /* Search for the Playlist object */
+    playlist_t *p_playlist;    
+    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
+    if( p_playlist == NULL )
+    {
+        msg_Err( p_intf, "we are not attached to a playlist" );
+        return 1;
+    }    
+
+    /* Allocate instance and initialize some members */
+    p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
+    if( p_intf->p_sys == NULL )
+    {
+        msg_Err( p_intf, "out of memory" );
+        return 1;
+    }
+    vlc_mutex_init( p_intf, &p_intf->p_sys->change_lock );
+    
+    var_Create( p_playlist, "run-program-command", VLC_VAR_STRING );
+    
+    p_intf->p_sys->p_first_command = NULL;
+    p_intf->p_sys->p_last_command = NULL;
+    p_intf->pf_run = Run;
+    var_AddCallback( p_playlist, "run-program-command", AddRunCommand, p_intf );
+    vlc_object_release( p_playlist );    
+    
+    return 0;
+}
+
+/*****************************************************************************
+ * Close: destroy interface
+ *****************************************************************************/
+static void Close( vlc_object_t *p_this )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)p_this;
+
+    playlist_t *p_playlist;    
+    p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
+    if( p_playlist == NULL )
+    {
+        msg_Err( p_intf, "we are not attached to a playlist" );
+        return;
+    }     
+    var_Destroy( p_playlist, "run-program-command" );
+    vlc_object_release( p_playlist );  
+    
+    run_command_t *p_command = p_intf->p_sys->p_first_command;
+    run_command_t *p_next;
+    
+    while( p_command )
+    {
+        p_next = p_command->p_next;
+        free( p_command->psz_torun );
+        free( p_command );
+        p_command = p_next;
+    }
+
+    /* Destroy structure */
+    free( p_intf->p_sys );
+}
+
+/*****************************************************************************
+ * Run: main loop
+ *****************************************************************************/
+static void Run( intf_thread_t *p_intf )
+{
+    p_intf->p_sys->next_check = mdate() + 1000000;
+    run_command_t *p_command;
+    run_command_t *p_previous;
+
+    while( !p_intf->b_die )
+    {
+        vlc_mutex_lock( &p_intf->p_sys->change_lock );
+        p_command = p_intf->p_sys->p_first_command;
+        p_previous = NULL;
+        while( p_command )
+        {
+            if( p_command->i_delay > 0 )
+            {
+                p_command->i_delay--;
+                p_previous = p_command;
+                p_command = p_previous->p_next;
+            }
+            else
+            {    
+                msg_Info( p_intf, "running %s", p_command->psz_torun );
+#if defined( WIN32 )
+
+                STARTUPINFO si;
+                PROCESS_INFORMATION pi;
+                ZeroMemory( &si, sizeof(si) );
+                si.cb = sizeof(si);
+                ZeroMemory( &pi, sizeof(pi) );
+                if( !CreateProcess( NULL, p_command->psz_torun, NULL, NULL,
+                    FALSE, 0, NULL, NULL, &si, &pi ) ) 
+                {
+                    msg_Err( p_intf, "can't run \"%s\"", p_command->psz_torun );
+                }    
+#else
+               if ( fork() )
+               {
+                   execl( p_command->psz_torun, NULL, (char *)NULL );
+               }
+#endif
+               if( p_previous )
+               {
+                   p_previous->p_next = p_command->p_next;
+                   free( p_command->psz_torun );
+                   free( p_command );
+                   p_command = p_previous->p_next;
+               }
+               else
+               {
+                   p_intf->p_sys->p_first_command = p_command->p_next;
+                   free( p_command->psz_torun );
+                   free( p_command );
+                   if ( p_intf->p_sys->p_last_command == p_command )
+                   {
+                       p_intf->p_sys->p_last_command = p_command->p_next;
+                   }
+                   p_command = p_intf->p_sys->p_first_command;
+               }
+            }
+        }
+        vlc_mutex_unlock( &p_intf->p_sys->change_lock );
+        mwait( p_intf->p_sys->next_check );
+        p_intf->p_sys->next_check += 1000000;
+    }
+    return VLC_SUCCESS;
+}
+
+
+/*****************************************************************************
+ * 
+ *****************************************************************************/
+static int AddRunCommand( vlc_object_t *p_this, char const *psz_var,
+                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
+{
+    intf_thread_t *p_intf = (intf_thread_t *)p_data;
+    
+
+    char *psz_command = NULL;
+    int i_delay = 0;
+    char *psz_delay = strrchr( newval.psz_string, ':' );
+    if ( psz_delay )
+    {
+        char *psz_err;
+        i_delay = ( int )strtol( psz_delay + 1, &psz_err, 10 );
+        if ( *psz_err == 0 )
+        {
+            psz_command = malloc( psz_delay - newval.psz_string );
+            if ( psz_command == NULL )
+            {
+                msg_Err( p_intf, "out of memory: can't add run command" );
+                return VLC_EGENERIC;
+            }
+            strncpy( psz_command, newval.psz_string, psz_delay - newval.psz_string );
+            psz_command[ psz_delay - newval.psz_string ] = '\0';
+        }
+    }
+    if ( !psz_command )
+    {
+        psz_command = malloc( strlen( newval.psz_string ) + 1 );
+        strcpy( psz_command, newval.psz_string );
+    }
+    
+    run_command_t *p_command = malloc( sizeof( run_command_t ) );
+    if ( p_command == NULL )
+    {
+        msg_Err( p_intf, "out of memory: can't add run command" );
+        return VLC_EGENERIC;    
+    }
+    p_command->i_delay = i_delay;
+    p_command->p_next = NULL;
+    p_command->psz_torun = psz_command;
+    
+    msg_Info( p_intf, "will run %s in %d seconds", psz_command, i_delay );
+    
+    
+    vlc_mutex_lock( &p_intf->p_sys->change_lock );
+    if ( p_intf->p_sys->p_last_command )
+    {
+        p_intf->p_sys->p_last_command->p_next = p_command;
+        p_intf->p_sys->p_last_command = p_command;
+    }
+    else
+    {
+        p_intf->p_sys->p_last_command = p_command;
+        p_intf->p_sys->p_first_command = p_command;
+    }
+    vlc_mutex_unlock( &p_intf->p_sys->change_lock );
+
+    return VLC_SUCCESS;
+}
+
+