]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/interpreter.cpp
* parser/expr_evaluator: expression evaluator using Reverse Polish Notation
[vlc] / modules / gui / skins2 / parser / interpreter.cpp
1 /*****************************************************************************
2  * interpreter.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id$
6  *
7  * Authors: Cyril Deguet     <asmax@via.ecp.fr>
8  *          Olivier Teulière <ipkiss@via.ecp.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "interpreter.hpp"
26 #include "expr_evaluator.hpp"
27 #include "../commands/cmd_playlist.hpp"
28 #include "../commands/cmd_dialogs.hpp"
29 #include "../commands/cmd_dummy.hpp"
30 #include "../commands/cmd_layout.hpp"
31 #include "../commands/cmd_quit.hpp"
32 #include "../commands/cmd_input.hpp"
33 #include "../commands/cmd_fullscreen.hpp"
34 #include "../commands/cmd_show_window.hpp"
35 #include "../src/theme.hpp"
36 #include "../src/var_manager.hpp"
37 #include "../src/vlcproc.hpp"
38
39
40 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
41 {
42     /// Create the generic commands
43 #define REGISTER_CMD( name, cmd ) \
44     m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
45
46     REGISTER_CMD( "none", CmdDummy )
47     REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
48     REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
49     REGISTER_CMD( "dialogs.file()", CmdDlgFile )
50     REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
51     REGISTER_CMD( "dialogs.net()", CmdDlgNet )
52     REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
53     REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
54     REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
55     REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
56     REGISTER_CMD( "playlist.add()", CmdDlgAdd )
57     VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
58     m_commandMap["playlist.del()"] =
59         CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
60     REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
61     REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
62     REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
63     m_commandMap["playlist.setRandom(true)"] =
64         CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
65     m_commandMap["playlist.setRandom(false)"] =
66         CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
67     m_commandMap["playlist.setLoop(true)"] =
68         CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
69     m_commandMap["playlist.setLoop(false)"] =
70         CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
71     REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
72     REGISTER_CMD( "vlc.play()", CmdPlay )
73     REGISTER_CMD( "vlc.pause()", CmdPause )
74     REGISTER_CMD( "vlc.quit()", CmdQuit )
75     REGISTER_CMD( "vlc.faster()", CmdFaster )
76     REGISTER_CMD( "vlc.slower()", CmdSlower )
77     REGISTER_CMD( "vlc.stop()", CmdStop )
78
79     // Register the constant bool variables in the var manager
80     VarManager *pVarManager = VarManager::instance( getIntf() );
81     VarBool *pVarTrue = new VarBoolTrue( getIntf() );
82     pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
83     VarBool *pVarFalse = new VarBoolFalse( getIntf() );
84     pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
85 }
86
87
88 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
89 {
90     if( ! pIntf->p_sys->p_interpreter )
91     {
92         Interpreter *pInterpreter;
93         pInterpreter = new Interpreter( pIntf );
94         if( pInterpreter )
95         {
96             pIntf->p_sys->p_interpreter = pInterpreter;
97         }
98     }
99     return pIntf->p_sys->p_interpreter;
100 }
101
102
103 void Interpreter::destroy( intf_thread_t *pIntf )
104 {
105     if( pIntf->p_sys->p_interpreter )
106     {
107         delete pIntf->p_sys->p_interpreter;
108         pIntf->p_sys->p_interpreter = NULL;
109     }
110 }
111
112
113 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
114 {
115     // Try to find the command in the global command map
116     if( m_commandMap.find( rAction ) != m_commandMap.end() )
117     {
118         return m_commandMap[rAction].get();
119     }
120
121     CmdGeneric *pCommand = NULL;
122
123     if( rAction.find( ".setLayout(" ) != string::npos )
124     {
125         int leftPos = rAction.find( ".setLayout(" );
126         string windowId = rAction.substr( 0, leftPos );
127         // 11 is the size of ".setLayout("
128         int rightPos = rAction.find( ")", windowId.size() + 11 );
129         string layoutId = rAction.substr( windowId.size() + 11,
130                                           rightPos - (windowId.size() + 11) );
131         pCommand = new CmdLayout( getIntf(), windowId, layoutId );
132     }
133     else if( rAction.find( ".show()" ) != string::npos )
134     {
135         int leftPos = rAction.find( ".show()" );
136         string windowId = rAction.substr( 0, leftPos );
137         TopWindow *pWin = pTheme->getWindowById( windowId );
138         if( pWin )
139         {
140             pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
141                                           *pWin );
142         }
143         else
144         {
145             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
146         }
147     }
148     else if( rAction.find( ".hide()" ) != string::npos )
149     {
150         int leftPos = rAction.find( ".hide()" );
151         string windowId = rAction.substr( 0, leftPos );
152         TopWindow *pWin = pTheme->getWindowById( windowId );
153         if( pWin )
154         {
155             pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
156                                           *pWin );
157         }
158         else
159         {
160             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
161         }
162     }
163
164     if( pCommand )
165     {
166         // Add the command in the pool
167         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
168     }
169
170     return pCommand;
171 }
172
173
174 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
175 {
176     VarManager *pVarManager = VarManager::instance( getIntf() );
177
178    // Convert the expression into Reverse Polish Notation
179     ExprEvaluator *pEvaluator = new ExprEvaluator( getIntf() );
180     pEvaluator->parse( rName );
181
182     list<VarBool*> varStack;
183
184     // Get the first token from the RPN stack
185     string token = pEvaluator->getToken();
186     while( !token.empty() )
187     {
188         if( token == "and" )
189         {
190             // Get the 2 last variables on the stack
191             if( varStack.empty() )
192             {
193                 msg_Err( getIntf(), "Invalid boolean expression: %s",
194                          rName.c_str());
195                 return NULL;
196             }
197             VarBool *pVar1 = varStack.back();
198             varStack.pop_back();
199             if( varStack.empty() )
200             {
201                 msg_Err( getIntf(), "Invalid boolean expression: %s",
202                          rName.c_str());
203                 return NULL;
204             }
205             VarBool *pVar2 = varStack.back();
206             varStack.pop_back();
207
208             // Create a composite boolean variable
209             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
210             varStack.push_back( pNewVar );
211             // Register this variable in the manager
212             pVarManager->registerVar( VariablePtr( pNewVar ) );
213         }
214         else if( token == "not" )
215         {
216             // Get the last variable on the stack
217             if( varStack.empty() )
218             {
219                 msg_Err( getIntf(), "Invalid boolean expression: %s",
220                          rName.c_str());
221                 return NULL;
222             }
223             VarBool *pVar = varStack.back();
224             varStack.pop_back();
225
226             // Create a composite boolean variable
227             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
228             varStack.push_back( pNewVar );
229             // Register this variable in the manager
230             pVarManager->registerVar( VariablePtr( pNewVar ) );
231         }
232         else if( token.find( ".isVisible" ) != string::npos )
233         {
234             int leftPos = token.find( ".isVisible" );
235             string windowId = token.substr( 0, leftPos );
236             TopWindow *pWin = pTheme->getWindowById( windowId );
237             if( pWin )
238             {
239                 // Push the visibility variable on the stack
240                 varStack.push_back( &pWin->getVisibleVar() );
241             }
242             else
243             {
244                 msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
245                 return NULL;
246             }
247         }
248         else
249         {
250             // Try to get the variable from the variable manager
251             VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
252             if( !pVar )
253             {
254                 msg_Err( getIntf(), "Cannot resolve boolean variable: %s",
255                          token.c_str());
256                 return NULL;
257             }
258             varStack.push_back( pVar );
259         }
260         // Get the first token from the RPN stack
261         token = pEvaluator->getToken();
262     }
263
264     // The stack should contain a single variable
265     if( varStack.size() != 1 )
266     {
267         msg_Err( getIntf(), "Invalid boolean expression: %s", rName.c_str() );
268         return NULL;
269     }
270     return varStack.back();
271 }
272
273
274 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
275 {
276     // Try to get the variable from the variable manager
277     VarManager *pVarManager = VarManager::instance( getIntf() );
278     VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
279     return pVar;
280 }
281
282
283 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
284 {
285     // Try to get the variable from the variable manager
286     VarManager *pVarManager = VarManager::instance( getIntf() );
287     VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
288     return pVar;
289 }
290