Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qt: fix shutdown on MacOS #2440

Merged
merged 3 commits into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ QT_MOC_CPP = \
qt/moc_favoritespage.cpp \
qt/moc_guiutil.cpp \
qt/moc_intro.cpp \
qt/moc_macdockiconhandler.cpp \
qt/moc_macnotificationhandler.cpp \
qt/moc_monitoreddatamapper.cpp \
qt/moc_noresult.cpp \
qt/moc_notificator.cpp \
Expand Down Expand Up @@ -199,6 +197,12 @@ QT_MOC_CPP = \
qt/voting/moc_votingmodel.cpp \
qt/voting/moc_votingpage.cpp

if TARGET_DARWIN
QT_MOC_CPP += \
qt/moc_macdockiconhandler.cpp \
qt/moc_macnotificationhandler.cpp
endif

GRIDCOIN_MM = \
qt/macdockiconhandler.mm \
qt/macnotificationhandler.mm \
Expand Down
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ bool AppInit2(ThreadHandlerPtr threads)

uiInterface.InitMessage(_("Loading block index..."));
LogPrintf("Loading block index...");
if (!LoadBlockIndex())
if (!LoadBlockIndex() && !fRequestShutdown)
return InitError(_("Error loading blkindex.dat"));

// as LoadBlockIndex can take several minutes, it's possible the user
Expand Down
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ BitcoinGUI::BitcoinGUI(QWidget* parent)
#ifdef Q_OS_MAC
m_app_nap_inhibitor = new CAppNapInhibitor;
m_app_nap_inhibitor->disableAppNap();

m_dock_shutdown_handler = new MacDockShutdownHandler();
#endif

// Accept D&D of URIs
Expand Down Expand Up @@ -239,6 +241,7 @@ BitcoinGUI::~BitcoinGUI()
trayIcon->hide();
#ifdef Q_OS_MAC
delete m_app_nap_inhibitor;
delete m_dock_shutdown_handler;
delete appMenuBar;
#endif
}
Expand Down
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#ifdef Q_OS_MAC
#include <qt/macos_appnap.h>

class MacDockShutdownHandler;
#endif

class TransactionTableModel;
Expand Down Expand Up @@ -160,6 +162,7 @@ class BitcoinGUI : public QMainWindow

#ifdef Q_OS_MAC
CAppNapInhibitor* m_app_nap_inhibitor = nullptr;
MacDockShutdownHandler* m_dock_shutdown_handler = nullptr;
#endif
// name extension to change icons according to stylesheet
QString sSheet;
Expand Down
9 changes: 9 additions & 0 deletions src/qt/macdockiconhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <QMainWindow>
#include <QObject>
#include <objc/objc.h>
#include <objc/message.h>

QT_BEGIN_NAMESPACE
class QIcon;
Expand Down Expand Up @@ -44,4 +46,11 @@ class MacDockIconHandler : public QObject
QMainWindow *mainWindow;
};

class MacDockShutdownHandler{
public:
MacDockShutdownHandler();

static int handleShutdown(id self, SEL _cmd, ...);
};

#endif // BITCOIN_QT_MACDOCKICONHANDLER_H
19 changes: 19 additions & 0 deletions src/qt/macdockiconhandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#undef slots
#include <Cocoa/Cocoa.h>

extern bool fRequestShutdown;

@interface DockIconClickEventHandler : NSObject
{
MacDockIconHandler* dockIconHandler;
Expand Down Expand Up @@ -44,6 +46,23 @@ - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAp

@end

MacDockShutdownHandler::MacDockShutdownHandler() {
Class delClass = (Class)[[[NSApplication sharedApplication] delegate] class];
SEL closeHandle = sel_registerName("applicationShouldTerminate:");
if (class_getInstanceMethod(delClass, closeHandle)) {
class_replaceMethod(delClass, closeHandle, (IMP)MacDockShutdownHandler::handleShutdown, "B@:");
} else {
class_addMethod(delClass, closeHandle, (IMP)MacDockShutdownHandler::handleShutdown,"B@:");
}
}

int MacDockShutdownHandler::handleShutdown(id self, SEL _cmd, ...) {
// Proper shutdown is possible, if MacOS does not decide to
// instantly send us to the shadow realm.
fRequestShutdown = true;
return false;
}

MacDockIconHandler::MacDockIconHandler() : QObject()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Expand Down