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