1
2
3
4
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
#include "init.hpp"
/* Copyright (c) 2013-2017 Stanislaw Halik
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*/
#if defined(Q_CREATOR_RUN)
# pragma clang diagnostic ignored "-Wmain"
#endif
#include "migration/migration.hpp"
#include "options/options.hpp"
using namespace options;
#include "compat/library-path.hpp"
#include <memory>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <QApplication>
#include <QStyleFactory>
#include <QLocale>
#include <QTranslator>
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QString>
#include <QSysInfo>
#include <QDebug>
#if /* denormal control */ \
/* GNU */ defined __x86_64__ || defined __SSE2__ || \
/* MSVC */ defined _M_AMD64 || (defined _M_IX86_FP && _M_IX86_FP >= 2)
# include <xmmintrin.h>
# include <pmmintrin.h>
# include <cfloat>
#define OTR_HAS_DENORM_CONTROL
void set_fp_mask()
{
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
_MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
_MM_SET_ROUNDING_MODE(_MM_ROUND_NEAREST);
_MM_SET_EXCEPTION_MASK(_MM_MASK_MASK);
}
#endif
void set_qt_style()
{
#if defined _WIN32
if (QSysInfo::WindowsVersion == QSysInfo::WV_XP)
return;
#endif
#if defined _WIN32 || defined __APPLE__
// our layouts on OSX make some control wrongly sized -sh 20160908
{
const char* const preferred[] { "fusion", "windowsvista", "macintosh" };
for (const char* style_name : preferred)
{
QStyle* s = QStyleFactory::create(style_name);
if (s)
{
QApplication::setStyle(s);
break;
}
}
}
#endif
}
#ifdef _WIN32
void qdebug_to_console(QtMsgType, const QMessageLogContext& ctx, const QString &msg)
{
const unsigned short* const str_ = msg.utf16();
auto str = reinterpret_cast<const wchar_t* const>(str_);
static_assert(sizeof(*str_) == sizeof(*str), "");
std::fflush(stderr);
if (ctx.function)
std::fprintf(stderr, "[%s:%d%s]: %ls\n", ctx.file, ctx.line, ctx.function, str);
else if (ctx.file)
std::fprintf(stderr, "[%s:%d]: %ls\n", ctx.file, ctx.line, str);
else
std::fprintf(stderr, "%ls\n", str);
std::fflush(stderr);
}
void add_win32_path()
{
// see https://software.intel.com/en-us/articles/limitation-to-the-length-of-the-system-path-variable
static char env_path[4096] { '\0', };
{
QString lib_path = OPENTRACK_BASE_PATH;
lib_path.replace("/", "\\");
const QByteArray lib_path_ = QFile::encodeName(lib_path);
QString mod_path = OPENTRACK_BASE_PATH + OPENTRACK_LIBRARY_PATH;
mod_path.replace("/", "\\");
const QByteArray mod_path_ = QFile::encodeName(mod_path);
const char* contents[] {
"PATH=",
lib_path_.constData(),
";",
mod_path_.constData(),
";",
getenv("PATH"),
};
bool ok = true;
for (const char* ptr : contents)
{
if (ptr)
strcat_s(env_path, sizeof(env_path), ptr);
if (!ptr || ptr[0] == '\0' || env_path[0] == '\0')
{
qDebug() << "bad path element"
<< (ptr == nullptr ? "<null>" : ptr);
ok = false;
break;
}
}
if (ok)
{
const int error = _putenv(env_path);
if (error)
qDebug() << "can't _putenv win32 path";
}
else
qDebug() << "can't set win32 path";
}
}
#include <windows.h>
void attach_parent_console()
{
std::fflush(stdin);
std::fflush(stderr);
(void)qInstallMessageHandler(qdebug_to_console);
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
_wfreopen(L"CON", L"w", stdout);
_wfreopen(L"CON", L"w", stderr);
_wfreopen(L"CON", L"r", stdin);
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
freopen("CON", "w", stderr);
// skip prompt in cmd.exe window
fprintf(stderr, "\n");
fflush(stderr);
}
}
#endif
int run_window(QApplication& app, std::unique_ptr<QWidget> main_window)
{
if (!main_window->isEnabled())
{
qDebug() << "exit before window created";
return 2;
}
app.setQuitOnLastWindowClosed(true);
int status = app.exec();
return status;
}
int otr_main(int argc, char** argv, std::function<QWidget*()> make_main_window)
{
#if defined OTR_HAS_DENORM_CONTROL
set_fp_mask();
#endif
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QCoreApplication::setAttribute(Qt::AA_X11InitThreads, true);
QApplication app(argc, argv);
#ifdef _WIN32
add_win32_path();
attach_parent_console();
#endif
QDir::setCurrent(OPENTRACK_BASE_PATH);
set_qt_style();
QTranslator t;
{
const char* forced_locale = getenv("OTR_FORCE_LANG");
if (forced_locale)
{
QLocale::setDefault(QLocale(forced_locale)); // force i18n for testing
qDebug() << "locale:" << forced_locale;
}
const bool no_i18n = group::with_global_settings_object([](QSettings& s) {
return !s.value("disable-translation", false).toBool();
});
if (forced_locale || !no_i18n)
{
(void) t.load(QLocale(), "", "", OPENTRACK_BASE_PATH + "/" OPENTRACK_I18N_PATH, ".qm");
(void) QCoreApplication::installTranslator(&t);
}
}
int ret = run_window(app, std::unique_ptr<QWidget>(make_main_window()));
#if 0
// msvc crashes in Qt plugin system's dtor
// Note: QLibrary::PreventUnloadHint seems to workaround it
#if defined _MSC_VER
TerminateProcess(GetCurrentProcess(), 0);
#endif
#endif
return ret;
}
|