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