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