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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "interpreter.hpp"
26 #include "expr_evaluator.hpp"
27 #include "../commands/cmd_audio.hpp"
28 #include "../commands/cmd_muxer.hpp"
29 #include "../commands/cmd_playlist.hpp"
30 #include "../commands/cmd_playtree.hpp"
31 #include "../commands/cmd_dialogs.hpp"
32 #include "../commands/cmd_dummy.hpp"
33 #include "../commands/cmd_dvd.hpp"
34 #include "../commands/cmd_layout.hpp"
35 #include "../commands/cmd_quit.hpp"
36 #include "../commands/cmd_minimize.hpp"
37 #include "../commands/cmd_input.hpp"
38 #include "../commands/cmd_fullscreen.hpp"
39 #include "../commands/cmd_on_top.hpp"
40 #include "../commands/cmd_show_window.hpp"
41 #include "../commands/cmd_snapshot.hpp"
42 #include "../src/theme.hpp"
43 #include "../src/var_manager.hpp"
44 #include "../src/vlcproc.hpp"
47 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
49 /// Create the generic commands
50 #define REGISTER_CMD( name, cmd ) \
51 m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
53 REGISTER_CMD( "none", CmdDummy )
54 REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
55 REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
56 REGISTER_CMD( "dialogs.file()", CmdDlgFile )
57 REGISTER_CMD( "dialogs.directory()", CmdDlgDirectory )
58 REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
59 REGISTER_CMD( "dialogs.net()", CmdDlgNet )
60 REGISTER_CMD( "dialogs.playlist()", CmdDlgPlaylist )
61 REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
62 REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
63 REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
64 REGISTER_CMD( "dialogs.streamingWizard()", CmdDlgStreamingWizard )
66 REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
67 REGISTER_CMD( "dialogs.audioPopup()", CmdDlgShowAudioPopupMenu )
68 REGISTER_CMD( "dialogs.videoPopup()", CmdDlgShowVideoPopupMenu )
69 REGISTER_CMD( "dialogs.miscPopup()", CmdDlgShowMiscPopupMenu )
71 REGISTER_CMD( "dvd.nextTitle()", CmdDvdNextTitle )
72 REGISTER_CMD( "dvd.previousTitle()", CmdDvdPreviousTitle )
73 REGISTER_CMD( "dvd.nextChapter()", CmdDvdNextChapter )
74 REGISTER_CMD( "dvd.previousChapter()", CmdDvdPreviousChapter )
75 REGISTER_CMD( "dvd.rootMenu()", CmdDvdRootMenu )
76 REGISTER_CMD( "playlist.load()", CmdDlgPlaylistLoad )
77 REGISTER_CMD( "playlist.save()", CmdDlgPlaylistSave )
78 REGISTER_CMD( "playlist.add()", CmdDlgAdd )
79 VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
80 m_commandMap["playlist.del()"] =
81 CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
82 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
83 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
84 REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
85 m_commandMap["playlist.setRandom(true)"] =
86 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
87 m_commandMap["playlist.setRandom(false)"] =
88 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
89 m_commandMap["playlist.setLoop(true)"] =
90 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
91 m_commandMap["playlist.setLoop(false)"] =
92 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
93 m_commandMap["playlist.setRepeat(true)"] =
94 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), true ) );
95 m_commandMap["playlist.setRepeat(false)"] =
96 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), false ) );
97 VarTree &rVarTree = VlcProc::instance( getIntf() )->getPlaytreeVar();
98 m_commandMap["playtree.del()"] =
99 CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
100 REGISTER_CMD( "playtree.sort()", CmdPlaytreeSort )
101 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
102 REGISTER_CMD( "vlc.play()", CmdPlay )
103 REGISTER_CMD( "vlc.pause()", CmdPause )
104 REGISTER_CMD( "vlc.stop()", CmdStop )
105 REGISTER_CMD( "vlc.faster()", CmdFaster )
106 REGISTER_CMD( "vlc.slower()", CmdSlower )
107 REGISTER_CMD( "vlc.mute()", CmdMute )
108 REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
109 REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
110 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
111 REGISTER_CMD( "vlc.onTop()", CmdOnTop )
112 REGISTER_CMD( "vlc.snapshot()", CmdSnapshot )
113 REGISTER_CMD( "vlc.quit()", CmdQuit )
114 m_commandMap["equalizer.enable()"] =
115 CmdGenericPtr( new CmdSetEqualizer( getIntf(), true ) );
116 m_commandMap["equalizer.disable()"] =
117 CmdGenericPtr( new CmdSetEqualizer( getIntf(), false ) );
119 // Register the constant bool variables in the var manager
120 VarManager *pVarManager = VarManager::instance( getIntf() );
121 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
122 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
123 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
124 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
128 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
130 if( ! pIntf->p_sys->p_interpreter )
132 Interpreter *pInterpreter;
133 pInterpreter = new Interpreter( pIntf );
136 pIntf->p_sys->p_interpreter = pInterpreter;
139 return pIntf->p_sys->p_interpreter;
143 void Interpreter::destroy( intf_thread_t *pIntf )
145 if( pIntf->p_sys->p_interpreter )
147 delete pIntf->p_sys->p_interpreter;
148 pIntf->p_sys->p_interpreter = NULL;
153 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
155 // Try to find the command in the global command map
156 if( m_commandMap.find( rAction ) != m_commandMap.end() )
158 return m_commandMap[rAction].get();
161 CmdGeneric *pCommand = NULL;
163 if( rAction.find( ";" ) != string::npos )
165 // Several actions are defined...
166 list<CmdGeneric *> actionList;
167 string rightPart = rAction;
168 string::size_type pos = rightPart.find( ";" );
169 while( pos != string::npos )
171 string leftPart = rightPart.substr( 0, pos );
172 // Remove any whitespace at the end of the left part, and parse it
174 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
175 actionList.push_back( parseAction( leftPart, pTheme ) );
176 // Now remove any whitespace at the beginning of the right part,
177 // and go on checking for further actions in it...
178 rightPart = rightPart.substr( pos, rightPart.size() );
179 if ( rightPart.find_first_not_of( " \t;" ) == string::npos )
181 // The right part is completely buggy, it's time to leave the
188 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
190 pos = rightPart.find( ";" );
192 actionList.push_back( parseAction( rightPart, pTheme ) );
194 // The list is filled, we remove NULL pointers from it, just in case...
195 actionList.remove( NULL );
197 pCommand = new CmdMuxer( getIntf(), actionList );
199 else if( rAction.find( ".setLayout(" ) != string::npos )
201 int leftPos = rAction.find( ".setLayout(" );
202 string windowId = rAction.substr( 0, leftPos );
203 // 11 is the size of ".setLayout("
204 int rightPos = rAction.find( ")", windowId.size() + 11 );
205 string layoutId = rAction.substr( windowId.size() + 11,
206 rightPos - (windowId.size() + 11) );
208 TopWindow *pWin = pTheme->getWindowById( windowId );
209 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
212 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
216 msg_Err( getIntf(), "unknown layout (%s)", layoutId.c_str() );
218 // Check that the layout doesn't correspond to another window
219 else if( pWin != pLayout->getWindow() )
221 msg_Err( getIntf(), "layout %s is not associated to window %s",
222 layoutId.c_str(), windowId.c_str() );
226 pCommand = new CmdLayout( getIntf(), *pWin, *pLayout );
229 else if( rAction.find( ".show()" ) != string::npos )
231 int leftPos = rAction.find( ".show()" );
232 string windowId = rAction.substr( 0, leftPos );
234 if( windowId == "playlist_window" &&
235 !config_GetInt( getIntf(), "skinned-playlist") )
237 list<CmdGeneric *> list;
238 list.push_back( new CmdDlgPlaylist( getIntf() ) );
239 TopWindow *pWin = pTheme->getWindowById( windowId );
241 list.push_back( new CmdHideWindow( getIntf(),
242 pTheme->getWindowManager(),
244 pCommand = new CmdMuxer( getIntf(), list );
248 TopWindow *pWin = pTheme->getWindowById( windowId );
251 pCommand = new CmdShowWindow( getIntf(),
252 pTheme->getWindowManager(),
257 // It was maybe the id of a popup
258 Popup *pPopup = pTheme->getPopupById( windowId );
261 pCommand = new CmdShowPopup( getIntf(), *pPopup );
265 msg_Err( getIntf(), "unknown window or popup (%s)",
271 else if( rAction.find( ".hide()" ) != string::npos )
273 int leftPos = rAction.find( ".hide()" );
274 string windowId = rAction.substr( 0, leftPos );
275 if( windowId == "playlist_window" &&
276 ! config_GetInt( getIntf(), "skinned-playlist") )
278 list<CmdGeneric *> list;
279 list.push_back( new CmdDlgPlaylist( getIntf() ) );
280 TopWindow *pWin = pTheme->getWindowById( windowId );
282 list.push_back( new CmdHideWindow( getIntf(),
283 pTheme->getWindowManager(),
285 pCommand = new CmdMuxer( getIntf(), list );
289 TopWindow *pWin = pTheme->getWindowById( windowId );
292 pCommand = new CmdHideWindow( getIntf(),
293 pTheme->getWindowManager(),
298 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
305 // Add the command in the pool
306 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
310 msg_Warn( getIntf(), "unknown action: %s", rAction.c_str() );
317 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
319 VarManager *pVarManager = VarManager::instance( getIntf() );
321 // Convert the expression into Reverse Polish Notation
322 ExprEvaluator evaluator( getIntf() );
323 evaluator.parse( rName );
325 list<VarBool*> varStack;
327 // Get the first token from the RPN stack
328 string token = evaluator.getToken();
329 while( !token.empty() )
333 // Get the 2 last variables on the stack
334 if( varStack.empty() )
336 msg_Err( getIntf(), "invalid boolean expression: %s",
340 VarBool *pVar1 = varStack.back();
342 if( varStack.empty() )
344 msg_Err( getIntf(), "invalid boolean expression: %s",
348 VarBool *pVar2 = varStack.back();
351 // Create a composite boolean variable
352 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
353 varStack.push_back( pNewVar );
354 // Register this variable in the manager
355 pVarManager->registerVar( VariablePtr( pNewVar ) );
357 else if( token == "or" )
359 // Get the 2 last variables on the stack
360 if( varStack.empty() )
362 msg_Err( getIntf(), "invalid boolean expression: %s",
366 VarBool *pVar1 = varStack.back();
368 if( varStack.empty() )
370 msg_Err( getIntf(), "invalid boolean expression: %s",
374 VarBool *pVar2 = varStack.back();
377 // Create a composite boolean variable
378 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
379 varStack.push_back( pNewVar );
380 // Register this variable in the manager
381 pVarManager->registerVar( VariablePtr( pNewVar ) );
383 else if( token == "not" )
385 // Get the last variable on the stack
386 if( varStack.empty() )
388 msg_Err( getIntf(), "invalid boolean expression: %s",
392 VarBool *pVar = varStack.back();
395 // Create a composite boolean variable
396 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
397 varStack.push_back( pNewVar );
398 // Register this variable in the manager
399 pVarManager->registerVar( VariablePtr( pNewVar ) );
401 else if( token.find( ".isVisible" ) != string::npos )
403 int leftPos = token.find( ".isVisible" );
404 string windowId = token.substr( 0, leftPos );
405 TopWindow *pWin = pTheme->getWindowById( windowId );
408 // Push the visibility variable on the stack
409 varStack.push_back( &pWin->getVisibleVar() );
413 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
419 // Try to get the variable from the variable manager
420 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
423 msg_Err( getIntf(), "cannot resolve boolean variable: %s",
427 varStack.push_back( pVar );
429 // Get the first token from the RPN stack
430 token = evaluator.getToken();
433 // The stack should contain a single variable
434 if( varStack.size() != 1 )
436 msg_Err( getIntf(), "invalid boolean expression: %s", rName.c_str() );
439 return varStack.back();
443 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
445 // Try to get the variable from the variable manager
446 VarManager *pVarManager = VarManager::instance( getIntf() );
447 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
452 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
454 // Try to get the variable from the variable manager
455 VarManager *pVarManager = VarManager::instance( getIntf() );
456 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
461 VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
463 // Try to get the variable from the variable manager
464 VarManager *pVarManager = VarManager::instance( getIntf() );
465 VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
470 string Interpreter::getConstant( const string &rValue )
472 // Check if the value is a registered constant
473 string val = VarManager::instance( getIntf() )->getConst( rValue );
476 // if not, keep the value as is