]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
vout_window_t: simplify via anynomous union
[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_vout_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  ( void * );
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 static struct
85 {
86     intf_thread_t *intf;
87     vlc_mutex_t mutex;
88 } skin_load = { NULL, VLC_STATIC_MUTEX };
89
90 //---------------------------------------------------------------------------
91 // Open: initialize interface
92 //---------------------------------------------------------------------------
93 static int Open( vlc_object_t *p_this )
94 {
95     intf_thread_t *p_intf = (intf_thread_t *)p_this;
96
97     // Allocate instance and initialize some members
98     p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
99     if( p_intf->p_sys == NULL )
100         return VLC_ENOMEM;
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     if( !p_intf->p_sys->p_playlist )
110     {
111         free( p_intf->p_sys );
112         return VLC_EGENERIC;
113     }
114
115     // Initialize "singleton" objects
116     p_intf->p_sys->p_logger = NULL;
117     p_intf->p_sys->p_queue = NULL;
118     p_intf->p_sys->p_dialogs = NULL;
119     p_intf->p_sys->p_interpreter = NULL;
120     p_intf->p_sys->p_osFactory = NULL;
121     p_intf->p_sys->p_osLoop = NULL;
122     p_intf->p_sys->p_varManager = NULL;
123     p_intf->p_sys->p_voutManager = NULL;
124     p_intf->p_sys->p_vlcProc = NULL;
125     p_intf->p_sys->p_repository = NULL;
126
127     // No theme yet
128     p_intf->p_sys->p_theme = NULL;
129
130     // Create a variable to be notified of skins to be loaded
131     var_Create( p_intf, "skin-to-load", VLC_VAR_STRING );
132
133     vlc_mutex_init( &p_intf->p_sys->vout_lock );
134     vlc_cond_init( &p_intf->p_sys->vout_wait );
135
136     vlc_mutex_init( &p_intf->p_sys->init_lock );
137     vlc_cond_init( &p_intf->p_sys->init_wait );
138
139     vlc_mutex_lock( &p_intf->p_sys->init_lock );
140     p_intf->p_sys->b_ready = false;
141
142     if( vlc_clone( &p_intf->p_sys->thread, Run, p_intf,
143                                VLC_THREAD_PRIORITY_LOW ) )
144     {
145         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
146
147         vlc_cond_destroy( &p_intf->p_sys->init_wait );
148         vlc_mutex_destroy( &p_intf->p_sys->init_lock );
149         vlc_cond_destroy( &p_intf->p_sys->vout_wait );
150         vlc_mutex_destroy( &p_intf->p_sys->vout_lock );
151         pl_Release( p_intf->p_sys->p_playlist );
152         free( p_intf->p_sys );
153         return VLC_EGENERIC;
154     }
155
156     while( !p_intf->p_sys->b_ready )
157         vlc_cond_wait( &p_intf->p_sys->init_wait, &p_intf->p_sys->init_lock );
158     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
159
160     vlc_mutex_lock( &skin_load.mutex );
161     skin_load.intf = p_intf;
162     vlc_mutex_unlock( &skin_load.mutex );
163
164     return VLC_SUCCESS;
165 }
166
167 //---------------------------------------------------------------------------
168 // Close: destroy interface
169 //---------------------------------------------------------------------------
170 static void Close( vlc_object_t *p_this )
171 {
172     intf_thread_t *p_intf = (intf_thread_t *)p_this;
173
174     msg_Dbg( p_intf, "closing skins2 module" );
175
176     vlc_mutex_lock( &skin_load.mutex );
177     skin_load.intf = NULL;
178     vlc_mutex_unlock( &skin_load.mutex);
179
180     vlc_join( p_intf->p_sys->thread, NULL );
181
182     vlc_mutex_destroy( &p_intf->p_sys->init_lock );
183     vlc_cond_destroy( &p_intf->p_sys->init_wait );
184
185     if( p_intf->p_sys->p_playlist )
186         pl_Release( p_this );
187
188     // Unsubscribe from messages bank
189 #if 0
190     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
191 #endif
192
193     vlc_cond_destroy( &p_intf->p_sys->vout_wait );
194     vlc_mutex_destroy( &p_intf->p_sys->vout_lock );
195
196     // Destroy structure
197     free( p_intf->p_sys );
198 }
199
200
201 //---------------------------------------------------------------------------
202 // Run: main loop
203 //---------------------------------------------------------------------------
204 static void *Run( void * p_obj )
205 {
206     int canc = vlc_savecancel();
207
208     intf_thread_t *p_intf = (intf_thread_t *)p_obj;
209
210     bool b_error = false;
211     char *skin_last = NULL;
212     ThemeLoader *pLoader = NULL;
213     OSLoop *loop = NULL;
214
215     vlc_mutex_lock( &p_intf->p_sys->init_lock );
216
217     // Initialize singletons
218     if( OSFactory::instance( p_intf ) == NULL )
219     {
220         msg_Err( p_intf, "cannot initialize OSFactory" );
221         b_error = true;
222         goto end;
223     }
224     if( AsyncQueue::instance( p_intf ) == NULL )
225     {
226         msg_Err( p_intf, "cannot initialize AsyncQueue" );
227         b_error = true;
228         goto end;
229     }
230     if( Interpreter::instance( p_intf ) == NULL )
231     {
232         msg_Err( p_intf, "cannot instanciate Interpreter" );
233         b_error = true;
234         goto end;
235     }
236     if( VarManager::instance( p_intf ) == NULL )
237     {
238         msg_Err( p_intf, "cannot instanciate VarManager" );
239         b_error = true;
240         goto end;
241     }
242     if( VlcProc::instance( p_intf ) == NULL )
243     {
244         msg_Err( p_intf, "cannot initialize VLCProc" );
245         b_error = true;
246         goto end;
247     }
248     if( VoutManager::instance( p_intf ) == NULL )
249     {
250         msg_Err( p_intf, "cannot instanciate VoutManager" );
251         b_error = true;
252         goto end;
253     }
254     if( ThemeRepository::instance( p_intf ) == NULL )
255     {
256         msg_Err( p_intf, "cannot instanciate ThemeRepository" );
257         b_error = true;
258         goto end;
259     }
260     if( Dialogs::instance( p_intf ) == NULL )
261     {
262         msg_Err( p_intf, "cannot instanciate qt4 dialogs provider" );
263         b_error = true;
264         goto end;
265     }
266
267     // Load a theme
268     skin_last = config_GetPsz( p_intf, "skins2-last" );
269     pLoader = new ThemeLoader( p_intf );
270
271     if( !skin_last || !*skin_last || !pLoader->load( skin_last ) )
272     {
273         // Get the resource path and try to load the default skin
274         OSFactory *pOSFactory = OSFactory::instance( p_intf );
275         const list<string> &resPath = pOSFactory->getResourcePath();
276         const string &sep = pOSFactory->getDirSeparator();
277
278         list<string>::const_iterator it;
279         for( it = resPath.begin(); it != resPath.end(); it++ )
280         {
281             string path = (*it) + sep + "default.vlt";
282             if( pLoader->load( path ) )
283             {
284                 // Theme loaded successfully
285                 break;
286             }
287         }
288         if( it == resPath.end() )
289         {
290             // Last chance: the user can select a new theme file
291             if( Dialogs::instance( p_intf ) )
292             {
293                 CmdDlgChangeSkin *pCmd = new CmdDlgChangeSkin( p_intf );
294                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
295                 pQueue->push( CmdGenericPtr( pCmd ) );
296             }
297             else
298             {
299                 // No dialogs provider, just quit...
300                 CmdQuit *pCmd = new CmdQuit( p_intf );
301                 AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
302                 pQueue->push( CmdGenericPtr( pCmd ) );
303                 msg_Err( p_intf,
304                          "cannot show the \"open skin\" dialog: exiting...");
305             }
306         }
307     }
308
309     delete pLoader;
310     free( skin_last );
311
312     // Get the instance of OSLoop
313     loop = OSFactory::instance( p_intf )->getOSLoop();
314
315     // Signal the main thread this thread is now ready
316     p_intf->p_sys->b_ready = true;
317     vlc_cond_signal( &p_intf->p_sys->init_wait );
318     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
319
320     // Enter the main event loop
321     loop->run();
322
323     // Destroy OSLoop
324     OSFactory::instance( p_intf )->destroyOSLoop();
325
326     // save and delete the theme
327     if( p_intf->p_sys->p_theme )
328     {
329         p_intf->p_sys->p_theme->saveConfig();
330
331         delete p_intf->p_sys->p_theme;
332         p_intf->p_sys->p_theme = NULL;
333
334         msg_Dbg( p_intf, "current theme deleted" );
335     }
336
337     // save config file
338     config_SaveConfigFile( p_intf, NULL );
339
340 end:
341     // Destroy "singleton" objects
342     Dialogs::destroy( p_intf );
343     ThemeRepository::destroy( p_intf );
344     VoutManager::destroy( p_intf );
345     VlcProc::destroy( p_intf );
346     VarManager::destroy( p_intf );
347     Interpreter::destroy( p_intf );
348     AsyncQueue::destroy( p_intf );
349     OSFactory::destroy( p_intf );
350
351     if( b_error )
352     {
353         p_intf->p_sys->b_ready = true;
354         vlc_cond_signal( &p_intf->p_sys->init_wait );
355         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
356
357         libvlc_Quit( p_intf->p_libvlc );
358     }
359
360     vlc_restorecancel(canc);
361     return NULL;
362 }
363
364 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
365
366 // Callbacks for vout requests
367 static int WindowOpen( vlc_object_t *p_this )
368 {
369     vout_window_t *pWnd = (vout_window_t *)p_this;
370
371     vlc_mutex_lock( &skin_load.mutex );
372     intf_thread_t *pIntf = skin_load.intf;
373     if( pIntf )
374         vlc_object_hold( pIntf );
375     vlc_mutex_unlock( &skin_load.mutex );
376
377     if( pIntf == NULL )
378         return VLC_EGENERIC;
379
380     if( !config_GetInt( pIntf, "skinned-video") )
381     {
382         vlc_object_release( pIntf );
383         return VLC_EGENERIC;
384     }
385
386     vlc_mutex_lock( &serializer );
387
388     pWnd->hwnd = VoutManager::getWindow( pIntf, pWnd );
389
390     if( pWnd->hwnd )
391     {
392         pWnd->control = &VoutManager::controlWindow;
393         pWnd->sys = (vout_window_sys_t*)pIntf;
394
395         vlc_mutex_unlock( &serializer );
396         return VLC_SUCCESS;
397     }
398     else
399     {
400         vlc_object_release( pIntf );
401         vlc_mutex_unlock( &serializer );
402         return VLC_EGENERIC;
403     }
404 }
405
406 static void WindowClose( vlc_object_t *p_this )
407 {
408     vout_window_t *pWnd = (vout_window_t *)p_this;
409     intf_thread_t *pIntf = (intf_thread_t *)pWnd->sys;
410
411     VoutManager::releaseWindow( pIntf, pWnd );
412
413     vlc_object_release( pIntf );
414 }
415
416 //---------------------------------------------------------------------------
417 // DemuxOpen: initialize demux
418 //---------------------------------------------------------------------------
419 static int DemuxOpen( vlc_object_t *p_this )
420 {
421     demux_t *p_demux = (demux_t*)p_this;
422     intf_thread_t *p_intf;
423     char *ext;
424
425     // Needed callbacks
426     p_demux->pf_demux   = Demux;
427     p_demux->pf_control = DemuxControl;
428
429     // Test that we have a valid .vlt or .wsz file, based on the extension
430     // TODO: an actual check of the contents would be better...
431     if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
432         ( strcasecmp( ext, ".vlt" ) && strcasecmp( ext, ".wsz" ) ) )
433     {
434         return VLC_EGENERIC;
435     }
436
437     vlc_mutex_lock( &skin_load.mutex );
438     p_intf = skin_load.intf;
439     if( p_intf )
440         vlc_object_hold( p_intf );
441     vlc_mutex_unlock( &skin_load.mutex );
442
443     if( p_intf != NULL )
444     {
445         playlist_t *p_playlist = pl_Hold( p_this );
446         // Make sure the item is deleted afterwards
447         /// \bug does not always work
448         playlist_CurrentPlayingItem( p_playlist )->i_flags |= PLAYLIST_REMOVE_FLAG;
449         pl_Release( p_this );
450
451         var_SetString( p_intf, "skin-to-load", p_demux->psz_path );
452         vlc_object_release( p_intf );
453     }
454     else
455     {
456         msg_Warn( p_this,
457                   "skin could not be loaded (not using skins2 intf)" );
458     }
459
460     return VLC_SUCCESS;
461 }
462
463
464 //---------------------------------------------------------------------------
465 // Demux: return EOF
466 //---------------------------------------------------------------------------
467 static int Demux( demux_t *p_demux )
468 {
469     return 0;
470 }
471
472
473 //---------------------------------------------------------------------------
474 // DemuxControl
475 //---------------------------------------------------------------------------
476 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
477 {
478     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
479 }
480
481
482 //---------------------------------------------------------------------------
483 // Callbacks
484 //---------------------------------------------------------------------------
485
486 /// Callback for the systray configuration option
487 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
488                             vlc_value_t oldVal, vlc_value_t newVal,
489                             void *pParam )
490 {
491     intf_thread_t *pIntf;
492
493     vlc_mutex_lock( &skin_load.mutex );
494     pIntf = skin_load.intf;
495     if( pIntf )
496         vlc_object_hold( pIntf );
497     vlc_mutex_unlock( &skin_load.mutex );
498
499     if( pIntf == NULL )
500     {
501         return VLC_EGENERIC;
502     }
503
504     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
505     if( newVal.b_bool )
506     {
507         CmdAddInTray *pCmd = new CmdAddInTray( pIntf );
508         pQueue->push( CmdGenericPtr( pCmd ) );
509     }
510     else
511     {
512         CmdRemoveFromTray *pCmd = new CmdRemoveFromTray( pIntf );
513         pQueue->push( CmdGenericPtr( pCmd ) );
514     }
515
516     vlc_object_release( pIntf );
517     return VLC_SUCCESS;
518 }
519
520
521 /// Callback for the systray configuration option
522 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
523                             vlc_value_t oldVal, vlc_value_t newVal,
524                             void *pParam )
525 {
526     intf_thread_t *pIntf;
527
528     vlc_mutex_lock( &skin_load.mutex );
529     pIntf = skin_load.intf;
530     if( pIntf )
531         vlc_object_hold( pIntf );
532     vlc_mutex_unlock( &skin_load.mutex );
533
534     if( pIntf == NULL )
535     {
536         return VLC_EGENERIC;
537     }
538
539     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
540     if( newVal.b_bool )
541     {
542         CmdAddInTaskBar *pCmd = new CmdAddInTaskBar( pIntf );
543         pQueue->push( CmdGenericPtr( pCmd ) );
544     }
545     else
546     {
547         CmdRemoveFromTaskBar *pCmd = new CmdRemoveFromTaskBar( pIntf );
548         pQueue->push( CmdGenericPtr( pCmd ) );
549     }
550
551     vlc_object_release( pIntf );
552     return VLC_SUCCESS;
553 }
554
555
556 //---------------------------------------------------------------------------
557 // Module descriptor
558 //---------------------------------------------------------------------------
559 #define SKINS2_LAST      N_("Skin to use")
560 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
561 #define SKINS2_CONFIG      N_("Config of last used skin")
562 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
563         "This option is updated automatically, do not touch it." )
564 #define SKINS2_SYSTRAY      N_("Systray icon")
565 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
566 #define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
567 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
568 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
569 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
570     " if you want. This is mainly useful when moving windows does not behave" \
571     " correctly.")
572 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
573 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
574 #define SKINS2_VIDEO N_("Display video in a skinned window if any")
575 #define SKINS2_VIDEO_LONG N_( \
576     "When set to 'no', this parameter is intended to give old skins a chance" \
577     " to play back video even though no video tag is implemented")
578
579 vlc_module_begin ()
580     set_category( CAT_INTERFACE )
581     set_subcategory( SUBCAT_INTERFACE_MAIN )
582     add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
583               true )
584         change_autosave ()
585     add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
586                 true )
587         change_autosave ()
588         change_internal ()
589 #ifdef WIN32
590     add_bool( "skins2-systray", false, onSystrayChange, SKINS2_SYSTRAY,
591               SKINS2_SYSTRAY_LONG, false );
592     add_bool( "skins2-taskbar", true, onTaskBarChange, SKINS2_TASKBAR,
593               SKINS2_TASKBAR_LONG, false );
594     add_bool( "skins2-transparency", false, NULL, SKINS2_TRANSPARENCY,
595               SKINS2_TRANSPARENCY_LONG, false );
596 #endif
597
598     add_bool( "skinned-playlist", true, NULL, SKINS2_PLAYLIST,
599               SKINS2_PLAYLIST_LONG, false );
600     add_bool( "skinned-video", true, NULL, SKINS2_VIDEO,
601               SKINS2_VIDEO_LONG, false );
602     set_shortname( N_("Skins"))
603     set_description( N_("Skinnable Interface") )
604     set_capability( "interface", 30 )
605     set_callbacks( Open, Close )
606     add_shortcut( "skins" )
607
608     add_submodule ()
609 #ifdef WIN32
610         set_capability( "vout window hwnd", 51 )
611 #else
612         set_capability( "vout window xid", 51 )
613 #endif
614         set_callbacks( WindowOpen, WindowClose )
615
616     add_submodule ()
617         set_description( N_("Skins loader demux") )
618         set_capability( "demux", 5 )
619         set_callbacks( DemuxOpen, NULL )
620         add_shortcut( "skins" )
621
622 vlc_module_end ()