WKernel.cpp 7.08 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 "../dataHandler/WDataHandler.h"
42
#include "../common/WPreferences.h"
43
#include "../common/WStringUtils.h"
44
#include "../common/WThreadedRunner.h"
45
#include "WModule.h"
ledig's avatar
ledig committed
46
#include "WBatchLoader.h"
47
#include "WModuleFactory.h"
ebaum's avatar
[ADD]  
ebaum committed
48

ebaum's avatar
ebaum committed
49 50
#include "../graphicsEngine/WGraphicsEngine.h"

51
#include "WKernel.h"
52

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

58 59 60
/**
 * Define shader path.
 */
61
boost::filesystem::path WKernel::m_shaderPath = boost::filesystem::path();
62 63 64 65

/**
 * Define app path.
 */
66
boost::filesystem::path WKernel::m_appPath = boost::filesystem::path();
67

68 69 70
/**
 * Define font path.
 */
71
boost::filesystem::path WKernel::m_fontPath = boost::filesystem::path();
72

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

78
    // init the singleton
ebaum's avatar
ebaum committed
79 80
    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
81
    // initialize members
82 83
    m_gui = gui;
    m_graphicsEngine = ge;
84

85
    // init
ebaum's avatar
ebaum committed
86
    init();
ebaum's avatar
[ADD]  
ebaum committed
87 88 89 90 91
}

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

95
void WKernel::init()
ebaum's avatar
[ADD]  
ebaum committed
96
{
97
    // initialize
98
    findAppPath();
99 100
    m_roiManager = boost::shared_ptr< WROIManagerFibers >( new WROIManagerFibers() );

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

104 105 106
    // init data handler
    WDataHandler::getDataHandler();

107 108 109 110 111
    // 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
112 113
    m_graphicsEngine->setShaderPath( m_shaderPath.file_string() );
    m_graphicsEngine->setFontPath( m_fontPath.file_string() );
114 115 116

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

ebaum's avatar
ebaum committed
119 120 121 122 123
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

124
boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
125
{
126
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
127 128
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
129
boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
130
{
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
131
    return m_moduleContainer;
132 133
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
134
boost::shared_ptr< WGUI > WKernel::getGui() const
135
{
136
    return m_gui;
137 138
}

139
void WKernel::finalize()
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
140
{
141
    WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
142 143

    // NOTE: stopping a container erases all modules inside.
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
144
    getRootContainer()->stop();
145 146 147

    WLogger::getLogger()->addLogMessage( "Stopping Data Handler", "Kernel", LL_INFO );
    WDataHandler::getDataHandler()->clear();
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
148 149
}

150
void WKernel::threadMain()
ebaum's avatar
ebaum committed
151
{
152
    WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
153

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

157 158 159
    // start GE
    m_graphicsEngine->run();

160
    // default modules
161
    {
162 163 164 165
        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 )
166
        {
167 168 169 170 171 172 173 174 175 176 177
            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
178 179 180
                WLogger::getLogger()->addLogMessage( "Specified default module \"" + moduleName + "\" does not exist. Ignoring.",
                                                     "Kernel",
                                                     LL_WARNING );
181
            }
182 183
        }
    }
184

185 186
    // actually there is nothing more to do here
    waitForStop();
187

188
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
189 190
}

191
void WKernel::findAppPath()
192
{
193
    // only get the path if not already done
194
    if ( !m_appPath.empty() )
195 196 197 198 199 200
    {
        return;
    }

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

203 204
    m_appPath = currentDir;
    WLogger::getLogger()->addLogMessage( "Application path: " + m_appPath.file_string(), "Kernel", LL_DEBUG );
205

206 207
    m_shaderPath = fs::path( currentDir / "shaders" );
    WLogger::getLogger()->addLogMessage( "Shader path: " + m_shaderPath.file_string(), "Kernel", LL_DEBUG );
208

209 210
    m_fontPath = fs::path( currentDir / "fonts" );
    WLogger::getLogger()->addLogMessage( "Font path: " + m_fontPath.file_string(), "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
211 212
}

213
const WBoolFlag& WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
214
{
215
    return m_shutdownFlag;
ebaum's avatar
[ADD]  
ebaum committed
216 217
}

218
void WKernel::loadDataSets( std::vector< std::string > fileNames )
219
{
220 221 222 223 224 225
    getRootContainer()->loadDataSets( fileNames );
}

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

228
boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
229
{
230
    return getRootContainer()->applyModule( applyOn, prototype );
231 232
}

233
boost::filesystem::path WKernel::getAppPathObject()
234
{
235
    findAppPath();
236
    return WKernel::m_appPath;
237 238
}

239 240 241 242 243 244
std::string WKernel::getAppPath()
{
    findAppPath();
    return WKernel::m_appPath.file_string();
}

245
std::string WKernel::getShaderPath()
246
{
247
    findAppPath();
248
    return WKernel::m_shaderPath.file_string();
249
}
250 251 252 253 254

boost::shared_ptr< WROIManagerFibers> WKernel::getRoiManager()
{
    return m_roiManager;
}
255 256 257 258

std::string WKernel::getFontPath()
{
    findAppPath();
259
    return WKernel::m_fontPath.file_string();
260
}