Skip to content

Commit a1d7dfb

Browse files
Cleaner handling of termination signals on UNIX
1 parent 981e12d commit a1d7dfb

2 files changed

Lines changed: 13 additions & 44 deletions

File tree

src/frontend/qt_sdl/Window.cpp

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@
4141
#include <QVector>
4242
#include <QCommandLineParser>
4343
#include <QDesktopServices>
44-
#ifndef _WIN32
45-
#include <QGuiApplication>
46-
#include <QSocketNotifier>
47-
#include <unistd.h>
48-
#include <sys/socket.h>
49-
#include <signal.h>
50-
#endif
5144

5245
#include "main.h"
5346
#include "CheatsDialog.h"
@@ -213,17 +206,6 @@ static bool FileIsSupportedFiletype(const QString& filename, bool insideArchive
213206
}
214207

215208

216-
#ifndef _WIN32
217-
static int signalFd[2];
218-
QSocketNotifier *signalSn;
219-
220-
static void signalHandler(int)
221-
{
222-
char a = 1;
223-
write(signalFd[0], &a, sizeof(a));
224-
}
225-
#endif
226-
227209

228210
MainWindow::MainWindow(int id, EmuInstance* inst, QWidget* parent) :
229211
QMainWindow(parent),
@@ -236,26 +218,6 @@ MainWindow::MainWindow(int id, EmuInstance* inst, QWidget* parent) :
236218
enabledSaved(false),
237219
focused(true)
238220
{
239-
#ifndef _WIN32
240-
if (!parent)
241-
{
242-
if (socketpair(AF_UNIX, SOCK_STREAM, 0, signalFd))
243-
{
244-
qFatal("Couldn't create socketpair");
245-
}
246-
247-
signalSn = new QSocketNotifier(signalFd[1], QSocketNotifier::Read, this);
248-
connect(signalSn, SIGNAL(activated(int)), this, SLOT(onQuit()));
249-
250-
struct sigaction sa;
251-
252-
sa.sa_handler = signalHandler;
253-
sigemptyset(&sa.sa_mask);
254-
sa.sa_flags = 0;
255-
sa.sa_flags |= SA_RESTART;
256-
sigaction(SIGINT, &sa, 0);
257-
}
258-
#endif
259221

260222
showOSD = windowCfg.GetBool("ShowOSD");
261223

@@ -1673,10 +1635,6 @@ void MainWindow::onImportSavefile()
16731635

16741636
void MainWindow::onQuit()
16751637
{
1676-
#ifndef _WIN32
1677-
if (!parentWidget())
1678-
signalSn->setEnabled(false);
1679-
#endif
16801638
close();
16811639
}
16821640

src/frontend/qt_sdl/main.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <QSocketNotifier>
4242
#include <unistd.h>
4343
#include <sys/socket.h>
44-
#include <signal.h>
44+
#include <csignal>
4545
#endif
4646

4747
#include <SDL2/SDL.h>
@@ -266,6 +266,14 @@ bool MelonApplication::event(QEvent *event)
266266
return QApplication::event(event);
267267
}
268268

269+
#ifndef _WIN32
270+
static void signalHandler(int signal)
271+
{
272+
std::signal(signal, SIG_DFL);
273+
qApp->quit();
274+
}
275+
#endif
276+
269277
int main(int argc, char** argv)
270278
{
271279
sysTimer.start();
@@ -276,7 +284,7 @@ int main(int argc, char** argv)
276284

277285
qputenv("QT_SCALE_FACTOR", "1");
278286

279-
#ifdef _WIN32
287+
#if defined(_WIN32)
280288
#if QT_VERSION_MAJOR == 6
281289
// Allow using the system dark theme palette on Windows
282290
qputenv("QT_QPA_PLATFORM", "windows:darkmode=2");
@@ -306,6 +314,9 @@ int main(int argc, char** argv)
306314
freopen("NUL:", "w", stderr);
307315
}
308316
}
317+
#else
318+
std::signal(SIGINT, signalHandler);
319+
std::signal(SIGTERM, signalHandler);
309320
#endif
310321

311322
printf("melonDS " MELONDS_VERSION "\n");

0 commit comments

Comments
 (0)