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