]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/interpreter.cpp
5f3ae2223c8a052468184be2a5b2a829ebc1858a
[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     if( pIntf->p_sys->p_interpreter )
145     {
146         delete pIntf->p_sys->p_interpreter;
147         pIntf->p_sys->p_interpreter = NULL;
148     }
149 }
150
151
152 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
153 {
154     // Try to find the command in the global command map
155     if( m_commandMap.find( rAction ) != m_commandMap.end() )
156     {
157         return m_commandMap[rAction].get();
158     }
159
160     CmdGeneric *pCommand = NULL;
161
162     if( rAction.find( ";" ) != string::npos )
163     {
164         // Several actions are defined...
165         list<CmdGeneric *> actionList;
166         string rightPart = rAction;
167         string::size_type pos = rightPart.find( ";" );
168         while( pos != string::npos )
169         {
170             string leftPart = rightPart.substr( 0, pos );
171             // Remove any whitespace at the end of the left part, and parse it
172             leftPart =
173                 leftPart.substr( 0, leftPart.find_last_not_of( " \t" ) + 1 );
174             actionList.push_back( parseAction( leftPart, pTheme ) );
175             // Now remove any whitespace at the beginning of the right part,
176             // and go on checking for further actions in it...
177             rightPart = rightPart.substr( pos, rightPart.size() );
178             if ( rightPart.find_first_not_of( " \t;" ) == string::npos )
179             {
180                 // The right part is completely buggy, it's time to leave the
181                 // loop...
182                 rightPart = "";
183                 break;
184             }
185
186             rightPart =
187                 rightPart.substr( rightPart.find_first_not_of( " \t;" ),
188                                   rightPart.size() );
189             pos = rightPart.find( ";" );
190         }
191         actionList.push_back( parseAction( rightPart, pTheme ) );
192
193         // The list is filled, we remove NULL pointers from it, just in case...
194         actionList.remove( NULL );
195
196         pCommand = new CmdMuxer( getIntf(), actionList );
197     }
198     else if( rAction.find( ".setLayout(" ) != string::npos )
199     {
200         int leftPos = rAction.find( ".setLayout(" );
201         string windowId = rAction.substr( 0, leftPos );
202         // 11 is the size of ".setLayout("
203         int rightPos = rAction.find( ")", windowId.size() + 11 );
204         string layoutId = rAction.substr( windowId.size() + 11,
205                                           rightPos - (windowId.size() + 11) );
206
207         TopWindow *pWin = pTheme->getWindowById( windowId );
208         GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
209         if( !pWin )
210         {
211             msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
212         }
213         else if( !pLayout )
214         {
215             msg_Err( getIntf(), "unknown layout (%s)", layoutId.c_str() );
216         }
217         // Check that the layout doesn't correspond to another window
218         else if( pWin != pLayout->getWindow() )
219         {
220             msg_Err( getIntf(), "layout %s is not associated to window %s",
221                      layoutId.c_str(), windowId.c_str() );
222         }
223         else
224         {
225             pCommand = new CmdLayout( getIntf(), *pWin, *pLayout );
226         }
227     }
228     else if( rAction.find( ".maximize()" ) != string::npos )
229     {
230         int leftPos = rAction.find( ".maximize()" );
231         string windowId = rAction.substr( 0, leftPos );
232
233         TopWindow *pWin = pTheme->getWindowById( windowId );
234         if( !pWin )
235         {
236             msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
237         }
238         else
239         {
240             pCommand = new CmdMaximize( getIntf(),
241                                         pTheme->getWindowManager(),
242                                         *pWin );
243         }
244     }
245     else if( rAction.find( ".unmaximize()" ) != string::npos )
246     {
247         int leftPos = rAction.find( ".unmaximize()" );
248         string windowId = rAction.substr( 0, leftPos );
249
250         TopWindow *pWin = pTheme->getWindowById( windowId );
251         if( !pWin )
252         {
253             msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
254         }
255         else
256         {
257             pCommand = new CmdUnmaximize( getIntf(),
258                                           pTheme->getWindowManager(),
259                                           *pWin );
260         }
261     }
262     else if( rAction.find( ".show()" ) != string::npos )
263     {
264         int leftPos = rAction.find( ".show()" );
265         string windowId = rAction.substr( 0, leftPos );
266
267         if( windowId == "playlist_window" &&
268             !config_GetInt( getIntf(), "skinned-playlist") )
269         {
270             list<CmdGeneric *> list;
271             list.push_back( new CmdDlgPlaylist( getIntf() ) );
272             TopWindow *pWin = pTheme->getWindowById( windowId );
273             if( pWin )
274                 list.push_back( new CmdHideWindow( getIntf(),
275                                                    pTheme->getWindowManager(),
276                                                    *pWin ) );
277             pCommand = new CmdMuxer( getIntf(), list );
278         }
279         else
280         {
281             TopWindow *pWin = pTheme->getWindowById( windowId );
282             if( pWin )
283             {
284                 pCommand = new CmdShowWindow( getIntf(),
285                                               pTheme->getWindowManager(),
286                                               *pWin );
287             }
288             else
289             {
290                 // It was maybe the id of a popup
291                 Popup *pPopup = pTheme->getPopupById( windowId );
292                 if( pPopup )
293                 {
294                     pCommand = new CmdShowPopup( getIntf(), *pPopup );
295                 }
296                 else
297                 {
298                     msg_Err( getIntf(), "unknown window or popup (%s)",
299                              windowId.c_str() );
300                 }
301             }
302         }
303     }
304     else if( rAction.find( ".hide()" ) != string::npos )
305     {
306         int leftPos = rAction.find( ".hide()" );
307         string windowId = rAction.substr( 0, leftPos );
308         if( windowId == "playlist_window" &&
309            ! config_GetInt( getIntf(), "skinned-playlist") )
310         {
311             list<CmdGeneric *> list;
312             list.push_back( new CmdDlgPlaylist( getIntf() ) );
313             TopWindow *pWin = pTheme->getWindowById( windowId );
314             if( pWin )
315                 list.push_back( new CmdHideWindow( getIntf(),
316                                                    pTheme->getWindowManager(),
317                                                    *pWin ) );
318             pCommand = new CmdMuxer( getIntf(), list );
319         }
320         else
321         {
322             TopWindow *pWin = pTheme->getWindowById( windowId );
323             if( pWin )
324             {
325                 pCommand = new CmdHideWindow( getIntf(),
326                                               pTheme->getWindowManager(),
327                                               *pWin );
328             }
329             else
330             {
331                 msg_Err( getIntf(), "unknown window (%s)", windowId.c_str() );
332             }
333         }
334     }
335
336     if( pCommand )
337     {
338         // Add the command in the pool
339         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
340     }
341     else
342     {
343         msg_Warn( getIntf(), "unknown action: %s", rAction.c_str() );
344     }
345
346     return pCommand;
347 }
348
349
350 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
351 {
352     VarManager *pVarManager = VarManager::instance( getIntf() );
353
354     // Convert the expression into Reverse Polish Notation
355     ExprEvaluator evaluator( getIntf() );
356     evaluator.parse( rName );
357
358     list<VarBool*> varStack;
359
360     // Get the first token from the RPN stack
361     string token = evaluator.getToken();
362     while( !token.empty() )
363     {
364         if( token == "and" )
365         {
366             // Get the 2 last variables on the stack
367             if( varStack.empty() )
368             {
369                 msg_Err( getIntf(), "invalid boolean expression: %s",
370                          rName.c_str());
371                 return NULL;
372             }
373             VarBool *pVar1 = varStack.back();
374             varStack.pop_back();
375             if( varStack.empty() )
376             {
377                 msg_Err( getIntf(), "invalid boolean expression: %s",
378                          rName.c_str());
379                 return NULL;
380             }
381             VarBool *pVar2 = varStack.back();
382             varStack.pop_back();
383
384             // Create a composite boolean variable
385             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
386             varStack.push_back( pNewVar );
387             // Register this variable in the manager
388             pVarManager->registerVar( VariablePtr( pNewVar ) );
389         }
390         else if( token == "or" )
391         {
392             // Get the 2 last variables on the stack
393             if( varStack.empty() )
394             {
395                 msg_Err( getIntf(), "invalid boolean expression: %s",
396                          rName.c_str());
397                 return NULL;
398             }
399             VarBool *pVar1 = varStack.back();
400             varStack.pop_back();
401             if( varStack.empty() )
402             {
403                 msg_Err( getIntf(), "invalid boolean expression: %s",
404                          rName.c_str());
405                 return NULL;
406             }
407             VarBool *pVar2 = varStack.back();
408             varStack.pop_back();
409
410             // Create a composite boolean variable
411             VarBool *pNewVar = new VarBoolOrBool( getIntf(), *pVar1, *pVar2 );
412             varStack.push_back( pNewVar );
413             // Register this variable in the manager
414             pVarManager->registerVar( VariablePtr( pNewVar ) );
415         }
416         else if( token == "not" )
417         {
418             // Get the last variable on the stack
419             if( varStack.empty() )
420             {
421                 msg_Err( getIntf(), "invalid boolean expression: %s",
422                          rName.c_str());
423                 return NULL;
424             }
425             VarBool *pVar = varStack.back();
426             varStack.pop_back();
427
428             // Create a composite boolean variable
429             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
430             varStack.push_back( pNewVar );
431             // Register this variable in the manager
432             pVarManager->registerVar( VariablePtr( pNewVar ) );
433         }
434         else
435         {
436             // Try first to get the variable from the variable manager
437             // Indeed, if the skin designer is stupid enough to call a layout
438             // "dvd", we want "dvd.isActive" to resolve as the built-in action
439             // and not as the "layoutId.isActive" one.
440             VarBool *pVar = (VarBool*)pVarManager->getVar( token, "bool" );
441             if( pVar )
442             {
443                 varStack.push_back( pVar );
444             }
445             else if( token.find( ".isVisible" ) != string::npos )
446             {
447                 int leftPos = token.find( ".isVisible" );
448                 string windowId = token.substr( 0, leftPos );
449                 TopWindow *pWin = pTheme->getWindowById( windowId );
450                 if( pWin )
451                 {
452                     // Push the visibility variable onto the stack
453                     varStack.push_back( &pWin->getVisibleVar() );
454                 }
455                 else
456                 {
457                     msg_Err( getIntf(), "unknown window (%s)",
458                              windowId.c_str() );
459                     return NULL;
460                 }
461             }
462             else if( token.find( ".isMaximized" ) != string::npos )
463             {
464                 int leftPos = token.find( ".isMaximized" );
465                 string windowId = token.substr( 0, leftPos );
466                 TopWindow *pWin = pTheme->getWindowById( windowId );
467                 if( pWin )
468                 {
469                     // Push the "maximized" variable onto the stack
470                     varStack.push_back( &pWin->getMaximizedVar() );
471                 }
472                 else
473                 {
474                     msg_Err( getIntf(), "unknown window (%s)",
475                              windowId.c_str() );
476                     return NULL;
477                 }
478             }
479             else if( token.find( ".isActive" ) != string::npos )
480             {
481                 int leftPos = token.find( ".isActive" );
482                 string layoutId = token.substr( 0, leftPos );
483                 GenericLayout *pLayout = pTheme->getLayoutById( layoutId );
484                 if( pLayout )
485                 {
486                     // Push the isActive variable onto the stack
487                     varStack.push_back( &pLayout->getActiveVar() );
488                 }
489                 else
490                 {
491                     msg_Err( getIntf(), "unknown layout (%s)",
492                              layoutId.c_str() );
493                     return NULL;
494                 }
495             }
496             else
497             {
498                 msg_Err( getIntf(), "cannot resolve boolean variable: %s",
499                          token.c_str());
500                 return NULL;
501             }
502         }
503         // Get the first token from the RPN stack
504         token = evaluator.getToken();
505     }
506
507     // The stack should contain a single variable
508     if( varStack.size() != 1 )
509     {
510         msg_Err( getIntf(), "invalid boolean expression: %s", rName.c_str() );
511         return NULL;
512     }
513     return varStack.back();
514 }
515
516
517 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
518 {
519     // Try to get the variable from the variable manager
520     VarManager *pVarManager = VarManager::instance( getIntf() );
521     VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
522     return pVar;
523 }
524
525
526 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
527 {
528     // Try to get the variable from the variable manager
529     VarManager *pVarManager = VarManager::instance( getIntf() );
530     VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
531     return pVar;
532 }
533
534
535 VarTree *Interpreter::getVarTree( const string &rName, Theme *pTheme )
536 {
537     // Try to get the variable from the variable manager
538     VarManager *pVarManager = VarManager::instance( getIntf() );
539     VarTree *pVar = (VarTree*)pVarManager->getVar( rName, "tree" );
540     return pVar;
541 }
542
543
544 string Interpreter::getConstant( const string &rValue )
545 {
546     // Check if the value is a registered constant
547     string val = VarManager::instance( getIntf() )->getConst( rValue );
548     if( val.empty() )
549     {
550         // if not, keep the value as is
551         val = rValue;
552     }
553     return val;
554 }
555