WKernel.cpp 5.27 KB
Newer Older
ebaum's avatar
[ADD]  
ebaum committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut
//
// Copyright 2009 SomeCopyrightowner
//
// 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/>.
//
//---------------------------------------------------------------------------

#include <iostream>
ebaum's avatar
ebaum committed
25
#include <list>
26 27
#include <string>
#include <vector>
ebaum's avatar
ebaum committed
28 29

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

#include "WKernel.h"
ebaum's avatar
ebaum committed
32
#include "WModule.h"
33
#include "../modules/navigationSlices/WNavigationSliceModule.h"
ebaum's avatar
[ADD]  
ebaum committed
34 35
#include "../common/WException.h"

ebaum's avatar
ebaum committed
36 37
#include "../graphicsEngine/WGraphicsEngine.h"

38
/**
ebaum's avatar
ebaum committed
39 40 41 42 43
 * Used for program wide access to the kernel.
 */
WKernel* kernel = NULL;

WKernel::WKernel( int argc, char* argv[] )
ebaum's avatar
[ADD]  
ebaum committed
44
{
ebaum's avatar
ebaum committed
45 46 47 48
    std::cout << "Initializing Kernel" << std::endl;

    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
49
    // initialize members
ebaum's avatar
ebaum committed
50 51 52
    m_ArgC = argc;
    m_ArgV = argv;
    m_FinishRequested = false;
ebaum's avatar
[ADD]  
ebaum committed
53

ebaum's avatar
ebaum committed
54 55 56
    // init GE, DataHandler, ...
    init();

ebaum's avatar
[ADD]  
ebaum committed
57 58 59 60 61 62 63
    // load modules
    loadModules();
}

WKernel::~WKernel()
{
    // cleanup
ebaum's avatar
ebaum committed
64
    std::cout << "Shutting down Kernel" << std::endl;
ebaum's avatar
[ADD]  
ebaum committed
65 66 67 68 69 70 71
}

WKernel::WKernel( const WKernel& other )
{
    *this = other;
}

ebaum's avatar
ebaum committed
72 73 74 75 76
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

77
boost::shared_ptr<WGraphicsEngine> WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
78 79 80 81
{
    return m_GraphicsEngine;
}

82 83 84 85 86 87
boost::shared_ptr<WDataHandler> WKernel::getDataHandler() const
{
    return m_DataHandler;
}


88
int WKernel::getArgumentCount() const
ebaum's avatar
ebaum committed
89 90 91 92
{
    return m_ArgC;
}

93
char** WKernel::getArguments() const
ebaum's avatar
ebaum committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
{
    return m_ArgV;
}

int WKernel::run()
{
    std::cout << "Starting Kernel" << std::endl;

    // TODO(ebaum): add separate graphics thread here
    m_GraphicsEngine->run();

    // run Gui
    // TODO(all): clean up this option handler mess
    m_Gui->run();

    // run? data handler stuff?

    // run module execution threads
    // TODO(ebaum): after having modules loaded they should be started here.
    // currently this is just the test module
    std::cout << "Starting modules:" << std::endl;
    for( std::list<WModule*>::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
116
            ++list_iter )
ebaum's avatar
ebaum committed
117 118 119 120 121 122 123 124 125 126 127
    {
        std::cout << "Starting Module: " << ( *list_iter )->getName() << std::endl;
        ( *list_iter )->run();
    }

    // wait
    m_Gui->wait( false );
    m_FinishRequested = true;

    // wait for modules to finish
    for( std::list<WModule*>::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
128
            ++list_iter )
ebaum's avatar
ebaum committed
129 130 131 132 133 134 135 136 137 138 139
    {
        ( *list_iter )->wait( true );
    }

    // finally GE
    m_GraphicsEngine->wait( true );

    // how to get QT return code from its thread?
    return 0;
}

ebaum's avatar
[ADD]  
ebaum committed
140 141
void WKernel::loadModules()
{
ebaum's avatar
ebaum committed
142
    // TODO(ebaum): add dynamic loading here
ebaum's avatar
ebaum committed
143
    std::cout << "Loading modules:" << std::endl;
ebaum's avatar
ebaum committed
144 145
    m_modules.clear();

146
    WModule* m = new WNavigationSliceModule();
ebaum's avatar
ebaum committed
147 148 149
    std::cout << "Loading Module: " << m->getName() << std::endl;

    m_modules.push_back( m );
ebaum's avatar
ebaum committed
150 151 152 153 154
}

void WKernel::init()
{
    // initialize
ebaum's avatar
ebaum committed
155 156 157 158 159 160 161 162

    // initialize graphics engine
    // this also includes initialization of WGEScene and OpenSceneGraph
    m_GraphicsEngine = boost::shared_ptr<WGraphicsEngine>( new WGraphicsEngine );

    // initialize GUI
    // TODO(all): clean up this option handler mess
    m_Gui = boost::shared_ptr<WMainApplication>( new WMainApplication() );
163 164 165

    // initialize Datahandler
    m_DataHandler = boost::shared_ptr<WDataHandler>( new WDataHandler() );
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    findAppPath();
}

bool WKernel::findAppPath()
{
    // FIXME (schurade)
    // this should work on linux, have to implement it for windows and mac later

    int length;
    char appPath[255];

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

    // Catch some errors
    if ( length < 0 )
    {
        fprintf( stderr, "Error resolving symlink /proc/self/exe.\n" );
        return false;
    }
    if ( length >= 255 )
    {
        fprintf( stderr, "Path too long. Truncated.\n" );
        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;
    }

    m_AppPath = appPath;

    std::string shaderPath( appPath );
    m_ShaderPath = shaderPath + "shaders/";

    return true;
ebaum's avatar
ebaum committed
208 209
}

210
bool WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
211 212
{
    return m_FinishRequested;
ebaum's avatar
[ADD]  
ebaum committed
213 214
}

215 216 217 218 219
void WKernel::doLoadDataSets( std::vector< std::string > fileNames )
{
    m_DataHandler->loadDataSets( fileNames );
}

220 221 222 223 224
boost::shared_ptr<WDataHandler> WKernel::getDataHandler()
{
    return m_DataHandler;
}

225 226 227 228 229 230 231 232 233
std::string WKernel::getAppPath()
{
    return m_AppPath;
}

std::string WKernel::getShaderPath()
{
    return m_ShaderPath;
}