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