Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6945 from spycrab/qt_sighandler
Qt/Unix: Implement signal handler
- Loading branch information
Showing
5 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,55 @@ | |||
// Copyright 2018 Dolphin Emulator Project | |||
// Licensed under GPLv2+ | |||
// Refer to the license.txt file included. | |||
|
|||
#include "DolphinQt2/QtUtils/SignalDaemon.h" | |||
|
|||
#include <sys/socket.h> | |||
#include <sys/types.h> | |||
#include <unistd.h> | |||
|
|||
#include <QSocketNotifier> | |||
|
|||
int SignalDaemon::s_sigterm_fd[2]; | |||
|
|||
static constexpr char message[] = | |||
"\nA signal was received. A second signal will force Dolphin to stop.\n"; | |||
|
|||
SignalDaemon::SignalDaemon(QObject* parent) : QObject(parent) | |||
{ | |||
if (socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigterm_fd)) | |||
qFatal("Couldn't create TERM socketpair"); | |||
|
|||
m_term = new QSocketNotifier(s_sigterm_fd[1], QSocketNotifier::Read, this); | |||
connect(m_term, &QSocketNotifier::activated, this, &SignalDaemon::OnNotifierActivated); | |||
} | |||
|
|||
SignalDaemon::~SignalDaemon() | |||
{ | |||
close(s_sigterm_fd[0]); | |||
close(s_sigterm_fd[1]); | |||
} | |||
|
|||
void SignalDaemon::OnNotifierActivated() | |||
{ | |||
m_term->setEnabled(false); | |||
|
|||
char tmp; | |||
if (read(s_sigterm_fd[1], &tmp, sizeof(char))) | |||
{ | |||
} | |||
|
|||
m_term->setEnabled(true); | |||
|
|||
emit InterruptReceived(); | |||
} | |||
|
|||
void SignalDaemon::HandleInterrupt(int) | |||
{ | |||
write(STDERR_FILENO, message, sizeof(message)); | |||
|
|||
char a = 1; | |||
if (write(s_sigterm_fd[0], &a, sizeof(a))) | |||
{ | |||
} | |||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
// Copyright 2018 Dolphin Emulator Project | |||
// Licensed under GPLv2+ | |||
// Refer to the license.txt file included. | |||
|
|||
#pragma once | |||
|
|||
#include <QObject> | |||
|
|||
class QSocketNotifier; | |||
|
|||
// Loosely based on https://doc.qt.io/qt-5.9/unix-signals.html | |||
class SignalDaemon : public QObject | |||
{ | |||
Q_OBJECT | |||
|
|||
public: | |||
explicit SignalDaemon(QObject* parent); | |||
~SignalDaemon(); | |||
|
|||
static void HandleInterrupt(int); | |||
|
|||
signals: | |||
void InterruptReceived(); | |||
|
|||
private: | |||
void OnNotifierActivated(); | |||
|
|||
static int s_sigterm_fd[2]; | |||
|
|||
QSocketNotifier* m_term; | |||
}; |