Skip to content

Commit f1755b1

Browse files
committed
Shutdown DiscordSocial before exiting main. Move Qt app into its own function.
1 parent bcbafe2 commit f1755b1

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,14 @@ void set_working_directory(){
4848
}
4949
}
5050

51-
int main(int argc, char *argv[]){
52-
setup_crash_handler();
53-
54-
#if defined(__linux) || defined(__APPLE__)
55-
// By default Qt uses native menubar but this only works on Windows.
56-
// We use menubar in our ButtonDiagram window to choose which controller's button mapping image to show.
57-
// So we fix it by don't using native menubar on non-Windows OS.
58-
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
59-
#endif
6051

61-
//#if QT_VERSION_MAJOR == 5 // AA_EnableHighDpiScaling is deprecated in Qt6
62-
// QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
63-
//#endif
52+
int run_qt(int argc, char *argv[]){
6453
QApplication application(argc, argv);
6554

66-
Logger& logger = global_logger_tagged();
67-
68-
logger.log("================================================================================");
69-
logger.log("Starting Program...");
70-
71-
qRegisterMetaType<size_t>("size_t");
72-
qRegisterMetaType<uint8_t>("uint8_t");
73-
qRegisterMetaType<std::string>("std::string");
74-
qRegisterMetaType<Resolution>("Resolution");
75-
76-
OutputRedirector redirect_stdout(std::cout, "stdout", Color());
77-
OutputRedirector redirect_stderr(std::cerr, "stderr", COLOR_RED);
78-
7955
// Preload all the cameras now so we don't hang the UI later on.
8056
get_all_cameras();
8157

82-
QDir().mkpath(QString::fromStdString(SETTINGS_PATH()));
83-
QDir().mkpath(QString::fromStdString(SCREENSHOTS_PATH()));
58+
Logger& logger = global_logger_tagged();
8459

8560
// Several novice developers struggled to build and run the program due to missing Resources folder.
8661
// Add this check to pop a message box when Resources folder is missing.
@@ -116,6 +91,51 @@ int main(int argc, char *argv[]){
11691

11792
check_new_version(logger);
11893

94+
set_working_directory();
95+
96+
// Run this asynchronously to we don't block startup.
97+
std::unique_ptr<AsyncTask> task = send_all_unsent_reports(logger, true);
98+
99+
100+
MainWindow w;
101+
w.show();
102+
w.raise(); // bring the window to front on macOS
103+
set_permissions(w);
104+
105+
return application.exec();
106+
}
107+
108+
109+
int main(int argc, char *argv[]){
110+
setup_crash_handler();
111+
112+
#if defined(__linux) || defined(__APPLE__)
113+
// By default Qt uses native menubar but this only works on Windows.
114+
// We use menubar in our ButtonDiagram window to choose which controller's button mapping image to show.
115+
// So we fix it by don't using native menubar on non-Windows OS.
116+
QCoreApplication::setAttribute(Qt::AA_DontUseNativeMenuBar);
117+
#endif
118+
119+
//#if QT_VERSION_MAJOR == 5 // AA_EnableHighDpiScaling is deprecated in Qt6
120+
// QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
121+
//#endif
122+
123+
Logger& logger = global_logger_tagged();
124+
125+
logger.log("================================================================================");
126+
logger.log("Starting Program...");
127+
128+
qRegisterMetaType<size_t>("size_t");
129+
qRegisterMetaType<uint8_t>("uint8_t");
130+
qRegisterMetaType<std::string>("std::string");
131+
qRegisterMetaType<Resolution>("Resolution");
132+
133+
OutputRedirector redirect_stdout(std::cout, "stdout", Color());
134+
OutputRedirector redirect_stderr(std::cerr, "stderr", COLOR_RED);
135+
136+
QDir().mkpath(QString::fromStdString(SETTINGS_PATH()));
137+
QDir().mkpath(QString::fromStdString(SCREENSHOTS_PATH()));
138+
119139
Integration::DiscordIntegrationSettingsOption& discord_settings = GlobalSettings::instance().DISCORD->integration;
120140
if (discord_settings.run_on_start){
121141
#ifdef PA_DPP
@@ -130,23 +150,18 @@ int main(int argc, char *argv[]){
130150
}
131151
#endif
132152

133-
set_working_directory();
134153

135-
// Run this asynchronously to we don't block startup.
136-
std::unique_ptr<AsyncTask> task = send_all_unsent_reports(logger, true);
154+
int ret = run_qt(argc, argv);
137155

138-
int ret = 0;
139-
{
140-
MainWindow w;
141-
w.show();
142-
w.raise(); // bring the window to front on macOS
143-
set_permissions(w);
144-
ret = application.exec();
145-
}
146156

147157
// Write program settings back to the json file.
148158
PERSISTENT_SETTINGS().write();
149159

160+
161+
#ifdef PA_SOCIAL_SDK
162+
Integration::DiscordSocialSDK::DiscordSocial::instance().stop();
163+
#endif
164+
150165
#ifdef PA_DPP
151166
Integration::DppClient::Client::instance().disconnect();
152167
#endif

SerialPrograms/Source/Integrations/DiscordSocial/DiscordSocial.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ DiscordSocial& DiscordSocial::instance(){
1717
return instance;
1818
}
1919

20+
void DiscordSocial::stop(){
21+
m_running.store(false, std::memory_order_release);
22+
m_thread.join();
23+
if (m_client) m_client.reset();
24+
}
2025
void DiscordSocial::run(){
2126
auto client = std::make_shared<Client>();
2227
if (!client){

SerialPrograms/Source/Integrations/DiscordSocial/DiscordSocial.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ class DiscordSocial{
1616
public:
1717
DiscordSocial() : m_running(false) {}
1818
~DiscordSocial(){
19-
m_running.store(false, std::memory_order_release);
20-
m_thread.join();
21-
if (m_client) m_client.reset();
19+
stop();
2220
}
2321

2422
static DiscordSocial& instance();
2523
void run();
24+
void stop();
2625

2726
private:
2827
Logger& logger();

0 commit comments

Comments
 (0)