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