]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
Merge branch 'master' into lpcm_encoder
[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_playlist.h>
33 #include <vlc_threads.h>
34 #include <vlc_vout_window.h>
35
36 #include "dialogs.hpp"
37 #include "os_factory.hpp"
38 #include "os_loop.hpp"
39 #include "var_manager.hpp"
40 #include "vlcproc.hpp"
41 #include "theme_loader.hpp"
42 #include "theme.hpp"
43 #include "theme_repository.hpp"
44 #include "vout_window.hpp"
45 #include "vout_manager.hpp"
46 #include "art_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 //---------------------------------------------------------------------------
71 // Prototypes for configuration callbacks
72 //---------------------------------------------------------------------------
73 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
74                             vlc_value_t oldVal, vlc_value_t newVal,
75                             void *pParam );
76 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
77                             vlc_value_t oldVal, vlc_value_t newVal,
78                             void *pParam );
79
80 static struct
81 {
82     intf_thread_t *intf;
83     vlc_mutex_t mutex;
84 } skin_load = { NULL, VLC_STATIC_MUTEX };
85
86 //---------------------------------------------------------------------------
87 // Open: initialize interface
88 //---------------------------------------------------------------------------
89 static int Open( vlc_object_t *p_this )
90 {
91     intf_thread_t *p_intf = (intf_thread_t *)p_this;
92
93     // Allocate instance and initialize some members
94     p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
95     if( p_intf->p_sys == NULL )
96         return VLC_ENOMEM;
97
98     // Suscribe to messages bank
99 #if 0
100     p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
101 #endif
102
103     p_intf->p_sys->p_input = NULL;
104     p_intf->p_sys->p_playlist = pl_Get( p_intf );
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_voutManager = NULL;
115     p_intf->p_sys->p_vlcProc = NULL;
116     p_intf->p_sys->p_repository = NULL;
117
118     // No theme yet
119     p_intf->p_sys->p_theme = NULL;
120
121     vlc_mutex_init( &p_intf->p_sys->vout_lock );
122     vlc_cond_init( &p_intf->p_sys->vout_wait );
123
124     vlc_mutex_init( &p_intf->p_sys->init_lock );
125     vlc_cond_init( &p_intf->p_sys->init_wait );
126
127     vlc_mutex_lock( &p_intf->p_sys->init_lock );
128     p_intf->p_sys->b_ready = false;
129
130     if( vlc_clone( &p_intf->p_sys->thread, Run, p_intf,
131                                VLC_THREAD_PRIORITY_LOW ) )
132     {
133         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
134
135         vlc_cond_destroy( &p_intf->p_sys->init_wait );
136         vlc_mutex_destroy( &p_intf->p_sys->init_lock );
137         vlc_cond_destroy( &p_intf->p_sys->vout_wait );
138         vlc_mutex_destroy( &p_intf->p_sys->vout_lock );
139         free( p_intf->p_sys );
140         return VLC_EGENERIC;
141     }
142
143     while( !p_intf->p_sys->b_ready )
144         vlc_cond_wait( &p_intf->p_sys->init_wait, &p_intf->p_sys->init_lock );
145     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
146
147     vlc_mutex_lock( &skin_load.mutex );
148     skin_load.intf = p_intf;
149     vlc_mutex_unlock( &skin_load.mutex );
150
151     return VLC_SUCCESS;
152 }
153
154 //---------------------------------------------------------------------------
155 // Close: destroy interface
156 //---------------------------------------------------------------------------
157 static void Close( vlc_object_t *p_this )
158 {
159     intf_thread_t *p_intf = (intf_thread_t *)p_this;
160
161     msg_Dbg( p_intf, "closing skins2 module" );
162
163     vlc_mutex_lock( &skin_load.mutex );
164     skin_load.intf = NULL;
165     vlc_mutex_unlock( &skin_load.mutex);
166
167     vlc_join( p_intf->p_sys->thread, NULL );
168
169     vlc_mutex_destroy( &p_intf->p_sys->init_lock );
170     vlc_cond_destroy( &p_intf->p_sys->init_wait );
171
172     // Unsubscribe from messages bank
173 #if 0
174     msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
175 #endif
176
177     vlc_cond_destroy( &p_intf->p_sys->vout_wait );
178     vlc_mutex_destroy( &p_intf->p_sys->vout_lock );
179
180     // Destroy structure
181     free( p_intf->p_sys );
182 }
183
184
185 //---------------------------------------------------------------------------
186 // Run: main loop
187 //---------------------------------------------------------------------------
188 static void *Run( void * p_obj )
189 {
190     int canc = vlc_savecancel();
191
192     intf_thread_t *p_intf = (intf_thread_t *)p_obj;
193
194     bool b_error = false;
195     char *skin_last = NULL;
196     ThemeLoader *pLoader = NULL;
197     OSLoop *loop = NULL;
198
199     vlc_mutex_lock( &p_intf->p_sys->init_lock );
200
201     // Initialize singletons
202     if( OSFactory::instance( p_intf ) == NULL )
203     {
204         msg_Err( p_intf, "cannot initialize OSFactory" );
205         b_error = true;
206         goto end;
207     }
208     if( AsyncQueue::instance( p_intf ) == NULL )
209     {
210         msg_Err( p_intf, "cannot initialize AsyncQueue" );
211         b_error = true;
212         goto end;
213     }
214     if( Interpreter::instance( p_intf ) == NULL )
215     {
216         msg_Err( p_intf, "cannot instantiate Interpreter" );
217         b_error = true;
218         goto end;
219     }
220     if( VarManager::instance( p_intf ) == NULL )
221     {
222         msg_Err( p_intf, "cannot instantiate VarManager" );
223         b_error = true;
224         goto end;
225     }
226     if( VlcProc::instance( p_intf ) == NULL )
227     {
228         msg_Err( p_intf, "cannot initialize VLCProc" );
229         b_error = true;
230         goto end;
231     }
232     if( VoutManager::instance( p_intf ) == NULL )
233     {
234         msg_Err( p_intf, "cannot instantiate VoutManager" );
235         b_error = true;
236         goto end;
237     }
238     if( ArtManager::instance( p_intf ) == NULL )
239     {
240         msg_Err( p_intf, "cannot instantiate ArtManager" );
241         b_error = true;
242         goto end;
243     }
244     if( ThemeRepository::instance( p_intf ) == NULL )
245     {
246         msg_Err( p_intf, "cannot instantiate ThemeRepository" );
247         b_error = true;
248         goto end;
249     }
250     if( Dialogs::instance( p_intf ) == NULL )
251     {
252         msg_Err( p_intf, "cannot instantiate qt4 dialogs provider" );
253         b_error = true;
254         goto end;
255     }
256
257     // Load a theme
258     skin_last = config_GetPsz( p_intf, "skins2-last" );
259     pLoader = new ThemeLoader( p_intf );
260
261     if( !skin_last || !pLoader->load( skin_last ) )
262     {
263         // No skins (not even the default one). let's quit
264         CmdQuit *pCmd = new CmdQuit( p_intf );
265         AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
266         pQueue->push( CmdGenericPtr( pCmd ) );
267         msg_Err( p_intf, "no skins found : exiting");
268     }
269
270     delete pLoader;
271     free( skin_last );
272
273     // Get the instance of OSLoop
274     loop = OSFactory::instance( p_intf )->getOSLoop();
275
276     // Signal the main thread this thread is now ready
277     p_intf->p_sys->b_ready = true;
278     vlc_cond_signal( &p_intf->p_sys->init_wait );
279     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
280
281     // Enter the main event loop
282     loop->run();
283
284     // Destroy OSLoop
285     OSFactory::instance( p_intf )->destroyOSLoop();
286
287     // save and delete the theme
288     if( p_intf->p_sys->p_theme )
289     {
290         p_intf->p_sys->p_theme->saveConfig();
291
292         delete p_intf->p_sys->p_theme;
293         p_intf->p_sys->p_theme = NULL;
294
295         msg_Dbg( p_intf, "current theme deleted" );
296     }
297
298     // save config file
299     config_SaveConfigFile( p_intf, NULL );
300
301 end:
302     // Destroy "singleton" objects
303     Dialogs::destroy( p_intf );
304     ThemeRepository::destroy( p_intf );
305     ArtManager::destroy( p_intf );
306     VoutManager::destroy( p_intf );
307     VlcProc::destroy( p_intf );
308     VarManager::destroy( p_intf );
309     Interpreter::destroy( p_intf );
310     AsyncQueue::destroy( p_intf );
311     OSFactory::destroy( p_intf );
312
313     if( b_error )
314     {
315         p_intf->p_sys->b_ready = true;
316         vlc_cond_signal( &p_intf->p_sys->init_wait );
317         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
318
319         libvlc_Quit( p_intf->p_libvlc );
320     }
321
322     vlc_restorecancel(canc);
323     return NULL;
324 }
325
326 static vlc_mutex_t serializer = VLC_STATIC_MUTEX;
327
328 // Callbacks for vout requests
329 static int WindowOpen( vlc_object_t *p_this )
330 {
331     vout_window_t *pWnd = (vout_window_t *)p_this;
332
333     vlc_mutex_lock( &skin_load.mutex );
334     intf_thread_t *pIntf = skin_load.intf;
335     if( pIntf )
336         vlc_object_hold( pIntf );
337     vlc_mutex_unlock( &skin_load.mutex );
338
339     if( pIntf == NULL )
340         return VLC_EGENERIC;
341
342     if( !vlc_object_alive( pIntf ) ||
343         !var_InheritBool( pIntf, "skinned-video") ||
344         pWnd->cfg->is_standalone )
345     {
346         vlc_object_release( pIntf );
347         return VLC_EGENERIC;
348     }
349
350     vlc_mutex_lock( &serializer );
351
352     pWnd->handle.hwnd = VoutManager::getWindow( pIntf, pWnd );
353
354     if( pWnd->handle.hwnd )
355     {
356         pWnd->control = &VoutManager::controlWindow;
357         pWnd->sys = (vout_window_sys_t*)pIntf;
358
359         vlc_mutex_unlock( &serializer );
360         return VLC_SUCCESS;
361     }
362     else
363     {
364         vlc_object_release( pIntf );
365         vlc_mutex_unlock( &serializer );
366         return VLC_EGENERIC;
367     }
368 }
369
370 static void WindowClose( vlc_object_t *p_this )
371 {
372     vout_window_t *pWnd = (vout_window_t *)p_this;
373     intf_thread_t *pIntf = (intf_thread_t *)pWnd->sys;
374
375     VoutManager::releaseWindow( pIntf, pWnd );
376
377     vlc_object_release( pIntf );
378 }
379
380
381 //---------------------------------------------------------------------------
382 // Callbacks
383 //---------------------------------------------------------------------------
384
385 /// Callback for the systray configuration option
386 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
387                             vlc_value_t oldVal, vlc_value_t newVal,
388                             void *pParam )
389 {
390     intf_thread_t *pIntf;
391
392     vlc_mutex_lock( &skin_load.mutex );
393     pIntf = skin_load.intf;
394     if( pIntf )
395         vlc_object_hold( pIntf );
396     vlc_mutex_unlock( &skin_load.mutex );
397
398     if( pIntf == NULL )
399     {
400         return VLC_EGENERIC;
401     }
402
403     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
404     if( newVal.b_bool )
405     {
406         CmdAddInTray *pCmd = new CmdAddInTray( pIntf );
407         pQueue->push( CmdGenericPtr( pCmd ) );
408     }
409     else
410     {
411         CmdRemoveFromTray *pCmd = new CmdRemoveFromTray( pIntf );
412         pQueue->push( CmdGenericPtr( pCmd ) );
413     }
414
415     vlc_object_release( pIntf );
416     return VLC_SUCCESS;
417 }
418
419
420 /// Callback for the systray configuration option
421 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
422                             vlc_value_t oldVal, vlc_value_t newVal,
423                             void *pParam )
424 {
425     intf_thread_t *pIntf;
426
427     vlc_mutex_lock( &skin_load.mutex );
428     pIntf = skin_load.intf;
429     if( pIntf )
430         vlc_object_hold( pIntf );
431     vlc_mutex_unlock( &skin_load.mutex );
432
433     if( pIntf == NULL )
434     {
435         return VLC_EGENERIC;
436     }
437
438     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
439     if( newVal.b_bool )
440     {
441         CmdAddInTaskBar *pCmd = new CmdAddInTaskBar( pIntf );
442         pQueue->push( CmdGenericPtr( pCmd ) );
443     }
444     else
445     {
446         CmdRemoveFromTaskBar *pCmd = new CmdRemoveFromTaskBar( pIntf );
447         pQueue->push( CmdGenericPtr( pCmd ) );
448     }
449
450     vlc_object_release( pIntf );
451     return VLC_SUCCESS;
452 }
453
454
455 //---------------------------------------------------------------------------
456 // Module descriptor
457 //---------------------------------------------------------------------------
458 #define SKINS2_LAST      N_("Skin to use")
459 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
460 #define SKINS2_CONFIG      N_("Config of last used skin")
461 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
462         "This option is updated automatically, do not touch it." )
463 #define SKINS2_SYSTRAY      N_("Systray icon")
464 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
465 #define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
466 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
467 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
468 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
469     " if you want. This is mainly useful when moving windows does not behave" \
470     " correctly.")
471 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
472 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
473 #define SKINS2_VIDEO N_("Display video in a skinned window if any")
474 #define SKINS2_VIDEO_LONG N_( \
475     "When set to 'no', this parameter is intended to give old skins a chance" \
476     " to play back video even though no video tag is implemented")
477
478 vlc_module_begin ()
479     set_category( CAT_INTERFACE )
480     set_subcategory( SUBCAT_INTERFACE_MAIN )
481     add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
482               true )
483         change_autosave ()
484     add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
485                 true )
486         change_autosave ()
487         change_private ()
488 #ifdef WIN32
489     add_bool( "skins2-systray", false, onSystrayChange, SKINS2_SYSTRAY,
490               SKINS2_SYSTRAY_LONG, false );
491     add_bool( "skins2-taskbar", true, onTaskBarChange, SKINS2_TASKBAR,
492               SKINS2_TASKBAR_LONG, false );
493 #endif
494     add_bool( "skins2-transparency", false, NULL, SKINS2_TRANSPARENCY,
495               SKINS2_TRANSPARENCY_LONG, false );
496
497     add_bool( "skinned-playlist", true, NULL, SKINS2_PLAYLIST,
498               SKINS2_PLAYLIST_LONG, false );
499     add_bool( "skinned-video", true, NULL, SKINS2_VIDEO,
500               SKINS2_VIDEO_LONG, false );
501     set_shortname( N_("Skins"))
502     set_description( N_("Skinnable Interface") )
503     set_capability( "interface", 30 )
504     set_callbacks( Open, Close )
505     add_shortcut( "skins" )
506
507     add_submodule ()
508 #ifdef WIN32
509         set_capability( "vout window hwnd", 51 )
510 #else
511         set_capability( "vout window xid", 51 )
512 #endif
513         set_callbacks( WindowOpen, WindowClose )
514
515 vlc_module_end ()