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

ebaum's avatar
ebaum committed
47 48
#include "../graphicsEngine/WGraphicsEngine.h"

49
#include "WKernel.h"
50

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

56 57 58 59 60 61 62 63
/**
 * Define shader path.
 */
std::string WKernel::m_shaderPath = std::string();

/**
 * Define app path.
 */
64
std::string WKernel::m_appPath = std::string();
65

66 67 68 69 70
/**
 * Define font path.
 */
std::string WKernel::m_fontPath = std::string();

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

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

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

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

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

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

99 100 101 102 103 104 105 106 107

    // 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
108
    m_graphicsEngine->setShaderPath( m_shaderPath );
109
    m_graphicsEngine->setFontPath( m_fontPath );
110 111 112

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

ebaum's avatar
ebaum committed
115 116 117 118 119
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

120
boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
121
{
122
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
123 124
}

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

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

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

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

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

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

150 151 152
    // start GE
    m_graphicsEngine->run();

153
    // default modules
154
    {
155 156 157 158
        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 )
159
        {
160 161 162 163 164 165 166 167 168 169 170
            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
171 172 173
                WLogger::getLogger()->addLogMessage( "Specified default module \"" + moduleName + "\" does not exist. Ignoring.",
                                                     "Kernel",
                                                     LL_WARNING );
174
            }
175 176
        }
    }
177

178 179
    // actually there is nothing more to do here
    waitForStop();
180

181
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
182 183
}

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

192
    if ( m_appPath != "" )
193 194 195 196 197 198
    {
        return;
    }

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

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

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

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

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

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

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

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

231
std::string WKernel::getAppPath()
232
{
233
    findAppPath();
234
    return WKernel::m_appPath;
235 236
}

237
std::string WKernel::getShaderPath()
238
{
239 240
    findAppPath();
    return WKernel::m_shaderPath;
241
}
242 243 244 245 246

boost::shared_ptr< WROIManagerFibers> WKernel::getRoiManager()
{
    return m_roiManager;
}
247 248 249 250 251 252

std::string WKernel::getFontPath()
{
    findAppPath();
    return WKernel::m_fontPath;
}