skin_main.cpp 16 KB
Newer Older
1 2 3
/*****************************************************************************
 * skin_main.cpp
 *****************************************************************************
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 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
 *****************************************************************************/
Clément Stenac's avatar
Clément Stenac committed
24 25 26 27 28 29

#include <vlc/vlc.h>
#include <vlc_input.h>
#include <vlc_demux.h>
#include <vlc_playlist.h>

30 31 32 33 34 35
#include "dialogs.hpp"
#include "os_factory.hpp"
#include "os_loop.hpp"
#include "var_manager.hpp"
#include "vlcproc.hpp"
#include "theme_loader.hpp"
36
#include "theme.hpp"
37
#include "theme_repository.hpp"
Cyril Deguet's avatar
Cyril Deguet committed
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 46 47 48 49 50 51 52 53

//---------------------------------------------------------------------------
// Exported interface functions.
//---------------------------------------------------------------------------
#ifdef WIN32_SKINS
extern "C" __declspec( dllexport )
    int __VLC_SYMBOL( vlc_entry ) ( module_t *p_module );
#endif


//---------------------------------------------------------------------------
54
// Local prototypes
55 56 57 58 59
//---------------------------------------------------------------------------
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void Run   ( intf_thread_t * );

60 61 62 63
static int DemuxOpen( vlc_object_t * );
static int Demux( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

64 65 66 67 68 69
//---------------------------------------------------------------------------
// Prototypes for configuration callbacks
//---------------------------------------------------------------------------
static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam );
70 71 72
static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam );
73

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

//---------------------------------------------------------------------------
// Open: initialize interface
//---------------------------------------------------------------------------
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    // Allocate instance and initialize some members
    p_intf->p_sys = (intf_sys_t *) malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        msg_Err( p_intf, "out of memory" );
        return( VLC_ENOMEM );
    };

    p_intf->pf_run = Run;

    // Suscribe to messages bank
93
    p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
94 95 96 97 98 99 100

    p_intf->p_sys->p_input = NULL;
    p_intf->p_sys->p_playlist = (playlist_t *)vlc_object_find( p_intf,
        VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
    if( p_intf->p_sys->p_playlist == NULL )
    {
        msg_Err( p_intf, "No playlist object found" );
101
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
102 103 104 105 106 107 108
        return VLC_EGENERIC;
    }

    // Initialize "singleton" objects
    p_intf->p_sys->p_logger = NULL;
    p_intf->p_sys->p_queue = NULL;
    p_intf->p_sys->p_dialogs = NULL;
Cyril Deguet's avatar
Cyril Deguet committed
109
    p_intf->p_sys->p_interpreter = NULL;
110 111 112 113
    p_intf->p_sys->p_osFactory = NULL;
    p_intf->p_sys->p_osLoop = NULL;
    p_intf->p_sys->p_varManager = NULL;
    p_intf->p_sys->p_vlcProc = NULL;
114
    p_intf->p_sys->p_repository = NULL;
115 116 117 118

    // No theme yet
    p_intf->p_sys->p_theme = NULL;

119 120 121
    // Create a variable to be notified of skins to be loaded
    var_Create( p_intf, "skin-to-load", VLC_VAR_STRING );

122
    // Initialize singletons
123 124
    if( OSFactory::instance( p_intf ) == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
125
        msg_Err( p_intf, "cannot initialize OSFactory" );
126 127
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
128 129
        return VLC_EGENERIC;
    }
130 131
    if( AsyncQueue::instance( p_intf ) == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
132
        msg_Err( p_intf, "cannot initialize AsyncQueue" );
133 134
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
135 136
        return VLC_EGENERIC;
    }
Cyril Deguet's avatar
Cyril Deguet committed
137 138
    if( Interpreter::instance( p_intf ) == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
139
        msg_Err( p_intf, "cannot instanciate Interpreter" );
140 141
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
Cyril Deguet's avatar
Cyril Deguet committed
142 143
        return VLC_EGENERIC;
    }
144 145
    if( VarManager::instance( p_intf ) == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
146
        msg_Err( p_intf, "cannot instanciate VarManager" );
147 148
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
149 150 151 152
        return VLC_EGENERIC;
    }
    if( VlcProc::instance( p_intf ) == NULL )
    {
Clément Stenac's avatar
Clément Stenac committed
153
        msg_Err( p_intf, "cannot initialize VLCProc" );
154 155
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
156 157 158
        return VLC_EGENERIC;
    }
    Dialogs::instance( p_intf );
159
    ThemeRepository::instance( p_intf );
160

161
    // We support play on start
162 163
    p_intf->b_play = VLC_TRUE;

164 165 166 167 168 169 170 171 172 173 174 175
    return( VLC_SUCCESS );
}

//---------------------------------------------------------------------------
// Close: destroy interface
//---------------------------------------------------------------------------
static void Close( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;

    // Destroy "singleton" objects
    OSFactory::instance( p_intf )->destroyOSLoop();
176
    ThemeRepository::destroy( p_intf );
177
    Dialogs::destroy( p_intf );
Cyril Deguet's avatar
Cyril Deguet committed
178
    Interpreter::destroy( p_intf );
179 180
    AsyncQueue::destroy( p_intf );
    VarManager::destroy( p_intf );
181
    VlcProc::destroy( p_intf );
182
    OSFactory::destroy( p_intf );
183 184 185 186 187 188

    if( p_intf->p_sys->p_playlist )
    {
        vlc_object_release( p_intf->p_sys->p_playlist );
    }

189
    // Unsubscribe from messages bank
190 191 192 193 194 195 196 197 198 199 200 201 202 203
    msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );

    // Destroy structure
    free( p_intf->p_sys );
}


//---------------------------------------------------------------------------
// Run: main loop
//---------------------------------------------------------------------------
static void Run( intf_thread_t *p_intf )
{
    // Load a theme
    ThemeLoader *pLoader = new ThemeLoader( p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
204
    char *skin_last = config_GetPsz( p_intf, "skins2-last" );
205

206
    if( !skin_last || !*skin_last || !pLoader->load( skin_last ) )
207
    {
208 209 210 211 212 213 214 215
        // Get the resource path and try to load the default skin
        OSFactory *pOSFactory = OSFactory::instance( p_intf );
        const list<string> &resPath = pOSFactory->getResourcePath();
        const string &sep = pOSFactory->getDirSeparator();

        list<string>::const_iterator it;
        for( it = resPath.begin(); it != resPath.end(); it++ )
        {
216
            string path = (*it) + sep + "default.vlt";
217 218 219 220 221 222 223
            if( pLoader->load( path ) )
            {
                // Theme loaded successfully
                break;
            }
        }
        if( it == resPath.end() )
224
        {
225
            // Last chance: the user can select a new theme file
226
            if( Dialogs::instance( p_intf ) )
227
            {
228 229 230
                CmdDlgChangeSkin *pCmd = new CmdDlgChangeSkin( p_intf );
                AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
                pQueue->push( CmdGenericPtr( pCmd ) );
231 232 233 234 235 236 237
            }
            else
            {
                // No dialogs provider, just quit...
                CmdQuit *pCmd = new CmdQuit( p_intf );
                AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
                pQueue->push( CmdGenericPtr( pCmd ) );
238
                msg_Err( p_intf,
Clément Stenac's avatar
Clément Stenac committed
239
                         "cannot show the \"open skin\" dialog: exiting...");
240
            }
241 242 243 244 245 246 247 248 249 250 251
        }
    }
    delete pLoader;

    if( skin_last )
    {
        free( skin_last );
    }

    // Get the instance of OSLoop
    OSLoop *loop = OSFactory::instance( p_intf )->getOSLoop();
252

253 254 255 256 257 258 259 260
    // Check if we need to start playing
    if( p_intf->b_play )
    {
        playlist_t *p_playlist =
            (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
                                           FIND_ANYWHERE );
        if( p_playlist )
        {
261
            playlist_Control( p_playlist, PLAYLIST_AUTOPLAY, VLC_FALSE );
262 263 264 265
            vlc_object_release( p_playlist );
        }
    }

266
    // Enter the main event loop
267 268
    loop->run();

269
    // Delete the theme and save the configuration of the windows
270 271
    if( p_intf->p_sys->p_theme )
    {
272
        p_intf->p_sys->p_theme->saveConfig();
273 274 275 276 277
        delete p_intf->p_sys->p_theme;
        p_intf->p_sys->p_theme = NULL;
    }
}

278 279 280 281 282 283 284 285 286 287 288 289 290 291

//---------------------------------------------------------------------------
// DemuxOpen: initialize demux
//---------------------------------------------------------------------------
static int DemuxOpen( vlc_object_t *p_this )
{
    demux_t *p_demux = (demux_t*)p_this;
    intf_thread_t *p_intf;
    char *ext;

    // Needed callbacks
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = DemuxControl;

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 298 299 300 301 302 303 304
    {
        return VLC_EGENERIC;
    }

    p_intf = (intf_thread_t *)vlc_object_find( p_this, VLC_OBJECT_INTF,
                                               FIND_ANYWHERE );
    if( p_intf != NULL )
    {
        // Do nothing is skins2 is not the main interface
305
        if( var_Type( p_intf, "skin-to-load" ) == VLC_VAR_STRING )
306 307 308 309 310 311 312
        {
            playlist_t *p_playlist =
                (playlist_t *) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
            if( p_playlist != NULL )
            {
                // Make sure the item is deleted afterwards
313 314
                /// \bug does not always work
                p_playlist->status.p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
315 316 317 318
                vlc_object_release( p_playlist );
            }

            vlc_value_t val;
319
            val.psz_string = p_demux->psz_path;
320 321 322 323 324 325 326
            var_Set( p_intf, "skin-to-load", val );
        }
        else
        {
            msg_Warn( p_this,
                      "skin could not be loaded (not using skins2 intf)" );
        }
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
        vlc_object_release( p_intf );
    }

    return VLC_SUCCESS;
}


//---------------------------------------------------------------------------
// Demux: return EOF
//---------------------------------------------------------------------------
static int Demux( demux_t *p_demux )
{
    return 0;
}


//---------------------------------------------------------------------------
// DemuxControl
//---------------------------------------------------------------------------
static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
{
    return demux2_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
}


353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
//---------------------------------------------------------------------------
// Callbacks
//---------------------------------------------------------------------------

/// Callback for the systray configuration option
static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam )
{
    intf_thread_t *pIntf =
        (intf_thread_t*)vlc_object_find( pObj, VLC_OBJECT_INTF, FIND_ANYWHERE );

    if( pIntf == NULL )
    {
        return VLC_EGENERIC;
    }

370 371
    // Check that we found the correct interface (same check as for the demux)
    if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
372
    {
373 374 375 376 377 378 379 380 381 382 383
        AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
        if( newVal.b_bool )
        {
            CmdAddInTray *pCmd = new CmdAddInTray( pIntf );
            pQueue->push( CmdGenericPtr( pCmd ) );
        }
        else
        {
            CmdRemoveFromTray *pCmd = new CmdRemoveFromTray( pIntf );
            pQueue->push( CmdGenericPtr( pCmd ) );
        }
384 385 386
    }

    vlc_object_release( pIntf );
387
    return VLC_SUCCESS;
388 389 390
}


391 392 393 394 395 396 397 398 399 400 401 402 403
/// Callback for the systray configuration option
static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam )
{
    intf_thread_t *pIntf =
        (intf_thread_t*)vlc_object_find( pObj, VLC_OBJECT_INTF, FIND_ANYWHERE );

    if( pIntf == NULL )
    {
        return VLC_EGENERIC;
    }

404 405
    // Check that we found the correct interface (same check as for the demux)
    if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
406
    {
407 408 409 410 411 412 413 414 415 416 417
        AsyncQueue *pQueue = AsyncQueue::instance( pIntf );
        if( newVal.b_bool )
        {
            CmdAddInTaskBar *pCmd = new CmdAddInTaskBar( pIntf );
            pQueue->push( CmdGenericPtr( pCmd ) );
        }
        else
        {
            CmdRemoveFromTaskBar *pCmd = new CmdRemoveFromTaskBar( pIntf );
            pQueue->push( CmdGenericPtr( pCmd ) );
        }
418 419 420
    }

    vlc_object_release( pIntf );
421
    return VLC_SUCCESS;
422 423 424
}


425 426 427
//---------------------------------------------------------------------------
// Module descriptor
//---------------------------------------------------------------------------
Clément Stenac's avatar
Clément Stenac committed
428 429
#define SKINS2_LAST      N_("Skin to use")
#define SKINS2_LAST_LONG N_("Path to the skin to use.")
430
#define SKINS2_CONFIG      N_("Config of last used skin")
431 432 433 434
#define SKINS2_CONFIG_LONG N_("Windows configuration of the last skin used. " \
        "This option is updated automatically, do not touch it." )
#define SKINS2_SYSTRAY      N_("Systray icon")
#define SKINS2_SYSTRAY_LONG N_("Show a systray icon for VLC")
435 436
#define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
#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 440
    " if you want. This is mainly useful when moving windows does not behave" \
    " correctly.")
Antoine Cellerier's avatar
Antoine Cellerier committed
441 442
#define SKINS2_PLAYLIST N_("Use a skinned playlist")
#define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
443 444

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
445
    set_category( CAT_INTERFACE );
Clément Stenac's avatar
Clément Stenac committed
446
    set_subcategory( SUBCAT_INTERFACE_MAIN );
447 448
    add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
              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 458
    add_bool( "skins2-taskbar", VLC_TRUE, onTaskBarChange, SKINS2_TASKBAR,
              SKINS2_TASKBAR_LONG, VLC_FALSE );
459
    add_bool( "skins2-transparency", VLC_FALSE, NULL, SKINS2_TRANSPARENCY,
460 461
              SKINS2_TRANSPARENCY_LONG, VLC_FALSE );
#endif
462

Antoine Cellerier's avatar
Antoine Cellerier committed
463 464
    add_bool( "skinned-playlist", VLC_TRUE, NULL, SKINS2_PLAYLIST,
              SKINS2_PLAYLIST_LONG, VLC_FALSE );
465
    set_shortname( _("Skins"));
466 467 468
    set_description( _("Skinnable Interface") );
    set_capability( "interface", 30 );
    set_callbacks( Open, Close );
469
    add_shortcut( "skins" );
470 471 472 473 474 475

    add_submodule();
        set_description( _("Skins loader demux") );
        set_capability( "demux2", 5 );
        set_callbacks( DemuxOpen, NULL );

476
vlc_module_end();