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