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