WKernel.cpp 6.22 KB
Newer Older
ebaum's avatar
[ADD]  
ebaum committed
1 2
//---------------------------------------------------------------------------
//
3
// Project: OpenWalnut ( http://www.openwalnut.org )
ebaum's avatar
[ADD]  
ebaum committed
4
//
5 6
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
ebaum's avatar
[ADD]  
ebaum committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25 26 27 28
#ifdef __linux__
#include <unistd.h> // used for getcwd (to get current directory)
#endif

29 30 31 32
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif

ebaum's avatar
[ADD]  
ebaum committed
33
#include <iostream>
ebaum's avatar
ebaum committed
34
#include <list>
35 36
#include <string>
#include <vector>
ebaum's avatar
ebaum committed
37

ledig's avatar
ledig committed
38
#include <boost/filesystem.hpp>
ebaum's avatar
ebaum committed
39
#include <boost/thread/xtime.hpp>
ebaum's avatar
[ADD]  
ebaum committed
40

41
#include "../common/WCondition.h"
42
#include "../common/WConditionOneShot.h"
43
#include "../common/WException.h"
44
#include "../common/WFlag.h"
45
#include "../common/WPreferences.h"
46 47 48 49
#include "../common/WStringUtils.h"
#include "WBatchLoader.h"
#include "WModule.h"
#include "WModuleFactory.h"
ebaum's avatar
[ADD]  
ebaum committed
50

ebaum's avatar
ebaum committed
51 52
#include "../graphicsEngine/WGraphicsEngine.h"

53
#include "WKernel.h"
54

55
/**
ebaum's avatar
ebaum committed
56 57 58 59
 * Used for program wide access to the kernel.
 */
WKernel* kernel = NULL;

60 61 62 63 64 65 66 67
/**
 * Define shader path.
 */
std::string WKernel::m_shaderPath = std::string();

/**
 * Define app path.
 */
68
std::string WKernel::m_appPath = std::string();
69

70
WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
71
    WThreadedRunner()
ebaum's avatar
[ADD]  
ebaum committed
72
{
73
    WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
74

75
    // init the singleton
ebaum's avatar
ebaum committed
76 77
    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
78
    // initialize members
79 80
    m_gui = gui;
    m_graphicsEngine = ge;
81

82
    // init
ebaum's avatar
ebaum committed
83
    init();
ebaum's avatar
[ADD]  
ebaum committed
84 85 86 87 88
}

WKernel::~WKernel()
{
    // cleanup
89
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
ebaum's avatar
[ADD]  
ebaum committed
90 91
}

92
void WKernel::init()
ebaum's avatar
[ADD]  
ebaum committed
93
{
94
    // initialize
95 96 97 98 99 100 101 102 103 104
    findAppPath();

    // get module factory
    m_moduleFactory = WModuleFactory::getModuleFactory();

    // initialize module container
    m_moduleContainer = boost::shared_ptr< WModuleContainer >( new WModuleContainer( "KernelRootContainer",
                "Root module container in Kernel." ) );

    // initialize graphics engine, or, at least set some stuff
105 106
    m_graphicsEngine->setShaderPath( m_shaderPath );
    m_graphicsEngine->setShaderPath( m_appPath + "/fonts/" );
107 108 109

    // load all modules
    m_moduleFactory->load();
ebaum's avatar
[ADD]  
ebaum committed
110 111
}

ebaum's avatar
ebaum committed
112 113 114 115 116
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

117
boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
118
{
119
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
120 121
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
122
boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
123
{
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
124
    return m_moduleContainer;
125 126
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
127
boost::shared_ptr< WGUI > WKernel::getGui() const
128
{
129
    return m_gui;
130 131
}

132
void WKernel::finalize()
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
133
{
134
    WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
135 136

    // NOTE: stopping a container erases all modules inside.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
137
    getRootContainer()->stop();
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
138 139
}

140
void WKernel::threadMain()
ebaum's avatar
ebaum committed
141
{
142
    WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
143

144
    // wait for GUI to be initialized properly
145
    m_gui->isInitialized().wait();
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
146

147 148 149
    // start GE
    m_graphicsEngine->run();

150
    // default modules
151
    {
152 153 154 155
        std::string stdModules = "Coordinate System Module,HUD,Navigation Slice Module";
        WPreferences::getPreference( "modules.default", &stdModules );
        std::vector< std::string > defMods = string_utils::tokenize( stdModules, "," );
        for ( std::vector< std::string >::iterator iter = defMods.begin(); iter != defMods.end(); ++iter )
156
        {
157 158 159 160 161 162 163 164 165 166 167
            std::string moduleName = string_utils::trim( ( *iter ) );
            boost::shared_ptr< WModule> proto = m_moduleFactory->isPrototypeAvailable( moduleName );

            // try to find a prototype
            if ( proto.get() )
            {
                // the module exists
                m_moduleContainer->add( m_moduleFactory->create( proto ) , true );
            }
            else
            {
Alexander Wiebel's avatar
Alexander Wiebel committed
168 169 170
                WLogger::getLogger()->addLogMessage( "Specified default module \"" + moduleName + "\" does not exist. Ignoring.",
                                                     "Kernel",
                                                     LL_WARNING );
171
            }
172 173
        }
    }
174

175 176
    // actually there is nothing more to do here
    waitForStop();
177

178
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
179 180
}

181
void WKernel::findAppPath()
182
{
183
    // only get the path if not already done
184
    if ( m_appPath != "" )
185 186 187 188
    {
        return;
    }

189
    if ( m_appPath != "" )
190 191 192 193 194 195
    {
        return;
    }

    // unified version with boost::filesystem
    namespace fs = boost::filesystem;
ledig's avatar
ledig committed
196 197
    fs::path currentDir( fs::initial_path<fs::path>() );

198 199
    m_appPath = currentDir.file_string();
//    std::cout << "Set app path to " << m_appPath << std::endl;
200 201 202

    m_shaderPath = fs::path( currentDir / "shaders" ).file_string();
//    std::cout << "Set shader path to " << m_shaderPath << std::endl;
ebaum's avatar
ebaum committed
203 204
}

205
const WBoolFlag& WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
206
{
207
    return m_shutdownFlag;
ebaum's avatar
[ADD]  
ebaum committed
208 209
}

210
void WKernel::loadDataSets( std::vector< std::string > fileNames )
211
{
212 213 214 215 216 217
    getRootContainer()->loadDataSets( fileNames );
}

void WKernel::loadDataSetsSynchronously( std::vector< std::string > fileNames )
{
    getRootContainer()->loadDataSetsSynchronously( fileNames );
218 219
}

220
boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
221
{
222
    return getRootContainer()->applyModule( applyOn, prototype );
223 224
}

225
std::string WKernel::getAppPath()
226
{
227
    findAppPath();
228
    return WKernel::m_appPath;
229 230
}

231
std::string WKernel::getShaderPath()
232
{
233 234
    findAppPath();
    return WKernel::m_shaderPath;
235
}