]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
478b61b50aa41e24cf5bacfefca46e800f66bfb6
[vlc] / modules / gui / skins2 / src / skin_main.cpp
1 /*****************************************************************************
2  * skin_main.cpp
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: skin_main.cpp,v 1.10 2004/03/01 19:58:16 gbazin 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 <stdlib.h>
26 #include "dialogs.hpp"
27 #include "os_factory.hpp"
28 #include "os_loop.hpp"
29 #include "var_manager.hpp"
30 #include "vlcproc.hpp"
31 #include "theme_loader.hpp"
32 #include "theme.hpp"
33 #include "../parser/interpreter.hpp"
34 #include "../commands/async_queue.hpp"
35 #include "../commands/cmd_quit.hpp"
36
37
38 //---------------------------------------------------------------------------
39 // Exported interface functions.
40 //---------------------------------------------------------------------------
41 #ifdef WIN32_SKINS
42 extern "C" __declspec( dllexport )
43     int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
44 #endif
45
46
47 //---------------------------------------------------------------------------
48 // Local prototypes.
49 //---------------------------------------------------------------------------
50 static int  Open  ( vlc_object_t * );
51 static void Close ( vlc_object_t * );
52 static void Run   ( intf_thread_t * );
53
54
55 //---------------------------------------------------------------------------
56 // Open: initialize interface
57 //---------------------------------------------------------------------------
58 static int Open( vlc_object_t *p_this )
59 {
60     intf_thread_t *p_intf = (intf_thread_t *)p_this;
61
62     // Allocate instance and initialize some members
63     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
64     if( p_intf->p_sys == NULL )
65     {
66         msg_Err( p_intf, "out of memory" );
67         return( VLC_ENOMEM );
68     };
69
70     p_intf->pf_run = Run;
71
72     // Suscribe to messages bank
73     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
74
75     p_intf->p_sys->p_input = NULL;
76     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
77         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
78     if( p_intf->p_sys->p_playlist == NULL )
79     {
80         msg_Err( p_intf, "No playlist object found" );
81         return VLC_EGENERIC;
82     }
83
84     // Initialize "singleton" objects
85     p_intf->p_sys->p_logger = NULL;
86     p_intf->p_sys->p_queue = NULL;
87     p_intf->p_sys->p_dialogs = NULL;
88     p_intf->p_sys->p_interpreter = NULL;
89     p_intf->p_sys->p_osFactory = NULL;
90     p_intf->p_sys->p_osLoop = NULL;
91     p_intf->p_sys->p_varManager = NULL;
92     p_intf->p_sys->p_vlcProc = NULL;
93
94     // No theme yet
95     p_intf->p_sys->p_theme = NULL;
96
97     // Initialize singletons
98     if( OSFactory::instance( p_intf ) == NULL )
99     {
100         msg_Err( p_intf, "Cannot initialize OSFactory" );
101         return VLC_EGENERIC;
102     }
103     if( AsyncQueue::instance( p_intf ) == NULL )
104     {
105         msg_Err( p_intf, "Cannot initialize AsyncQueue" );
106         return VLC_EGENERIC;
107     }
108     if( Interpreter::instance( p_intf ) == NULL )
109     {
110         msg_Err( p_intf, "Cannot instanciate Interpreter" );
111         return VLC_EGENERIC;
112     }
113     if( VarManager::instance( p_intf ) == NULL )
114     {
115         msg_Err( p_intf, "Cannot instanciate VarManager" );
116         return VLC_EGENERIC;
117     }
118     if( VlcProc::instance( p_intf ) == NULL )
119     {
120         msg_Err( p_intf, "Cannot initialize VLCProc" );
121         return VLC_EGENERIC;
122     }
123     Dialogs::instance( p_intf );
124
125     return( VLC_SUCCESS );
126 }
127
128 //---------------------------------------------------------------------------
129 // Close: destroy interface
130 //---------------------------------------------------------------------------
131 static void Close( vlc_object_t *p_this )
132 {
133     intf_thread_t *p_intf = (intf_thread_t *)p_this;
134
135     // Destroy "singleton" objects
136     OSFactory::instance( p_intf )->destroyOSLoop();
137     Dialogs::destroy( p_intf );
138     Interpreter::destroy( p_intf );
139     AsyncQueue::destroy( p_intf );
140     VarManager::destroy( p_intf );
141     VlcProc::destroy( p_intf );
142     OSFactory::destroy( p_intf );
143
144     if( p_intf->p_sys->p_playlist )
145     {
146         vlc_object_release( p_intf->p_sys->p_playlist );
147     }
148
149    // Unsuscribe to messages bank
150     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
151
152     // Destroy structure
153     free( p_intf->p_sys );
154 }
155
156
157 //---------------------------------------------------------------------------
158 // Run: main loop
159 //---------------------------------------------------------------------------
160 static void Run( intf_thread_t *p_intf )
161 {
162     // Load a theme
163     ThemeLoader *pLoader = new ThemeLoader( p_intf );
164     char *skin_last = config_GetPsz( p_intf, "skins2-last" );
165
166     if( skin_last == NULL || !pLoader->load( skin_last ) )
167     {
168         // Too bad, it failed. Let's try with the default theme
169 #ifdef WIN32_SKINS
170         string default_dir = (string)p_intf->p_libvlc->psz_vlcpath +
171                              "\\skins2\\default\\theme.xml";
172         if( !pLoader->load( default_dir ) )
173 #else
174         string user_skin = (string)p_intf->p_vlc->psz_homedir +
175                            "/" + CONFIG_DIR + "/skins2/default/theme.xml";
176
177         string default_skin = (string)DATA_PATH + "/skins2/default/theme.xml";
178         if( !pLoader->load( user_skin ) && !pLoader->load( default_skin ) )
179 #endif
180         {
181             // Last chance: the user can select a new theme file (blocking call)
182             Dialogs *pDialogs = Dialogs::instance( p_intf );
183             if( pDialogs )
184             {
185                 pDialogs->showChangeSkin();
186             }
187             else
188             {
189                 // No dialogs provider, just quit...
190                 CmdQuit *pCmd = new CmdQuit( p_intf );
191                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
192                 pQueue->push( CmdGenericPtr( pCmd ) );
193                 msg_Err( p_intf, "Cannot show the \"open skin\" dialog: exiting...");
194             }
195         }
196     }
197     delete pLoader;
198
199     if( skin_last )
200     {
201         free( skin_last );
202     }
203
204     // Get the instance of OSLoop
205     OSLoop *loop = OSFactory::instance( p_intf )->getOSLoop();
206
207     // Enter the main event loop
208     loop->run();
209
210     // Delete the theme
211     if( p_intf->p_sys->p_theme )
212     {
213         delete p_intf->p_sys->p_theme;
214         p_intf->p_sys->p_theme = NULL;
215     }
216 }
217
218 //---------------------------------------------------------------------------
219 // Module descriptor
220 //---------------------------------------------------------------------------
221 #define DEFAULT_SKIN        N_("Last skin used")
222 #define DEFAULT_SKIN_LONG   N_("Select the path to the last skin used.")
223 #define SKIN_CONFIG         N_("Config of last used skin")
224 #define SKIN_CONFIG_LONG    N_("Config of last used skin.")
225
226 vlc_module_begin();
227     add_string( "skins2-last", "", NULL, DEFAULT_SKIN, DEFAULT_SKIN_LONG,
228                 VLC_TRUE );
229     add_string( "skins2-config", "", NULL, SKIN_CONFIG, SKIN_CONFIG_LONG,
230                 VLC_TRUE );
231     set_description( _("Skinnable Interface") );
232     set_capability( "interface", 30 );
233     set_callbacks( Open, Close );
234     set_program( "s2vlc" );
235 vlc_module_end();