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