]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/interpreter.cpp
cosmetic: remove nullity test on free() and delete
[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["playlist.del()"] =
95         CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
96     m_commandMap["playtree.del()"] =
97         CmdGenericPtr( new CmdPlaytreeDel( getIntf(), rVarTree ) );
98     REGISTER_CMD( "playlist.sort()", CmdPlaytreeSort )
99     REGISTER_CMD( "playtree.sort()", CmdPlaytreeSort )
100     REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
101     REGISTER_CMD( "vlc.play()", CmdPlay )
102     REGISTER_CMD( "vlc.pause()", CmdPause )
103     REGISTER_CMD( "vlc.stop()", CmdStop )
104     REGISTER_CMD( "vlc.faster()", CmdFaster )
105     REGISTER_CMD( "vlc.slower()", CmdSlower )
106     REGISTER_CMD( "vlc.mute()", CmdMute )
107     REGISTER_CMD( "vlc.volumeUp()", CmdVolumeUp )
108     REGISTER_CMD( "vlc.volumeDown()", CmdVolumeDown )
109     REGISTER_CMD( "vlc.minimize()", CmdMinimize )
110     REGISTER_CMD( "vlc.onTop()", CmdOnTop )
111     REGISTER_CMD( "vlc.snapshot()", CmdSnapshot )
112     REGISTER_CMD( "vlc.quit()", CmdQuit )
113     m_commandMap["equalizer.enable()"] =
114         CmdGenericPtr( new CmdSetEqualizer( getIntf(), true ) );
115     m_commandMap["equalizer.disable()"] =
116         CmdGenericPtr( new CmdSetEqualizer( getIntf(), false ) );
117
118     // Register the constant bool variables in the var manager
119     VarManager *pVarManager = VarManager::instance( getIntf() );
120     VarBool *pVarTrue = new VarBoolTrue( getIntf() );
121     pVarManager->registerVar( VariablePtr( pVarTrue ), "true" );
122     VarBool *pVarFalse = new VarBoolFalse( getIntf() );
123     pVarManager->registerVar( VariablePtr( pVarFalse ), "false" );
124 }
125
126
127 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
128 {
129     if( ! pIntf->p_sys->p_interpreter )
130     {
131         Interpreter *pInterpreter;
132         pInterpreter = new Interpreter( pIntf );
133         if( pInterpreter )
134         {
135             pIntf->p_sys->p_interpreter = pInterpreter;
136         }
137     }
138     return pIntf->p_sys->p_interpreter;
139 }
140
141
142 void Interpreter::destroy( intf_thread_t *pIntf )
143 {
144     delete pIntf->p_sys->p_interpreter;
145     pIntf->p_sys->p_interpreter = NULL;
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( ".maximize()" ) != string::npos )
226     {
227         int leftPos = rAction.find( ".maximize()" );
228         string windowId = rAction.substr( 0, leftPos );
229
230         TopWindow *pWin = pTheme->getWindowById( windowId );
231         if( !pWin )
232         {
233             msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
234         }
235         else
236         {
237             pCommand = new CmdMaximize( getIntf(),
238                                         pTheme->getWindowManager(),
239                                         *pWin );
240         }
241     }
242     else if( rAction.find( ".unmaximize()" ) != string::npos )
243     {
244         int leftPos = rAction.find( ".unmaximize()" );
245         string windowId = rAction.substr( 0, leftPos );
246
247         TopWindow *pWin = pTheme->getWindowById( windowId );
248         if( !pWin )
249         {
250             msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
251         }
252         else
253         {
254             pCommand = new CmdUnmaximize( getIntf(),
255                                           pTheme->getWindowManager(),
256                                           *pWin );
257         }
258     }
259     else if( rAction.find( ".show()" ) != string::npos )
260     {
261         int leftPos = rAction.find( ".show()" );
262         string windowId = rAction.substr( 0, leftPos );
263
264         if( windowId == "playlist_window" &&
265             !config_GetInt( getIntf(), "skinned-playlist") )
266         {
267             list<CmdGeneric *> list;
268             list.push_back( new CmdDlgPlaylist( getIntf() ) );
269             TopWindow *pWin = pTheme->getWindowById( windowId );
270             if( pWin )
271                 list.push_back( new CmdHideWindow( getIntf(),
272                                                    pTheme->getWindowManager(),
273                                                    *pWin ) );
274             pCommand = new CmdMuxer( getIntf(), list );
275         }
276         else
277         {
278             TopWindow *pWin = pTheme->getWindowById( windowId );
279             if( pWin )
280             {
281                 pCommand = new CmdShowWindow( getIntf(),
282                                               pTheme->getWindowManager(),
283                                               *pWin );
284             }
285             else
286             {
287                 // It was maybe the id of a popup
288                 Popup *pPopup = pTheme->getPopupById( windowId );
289                 if( pPopup )
290                 {
291                     pCommand = new CmdShowPopup( getIntf(), *pPopup );
292                 }
293                 else
294                 {
295                     msg_Err( getIntf(), "unknown window or popup (%s)",
296                              windowId.c_str() );
297                 }
298             }
299         }
300     }
301     else if( rAction.find( ".hide()" ) != string::npos )
302     {
303         int leftPos = rAction.find( ".hide()" );
304         string windowId = rAction.substr( 0, leftPos );
305         if( windowId == "playlist_window" &&
306            ! config_GetInt( getIntf(), "skinned-playlist") )
307         {
308             list<CmdGeneric *> list;
309             list.push_back( new CmdDlgPlaylist( getIntf() ) );
310             TopWindow *pWin = pTheme->getWindowById( windowId );
311             if( pWin )
312                 list.push_back( new CmdHideWindow( getIntf(),
313                                                    pTheme->getWindowManager(),
314                                                    *pWin ) );
315             pCommand = new CmdMuxer( getIntf(), list );
316         }
317         else
318         {
319             TopWindow *pWin = pTheme->getWindowById( windowId );
320             if( pWin )
321             {
322                 pCommand = new CmdHideWindow( getIntf(),
323                                               pTheme->getWindowManager(),
324                                               *pWin );
325             }
326             else
327             {
328                 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
329             }
330         }
331     }
332
333     if( pCommand )
334     {
335         // Add the command in the pool
336         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
337     }
338     else
339     {
340         msg_Warn( getIntf(), "unknown action: %s", rAction.c_str() );
341     }
342
343     return pCommand;
344 }
345
346
347 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
348 {
349     VarManager *pVarManager = VarManager::instance( getIntf() );
350
351     // Convert the expression into Reverse Polish Notation
352     ExprEvaluator evaluator( getIntf() );
353     evaluator.parse( rName );
354
355     list<VarBool*> varStack;
356
357     // Get the first token from the RPN stack
358     string token = evaluator.getToken();
359     while( !token.empty() )
360     {
361         if( token == "and" )
362         {
363             // Get the 2 last variables on the stack
364             if( varStack.empty() )
365             {
366                 msg_Err( getIntf(), "invalid boolean expression: %s",
367                          rName.c_str());
368                 return NULL;
369             }
370             VarBool *pVar1 = varStack.back();
371             varStack.pop_back();
372             if( varStack.empty() )
373             {
374                 msg_Err( getIntf(), "invalid boolean expression: %s",
375                          rName.c_str());
376                 return NULL;
377             }
378             VarBool *pVar2 = varStack.back();
379             varStack.pop_back();
380
381             // Create a composite boolean variable
382             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
383             varStack.push_back( pNewVar );
384             // Register this variable in the manager
385             pVarManager->registerVar( VariablePtr( pNewVar ) );
386         }
387         else if( token == "or" )
388         {
389             // Get the 2 last variables on the stack
390             if( varStack.empty() )
391             {
392                 msg_Err( getIntf(), "invalid boolean expression: %s",
393                          rName.c_str());
394                 return NULL;
395             }
396             VarBool *pVar1 = varStack.back();
397             varStack.pop_back();
398             if( varStack.empty() )
399             {
400                 msg_Err( getIntf(), "invalid boolean expression: %s",
401                          rName.c_str());
402                 return NULL;
403             }
404             VarBool *pVar2 = varStack.back();
405             varStack.pop_back();
406
407             // Create a composite boolean variable
408             VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
409             varStack.push_back( pNewVar );
410             // Register this variable in the manager
411             pVarManager->registerVar( VariablePtr( pNewVar ) );
412         }
413         else if( token == "not" )
414         {
415             // Get the last variable on the stack
416             if( varStack.empty() )
417             {
418                 msg_Err( getIntf(), "invalid boolean expression: %s",
419                          rName.c_str());
420                 return NULL;
421             }
422             VarBool *pVar = varStack.back();
423             varStack.pop_back();
424
425             // Create a composite boolean variable
426             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
427             varStack.push_back( pNewVar );
428             // Register this variable in the manager
429             pVarManager->registerVar( VariablePtr( pNewVar ) );
430         }
431         else
432         {
433             // Try first to get the variable from the variable manager
434             // Indeed, if the skin designer is stupid enough to call a layout
435             // "dvd", we want "dvd.isActive" to resolve as the built-in action
436             // and not as the "layoutId.isActive" one.
437             VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
438             if( pVar )
439             {
440                 varStack.push_back( pVar );
441             }
442             else if( token.find( ".isVisible" ) != string::npos )
443             {
444                 int leftPos = token.find( ".isVisible" );
445                 string windowId = token.substr( 0, leftPos );
446                 TopWindow *pWin = pTheme->getWindowById( windowId );
447                 if( pWin )
448                 {
449                     // Push the visibility variable onto the stack
450                     varStack.push_back( &pWin->getVisibleVar() );
451                 }
452                 else
453                 {
454                     msg_Err( getIntf(), "unknown window (%s)",
455                              windowId.c_str() );
456                     return NULL;
457                 }
458             }
459             else if( token.find( ".isMaximized" ) != string::npos )
460             {
461                 int leftPos = token.find( ".isMaximized" );
462                 string windowId = token.substr( 0, leftPos );
463                 TopWindow *pWin = pTheme->getWindowById( windowId );
464                 if( pWin )
465                 {
466                     // Push the "maximized" variable onto the stack
467                     varStack.push_back( &pWin->getMaximizedVar() );
468                 }
469                 else
470                 {
471                     msg_Err( getIntf(), "unknown window (%s)",
472                              windowId.c_str() );
473                     return NULL;
474                 }
475             }
476             else if( token.find( ".isActive" ) != string::npos )
477             {
478                 int leftPos = token.find( ".isActive" );
479                 string layoutId = token.substr( 0, leftPos );
480                 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
481                 if( pLayout )
482                 {
483                     // Push the isActive variable onto the stack
484                     varStack.push_back( &pLayout->getActiveVar() );
485                 }
486                 else
487                 {
488                     msg_Err( getIntf(), "unknown layout (%s)",
489                              layoutId.c_str() );
490                     return NULL;
491                 }
492             }
493             else
494             {
495                 msg_Err( getIntf(), "cannot resolve boolean variable: %s",
496                          token.c_str());
497                 return NULL;
498             }
499         }
500         // Get the first token from the RPN stack
501         token = evaluator.getToken();
502     }
503
504     // The stack should contain a single variable
505     if( varStack.size() != 1 )
506     {
507         msg_Err( getIntf(), "invalid boolean expression: %s", rName.c_str() );
508         return NULL;
509     }
510     return varStack.back();
511 }
512
513
514 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
515 {
516     // Try to get the variable from the variable manager
517     VarManager *pVarManager = VarManager::instance( getIntf() );
518     VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
519     return pVar;
520 }
521
522
523 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
524 {
525     // Try to get the variable from the variable manager
526     VarManager *pVarManager = VarManager::instance( getIntf() );
527     VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
528     return pVar;
529 }
530
531
532 VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
533 {
534     // Try to get the variable from the variable manager
535     VarManager *pVarManager = VarManager::instance( getIntf() );
536     VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
537     return pVar;
538 }
539
540
541 string Interpreter::getConstant( const string &rValue )
542 {
543     // Check if the value is a registered constant
544     string val = VarManager::instance( getIntf() )->getConst( rValue );
545     if( val.empty() )
546     {
547         // if not, keep the value as is
548         val = rValue;
549     }
550     return val;
551 }
552