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