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