skin_main.cpp 12.5 KB
Newer Older
1 2 3
/*****************************************************************************
 * skin_main.cpp
 *****************************************************************************
4
 * Copyright (C) 2003 the VideoLAN team
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * Authors: Cyril Deguet     <asmax@via.ecp.fr>
 *          Olivier Teulire <ipkiss@via.ecp.fr>
 *
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>
26
#include <vlc/input.h>
27 28 29 30 31 32
#include "dialogs.hpp"
#include "os_factory.hpp"
#include "os_loop.hpp"
#include "var_manager.hpp"
#include "vlcproc.hpp"
#include "theme_loader.hpp"
33
#include "theme.hpp"
34
#include "theme_repository.hpp"
Cyril Deguet's avatar
Cyril Deguet committed
35
#include "../parser/interpreter.hpp"
36
#include "../commands/async_queue.hpp"
37
#include "../commands/cmd_quit.hpp"
38
#include "../commands/cmd_dialogs.hpp"
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56


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


//---------------------------------------------------------------------------
// Local prototypes.
//---------------------------------------------------------------------------
static int  Open  ( vlc_object_t * );
static void Close ( vlc_object_t * );
static void Run   ( intf_thread_t * );

57 58 59 60
static int DemuxOpen( vlc_object_t * );
static int Demux( demux_t * );
static int DemuxControl( demux_t *, int, va_list );

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

//---------------------------------------------------------------------------
// 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
80
    p_intf->p_sys->p_sub = msg_Subscribe( p_intf, MSG_QUEUE_NORMAL );
81 82 83 84 85 86 87

    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" );
88
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
89 90 91 92 93 94 95
        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
96
    p_intf->p_sys->p_interpreter = NULL;
97 98 99 100
    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;
101
    p_intf->p_sys->p_repository = NULL;
102 103 104 105

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

106 107 108
    // Create a variable to be notified of skins to be loaded
    var_Create( p_intf, "skin-to-load", VLC_VAR_STRING );

109
    // Initialize singletons
110 111 112
    if( OSFactory::instance( p_intf ) == NULL )
    {
        msg_Err( p_intf, "Cannot initialize OSFactory" );
113 114
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
115 116
        return VLC_EGENERIC;
    }
117 118 119
    if( AsyncQueue::instance( p_intf ) == NULL )
    {
        msg_Err( p_intf, "Cannot initialize AsyncQueue" );
120 121
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
122 123
        return VLC_EGENERIC;
    }
Cyril Deguet's avatar
Cyril Deguet committed
124 125 126
    if( Interpreter::instance( p_intf ) == NULL )
    {
        msg_Err( p_intf, "Cannot instanciate Interpreter" );
127 128
        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
129 130
        return VLC_EGENERIC;
    }
131 132 133
    if( VarManager::instance( p_intf ) == NULL )
    {
        msg_Err( p_intf, "Cannot instanciate VarManager" );
134 135
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
136 137 138 139 140
        return VLC_EGENERIC;
    }
    if( VlcProc::instance( p_intf ) == NULL )
    {
        msg_Err( p_intf, "Cannot initialize VLCProc" );
141 142
        vlc_object_release( p_intf->p_sys->p_playlist );
        msg_Unsubscribe( p_intf, p_intf->p_sys->p_sub );
143 144 145
        return VLC_EGENERIC;
    }
    Dialogs::instance( p_intf );
146
    ThemeRepository::instance( p_intf );
147

148
    // We support play on start
149 150
    p_intf->b_play = VLC_TRUE;

151 152 153 154 155 156 157 158 159 160 161 162
    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();
163
    ThemeRepository::destroy( p_intf );
164
    Dialogs::destroy( p_intf );
Cyril Deguet's avatar
Cyril Deguet committed
165
    Interpreter::destroy( p_intf );
166 167
    AsyncQueue::destroy( p_intf );
    VarManager::destroy( p_intf );
168
    VlcProc::destroy( p_intf );
169
    OSFactory::destroy( p_intf );
170 171 172 173 174 175

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

176
    // Unsubscribe from messages bank
177 178 179 180 181 182 183 184 185 186 187 188 189 190
    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
191
    char *skin_last = config_GetPsz( p_intf, "skins2-last" );
192

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

    if( skin_last )
    {
        free( skin_last );
    }

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

240 241 242 243 244 245 246 247
    // 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 )
        {
248
            playlist_LockControl( p_playlist, PLAYLIST_AUTOPLAY );
249 250 251 252
            vlc_object_release( p_playlist );
        }
    }

253
    // Enter the main event loop
254 255
    loop->run();

256
    // Delete the theme and save the configuration of the windows
257 258
    if( p_intf->p_sys->p_theme )
    {
259
        p_intf->p_sys->p_theme->saveConfig();
260 261 262 263 264
        delete p_intf->p_sys->p_theme;
        p_intf->p_sys->p_theme = NULL;
    }
}

265 266 267 268 269 270 271 272 273 274 275 276 277 278

//---------------------------------------------------------------------------
// 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;

279
    // Test that we have a valid .vlt or .wsz file, based on the extension
280
    // TODO: an actual check of the contents would be better...
281
    if( ( ext = strchr( p_demux->psz_path, '.' ) ) == NULL ||
282
        ( strcasecmp( ext, ".vlt" ) && strcasecmp( ext, ".wsz" ) ) )
283 284 285 286 287 288 289 290 291
    {
        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
292
        if( var_Type( p_intf, "skin-to-load" ) == VLC_VAR_STRING )
293 294 295 296 297 298 299 300 301 302 303 304 305
        {
            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
                p_playlist->pp_items[p_playlist->i_index]->b_autodeletion =
                    VLC_TRUE;
                vlc_object_release( p_playlist );
            }

            vlc_value_t val;
306
            val.psz_string = p_demux->psz_path;
307 308 309 310 311 312 313
            var_Set( p_intf, "skin-to-load", val );
        }
        else
        {
            msg_Warn( p_this,
                      "skin could not be loaded (not using skins2 intf)" );
        }
314

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
        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 );
}


340 341 342
//---------------------------------------------------------------------------
// Module descriptor
//---------------------------------------------------------------------------
343 344 345 346 347
#define SKINS2_LAST      N_("Last skin used")
#define SKINS2_LAST_LONG N_("Select the path to the last skin used.")
#define SKINS2_CONFIG      N_("Config of last used skin")
#define SKINS2_CONFIG_LONG N_("Config of last used skin.")
#define SKINS2_TRANSPARENCY      N_("Enable transparency effects")
348
#define SKINS2_TRANSPARENCY_LONG N_("You can disable all transparency effects"\
349 350
    " if you want. This is mainly useful when moving windows does not behave" \
    " correctly.")
351 352

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
353 354
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_GENERAL );
355
    add_string( "skins2-last", "", NULL, SKINS2_LAST, SKINS2_LAST_LONG,
356
                VLC_TRUE );
357
        change_autosave();
358
    add_string( "skins2-config", "", NULL, SKINS2_CONFIG, SKINS2_CONFIG_LONG,
359
                VLC_TRUE );
360
        change_autosave();
361
#ifdef WIN32
362
    add_bool( "skins2-transparency", VLC_FALSE, NULL, SKINS2_TRANSPARENCY,
363 364
              SKINS2_TRANSPARENCY_LONG, VLC_FALSE );
#endif
365
    set_shortname( _("Skins"));
366 367 368
    set_description( _("Skinnable Interface") );
    set_capability( "interface", 30 );
    set_callbacks( Open, Close );
369
    add_shortcut( "skins" );
370
    set_program( "svlc" );
371 372 373 374 375 376

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

377
vlc_module_end();