17 #include <kcmdlineargs.h>
18 #include <kaboutdata.h>
19 #include <kapplication.h>
20 #include <kstandarddirs.h>
22 #include <QCommandLineParser>
23 #include <QCommandLineOption>
24 #include <QApplication>
25 #include <QMainWindow>
30 #include "icons/icon.h"
31 #include "messagebox.h"
34 #include <QTranslator>
35 #include <QLibraryInfo>
37 #include <QStandardPaths>
41 #if defined __WIN32__ || defined WINDOWS || defined _WIN32
45 #define USE_LOADLIBRARY
51 #include <gsl/gsl_errno.h>
54 my_gsl_err_handler (
const char *reason,
const char *file,
int line,
int gsl_errno) {
56 fprintf(stderr,
"GSL emitted an error for a reason:%s; %s, at %s:%d\n", reason, gsl_strerror(gsl_errno), file, line);
60 int load_module(
const char *filename, lt_ptr data) {
61 static_cast<std::deque<XString> *
>(data)->push_back(QString::fromLocal8Bit(filename));
66 int main(
int argc,
char *argv[]) {
67 char dummy_for_mlock[8192];
75 Q_INIT_RESOURCE(kame);
78 const char *description =
82 KAboutData aboutData(
"kame",
"", ki18n(
"KAME"),
83 VERSION, ki18n(description), KAboutData::License_GPL,
84 ki18n(
"(c) 2003-2014"), ki18n(
""),
"",
"kitagawa@phys.s.u-tokyo.ac.jp");
85 KCmdLineArgs::init( argc, argv, &aboutData );
87 KCmdLineOptions options;
88 options.add(
"logging", ki18n(
"log debugging info."));
89 options.add(
"mlockall", ki18n(
"never cause swapping, perhaps you need 'ulimit -l <MB>'"));
90 options.add(
"nomlock", ki18n(
"never use mlock"));
92 options.add(
"moduledir <path>", ki18n(
"search modules in <path> instead of the standard dirs"));
93 options.add(
"+[File]", ki18n(
"measurement file to open"));
95 KCmdLineArgs::addCmdLineOptions( options );
99 KGlobal::dirs()->addPrefix(
".");
101 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
102 g_bLogDbgPrint = args->isSet(
"logging");
103 g_bMLockAlways = args->isSet(
"mlockall");
104 g_bUseMLock = args->isSet(
"mlock");
105 QStringList module_dir = args->getOptionList(
"moduledir");
106 if(module_dir.isEmpty())
107 module_dir = KGlobal::dirs()->resourceDirs(
"lib");
109 XString mesfile = args->count() ? args->arg(0) :
"";
112 QApplication app(argc, argv);
113 QApplication::setApplicationName(
"kame");
114 QApplication::setApplicationVersion(VERSION);
116 QCommandLineParser parser;
117 parser.setApplicationDescription(
"KAME");
118 parser.addHelpOption();
119 parser.addVersionOption();
121 parser.addPositionalArgument(
"file", QCoreApplication::translate(
"main",
"Measurement file to open"));
123 QCommandLineOption logOption(QStringList() <<
"l" <<
"logging",
"Log debugging info.");
124 parser.addOption(logOption);
125 QCommandLineOption mlockAllOption(QStringList() <<
"m" <<
"mlockall",
126 "Never cause swapping, perhaps you need 'ulimit -l <MB>'");
127 parser.addOption(mlockAllOption);
128 QCommandLineOption noMLockOption(QStringList() <<
"n" <<
"nomlock",
"Never use mlock");
129 parser.addOption(noMLockOption);
131 QCommandLineOption moduleDirectoryOption(
"moduledir",
132 QCoreApplication::translate(
"main",
"search modules in <path> instead of the standard dirs"),
133 QCoreApplication::translate(
"main",
"path"));
134 parser.addOption(moduleDirectoryOption);
138 QStringList args = parser.positionalArguments();
140 g_bLogDbgPrint = parser.isSet(logOption);
141 g_bMLockAlways = parser.isSet(mlockAllOption);
142 g_bUseMLock = !parser.isSet(noMLockOption);
143 QStringList module_dir = parser.values(moduleDirectoryOption);
145 XString mesfile = args.count() ? args.at(0) :
"";
149 QTranslator qtTranslator;
150 qtTranslator.load(
"qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
151 app.installTranslator(&qtTranslator);
153 QTranslator appTranslator;
154 if( !appTranslator.load(
"kame_" + QLocale::system().name())) {
155 appTranslator.load(
"kame_" + QLocale::system().name(), app.applicationDirPath());
157 app.installTranslator(&appTranslator);
171 #if !defined __WIN32__ && !defined WINDOWS && !defined _WIN32
173 if(( mlockall(MCL_CURRENT) == 0)) {
174 dbgPrint(
"MLOCKALL succeeded.");
177 dbgPrint(formatString(
"MLOCKALL failed errno=%d.", errno));
181 if(isMemLockAvailable())
182 mlock(dummy_for_mlock,
sizeof(dummy_for_mlock));
189 if(cg_cpuSpec.verSSE < 2) {
190 fprintf(stderr,
"SSE2 is needed. Aborting.");
198 if(mesfile.length()) {
199 form->openMes(mesfile);
205 gsl_set_error_handler(&my_gsl_err_handler);
207 fprintf(stderr,
"Start processing events.\n");
212 fprintf(stderr,
"Initializing LTDL.\n");
215 LTDL_SET_PRELOADED_SYMBOLS();
218 if(module_dir.isEmpty())
219 module_dir = app.libraryPaths();
220 std::deque<XString> modules;
221 for(
auto it = module_dir.begin(); it != module_dir.end(); it++) {
223 #if defined KAME_COREMODULE_DIR_SURFIX
224 paths += *it + KAME_COREMODULE_DIR_SURFIX;
226 #if defined KAME_COREMODULE2_DIR_SURFIX
227 paths += *it + KAME_COREMODULE2_DIR_SURFIX;
229 paths += *it + KAME_MODULE_DIR_SURFIX;
232 for(
auto sit = paths.begin(); sit != paths.end(); sit++) {
234 lt_dladdsearchdir(sit->toLocal8Bit().data());
236 XMessageBox::post(
"Searching for modules in " + *sit, *g_pIconInfo);
238 lt_dlforeachfile(sit->toLocal8Bit().data(), &load_module, &modules);
240 #ifdef USE_LOADLIBRARY
241 QFileInfoList files = QDir(*sit).entryInfoList(QStringList(
"*.dll"), QDir::Files);
242 for(QFileInfoList::const_iterator it = files.constBegin(); it != files.constEnd(); ++it) {
243 modules.push_back(it->filePath());
249 for(
auto it = modules.begin(); it != modules.end(); it++) {
252 lt_dlhandle handle = lt_dlopenext(QString( *it).toLocal8Bit().data());
254 #ifdef USE_LOADLIBRARY
255 DWORD currerrmode = GetThreadErrorMode();
256 SetThreadErrorMode(currerrmode | SEM_FAILCRITICALERRORS, NULL);
257 HANDLE handle = LoadLibraryA(QString( *it).toLocal8Bit().data());
258 DWORD lasterr = GetLastError();
259 SetThreadErrorMode(currerrmode, NULL);
260 SetLastError(lasterr);
263 XMessageBox::post(
"Module \"" + *it +
"\" loaded", *g_pIconKame);
266 XMessageBox::post(
"Failure during loading module \"" + *it +
"\"", *g_pIconError);
271 const char *greeting =
"KAME ver:" VERSION
", built at " __DATE__
" " __TIME__;
272 fprintf(stderr,
"%s\n", greeting);
273 gMessagePrint(greeting);
275 int ret = app.exec();