]> git.sesse.net Git - vlc/blobdiff - modules/gui/skins2/parser/interpreter.cpp
* skins2: support for custom popup menus, and win32 implementation.
[vlc] / modules / gui / skins2 / parser / interpreter.cpp
index 353bb72fc44a08b0621793333e62af1dd4f5b55c..26a464713450ba7a04c72145241b0bff04aee1cb 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * interpreter.cpp
  *****************************************************************************
- * Copyright (C) 2003 VideoLAN
+ * Copyright (C) 2003 the VideoLAN team
  * $Id$
  *
  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
  *****************************************************************************/
 
 #include "interpreter.hpp"
+#include "expr_evaluator.hpp"
+#include "../commands/cmd_audio.hpp"
+#include "../commands/cmd_muxer.hpp"
 #include "../commands/cmd_playlist.hpp"
+#include "../commands/cmd_playtree.hpp"
 #include "../commands/cmd_dialogs.hpp"
 #include "../commands/cmd_dummy.hpp"
 #include "../commands/cmd_layout.hpp"
 #include "../commands/cmd_quit.hpp"
+#include "../commands/cmd_minimize.hpp"
 #include "../commands/cmd_input.hpp"
 #include "../commands/cmd_fullscreen.hpp"
+#include "../commands/cmd_on_top.hpp"
 #include "../commands/cmd_show_window.hpp"
 #include "../src/theme.hpp"
 #include "../src/var_manager.hpp"
@@ -46,12 +52,16 @@ Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
     REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
     REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
     REGISTER_CMD( "dialogs.file()", CmdDlgFile )
+    REGISTER_CMD( "dialogs.directory()", CmdDlgDirectory )
     REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
     REGISTER_CMD( "dialogs.net()", CmdDlgNet )
     REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
     REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
     REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
+    REGISTER_CMD( "dialogs.streamingWizard()", CmdDlgStreamingWizard )
     REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
+    REGISTER_CMD( "playlist.load()", CmdDlgPlaylistLoad )
+    REGISTER_CMD( "playlist.save()", CmdDlgPlaylistSave )
     REGISTER_CMD( "playlist.add()", CmdDlgAdd )
     VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
     m_commandMap["playlist.del()"] =
@@ -59,13 +69,45 @@ Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
     REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
     REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
     REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
+    m_commandMap["playlist.setRandom(true)"] =
+        CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
+    m_commandMap["playlist.setRandom(false)"] =
+        CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
+    m_commandMap["playlist.setLoop(true)"] =
+        CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
+    m_commandMap["playlist.setLoop(false)"] =
+        CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
+    m_commandMap["playlist.setRepeat(true)"] =
+        CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), true ) );
+    m_commandMap["playlist.setRepeat(false)"] =
+        CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), false ) );
+    VarTree &rVarTree = VlcProc::instance( getIntf() )->getPlaytreeVar();
+    m_commandMap["playtree.del()"] =
+        CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
+    REGISTER_CMD( "playtree.sort()", CmdPlaytreeSort )
     REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
     REGISTER_CMD( "vlc.play()", CmdPlay )
     REGISTER_CMD( "vlc.pause()", CmdPause )
-    REGISTER_CMD( "vlc.quit()", CmdQuit )
+    REGISTER_CMD( "vlc.stop()", CmdStop )
     REGISTER_CMD( "vlc.faster()", CmdFaster )
     REGISTER_CMD( "vlc.slower()", CmdSlower )
-    REGISTER_CMD( "vlc.stop()", CmdStop )
+    REGISTER_CMD( "vlc.mute()", CmdMute )
+    REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
+    REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
+    REGISTER_CMD( "vlc.minimize()", CmdMinimize )
+    REGISTER_CMD( "vlc.onTop()", CmdOnTop )
+    REGISTER_CMD( "vlc.quit()", CmdQuit )
+    m_commandMap["equalizer.enable()"] =
+        CmdGenericPtr( new CmdSetEqualizer( getIntf(), true ) );
+    m_commandMap["equalizer.disable()"] =
+        CmdGenericPtr( new CmdSetEqualizer( getIntf(), false ) );
+
+    // Register the constant bool variables in the var manager
+    VarManager *pVarManager = VarManager::instance( getIntf() );
+    VarBool *pVarTrue = new VarBoolTrue( getIntf() );
+    pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
+    VarBool *pVarFalse = new VarBoolFalse( getIntf() );
+    pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
 }
 
 
@@ -104,7 +146,35 @@ CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
 
     CmdGeneric *pCommand = NULL;
 
-    if( rAction.find( ".setLayout(" ) != string::npos )
+    if( rAction.find( ";" ) != string::npos )
+    {
+        // Several actions are defined...
+        list<CmdGeneric *> actionList;
+        string rightPart = rAction;
+        string::size_type pos = rightPart.find( ";" );
+        while( pos != string::npos )
+        {
+            string leftPart = rightPart.substr( 0, pos );
+            // Remove any whitespace at the end of the left part, and parse it
+            leftPart =
+                leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
+            actionList.push_back( parseAction( leftPart, pTheme ) );
+            // Now remove any whitespace at the beginning of the right part,
+            // and go on checking for further actions in it...
+            rightPart = rightPart.substr( pos, rightPart.size() );
+            rightPart =
+                rightPart.substr( rightPart.find_first_not_of( " \t;" ),
+                                  rightPart.size() );
+            pos = rightPart.find( ";" );
+        }
+        actionList.push_back( parseAction( rightPart, pTheme ) );
+
+        // The list is filled, we remove NULL pointers from it, just in case...
+        actionList.remove( NULL );
+
+        pCommand = new CmdMuxer( getIntf(), actionList );
+    }
+    else if( rAction.find( ".setLayout(" ) != string::npos )
     {
         int leftPos = rAction.find( ".setLayout(" );
         string windowId = rAction.substr( 0, leftPos );
@@ -112,13 +182,33 @@ CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
         int rightPos = rAction.find( ")", windowId.size() + 11 );
         string layoutId = rAction.substr( windowId.size() + 11,
                                           rightPos - (windowId.size() + 11) );
-        pCommand = new CmdLayout( getIntf(), windowId, layoutId );
+
+        TopWindow *pWin = pTheme->getWindowById( windowId );
+        GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
+        if( !pWin )
+        {
+            msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
+        }
+        else if( !pLayout )
+        {
+            msg_Err( getIntf(), "Unknown layout (%s)", layoutId.c_str() );
+        }
+        // Check that the layout doesn't correspond to another window
+        else if( pWin != pLayout->getWindow() )
+        {
+            msg_Err( getIntf(), "Layout %s is not associated to window %s",
+                     layoutId.c_str(), windowId.c_str() );
+        }
+        else
+        {
+            pCommand = new CmdLayout( getIntf(), *pWin, *pLayout );
+        }
     }
     else if( rAction.find( ".show()" ) != string::npos )
     {
         int leftPos = rAction.find( ".show()" );
         string windowId = rAction.substr( 0, leftPos );
-        GenericWindow *pWin = pTheme->getWindowById( windowId );
+        TopWindow *pWin = pTheme->getWindowById( windowId );
         if( pWin )
         {
             pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
@@ -126,14 +216,24 @@ CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
         }
         else
         {
-            msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
+            // It was maybe the id of a popup
+            Popup *pPopup = pTheme->getPopupById( windowId );
+            if( pPopup )
+            {
+                pCommand = new CmdShowPopup( getIntf(), *pPopup );
+            }
+            else
+            {
+                msg_Err( getIntf(), "Unknown window or popup (%s)",
+                         windowId.c_str() );
+            }
         }
     }
     else if( rAction.find( ".hide()" ) != string::npos )
     {
         int leftPos = rAction.find( ".hide()" );
         string windowId = rAction.substr( 0, leftPos );
-        GenericWindow *pWin = pTheme->getWindowById( windowId );
+        TopWindow *pWin = pTheme->getWindowById( windowId );
         if( pWin )
         {
             pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
@@ -150,6 +250,10 @@ CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
         // Add the command in the pool
         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
     }
+    else
+    {
+        msg_Warn( getIntf(), "Unknown action: %s", rAction.c_str() );
+    }
 
     return pCommand;
 }
@@ -157,74 +261,127 @@ CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
 
 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
 {
-    // Try to get the variable from the variable manager
     VarManager *pVarManager = VarManager::instance( getIntf() );
-    VarBool *pVar = (VarBool*)pVarManager->getVar( rName, "bool" );
 
-    if( pVar )
-    {
-        return pVar;
-    }
-    else if( rName.find( " and " ) != string::npos )
+    // Convert the expression into Reverse Polish Notation
+    ExprEvaluator evaluator( getIntf() );
+    evaluator.parse( rName );
+
+    list<VarBool*> varStack;
+
+    // Get the first token from the RPN stack
+    string token = evaluator.getToken();
+    while( !token.empty() )
     {
-        int leftPos = rName.find( " and " );
-        string name1 = rName.substr( 0, leftPos );
-        int rightPos = leftPos + 5;   // 5 is the size of " and "
-        string name2 = rName.substr( rightPos, rName.size() - rightPos );
-        // Retrive the two boolean variables
-        VarBool *pVar1 = getVarBool( name1, pTheme );
-        VarBool *pVar2 = getVarBool( name2, pTheme );
-        // Create a composite boolean variable
-        if( pVar1 && pVar2 )
+        if( token == "and" )
         {
+            // Get the 2 last variables on the stack
+            if( varStack.empty() )
+            {
+                msg_Err( getIntf(), "Invalid boolean expression: %s",
+                         rName.c_str());
+                return NULL;
+            }
+            VarBool *pVar1 = varStack.back();
+            varStack.pop_back();
+            if( varStack.empty() )
+            {
+                msg_Err( getIntf(), "Invalid boolean expression: %s",
+                         rName.c_str());
+                return NULL;
+            }
+            VarBool *pVar2 = varStack.back();
+            varStack.pop_back();
+
+            // Create a composite boolean variable
             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
+            varStack.push_back( pNewVar );
             // Register this variable in the manager
-            pVarManager->registerVar( VariablePtr( pNewVar ), rName );
-            return pNewVar;
+            pVarManager->registerVar( VariablePtr( pNewVar ) );
         }
-        else
+        else if( token == "or" )
         {
-            return NULL;
+            // Get the 2 last variables on the stack
+            if( varStack.empty() )
+            {
+                msg_Err( getIntf(), "Invalid boolean expression: %s",
+                         rName.c_str());
+                return NULL;
+            }
+            VarBool *pVar1 = varStack.back();
+            varStack.pop_back();
+            if( varStack.empty() )
+            {
+                msg_Err( getIntf(), "Invalid boolean expression: %s",
+                         rName.c_str());
+                return NULL;
+            }
+            VarBool *pVar2 = varStack.back();
+            varStack.pop_back();
+
+            // Create a composite boolean variable
+            VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
+            varStack.push_back( pNewVar );
+            // Register this variable in the manager
+            pVarManager->registerVar( VariablePtr( pNewVar ) );
         }
-    }
-    else if( rName.find( "not " ) != string::npos )
-    {
-        int rightPos = rName.find( "not " ) + 4;
-        string name = rName.substr( rightPos, rName.size() - rightPos );
-        // Retrive the boolean variable
-        VarBool *pVar = getVarBool( name, pTheme );
-        // Create a composite boolean variable
-        if( pVar )
+        else if( token == "not" )
         {
+            // Get the last variable on the stack
+            if( varStack.empty() )
+            {
+                msg_Err( getIntf(), "Invalid boolean expression: %s",
+                         rName.c_str());
+                return NULL;
+            }
+            VarBool *pVar = varStack.back();
+            varStack.pop_back();
+
+            // Create a composite boolean variable
             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
+            varStack.push_back( pNewVar );
             // Register this variable in the manager
-            pVarManager->registerVar( VariablePtr( pNewVar ), rName );
-            return pNewVar;
-        }
-        else
-        {
-            return NULL;
+            pVarManager->registerVar( VariablePtr( pNewVar ) );
         }
-    }
-    else if( rName.find( ".isVisible" ) != string::npos )
-    {
-        int leftPos = rName.find( ".isVisible" );
-        string windowId = rName.substr( 0, leftPos );
-        GenericWindow *pWin = pTheme->getWindowById( windowId );
-        if( pWin )
+        else if( token.find( ".isVisible" ) != string::npos )
         {
-            return &pWin->getVisibleVar();
+            int leftPos = token.find( ".isVisible" );
+            string windowId = token.substr( 0, leftPos );
+            TopWindow *pWin = pTheme->getWindowById( windowId );
+            if( pWin )
+            {
+                // Push the visibility variable on the stack
+                varStack.push_back( &pWin->getVisibleVar() );
+            }
+            else
+            {
+                msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
+                return NULL;
+            }
         }
         else
         {
-            msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
-            return NULL;
+            // Try to get the variable from the variable manager
+            VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
+            if( !pVar )
+            {
+                msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
+                         token.c_str());
+                return NULL;
+            }
+            varStack.push_back( pVar );
         }
+        // Get the first token from the RPN stack
+        token = evaluator.getToken();
     }
-    else
+
+    // The stack should contain a single variable
+    if( varStack.size() != 1 )
     {
+        msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
         return NULL;
     }
+    return varStack.back();
 }
 
 
@@ -245,3 +402,10 @@ VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
     return pVar;
 }
 
+VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
+{
+    // Try to get the variable from the variable manager
+    VarManager *pVarManager = VarManager::instance( getIntf() );
+    VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
+    return pVar;
+}