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