skins2: add OS/2 support
[vlc.git] / modules / gui / skins2 / src / skin_main.cpp
blob52200b16674481a27fc56d71a6263fded880e163
1 /*****************************************************************************
2 * skin_main.cpp
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
5 * $Id$
7 * Authors: Cyril Deguet <asmax@via.ecp.fr>
8 * Olivier Teulière <ipkiss@via.ecp.fr>
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.
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.
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
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>
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"
59 //---------------------------------------------------------------------------
60 // Local prototypes
61 //---------------------------------------------------------------------------
62 static int Open ( vlc_object_t * );
63 static void Close ( vlc_object_t * );
64 static void *Run ( void * );
66 static struct
68 intf_thread_t *intf;
69 vlc_mutex_t mutex;
70 } skin_load = { NULL, VLC_STATIC_MUTEX };
72 //---------------------------------------------------------------------------
73 // Open: initialize interface
74 //---------------------------------------------------------------------------
75 static int Open( vlc_object_t *p_this )
77 intf_thread_t *p_intf = (intf_thread_t *)p_this;
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;
84 p_intf->p_sys->p_input = NULL;
85 p_intf->p_sys->p_playlist = pl_Get( p_intf );
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;
99 // No theme yet
100 p_intf->p_sys->p_theme = NULL;
102 vlc_mutex_init( &p_intf->p_sys->init_lock );
103 vlc_cond_init( &p_intf->p_sys->init_wait );
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;
109 if( vlc_clone( &p_intf->p_sys->thread, Run, p_intf,
110 VLC_THREAD_PRIORITY_LOW ) )
112 vlc_mutex_unlock( &p_intf->p_sys->init_lock );
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;
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 );
124 if( p_intf->p_sys->b_error )
126 vlc_join( p_intf->p_sys->thread, NULL );
128 vlc_mutex_destroy( &p_intf->p_sys->init_lock );
129 vlc_cond_destroy( &p_intf->p_sys->init_wait );
131 free( p_intf->p_sys );
132 return VLC_EGENERIC;
135 vlc_mutex_lock( &skin_load.mutex );
136 skin_load.intf = p_intf;
137 vlc_mutex_unlock( &skin_load.mutex );
139 return VLC_SUCCESS;
142 //---------------------------------------------------------------------------
143 // Close: destroy interface
144 //---------------------------------------------------------------------------
145 static void Close( vlc_object_t *p_this )
147 intf_thread_t *p_intf = (intf_thread_t *)p_this;
149 msg_Dbg( p_intf, "closing skins2 module" );
151 /* Terminate input to ensure that our window provider is released. */
152 playlist_Deactivate( p_intf->p_sys->p_playlist );
154 vlc_mutex_lock( &skin_load.mutex );
155 skin_load.intf = NULL;
156 vlc_mutex_unlock( &skin_load.mutex);
158 AsyncQueue *pQueue = p_intf->p_sys->p_queue;
159 if( pQueue )
161 CmdGeneric *pCmd = new CmdExitLoop( p_intf );
162 if( pCmd )
163 pQueue->push( CmdGenericPtr( pCmd ) );
165 else
167 msg_Err( p_intf, "thread found already stopped (weird!)" );
170 vlc_join( p_intf->p_sys->thread, NULL );
172 vlc_mutex_destroy( &p_intf->p_sys->init_lock );
173 vlc_cond_destroy( &p_intf->p_sys->init_wait );
175 // Destroy structure
176 free( p_intf->p_sys );
180 //---------------------------------------------------------------------------
181 // Run: main loop
182 //---------------------------------------------------------------------------
183 static void *Run( void * p_obj )
185 int canc = vlc_savecancel();
187 intf_thread_t *p_intf = (intf_thread_t *)p_obj;
189 bool b_error = false;
190 char *skin_last = NULL;
191 ThemeLoader *pLoader = NULL;
192 OSLoop *loop = NULL;
194 vlc_mutex_lock( &p_intf->p_sys->init_lock );
196 // Initialize singletons
197 if( OSFactory::instance( p_intf ) == NULL )
199 msg_Err( p_intf, "cannot initialize OSFactory" );
200 b_error = true;
201 goto end;
203 if( AsyncQueue::instance( p_intf ) == NULL )
205 msg_Err( p_intf, "cannot initialize AsyncQueue" );
206 b_error = true;
207 goto end;
209 if( Interpreter::instance( p_intf ) == NULL )
211 msg_Err( p_intf, "cannot instantiate Interpreter" );
212 b_error = true;
213 goto end;
215 if( VarManager::instance( p_intf ) == NULL )
217 msg_Err( p_intf, "cannot instantiate VarManager" );
218 b_error = true;
219 goto end;
221 if( VlcProc::instance( p_intf ) == NULL )
223 msg_Err( p_intf, "cannot initialize VLCProc" );
224 b_error = true;
225 goto end;
227 if( VoutManager::instance( p_intf ) == NULL )
229 msg_Err( p_intf, "cannot instantiate VoutManager" );
230 b_error = true;
231 goto end;
233 if( ArtManager::instance( p_intf ) == NULL )
235 msg_Err( p_intf, "cannot instantiate ArtManager" );
236 b_error = true;
237 goto end;
239 if( ThemeRepository::instance( p_intf ) == NULL )
241 msg_Err( p_intf, "cannot instantiate ThemeRepository" );
242 b_error = true;
243 goto end;
245 if( Dialogs::instance( p_intf ) == NULL )
247 msg_Err( p_intf, "cannot instantiate qt4 dialogs provider" );
248 b_error = true;
249 goto end;
252 // Load a theme
253 skin_last = config_GetPsz( p_intf, "skins2-last" );
254 pLoader = new ThemeLoader( p_intf );
256 if( !skin_last || !pLoader->load( skin_last ) )
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");
265 delete pLoader;
266 free( skin_last );
268 // Get the instance of OSLoop
269 loop = OSFactory::instance( p_intf )->getOSLoop();
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 );
277 // Enter the main event loop
278 loop->run();
280 // Destroy OSLoop
281 OSFactory::instance( p_intf )->destroyOSLoop();
283 // save and delete the theme
284 if( p_intf->p_sys->p_theme )
286 p_intf->p_sys->p_theme->saveConfig();
288 delete p_intf->p_sys->p_theme;
289 p_intf->p_sys->p_theme = NULL;
291 msg_Dbg( p_intf, "current theme deleted" );
294 // save config file
295 config_SaveConfigFile( p_intf );
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 );
309 if( b_error )
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 );
317 vlc_restorecancel(canc);
318 return NULL;
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 );
325 struct vout_window_sys_t
327 intf_thread_t* pIntf;
328 vout_window_cfg_t cfg;
331 static void WindowOpenLocal( intf_thread_t* pIntf, vlc_object_t *pObj )
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 );
339 static void WindowCloseLocal( intf_thread_t* pIntf, vlc_object_t *pObj )
341 vout_window_t* pWnd = (vout_window_t*)pObj;
342 VoutManager::instance( pIntf )->releaseWnd( pWnd );
345 static int WindowOpen( vout_window_t *pWnd, const vout_window_cfg_t *cfg )
347 vout_window_sys_t* sys;
349 vlc_mutex_lock( &skin_load.mutex );
350 intf_thread_t *pIntf = skin_load.intf;
351 if( pIntf )
352 vlc_object_hold( pIntf );
353 vlc_mutex_unlock( &skin_load.mutex );
355 if( pIntf == NULL )
356 return VLC_EGENERIC;
358 if( !var_InheritBool( pIntf, "skinned-video") ||
359 cfg->is_standalone )
361 vlc_object_release( pIntf );
362 return VLC_EGENERIC;
365 sys = (vout_window_sys_t*)calloc( 1, sizeof( *sys ) );
366 if( !sys )
368 vlc_object_release( pIntf );
369 return VLC_ENOMEM;
372 pWnd->sys = sys;
373 pWnd->sys->cfg = *cfg;
374 pWnd->sys->pIntf = pIntf;
375 pWnd->control = WindowControl;
377 // force execution in the skins2 thread context
378 CmdExecuteBlock* cmd = new CmdExecuteBlock( pIntf, VLC_OBJECT( pWnd ),
379 WindowOpenLocal );
380 CmdExecuteBlock::executeWait( CmdGenericPtr( cmd ) );
382 #ifdef X11_SKINS
383 if( !pWnd->handle.xid )
384 #else
385 if( !pWnd->handle.hwnd )
386 #endif
388 free( sys );
389 vlc_object_release( pIntf );
390 return VLC_EGENERIC;
393 return VLC_SUCCESS;
396 static void WindowClose( vout_window_t *pWnd )
398 vout_window_sys_t* sys = pWnd->sys;
399 intf_thread_t *pIntf = sys->pIntf;
401 // force execution in the skins2 thread context
402 CmdExecuteBlock* cmd = new CmdExecuteBlock( pIntf, VLC_OBJECT( pWnd ),
403 WindowCloseLocal );
404 CmdExecuteBlock::executeWait( CmdGenericPtr( cmd ) );
406 vlc_object_release( sys->pIntf );
407 free( sys );
410 static int WindowControl( vout_window_t *pWnd, int query, va_list args )
412 vout_window_sys_t* sys = pWnd->sys;
413 intf_thread_t *pIntf = sys->pIntf;
414 AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
416 switch( query )
418 case VOUT_WINDOW_SET_SIZE:
420 unsigned int i_width = va_arg( args, unsigned int );
421 unsigned int i_height = va_arg( args, unsigned int );
423 if( i_width && i_height )
425 // Post a vout resize command
426 CmdResizeVout *pCmd =
427 new CmdResizeVout( pIntf, pWnd,
428 (int)i_width, (int)i_height );
429 pQueue->push( CmdGenericPtr( pCmd ) );
431 return VLC_EGENERIC;
434 case VOUT_WINDOW_SET_FULLSCREEN:
436 bool b_fullscreen = va_arg( args, int );
438 // Post a set fullscreen command
439 CmdSetFullscreen* pCmd =
440 new CmdSetFullscreen( pIntf, pWnd, b_fullscreen );
441 pQueue->push( CmdGenericPtr( pCmd ) );
442 return VLC_SUCCESS;
445 case VOUT_WINDOW_SET_STATE:
447 unsigned i_arg = va_arg( args, unsigned );
448 unsigned on_top = i_arg & VOUT_WINDOW_STATE_ABOVE;
450 // Post a SetOnTop command
451 CmdSetOnTop* pCmd =
452 new CmdSetOnTop( pIntf, on_top );
453 pQueue->push( CmdGenericPtr( pCmd ) );
454 return VLC_SUCCESS;
457 default:
458 msg_Dbg( pIntf, "control query not supported" );
459 return VLC_EGENERIC;
463 //---------------------------------------------------------------------------
464 // Module descriptor
465 //---------------------------------------------------------------------------
466 #define SKINS2_LAST N_("Skin to use")
467 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
468 #define SKINS2_CONFIG N_("Config of last used skin")
469 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
470 "This option is updated automatically, do not touch it." )
471 #define SKINS2_SYSTRAY N_("Systray icon")
472 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
473 #define SKINS2_TASKBAR N_("Show VLC on the taskbar")
474 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
475 #define SKINS2_TRANSPARENCY N_("Enable transparency effects")
476 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
477 " if you want. This is mainly useful when moving windows does not behave" \
478 " correctly.")
479 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
480 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
481 #define SKINS2_VIDEO N_("Display video in a skinned window if any")
482 #define SKINS2_VIDEO_LONG N_( \
483 "When set to 'no', this parameter is intended to give old skins a chance" \
484 " to play back video even though no video tag is implemented")
486 vlc_module_begin ()
487 set_category( CAT_INTERFACE )
488 set_subcategory( SUBCAT_INTERFACE_MAIN )
489 add_loadfile( "skins2-last", "", SKINS2_LAST, SKINS2_LAST_LONG,
490 true )
491 add_string( "skins2-config", "", SKINS2_CONFIG, SKINS2_CONFIG_LONG,
492 true )
493 change_private ()
494 #ifdef WIN32
495 add_bool( "skins2-systray", true, SKINS2_SYSTRAY,
496 SKINS2_SYSTRAY_LONG, false );
497 add_bool( "skins2-taskbar", true, SKINS2_TASKBAR,
498 SKINS2_TASKBAR_LONG, false );
499 #endif
500 add_bool( "skins2-transparency", false, SKINS2_TRANSPARENCY,
501 SKINS2_TRANSPARENCY_LONG, false );
503 add_bool( "skinned-playlist", true, SKINS2_PLAYLIST,
504 SKINS2_PLAYLIST_LONG, false );
505 add_bool( "skinned-video", true, SKINS2_VIDEO,
506 SKINS2_VIDEO_LONG, false );
507 set_shortname( N_("Skins"))
508 set_description( N_("Skinnable Interface") )
509 set_capability( "interface", 30 )
510 set_callbacks( Open, Close )
511 add_shortcut( "skins" )
513 add_submodule ()
514 #if defined( WIN32 ) || defined( __OS2__ )
515 set_capability( "vout window hwnd", 51 )
516 #else
517 set_capability( "vout window xid", 51 )
518 #endif
519 set_callbacks( WindowOpen, WindowClose )
521 vlc_module_end ()