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