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

Add support on MacOS to detect when the accessibility permission is removed #1636

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions doc/newsfragments/mac-accessibility-permission.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
+ Add support to detect when the accessibility permission has been removed on MacOS and terminate the application.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(GUI_COMMON_HEADER_FILES

set(GUI_SOURCE_FILES
src/AboutDialog.cpp
src/AccessibilityPermissionObserver.cpp
src/ActionDialog.cpp
src/AddClientDialog.cpp
src/AppConfig.cpp
Expand Down Expand Up @@ -61,6 +62,7 @@ set(GUI_SOURCE_FILES

set(GUI_HEADER_FILES
src/AboutDialog.h
src/AccessibilityPermissionObserver.h
src/ActionDialog.h
src/AddClientDialog.h
src/AppConfig.h
Expand Down
52 changes: 52 additions & 0 deletions src/gui/src/AccessibilityPermissionObserver.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2022 Duncan Cunningham
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "AccessibilityPermissionObserver.h"

#include <QApplication>
#include <QTimer>

#if defined(Q_OS_MAC)
#include <Carbon/Carbon.h>
#endif

AccessibilityPermissionObserver::AccessibilityPermissionObserver(QObject* parent)
: QObject(parent)
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 // mavericks
m_pTimer = new QTimer(this);

connect(m_pTimer, SIGNAL(timeout()), this, SLOT(checkAccessibilityPermissions()));
#endif
}

void AccessibilityPermissionObserver::start()
{
if (m_pTimer) {
m_pTimer->start(1000);
}
}

void AccessibilityPermissionObserver::checkAccessibilityPermissions()
{
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 // mavericks
// There's no API to be notified when this permission changes, so we have to poll it.
if (!AXIsProcessTrusted()) {
QApplication::quit();
}
#endif
}
34 changes: 34 additions & 0 deletions src/gui/src/AccessibilityPermissionObserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* barrier -- mouse and keyboard sharing utility
* Copyright (C) 2022 Duncan Cunningham
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file LICENSE that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <QObject>

class QTimer;

class AccessibilityPermissionObserver: public QObject
{
Q_OBJECT
public:
explicit AccessibilityPermissionObserver(QObject* parent = nullptr);
void start();
public slots:
void checkAccessibilityPermissions();
private:
QTimer* m_pTimer;
};
14 changes: 13 additions & 1 deletion src/gui/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "MainWindow.h"

#include "AboutDialog.h"
#include "AccessibilityPermissionObserver.h"
#include "ServerConfigDialog.h"
#include "SettingsDialog.h"
#include "ZeroconfService.h"
Expand Down Expand Up @@ -118,7 +119,8 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) :
m_SuppressEmptyServerWarning(false),
m_ExpectedRunningState(kStopped),
m_pSslCertificate(NULL),
m_pLogWindow(new LogWindow(nullptr))
m_pLogWindow(new LogWindow(nullptr)),
m_pAccessibilityPermissionObserver(nullptr)
{
// explicitly unset DeleteOnClose so the window can be show and hidden
// repeatedly until Barrier is finished
Expand Down Expand Up @@ -503,6 +505,11 @@ void MainWindow::startBarrier()
bool desktopMode = appConfig().processMode() == Desktop;
bool serviceMode = appConfig().processMode() == Service;

#if defined(Q_OS_MAC)
m_pAccessibilityPermissionObserver = new AccessibilityPermissionObserver(this);
m_pAccessibilityPermissionObserver->start();
#endif

appendLogDebug("starting process");
m_ExpectedRunningState = kStarted;
setBarrierState(barrierConnecting);
Expand Down Expand Up @@ -767,6 +774,11 @@ void MainWindow::stopBarrier()

// reset so that new connects cause auto-hide.
m_AlreadyHidden = false;

#if defined(Q_OS_MAC)
delete m_pAccessibilityPermissionObserver;
m_pAccessibilityPermissionObserver = nullptr;
#endif
}

void MainWindow::stopService()
Expand Down
2 changes: 2 additions & 0 deletions src/gui/src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class QTemporaryFile;
class QMessageBox;
class QAbstractButton;

class AccessibilityPermissionObserver;
class LogDialog;
class QBarrierApplication;
class SetupWizard;
Expand Down Expand Up @@ -198,6 +199,7 @@ public slots:
SslCertificate* m_pSslCertificate;
QStringList m_PendingClientNames;
LogWindow *m_pLogWindow;
AccessibilityPermissionObserver* m_pAccessibilityPermissionObserver;

bool m_fingerprint_expanded = false;

Expand Down