]> git.sesse.net Git - vlc/blobdiff - modules/control/hotkeys.c
hotkey: Stop the playlist on Quit
[vlc] / modules / control / hotkeys.c
index 8fbe4cd689d42bb16cf76a4fdd6d3db74a32e570..87cdda4f42f1da829b5353e10f48965e93a3c475 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/intf.h>
-#include <vlc/input.h>
-#include <vlc/vout.h>
-#include <vlc/aout.h>
+#include <vlc_interface.h>
+#include <vlc_input.h>
+#include <vlc_vout.h>
+#include <vlc_aout.h>
 #include <vlc_osd.h>
-
+#include <vlc_playlist.h>
 #include "vlc_keys.h"
 
 #define BUFFER_SIZE 10
@@ -112,9 +111,6 @@ static int Open( vlc_object_t *p_this )
     p_intf->p_sys->i_size = 0;
     p_intf->pf_run = Run;
 
-    p_intf->p_sys->p_input = NULL;
-    p_intf->p_sys->p_vout = NULL;
-
     var_AddCallback( p_intf->p_libvlc, "key-pressed", KeyEvent, p_intf );
     return VLC_SUCCESS;
 }
@@ -127,14 +123,8 @@ static void Close( vlc_object_t *p_this )
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
 
     var_DelCallback( p_intf->p_libvlc, "key-pressed", KeyEvent, p_intf );
-    if( p_intf->p_sys->p_input )
-    {
-        vlc_object_release( p_intf->p_sys->p_input );
-    }
-    if( p_intf->p_sys->p_vout )
-    {
-        vlc_object_release( p_intf->p_sys->p_vout );
-    }
+
+    vlc_mutex_destroy( &p_intf->p_sys->change_lock );
     /* Destroy structure */
     free( p_intf->p_sys );
 }
@@ -170,59 +160,45 @@ static void Run( intf_thread_t *p_intf )
         int i_times = 0;
 
         /* Sleep a bit */
-//        msleep( INTF_IDLE_SLEEP );
-
-        /* Update the input */
-        if( p_intf->p_sys->p_input == NULL )
-        {
-            PL_LOCK;
-            p_intf->p_sys->p_input = p_playlist->p_input;
-            if( p_intf->p_sys->p_input )
-                vlc_object_yield( p_intf->p_sys->p_input );
-            PL_UNLOCK;
-        }
-        else if( p_intf->p_sys->p_input->b_dead )
-        {
-            vlc_object_release( p_intf->p_sys->p_input );
-            p_intf->p_sys->p_input = NULL;
-        }
-        p_input = p_intf->p_sys->p_input;
+        /* msleep( INTF_IDLE_SLEEP ); */
 
-        /* Update the vout */
-        p_last_vout = p_intf->p_sys->p_vout;
-        if( p_vout == NULL )
-        {
-            p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
-            p_intf->p_sys->p_vout = p_vout;
-        }
-        else if( p_vout->b_die )
-        {
-            vlc_object_release( p_vout );
-            p_vout = NULL;
-            p_intf->p_sys->p_vout = NULL;
-        }
+        i_action = 0;
+        i_key = GetKey( p_intf );
 
-        /* Register OSD channels */
-        if( p_vout && p_vout != p_last_vout )
+        /* Special action for mouse event */
+        /* FIXME: This should probably be configurable */
+        /* FIXME: rework hotkeys handling to allow more than 1 event
+         * to trigger one same action */
+        switch (i_key & KEY_SPECIAL)
         {
-            for( i = 0; i < CHANNELS_NUMBER; i++ )
-            {
-                spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
-                             &p_intf->p_sys->p_channels[ i ] );
-            }
+            case KEY_MOUSEWHEELUP:
+                i_action = ACTIONID_VOL_UP;
+                break;
+            case KEY_MOUSEWHEELDOWN:
+                i_action = ACTIONID_VOL_DOWN;
+                break;
+            case KEY_MOUSEWHEELLEFT:
+                i_action = ACTIONID_JUMP_BACKWARD_EXTRASHORT;
+                break;
+            case KEY_MOUSEWHEELRIGHT:
+                i_action = ACTIONID_JUMP_FORWARD_EXTRASHORT;
+                break;
+            default: break;
         }
 
-        /* Find action triggered by hotkey */
-        i_action = 0;
-        i_key = GetKey( p_intf );
-        for( i = 0; i_key != -1 && p_hotkeys[i].psz_action != NULL; i++ )
+        /* No mouse action, find action triggered by hotkey */
+        if(!i_action)
         {
-            if( p_hotkeys[i].i_key == i_key )
+            for( i = 0; i_key != -1 && p_hotkeys[i].psz_action != NULL; i++ )
             {
-                i_action = p_hotkeys[i].i_action;
-                i_times  = p_hotkeys[i].i_times;
-                /* times key pressed within max. delta time */
-                p_hotkeys[i].i_times = 0;
+                if( p_hotkeys[i].i_key == i_key )
+                {
+                    i_action = p_hotkeys[i].i_action;
+                    i_times  = p_hotkeys[i].i_times;
+                    /* times key pressed within max. delta time */
+                    p_hotkeys[i].i_times = 0;
+                    break;
+                }
             }
         }
 
@@ -236,11 +212,39 @@ static void Run( intf_thread_t *p_intf )
             continue;
         }
 
+        /* Update the input */
+        PL_LOCK;
+        p_input = p_playlist->p_input;
+        if( p_input )
+            vlc_object_yield( p_input );
+        PL_UNLOCK;
+
+        /* Update the vout */
+        p_last_vout = p_vout;
+        p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
+
+        /* Register OSD channels */
+        if( p_vout && p_vout != p_last_vout )
+        {
+            for( i = 0; i < CHANNELS_NUMBER; i++ )
+            {
+                spu_Control( p_vout->p_spu, SPU_CHANNEL_REGISTER,
+                             &p_intf->p_sys->p_channels[ i ] );
+            }
+        }
+
         if( i_action == ACTIONID_QUIT )
         {
-            p_intf->p_libvlc->b_die = VLC_TRUE;
+            if( p_playlist )
+                playlist_Stop( p_playlist );
+            vlc_object_kill( p_intf->p_libvlc );
+            vlc_object_kill( p_intf );
             ClearChannels( p_intf, p_vout );
             vout_OSDMessage( p_intf, DEFAULT_CHAN, _( "Quit" ) );
+            if( p_vout )
+                vlc_object_release( p_vout );
+            if( p_input )
+                vlc_object_release( p_input );
             continue;
         }
         else if( i_action == ACTIONID_VOL_UP )
@@ -281,7 +285,7 @@ static void Run( intf_thread_t *p_intf )
         {
             if( p_vout ) vout_Control( p_vout, VOUT_SNAPSHOT );
         }
-        else if( i_action == ACTIONID_FULLSCREEN )
+        else if( i_action == ACTIONID_TOGGLE_FULLSCREEN )
         {
             if( p_vout )
             {
@@ -296,6 +300,57 @@ static void Run( intf_thread_t *p_intf )
                 var_Set( p_playlist, "fullscreen", val );
             }
         }
+        else if( i_action == ACTIONID_LEAVE_FULLSCREEN )
+        {
+            if( p_vout && var_GetBool( p_vout, "fullscreen" ) )
+            {
+                var_SetBool( p_vout, "fullscreen", VLC_FALSE );
+            }
+        }
+        else if( i_action == ACTIONID_WALLPAPER )
+        {
+            if( p_vout )
+            {
+                var_Get( p_vout, "directx-wallpaper", &val );
+                val.b_bool = !val.b_bool;
+                var_Set( p_vout, "directx-wallpaper", val );
+            }
+            else
+            {
+                var_Get( p_playlist, "directx-wallpaper", &val );
+                val.b_bool = !val.b_bool;
+                var_Set( p_playlist, "directx-wallpaper", val );
+            }
+        }
+        else if( i_action == ACTIONID_LOOP )
+        {
+            /* Toggle Normal -> Loop -> Repeat -> Normal ... */
+            vlc_value_t val2;
+            var_Get( p_playlist, "loop", &val );
+            var_Get( p_playlist, "repeat", &val2 );
+            if( val2.b_bool == VLC_TRUE )
+            {
+                val.b_bool = VLC_FALSE;
+                val2.b_bool = VLC_FALSE;
+            }
+            else if( val.b_bool == VLC_TRUE )
+            {
+                val.b_bool = VLC_FALSE;
+                val2.b_bool = VLC_TRUE;
+            }
+            else
+            {
+                val.b_bool = VLC_TRUE;
+            }
+            var_Set( p_playlist, "loop", val );
+            var_Set( p_playlist, "repeat", val2 );
+        }
+        else if( i_action == ACTIONID_RANDOM )
+        {
+            var_Get( p_playlist, "random", &val );
+            val.b_bool = !val.b_bool;
+            var_Set( p_playlist, "random", val );
+        }
         else if( i_action == ACTIONID_PLAY_PAUSE )
         {
             val.i_int = PLAYING_S;
@@ -657,7 +712,8 @@ static void Run( intf_thread_t *p_intf )
                 i_delay -= 50000;    /* 50 ms */
                 var_SetTime( p_input, "spu-delay", i_delay );
                 ClearChannels( p_intf, p_vout );
-                vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
+                vout_OSDMessage( p_intf, DEFAULT_CHAN,
+                                 _( "Subtitle delay %i ms" ),
                                  (int)(i_delay/1000) );
             }
             else if( i_action == ACTIONID_SUBDELAY_UP )
@@ -666,7 +722,8 @@ static void Run( intf_thread_t *p_intf )
                 i_delay += 50000;    /* 50 ms */
                 var_SetTime( p_input, "spu-delay", i_delay );
                 ClearChannels( p_intf, p_vout );
-                vout_OSDMessage( p_intf, DEFAULT_CHAN, "Subtitle delay %i ms",
+                vout_OSDMessage( p_intf, DEFAULT_CHAN,
+                                _( "Subtitle delay %i ms" ),
                                  (int)(i_delay/1000) );
             }
             else if( i_action == ACTIONID_AUDIODELAY_DOWN )
@@ -675,7 +732,8 @@ static void Run( intf_thread_t *p_intf )
                 i_delay -= 50000;    /* 50 ms */
                 var_SetTime( p_input, "audio-delay", i_delay );
                 ClearChannels( p_intf, p_vout );
-                vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
+                vout_OSDMessage( p_intf, DEFAULT_CHAN,
+                                _( "Audio delay %i ms" ),
                                  (int)(i_delay/1000) );
             }
             else if( i_action == ACTIONID_AUDIODELAY_UP )
@@ -684,7 +742,8 @@ static void Run( intf_thread_t *p_intf )
                 i_delay += 50000;    /* 50 ms */
                 var_SetTime( p_input, "audio-delay", i_delay );
                 ClearChannels( p_intf, p_vout );
-                vout_OSDMessage( p_intf, DEFAULT_CHAN, "Audio delay %i ms",
+                vout_OSDMessage( p_intf, DEFAULT_CHAN,
+                                _( "Audio delay %i ms" ),
                                  (int)(i_delay/1000) );
             }
             else if( i_action == ACTIONID_PLAY )
@@ -703,7 +762,39 @@ static void Run( intf_thread_t *p_intf )
                     playlist_Play( p_playlist );
                 }
             }
+            else if( i_action == ACTIONID_MENU_ON )
+            {
+                osd_MenuShow( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_OFF )
+            {
+                osd_MenuHide( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_LEFT )
+            {
+                osd_MenuPrev( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_RIGHT )
+            {
+                osd_MenuNext( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_UP )
+            {
+                osd_MenuUp( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_DOWN )
+            {
+                osd_MenuDown( VLC_OBJECT(p_intf) );
+            }
+            else if( i_action == ACTIONID_MENU_SELECT )
+            {
+                osd_MenuActivate( VLC_OBJECT(p_intf) );
+            }
         }
+        if( p_vout )
+            vlc_object_release( p_vout );
+        if( p_input )
+            vlc_object_release( p_input );
     }
     pl_Release( p_intf );
 }
@@ -737,6 +828,11 @@ static int KeyEvent( 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;
+    if ( !newval.i_int )
+    {
+        msg_Warn( p_this, "Received invalid key event %d", newval.i_int );
+        return VLC_EGENERIC;
+    }
     vlc_mutex_lock( &p_intf->p_sys->change_lock );
     if ( p_intf->p_sys->i_size == BUFFER_SIZE )
     {
@@ -787,7 +883,6 @@ static int ActionKeyCB( vlc_object_t *p_this, char const *psz_var,
 static void PlayBookmark( intf_thread_t *p_intf, int i_num )
 {
     vlc_value_t val;
-    int i;
     char psz_bookmark_name[11];
     playlist_t *p_playlist = pl_Yield( p_intf );
 
@@ -796,16 +891,20 @@ static void PlayBookmark( intf_thread_t *p_intf, int i_num )
     var_Get( p_intf, psz_bookmark_name, &val );
 
     char *psz_bookmark = strdup( val.psz_string );
-    for( i = 0; i < p_playlist->items.i_size; i++)
-    {
-        if( !strcmp( psz_bookmark,
-                     ARRAY_VAL( p_playlist->items,i )->p_input->psz_uri ) )
+    PL_LOCK;
+    FOREACH_ARRAY( playlist_item_t *p_item, p_playlist->items )
+        char *psz_uri = input_item_GetURI( p_item->p_input );
+        if( !strcmp( psz_bookmark, psz_uri ) )
         {
-            playlist_LockControl( p_playlist, PLAYLIST_VIEWPLAY, NULL,
-                                  ARRAY_VAL( p_playlist->items, i ) );
+            free( psz_uri );
+            playlist_Control( p_playlist, PLAYLIST_VIEWPLAY, VLC_TRUE,
+                              NULL, p_item );
             break;
         }
-    }
+        else
+            free( psz_uri );
+    FOREACH_END();
+    PL_UNLOCK;
     vlc_object_release( p_playlist );
 }
 
@@ -818,10 +917,10 @@ static void SetBookmark( intf_thread_t *p_intf, int i_num )
                 VLC_VAR_STRING|VLC_VAR_DOINHERIT );
     if( p_playlist->status.p_item )
     {
-        config_PutPsz( p_intf, psz_bookmark_name,
-                       p_playlist->status.p_item->p_input->psz_uri);
-        msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num,
-                  p_playlist->status.p_item->p_input->psz_uri);
+        char *psz_uri = input_item_GetURI( p_playlist->status.p_item->p_input );
+        config_PutPsz( p_intf, psz_bookmark_name, psz_uri);
+        msg_Info( p_intf, "setting playlist bookmark %i to %s", i_num, psz_uri);
+        free( psz_uri );
         config_SaveConfigFile( p_intf, "hotkeys" );
     }
     pl_Release( p_intf );
@@ -847,7 +946,7 @@ static void DisplayPosition( intf_thread_t *p_intf, vout_thread_t *p_vout,
     if( time.i_time > 0 )
     {
         secstotimestr( psz_duration, time.i_time / 1000000 );
-        vout_OSDMessage( p_input, POSITION_TEXT_CHAN, "%s / %s",
+        vout_OSDMessage( p_input, POSITION_TEXT_CHAN, (char *) "%s / %s",
                          psz_time, psz_duration );
     }
     else if( i_seconds > 0 )
@@ -879,7 +978,7 @@ static void DisplayVolume( intf_thread_t *p_intf, vout_thread_t *p_vout,
     }
     else
     {
-        vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, "Volume %d%%",
+        vout_OSDMessage( p_vout, VOLUME_TEXT_CHAN, _( "Volume %d%%" ),
                          i_vol*400/AOUT_VOLUME_MAX );
     }
 }