forked from GLEECBTC/gleec-wallet
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
249 lines (212 loc) · 7.34 KB
/
main.cpp
File metadata and controls
249 lines (212 loc) · 7.34 KB
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
240
241
242
243
244
245
246
247
248
249
#include <csignal>
#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QQmlApplicationEngine>
#include <QScreen>
#include <QWindow>
#include <Qaterial/Qaterial.hpp>
#include <QtQml>
#include <QtWebEngine>
#define QZXING_QML
#include "QZXing.h"
//! PCH Headers
#include "atomicdex/pch.hpp"
//! Deps
#include <sodium/core.h>
#include <wally.hpp>
#if defined(linux)
# define BOOST_STACKTRACE_USE_ADDR2LINE
# include <boost/stacktrace.hpp>
#endif
//! Project Headers
#include "atomicdex/app.hpp"
#include "atomicdex/models/qt.portfolio.model.hpp"
#include "atomicdex/utilities/kill.hpp"
#ifdef __APPLE__
# include "atomicdex/platform/osx/manager.hpp"
#endif
inline constexpr size_t g_qsize_spdlog = 10240;
inline constexpr size_t g_spdlog_thread_count = 2;
inline constexpr size_t g_spdlog_max_file_size = 7777777;
inline constexpr size_t g_spdlog_max_file_rotation = 3;
void
signal_handler(int signal)
{
spdlog::trace("sigabort received, cleaning mm2.service");
atomic_dex::kill_executable("mm2.service");
#if defined(linux)
boost::stacktrace::safe_dump_to("./backtrace.dump");
#endif
std::exit(signal);
}
static void
connect_signals_handler()
{
spdlog::info("connecting signal SIGABRT to the signal handler");
#if defined(linux)
if (boost::filesystem::exists("./backtrace.dump"))
{
// there is a backtrace
std::ifstream ifs("./backtrace.dump");
boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace::from_dump(ifs);
std::cout << "Previous run crashed:\n" << st << std::endl;
// cleaning up
ifs.close();
boost::filesystem::remove("./backtrace.dump");
}
#endif
std::signal(SIGABRT, signal_handler);
std::signal(SIGSEGV, signal_handler);
}
static void
init_wally()
{
[[maybe_unused]] auto wally_res = wally_init(0);
assert(wally_res == WALLY_OK);
spdlog::info("wally successfully initialized");
}
static void
init_sodium()
{
//! Sodium Initialization
[[maybe_unused]] auto sodium_return_value = sodium_init();
assert(sodium_return_value == 0); //< This is not executed when build = Release
spdlog::info("libsodium successfully initialized");
}
static void
clean_previous_run()
{
spdlog::info("cleaning previous mm2 instance");
atomic_dex::kill_executable("mm2");
}
static void
init_logging()
{
//! Log Initialization
std::string path = get_atomic_dex_current_log_file().string();
spdlog::init_thread_pool(g_qsize_spdlog, g_spdlog_thread_count);
auto tp = spdlog::thread_pool();
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(path.c_str(), g_spdlog_max_file_size, g_spdlog_max_file_rotation);
std::vector<spdlog::sink_ptr> sinks{stdout_sink, rotating_sink};
auto logger = std::make_shared<spdlog::async_logger>("log_mt", sinks.begin(), sinks.end(), tp, spdlog::async_overflow_policy::block);
spdlog::register_logger(logger);
spdlog::set_default_logger(logger);
spdlog::set_level(spdlog::level::trace);
spdlog::set_pattern("[%H:%M:%S %z] [%L] [thr %t] %v");
spdlog::info("Logger successfully initialized");
}
static void
init_dpi()
{
spdlog::info("initializing high dpi support");
bool should_floor = false;
#if defined(_WIN32) || defined(WIN32) || defined(__linux__)
{
int ac = 0;
QApplication tmp(ac, nullptr);
double min_window_size = 800.0;
auto screens = tmp.screens();
for (auto&& cur_screen: screens)
{
spdlog::trace("physical dpi: {}", cur_screen->physicalDotsPerInch());
spdlog::trace("logical dpi: {}", cur_screen->logicalDotsPerInch());
double scale = cur_screen->logicalDotsPerInch() / 96.0;
spdlog::trace("scale: {}", scale);
double height = cur_screen->availableSize().height();
spdlog::trace("height: {}", height);
if (scale * min_window_size > height)
{
should_floor = true;
spdlog::trace("should floor");
}
}
}
#endif
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(
should_floor ? Qt::HighDpiScaleFactorRoundingPolicy::Floor : Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
QGuiApplication::setAttribute(should_floor ? Qt::AA_DisableHighDpiScaling : Qt::AA_EnableHighDpiScaling);
}
static void
clean_wally()
{
[[maybe_unused]] auto wallet_exit_res = wally_cleanup(0);
assert(wallet_exit_res == WALLY_OK);
spdlog::info("wally successfully cleaned");
}
static void
init_timezone_db()
{
spdlog::info("Init timezone db");
#if defined(_WIN32) || defined(WIN32)
using namespace std::string_literals;
auto install_db_tz_path = std::make_unique<fs::path>(ag::core::assets_real_path() / "tools" / "timezone" / "tzdata");
std::cout << install_db_tz_path->string() << std::endl;
date::set_install(install_db_tz_path->string());
#endif
}
#if defined(WINDOWS_RELEASE_MAIN)
INT WINAPI
WinMain([[maybe_unused]] HINSTANCE hInst, HINSTANCE, [[maybe_unused]] LPSTR strCmdLine, INT)
#else
int
main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
#endif
{
#if defined(WINDOWS_RELEASE_MAIN)
int argc = __argc;
char** argv = __argv;
#endif
init_logging();
connect_signals_handler();
init_timezone_db();
init_wally();
init_sodium();
clean_previous_run();
init_dpi();
//! App declaration
atomic_dex::application atomic_app;
//! QT
QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QtWebEngine::initialize();
std::shared_ptr<QApplication> app = std::make_shared<QApplication>(argc, argv);
app->setOrganizationName("KomodoPlatform");
app->setOrganizationDomain("com");
QQmlApplicationEngine engine;
atomic_app.set_qt_app(app, &engine);
//! QT QML
engine.addImportPath("qrc:///");
QZXing::registerQMLTypes();
QZXing::registerQMLImageProvider(engine);
engine.rootContext()->setContextProperty("atomic_app", &atomic_app);
// Load Qaterial.
qaterial::loadQmlResources(false);
// qaterial::registerQmlTypes("Qaterial", 1, 0);
engine.addImportPath("qrc:/atomic_defi_design/imports");
engine.addImportPath("qrc:/atomic_defi_design/Constants");
qmlRegisterSingletonType(QUrl("qrc:/atomic_defi_design/qml/Constants/General.qml"), "App", 1, 0, "General");
qmlRegisterSingletonType(QUrl("qrc:/atomic_defi_design/qml/Constants/Style.qml"), "App", 1, 0, "Style");
qmlRegisterSingletonType(QUrl("qrc:/atomic_defi_design/qml/Constants/API.qml"), "App", 1, 0, "API");
qRegisterMetaType<t_portfolio_roles>("PortfolioRoles");
const QUrl url(QStringLiteral("qrc:/atomic_defi_design/qml/main.qml"));
QObject::connect(
&engine, &QQmlApplicationEngine::objectCreated, app.get(),
[url](QObject* obj, const QUrl& objUrl) {
if ((obj == nullptr) && url == objUrl)
{
QCoreApplication::exit(-1);
}
},
Qt::QueuedConnection);
engine.load(url);
#ifdef __APPLE__
QWindowList windows = QGuiApplication::allWindows();
QWindow* win = windows.first();
atomic_dex::mac_window_setup(win->winId());
#endif
atomic_app.launch();
auto res = app->exec();
clean_wally();
return res;
}