1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 #include "interpreter.hpp"
26 #include "expr_evaluator.hpp"
27 #include "../commands/cmd_playlist.hpp"
28 #include "../commands/cmd_dialogs.hpp"
29 #include "../commands/cmd_dummy.hpp"
30 #include "../commands/cmd_layout.hpp"
31 #include "../commands/cmd_quit.hpp"
32 #include "../commands/cmd_input.hpp"
33 #include "../commands/cmd_fullscreen.hpp"
34 #include "../commands/cmd_show_window.hpp"
35 #include "../src/theme.hpp"
36 #include "../src/var_manager.hpp"
37 #include "../src/vlcproc.hpp"
40 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
42 /// Create the generic commands
43 #define REGISTER_CMD( name, cmd ) \
44 m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
46 REGISTER_CMD( "none", CmdDummy )
47 REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
48 REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
49 REGISTER_CMD( "dialogs.file()", CmdDlgFile )
50 REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
51 REGISTER_CMD( "dialogs.net()", CmdDlgNet )
52 REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
53 REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
54 REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
55 REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
56 REGISTER_CMD( "playlist.add()", CmdDlgAdd )
57 VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
58 m_commandMap["playlist.del()"] =
59 CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
60 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
61 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
62 REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
63 m_commandMap["playlist.setRandom(true)"] =
64 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
65 m_commandMap["playlist.setRandom(false)"] =
66 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
67 m_commandMap["playlist.setLoop(true)"] =
68 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
69 m_commandMap["playlist.setLoop(false)"] =
70 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
71 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
72 REGISTER_CMD( "vlc.play()", CmdPlay )
73 REGISTER_CMD( "vlc.pause()", CmdPause )
74 REGISTER_CMD( "vlc.quit()", CmdQuit )
75 REGISTER_CMD( "vlc.faster()", CmdFaster )
76 REGISTER_CMD( "vlc.slower()", CmdSlower )
77 REGISTER_CMD( "vlc.stop()", CmdStop )
79 // Register the constant bool variables in the var manager
80 VarManager *pVarManager = VarManager::instance( getIntf() );
81 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
82 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
83 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
84 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
88 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
90 if( ! pIntf->p_sys->p_interpreter )
92 Interpreter *pInterpreter;
93 pInterpreter = new Interpreter( pIntf );
96 pIntf->p_sys->p_interpreter = pInterpreter;
99 return pIntf->p_sys->p_interpreter;
103 void Interpreter::destroy( intf_thread_t *pIntf )
105 if( pIntf->p_sys->p_interpreter )
107 delete pIntf->p_sys->p_interpreter;
108 pIntf->p_sys->p_interpreter = NULL;
113 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
115 // Try to find the command in the global command map
116 if( m_commandMap.find( rAction ) != m_commandMap.end() )
118 return m_commandMap[rAction].get();
121 CmdGeneric *pCommand = NULL;
123 if( rAction.find( ".setLayout(" ) != string::npos )
125 int leftPos = rAction.find( ".setLayout(" );
126 string windowId = rAction.substr( 0, leftPos );
127 // 11 is the size of ".setLayout("
128 int rightPos = rAction.find( ")", windowId.size() + 11 );
129 string layoutId = rAction.substr( windowId.size() + 11,
130 rightPos - (windowId.size() + 11) );
131 pCommand = new CmdLayout( getIntf(), windowId, layoutId );
133 else if( rAction.find( ".show()" ) != string::npos )
135 int leftPos = rAction.find( ".show()" );
136 string windowId = rAction.substr( 0, leftPos );
137 TopWindow *pWin = pTheme->getWindowById( windowId );
140 pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
145 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
148 else if( rAction.find( ".hide()" ) != string::npos )
150 int leftPos = rAction.find( ".hide()" );
151 string windowId = rAction.substr( 0, leftPos );
152 TopWindow *pWin = pTheme->getWindowById( windowId );
155 pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
160 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
166 // Add the command in the pool
167 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
174 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
176 VarManager *pVarManager = VarManager::instance( getIntf() );
178 // Convert the expression into Reverse Polish Notation
179 ExprEvaluator *pEvaluator = new ExprEvaluator( getIntf() );
180 pEvaluator->parse( rName );
182 list<VarBool*> varStack;
184 // Get the first token from the RPN stack
185 string token = pEvaluator->getToken();
186 while( !token.empty() )
190 // Get the 2 last variables on the stack
191 if( varStack.empty() )
193 msg_Err( getIntf(), "Invalid boolean expression: %s",
197 VarBool *pVar1 = varStack.back();
199 if( varStack.empty() )
201 msg_Err( getIntf(), "Invalid boolean expression: %s",
205 VarBool *pVar2 = varStack.back();
208 // Create a composite boolean variable
209 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
210 varStack.push_back( pNewVar );
211 // Register this variable in the manager
212 pVarManager->registerVar( VariablePtr( pNewVar ) );
214 else if( token == "not" )
216 // Get the last variable on the stack
217 if( varStack.empty() )
219 msg_Err( getIntf(), "Invalid boolean expression: %s",
223 VarBool *pVar = varStack.back();
226 // Create a composite boolean variable
227 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
228 varStack.push_back( pNewVar );
229 // Register this variable in the manager
230 pVarManager->registerVar( VariablePtr( pNewVar ) );
232 else if( token.find( ".isVisible" ) != string::npos )
234 int leftPos = token.find( ".isVisible" );
235 string windowId = token.substr( 0, leftPos );
236 TopWindow *pWin = pTheme->getWindowById( windowId );
239 // Push the visibility variable on the stack
240 varStack.push_back( &pWin->getVisibleVar() );
244 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
250 // Try to get the variable from the variable manager
251 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
254 msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
258 varStack.push_back( pVar );
260 // Get the first token from the RPN stack
261 token = pEvaluator->getToken();
264 // The stack should contain a single variable
265 if( varStack.size() != 1 )
267 msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
270 return varStack.back();
274 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
276 // Try to get the variable from the variable manager
277 VarManager *pVarManager = VarManager::instance( getIntf() );
278 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
283 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
285 // Try to get the variable from the variable manager
286 VarManager *pVarManager = VarManager::instance( getIntf() );
287 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );