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