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