1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
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_muxer.hpp"
28 #include "../commands/cmd_playlist.hpp"
29 #include "../commands/cmd_dialogs.hpp"
30 #include "../commands/cmd_dummy.hpp"
31 #include "../commands/cmd_layout.hpp"
32 #include "../commands/cmd_quit.hpp"
33 #include "../commands/cmd_minimize.hpp"
34 #include "../commands/cmd_input.hpp"
35 #include "../commands/cmd_fullscreen.hpp"
36 #include "../commands/cmd_on_top.hpp"
37 #include "../commands/cmd_show_window.hpp"
38 #include "../src/theme.hpp"
39 #include "../src/var_manager.hpp"
40 #include "../src/vlcproc.hpp"
43 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
45 /// Create the generic commands
46 #define REGISTER_CMD( name, cmd ) \
47 m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
49 REGISTER_CMD( "none", CmdDummy )
50 REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
51 REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
52 REGISTER_CMD( "dialogs.file()", CmdDlgFile )
53 REGISTER_CMD( "dialogs.directory()", CmdDlgDirectory )
54 REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
55 REGISTER_CMD( "dialogs.net()", CmdDlgNet )
56 REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
57 REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
58 REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
59 REGISTER_CMD( "dialogs.streamingWizard()", CmdDlgStreamingWizard )
60 REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
61 REGISTER_CMD( "playlist.load()", CmdDlgPlaylistLoad )
62 REGISTER_CMD( "playlist.save()", CmdDlgPlaylistSave )
63 REGISTER_CMD( "playlist.add()", CmdDlgAdd )
64 VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
65 m_commandMap["playlist.del()"] =
66 CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
67 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
68 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
69 REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
70 m_commandMap["playlist.setRandom(true)"] =
71 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
72 m_commandMap["playlist.setRandom(false)"] =
73 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
74 m_commandMap["playlist.setLoop(true)"] =
75 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
76 m_commandMap["playlist.setLoop(false)"] =
77 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
78 m_commandMap["playlist.setRepeat(true)"] =
79 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), true ) );
80 m_commandMap["playlist.setRepeat(false)"] =
81 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), false ) );
82 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
83 REGISTER_CMD( "vlc.play()", CmdPlay )
84 REGISTER_CMD( "vlc.pause()", CmdPause )
85 REGISTER_CMD( "vlc.stop()", CmdStop )
86 REGISTER_CMD( "vlc.faster()", CmdFaster )
87 REGISTER_CMD( "vlc.slower()", CmdSlower )
88 REGISTER_CMD( "vlc.mute()", CmdMute )
89 REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
90 REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
91 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
92 REGISTER_CMD( "vlc.onTop()", CmdOnTop )
93 REGISTER_CMD( "vlc.quit()", CmdQuit )
95 // Register the constant bool variables in the var manager
96 VarManager *pVarManager = VarManager::instance( getIntf() );
97 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
98 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
99 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
100 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
104 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
106 if( ! pIntf->p_sys->p_interpreter )
108 Interpreter *pInterpreter;
109 pInterpreter = new Interpreter( pIntf );
112 pIntf->p_sys->p_interpreter = pInterpreter;
115 return pIntf->p_sys->p_interpreter;
119 void Interpreter::destroy( intf_thread_t *pIntf )
121 if( pIntf->p_sys->p_interpreter )
123 delete pIntf->p_sys->p_interpreter;
124 pIntf->p_sys->p_interpreter = NULL;
129 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
131 // Try to find the command in the global command map
132 if( m_commandMap.find( rAction ) != m_commandMap.end() )
134 return m_commandMap[rAction].get();
137 CmdGeneric *pCommand = NULL;
139 if( rAction.find( ";" ) != string::npos )
141 // Several actions are defined...
142 list<CmdGeneric *> actionList;
143 string rightPart = rAction;
144 string::size_type pos = rightPart.find( ";" );
145 while( pos != string::npos )
147 string leftPart = rightPart.substr( 0, rightPart.find( ";" ) );
148 // Remove any whitespace at the end of the left part, and parse it
150 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
151 actionList.push_back( parseAction( leftPart, pTheme ) );
152 // Now remove any whitespace at the beginning of the right part,
153 // and go on checking for further actions in it...
154 rightPart = rightPart.substr( pos, rightPart.size() );
156 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
158 pos = rightPart.find( ";" );
160 actionList.push_back( parseAction( rightPart, pTheme ) );
162 // The list is filled, we remove NULL pointers from it, just in case...
163 actionList.remove( NULL );
165 pCommand = new CmdMuxer( getIntf(), actionList );
167 else if( rAction.find( ".setLayout(" ) != string::npos )
169 int leftPos = rAction.find( ".setLayout(" );
170 string windowId = rAction.substr( 0, leftPos );
171 // 11 is the size of ".setLayout("
172 int rightPos = rAction.find( ")", windowId.size() + 11 );
173 string layoutId = rAction.substr( windowId.size() + 11,
174 rightPos - (windowId.size() + 11) );
175 pCommand = new CmdLayout( getIntf(), windowId, layoutId );
177 else if( rAction.find( ".show()" ) != string::npos )
179 int leftPos = rAction.find( ".show()" );
180 string windowId = rAction.substr( 0, leftPos );
181 TopWindow *pWin = pTheme->getWindowById( windowId );
184 pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
189 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
192 else if( rAction.find( ".hide()" ) != string::npos )
194 int leftPos = rAction.find( ".hide()" );
195 string windowId = rAction.substr( 0, leftPos );
196 TopWindow *pWin = pTheme->getWindowById( windowId );
199 pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
204 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
210 // Add the command in the pool
211 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
215 msg_Warn( getIntf(), "Unknown action: %s", rAction.c_str() );
222 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
224 VarManager *pVarManager = VarManager::instance( getIntf() );
226 // Convert the expression into Reverse Polish Notation
227 ExprEvaluator evaluator( getIntf() );
228 evaluator.parse( rName );
230 list<VarBool*> varStack;
232 // Get the first token from the RPN stack
233 string token = evaluator.getToken();
234 while( !token.empty() )
238 // Get the 2 last variables on the stack
239 if( varStack.empty() )
241 msg_Err( getIntf(), "Invalid boolean expression: %s",
245 VarBool *pVar1 = varStack.back();
247 if( varStack.empty() )
249 msg_Err( getIntf(), "Invalid boolean expression: %s",
253 VarBool *pVar2 = varStack.back();
256 // Create a composite boolean variable
257 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
258 varStack.push_back( pNewVar );
259 // Register this variable in the manager
260 pVarManager->registerVar( VariablePtr( pNewVar ) );
262 else if( token == "or" )
264 // Get the 2 last variables on the stack
265 if( varStack.empty() )
267 msg_Err( getIntf(), "Invalid boolean expression: %s",
271 VarBool *pVar1 = varStack.back();
273 if( varStack.empty() )
275 msg_Err( getIntf(), "Invalid boolean expression: %s",
279 VarBool *pVar2 = varStack.back();
282 // Create a composite boolean variable
283 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
284 varStack.push_back( pNewVar );
285 // Register this variable in the manager
286 pVarManager->registerVar( VariablePtr( pNewVar ) );
288 else if( token == "not" )
290 // Get the last variable on the stack
291 if( varStack.empty() )
293 msg_Err( getIntf(), "Invalid boolean expression: %s",
297 VarBool *pVar = varStack.back();
300 // Create a composite boolean variable
301 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
302 varStack.push_back( pNewVar );
303 // Register this variable in the manager
304 pVarManager->registerVar( VariablePtr( pNewVar ) );
306 else if( token.find( ".isVisible" ) != string::npos )
308 int leftPos = token.find( ".isVisible" );
309 string windowId = token.substr( 0, leftPos );
310 TopWindow *pWin = pTheme->getWindowById( windowId );
313 // Push the visibility variable on the stack
314 varStack.push_back( &pWin->getVisibleVar() );
318 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
324 // Try to get the variable from the variable manager
325 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
328 msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
332 varStack.push_back( pVar );
334 // Get the first token from the RPN stack
335 token = evaluator.getToken();
338 // The stack should contain a single variable
339 if( varStack.size() != 1 )
341 msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
344 return varStack.back();
348 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
350 // Try to get the variable from the variable manager
351 VarManager *pVarManager = VarManager::instance( getIntf() );
352 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
357 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
359 // Try to get the variable from the variable manager
360 VarManager *pVarManager = VarManager::instance( getIntf() );
361 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );