skin_main.cpp 16.2 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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

29
#include <vlc_common.h>
30
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
31 32 33
#include <vlc_input.h>
#include <vlc_demux.h>
#include <vlc_playlist.h>
34
#include <vlc_window.h>
Clément Stenac's avatar
Clément Stenac committed
35

36 37 38 39 40 41
#include "dialogs.hpp"
#include "os_factory.hpp"
#include "os_loop.hpp"
#include "var_manager.hpp"
#include "vlcproc.hpp"
#include "theme_loader.hpp"
42
#include "theme.hpp"
43
#include "theme_repository.hpp"
Cyril Deguet's avatar
Cyril Deguet committed
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 52 53 54 55 56 57 58 59

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


//---------------------------------------------------------------------------
60
// Local prototypes
61 62 63 64 65
//---------------------------------------------------------------------------
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void Run   ( intf_thread_t * );

66 67 68 69
static int DemuxOpen( vlc_object_t * );
static int Demux( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

70 71 72 73 74 75
//---------------------------------------------------------------------------
// Prototypes for configuration callbacks
//---------------------------------------------------------------------------
static int onSystrayChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam );
76 77 78
static int onTaskBarChange( vlc_object_t *pObj, const char *pVariable,
                            vlc_value_t oldVal, vlc_value_t newVal,
                            void *pParam );
79

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

//---------------------------------------------------------------------------
// 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
99
    p_intf->p_sys->p_sub = msg_Subscribe( p_intf );
100 101

    p_intf->p_sys->p_input = NULL;
102
    p_intf->p_sys->p_playlist = pl_Yield( p_intf );
103 104 105 106 107

    // 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
108
    p_intf->p_sys->p_interpreter = NULL;
109 110 111 112
    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;
113
    p_intf->p_sys->p_repository = NULL;
114 115 116 117

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

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

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

    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();
172
    ThemeRepository::destroy( p_intf );
173
    Dialogs::destroy( p_intf );
Cyril Deguet's avatar
Cyril Deguet committed
174
    Interpreter::destroy( p_intf );
175 176
    AsyncQueue::destroy( p_intf );
    VarManager::destroy( p_intf );
177
    VlcProc::destroy( p_intf );
178
    OSFactory::destroy( p_intf );
179 180 181 182 183 184

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

185
    // Unsubscribe from messages bank
186 187 188 189 190 191 192 193 194 195 196 197
    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 )
{
198
    int canc = vlc_savecancel();
199 200
    // Load a theme
    ThemeLoader *pLoader = new ThemeLoader( p_intf );
Gildas Bazin's avatar
 
Gildas Bazin committed
201
    char *skin_last = config_GetPsz( p_intf, "skins2-last" );
202

203
    if( !skin_last || !*skin_last || !pLoader->load( skin_last ) )
204
    {
205 206 207 208 209 210 211 212
        // 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++ )
        {
213
            string path = (*it) + sep + "default.vlt";
214 215 216 217 218 219 220
            if( pLoader->load( path ) )
            {
                // Theme loaded successfully
                break;
            }
        }
        if( it == resPath.end() )
221
        {
222
            // Last chance: the user can select a new theme file
223
            if( Dialogs::instance( p_intf ) )
224
            {
225 226 227
                CmdDlgChangeSkin *pCmd = new CmdDlgChangeSkin( p_intf );
                AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
                pQueue->push( CmdGenericPtr( pCmd ) );
228 229 230 231 232 233 234
            }
            else
            {
                // No dialogs provider, just quit...
                CmdQuit *pCmd = new CmdQuit( p_intf );
                AsyncQueue *pQueue = AsyncQueue::instance( p_intf );
                pQueue->push( CmdGenericPtr( pCmd ) );
235
                msg_Err( p_intf,
Clément Stenac's avatar
Clément Stenac committed
236
                         "cannot show the \"open skin\" dialog: exiting...");
237
            }
238 239 240 241
        }
    }
    delete pLoader;

242
    free( skin_last );
243 244 245

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

    // Enter the main event loop
248 249
    loop->run();

250
    // Delete the theme and save the configuration of the windows
251 252
    if( p_intf->p_sys->p_theme )
    {
253
        p_intf->p_sys->p_theme->saveConfig();
254 255 256
        delete p_intf->p_sys->p_theme;
        p_intf->p_sys->p_theme = NULL;
    }
257
    vlc_restorecancel(canc);
258 259
}

260

261 262 263 264 265 266 267
// Callbacks for vout requests
static int WindowOpen( vlc_object_t *p_this )
{
    vout_window_t *pWnd = (vout_window_t *)p_this;
    intf_thread_t *pIntf = (intf_thread_t *)
        vlc_object_find_name( p_this, "skins2", FIND_ANYWHERE );

268
    if( pIntf == NULL )
269 270 271 272
        return VLC_EGENERIC;

    /* FIXME: most probably not thread-safe,
     * albeit no worse than ever before */
273 274 275 276
    pWnd->handle = VlcProc::getWindow( pIntf, pWnd->vout,
                                       &pWnd->pos_x, &pWnd->pos_y,
                                       &pWnd->width, &pWnd->height );
    pWnd->p_private = pIntf;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
277
    pWnd->control = &VlcProc::controlWindow;
278 279 280 281 282 283 284 285 286
    return VLC_SUCCESS;
}


static void WindowClose( vlc_object_t *p_this )
{
    vout_window_t *pWnd = (vout_window_t *)p_this;
    intf_thread_t *pIntf = (intf_thread_t *)p_this->p_private;

Christophe Mutricy's avatar
Typo  
Christophe Mutricy committed
287
    VlcProc::releaseWindow( pIntf, pWnd->handle );
288 289
}

290 291 292 293 294 295 296 297 298 299 300 301 302
//---------------------------------------------------------------------------
// 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;

303
    // Test that we have a valid .vlt or .wsz file, based on the extension
304
    // TODO: an actual check of the contents would be better...
305
    if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
306
        ( strcasecmp( ext, ".vlt" ) && strcasecmp( ext, ".wsz" ) ) )
307 308 309 310 311 312 313 314 315
    {
        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
316
        if( var_Type( p_intf, "skin-to-load" ) == VLC_VAR_STRING )
317
        {
318 319 320 321 322
            playlist_t *p_playlist = pl_Yield( p_this );
            // Make sure the item is deleted afterwards
            /// \bug does not always work
            p_playlist->status.p_item->i_flags |= PLAYLIST_REMOVE_FLAG;
            vlc_object_release( p_playlist );
323 324

            vlc_value_t val;
325
            val.psz_string = p_demux->psz_path;
326 327 328 329 330 331 332
            var_Set( p_intf, "skin-to-load", val );
        }
        else
        {
            msg_Warn( p_this,
                      "skin could not be loaded (not using skins2 intf)" );
        }
333

334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        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 )
{
355
    return demux_vaControlHelper( p_demux->s, 0, 0, 0, 1, i_query, args );
356 357 358
}


359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
//---------------------------------------------------------------------------
// 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;
    }

376 377
    // Check that we found the correct interface (same check as for the demux)
    if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
378
    {
379 380 381 382 383 384 385 386 387 388 389
        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 ) );
        }
390 391 392
    }

    vlc_object_release( pIntf );
393
    return VLC_SUCCESS;
394 395 396
}


397 398 399 400 401 402 403 404 405 406 407 408 409
/// 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;
    }

410 411
    // Check that we found the correct interface (same check as for the demux)
    if( var_Type( pIntf, "skin-to-load" ) == VLC_VAR_STRING )
412
    {
413 414 415 416 417 418 419 420 421 422 423
        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 ) );
        }
424 425 426
    }

    vlc_object_release( pIntf );
427
    return VLC_SUCCESS;
428 429 430
}


431 432 433
//---------------------------------------------------------------------------
// Module descriptor
//---------------------------------------------------------------------------
Clément Stenac's avatar
Clément Stenac committed
434 435
#define SKINS2_LAST      N_("Skin to use")
#define SKINS2_LAST_LONG N_("Path to the skin to use.")
436
#define SKINS2_CONFIG      N_("Config of last used skin")
437 438 439 440
#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")
441 442
#define SKINS2_TASKBAR      N_("Show VLC on the taskbar")
#define SKINS2_TASKBAR_LONG N_("Show VLC on the taskbar")
443
#define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
444
#define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
445 446
    " if you want. This is mainly useful when moving windows does not behave" \
    " correctly.")
Antoine Cellerier's avatar
Antoine Cellerier committed
447 448
#define SKINS2_PLAYLIST N_("Use a skinned playlist")
#define SKINS2_PLAYLIST_LONG N_("Use a skinned playlist")
449 450

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
451
    set_category( CAT_INTERFACE );
Clément Stenac's avatar
Clément Stenac committed
452
    set_subcategory( SUBCAT_INTERFACE_MAIN );
453
    add_file( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
454
              true );
455
        change_autosave();
456
    add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
457
                true );
458
        change_autosave();
459
        change_internal();
460
#ifdef WIN32
461 462 463 464 465 466
    add_bool( "skins2-systray", false, onSystrayChange, SKINS2_SYSTRAY,
              SKINS2_SYSTRAY_LONG, false );
    add_bool( "skins2-taskbar", true, onTaskBarChange, SKINS2_TASKBAR,
              SKINS2_TASKBAR_LONG, false );
    add_bool( "skins2-transparency", false, NULL, SKINS2_TRANSPARENCY,
              SKINS2_TRANSPARENCY_LONG, false );
467
#endif
468

469 470
    add_bool( "skinned-playlist", true, NULL, SKINS2_PLAYLIST,
              SKINS2_PLAYLIST_LONG, false );
471 472
    set_shortname( N_("Skins"));
    set_description( N_("Skinnable Interface") );
473 474
    set_capability( "interface", 30 );
    set_callbacks( Open, Close );
475
    add_shortcut( "skins" );
476

477 478 479 480
    add_submodule();
        set_capability( "vout window", 51 );
        set_callbacks( WindowOpen, WindowClose );

481
    add_submodule();
482
        set_description( N_("Skins loader demux") );
483
        set_capability( "demux", 5 );
484 485
        set_callbacks( DemuxOpen, NULL );

486
vlc_module_end();