]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/interpreter.cpp
419e78cc8c45daa9fbc46b26a87f927b39e4a1e6
[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 "../commands/cmd_playlist.hpp"
27 #include "../commands/cmd_dialogs.hpp"
28 #include "../commands/cmd_dummy.hpp"
29 #include "../commands/cmd_layout.hpp"
30 #include "../commands/cmd_quit.hpp"
31 #include "../commands/cmd_input.hpp"
32 #include "../commands/cmd_fullscreen.hpp"
33 #include "../commands/cmd_show_window.hpp"
34 #include "../src/theme.hpp"
35 #include "../src/var_manager.hpp"
36 #include "../src/vlcproc.hpp"
37
38
39 Interpreter::Interpreter( intf_thread_t *pIntf ): SkinObject( pIntf )
40 {
41     /// Create the generic commands
42 #define REGISTER_CMD( name, cmd ) \
43     m_commandMap[name] = CmdGenericPtr( new cmd( getIntf() ) );
44
45     REGISTER_CMD( "none", CmdDummy )
46     REGISTER_CMD( "dialogs.changeSkin()", CmdDlgChangeSkin )
47     REGISTER_CMD( "dialogs.fileSimple()", CmdDlgFileSimple )
48     REGISTER_CMD( "dialogs.file()", CmdDlgFile )
49     REGISTER_CMD( "dialogs.disc()", CmdDlgDisc )
50     REGISTER_CMD( "dialogs.net()", CmdDlgNet )
51     REGISTER_CMD( "dialogs.messages()", CmdDlgMessages )
52     REGISTER_CMD( "dialogs.prefs()", CmdDlgPrefs )
53     REGISTER_CMD( "dialogs.fileInfo()", CmdDlgFileInfo )
54     REGISTER_CMD( "dialogs.popup()", CmdDlgShowPopupMenu )
55     REGISTER_CMD( "playlist.add()", CmdDlgAdd )
56     VarList &rVar = VlcProc::instance( getIntf() )->getPlaylistVar();
57     m_commandMap["playlist.del()"] =
58         CmdGenericPtr( new CmdPlaylistDel( getIntf(), rVar ) );
59     REGISTER_CMD( "playlist.next()", CmdPlaylistNext )
60     REGISTER_CMD( "playlist.previous()", CmdPlaylistPrevious )
61     REGISTER_CMD( "playlist.sort()", CmdPlaylistSort )
62     m_commandMap["playlist.setRandom(true)"] =
63         CmdGenericPtr( new CmdPlaylistRandom( getIntf(), true ) );
64     m_commandMap["playlist.setRandom(false)"] =
65         CmdGenericPtr( new CmdPlaylistRandom( getIntf(), false ) );
66     m_commandMap["playlist.setLoop(true)"] =
67         CmdGenericPtr( new CmdPlaylistLoop( getIntf(), true ) );
68     m_commandMap["playlist.setLoop(false)"] =
69         CmdGenericPtr( new CmdPlaylistLoop( getIntf(), false ) );
70     REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
71     REGISTER_CMD( "vlc.play()", CmdPlay )
72     REGISTER_CMD( "vlc.pause()", CmdPause )
73     REGISTER_CMD( "vlc.quit()", CmdQuit )
74     REGISTER_CMD( "vlc.faster()", CmdFaster )
75     REGISTER_CMD( "vlc.slower()", CmdSlower )
76     REGISTER_CMD( "vlc.stop()", CmdStop )
77 }
78
79
80 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
81 {
82     if( ! pIntf->p_sys->p_interpreter )
83     {
84         Interpreter *pInterpreter;
85         pInterpreter = new Interpreter( pIntf );
86         if( pInterpreter )
87         {
88             pIntf->p_sys->p_interpreter = pInterpreter;
89         }
90     }
91     return pIntf->p_sys->p_interpreter;
92 }
93
94
95 void Interpreter::destroy( intf_thread_t *pIntf )
96 {
97     if( pIntf->p_sys->p_interpreter )
98     {
99         delete pIntf->p_sys->p_interpreter;
100         pIntf->p_sys->p_interpreter = NULL;
101     }
102 }
103
104
105 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
106 {
107     // Try to find the command in the global command map
108     if( m_commandMap.find( rAction ) != m_commandMap.end() )
109     {
110         return m_commandMap[rAction].get();
111     }
112
113     CmdGeneric *pCommand = NULL;
114
115     if( rAction.find( ".setLayout(" ) != string::npos )
116     {
117         int leftPos = rAction.find( ".setLayout(" );
118         string windowId = rAction.substr( 0, leftPos );
119         // 11 is the size of ".setLayout("
120         int rightPos = rAction.find( ")", windowId.size() + 11 );
121         string layoutId = rAction.substr( windowId.size() + 11,
122                                           rightPos - (windowId.size() + 11) );
123         pCommand = new CmdLayout( getIntf(), windowId, layoutId );
124     }
125     else if( rAction.find( ".show()" ) != string::npos )
126     {
127         int leftPos = rAction.find( ".show()" );
128         string windowId = rAction.substr( 0, leftPos );
129         TopWindow *pWin = pTheme->getWindowById( windowId );
130         if( pWin )
131         {
132             pCommand = new CmdShowWindow( getIntf(), pTheme->getWindowManager(),
133                                           *pWin );
134         }
135         else
136         {
137             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
138         }
139     }
140     else if( rAction.find( ".hide()" ) != string::npos )
141     {
142         int leftPos = rAction.find( ".hide()" );
143         string windowId = rAction.substr( 0, leftPos );
144         TopWindow *pWin = pTheme->getWindowById( windowId );
145         if( pWin )
146         {
147             pCommand = new CmdHideWindow( getIntf(), pTheme->getWindowManager(),
148                                           *pWin );
149         }
150         else
151         {
152             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
153         }
154     }
155
156     if( pCommand )
157     {
158         // Add the command in the pool
159         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
160     }
161
162     return pCommand;
163 }
164
165
166 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
167 {
168     // Try to get the variable from the variable manager
169     VarManager *pVarManager = VarManager::instance( getIntf() );
170     VarBool *pVar = (VarBool*)pVarManager->getVar( rName, "bool" );
171
172     if( pVar )
173     {
174         return pVar;
175     }
176     else if( rName.find( " and " ) != string::npos )
177     {
178         int leftPos = rName.find( " and " );
179         string name1 = rName.substr( 0, leftPos );
180         int rightPos = leftPos + 5;   // 5 is the size of " and "
181         string name2 = rName.substr( rightPos, rName.size() - rightPos );
182         // Retrive the two boolean variables
183         VarBool *pVar1 = getVarBool( name1, pTheme );
184         VarBool *pVar2 = getVarBool( name2, pTheme );
185         // Create a composite boolean variable
186         if( pVar1 && pVar2 )
187         {
188             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
189             // Register this variable in the manager
190             pVarManager->registerVar( VariablePtr( pNewVar ), rName );
191             return pNewVar;
192         }
193         else
194         {
195             return NULL;
196         }
197     }
198     else if( rName.find( "not " ) != string::npos )
199     {
200         int rightPos = rName.find( "not " ) + 4;
201         string name = rName.substr( rightPos, rName.size() - rightPos );
202         // Retrive the boolean variable
203         VarBool *pVar = getVarBool( name, pTheme );
204         // Create a composite boolean variable
205         if( pVar )
206         {
207             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
208             // Register this variable in the manager
209             pVarManager->registerVar( VariablePtr( pNewVar ), rName );
210             return pNewVar;
211         }
212         else
213         {
214             return NULL;
215         }
216     }
217     else if( rName.find( ".isVisible" ) != string::npos )
218     {
219         int leftPos = rName.find( ".isVisible" );
220         string windowId = rName.substr( 0, leftPos );
221         TopWindow *pWin = pTheme->getWindowById( windowId );
222         if( pWin )
223         {
224             return &pWin->getVisibleVar();
225         }
226         else
227         {
228             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
229             return NULL;
230         }
231     }
232     else
233     {
234         return NULL;
235     }
236 }
237
238
239 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
240 {
241     // Try to get the variable from the variable manager
242     VarManager *pVarManager = VarManager::instance( getIntf() );
243     VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
244     return pVar;
245 }
246
247
248 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
249 {
250     // Try to get the variable from the variable manager
251     VarManager *pVarManager = VarManager::instance( getIntf() );
252     VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
253     return pVar;
254 }
255