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