Skip to content

Commit 3f0044d

Browse files
tresfrubiefawn
andauthored
Handle SIGINT (LMMS#7698)
Handle SIGINT Allows Ctrl + C to close LMMS for unix-like environments that --------- Co-authored-by: Fawn Sannar <rubiefawn@gmail.com>
1 parent 1721534 commit 3f0044d

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

include/GuiApplication.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#define LMMS_GUI_GUI_APPLICATION_H
2727

2828
#include <QObject>
29+
#include <QSocketNotifier>
2930

3031
#include "lmms_export.h"
3132
#include "lmmsconfig.h"
@@ -53,10 +54,13 @@ class LMMS_EXPORT GuiApplication : public QObject
5354
~GuiApplication() override;
5455

5556
static GuiApplication* instance();
57+
static void sigintHandler(int);
5658
#ifdef LMMS_BUILD_WIN32
5759
static QFont getWin32SystemFont();
5860
#endif
5961

62+
void createSocketNotifier();
63+
6064
MainWindow* mainWindow() { return m_mainWindow; }
6165
MixerView* mixerView() { return m_mixerView; }
6266
SongEditorWindow* songEditor() { return m_songEditor; }
@@ -67,11 +71,15 @@ class LMMS_EXPORT GuiApplication : public QObject
6771
AutomationEditorWindow* automationEditor() { return m_automationEditor; }
6872
ControllerRackView* getControllerRackView() { return m_controllerRackView; }
6973

74+
//! File descriptors for unix socketpair, used to receive SIGINT
75+
static inline int s_sigintFd[2];
76+
7077
public slots:
7178
void displayInitProgress(const QString &msg);
7279

7380
private slots:
7481
void childDestroyed(QObject *obj);
82+
void sigintOccurred();
7583

7684
private:
7785
static GuiApplication* s_instance;
@@ -86,6 +94,7 @@ private slots:
8694
MicrotunerConfig* m_microtunerConfig;
8795
ControllerRackView* m_controllerRackView;
8896
QLabel* m_loadingProgressLabel;
97+
QSocketNotifier* m_sigintNotifier;
8998
};
9099

91100
// Short-hand function

src/core/main.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
#include <sys/prctl.h>
5656
#endif
5757

58-
#include <csignal>
58+
#include <csignal> // To register the signal handler
5959

6060
#include "MainApplication.h"
6161
#include "ConfigManager.h"
@@ -76,12 +76,12 @@
7676
#include <fenv.h> // For feenableexcept
7777
#include <execinfo.h> // For backtrace and backtrace_symbols_fd
7878
#include <unistd.h> // For STDERR_FILENO
79-
#include <csignal> // To register the signal handler
8079
#endif
8180

8281

8382
#ifdef LMMS_DEBUG_FPE
84-
void signalHandler( int signum ) {
83+
void sigfpeHandler(int signum)
84+
{
8585

8686
// Get a back trace
8787
void *array[10];
@@ -315,8 +315,9 @@ int main( int argc, char * * argv )
315315

316316
// Install the trap handler
317317
// register signal SIGFPE and signal handler
318-
signal(SIGFPE, signalHandler);
318+
signal(SIGFPE, sigfpeHandler);
319319
#endif
320+
signal(SIGINT, gui::GuiApplication::sigintHandler);
320321

321322
#ifdef LMMS_BUILD_WIN32
322323
// Don't touch redirected streams here

src/gui/GuiApplication.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@
4141
#include "SongEditor.h"
4242

4343
#include <QApplication>
44+
#include <QDebug>
4445
#include <QDir>
4546
#include <QtGlobal>
4647
#include <QLabel>
4748
#include <QMessageBox>
4849
#include <QSplashScreen>
50+
#include <QSocketNotifier>
51+
#include <csignal>
4952

5053
#ifdef LMMS_BUILD_WIN32
54+
#include <io.h>
55+
#include <stdio.h>
5156
#include <windows.h>
57+
#else
58+
#include <sys/socket.h>
59+
#include <unistd.h>
5260
#endif
5361

5462
namespace lmms
@@ -75,6 +83,9 @@ GuiApplication* GuiApplication::instance()
7583

7684
GuiApplication::GuiApplication()
7785
{
86+
// Immediately register our SIGINT handler
87+
createSocketNotifier();
88+
7889
// prompt the user to create the LMMS working directory (e.g. ~/Documents/lmms) if it doesn't exist
7990
if ( !ConfigManager::inst()->hasWorkingDir() &&
8091
QMessageBox::question( nullptr,
@@ -240,6 +251,52 @@ void GuiApplication::childDestroyed(QObject *obj)
240251
}
241252
}
242253

254+
/** \brief Called from main when SIGINT is fired
255+
*
256+
* Unix signal handlers can only call async-signal-safe functions:
257+
* write(fd) --> QSocketNotifier --> SLOT sigintOccurred()
258+
*
259+
* See https://doc.qt.io/qt-6/unix-signals.html
260+
*/
261+
void GuiApplication::sigintHandler(int)
262+
{
263+
#ifdef LMMS_BUILD_WIN32
264+
char message[] = "Sorry, SIGINT is unhandled on this platform\n";
265+
std::ignore = _write(_fileno(stderr), message, sizeof(message));
266+
#else
267+
char a = 1;
268+
std::ignore = ::write(s_sigintFd[0], &a, sizeof(a));
269+
#endif
270+
}
271+
272+
// Create our unix signal notifiers
273+
void GuiApplication::createSocketNotifier()
274+
{
275+
#ifdef LMMS_BUILD_WIN32
276+
// no-op
277+
#else
278+
if (::socketpair(AF_UNIX, SOCK_STREAM, 0, s_sigintFd))
279+
{
280+
qFatal("Couldn't create SIGINT socketpair");
281+
return;
282+
}
283+
284+
// Listen on the file descriptor for SIGINT
285+
m_sigintNotifier = new QSocketNotifier(s_sigintFd[1], QSocketNotifier::Read, this);
286+
connect(m_sigintNotifier, SIGNAL(activated(QSocketDescriptor)), this, SLOT(sigintOccurred()), Qt::QueuedConnection);
287+
#endif
288+
}
289+
290+
// Handle the shutdown event
291+
void GuiApplication::sigintOccurred()
292+
{
293+
m_sigintNotifier->setEnabled(false);
294+
qDebug() << "Shutting down...";
295+
// cleanup, etc
296+
qApp->exit(3);
297+
m_sigintNotifier->setEnabled(true);
298+
}
299+
243300
#ifdef LMMS_BUILD_WIN32
244301
/*!
245302
* @brief Returns the Windows System font.

0 commit comments

Comments
 (0)