OpenFlipper.cc 16.1 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper 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 with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper 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 LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
34

Jan Möbius's avatar
Jan Möbius committed
35 36 37 38 39 40 41
/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42 43 44 45 46 47 48 49



// Mainwindow

#include "OpenFlipper/Core/Core.hh"

// Qt
50
#include <QApplication>
Jan Möbius's avatar
 
Jan Möbius committed
51 52 53 54 55 56 57 58 59 60 61 62
#include <qgl.h>

// stdc++
#include <iostream>
#include <fstream>
#include <csignal>
#include <cstdlib>

#include "OpenFlipper/common/GlobalOptions.hh"

#include <OpenFlipper/SimpleOpt/SimpleOpt.h>

Mike Kremer's avatar
Mike Kremer committed
63
#if ( defined(WIN32) || defined(ARCH_DARWIN) )
Mike Kremer's avatar
Mike Kremer committed
64 65 66 67
  #define NO_EXECINFO
#endif

#ifndef NO_EXECINFO
68
#include <execinfo.h>
Jan Möbius's avatar
 
Jan Möbius committed
69 70
#endif

71 72 73 74 75 76
#ifdef WIN32
#ifndef NDEBUG
#define WIN_GET_DEBUG_CONSOLE
#endif
#endif

Jan Möbius's avatar
 
Jan Möbius committed
77 78 79 80
#ifdef USE_OPENMP
#include <omp.h>
#endif

81 82 83 84 85 86 87 88 89

/* ==========================================================
 *
 * Stackwalker code. Used to get a backtrace if OpenFlipper
 * crashes under windows
 *
 * ==========================================================*/


90 91
#ifdef WIN32
  #include "StackWalker/StackWalker.hh"
92
  #include <fstream>
93 94 95 96 97 98

  class StackWalkerToConsole : public StackWalker
  {
  protected:
	  virtual void OnOutput(LPCSTR szText)
    	{
99 100 101
			// Writes crash dump to .OpenFlipper config directory
			std::ofstream crashFile;
			QString crashName = OpenFlipper::Options::configDirStr() + QDir::separator() + "CrashDump.txt";
Jan Möbius's avatar
Jan Möbius committed
102
			crashFile.open(crashName.toLatin1(),std::ios::out | std::ios::app);
103 104
			crashFile << szText;
			crashFile.close();
105 106 107 108
	  }
  };
#endif

109 110 111 112 113 114 115 116

/* ==========================================================
 *
 * Console for Windows to get additional output written via
 * cerr, cout, ... that is not forwarded to log window
 *
 * ==========================================================*/

117 118
// Includes for windows debugging console
#ifdef WIN32
119
#ifdef WIN_GET_DEBUG_CONSOLE
Jan Möbius's avatar
Jan Möbius committed
120 121
  #include <fcntl.h>
  #include <io.h>
122
#endif
123
#endif
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 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 208 209
#ifdef WIN32
#ifdef WIN_GET_DEBUG_CONSOLE
    void getConsole() {
      //Create a console for this application
      AllocConsole();
      //Redirect unbuffered STDOUT to the console
      HANDLE ConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
      int SystemOutput = _open_osfhandle(intptr_t(ConsoleOutput), _O_TEXT);
      FILE *COutputHandle = _fdopen(SystemOutput, "w" );
      *stdout = *COutputHandle;
      setvbuf(stdout, NULL, _IONBF, 0);
      //Redirect unbuffered STDERR to the console
      HANDLE ConsoleError = GetStdHandle(STD_ERROR_HANDLE);
      int SystemError = _open_osfhandle(intptr_t(ConsoleError), _O_TEXT);
      FILE *CErrorHandle = _fdopen(SystemError, "w" );
      *stderr = *CErrorHandle;
      setvbuf(stderr, NULL, _IONBF, 0);
      //Redirect unbuffered STDIN to the console
      HANDLE ConsoleInput = GetStdHandle(STD_INPUT_HANDLE);
      int SystemInput = _open_osfhandle(intptr_t(ConsoleInput), _O_TEXT);
      FILE *CInputHandle = _fdopen(SystemInput, "r" );
      *stdin = *CInputHandle;
      setvbuf(stdin, NULL, _IONBF, 0);
    }
#endif
#endif

/* ==========================================================
 *
 * Linux function printing a full stack trace to the console
 *
 * ==========================================================*/
#ifndef NO_EXECINFO
void backtrace()
{
  void *addresses[20];
  char **strings;

  int size = backtrace(addresses, 20);
  strings = backtrace_symbols(addresses, size);
  std::cerr << "Stack frames: " << size << std::endl;
  for(int i = 0; i < size; i++)
    std::cerr << i << ": " << strings[i] << std::endl;
  free(strings);
}
#endif

/* ==========================================================
 *
 * General segfault handler. This function is called if OpenFlipper
 * crashes
 *
 * ==========================================================*/
void segfaultHandling (int) {

  // prevent infinite recursion if segfaultHandling() causes another segfault
  std::signal(SIGSEGV, SIG_DFL);


  std::cerr << "\n" << std::endl;
  std::cerr << "\n" << std::endl;
  std::cerr << "\33[31m" << "=====================================================" << std::endl;
  std::cerr << "\33[31m" << "OpenFlipper or one of its plugins caused a Segfault." << std::endl;
  std::cerr << "\33[31m" << "This should not happen,... Sorry :-(" << std::endl;
  std::cerr << "\33[31m" << "=====================================================" << std::endl;
  std::cerr << "\n" << std::endl;

  // Linux Handler
#ifndef NO_EXECINFO
  std::cerr << "\33[0m"  << "Trying a backtrace to show what happened last: " << std::endl;
  backtrace();

  std::cerr << "\n" << std::endl;
  std::cerr << "Backtrace completed, trying to abort now ..." << std::endl;
#endif

  // Windows handler via StackWalker
#ifdef WIN32
  StackWalkerToConsole sw;
  sw.ShowCallstack();
#endif


  std::abort();
}
210

Jan Möbius's avatar
 
Jan Möbius committed
211
enum {OPT_HELP , OPT_STEREO, OPT_BATCH ,OPT_CONSOLE_LOG , OPT_DEBUGGING, OPT_FULLSCREEN,
212
      OPT_HIDDDEN_LOGGER , OPT_NOSPLASH ,OPT_HIDDDEN_TOOLBOX , OPT_LOAD_POLYMESHES,
213
      OPT_REMOTE, OPT_REMOTE_PORT};
Jan Möbius's avatar
 
Jan Möbius committed
214 215

CSimpleOpt::SOption g_rgOptions[] = {
216 217 218 219
    { OPT_DEBUGGING        , (char*) "--debug"          , SO_NONE    },
    { OPT_HELP             , (char*) "-?"               , SO_NONE    },
    { OPT_HELP             , (char*) "--help"           , SO_NONE    },
    { OPT_HELP             , (char*) "-h"               , SO_NONE    },
Jan Möbius's avatar
Jan Möbius committed
220
    { OPT_STEREO           , (char*) "--disable-stereo" , SO_NONE    },
221 222 223 224 225 226 227
    { OPT_BATCH            , (char*) "-b"               , SO_NONE    },
    { OPT_CONSOLE_LOG      , (char*) "-c"               , SO_NONE    },
    { OPT_CONSOLE_LOG      , (char*) "--log-to-console" , SO_NONE    },
    { OPT_FULLSCREEN       , (char*) "-f"               , SO_NONE    },
    { OPT_HIDDDEN_LOGGER   , (char*) "-l"               , SO_NONE    },
    { OPT_NOSPLASH         , (char*) "--no-splash"      , SO_NONE    },
    { OPT_HIDDDEN_TOOLBOX  , (char*) "-t"               , SO_NONE    },
Mike Kremer's avatar
Mike Kremer committed
228
    { OPT_LOAD_POLYMESHES  , (char*) "-p"               , SO_NONE    },
229
    { OPT_REMOTE           , (char*) "--remote-control" , SO_NONE    },
230
    { OPT_REMOTE_PORT      , (char*) "--remote-port"    , SO_REQ_SEP },
Jan Möbius's avatar
 
Jan Möbius committed
231 232 233 234 235 236 237
    SO_END_OF_OPTIONS                       // END
};

void showHelp() {
  std::cerr << "OpenFlipper [Options] <filenames> " << std::endl << std::endl;;
  std::cerr << "Possible Options : " << std::endl;
  std::cerr << std::endl;
238 239 240 241 242 243 244 245 246 247 248

  std::cerr << "Load/Save Options:" << std::endl;
  std::cerr << " -p \t: Open files as PolyMeshes" << std::endl;
  std::cerr << std::endl;

  std::cerr << "Gui Options:" << std::endl;
  std::cerr << " -f \t\t: Start Fullscreen" << std::endl;
  std::cerr << " -l \t\t: Start with hidden logger" << std::endl;
  std::cerr << " -t \t\t: Start with hidden Toolbox" << std::endl;
  std::cerr << " --no-splash \t: Disable splash screen" << std::endl;

Jan Möbius's avatar
 
Jan Möbius committed
249
  std::cerr << " --disable-stereo \t: Disable Stereo Mode" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
250
  std::cerr << std::endl;
251 252 253

  std::cerr << "Log options:" << std::endl;
  std::cerr << " --log-to-console ( -c ) \t: Write logger window contents to console" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
254
  std::cerr << std::endl;
255 256 257 258 259

  std::cerr << "Other options:" << std::endl;
  std::cerr << " -b \t: Batch mode, you have to provide a script for execution" << std::endl;
  std::cerr << " --remote-control \t: Batch mode accepting remote connections" << std::endl;

Jan Möbius's avatar
 
Jan Möbius committed
260
  std::cerr << std::endl;
261 262 263


  std::cerr << " -h \t: This help" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
264 265 266
}


267 268 269



270

Dirk Wilden's avatar
Dirk Wilden committed
271 272
bool openPolyMeshes = false;
bool remoteControl  = false;
273

Dirk Wilden's avatar
Dirk Wilden committed
274
bool parseCommandLineOptions(CSimpleOpt& args){
275

276 277
  QString port;

Jan Möbius's avatar
 
Jan Möbius committed
278 279
  // while there are arguments left to process
  while (args.Next()) {
Dirk Wilden's avatar
Dirk Wilden committed
280

Jan Möbius's avatar
 
Jan Möbius committed
281
    if (args.LastError() == SO_SUCCESS) {
Dirk Wilden's avatar
Dirk Wilden committed
282

Jan Möbius's avatar
 
Jan Möbius committed
283 284 285 286 287 288 289 290 291
      switch (args.OptionId() ) {
        case OPT_BATCH:
          OpenFlipper::Options::nogui(true);
        break;
        case OPT_CONSOLE_LOG:
          OpenFlipper::Options::logToConsole(true);
          break;
        case OPT_DEBUGGING:
          OpenFlipper::Options::debug(true);
292
          break;
Jan Möbius's avatar
 
Jan Möbius committed
293
        case OPT_STEREO:
294 295
          OpenFlipper::Options::stereo(false);
          break;
Jan Möbius's avatar
 
Jan Möbius committed
296
        case OPT_HIDDDEN_TOOLBOX:
297 298
          OpenFlipperSettings().setValue("Core/Gui/ToolBoxes/hidden",true);
          break;
Jan Möbius's avatar
 
Jan Möbius committed
299
        case OPT_HIDDDEN_LOGGER:
300 301
          OpenFlipper::Options::loggerState(OpenFlipper::Options::Hidden);
          break;
Jan Möbius's avatar
 
Jan Möbius committed
302
        case OPT_FULLSCREEN:
303 304
          OpenFlipperSettings().setValue("Core/Gui/fullscreen",false);
          break;
Jan Möbius's avatar
 
Jan Möbius committed
305
        case OPT_LOAD_POLYMESHES:
306 307
          openPolyMeshes = true;
          break;
Jan Möbius's avatar
 
Jan Möbius committed
308
        case OPT_NOSPLASH:
309 310
          OpenFlipperSettings().setValue("Core/Gui/splash",false);
          break;
Jan Möbius's avatar
 
Jan Möbius committed
311
        case OPT_REMOTE:
312 313 314 315 316 317 318
          OpenFlipper::Options::remoteControl(true);
          break;
        case OPT_REMOTE_PORT:
          port = args.OptionArg();
          std::cerr << "Got option : " << port.toStdString() << std::endl;
          OpenFlipper::Options::remoteControl(port.toInt());
          break;
Jan Möbius's avatar
 
Jan Möbius committed
319 320 321 322 323 324 325
        case OPT_HELP:
          showHelp();
          return 0;
      }
    } else {
      std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
      showHelp();
Dirk Wilden's avatar
Dirk Wilden committed
326
      return false;
Jan Möbius's avatar
 
Jan Möbius committed
327 328
    }
  }
Dirk Wilden's avatar
Dirk Wilden committed
329 330 331 332 333
  return true;
}

int main(int argc, char **argv)
{
334

335 336
  // Remove -psn_0_xxxxx argument which is automatically
  // attached by MacOSX
Mike Kremer's avatar
 
Mike Kremer committed
337
  for (int i = 0; i < argc; i++) {
338 339 340 341 342
    if(strlen(argv[i]) > 4) {
      if( ( (argv[i])[0] == '-' ) &&
        ( (argv[i])[1] == 'p' ) &&
        ( (argv[i])[2] == 's' ) &&
        ( (argv[i])[3] == 'n' ) ) {
Jan Möbius's avatar
Jan Möbius committed
343 344
        argc--;
        argv[i] = (char *)"";
345 346 347
      }
    }
  }
Jan Möbius's avatar
Jan Möbius committed
348

Dirk Wilden's avatar
Dirk Wilden committed
349 350 351
  OpenFlipper::Options::argc(&argc);
  OpenFlipper::Options::argv(&argv);

Dirk Wilden's avatar
Dirk Wilden committed
352
  CSimpleOpt argBatch(argc, argv, g_rgOptions);
Dirk Wilden's avatar
Dirk Wilden committed
353

Dirk Wilden's avatar
Dirk Wilden committed
354
  //check only batchMode before the core is created
Dirk Wilden's avatar
Dirk Wilden committed
355 356
  while (argBatch.Next())
    if (argBatch.OptionId() == OPT_BATCH ){
Dirk Wilden's avatar
Dirk Wilden committed
357 358 359 360
      OpenFlipper::Options::nogui(true);
      break;
    }

Dirk Wilden's avatar
Dirk Wilden committed
361 362
  CSimpleOpt args(argc, argv, g_rgOptions);

Dirk Wilden's avatar
Dirk Wilden committed
363 364 365 366 367 368
// Only Install signal handler if not running in debug version, otherwise gdb will get confused
// #ifndef DEBUG
  // Set a handler for segfaults
  std::signal(SIGSEGV, segfaultHandling);
// #endif

Jan Möbius's avatar
Jan Möbius committed
369
  OpenFlipper::Options::windowTitle(TOSTRING(PRODUCT_STRING)" v" + OpenFlipper::Options::coreVersion());
370

371 372 373 374 375
#ifdef WIN32
#ifdef WIN_GET_DEBUG_CONSOLE
  getConsole();
#endif
#endif
Jan Möbius's avatar
Jan Möbius committed
376

Jan Möbius's avatar
 
Jan Möbius committed
377
  if ( !OpenFlipper::Options::nogui() ) {
378

Jan Möbius's avatar
 
Jan Möbius committed
379 380 381
    // OpenGL check
    QApplication::setColorSpec( QApplication::CustomColor );
    QApplication app(argc,argv);
382

383
#ifdef __APPLE__
Mike Kremer's avatar
Mike Kremer committed
384
    // Set organization and application names
385 386 387 388
    QCoreApplication::setOrganizationName("rwth-aachen.de");
    QCoreApplication::setApplicationName("graphics.openflipper");
#endif

Jan Möbius's avatar
 
Jan Möbius committed
389 390 391 392
    if ( !QGLFormat::hasOpenGL() ) {
      std::cerr << "This system has no OpenGL support.\n";
      return -1;
    }
393

394
#ifndef __APPLE__
Jan Möbius's avatar
 
Jan Möbius committed
395
    glutInit(&argc,argv);
396
#endif
397

Jan Möbius's avatar
 
Jan Möbius committed
398 399
    // create core ( this also reads the ini files )
    Core * w = new Core( );
400

Jan Möbius's avatar
Jan Möbius committed
401 402
    if ( !parseCommandLineOptions(args) ) {
      delete w;
Dirk Wilden's avatar
Dirk Wilden committed
403
      return 1;
Jan Möbius's avatar
Jan Möbius committed
404
    }
Dirk Wilden's avatar
Dirk Wilden committed
405

406
    QString tLang = OpenFlipperSettings().value("Core/Language/Translation","en_US").toString();
407 408 409 410

    if (tLang == "locale")
      tLang = QLocale::system().name();

411 412
    // Install translator for qt internals
    QTranslator qtTranslator;
Jan Möbius's avatar
Jan Möbius committed
413 414 415 416 417 418 419
//     std::cerr << "Loading qt translations from: " << QLibraryInfo::location(QLibraryInfo::TranslationsPath).toStdString() << std::endl;
//     if ( qtTranslator.load("qt_" + tLang, QLibraryInfo::location(QLibraryInfo::TranslationsPath)) )
//       std::cerr << "Loaded" << std::endl;
//     std::cerr << "Loading qt translations from: " << QLibraryInfo::location(QLibraryInfo::TranslationsPath).toStdString() << std::endl;
    qtTranslator.load("qt_" + tLang, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
//       std::cerr << "Loaded" << std::endl;

420 421 422
    app.installTranslator(&qtTranslator);
    
    // install translator for Core Application
Dirk Wilden's avatar
Dirk Wilden committed
423
    QString translationDir = OpenFlipper::Options::translationsDirStr() + QDir::separator();
424

Jan Möbius's avatar
Jan Möbius committed
425
//     std::cerr << "Loading own translations from: " << QString(translationDir + " (" + tLang + ")").toStdString() << std::endl;
Dirk Wilden's avatar
Dirk Wilden committed
426 427 428 429 430 431 432 433 434 435 436 437 438

    QDir dir(translationDir);
    dir.setFilter(QDir::Files);

    QFileInfoList list = dir.entryInfoList();

    for (int i = 0; i < list.size(); ++i) {
      QFileInfo fileInfo = list.at(i);

      if ( fileInfo.baseName().contains(tLang) ){
        QTranslator* myAppTranslator = new QTranslator();

        if ( myAppTranslator->load( fileInfo.filePath() ) ){
Jan Möbius's avatar
Jan Möbius committed
439
//           std::cerr << "Loaded " << fileInfo.fileName().toStdString() << std::endl;
440
    
Dirk Wilden's avatar
Dirk Wilden committed
441
          app.installTranslator(myAppTranslator);
Jan Möbius's avatar
Jan Möbius committed
442 443
        } else {
  	  delete myAppTranslator;
Dirk Wilden's avatar
Dirk Wilden committed
444 445 446
        }
      }
     }
447
    
Jan Möbius's avatar
 
Jan Möbius committed
448 449
    // After setting all Options from command line, build the real gui
    w->init();
450

451
    #ifndef __APPLE__
452 453 454 455 456 457
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
      /* Problem: glewInit failed, something is seriously wrong. */
      fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }
458
    #endif
Jan Möbius's avatar
Jan Möbius committed
459
 
460
    for ( int i = 0 ; i < args.FileCount(); ++i )
461
      w->commandLineOpen(args.File(i), openPolyMeshes);    
462

Jan Möbius's avatar
 
Jan Möbius committed
463
    return app.exec();
464

Jan Möbius's avatar
 
Jan Möbius committed
465
  } else {
466

Jan Möbius's avatar
 
Jan Möbius committed
467
    QCoreApplication app(argc,argv);
468

469
#ifdef __APPLE__
Mike Kremer's avatar
Mike Kremer committed
470
    // Set organization and application names
471 472 473 474
    QCoreApplication::setOrganizationName("rwth-aachen.de");
    QCoreApplication::setApplicationName("graphics.openflipper");
#endif

Jan Möbius's avatar
 
Jan Möbius committed
475 476
    // create widget ( this also reads the ini files )
    Core * w = new Core( );
477

Jan Möbius's avatar
Jan Möbius committed
478 479
    if ( !parseCommandLineOptions(args) ) {
      delete w;
Dirk Wilden's avatar
Dirk Wilden committed
480
      return 1;
Jan Möbius's avatar
Jan Möbius committed
481
    }
Dirk Wilden's avatar
Dirk Wilden committed
482

Jan Möbius's avatar
 
Jan Möbius committed
483 484
    // After setting all Options from command line, build the real gui
    w->init();
485 486 487 488

    for ( int i = 0 ; i < args.FileCount(); ++i )
      w->commandLineScript(args.File(i));

Dirk Wilden's avatar
Dirk Wilden committed
489
    return app.exec();
Jan Möbius's avatar
 
Jan Möbius committed
490 491 492 493
  }

  return 0;
}