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_minimize.hpp"
33 #include "../commands/cmd_input.hpp"
34 #include "../commands/cmd_fullscreen.hpp"
35 #include "../commands/cmd_on_top.hpp"
36 #include "../commands/cmd_show_window.hpp"
37 #include "../src/theme.hpp"
38 #include "../src/var_manager.hpp"
39 #include "../src/vlcproc.hpp"
42 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
44 /// Create the generic commands
45 #define REGISTER_CMD( name, cmd ) \
46 m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
48 REGISTER_CMD( "none", CmdDummy )
49 REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
50 REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
51 REGISTER_CMD( "dialogs.file()", CmdDlgFile )
52 REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
53 REGISTER_CMD( "dialogs.net()", CmdDlgNet )
54 REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
55 REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
56 REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
57 REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
58 REGISTER_CMD( "playlist.add()", CmdDlgAdd )
59 VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
60 m_commandMap["playlist.del()"] =
61 CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
62 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
63 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
64 REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
65 m_commandMap["playlist.setRandom(true)"] =
66 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
67 m_commandMap["playlist.setRandom(false)"] =
68 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
69 m_commandMap["playlist.setLoop(true)"] =
70 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
71 m_commandMap["playlist.setLoop(false)"] =
72 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
73 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
74 REGISTER_CMD( "vlc.play()", CmdPlay )
75 REGISTER_CMD( "vlc.pause()", CmdPause )
76 REGISTER_CMD( "vlc.stop()", CmdStop )
77 REGISTER_CMD( "vlc.faster()", CmdFaster )
78 REGISTER_CMD( "vlc.slower()", CmdSlower )
79 REGISTER_CMD( "vlc.mute()", CmdMute )
80 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
81 REGISTER_CMD( "vlc.ontop()", CmdOnTop )
82 REGISTER_CMD( "vlc.quit()", CmdQuit )
84 // Register the constant bool variables in the var manager
85 VarManager *pVarManager = VarManager::instance( getIntf() );
86 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
87 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
88 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
89 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
93 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
95 if( ! pIntf->p_sys->p_interpreter )
97 Interpreter *pInterpreter;
98 pInterpreter = new Interpreter( pIntf );
101 pIntf->p_sys->p_interpreter = pInterpreter;
104 return pIntf->p_sys->p_interpreter;
108 void Interpreter::destroy( intf_thread_t *pIntf )
110 if( pIntf->p_sys->p_interpreter )
112 delete pIntf->p_sys->p_interpreter;
113 pIntf->p_sys->p_interpreter = NULL;
118 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
120 // Try to find the command in the global command map
121 if( m_commandMap.find( rAction ) != m_commandMap.end() )
123 return m_commandMap[rAction].get();
126 CmdGeneric *pCommand = NULL;
128 if( rAction.find( ".setLayout(" ) != string::npos )
130 int leftPos = rAction.find( ".setLayout(" );
131 string windowId = rAction.substr( 0, leftPos );
132 // 11 is the size of ".setLayout("
133 int rightPos = rAction.find( ")", windowId.size() + 11 );
134 string layoutId = rAction.substr( windowId.size() + 11,
135 rightPos - (windowId.size() + 11) );
136 pCommand = new CmdLayout( getIntf(), windowId, layoutId );
138 else if( rAction.find( ".show()" ) != string::npos )
140 int leftPos = rAction.find( ".show()" );
141 string windowId = rAction.substr( 0, leftPos );
142 TopWindow *pWin = pTheme->getWindowById( windowId );
145 pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
150 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
153 else if( rAction.find( ".hide()" ) != string::npos )
155 int leftPos = rAction.find( ".hide()" );
156 string windowId = rAction.substr( 0, leftPos );
157 TopWindow *pWin = pTheme->getWindowById( windowId );
160 pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
165 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
171 // Add the command in the pool
172 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
179 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
181 VarManager *pVarManager = VarManager::instance( getIntf() );
183 // Convert the expression into Reverse Polish Notation
184 ExprEvaluator *pEvaluator = new ExprEvaluator( getIntf() );
185 pEvaluator->parse( rName );
187 list<VarBool*> varStack;
189 // Get the first token from the RPN stack
190 string token = pEvaluator->getToken();
191 while( !token.empty() )
195 // Get the 2 last variables on the stack
196 if( varStack.empty() )
198 msg_Err( getIntf(), "Invalid boolean expression: %s",
202 VarBool *pVar1 = varStack.back();
204 if( varStack.empty() )
206 msg_Err( getIntf(), "Invalid boolean expression: %s",
210 VarBool *pVar2 = varStack.back();
213 // Create a composite boolean variable
214 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
215 varStack.push_back( pNewVar );
216 // Register this variable in the manager
217 pVarManager->registerVar( VariablePtr( pNewVar ) );
219 else if( token == "or" )
221 // Get the 2 last variables on the stack
222 if( varStack.empty() )
224 msg_Err( getIntf(), "Invalid boolean expression: %s",
228 VarBool *pVar1 = varStack.back();
230 if( varStack.empty() )
232 msg_Err( getIntf(), "Invalid boolean expression: %s",
236 VarBool *pVar2 = varStack.back();
239 // Create a composite boolean variable
240 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
241 varStack.push_back( pNewVar );
242 // Register this variable in the manager
243 pVarManager->registerVar( VariablePtr( pNewVar ) );
245 else if( token == "not" )
247 // Get the last variable on the stack
248 if( varStack.empty() )
250 msg_Err( getIntf(), "Invalid boolean expression: %s",
254 VarBool *pVar = varStack.back();
257 // Create a composite boolean variable
258 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
259 varStack.push_back( pNewVar );
260 // Register this variable in the manager
261 pVarManager->registerVar( VariablePtr( pNewVar ) );
263 else if( token.find( ".isVisible" ) != string::npos )
265 int leftPos = token.find( ".isVisible" );
266 string windowId = token.substr( 0, leftPos );
267 TopWindow *pWin = pTheme->getWindowById( windowId );
270 // Push the visibility variable on the stack
271 varStack.push_back( &pWin->getVisibleVar() );
275 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
281 // Try to get the variable from the variable manager
282 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
285 msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
289 varStack.push_back( pVar );
291 // Get the first token from the RPN stack
292 token = pEvaluator->getToken();
295 // The stack should contain a single variable
296 if( varStack.size() != 1 )
298 msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
301 return varStack.back();
305 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
307 // Try to get the variable from the variable manager
308 VarManager *pVarManager = VarManager::instance( getIntf() );
309 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
314 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
316 // Try to get the variable from the variable manager
317 VarManager *pVarManager = VarManager::instance( getIntf() );
318 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );