]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
1739b31d38c91d6d229854c557e5d4bb2207d366
[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 #include <vlc_vout_display.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 "art_manager.hpp"
48 #include "../parser/interpreter.hpp"
49 #include "../commands/async_queue.hpp"
50 #include "../commands/cmd_quit.hpp"
51 #include "../commands/cmd_dialogs.hpp"
52 #include "../commands/cmd_minimize.hpp"
53 #include "../commands/cmd_playlist.hpp"
54 #include "../commands/cmd_callbacks.hpp"
55 #include "../commands/cmd_show_window.hpp"
56 #include "../commands/cmd_resize.hpp"
57 #include "../commands/cmd_on_top.hpp"
58
59 //---------------------------------------------------------------------------
60 // Local prototypes
61 //---------------------------------------------------------------------------
62 static int  Open  ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
64 static void *Run  ( void * );
65
66 static struct
67 {
68     intf_thread_t *intf;
69     vlc_mutex_t mutex;
70 } skin_load = { NULL, VLC_STATIC_MUTEX };
71
72 //---------------------------------------------------------------------------
73 // Open: initialize interface
74 //---------------------------------------------------------------------------
75 static int Open( vlc_object_t *p_this )
76 {
77     intf_thread_t *p_intf = (intf_thread_t *)p_this;
78
79     // Allocate instance and initialize some members
80     p_intf->p_sys = (intf_sys_t *) calloc( 1, sizeof( intf_sys_t ) );
81     if( p_intf->p_sys == NULL )
82         return VLC_ENOMEM;
83
84     p_intf->p_sys->p_input = NULL;
85     p_intf->p_sys->p_playlist = pl_Get( p_intf );
86
87     // Initialize "singleton" objects
88     p_intf->p_sys->p_logger = NULL;
89     p_intf->p_sys->p_queue = NULL;
90     p_intf->p_sys->p_dialogs = NULL;
91     p_intf->p_sys->p_interpreter = NULL;
92     p_intf->p_sys->p_osFactory = NULL;
93     p_intf->p_sys->p_osLoop = NULL;
94     p_intf->p_sys->p_varManager = NULL;
95     p_intf->p_sys->p_voutManager = NULL;
96     p_intf->p_sys->p_vlcProc = NULL;
97     p_intf->p_sys->p_repository = NULL;
98
99     // No theme yet
100     p_intf->p_sys->p_theme = NULL;
101
102     vlc_mutex_init( &p_intf->p_sys->init_lock );
103     vlc_cond_init( &p_intf->p_sys->init_wait );
104
105     vlc_mutex_lock( &p_intf->p_sys->init_lock );
106     p_intf->p_sys->b_error = false;
107     p_intf->p_sys->b_ready = false;
108
109     if( vlc_clone( &p_intf->p_sys->thread, Run, p_intf,
110                                VLC_THREAD_PRIORITY_LOW ) )
111     {
112         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
113
114         vlc_cond_destroy( &p_intf->p_sys->init_wait );
115         vlc_mutex_destroy( &p_intf->p_sys->init_lock );
116         free( p_intf->p_sys );
117         return VLC_EGENERIC;
118     }
119
120     while( !p_intf->p_sys->b_ready )
121         vlc_cond_wait( &p_intf->p_sys->init_wait, &p_intf->p_sys->init_lock );
122     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
123
124     if( p_intf->p_sys->b_error )
125     {
126         vlc_join( p_intf->p_sys->thread, NULL );
127
128         vlc_mutex_destroy( &p_intf->p_sys->init_lock );
129         vlc_cond_destroy( &p_intf->p_sys->init_wait );
130
131         free( p_intf->p_sys );
132         return VLC_EGENERIC;
133     }
134
135     vlc_mutex_lock( &skin_load.mutex );
136     skin_load.intf = p_intf;
137     vlc_mutex_unlock( &skin_load.mutex );
138
139     return VLC_SUCCESS;
140 }
141
142 //---------------------------------------------------------------------------
143 // Close: destroy interface
144 //---------------------------------------------------------------------------
145 static void Close( vlc_object_t *p_this )
146 {
147     intf_thread_t *p_intf = (intf_thread_t *)p_this;
148
149     msg_Dbg( p_intf, "closing skins2 module" );
150
151     /* Terminate input to ensure that our window provider is released. */
152     playlist_Deactivate( p_intf->p_sys->p_playlist );
153
154     vlc_mutex_lock( &skin_load.mutex );
155     skin_load.intf = NULL;
156     vlc_mutex_unlock( &skin_load.mutex);
157
158     AsyncQueue *pQueue = p_intf->p_sys->p_queue;
159     if( pQueue )
160     {
161         CmdGeneric *pCmd = new CmdExitLoop( p_intf );
162         if( pCmd )
163             pQueue->push( CmdGenericPtr( pCmd ) );
164     }
165     else
166     {
167         msg_Err( p_intf, "thread found already stopped (weird!)" );
168     }
169
170     vlc_join( p_intf->p_sys->thread, NULL );
171
172     vlc_mutex_destroy( &p_intf->p_sys->init_lock );
173     vlc_cond_destroy( &p_intf->p_sys->init_wait );
174
175     // Destroy structure
176     free( p_intf->p_sys );
177 }
178
179
180 //---------------------------------------------------------------------------
181 // Run: main loop
182 //---------------------------------------------------------------------------
183 static void *Run( void * p_obj )
184 {
185     int canc = vlc_savecancel();
186
187     intf_thread_t *p_intf = (intf_thread_t *)p_obj;
188
189     bool b_error = false;
190     char *skin_last = NULL;
191     ThemeLoader *pLoader = NULL;
192     OSLoop *loop = NULL;
193
194     vlc_mutex_lock( &p_intf->p_sys->init_lock );
195
196     // Initialize singletons
197     if( OSFactory::instance( p_intf ) == NULL )
198     {
199         msg_Err( p_intf, "cannot initialize OSFactory" );
200         b_error = true;
201         goto end;
202     }
203     if( AsyncQueue::instance( p_intf ) == NULL )
204     {
205         msg_Err( p_intf, "cannot initialize AsyncQueue" );
206         b_error = true;
207         goto end;
208     }
209     if( Interpreter::instance( p_intf ) == NULL )
210     {
211         msg_Err( p_intf, "cannot instantiate Interpreter" );
212         b_error = true;
213         goto end;
214     }
215     if( VarManager::instance( p_intf ) == NULL )
216     {
217         msg_Err( p_intf, "cannot instantiate VarManager" );
218         b_error = true;
219         goto end;
220     }
221     if( VlcProc::instance( p_intf ) == NULL )
222     {
223         msg_Err( p_intf, "cannot initialize VLCProc" );
224         b_error = true;
225         goto end;
226     }
227     if( VoutManager::instance( p_intf ) == NULL )
228     {
229         msg_Err( p_intf, "cannot instantiate VoutManager" );
230         b_error = true;
231         goto end;
232     }
233     if( ArtManager::instance( p_intf ) == NULL )
234     {
235         msg_Err( p_intf, "cannot instantiate ArtManager" );
236         b_error = true;
237         goto end;
238     }
239     if( ThemeRepository::instance( p_intf ) == NULL )
240     {
241         msg_Err( p_intf, "cannot instantiate ThemeRepository" );
242         b_error = true;
243         goto end;
244     }
245     if( Dialogs::instance( p_intf ) == NULL )
246     {
247         msg_Err( p_intf, "cannot instantiate qt4 dialogs provider" );
248         b_error = true;
249         goto end;
250     }
251
252     // Load a theme
253     skin_last = config_GetPsz( p_intf, "skins2-last" );
254     pLoader = new ThemeLoader( p_intf );
255
256     if( !skin_last || !pLoader->load( skin_last ) )
257     {
258         // No skins (not even the default one). let's quit
259         CmdQuit *pCmd = new CmdQuit( p_intf );
260         AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
261         pQueue->push( CmdGenericPtr( pCmd ) );
262         msg_Err( p_intf, "no skins found : exiting");
263     }
264
265     delete pLoader;
266     free( skin_last );
267
268     // Get the instance of OSLoop
269     loop = OSFactory::instance( p_intf )->getOSLoop();
270
271     // Signal the main thread this thread is now ready
272     p_intf->p_sys->b_error = false;
273     p_intf->p_sys->b_ready = true;
274     vlc_cond_signal( &p_intf->p_sys->init_wait );
275     vlc_mutex_unlock( &p_intf->p_sys->init_lock );
276
277     // Enter the main event loop
278     loop->run();
279
280     // Destroy OSLoop
281     OSFactory::instance( p_intf )->destroyOSLoop();
282
283     // save and delete the theme
284     if( p_intf->p_sys->p_theme )
285     {
286         p_intf->p_sys->p_theme->saveConfig();
287
288         delete p_intf->p_sys->p_theme;
289         p_intf->p_sys->p_theme = NULL;
290
291         msg_Dbg( p_intf, "current theme deleted" );
292     }
293
294     // save config file
295     config_SaveConfigFile( p_intf );
296
297 end:
298     // Destroy "singleton" objects
299     Dialogs::destroy( p_intf );
300     ThemeRepository::destroy( p_intf );
301     ArtManager::destroy( p_intf );
302     VoutManager::destroy( p_intf );
303     VlcProc::destroy( p_intf );
304     VarManager::destroy( p_intf );
305     Interpreter::destroy( p_intf );
306     AsyncQueue::destroy( p_intf );
307     OSFactory::destroy( p_intf );
308
309     if( b_error )
310     {
311         p_intf->p_sys->b_error = true;
312         p_intf->p_sys->b_ready = true;
313         vlc_cond_signal( &p_intf->p_sys->init_wait );
314         vlc_mutex_unlock( &p_intf->p_sys->init_lock );
315     }
316
317     vlc_restorecancel(canc);
318     return NULL;
319 }
320
321 static int  WindowOpen( vout_window_t *, const vout_window_cfg_t * );
322 static void WindowClose( vout_window_t * );
323 static int  WindowControl( vout_window_t *, int, va_list );
324
325 struct vout_window_sys_t
326 {
327     intf_thread_t*     pIntf;
328     vout_window_cfg_t  cfg;
329 };
330
331 static void WindowOpenLocal( intf_thread_t* pIntf, vlc_object_t *pObj )
332 {
333     vout_window_t* pWnd = (vout_window_t*)pObj;
334     int width = (int)pWnd->sys->cfg.width;
335     int height = (int)pWnd->sys->cfg.height;
336     VoutManager::instance( pIntf )->acceptWnd( pWnd, width, height );
337 }
338
339 static void WindowCloseLocal( intf_thread_t* pIntf, vlc_object_t *pObj )
340 {
341     vout_window_t* pWnd = (vout_window_t*)pObj;
342     VoutManager::instance( pIntf )->releaseWnd( pWnd );
343 }
344
345 static int WindowOpen( vout_window_t *pWnd, const vout_window_cfg_t *cfg )
346 {
347     if( cfg->type != VOUT_WINDOW_TYPE_INVALID )
348     {
349 #ifdef X11_SKINS
350         if( cfg->type != VOUT_WINDOW_TYPE_XID )
351 #else
352         if( cfg->type != VOUT_WINDOW_TYPE_HWND )
353 #endif
354             return VLC_EGENERIC;
355     }
356
357     vout_window_sys_t* sys;
358
359     vlc_mutex_lock( &skin_load.mutex );
360     intf_thread_t *pIntf = skin_load.intf;
361     if( pIntf )
362         vlc_object_hold( pIntf );
363     vlc_mutex_unlock( &skin_load.mutex );
364
365     if( pIntf == NULL )
366         return VLC_EGENERIC;
367
368     if( !var_InheritBool( pIntf, "skinned-video") ||
369         cfg->is_standalone )
370     {
371         vlc_object_release( pIntf );
372         return VLC_EGENERIC;
373     }
374
375     sys = (vout_window_sys_t*)calloc( 1, sizeof( *sys ) );
376     if( !sys )
377     {
378         vlc_object_release( pIntf );
379         return VLC_ENOMEM;
380     }
381
382     pWnd->sys = sys;
383     pWnd->sys->cfg = *cfg;
384     pWnd->sys->pIntf = pIntf;
385 #ifdef X11_SKINS
386     pWnd->type = VOUT_WINDOW_TYPE_XID;
387 #else
388     pWnd->type = VOUT_WINDOW_TYPE_HWND;
389 #endif
390     pWnd->control = WindowControl;
391
392     // force execution in the skins2 thread context
393     CmdExecuteBlock* cmd = new CmdExecuteBlock( pIntf, VLC_OBJECT( pWnd ),
394                                                 WindowOpenLocal );
395     CmdExecuteBlock::executeWait( CmdGenericPtr( cmd ) );
396
397 #ifdef X11_SKINS
398     pWnd->display.x11 = NULL;
399
400     if( !pWnd->handle.xid )
401 #else
402     if( !pWnd->handle.hwnd )
403 #endif
404     {
405         free( sys );
406         vlc_object_release( pIntf );
407         return VLC_EGENERIC;
408     }
409
410     return VLC_SUCCESS;
411 }
412
413 static void WindowClose( vout_window_t *pWnd )
414 {
415     vout_window_sys_t* sys = pWnd->sys;
416     intf_thread_t *pIntf = sys->pIntf;
417
418     // force execution in the skins2 thread context
419     CmdExecuteBlock* cmd = new CmdExecuteBlock( pIntf, VLC_OBJECT( pWnd ),
420                                                 WindowCloseLocal );
421     CmdExecuteBlock::executeWait( CmdGenericPtr( cmd ) );
422
423     vlc_object_release( sys->pIntf );
424     free( sys );
425 }
426
427 static int WindowControl( vout_window_t *pWnd, int query, va_list args )
428 {
429     vout_window_sys_t* sys = pWnd->sys;
430     intf_thread_t *pIntf = sys->pIntf;
431     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
432
433     switch( query )
434     {
435         case VOUT_WINDOW_SET_SIZE:
436         {
437             unsigned int i_width  = va_arg( args, unsigned int );
438             unsigned int i_height = va_arg( args, unsigned int );
439
440             if( i_width && i_height )
441             {
442                 // Post a vout resize command
443                 CmdResizeVout *pCmd =
444                     new CmdResizeVout( pIntf, pWnd,
445                                        (int)i_width, (int)i_height );
446                 pQueue->push( CmdGenericPtr( pCmd ) );
447             }
448             return VLC_EGENERIC;
449         }
450
451         case VOUT_WINDOW_SET_FULLSCREEN:
452         {
453             bool b_fullscreen = va_arg( args, int );
454
455             // Post a set fullscreen command
456             CmdSetFullscreen* pCmd =
457                 new CmdSetFullscreen( pIntf, pWnd, b_fullscreen );
458             pQueue->push( CmdGenericPtr( pCmd ) );
459             return VLC_SUCCESS;
460         }
461
462         case VOUT_WINDOW_SET_STATE:
463         {
464             unsigned i_arg = va_arg( args, unsigned );
465             unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
466
467             // Post a SetOnTop command
468             CmdSetOnTop* pCmd =
469                 new CmdSetOnTop( pIntf, on_top );
470             pQueue->push( CmdGenericPtr( pCmd ) );
471             return VLC_SUCCESS;
472         }
473
474         default:
475             msg_Dbg( pIntf, "control query not supported" );
476             return VLC_EGENERIC;
477     }
478 }
479
480 //---------------------------------------------------------------------------
481 // Module descriptor
482 //---------------------------------------------------------------------------
483 #define SKINS2_LAST      N_("Skin to use")
484 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
485 #define SKINS2_CONFIG      N_("Config of last used skin")
486 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
487         "This option is updated automatically, do not touch it." )
488 #define SKINS2_SYSTRAY      N_("Systray icon")
489 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
490 #define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
491 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
492 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
493 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
494     " if you want. This is mainly useful when moving windows does not behave" \
495     " correctly.")
496 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
497 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
498 #define SKINS2_VIDEO N_("Display video in a skinned window if any")
499 #define SKINS2_VIDEO_LONG N_( \
500     "When set to 'no', this parameter is intended to give old skins a chance" \
501     " to play back video even though no video tag is implemented")
502
503 vlc_module_begin ()
504     set_category( CAT_INTERFACE )
505     set_subcategory( SUBCAT_INTERFACE_MAIN )
506     add_loadfile( "skins2-last", "", SKINS2_LAST, SKINS2_LAST_LONG,
507                   true )
508     add_string( "skins2-config", "", SKINS2_CONFIG, SKINS2_CONFIG_LONG,
509                 true )
510         change_private ()
511 #ifdef _WIN32
512     add_bool( "skins2-systray", true, SKINS2_SYSTRAY,
513               SKINS2_SYSTRAY_LONG, false );
514     add_bool( "skins2-taskbar", true, SKINS2_TASKBAR,
515               SKINS2_TASKBAR_LONG, false );
516 #endif
517     add_bool( "skins2-transparency", false, SKINS2_TRANSPARENCY,
518               SKINS2_TRANSPARENCY_LONG, false );
519
520     add_bool( "skinned-playlist", true, SKINS2_PLAYLIST,
521               SKINS2_PLAYLIST_LONG, false );
522     add_bool( "skinned-video", true, SKINS2_VIDEO,
523               SKINS2_VIDEO_LONG, false );
524     set_shortname( N_("Skins"))
525     set_description( N_("Skinnable Interface") )
526     set_capability( "interface", 30 )
527     set_callbacks( Open, Close )
528     add_shortcut( "skins" )
529
530     add_submodule ()
531         set_capability( "vout window", 51 )
532         set_callbacks( WindowOpen, WindowClose )
533
534 vlc_module_end ()