]> git.sesse.net Git - vlc/blobdiff - modules/access_filter/record.c
Add a variable + callback to toggle the record status.
[vlc] / modules / access_filter / record.c
index f3362765ea739e4a6b57b04f5db73c969923a33b..67dba9d280a221898da2188881f4ac723b6a6ddf 100644 (file)
  * Preamble
  *****************************************************************************/
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 
-#include <stdio.h>
 #include <vlc_input.h>
 #include <vlc_access.h>
 
@@ -49,15 +53,16 @@ static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 vlc_module_begin();
-    set_shortname( _("Record") );
-    set_description( _("Record") );
+    set_shortname( N_("Record") );
+    set_description( N_("Record") );
     set_category( CAT_INPUT );
     set_subcategory( SUBCAT_INPUT_ACCESS_FILTER );
     set_capability( "access_filter", 0 );
     add_shortcut( "record" );
 
     add_directory( "record-path", NULL, NULL,
-                   RECORD_PATH_TXT, RECORD_PATH_LONGTXT, VLC_TRUE );
+                   RECORD_PATH_TXT, RECORD_PATH_LONGTXT, true );
+        change_unsafe();
 
     set_callbacks( Open, Close );
 
@@ -68,7 +73,7 @@ vlc_module_end();
  *****************************************************************************/
 
 static block_t *Block  ( access_t * );
-static int      Read   ( access_t *, uint8_t *, int );
+static ssize_t  Read   ( access_t *, uint8_t *, size_t );
 static int      Control( access_t *, int i_query, va_list args );
 static int      Seek   ( access_t *, int64_t );
 
@@ -76,10 +81,12 @@ static void Dump( access_t *, uint8_t *, int );
 
 static int EventKey( vlc_object_t *, char const *,
                      vlc_value_t, vlc_value_t, void * );
+static int ToggleRecord( vlc_object_t *, char const *,
+                         vlc_value_t, vlc_value_t, void * );
 
 struct access_sys_t
 {
-    vlc_bool_t b_dump;
+    bool b_dump;
 
     char *psz_path;
     const char *psz_ext;
@@ -103,7 +110,7 @@ static inline void PreUpdateFlags( access_t *p_access )
 static inline void PostUpdateFlags( access_t *p_access )
 {
     access_t *p_src = p_access->p_source;
-    /* */
+
     p_access->info = p_src->info;
     p_access->p_sys->i_update_sav = p_access->info.i_update;
 }
@@ -119,24 +126,22 @@ static int Open( vlc_object_t *p_this )
     access_sys_t *p_sys;
     char *psz;
 
-    /* */
     p_access->pf_read  = p_src->pf_read  ? Read : NULL;
     p_access->pf_block = p_src->pf_block ? Block : NULL;
     p_access->pf_seek  = p_src->pf_seek  ? Seek : NULL;
     p_access->pf_control = Control;
 
-    /* */
     p_access->info = p_src->info;
 
-    /* */
     p_access->p_sys = p_sys = malloc( sizeof( access_t ) );
+    if( !p_sys ) return VLC_ENOMEM;
 
     /* */
     p_sys->f = NULL;
     p_sys->i_size = 0;
     p_sys->psz_file = NULL;
     p_sys->psz_ext = "dat";
-    p_sys->b_dump = VLC_FALSE;
+    p_sys->b_dump = false;
     p_sys->p_vout = NULL;
     p_sys->i_vout_chan = -1;
     p_sys->i_update_sav = p_access->info.i_update;
@@ -149,14 +154,22 @@ static int Open( vlc_object_t *p_this )
     if( *psz == '\0' )
     {
         free( psz );
-        if( p_access->p_libvlc->psz_homedir )
-            psz = strdup( p_access->p_libvlc->psz_homedir );
+        psz = strdup( config_GetHomeDir() );
     }
     p_sys->psz_path = psz;
     msg_Dbg( p_access, "Record access filter path %s", psz );
 
+    input_thread_t *p_input = ( input_thread_t * )
+            vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
+    if( p_input )
+    {
+        var_Create( p_input, "record-toggle", VLC_VAR_VOID );
+        var_AddCallback( p_input, "record-toggle", ToggleRecord, p_access );
+        vlc_object_release( p_input );
+    }
+
     /* catch all key event */
-    var_AddCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
+    var_AddCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
 
     return VLC_SUCCESS;
 }
@@ -169,7 +182,14 @@ static void Close( vlc_object_t *p_this )
     access_t     *p_access = (access_t*)p_this;
     access_sys_t *p_sys = p_access->p_sys;
 
-    var_DelCallback( p_access->p_libvlc, "key-pressed", EventKey, p_access );
+    var_DelCallback( p_access->p_libvlc, "key-action", EventKey, p_access );
+    input_thread_t *p_input = ( input_thread_t * )
+            vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
+    if( p_input )
+    {
+        var_Destroy( p_input, "record-toggle" );
+        vlc_object_release( p_input );
+    }
 
     if( p_sys->f )
     {
@@ -189,15 +209,12 @@ static block_t *Block( access_t *p_access )
     access_t     *p_src = p_access->p_source;
     block_t      *p_block;
 
-    /* */
     PreUpdateFlags( p_access );
 
-    /* */
     p_block = p_src->pf_block( p_src );
     if( p_block && p_block->i_buffer )
         Dump( p_access, p_block->p_buffer, p_block->i_buffer );
 
-    /* */
     PostUpdateFlags( p_access );
 
     return p_block;
@@ -206,21 +223,17 @@ static block_t *Block( access_t *p_access )
 /*****************************************************************************
  *
  *****************************************************************************/
-static int Read( access_t *p_access, uint8_t *p_buffer, int i_len )
+static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
 {
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
-    /* */
     PreUpdateFlags( p_access );
 
-    /* */
     i_ret = p_src->pf_read( p_src, p_buffer, i_len );
-
     if( i_ret > 0 )
         Dump( p_access, p_buffer, i_ret );
 
-    /* */
     PostUpdateFlags( p_access );
 
     return i_ret;
@@ -234,13 +247,10 @@ static int Control( access_t *p_access, int i_query, va_list args )
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
-    /* */
     PreUpdateFlags( p_access );
 
-    /* */
     i_ret = p_src->pf_control( p_src, i_query, args );
 
-    /* */
     PostUpdateFlags( p_access );
 
     return i_ret;
@@ -254,13 +264,10 @@ static int Seek( access_t *p_access, int64_t i_pos )
     access_t     *p_src = p_access->p_source;
     int i_ret;
 
-    /* */
     PreUpdateFlags( p_access );
 
-    /* */
     i_ret = p_src->pf_seek( p_src, i_pos );
 
-    /* */
     PostUpdateFlags( p_access );
 
     return i_ret;
@@ -275,32 +282,42 @@ static int EventKey( vlc_object_t *p_this, char const *psz_var,
     access_t     *p_access = p_data;
     access_sys_t *p_sys = p_access->p_sys;
 
-    struct hotkey *p_hotkeys = p_access->p_libvlc->p_hotkeys;
-    int i_action = -1, i;
-
-    for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
-    {
-        if( p_hotkeys[i].i_key == newval.i_int )
-        {
-            i_action = p_hotkeys[i].i_action;
-        }
-    }
+    (void)p_this;
+    (void)psz_var;
+    (void)oldval;
 
-    if( i_action == ACTIONID_RECORD )
+    if( newval.i_int == ACTIONID_RECORD )
     {
         if( p_sys->b_dump )
-            p_sys->b_dump = VLC_FALSE;
+            p_sys->b_dump = false;
         else
-            p_sys->b_dump = VLC_TRUE;
+            p_sys->b_dump = true;
     }
 
     return VLC_SUCCESS;
 }
 
+static int ToggleRecord( vlc_object_t *p_this, char const *psz_var,
+                          vlc_value_t oldval, vlc_value_t newval,
+                          void *p_data )
+{
+    access_t     *p_access = p_data;
+    access_sys_t *p_sys = p_access->p_sys;
+
+    (void)p_this;
+    (void)psz_var;
+    (void)oldval;
+    (void)newval;
+
+    p_sys->b_dump = !p_sys->b_dump;
+
+    return VLC_SUCCESS;
+}
+
 /*****************************************************************************
  *
  *****************************************************************************/
-static void Notify( access_t *p_access, vlc_bool_t b_dump )
+static void Notify( access_t *p_access, bool b_dump )
 {
     access_sys_t *p_sys = p_access->p_sys;
     vout_thread_t *p_vout;
@@ -334,15 +351,14 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
     access_sys_t *p_sys = p_access->p_sys;
     int i_write;
 
-    /* */
     if( !p_sys->b_dump )
     {
         if( p_sys->f )
         {
-            msg_Dbg( p_access, "dumped "I64Fd" kb (%s)",
+            msg_Dbg( p_access, "dumped %"PRId64" kb (%s)",
                      p_sys->i_size/1024, p_sys->psz_file );
 
-            Notify( p_access, VLC_FALSE );
+            Notify( p_access, false );
 
             fclose( p_sys->f );
             p_sys->f = NULL;
@@ -355,7 +371,6 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
         return;
     }
 
-    /* */
     if( !p_sys->f )
     {
         input_thread_t *p_input;
@@ -363,16 +378,7 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
         time_t t = time(NULL);
         struct tm l;
 
-#ifdef HAVE_LOCALTIME_R
         if( !localtime_r( &t, &l ) ) memset( &l, 0, sizeof(l) );
-#else
-        /* Grrr */
-        {
-            struct tm *p_l = localtime( &t );
-            if( p_l ) l = *p_l;
-            else memset( &l, 0, sizeof(l) );
-        }
-#endif
 
         p_input = vlc_object_find( p_access, VLC_OBJECT_INPUT, FIND_PARENT );
         if( p_input )
@@ -395,16 +401,19 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
             vlc_object_release( p_input );
         }
 
-        if( psz_name == NULL )
-            psz_name = strdup( "Unknown" );
-
-        asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
-                  psz_name,
-                  l.tm_mday, l.tm_mon+1, l.tm_year+1900,
-                  l.tm_hour, l.tm_min, l.tm_sec,
-                  p_sys->psz_ext );
+        if( asprintf( &p_sys->psz_file, "%s %d-%d-%d %.2dh%.2dm%.2ds.%s",
+                      ( psz_name != NULL ) ? psz_name : "Unknown",
+                      l.tm_mday, l.tm_mon+1, l.tm_year+1900,
+                      l.tm_hour, l.tm_min, l.tm_sec,
+                      p_sys->psz_ext ) == -1 )
+            p_sys->psz_file = NULL;
 
         free( psz_name );
+        if( p_sys->psz_file == NULL )
+        {
+            p_sys->b_dump = false;
+            return;
+        }
 
         /* Remove all forbidden characters (except (back)slashes) */
         for( psz = p_sys->psz_file; *psz; psz++ )
@@ -423,36 +432,41 @@ static void Dump( access_t *p_access, uint8_t *p_buffer, int i_buffer )
 #endif
         }
 
-        psz_name=strdup(p_sys->psz_file);
+        psz_name = p_sys->psz_file;
 
 #if defined (WIN32) || defined (UNDER_CE)
 #define DIR_SEP "\\"
 #else
 #define DIR_SEP "/"
 #endif
-        asprintf(&p_sys->psz_file, "%s" DIR_SEP "%s",
-                 p_sys->psz_path, psz_name);
-        free(psz_name);
+        if( asprintf( &p_sys->psz_file, "%s" DIR_SEP "%s",
+                      p_sys->psz_path, psz_name ) == -1 )
+            p_sys->psz_file = NULL;
+        free( psz_name );
+        if( p_sys->psz_file == NULL )
+        {
+            p_sys->b_dump = false;
+            return;
+        }
 
         msg_Dbg( p_access, "dump in file '%s'", p_sys->psz_file );
 
         p_sys->f = utf8_fopen( p_sys->psz_file, "wb" );
         if( p_sys->f == NULL )
         {
-            msg_Err( p_access, "cannot open file '%s' (%s)",
-                     p_sys->psz_file, strerror(errno) );
+            msg_Err( p_access, "cannot open file '%s' (%m)",
+                     p_sys->psz_file );
             free( p_sys->psz_file );
             p_sys->psz_file = NULL;
-            p_sys->b_dump = VLC_FALSE;
+            p_sys->b_dump = false;
             return;
         }
 
-        Notify( p_access, VLC_TRUE );
+        Notify( p_access, true );
 
         p_sys->i_size = 0;
     }
 
-    /* */
     if( ( i_write = fwrite( p_buffer, 1, i_buffer, p_sys->f ) ) > 0 )
         p_sys->i_size += i_write;
 }