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