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