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.disc()", CmdDlgDisc )
54 REGISTER_CMD( "dialogs.net()", CmdDlgNet )
55 REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
56 REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
57 REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
58 REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
59 REGISTER_CMD( "playlist.load()", CmdDlgPlaylistLoad )
60 REGISTER_CMD( "playlist.save()", CmdDlgPlaylistSave )
61 REGISTER_CMD( "playlist.add()", CmdDlgAdd )
62 VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
63 m_commandMap["playlist.del()"] =
64 CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
65 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
66 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
67 REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
68 m_commandMap["playlist.setRandom(true)"] =
69 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
70 m_commandMap["playlist.setRandom(false)"] =
71 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
72 m_commandMap["playlist.setLoop(true)"] =
73 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
74 m_commandMap["playlist.setLoop(false)"] =
75 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
76 m_commandMap["playlist.setRepeat(true)"] =
77 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), true ) );
78 m_commandMap["playlist.setRepeat(false)"] =
79 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), false ) );
80 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
81 REGISTER_CMD( "vlc.play()", CmdPlay )
82 REGISTER_CMD( "vlc.pause()", CmdPause )
83 REGISTER_CMD( "vlc.stop()", CmdStop )
84 REGISTER_CMD( "vlc.faster()", CmdFaster )
85 REGISTER_CMD( "vlc.slower()", CmdSlower )
86 REGISTER_CMD( "vlc.mute()", CmdMute )
87 REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
88 REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
89 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
90 REGISTER_CMD( "vlc.onTop()", CmdOnTop )
91 REGISTER_CMD( "vlc.quit()", CmdQuit )
93 // Register the constant bool variables in the var manager
94 VarManager *pVarManager = VarManager::instance( getIntf() );
95 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
96 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
97 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
98 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
102 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
104 if( ! pIntf->p_sys->p_interpreter )
106 Interpreter *pInterpreter;
107 pInterpreter = new Interpreter( pIntf );
110 pIntf->p_sys->p_interpreter = pInterpreter;
113 return pIntf->p_sys->p_interpreter;
117 void Interpreter::destroy( intf_thread_t *pIntf )
119 if( pIntf->p_sys->p_interpreter )
121 delete pIntf->p_sys->p_interpreter;
122 pIntf->p_sys->p_interpreter = NULL;
127 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
129 // Try to find the command in the global command map
130 if( m_commandMap.find( rAction ) != m_commandMap.end() )
132 return m_commandMap[rAction].get();
135 CmdGeneric *pCommand = NULL;
137 if( rAction.find( ";" ) != string::npos )
139 // Several actions are defined...
140 list<CmdGeneric *> actionList;
141 string rightPart = rAction;
142 string::size_type pos = rightPart.find( ";" );
143 while( pos != string::npos )
145 string leftPart = rightPart.substr( 0, rightPart.find( ";" ) );
146 // Remove any whitespace at the end of the left part, and parse it
148 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
149 actionList.push_back( parseAction( leftPart, pTheme ) );
150 // Now remove any whitespace at the beginning of the right part,
151 // and go on checking for further actions in it...
152 rightPart = rightPart.substr( pos, rightPart.size() );
154 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
156 pos = rightPart.find( ";" );
158 actionList.push_back( parseAction( rightPart, pTheme ) );
160 // The list is filled, we remove NULL pointers from it, just in case...
161 actionList.remove( NULL );
163 pCommand = new CmdMuxer( getIntf(), actionList );
165 else if( rAction.find( ".setLayout(" ) != string::npos )
167 int leftPos = rAction.find( ".setLayout(" );
168 string windowId = rAction.substr( 0, leftPos );
169 // 11 is the size of ".setLayout("
170 int rightPos = rAction.find( ")", windowId.size() + 11 );
171 string layoutId = rAction.substr( windowId.size() + 11,
172 rightPos - (windowId.size() + 11) );
173 pCommand = new CmdLayout( getIntf(), windowId, layoutId );
175 else if( rAction.find( ".show()" ) != string::npos )
177 int leftPos = rAction.find( ".show()" );
178 string windowId = rAction.substr( 0, leftPos );
179 TopWindow *pWin = pTheme->getWindowById( windowId );
182 pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
187 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
190 else if( rAction.find( ".hide()" ) != string::npos )
192 int leftPos = rAction.find( ".hide()" );
193 string windowId = rAction.substr( 0, leftPos );
194 TopWindow *pWin = pTheme->getWindowById( windowId );
197 pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
202 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
208 // Add the command in the pool
209 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
213 msg_Warn( getIntf(), "Unknown action: %s", rAction.c_str() );
220 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
222 VarManager *pVarManager = VarManager::instance( getIntf() );
224 // Convert the expression into Reverse Polish Notation
225 ExprEvaluator evaluator( getIntf() );
226 evaluator.parse( rName );
228 list<VarBool*> varStack;
230 // Get the first token from the RPN stack
231 string token = evaluator.getToken();
232 while( !token.empty() )
236 // Get the 2 last variables on the stack
237 if( varStack.empty() )
239 msg_Err( getIntf(), "Invalid boolean expression: %s",
243 VarBool *pVar1 = varStack.back();
245 if( varStack.empty() )
247 msg_Err( getIntf(), "Invalid boolean expression: %s",
251 VarBool *pVar2 = varStack.back();
254 // Create a composite boolean variable
255 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
256 varStack.push_back( pNewVar );
257 // Register this variable in the manager
258 pVarManager->registerVar( VariablePtr( pNewVar ) );
260 else if( token == "or" )
262 // Get the 2 last variables on the stack
263 if( varStack.empty() )
265 msg_Err( getIntf(), "Invalid boolean expression: %s",
269 VarBool *pVar1 = varStack.back();
271 if( varStack.empty() )
273 msg_Err( getIntf(), "Invalid boolean expression: %s",
277 VarBool *pVar2 = varStack.back();
280 // Create a composite boolean variable
281 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
282 varStack.push_back( pNewVar );
283 // Register this variable in the manager
284 pVarManager->registerVar( VariablePtr( pNewVar ) );
286 else if( token == "not" )
288 // Get the last variable on the stack
289 if( varStack.empty() )
291 msg_Err( getIntf(), "Invalid boolean expression: %s",
295 VarBool *pVar = varStack.back();
298 // Create a composite boolean variable
299 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
300 varStack.push_back( pNewVar );
301 // Register this variable in the manager
302 pVarManager->registerVar( VariablePtr( pNewVar ) );
304 else if( token.find( ".isVisible" ) != string::npos )
306 int leftPos = token.find( ".isVisible" );
307 string windowId = token.substr( 0, leftPos );
308 TopWindow *pWin = pTheme->getWindowById( windowId );
311 // Push the visibility variable on the stack
312 varStack.push_back( &pWin->getVisibleVar() );
316 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
322 // Try to get the variable from the variable manager
323 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
326 msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
330 varStack.push_back( pVar );
332 // Get the first token from the RPN stack
333 token = evaluator.getToken();
336 // The stack should contain a single variable
337 if( varStack.size() != 1 )
339 msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
342 return varStack.back();
346 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
348 // Try to get the variable from the variable manager
349 VarManager *pVarManager = VarManager::instance( getIntf() );
350 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
355 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
357 // Try to get the variable from the variable manager
358 VarManager *pVarManager = VarManager::instance( getIntf() );
359 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );