WKernel.cpp 6.89 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
#if defined(__APPLE__)
#include <mach-o/dyld.h>
#endif

ebaum's avatar
[ADD]  
ebaum committed
29
#include <iostream>
ebaum's avatar
ebaum committed
30
#include <list>
31 32
#include <string>
#include <vector>
ebaum's avatar
ebaum committed
33 34

#include <boost/thread/xtime.hpp>
ebaum's avatar
[ADD]  
ebaum committed
35

ebaum's avatar
ebaum committed
36
#include "WModule.h"
37
#include "WModuleFactory.h"
ebaum's avatar
[ADD]  
ebaum committed
38
#include "../common/WException.h"
39
#include "../common/WCondition.h"
40
#include "../common/WConditionOneShot.h"
41
#include "../common/WFlag.h"
ebaum's avatar
[ADD]  
ebaum committed
42

ebaum's avatar
ebaum committed
43 44
#include "../graphicsEngine/WGraphicsEngine.h"

45
#include "WKernel.h"
46

47
/**
ebaum's avatar
ebaum committed
48 49 50 51
 * Used for program wide access to the kernel.
 */
WKernel* kernel = NULL;

52
WKernel::WKernel( boost::shared_ptr< WGraphicsEngine > ge, boost::shared_ptr< WGUI > gui ):
53
    WThreadedRunner()
ebaum's avatar
[ADD]  
ebaum committed
54
{
55
    WLogger::getLogger()->addLogMessage( "Initializing Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
56

57
    // init the singleton
ebaum's avatar
ebaum committed
58 59
    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
60
    // initialize members
61 62
    m_gui = gui;
    m_graphicsEngine = ge;
63

64
    // init
ebaum's avatar
ebaum committed
65
    init();
ebaum's avatar
[ADD]  
ebaum committed
66 67 68 69 70
}

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

74
void WKernel::init()
ebaum's avatar
[ADD]  
ebaum committed
75
{
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
     // initialize
    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
    m_graphicsEngine->setShaderPath( m_shaderPath );

    // initialize Datahandler
    m_dataHandler = boost::shared_ptr< WDataHandler >( new WDataHandler() );

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

ebaum's avatar
ebaum committed
96 97 98 99 100
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

101
boost::shared_ptr< WGraphicsEngine > WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
102
{
103
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
104 105
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
106
boost::shared_ptr< WModuleContainer > WKernel::getRootContainer() const
107
{
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
108
    return m_moduleContainer;
109 110
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
111
boost::shared_ptr< WGUI > WKernel::getGui() const
112
{
113
    return m_gui;
114 115
}

116
void WKernel::finalize()
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
117
{
118
    WLogger::getLogger()->addLogMessage( "Stopping Kernel", "Kernel", LL_INFO );
119 120

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

124
void WKernel::threadMain()
ebaum's avatar
ebaum committed
125
{
126
    WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_INFO );
ebaum's avatar
ebaum committed
127

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

131 132 133
    // start GE
    m_graphicsEngine->run();

134
    // default modules
135 136
    m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Navigation Slice Module" ) ) , true );
    m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Coordinate System Module" ) ) , true );
schurade's avatar
schurade committed
137
    m_moduleContainer->add( m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "HUD" ) ) , true );
138

139 140
    // actually there is nothing more to do here
    waitForStop();
141

142
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_INFO );
143 144 145 146 147 148
}

bool WKernel::findAppPath()
{
    // FIXME (schurade)
    // this should work on linux, have to implement it for windows and mac later
Alexander Wiebel's avatar
Alexander Wiebel committed
149
#ifdef __linux__
150 151 152 153 154 155 156 157
    int length;
    char appPath[255];

    length = readlink( "/proc/self/exe", appPath, sizeof( appPath ) );

    // Catch some errors
    if ( length < 0 )
    {
158
        WLogger::getLogger()->addLogMessage( "Error resolving symlink /proc/self/exe.", "Kernel", LL_ERROR );
159 160 161 162
        return false;
    }
    if ( length >= 255 )
    {
163
        WLogger::getLogger()->addLogMessage( "Path too long. Truncated.", "Kernel", LL_ERROR );
164 165 166 167 168 169 170 171 172 173 174
        return false;
    }

    // the string this readlink() function returns is appended with a '@'.
    appPath[length] = '\0';

    // strip off the executable name
    while ( appPath[length] != '/' )
    {
        appPath[length] = '\0';
        --length;
schurade's avatar
schurade committed
175
        assert( length >= 0 );
176 177 178 179 180
    }

    m_AppPath = appPath;

    std::string shaderPath( appPath );
181
    m_shaderPath = shaderPath + "shaders/";
182 183

    return true;
schurade's avatar
schurade committed
184
#elif defined( __APPLE__ )
185
    char path[1024];
schurade's avatar
schurade committed
186
    uint32_t size = sizeof( path );
187
    if( _NSGetExecutablePath( path, &size ) == 0 )
188
    {
189
        WLogger::getLogger()->addLogMessage( "Executable path is " + std::string( path ), "Kernel", LL_DEBUG );
190 191

        int i = strlen( path );
schurade's avatar
schurade committed
192 193 194 195 196 197
        while( path[i] != '/' )
        {
            path[i] = '\0';
            i--;
            assert( i >= 0 );
        }
198
        WLogger::getLogger()->addLogMessage( "Application path is " + std::string( path ), "Kernel", LL_DEBUG );
schurade's avatar
schurade committed
199 200 201 202 203 204
        m_AppPath = path;

        std::string shaderPath( path );
        m_shaderPath = shaderPath + "shaders/";

        return true;
205 206 207
    }
    else
    {
208
        WLogger::getLogger()->addLogMessage( "Buffer too small; need size " + boost::lexical_cast< std::string >( size ),
209
                                             "Kernel", LL_ERROR );
schurade's avatar
schurade committed
210
        assert( size <= sizeof( path ) );
211 212
    }
#else
213
#pragma message ( "Error: findAppPath not implemented for this platform" )
214 215 216
#endif

    return false;
ebaum's avatar
ebaum committed
217 218
}

219
const WBoolFlag& WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
220
{
221
    return m_shutdownFlag;
ebaum's avatar
[ADD]  
ebaum committed
222 223
}

224
void WKernel::loadDataSets( std::vector< std::string > fileNames )
225
{
226
    // add a new data module for each file to load
227 228
    using boost::shared_ptr;
    for( std::vector< std::string >::iterator iter = fileNames.begin(); iter != fileNames.end(); ++iter )
229
    {
230
        shared_ptr< WModule > mod = m_moduleFactory->create( m_moduleFactory->getPrototypeByName( "Data Module" ) );
231 232 233
        mod->getProperties()->setValue( "filename" , ( *iter ) );
        m_moduleContainer->add( mod );
    }
234 235
}

236
boost::shared_ptr< WModule > WKernel::applyModule( boost::shared_ptr< WModule > applyOn, boost::shared_ptr< WModule > prototype )
237
{
238
    return getRootContainer()->applyModule( applyOn, prototype );
239 240
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
241
boost::shared_ptr< WDataHandler > WKernel::getDataHandler() const
242
{
243
    return m_dataHandler;
244 245
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
246
std::string WKernel::getAppPath() const
247 248 249 250
{
    return m_AppPath;
}

Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
251
std::string WKernel::getShaderPath() const
252
{
253
    return m_shaderPath;
254
}