]> git.sesse.net Git - vlc/blobdiff - modules/control/gestures.c
Fix memleak in gesture module.
[vlc] / modules / control / gestures.c
index c95a434d60bcf7cde5fb317d6cf3f475987abb75..df9f8079e1620183e660a5f3f1aac66d70f06b9d 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
-#include <string.h>
 
-#include <vlc/vlc.h>
-#include <vlc/intf.h>
-#include <vlc/vout.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_interface.h>
+#include <vlc_vout.h>
+#include <vlc_aout.h>
+#include <vlc_playlist.h>
 
 #ifdef HAVE_UNISTD_H
 #    include <unistd.h>
@@ -41,9 +46,8 @@
 struct intf_sys_t
 {
     vlc_object_t *      p_vout;
-    input_thread_t *    p_input;
-    vlc_bool_t          b_got_gesture;
-    vlc_bool_t          b_button_pressed;
+    bool          b_got_gesture;
+    bool          b_button_pressed;
     int                 i_mouse_x, i_mouse_y;
     int                 i_last_x, i_last_y;
     unsigned int        i_pattern;
@@ -62,9 +66,10 @@ struct intf_sys_t
 #define NONE 0
 #define GESTURE( a, b, c, d ) (a | ( b << 4 ) | ( c << 8 ) | ( d << 12 ))
 
-int  E_(Open)   ( vlc_object_t * );
-void E_(Close)  ( vlc_object_t * );
+int  Open   ( vlc_object_t * );
+void Close  ( vlc_object_t * );
 static int  InitThread     ( intf_thread_t *p_intf );
+static void EndThread      ( intf_thread_t *p_intf );
 static int  MouseEvent     ( vlc_object_t *, char const *,
                              vlc_value_t, vlc_value_t, void * );
 
@@ -80,30 +85,31 @@ static void RunIntf        ( intf_thread_t *p_intf );
 
 #define BUTTON_TEXT N_( "Trigger button" )
 #define BUTTON_LONGTEXT N_( \
-    "You can set the trigger button for mouse gestures here." )
+    "Trigger button for mouse gestures." )
 
-static char *button_list[] = { "left", "middle", "right" };
-static char *button_list_text[] = { N_("Left"), N_("Middle"), N_("Right") };
+static const char *const button_list[] = { "left", "middle", "right" };
+static const char *const button_list_text[] =
+                                   { N_("Left"), N_("Middle"), N_("Right") };
 
 vlc_module_begin();
-    set_shortname( _("Gestures"));
+    set_shortname( N_("Gestures"));
     set_category( CAT_INTERFACE );
     set_subcategory( SUBCAT_INTERFACE_CONTROL );
     add_integer( "gestures-threshold", 30, NULL,
-                 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, VLC_TRUE );
+                 THRESHOLD_TEXT, THRESHOLD_LONGTEXT, true );
     add_string( "gestures-button", "right", NULL,
-                BUTTON_TEXT, BUTTON_LONGTEXT, VLC_FALSE );
+                BUTTON_TEXT, BUTTON_LONGTEXT, false );
         change_string_list( button_list, button_list_text, 0 );
-    set_description( _("Mouse gestures control interface") );
+    set_description( N_("Mouse gestures control interface") );
 
     set_capability( "interface", 0 );
-    set_callbacks( E_(Open), E_(Close) );
+    set_callbacks( Open, Close );
 vlc_module_end();
 
 /*****************************************************************************
  * OpenIntf: initialize interface
  *****************************************************************************/
-int E_(Open) ( vlc_object_t *p_this )
+int Open ( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
 
@@ -127,10 +133,27 @@ static int gesture( int i_pattern, int i_num )
     return ( i_pattern >> ( i_num * 4 ) ) & 0xF;
 }
 
+/*****************************************************************************
+ * input_from_playlist: don't forget to release the return value
+ *  Also this function should really be available from core.
+ *****************************************************************************/
+static input_thread_t * input_from_playlist ( playlist_t *p_playlist )
+{
+    input_thread_t * p_input;
+
+    PL_LOCK;
+    p_input = p_playlist->p_input;
+    if( p_input )
+        vlc_object_yield( p_input );
+    PL_UNLOCK;
+
+    return p_input;
+}
+
 /*****************************************************************************
  * CloseIntf: destroy dummy interface
  *****************************************************************************/
-void E_(Close) ( vlc_object_t *p_this )
+void Close ( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
 
@@ -145,17 +168,20 @@ void E_(Close) ( vlc_object_t *p_this )
 static void RunIntf( intf_thread_t *p_intf )
 {
     playlist_t * p_playlist = NULL;
+
+    vlc_mutex_lock( &p_intf->change_lock );
     p_intf->p_sys->p_vout = NULL;
+    vlc_mutex_unlock( &p_intf->change_lock );
 
     if( InitThread( p_intf ) < 0 )
     {
-        msg_Err( p_intf, "can't initialize intferface thread" );
+        msg_Err( p_intf, "can't initialize interface thread" );
         return;
     }
     msg_Dbg( p_intf, "interface thread initialized" );
 
     /* Main loop */
-    while( !p_intf->b_die )
+    while( !intf_ShouldDie( p_intf ) )
     {
         vlc_mutex_lock( &p_intf->change_lock );
 
@@ -164,43 +190,223 @@ static void RunIntf( intf_thread_t *p_intf )
          */
         if( p_intf->p_sys->b_got_gesture )
         {
+            vlc_value_t val;
+            int i_interval = 0;
             /* Do something */
+            /* If you modify this, please try to follow this convention:
+               Start with LEFT, RIGHT for playback related commands
+               and UP, DOWN, for other commands */
             switch( p_intf->p_sys->i_pattern )
             {
             case LEFT:
-                p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                              FIND_ANYWHERE );
-                if( p_playlist == NULL )
+                i_interval = config_GetInt( p_intf , "short-jump-size" );
+                if ( i_interval > 0 ) {
+                    val.i_time = ( (mtime_t)( -i_interval ) * 1000000L);
+                    var_Set( p_intf, "time-offset", val );
+                }
+                msg_Dbg(p_intf, "Go backward in the movie!");
+                break;
+            case RIGHT:
+                i_interval = config_GetInt( p_intf , "short-jump-size" );
+                if ( i_interval > 0 ) {
+                    val.i_time = ( (mtime_t)( i_interval ) * 1000000L);
+                    var_Set( p_intf, "time-offset", val );
+                }
+                msg_Dbg(p_intf, "Go forward in the movie!");
+                break;
+            case GESTURE(LEFT,UP,NONE,NONE):
+                /*FIXME BF*/
+                msg_Dbg(p_intf, "Going slower.");
+                break;
+            case GESTURE(RIGHT,UP,NONE,NONE):
+                /*FIXME FF*/
+                msg_Dbg(p_intf, "Going faster.");
+                break;
+            case GESTURE(LEFT,RIGHT,NONE,NONE):
+            case GESTURE(RIGHT,LEFT,NONE,NONE):
                 {
-                    break;
+                    input_thread_t * p_input;
+                    p_playlist = pl_Yield( p_intf );
+
+                    p_input = input_from_playlist( p_playlist );
+                    vlc_object_release( p_playlist );
+                    if( !p_input )
+                        break;
+                    val.i_int = PLAYING_S;
+                    if( p_input )
+                    {
+                        var_Get( p_input, "state", &val);
+                        if( val.i_int == PAUSE_S )
+                        {
+                            val.i_int = PLAYING_S;
+                        }
+                        else
+                        {
+                            val.i_int = PAUSE_S;
+                        }
+                        var_Set( p_input, "state", val);
+                    }
+                    msg_Dbg(p_intf, "Play/Pause");
+                    vlc_object_release( p_input );
                 }
+                break;
+            case GESTURE(LEFT,DOWN,NONE,NONE):
+                p_playlist = pl_Yield( p_intf );
 
                 playlist_Prev( p_playlist );
                 vlc_object_release( p_playlist );
                 break;
-            case RIGHT:
-                p_playlist = vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
-                                              FIND_ANYWHERE );
-                if( p_playlist == NULL )
-                {
-                    break;
-                }
+            case GESTURE(RIGHT,DOWN,NONE,NONE):
+                p_playlist = pl_Yield( p_intf );
 
                 playlist_Next( p_playlist );
                 vlc_object_release( p_playlist );
                 break;
+            case UP:
+                {
+                    audio_volume_t i_newvol;
+                    aout_VolumeUp( p_intf, 1, &i_newvol );
+                    msg_Dbg(p_intf, "Louder");
+                }
+                break;
+            case DOWN:
+                {
+                    audio_volume_t i_newvol;
+                    aout_VolumeDown( p_intf, 1, &i_newvol );
+                    msg_Dbg(p_intf, "Quieter");
+                }
+                break;
+            case GESTURE(UP,DOWN,NONE,NONE):
+            case GESTURE(DOWN,UP,NONE,NONE):
+                {
+                    audio_volume_t i_newvol = -1;
+                    aout_VolumeMute( p_intf, &i_newvol );
+                    msg_Dbg(p_intf, "Mute sound");
+                }
+                break;
             case GESTURE(UP,RIGHT,NONE,NONE):
+                {
+                   input_thread_t * p_input;
+                   vlc_value_t val, list, list2;
+                   int i_count, i;
+
+                    p_playlist = pl_Yield( p_intf );
+
+                    p_input = input_from_playlist( p_playlist );
+
+                    vlc_object_release( p_playlist );
+
+                    if( !p_input )
+                        break;
+
+                   var_Get( p_input, "audio-es", &val );
+                   var_Change( p_input, "audio-es", VLC_VAR_GETCHOICES,
+                               &list, &list2 );
+                   i_count = list.p_list->i_count;
+                   if( i_count <= 1 )
+                   {
+                       vlc_object_release( p_input );
+                       break;
+                   }
+                   for( i = 0; i < i_count; i++ )
+                   {
+                       if( val.i_int == list.p_list->p_values[i].i_int )
+                       {
+                           break;
+                       }
+                   }
+                   /* value of audio-es was not in choices list */
+                   if( i == i_count )
+                   {
+                       msg_Warn( p_input,
+                               "invalid current audio track, selecting 0" );
+                       var_Set( p_input, "audio-es",
+                               list.p_list->p_values[0] );
+                       i = 0;
+                   }
+                   else if( i == i_count - 1 )
+                   {
+                       var_Set( p_input, "audio-es",
+                               list.p_list->p_values[1] );
+                       i = 1;
+                   }
+                   else
+                   {
+                       var_Set( p_input, "audio-es",
+                               list.p_list->p_values[i+1] );
+                       i++;
+                   }
+                   vlc_object_release( p_input );
+                }
+                break;
+            case GESTURE(DOWN,RIGHT,NONE,NONE):
+                {
+                    input_thread_t * p_input;
+                    vlc_value_t val, list, list2;
+                    int i_count, i;
+
+                    p_playlist = pl_Yield( p_intf );
+
+                    p_input = input_from_playlist( p_playlist );
+                    vlc_object_release( p_playlist );
+
+                    if( !p_input )
+                        break;
+
+                    var_Get( p_input, "spu-es", &val );
+
+                    var_Change( p_input, "spu-es", VLC_VAR_GETCHOICES,
+                            &list, &list2 );
+                    i_count = list.p_list->i_count;
+                    if( i_count <= 1 )
+                    {
+                        vlc_object_release( p_input );
+                        break;
+                    }
+                    for( i = 0; i < i_count; i++ )
+                    {
+                        if( val.i_int == list.p_list->p_values[i].i_int )
+                        {
+                            break;
+                        }
+                    }
+                    /* value of spu-es was not in choices list */
+                    if( i == i_count )
+                    {
+                        msg_Warn( p_input,
+                                "invalid current subtitle track, selecting 0" );
+                        var_Set( p_input, "spu-es", list.p_list->p_values[0] );
+                        i = 0;
+                    }
+                    else if( i == i_count - 1 )
+                    {
+                        var_Set( p_input, "spu-es", list.p_list->p_values[0] );
+                        i = 0;
+                    }
+                    else
+                    {
+                        var_Set( p_input, "spu-es",
+                                list.p_list->p_values[i+1] );
+                        i = i + 1;
+                    }
+                    vlc_object_release( p_input );
+                }
+                break;
+            case GESTURE(UP,LEFT,NONE,NONE):
                 if (p_intf->p_sys->p_vout )
                 {
                     ((vout_thread_t *)p_intf->p_sys->p_vout)->i_changes |=
                         VOUT_FULLSCREEN_CHANGE;
                 }
                 break;
-            case GESTURE(DOWN,RIGHT,NONE,NONE):
+            case GESTURE(DOWN,LEFT,NONE,NONE):
                 /* FIXME: Should close the vout!"*/
-                p_intf->p_vlc->b_die = VLC_TRUE;
+                vlc_object_kill( p_intf->p_libvlc );
                 break;
             case GESTURE(DOWN,LEFT,UP,RIGHT):
+            case GESTURE(UP,RIGHT,DOWN,LEFT):
                 msg_Dbg(p_intf, "a square was drawn!" );
                 break;
             default:
@@ -208,16 +414,13 @@ static void RunIntf( intf_thread_t *p_intf )
             }
             p_intf->p_sys->i_num_gestures = 0;
             p_intf->p_sys->i_pattern = 0;
-            p_intf->p_sys->b_got_gesture = VLC_FALSE;
+            p_intf->p_sys->b_got_gesture = false;
         }
 
-
-        vlc_mutex_unlock( &p_intf->change_lock );
-
         /*
          * video output
          */
-        if( p_intf->p_sys->p_vout && p_intf->p_sys->p_vout->b_die )
+        if( p_intf->p_sys->p_vout && !vlc_object_alive (p_intf->p_sys->p_vout) )
         {
             var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
                              MouseEvent, p_intf );
@@ -240,18 +443,13 @@ static void RunIntf( intf_thread_t *p_intf )
             }
         }
 
+        vlc_mutex_unlock( &p_intf->change_lock );
+
         /* Wait a bit */
         msleep( INTF_IDLE_SLEEP );
     }
 
-    if( p_intf->p_sys->p_vout )
-    {
-        var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
-                         MouseEvent, p_intf );
-        var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
-                         MouseEvent, p_intf );
-        vlc_object_release( p_intf->p_sys->p_vout );
-    }
+    EndThread( p_intf );
 }
 
 /*****************************************************************************
@@ -261,17 +459,17 @@ static int InitThread( intf_thread_t * p_intf )
 {
     char *psz_button;
     /* we might need some locking here */
-    if( !p_intf->b_die )
+    if( !intf_ShouldDie( p_intf ) )
     {
-        input_thread_t * p_input;
-
-        p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
-
+        /* p_intf->change_lock locking strategy:
+         * - Every access to p_intf->p_sys are locked threw p_intf->change_lock
+         * - make sure there won't be  cross increment/decrement ref count
+         *   of p_intf->p_sys members p_intf->change_lock should be locked
+         *   during those operations */
         vlc_mutex_lock( &p_intf->change_lock );
 
-        p_intf->p_sys->p_input = p_input;
-        p_intf->p_sys->b_got_gesture = VLC_FALSE;
-        p_intf->p_sys->b_button_pressed = VLC_FALSE;
+        p_intf->p_sys->b_got_gesture = false;
+        p_intf->p_sys->b_button_pressed = false;
         p_intf->p_sys->i_threshold =
                      config_GetInt( p_intf, "gestures-threshold" );
         psz_button = config_GetPsz( p_intf, "gestures-button" );
@@ -287,6 +485,7 @@ static int InitThread( intf_thread_t * p_intf )
         {
             p_intf->p_sys->i_button_mask = 4;
         }
+        free( psz_button );
 
         p_intf->p_sys->i_pattern = 0;
         p_intf->p_sys->i_num_gestures = 0;
@@ -300,25 +499,47 @@ static int InitThread( intf_thread_t * p_intf )
     }
 }
 
+/*****************************************************************************
+ * EndThread:
+ *****************************************************************************/
+static void EndThread( intf_thread_t * p_intf )
+{
+    vlc_mutex_lock( &p_intf->change_lock );
+
+    if( p_intf->p_sys->p_vout )
+    {
+        var_DelCallback( p_intf->p_sys->p_vout, "mouse-moved",
+                         MouseEvent, p_intf );
+        var_DelCallback( p_intf->p_sys->p_vout, "mouse-button-down",
+                         MouseEvent, p_intf );
+        vlc_object_release( p_intf->p_sys->p_vout );
+    }
+
+    vlc_mutex_unlock( &p_intf->change_lock );
+}
+
 /*****************************************************************************
  * MouseEvent: callback for mouse events
  *****************************************************************************/
 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
+    VLC_UNUSED(p_this); VLC_UNUSED(oldval);
     vlc_value_t val;
     int pattern = 0;
 
     signed int i_horizontal, i_vertical;
     intf_thread_t *p_intf = (intf_thread_t *)p_data;
 
+    vlc_mutex_lock( &p_intf->change_lock );
+
     /* don't process new gestures before the last events are processed */
     if( p_intf->p_sys->b_got_gesture )
     {
+        vlc_mutex_unlock( &p_intf->change_lock );
         return VLC_SUCCESS;
     }
 
-    vlc_mutex_lock( &p_intf->change_lock );
     if( !strcmp(psz_var, "mouse-moved" ) && p_intf->p_sys->b_button_pressed )
     {
         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
@@ -370,7 +591,7 @@ static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
         && newval.i_int & p_intf->p_sys->i_button_mask
         && !p_intf->p_sys->b_button_pressed )
     {
-        p_intf->p_sys->b_button_pressed = VLC_TRUE;
+        p_intf->p_sys->b_button_pressed = true;
         var_Get( p_intf->p_sys->p_vout, "mouse-x", &val );
         p_intf->p_sys->i_last_x = val.i_int;
         var_Get( p_intf->p_sys->p_vout, "mouse-y", &val );
@@ -380,8 +601,8 @@ static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
         && !( newval.i_int & p_intf->p_sys->i_button_mask )
         && p_intf->p_sys->b_button_pressed )
     {
-        p_intf->p_sys->b_button_pressed = VLC_FALSE;
-        p_intf->p_sys->b_got_gesture = VLC_TRUE;
+        p_intf->p_sys->b_button_pressed = false;
+        p_intf->p_sys->b_got_gesture = true;
     }
 
     vlc_mutex_unlock( &p_intf->change_lock );