]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
* modules/gui/skins2/src/skin_main.cpp: small cleanup to the skins loader demux.
[vlc] / modules / gui / skins2 / src / skin_main.cpp
1 /*****************************************************************************
2  * skin_main.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 <stdlib.h>
26 #include <vlc/input.h>
27 #include "dialogs.hpp"
28 #include "os_factory.hpp"
29 #include "os_loop.hpp"
30 #include "var_manager.hpp"
31 #include "vlcproc.hpp"
32 #include "theme_loader.hpp"
33 #include "theme.hpp"
34 #include "../parser/interpreter.hpp"
35 #include "../commands/async_queue.hpp"
36 #include "../commands/cmd_quit.hpp"
37
38
39 //---------------------------------------------------------------------------
40 // Exported interface functions.
41 //---------------------------------------------------------------------------
42 #ifdef WIN32_SKINS
43 extern "C" __declspec( dllexport )
44     int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
45 #endif
46
47
48 //---------------------------------------------------------------------------
49 // Local prototypes.
50 //---------------------------------------------------------------------------
51 static int  Open  ( vlc_object_t * );
52 static void Close ( vlc_object_t * );
53 static void Run   ( intf_thread_t * );
54
55 static int DemuxOpen( vlc_object_t * );
56 static int Demux( demux_t * );
57 static int DemuxControl( demux_t *, int, va_list );
58
59
60 //---------------------------------------------------------------------------
61 // Open: initialize interface
62 //---------------------------------------------------------------------------
63 static int Open( vlc_object_t *p_this )
64 {
65     intf_thread_t *p_intf = (intf_thread_t *)p_this;
66
67     // Allocate instance and initialize some members
68     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
69     if( p_intf->p_sys == NULL )
70     {
71         msg_Err( p_intf, "out of memory" );
72         return( VLC_ENOMEM );
73     };
74
75     p_intf->pf_run = Run;
76
77     // Suscribe to messages bank
78     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
79
80     p_intf->p_sys->p_input = NULL;
81     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
82         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
83     if( p_intf->p_sys->p_playlist == NULL )
84     {
85         msg_Err( p_intf, "No playlist object found" );
86         return VLC_EGENERIC;
87     }
88
89     // Initialize "singleton" objects
90     p_intf->p_sys->p_logger = NULL;
91     p_intf->p_sys->p_queue = NULL;
92     p_intf->p_sys->p_dialogs = NULL;
93     p_intf->p_sys->p_interpreter = NULL;
94     p_intf->p_sys->p_osFactory = NULL;
95     p_intf->p_sys->p_osLoop = NULL;
96     p_intf->p_sys->p_varManager = NULL;
97     p_intf->p_sys->p_vlcProc = NULL;
98
99     // No theme yet
100     p_intf->p_sys->p_theme = NULL;
101
102     // Create a variable to be notified of skins to be loaded
103     var_Create( p_intf, "skin-to-load", VLC_VAR_STRING );
104
105     // Initialize singletons
106     if( OSFactory::instance( p_intf ) == NULL )
107     {
108         msg_Err( p_intf, "Cannot initialize OSFactory" );
109         return VLC_EGENERIC;
110     }
111     if( AsyncQueue::instance( p_intf ) == NULL )
112     {
113         msg_Err( p_intf, "Cannot initialize AsyncQueue" );
114         return VLC_EGENERIC;
115     }
116     if( Interpreter::instance( p_intf ) == NULL )
117     {
118         msg_Err( p_intf, "Cannot instanciate Interpreter" );
119         return VLC_EGENERIC;
120     }
121     if( VarManager::instance( p_intf ) == NULL )
122     {
123         msg_Err( p_intf, "Cannot instanciate VarManager" );
124         return VLC_EGENERIC;
125     }
126     if( VlcProc::instance( p_intf ) == NULL )
127     {
128         msg_Err( p_intf, "Cannot initialize VLCProc" );
129         return VLC_EGENERIC;
130     }
131     Dialogs::instance( p_intf );
132
133     // We support play on start
134     p_intf->b_play = VLC_TRUE;
135
136     return( VLC_SUCCESS );
137 }
138
139 //---------------------------------------------------------------------------
140 // Close: destroy interface
141 //---------------------------------------------------------------------------
142 static void Close( vlc_object_t *p_this )
143 {
144     intf_thread_t *p_intf = (intf_thread_t *)p_this;
145
146     // Destroy "singleton" objects
147     OSFactory::instance( p_intf )->destroyOSLoop();
148     Dialogs::destroy( p_intf );
149     Interpreter::destroy( p_intf );
150     AsyncQueue::destroy( p_intf );
151     VarManager::destroy( p_intf );
152     VlcProc::destroy( p_intf );
153     OSFactory::destroy( p_intf );
154
155     if( p_intf->p_sys->p_playlist )
156     {
157         vlc_object_release( p_intf->p_sys->p_playlist );
158     }
159
160    // Unsubscribe from messages bank
161     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
162
163     // Destroy structure
164     free( p_intf->p_sys );
165 }
166
167
168 //---------------------------------------------------------------------------
169 // Run: main loop
170 //---------------------------------------------------------------------------
171 static void Run( intf_thread_t *p_intf )
172 {
173     // Load a theme
174     ThemeLoader *pLoader = new ThemeLoader( p_intf );
175     char *skin_last = config_GetPsz( p_intf, "skins2-last" );
176
177     if( !skin_last || !*skin_last || !pLoader->load( skin_last ) )
178     {
179         // Get the resource path and try to load the default skin
180         OSFactory *pOSFactory = OSFactory::instance( p_intf );
181         const list<string> &resPath = pOSFactory->getResourcePath();
182         const string &sep = pOSFactory->getDirSeparator();
183
184         list<string>::const_iterator it;
185         for( it = resPath.begin(); it != resPath.end(); it++ )
186         {
187             string path = (*it) + sep + "default" + sep + "theme.xml";
188             if( pLoader->load( path ) )
189             {
190                 // Theme loaded successfully
191                 break;
192             }
193         }
194         if( it == resPath.end() )
195         {
196             // Last chance: the user can select a new theme file
197             Dialogs *pDialogs = Dialogs::instance( p_intf );
198             if( pDialogs )
199             {
200                 pDialogs->showChangeSkin();
201             }
202             else
203             {
204                 // No dialogs provider, just quit...
205                 CmdQuit *pCmd = new CmdQuit( p_intf );
206                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
207                 pQueue->push( CmdGenericPtr( pCmd ) );
208                 msg_Err( p_intf,
209                          "Cannot show the \"open skin\" dialog: exiting...");
210             }
211         }
212     }
213     delete pLoader;
214
215     if( skin_last )
216     {
217         free( skin_last );
218     }
219
220     // Get the instance of OSLoop
221     OSLoop *loop = OSFactory::instance( p_intf )->getOSLoop();
222
223     // Check if we need to start playing
224     if( p_intf->b_play )
225     {
226         playlist_t *p_playlist =
227             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
228                                            FIND_ANYWHERE );
229         if( p_playlist )
230         {
231             playlist_Play( p_playlist );
232             vlc_object_release( p_playlist );
233         }
234     }
235
236     // Enter the main event loop
237     loop->run();
238
239     // Delete the theme and save the configuration of the windows
240     if( p_intf->p_sys->p_theme )
241     {
242         p_intf->p_sys->p_theme->saveConfig();
243         delete p_intf->p_sys->p_theme;
244         p_intf->p_sys->p_theme = NULL;
245     }
246 }
247
248
249 //---------------------------------------------------------------------------
250 // DemuxOpen: initialize demux
251 //---------------------------------------------------------------------------
252 static int DemuxOpen( vlc_object_t *p_this )
253 {
254     demux_t *p_demux = (demux_t*)p_this;
255     intf_thread_t *p_intf;
256     char *ext;
257
258     // Needed callbacks
259     p_demux->pf_demux   = Demux;
260     p_demux->pf_control = DemuxControl;
261
262     // Test that we have a valid .vlt file, based on the extension
263     // TODO: an actual check of the contents would be better...
264     if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
265         strcasecmp( ext, ".vlt" ) )
266     {
267         return VLC_EGENERIC;
268     }
269
270     p_intf = (intf_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INTF,
271                                                FIND_ANYWHERE );
272     if( p_intf != NULL )
273     {
274         // Do nothing is skins2 is not the main interface
275         if( var_Type( p_intf, "skin-to-load" ) != VLC_VAR_STRING )
276         {
277             playlist_t *p_playlist =
278                 (playlist_t *) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
279                                                 FIND_ANYWHERE );
280             if( p_playlist != NULL )
281             {
282                 // Make sure the item is deleted afterwards
283                 p_playlist->pp_items[p_playlist->i_index]->b_autodeletion =
284                     VLC_TRUE;
285                 vlc_object_release( p_playlist );
286             }
287
288             vlc_value_t val;
289             val.psz_string = p_demux->psz_path;
290             var_Set( p_intf, "skin-to-load", val );
291         }
292         else
293         {
294             msg_Warn( p_this,
295                       "skin could not be loaded (not using skins2 intf)" );
296         }
297
298         vlc_object_release( p_intf );
299     }
300
301     return VLC_SUCCESS;
302 }
303
304
305 //---------------------------------------------------------------------------
306 // Demux: return EOF
307 //---------------------------------------------------------------------------
308 static int Demux( demux_t *p_demux )
309 {
310     return 0;
311 }
312
313
314 //---------------------------------------------------------------------------
315 // DemuxControl
316 //---------------------------------------------------------------------------
317 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
318 {
319     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
320 }
321
322
323 //---------------------------------------------------------------------------
324 // Module descriptor
325 //---------------------------------------------------------------------------
326 #define SKINS2_LAST      N_("Last skin used")
327 #define SKINS2_LAST_LONG N_("Select the path to the last skin used.")
328 #define SKINS2_CONFIG      N_("Config of last used skin")
329 #define SKINS2_CONFIG_LONG N_("Config of last used skin.")
330 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
331 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
332     " if you want. This is mainly useful when moving windows does not behave" \
333     " correctly.")
334
335 vlc_module_begin();
336     add_string( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
337                 VLC_TRUE );
338     add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
339                 VLC_TRUE );
340 #ifdef WIN32
341     add_bool( "skins2-transparency", VLC_FALSE, NULL, SKINS2_TRANSPARENCY,
342               SKINS2_TRANSPARENCY_LONG, VLC_FALSE );
343 #endif
344     set_description( _("Skinnable Interface") );
345     set_capability( "interface", 30 );
346     set_callbacks( Open, Close );
347     set_program( "svlc" );
348
349     add_submodule();
350         set_description( _("Skins loader demux") );
351         set_capability( "demux2", 5 );
352         set_callbacks( DemuxOpen, NULL );
353
354 vlc_module_end();