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 REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
80 REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
81 m_commandMap["playlist.setRandom(true)"] =
82 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
83 m_commandMap["playlist.setRandom(false)"] =
84 CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
85 m_commandMap["playlist.setLoop(true)"] =
86 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
87 m_commandMap["playlist.setLoop(false)"] =
88 CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
89 m_commandMap["playlist.setRepeat(true)"] =
90 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), true ) );
91 m_commandMap["playlist.setRepeat(false)"] =
92 CmdGenericPtr( new CmdPlaylistRepeat( getIntf(), false ) );
93 VarTree &rVarTree = VlcProc::instance( getIntf() )->getPlaytreeVar();
94 m_commandMap["playtree.del()"] =
95 CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
96 REGISTER_CMD( "playtree.sort()", CmdPlaytreeSort )
97 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
98 REGISTER_CMD( "vlc.play()", CmdPlay )
99 REGISTER_CMD( "vlc.pause()", CmdPause )
100 REGISTER_CMD( "vlc.stop()", CmdStop )
101 REGISTER_CMD( "vlc.faster()", CmdFaster )
102 REGISTER_CMD( "vlc.slower()", CmdSlower )
103 REGISTER_CMD( "vlc.mute()", CmdMute )
104 REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
105 REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
106 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
107 REGISTER_CMD( "vlc.onTop()", CmdOnTop )
108 REGISTER_CMD( "vlc.snapshot()", CmdSnapshot )
109 REGISTER_CMD( "vlc.quit()", CmdQuit )
110 m_commandMap["equalizer.enable()"] =
111 CmdGenericPtr( new CmdSetEqualizer( getIntf(), true ) );
112 m_commandMap["equalizer.disable()"] =
113 CmdGenericPtr( new CmdSetEqualizer( getIntf(), false ) );
115 // Register the constant bool variables in the var manager
116 VarManager *pVarManager = VarManager::instance( getIntf() );
117 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
118 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
119 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
120 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
124 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
126 if( ! pIntf->p_sys->p_interpreter )
128 Interpreter *pInterpreter;
129 pInterpreter = new Interpreter( pIntf );
132 pIntf->p_sys->p_interpreter = pInterpreter;
135 return pIntf->p_sys->p_interpreter;
139 void Interpreter::destroy( intf_thread_t *pIntf )
141 if( pIntf->p_sys->p_interpreter )
143 delete pIntf->p_sys->p_interpreter;
144 pIntf->p_sys->p_interpreter = NULL;
149 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
151 // Try to find the command in the global command map
152 if( m_commandMap.find( rAction ) != m_commandMap.end() )
154 return m_commandMap[rAction].get();
157 CmdGeneric *pCommand = NULL;
159 if( rAction.find( ";" ) != string::npos )
161 // Several actions are defined...
162 list<CmdGeneric *> actionList;
163 string rightPart = rAction;
164 string::size_type pos = rightPart.find( ";" );
165 while( pos != string::npos )
167 string leftPart = rightPart.substr( 0, pos );
168 // Remove any whitespace at the end of the left part, and parse it
170 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
171 actionList.push_back( parseAction( leftPart, pTheme ) );
172 // Now remove any whitespace at the beginning of the right part,
173 // and go on checking for further actions in it...
174 rightPart = rightPart.substr( pos, rightPart.size() );
175 if ( rightPart.find_first_not_of( " \t;" ) == string::npos )
177 // The right part is completely buggy, it's time to leave the
184 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
186 pos = rightPart.find( ";" );
188 actionList.push_back( parseAction( rightPart, pTheme ) );
190 // The list is filled, we remove NULL pointers from it, just in case...
191 actionList.remove( NULL );
193 pCommand = new CmdMuxer( getIntf(), actionList );
195 else if( rAction.find( ".setLayout(" ) != string::npos )
197 int leftPos = rAction.find( ".setLayout(" );
198 string windowId = rAction.substr( 0, leftPos );
199 // 11 is the size of ".setLayout("
200 int rightPos = rAction.find( ")", windowId.size() + 11 );
201 string layoutId = rAction.substr( windowId.size() + 11,
202 rightPos - (windowId.size() + 11) );
204 TopWindow *pWin = pTheme->getWindowById( windowId );
205 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
208 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
212 msg_Err( getIntf(), "unknown layout (%s)", layoutId.c_str() );
214 // Check that the layout doesn't correspond to another window
215 else if( pWin != pLayout->getWindow() )
217 msg_Err( getIntf(), "layout %s is not associated to window %s",
218 layoutId.c_str(), windowId.c_str() );
222 pCommand = new CmdLayout( getIntf(), *pWin, *pLayout );
225 else if( rAction.find( ".show()" ) != string::npos )
227 int leftPos = rAction.find( ".show()" );
228 string windowId = rAction.substr( 0, leftPos );
230 if( windowId == "playlist_window" &&
231 !config_GetInt( getIntf(), "skinned-playlist") )
233 list<CmdGeneric *> list;
234 list.push_back( new CmdDlgPlaylist( getIntf() ) );
235 TopWindow *pWin = pTheme->getWindowById( windowId );
237 list.push_back( new CmdHideWindow( getIntf(),
238 pTheme->getWindowManager(),
240 pCommand = new CmdMuxer( getIntf(), list );
244 TopWindow *pWin = pTheme->getWindowById( windowId );
247 pCommand = new CmdShowWindow( getIntf(),
248 pTheme->getWindowManager(),
253 // It was maybe the id of a popup
254 Popup *pPopup = pTheme->getPopupById( windowId );
257 pCommand = new CmdShowPopup( getIntf(), *pPopup );
261 msg_Err( getIntf(), "unknown window or popup (%s)",
267 else if( rAction.find( ".hide()" ) != string::npos )
269 int leftPos = rAction.find( ".hide()" );
270 string windowId = rAction.substr( 0, leftPos );
271 if( windowId == "playlist_window" &&
272 ! config_GetInt( getIntf(), "skinned-playlist") )
274 list<CmdGeneric *> list;
275 list.push_back( new CmdDlgPlaylist( getIntf() ) );
276 TopWindow *pWin = pTheme->getWindowById( windowId );
278 list.push_back( new CmdHideWindow( getIntf(),
279 pTheme->getWindowManager(),
281 pCommand = new CmdMuxer( getIntf(), list );
285 TopWindow *pWin = pTheme->getWindowById( windowId );
288 pCommand = new CmdHideWindow( getIntf(),
289 pTheme->getWindowManager(),
294 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
301 // Add the command in the pool
302 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
306 msg_Warn( getIntf(), "unknown action: %s", rAction.c_str() );
313 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
315 VarManager *pVarManager = VarManager::instance( getIntf() );
317 // Convert the expression into Reverse Polish Notation
318 ExprEvaluator evaluator( getIntf() );
319 evaluator.parse( rName );
321 list<VarBool*> varStack;
323 // Get the first token from the RPN stack
324 string token = evaluator.getToken();
325 while( !token.empty() )
329 // Get the 2 last variables on the stack
330 if( varStack.empty() )
332 msg_Err( getIntf(), "invalid boolean expression: %s",
336 VarBool *pVar1 = varStack.back();
338 if( varStack.empty() )
340 msg_Err( getIntf(), "invalid boolean expression: %s",
344 VarBool *pVar2 = varStack.back();
347 // Create a composite boolean variable
348 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
349 varStack.push_back( pNewVar );
350 // Register this variable in the manager
351 pVarManager->registerVar( VariablePtr( pNewVar ) );
353 else if( token == "or" )
355 // Get the 2 last variables on the stack
356 if( varStack.empty() )
358 msg_Err( getIntf(), "invalid boolean expression: %s",
362 VarBool *pVar1 = varStack.back();
364 if( varStack.empty() )
366 msg_Err( getIntf(), "invalid boolean expression: %s",
370 VarBool *pVar2 = varStack.back();
373 // Create a composite boolean variable
374 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
375 varStack.push_back( pNewVar );
376 // Register this variable in the manager
377 pVarManager->registerVar( VariablePtr( pNewVar ) );
379 else if( token == "not" )
381 // Get the last variable on the stack
382 if( varStack.empty() )
384 msg_Err( getIntf(), "invalid boolean expression: %s",
388 VarBool *pVar = varStack.back();
391 // Create a composite boolean variable
392 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
393 varStack.push_back( pNewVar );
394 // Register this variable in the manager
395 pVarManager->registerVar( VariablePtr( pNewVar ) );
397 else if( token.find( ".isVisible" ) != string::npos )
399 int leftPos = token.find( ".isVisible" );
400 string windowId = token.substr( 0, leftPos );
401 TopWindow *pWin = pTheme->getWindowById( windowId );
404 // Push the visibility variable on the stack
405 varStack.push_back( &pWin->getVisibleVar() );
409 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
415 // Try to get the variable from the variable manager
416 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
419 msg_Err( getIntf(), "cannot resolve boolean variable: %s",
423 varStack.push_back( pVar );
425 // Get the first token from the RPN stack
426 token = evaluator.getToken();
429 // The stack should contain a single variable
430 if( varStack.size() != 1 )
432 msg_Err( getIntf(), "invalid boolean expression: %s", rName.c_str() );
435 return varStack.back();
439 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
441 // Try to get the variable from the variable manager
442 VarManager *pVarManager = VarManager::instance( getIntf() );
443 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
448 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
450 // Try to get the variable from the variable manager
451 VarManager *pVarManager = VarManager::instance( getIntf() );
452 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
457 VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
459 // Try to get the variable from the variable manager
460 VarManager *pVarManager = VarManager::instance( getIntf() );
461 VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
466 string Interpreter::getConstant( const string &rValue )
468 // Check if the value is a registered constant
469 string val = VarManager::instance( getIntf() )->getConst( rValue );
472 // if not, keep the value as is