]> git.sesse.net Git - vlc/blob - modules/gui/skins2/src/skin_main.cpp
9590e777603cd16181fe30095db93aa96ce4a533
[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
290     // cannot be called in "Close", because it refcounts skins2
291     Dialogs::destroy( p_intf );
292
293     vlc_restorecancel(canc);
294 }
295
296
297 // Callbacks for vout requests
298 static int WindowOpen( vlc_object_t *p_this )
299 {
300     vout_window_t *pWnd = (vout_window_t *)p_this;
301     intf_thread_t *pIntf = (intf_thread_t *)
302         vlc_object_find_name( p_this, "skins2", FIND_ANYWHERE );
303
304     if( pIntf == NULL )
305         return VLC_EGENERIC;
306
307     /* FIXME: most probably not thread-safe,
308      * albeit no worse than ever before */
309     pWnd->handle.hwnd = VlcProc::getWindow( pIntf, pWnd->vout,
310                                        &pWnd->pos_x, &pWnd->pos_y,
311                                        &pWnd->width, &pWnd->height );
312     if( pWnd->handle.hwnd )
313     {
314         pWnd->p_private = pIntf;
315         pWnd->control = &VlcProc::controlWindow;
316         return VLC_SUCCESS;
317     }
318     else
319     {
320         return VLC_EGENERIC;
321     }
322 }
323
324 static void WindowClose( vlc_object_t *p_this )
325 {
326     vout_window_t *pWnd = (vout_window_t *)p_this;
327     intf_thread_t *pIntf = (intf_thread_t *)p_this->p_private;
328
329     VlcProc::releaseWindow( pIntf, pWnd->handle.hwnd );
330 }
331
332 //---------------------------------------------------------------------------
333 // DemuxOpen: initialize demux
334 //---------------------------------------------------------------------------
335 static int DemuxOpen( vlc_object_t *p_this )
336 {
337     demux_t *p_demux = (demux_t*)p_this;
338     intf_thread_t *p_intf;
339     char *ext;
340
341     // Needed callbacks
342     p_demux->pf_demux   = Demux;
343     p_demux->pf_control = DemuxControl;
344
345     // Test that we have a valid .vlt or .wsz file, based on the extension
346     // TODO: an actual check of the contents would be better...
347     if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
348         ( strcasecmp( ext, ".vlt" ) && strcasecmp( ext, ".wsz" ) ) )
349     {
350         return VLC_EGENERIC;
351     }
352
353     vlc_mutex_lock( &skin_load.mutex );
354     p_intf = skin_load.intf;
355     if( p_intf )
356         vlc_object_hold( p_intf );
357     vlc_mutex_unlock( &skin_load.mutex );
358
359     if( p_intf != NULL )
360     {
361         playlist_t *p_playlist = pl_Hold( p_this );
362         // Make sure the item is deleted afterwards
363         /// \bug does not always work
364         playlist_CurrentPlayingItem( p_playlist )->i_flags |= PLAYLIST_REMOVE_FLAG;
365         vlc_object_release( p_playlist );
366
367         var_SetString( p_intf, "skin-to-load", p_demux->psz_path );
368         vlc_object_release( p_intf );
369     }
370     else
371     {
372         msg_Warn( p_this,
373                   "skin could not be loaded (not using skins2 intf)" );
374     }
375
376     return VLC_SUCCESS;
377 }
378
379
380 //---------------------------------------------------------------------------
381 // Demux: return EOF
382 //---------------------------------------------------------------------------
383 static int Demux( demux_t *p_demux )
384 {
385     return 0;
386 }
387
388
389 //---------------------------------------------------------------------------
390 // DemuxControl
391 //---------------------------------------------------------------------------
392 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
393 {
394     return demux_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
395 }
396
397
398 //---------------------------------------------------------------------------
399 // Callbacks
400 //---------------------------------------------------------------------------
401
402 /// Callback for the systray configuration option
403 static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
404                             vlc_value_t oldVal, vlc_value_t newVal,
405                             void *pParam )
406 {
407     intf_thread_t *pIntf;
408
409     vlc_mutex_lock( &skin_load.mutex );
410     pIntf = skin_load.intf;
411     if( pIntf )
412         vlc_object_hold( pIntf );
413     vlc_mutex_unlock( &skin_load.mutex );
414
415     if( pIntf == NULL )
416     {
417         return VLC_EGENERIC;
418     }
419
420     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
421     if( newVal.b_bool )
422     {
423         CmdAddInTray *pCmd = new CmdAddInTray( pIntf );
424         pQueue->push( CmdGenericPtr( pCmd ) );
425     }
426     else
427     {
428         CmdRemoveFromTray *pCmd = new CmdRemoveFromTray( pIntf );
429         pQueue->push( CmdGenericPtr( pCmd ) );
430     }
431
432     vlc_object_release( pIntf );
433     return VLC_SUCCESS;
434 }
435
436
437 /// Callback for the systray configuration option
438 static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
439                             vlc_value_t oldVal, vlc_value_t newVal,
440                             void *pParam )
441 {
442     intf_thread_t *pIntf;
443
444     vlc_mutex_lock( &skin_load.mutex );
445     pIntf = skin_load.intf;
446     if( pIntf )
447         vlc_object_hold( pIntf );
448     vlc_mutex_unlock( &skin_load.mutex );
449
450     if( pIntf == NULL )
451     {
452         return VLC_EGENERIC;
453     }
454
455     AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
456     if( newVal.b_bool )
457     {
458         CmdAddInTaskBar *pCmd = new CmdAddInTaskBar( pIntf );
459         pQueue->push( CmdGenericPtr( pCmd ) );
460     }
461     else
462     {
463         CmdRemoveFromTaskBar *pCmd = new CmdRemoveFromTaskBar( pIntf );
464         pQueue->push( CmdGenericPtr( pCmd ) );
465     }
466
467     vlc_object_release( pIntf );
468     return VLC_SUCCESS;
469 }
470
471
472 //---------------------------------------------------------------------------
473 // Module descriptor
474 //---------------------------------------------------------------------------
475 #define SKINS2_LAST      N_("Skin to use")
476 #define SKINS2_LAST_LONG N_("Path to the skin to use.")
477 #define SKINS2_CONFIG      N_("Config of last used skin")
478 #define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
479         "This option is updated automatically, do not touch it." )
480 #define SKINS2_SYSTRAY      N_("Systray icon")
481 #define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
482 #define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
483 #define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
484 #define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
485 #define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
486     " if you want. This is mainly useful when moving windows does not behave" \
487     " correctly.")
488 #define SKINS2_PLAYLIST N_("Use a skinned playlist")
489 #define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
490
491 vlc_module_begin ()
492     set_category( CAT_INTERFACE )
493     set_subcategory( SUBCAT_INTERFACE_MAIN )
494     add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
495               true )
496         change_autosave ()
497     add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
498                 true )
499         change_autosave ()
500         change_internal ()
501 #ifdef WIN32
502     add_bool( "skins2-systray", false, onSystrayChange, SKINS2_SYSTRAY,
503               SKINS2_SYSTRAY_LONG, false );
504     add_bool( "skins2-taskbar", true, onTaskBarChange, SKINS2_TASKBAR,
505               SKINS2_TASKBAR_LONG, false );
506     add_bool( "skins2-transparency", false, NULL, SKINS2_TRANSPARENCY,
507               SKINS2_TRANSPARENCY_LONG, false );
508 #endif
509
510     add_bool( "skinned-playlist", true, NULL, SKINS2_PLAYLIST,
511               SKINS2_PLAYLIST_LONG, false );
512     set_shortname( N_("Skins"))
513     set_description( N_("Skinnable Interface") )
514     set_capability( "interface", 30 )
515     set_callbacks( Open, Close )
516     add_shortcut( "skins" )
517
518     add_submodule ()
519 #ifndef WIN32
520         set_capability( "xwindow", 51 )
521 #else
522         set_capability( "hwnd", 51 )
523 #endif
524         set_callbacks( WindowOpen, WindowClose )
525
526     add_submodule ()
527         set_description( N_("Skins loader demux") )
528         set_capability( "demux", 5 )
529         set_callbacks( DemuxOpen, NULL )
530
531 vlc_module_end ()