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