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