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["playlist.del()"] =
95 CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
96 m_commandMap["playtree.del()"] =
97 CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
98 REGISTER_CMD( "playlist.sort()", CmdPlaytreeSort )
99 REGISTER_CMD( "playtree.sort()", CmdPlaytreeSort )
100 REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
101 REGISTER_CMD( "vlc.play()", CmdPlay )
102 REGISTER_CMD( "vlc.pause()", CmdPause )
103 REGISTER_CMD( "vlc.stop()", CmdStop )
104 REGISTER_CMD( "vlc.faster()", CmdFaster )
105 REGISTER_CMD( "vlc.slower()", CmdSlower )
106 REGISTER_CMD( "vlc.mute()", CmdMute )
107 REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
108 REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
109 REGISTER_CMD( "vlc.minimize()", CmdMinimize )
110 REGISTER_CMD( "vlc.onTop()", CmdOnTop )
111 REGISTER_CMD( "vlc.snapshot()", CmdSnapshot )
112 REGISTER_CMD( "vlc.quit()", CmdQuit )
113 m_commandMap["equalizer.enable()"] =
114 CmdGenericPtr( new CmdSetEqualizer( getIntf(), true ) );
115 m_commandMap["equalizer.disable()"] =
116 CmdGenericPtr( new CmdSetEqualizer( getIntf(), false ) );
118 // Register the constant bool variables in the var manager
119 VarManager *pVarManager = VarManager::instance( getIntf() );
120 VarBool *pVarTrue = new VarBoolTrue( getIntf() );
121 pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
122 VarBool *pVarFalse = new VarBoolFalse( getIntf() );
123 pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
127 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
129 if( ! pIntf->p_sys->p_interpreter )
131 Interpreter *pInterpreter;
132 pInterpreter = new Interpreter( pIntf );
135 pIntf->p_sys->p_interpreter = pInterpreter;
138 return pIntf->p_sys->p_interpreter;
142 void Interpreter::destroy( intf_thread_t *pIntf )
144 if( pIntf->p_sys->p_interpreter )
146 delete pIntf->p_sys->p_interpreter;
147 pIntf->p_sys->p_interpreter = NULL;
152 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
154 // Try to find the command in the global command map
155 if( m_commandMap.find( rAction ) != m_commandMap.end() )
157 return m_commandMap[rAction].get();
160 CmdGeneric *pCommand = NULL;
162 if( rAction.find( ";" ) != string::npos )
164 // Several actions are defined...
165 list<CmdGeneric *> actionList;
166 string rightPart = rAction;
167 string::size_type pos = rightPart.find( ";" );
168 while( pos != string::npos )
170 string leftPart = rightPart.substr( 0, pos );
171 // Remove any whitespace at the end of the left part, and parse it
173 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
174 actionList.push_back( parseAction( leftPart, pTheme ) );
175 // Now remove any whitespace at the beginning of the right part,
176 // and go on checking for further actions in it...
177 rightPart = rightPart.substr( pos, rightPart.size() );
178 if ( rightPart.find_first_not_of( " \t;" ) == string::npos )
180 // The right part is completely buggy, it's time to leave the
187 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
189 pos = rightPart.find( ";" );
191 actionList.push_back( parseAction( rightPart, pTheme ) );
193 // The list is filled, we remove NULL pointers from it, just in case...
194 actionList.remove( NULL );
196 pCommand = new CmdMuxer( getIntf(), actionList );
198 else if( rAction.find( ".setLayout(" ) != string::npos )
200 int leftPos = rAction.find( ".setLayout(" );
201 string windowId = rAction.substr( 0, leftPos );
202 // 11 is the size of ".setLayout("
203 int rightPos = rAction.find( ")", windowId.size() + 11 );
204 string layoutId = rAction.substr( windowId.size() + 11,
205 rightPos - (windowId.size() + 11) );
207 TopWindow *pWin = pTheme->getWindowById( windowId );
208 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
211 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
215 msg_Err( getIntf(), "unknown layout (%s)", layoutId.c_str() );
217 // Check that the layout doesn't correspond to another window
218 else if( pWin != pLayout->getWindow() )
220 msg_Err( getIntf(), "layout %s is not associated to window %s",
221 layoutId.c_str(), windowId.c_str() );
225 pCommand = new CmdLayout( getIntf(), *pWin, *pLayout );
228 else if( rAction.find( ".maximize()" ) != string::npos )
230 int leftPos = rAction.find( ".maximize()" );
231 string windowId = rAction.substr( 0, leftPos );
233 TopWindow *pWin = pTheme->getWindowById( windowId );
236 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
240 pCommand = new CmdMaximize( getIntf(),
241 pTheme->getWindowManager(),
245 else if( rAction.find( ".unmaximize()" ) != string::npos )
247 int leftPos = rAction.find( ".unmaximize()" );
248 string windowId = rAction.substr( 0, leftPos );
250 TopWindow *pWin = pTheme->getWindowById( windowId );
253 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
257 pCommand = new CmdUnmaximize( getIntf(),
258 pTheme->getWindowManager(),
262 else if( rAction.find( ".show()" ) != string::npos )
264 int leftPos = rAction.find( ".show()" );
265 string windowId = rAction.substr( 0, leftPos );
267 if( windowId == "playlist_window" &&
268 !config_GetInt( getIntf(), "skinned-playlist") )
270 list<CmdGeneric *> list;
271 list.push_back( new CmdDlgPlaylist( getIntf() ) );
272 TopWindow *pWin = pTheme->getWindowById( windowId );
274 list.push_back( new CmdHideWindow( getIntf(),
275 pTheme->getWindowManager(),
277 pCommand = new CmdMuxer( getIntf(), list );
281 TopWindow *pWin = pTheme->getWindowById( windowId );
284 pCommand = new CmdShowWindow( getIntf(),
285 pTheme->getWindowManager(),
290 // It was maybe the id of a popup
291 Popup *pPopup = pTheme->getPopupById( windowId );
294 pCommand = new CmdShowPopup( getIntf(), *pPopup );
298 msg_Err( getIntf(), "unknown window or popup (%s)",
304 else if( rAction.find( ".hide()" ) != string::npos )
306 int leftPos = rAction.find( ".hide()" );
307 string windowId = rAction.substr( 0, leftPos );
308 if( windowId == "playlist_window" &&
309 ! config_GetInt( getIntf(), "skinned-playlist") )
311 list<CmdGeneric *> list;
312 list.push_back( new CmdDlgPlaylist( getIntf() ) );
313 TopWindow *pWin = pTheme->getWindowById( windowId );
315 list.push_back( new CmdHideWindow( getIntf(),
316 pTheme->getWindowManager(),
318 pCommand = new CmdMuxer( getIntf(), list );
322 TopWindow *pWin = pTheme->getWindowById( windowId );
325 pCommand = new CmdHideWindow( getIntf(),
326 pTheme->getWindowManager(),
331 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
338 // Add the command in the pool
339 pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
343 msg_Warn( getIntf(), "unknown action: %s", rAction.c_str() );
350 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
352 VarManager *pVarManager = VarManager::instance( getIntf() );
354 // Convert the expression into Reverse Polish Notation
355 ExprEvaluator evaluator( getIntf() );
356 evaluator.parse( rName );
358 list<VarBool*> varStack;
360 // Get the first token from the RPN stack
361 string token = evaluator.getToken();
362 while( !token.empty() )
366 // Get the 2 last variables on the stack
367 if( varStack.empty() )
369 msg_Err( getIntf(), "invalid boolean expression: %s",
373 VarBool *pVar1 = varStack.back();
375 if( varStack.empty() )
377 msg_Err( getIntf(), "invalid boolean expression: %s",
381 VarBool *pVar2 = varStack.back();
384 // Create a composite boolean variable
385 VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
386 varStack.push_back( pNewVar );
387 // Register this variable in the manager
388 pVarManager->registerVar( VariablePtr( pNewVar ) );
390 else if( token == "or" )
392 // Get the 2 last variables on the stack
393 if( varStack.empty() )
395 msg_Err( getIntf(), "invalid boolean expression: %s",
399 VarBool *pVar1 = varStack.back();
401 if( varStack.empty() )
403 msg_Err( getIntf(), "invalid boolean expression: %s",
407 VarBool *pVar2 = varStack.back();
410 // Create a composite boolean variable
411 VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
412 varStack.push_back( pNewVar );
413 // Register this variable in the manager
414 pVarManager->registerVar( VariablePtr( pNewVar ) );
416 else if( token == "not" )
418 // Get the last variable on the stack
419 if( varStack.empty() )
421 msg_Err( getIntf(), "invalid boolean expression: %s",
425 VarBool *pVar = varStack.back();
428 // Create a composite boolean variable
429 VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
430 varStack.push_back( pNewVar );
431 // Register this variable in the manager
432 pVarManager->registerVar( VariablePtr( pNewVar ) );
436 // Try first to get the variable from the variable manager
437 // Indeed, if the skin designer is stupid enough to call a layout
438 // "dvd", we want "dvd.isActive" to resolve as the built-in action
439 // and not as the "layoutId.isActive" one.
440 VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
443 varStack.push_back( pVar );
445 else if( token.find( ".isVisible" ) != string::npos )
447 int leftPos = token.find( ".isVisible" );
448 string windowId = token.substr( 0, leftPos );
449 TopWindow *pWin = pTheme->getWindowById( windowId );
452 // Push the visibility variable onto the stack
453 varStack.push_back( &pWin->getVisibleVar() );
457 msg_Err( getIntf(), "unknown window (%s)",
462 else if( token.find( ".isMaximized" ) != string::npos )
464 int leftPos = token.find( ".isMaximized" );
465 string windowId = token.substr( 0, leftPos );
466 TopWindow *pWin = pTheme->getWindowById( windowId );
469 // Push the "maximized" variable onto the stack
470 varStack.push_back( &pWin->getMaximizedVar() );
474 msg_Err( getIntf(), "unknown window (%s)",
479 else if( token.find( ".isActive" ) != string::npos )
481 int leftPos = token.find( ".isActive" );
482 string layoutId = token.substr( 0, leftPos );
483 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
486 // Push the isActive variable onto the stack
487 varStack.push_back( &pLayout->getActiveVar() );
491 msg_Err( getIntf(), "unknown layout (%s)",
498 msg_Err( getIntf(), "cannot resolve boolean variable: %s",
503 // Get the first token from the RPN stack
504 token = evaluator.getToken();
507 // The stack should contain a single variable
508 if( varStack.size() != 1 )
510 msg_Err( getIntf(), "invalid boolean expression: %s", rName.c_str() );
513 return varStack.back();
517 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
519 // Try to get the variable from the variable manager
520 VarManager *pVarManager = VarManager::instance( getIntf() );
521 VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
526 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
528 // Try to get the variable from the variable manager
529 VarManager *pVarManager = VarManager::instance( getIntf() );
530 VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
535 VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
537 // Try to get the variable from the variable manager
538 VarManager *pVarManager = VarManager::instance( getIntf() );
539 VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
544 string Interpreter::getConstant( const string &rValue )
546 // Check if the value is a registered constant
547 string val = VarManager::instance( getIntf() )->getConst( rValue );
550 // if not, keep the value as is