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