]> git.sesse.net Git - vlc/blob - modules/gui/skins2/parser/interpreter.cpp
* parser/xmlparser.cpp: abort parsing when an error is detected
[vlc] / modules / gui / skins2 / parser / interpreter.cpp
1 /*****************************************************************************
2  * interpreter.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: interpreter.cpp,v 1.5 2004/02/01 14:44:11 asmax Exp $
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     REGISTER_CMD( "vlc.fullscreen()", CmdFullscreen )
63     REGISTER_CMD( "vlc.play()", CmdPlay )
64     REGISTER_CMD( "vlc.pause()", CmdPause )
65     REGISTER_CMD( "vlc.quit()", CmdQuit )
66     REGISTER_CMD( "vlc.faster()", CmdFaster )
67     REGISTER_CMD( "vlc.slower()", CmdSlower )
68     REGISTER_CMD( "vlc.stop()", CmdStop )
69 }
70
71
72 Interpreter *Interpreter::instance( intf_thread_t *pIntf )
73 {
74     if( ! pIntf->p_sys->p_interpreter )
75     {
76         Interpreter *pInterpreter;
77         pInterpreter = new Interpreter( pIntf );
78         if( pInterpreter )
79         {
80             pIntf->p_sys->p_interpreter = pInterpreter;
81         }
82     }
83     return pIntf->p_sys->p_interpreter;
84 }
85
86
87 void Interpreter::destroy( intf_thread_t *pIntf )
88 {
89     if( pIntf->p_sys->p_interpreter )
90     {
91         delete pIntf->p_sys->p_interpreter;
92         pIntf->p_sys->p_interpreter = NULL;
93     }
94 }
95
96
97 CmdGeneric *Interpreter::parseAction( const string &rAction, Theme *pTheme )
98 {
99     // Try to find the command in the global command map
100     if( m_commandMap.find( rAction ) != m_commandMap.end() )
101     {
102         return m_commandMap[rAction].get();
103     }
104
105     CmdGeneric *pCommand = NULL;
106
107     if( rAction.find( ".setLayout(" ) != string::npos )
108     {
109         int leftPos = rAction.find( ".setLayout(" );
110         string windowId = rAction.substr( 0, leftPos );
111         // 11 is the size of ".setLayout("
112         int rightPos = rAction.find( ")", windowId.size() + 11 );
113         string layoutId = rAction.substr( windowId.size() + 11,
114                                           rightPos - (windowId.size() + 11) );
115         pCommand = new CmdLayout( getIntf(), windowId, layoutId );
116     }
117     else if( rAction.find( ".show()" ) != string::npos )
118     {
119         int leftPos = rAction.find( ".show()" );
120         string windowId = rAction.substr( 0, leftPos );
121         GenericWindow *pWin = pTheme->getWindowById( windowId );
122         if( pWin )
123         {
124             pCommand = new CmdShowWindow( getIntf(), *pWin );
125         }
126         else
127         {
128             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
129         }
130     }
131     else if( rAction.find( ".hide()" ) != string::npos )
132     {
133         int leftPos = rAction.find( ".hide()" );
134         string windowId = rAction.substr( 0, leftPos );
135         GenericWindow *pWin = pTheme->getWindowById( windowId );
136         if( pWin )
137         {
138             pCommand = new CmdHideWindow( getIntf(), *pWin );
139         }
140         else
141         {
142             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
143         }
144     }
145
146     if( pCommand )
147     {
148         // Add the command in the pool
149         pTheme->m_commands.push_back( CmdGenericPtr( pCommand ) );
150     }
151
152     return pCommand;
153 }
154
155
156 VarBool *Interpreter::getVarBool( const string &rName, Theme *pTheme )
157 {
158     // Try to get the variable from the variable manager
159     VarManager *pVarManager = VarManager::instance( getIntf() );
160     VarBool *pVar = (VarBool*)pVarManager->getVar( rName, "bool" );
161
162     if( pVar )
163     {
164         return pVar;
165     }
166     else if( rName.find( " and " ) != string::npos )
167     {
168         int leftPos = rName.find( " and " );
169         string name1 = rName.substr( 0, leftPos );
170         int rightPos = leftPos + 5;   // 5 is the size of " and "
171         string name2 = rName.substr( rightPos, rName.size() - rightPos );
172         // Retrive the two boolean variables
173         VarBool *pVar1 = getVarBool( name1, pTheme );
174         VarBool *pVar2 = getVarBool( name2, pTheme );
175         // Create a composite boolean variable
176         if( pVar1 && pVar2 )
177         {
178             VarBool *pNewVar = new VarBoolAndBool( getIntf(), *pVar1, *pVar2 );
179             // Register this variable in the manager
180             pVarManager->registerVar( VariablePtr( pNewVar ), rName );
181             return pNewVar;
182         }
183         else
184         {
185             return NULL;
186         }
187     }
188     else if( rName.find( "not " ) != string::npos )
189     {
190         int rightPos = rName.find( "not " ) + 4;
191         string name = rName.substr( rightPos, rName.size() - rightPos );
192         // Retrive the boolean variable
193         VarBool *pVar = getVarBool( name, pTheme );
194         // Create a composite boolean variable
195         if( pVar )
196         {
197             VarBool *pNewVar = new VarNotBool( getIntf(), *pVar );
198             // Register this variable in the manager
199             pVarManager->registerVar( VariablePtr( pNewVar ), rName );
200             return pNewVar;
201         }
202         else
203         {
204             return NULL;
205         }
206     }
207     else if( rName.find( ".isVisible" ) != string::npos )
208     {
209         int leftPos = rName.find( ".isVisible" );
210         string windowId = rName.substr( 0, leftPos );
211         GenericWindow *pWin = pTheme->getWindowById( windowId );
212         if( pWin )
213         {
214             return &pWin->getVisibleVar();
215         }
216         else
217         {
218             msg_Err( getIntf(), "Unknown window (%s)", windowId.c_str() );
219             return NULL;
220         }
221     }
222     else
223     {
224         return NULL;
225     }
226 }
227
228
229 VarPercent *Interpreter::getVarPercent( const string &rName, Theme *pTheme )
230 {
231     // Try to get the variable from the variable manager
232     VarManager *pVarManager = VarManager::instance( getIntf() );
233     VarPercent *pVar = (VarPercent*)pVarManager->getVar( rName, "percent" );
234     return pVar;
235 }
236
237
238 VarList *Interpreter::getVarList( const string &rName, Theme *pTheme )
239 {
240     // Try to get the variable from the variable manager
241     VarManager *pVarManager = VarManager::instance( getIntf() );
242     VarList *pVar = (VarList*)pVarManager->getVar( rName, "list" );
243     return pVar;
244 }
245