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