]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
1ce2dd938c0cbf54a649e3e1a3d39a0a1ef1dba6
[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 #include <stdlib.h>
25
26 #include <vlc/vlc.h>
27 #include <vlc_input.h>
28 #include <vlc_demux.h>
29 #include <vlc_playlist.h>
30
31 #include "dialogs.hpp"
32 #include "os_factory.hpp"
33 #include "os_loop.hpp"
34 #include "var_manager.hpp"
35 #include "vlcproc.hpp"
36 #include "theme_loader.hpp"
37 #include "theme.hpp"
38 #include "theme_repository.hpp"
39 #include "../parser/interpreter.hpp"
40 #include "../commands/async_queue.hpp"
41 #include "../commands/cmd_quit.hpp"
42 #include "../commands/cmd_dialogs.hpp"
43 #include "../commands/cmd_minimize.hpp"
44
45 //---------------------------------------------------------------------------
46 // Exported interface functions.
47 //---------------------------------------------------------------------------
48 #ifdef WIN32_SKINS
49 extern "C" __declspec( dllexport )
50     int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
51 #endif
52
53
54 //---------------------------------------------------------------------------
55 // Local prototypes
56 //---------------------------------------------------------------------------
57 static int  Open  ( vlc_object_t * );
58 static void Close ( vlc_object_t * );
59 static void Run   ( intf_thread_t * );
60
61 static int DemuxOpen( vlc_object_t * );
62 static int Demux( demux_t * );
63 static int DemuxControl( demux_t *, int, va_list );
64
65 //---------------------------------------------------------------------------
66 // Prototypes for configuration callbacks
67 //---------------------------------------------------------------------------
68 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
69                             vlc_value_t oldVal, vlc_value_t newVal,
70                             void *pParam );
71 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
72                             vlc_value_t oldVal, vlc_value_t newVal,
73                             void *pParam );
74
75
76 //---------------------------------------------------------------------------
77 // Open: initialize interface
78 //---------------------------------------------------------------------------
79 static int Open( vlc_object_t *p_this )
80 {
81     intf_thread_t *p_intf = (intf_thread_t *)p_this;
82
83     // Allocate instance and initialize some members
84     p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
85     if( p_intf->p_sys == NULL )
86     {
87         msg_Err( p_intf, "out of memory" );
88         return( VLC_ENOMEM );
89     };
90
91     p_intf->pf_run = Run;
92
93     // Suscribe to messages bank
94     p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
95
96     p_intf->p_sys->p_input = NULL;
97     p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
98         VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
99     if( p_intf->p_sys->p_playlist == NULL )
100     {
101         msg_Err( p_intf, "No playlist object found" );
102         msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
103         return VLC_EGENERIC;
104     }
105
106     // Initialize "singleton" objects
107     p_intf->p_sys->p_logger = NULL;
108     p_intf->p_sys->p_queue = NULL;
109     p_intf->p_sys->p_dialogs = NULL;
110     p_intf->p_sys->p_interpreter = NULL;
111     p_intf->p_sys->p_osFactory = NULL;
112     p_intf->p_sys->p_osLoop = NULL;
113     p_intf->p_sys->p_varManager = NULL;
114     p_intf->p_sys->p_vlcProc = NULL;
115     p_intf->p_sys->p_repository = NULL;
116
117     // No theme yet
118     p_intf->p_sys->p_theme = NULL;
119
120     // Create a variable to be notified of skins to be loaded
121     var_Create( p_intf, "skin-to-load", VLC_VAR_STRING );
122
123     // Initialize singletons
124     if( OSFactory::instance( p_intf ) == NULL )
125     {
126         msg_Err( p_intf, "cannot initialize OSFactory" );
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( AsyncQueue::instance( p_intf ) == NULL )
132     {
133         msg_Err( p_intf, "cannot initialize AsyncQueue" );
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( Interpreter::instance( p_intf ) == NULL )
139     {
140         msg_Err( p_intf, "cannot instanciate Interpreter" );
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     if( VarManager::instance( p_intf ) == NULL )
146     {
147         msg_Err( p_intf, "cannot instanciate VarManager" );
148         vlc_object_release( p_intf->p_sys->p_playlist );
149         msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
150         return VLC_EGENERIC;
151     }
152     if( VlcProc::instance( p_intf ) == NULL )
153     {
154         msg_Err( p_intf, "cannot initialize VLCProc" );
155         vlc_object_release( p_intf->p_sys->p_playlist );
156         msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
157         return VLC_EGENERIC;
158     }
159     Dialogs::instance( p_intf );
160     ThemeRepository::instance( p_intf );
161
162     // We support play on start
163     p_intf->b_play = VLC_TRUE;
164
165     return( VLC_SUCCESS );
166 }
167
168 //---------------------------------------------------------------------------
169 // Close: destroy interface
170 //---------------------------------------------------------------------------
171 static void Close( vlc_object_t *p_this )
172 {
173     intf_thread_t *p_intf = (intf_thread_t *)p_this;
174
175     // Destroy "singleton" objects
176     OSFactory::instance( p_intf )->destroyOSLoop();
177     ThemeRepository::destroy( p_intf );
178     Dialogs::destroy( p_intf );
179     Interpreter::destroy( p_intf );
180     AsyncQueue::destroy( p_intf );
181     VarManager::destroy( p_intf );
182     VlcProc::destroy( p_intf );
183     OSFactory::destroy( p_intf );
184
185     if( p_intf->p_sys->p_playlist )
186     {
187         vlc_object_release( p_intf->p_sys->p_playlist );
188     }
189
190     // Unsubscribe from messages bank
191     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
192
193     // Destroy structure
194     free( p_intf->p_sys );
195 }
196
197
198 //---------------------------------------------------------------------------
199 // Run: main loop
200 //---------------------------------------------------------------------------
201 static void Run( intf_thread_t *p_intf )
202 {
203     // Load a theme
204     ThemeLoader *pLoader = new ThemeLoader( p_intf );
205     char *skin_last = config_GetPsz( p_intf, "skins2-last" );
206
207     if( !skin_last || !*skin_last || !pLoader->load( skin_last ) )
208     {
209         // Get the resource path and try to load the default skin
210         OSFactory *pOSFactory = OSFactory::instance( p_intf );
211         const list<string> &resPath = pOSFactory->getResourcePath();
212         const string &sep = pOSFactory->getDirSeparator();
213
214         list<string>::const_iterator it;
215         for( it = resPath.begin(); it != resPath.end(); it++ )
216         {
217             string path = (*it) + sep + "default.vlt";
218             if( pLoader->load( path ) )
219             {
220                 // Theme loaded successfully
221                 break;
222             }
223         }
224         if( it == resPath.end() )
225         {
226             // Last chance: the user can select a new theme file
227             if( Dialogs::instance( p_intf ) )
228             {
229                 CmdDlgChangeSkin *pCmd = new CmdDlgChangeSkin( p_intf );
230                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
231                 pQueue->push( CmdGenericPtr( pCmd ) );
232             }
233             else
234             {
235                 // No dialogs provider, just quit...
236                 CmdQuit *pCmd = new CmdQuit( p_intf );
237                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
238                 pQueue->push( CmdGenericPtr( pCmd ) );
239                 msg_Err( p_intf,
240                          "cannot show the \"open skin\" dialog: exiting...");
241             }
242         }
243     }
244     delete pLoader;
245
246     if( skin_last )
247     {
248         free( skin_last );
249     }
250
251     // Get the instance of OSLoop
252     OSLoop *loop = OSFactory::instance( p_intf )->getOSLoop();
253
254     // Check if we need to start playing
255     if( p_intf->b_play )
256     {
257         playlist_t *p_playlist =
258             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
259                                            FIND_ANYWHERE );
260         if( p_playlist )
261         {
262             playlist_Control( p_playlist, PLAYLIST_AUTOPLAY, VLC_FALSE );
263             vlc_object_release( p_playlist );
264         }
265     }
266
267     // Enter the main event loop
268     loop->run();
269
270     // Delete the theme and save the configuration of the windows
271     if( p_intf->p_sys->p_theme )
272     {
273         p_intf->p_sys->p_theme->saveConfig();
274         delete p_intf->p_sys->p_theme;
275         p_intf->p_sys->p_theme = NULL;
276     }
277 }
278
279
280 //---------------------------------------------------------------------------
281 // DemuxOpen: initialize demux
282 //---------------------------------------------------------------------------
283 static int DemuxOpen( vlc_object_t *p_this )
284 {
285     demux_t *p_demux = (demux_t*)p_this;
286     intf_thread_t *p_intf;
287     char *ext;
288
289     // Needed callbacks
290     p_demux->pf_demux   = Demux;
291     p_demux->pf_control = DemuxControl;
292
293     // Test that we have a valid .vlt or .wsz file, based on the extension
294     // TODO: an actual check of the contents would be better...
295     if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
296         ( strcasecmp( ext, ".vlt" ) && strcasecmp( ext, ".wsz" ) ) )
297     {
298         return VLC_EGENERIC;
299     }
300
301     p_intf = (intf_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INTF,
302                                                FIND_ANYWHERE );
303     if( p_intf != NULL )
304     {
305         // Do nothing is skins2 is not the main interface
306         if( var_Type( p_intf, "skin-to-load" ) == VLC_VAR_STRING )
307         {
308             playlist_t *p_playlist =
309                 (playlist_t *) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
310                                                 FIND_ANYWHERE );
311             if( p_playlist != NULL )
312             {
313                 // Make sure the item is deleted afterwards
314                 /// \bug does not always work
315                 p_playlist->status.p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
316                 vlc_object_release( p_playlist );
317             }
318
319             vlc_value_t val;
320             val.psz_string = p_demux->psz_path;
321             var_Set( p_intf, "skin-to-load", val );
322         }
323         else
324         {
325             msg_Warn( p_this,
326                       "skin could not be loaded (not using skins2 intf)" );
327         }
328
329         vlc_object_release( p_intf );
330     }
331
332     return VLC_SUCCESS;
333 }
334
335
336 //---------------------------------------------------------------------------
337 // Demux: return EOF
338 //---------------------------------------------------------------------------
339 static int Demux( demux_t *p_demux )
340 {
341     return 0;
342 }
343
344
345 //---------------------------------------------------------------------------
346 // DemuxControl
347 //---------------------------------------------------------------------------
348 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
349 {
350     return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
351 }
352
353
354 //---------------------------------------------------------------------------
355 // Callbacks
356 //---------------------------------------------------------------------------
357
358 /// Callback for the systray configuration option
359 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
360                             vlc_value_t oldVal, vlc_value_t newVal,
361                             void *pParam )
362 {
363     intf_thread_t *pIntf =
364         (intf_thread_t*)vlc_object_find( pObj, VLC_OBJECT_INTF, FIND_ANYWHERE );
365
366     if( pIntf == NULL )
367     {
368         return VLC_EGENERIC;
369     }
370
371     // Check that we found the correct interface (same check as for the demux)
372     if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
373     {
374         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
375         if( newVal.b_bool )
376         {
377             CmdAddInTray *pCmd = new CmdAddInTray( pIntf );
378             pQueue->push( CmdGenericPtr( pCmd ) );
379         }
380         else
381         {
382             CmdRemoveFromTray *pCmd = new CmdRemoveFromTray( pIntf );
383             pQueue->push( CmdGenericPtr( pCmd ) );
384         }
385     }
386
387     vlc_object_release( pIntf );
388     return VLC_SUCCESS;
389 }
390
391
392 /// Callback for the systray configuration option
393 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
394                             vlc_value_t oldVal, vlc_value_t newVal,
395                             void *pParam )
396 {
397     intf_thread_t *pIntf =
398         (intf_thread_t*)vlc_object_find( pObj, VLC_OBJECT_INTF, FIND_ANYWHERE );
399
400     if( pIntf == NULL )
401     {
402         return VLC_EGENERIC;
403     }
404
405     // Check that we found the correct interface (same check as for the demux)
406     if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
407     {
408         AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
409         if( newVal.b_bool )
410         {
411             CmdAddInTaskBar *pCmd = new CmdAddInTaskBar( pIntf );
412             pQueue->push( CmdGenericPtr( pCmd ) );
413         }
414         else
415         {
416             CmdRemoveFromTaskBar *pCmd = new CmdRemoveFromTaskBar( pIntf );
417             pQueue->push( CmdGenericPtr( pCmd ) );
418         }
419     }
420
421     vlc_object_release( pIntf );
422     return VLC_SUCCESS;
423 }
424
425
426 //---------------------------------------------------------------------------
427 // Module descriptor
428 //---------------------------------------------------------------------------
429 #define SKINS2_LAST      N_("Skin to use")
430 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
431 #define SKINS2_CONFIG      N_("Config of last used skin")
432 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
433         "This option is updated automatically, do not touch it." )
434 #define SKINS2_SYSTRAY      N_("Systray icon")
435 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
436 #define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
437 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
438 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
439 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
440     " if you want. This is mainly useful when moving windows does not behave" \
441     " correctly.")
442 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
443 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
444
445 vlc_module_begin();
446     set_category( CAT_INTERFACE );
447     set_subcategory( SUBCAT_INTERFACE_MAIN );
448     add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
449               VLC_TRUE );
450         change_autosave();
451     add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
452                 VLC_TRUE );
453         change_autosave();
454         change_internal();
455 #ifdef WIN32
456     add_bool( "skins2-systray", VLC_FALSE, onSystrayChange, SKINS2_SYSTRAY,
457               SKINS2_SYSTRAY_LONG, VLC_FALSE );
458     add_bool( "skins2-taskbar", VLC_TRUE, onTaskBarChange, SKINS2_TASKBAR,
459               SKINS2_TASKBAR_LONG, VLC_FALSE );
460     add_bool( "skins2-transparency", VLC_FALSE, NULL, SKINS2_TRANSPARENCY,
461               SKINS2_TRANSPARENCY_LONG, VLC_FALSE );
462 #endif
463
464     add_bool( "skinned-playlist", VLC_TRUE, NULL, SKINS2_PLAYLIST,
465               SKINS2_PLAYLIST_LONG, VLC_FALSE );
466     set_shortname( _("Skins"));
467     set_description( _("Skinnable Interface") );
468     set_capability( "interface", 30 );
469     set_callbacks( Open, Close );
470     add_shortcut( "skins" );
471     set_program( "svlc" );
472
473     add_submodule();
474         set_description( _("Skins loader demux") );
475         set_capability( "demux2", 5 );
476         set_callbacks( DemuxOpen, NULL );
477
478 vlc_module_end();