WKernel.cpp 8.7 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 "../modules/data/WDataModule.hpp"
38
#include "../modules/navigationSlices/WNavigationSliceModule.h"
ebaum's avatar
[ADD]  
ebaum committed
39 40
#include "../common/WException.h"

ebaum's avatar
ebaum committed
41 42
#include "../graphicsEngine/WGraphicsEngine.h"

43
#include "WKernel.h"
44

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

50
WKernel::WKernel( int argc, char* argv[], boost::shared_ptr< WGUI > gui )
51
    : m_gui( gui )
ebaum's avatar
[ADD]  
ebaum committed
52
{
ebaum's avatar
ebaum committed
53 54 55 56
    std::cout << "Initializing Kernel" << std::endl;

    kernel = this;

ebaum's avatar
[ADD]  
ebaum committed
57
    // initialize members
ebaum's avatar
ebaum committed
58 59 60
    m_ArgC = argc;
    m_ArgV = argv;
    m_FinishRequested = false;
ebaum's avatar
[ADD]  
ebaum committed
61

ebaum's avatar
ebaum committed
62 63 64
    // init GE, DataHandler, ...
    init();

ebaum's avatar
[ADD]  
ebaum committed
65 66 67 68 69 70 71
    // load modules
    loadModules();
}

WKernel::~WKernel()
{
    // cleanup
72
    WLogger::getLogger()->addLogMessage( "Shutting down Kernel", "Kernel", LL_DEBUG );
73 74

    // finish running thread
75
    WLogger::getLogger()->wait( true );
76
    // write remaining log messages
77
    WLogger::getLogger()->processQueue();
ebaum's avatar
[ADD]  
ebaum committed
78 79 80 81 82 83 84
}

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

ebaum's avatar
ebaum committed
85 86 87 88 89
WKernel* WKernel::getRunningKernel()
{
    return kernel;
}

90
boost::shared_ptr<WGraphicsEngine> WKernel::getGraphicsEngine() const
ebaum's avatar
ebaum committed
91
{
92
    return m_graphicsEngine;
ebaum's avatar
ebaum committed
93 94
}

95 96
boost::shared_ptr<WDataHandler> WKernel::getDataHandler() const
{
97
    return m_dataHandler;
98 99
}

100
boost::shared_ptr< WGUI > WKernel::getGui()
101
{
102
    return m_gui;
103 104
}

105 106
void WKernel::setGui( boost::shared_ptr< WGUI > gui )
{
107
    m_gui = gui;
108
}
109

110
int WKernel::getArgumentCount() const
ebaum's avatar
ebaum committed
111 112 113 114
{
    return m_ArgC;
}

115
char** WKernel::getArguments() const
ebaum's avatar
ebaum committed
116 117 118 119 120 121
{
    return m_ArgV;
}

int WKernel::run()
{
122
    WLogger::getLogger()->addLogMessage( "Starting Kernel", "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
123 124

    // TODO(ebaum): add separate graphics thread here
125
    m_graphicsEngine->run();
ebaum's avatar
ebaum committed
126 127 128

    // run Gui
    // TODO(all): clean up this option handler mess
129
    m_gui->run();
ebaum's avatar
ebaum committed
130 131 132

    // run? data handler stuff?

133 134 135 136
    // **************************************************************************************************************************
    // This part will be exchanged by some kind of ModuleContainer managing module execution.
    // TODO(ebaum): replace by ModuleContainer
    // **************************************************************************************************************************
137

ebaum's avatar
ebaum committed
138 139 140
    // run module execution threads
    // TODO(ebaum): after having modules loaded they should be started here.
    // currently this is just the test module
141
    WLogger::getLogger()->addLogMessage( "*** Starting modules:", "Kernel", LL_DEBUG );
142
    for( std::list<boost::shared_ptr<WModule> >::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
143
            ++list_iter )
ebaum's avatar
ebaum committed
144
    {
145
        WLogger::getLogger()->addLogMessage( "Starting module: " + ( *list_iter )->getName(), "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
146 147 148
        ( *list_iter )->run();
    }

149
    // TODO(schurade): this must be moved somewhere else, and realize the wait loop in another fashion
150
    while ( !m_gui->isInitalized() )
151 152
    {
    }
153
    m_gui->getLoadButtonSignal()->connect( boost::bind( &WKernel::doLoadDataSets, this, _1 ) );
Sebastian Eichelbaum's avatar
[MERGE]  
Sebastian Eichelbaum committed
154 155

    for( std::list<boost::shared_ptr<WModule> >::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
156 157 158 159 160
                ++list_iter )
    {
        ( *list_iter )->connectToGui();
    }

ebaum's avatar
ebaum committed
161
    // wait
162
    // TODO(ebaum): this is not the optimal. It would be better to quit OSG, GE and so on in the right order.
163
    m_gui->wait( false );
ebaum's avatar
ebaum committed
164 165 166
    m_FinishRequested = true;

    // wait for modules to finish
167
    for( std::list<boost::shared_ptr<WModule> >::iterator list_iter = m_modules.begin(); list_iter != m_modules.end();
wiebel's avatar
[STYLE]  
wiebel committed
168
            ++list_iter )
ebaum's avatar
ebaum committed
169 170 171 172 173
    {
        ( *list_iter )->wait( true );
    }

    // finally GE
174
    m_graphicsEngine->wait( true );
ebaum's avatar
ebaum committed
175 176 177 178 179

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

ebaum's avatar
[ADD]  
ebaum committed
180 181
void WKernel::loadModules()
{
ebaum's avatar
ebaum committed
182
    // TODO(ebaum): add dynamic loading here
183
    WLogger::getLogger()->addLogMessage( "*** Loading Modules: ", "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
184 185
    m_modules.clear();

186
    boost::shared_ptr<WModule> m = boost::shared_ptr<WModule>( new WNavigationSliceModule() );
187
    WLogger::getLogger()->addLogMessage( "Loading module: " + m->getName(), "Kernel", LL_DEBUG );
ebaum's avatar
ebaum committed
188 189

    m_modules.push_back( m );
ebaum's avatar
ebaum committed
190 191 192 193 194
}

void WKernel::init()
{
    // initialize
195
    findAppPath();
ebaum's avatar
ebaum committed
196 197 198

    // initialize graphics engine
    // this also includes initialization of WGEScene and OpenSceneGraph
199
    m_graphicsEngine = boost::shared_ptr<WGraphicsEngine>( new WGraphicsEngine( m_shaderPath ) );
200 201

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

    m_dataHandler->getSignalAddDataset()->connect( boost::bind( &WKernel::slotFinishLoadData, this, _1 ) );
205 206 207 208 209 210
}

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
211
#ifdef __linux__
212 213 214 215 216 217 218 219
    int length;
    char appPath[255];

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

    // Catch some errors
    if ( length < 0 )
    {
220
        WLogger::getLogger()->addLogMessage( "Error resolving symlink /proc/self/exe.", "Kernel", LL_ERROR );
221 222 223 224
        return false;
    }
    if ( length >= 255 )
    {
225
        WLogger::getLogger()->addLogMessage( "Path too long. Truncated.", "Kernel", LL_ERROR );
226 227 228 229 230 231 232 233 234 235 236
        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
237
        assert( length >= 0 );
238 239 240 241 242
    }

    m_AppPath = appPath;

    std::string shaderPath( appPath );
243
    m_shaderPath = shaderPath + "shaders/";
244 245

    return true;
schurade's avatar
schurade committed
246
#elif defined( __APPLE__ )
247
    char path[1024];
schurade's avatar
schurade committed
248
    uint32_t size = sizeof( path );
249
    if( _NSGetExecutablePath( path, &size ) == 0 )
250
    {
251 252 253
        WLogger::getLogger()->addLogMessage( "Executable path is " + std::string( path ), "Kernel", LL_ERROR );

        int i = strlen( path );
schurade's avatar
schurade committed
254 255 256 257 258 259
        while( path[i] != '/' )
        {
            path[i] = '\0';
            i--;
            assert( i >= 0 );
        }
260
        WLogger::getLogger()->addLogMessage( "Application path is " + std::string( path ), "Kernel", LL_ERROR );
schurade's avatar
schurade committed
261 262 263 264 265 266
        m_AppPath = path;

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

        return true;
267 268 269
    }
    else
    {
270 271
        WLogger::getLogger()->addLogMessage( "Buffer too small; need size " + boost::lexical_cast<std::string>( size ),
                                             "Kernel", LL_ERROR );
schurade's avatar
schurade committed
272
        assert( size <= sizeof( path ) );
273 274 275 276 277 278
    }
#else
#error findAppPath not implemented for this platform
#endif

    return false;
ebaum's avatar
ebaum committed
279 280
}

281
bool WKernel::isFinishRequested() const
ebaum's avatar
ebaum committed
282 283
{
    return m_FinishRequested;
ebaum's avatar
[ADD]  
ebaum committed
284 285
}

286 287
void WKernel::doLoadDataSets( std::vector< std::string > fileNames )
{
288
    m_dataHandler->loadDataSets( fileNames );
289
}
290

291 292 293
void WKernel::slotFinishLoadData( boost::shared_ptr< WDataSet > dataSet )
{
    boost::shared_ptr< WModule > module = boost::shared_ptr< WModule >( new WDataModule<int>( dataSet ) );
294

295
    module->getProperties()->addBool( "active", true );
296
    module->getProperties()->addBool( "interpolation", true );
297 298
    module->getProperties()->addInt( "threshold", 0 );
    module->getProperties()->addInt( "alpha", 100 );
299
    module->getProperties()->setMax( "alpha", 100 );
300
    module->getProperties()->setValue( "name", dataSet->getFileName() );
301

302
    m_gui->addDatasetToBrowser( module, 0 );
303 304
}

305 306
boost::shared_ptr<WDataHandler> WKernel::getDataHandler()
{
307
    return m_dataHandler;
308 309
}

310 311 312 313 314 315 316
std::string WKernel::getAppPath()
{
    return m_AppPath;
}

std::string WKernel::getShaderPath()
{
317
    return m_shaderPath;
318
}