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