feat(ui): Show warning if Input Monitoring is not allowed

Related to #125
This commit is contained in:
Lars Toenning
2023-12-12 11:12:33 +01:00
parent 45229f1087
commit 46e6eaba7f
4 changed files with 32 additions and 0 deletions

View File

@@ -16,6 +16,9 @@ namespace BlackInput
public:
CMacOSInputUtils() = delete;
//! Check OS permission for input monitoring access
static bool hasAccess();
//! Request OS permission for input monitoring access
static bool requestAccess();

View File

@@ -8,6 +8,19 @@
namespace BlackInput
{
bool CMacOSInputUtils::hasAccess()
{
if (@available(macOS 10.15, *))
{
return IOHIDCheckAccess(kIOHIDRequestTypeListenEvent) == IOHIDAccessType::kIOHIDAccessTypeGranted;
}
else
{
return true;
}
}
bool CMacOSInputUtils::requestAccess()
{
if (@available(macOS 10.15, *))

View File

@@ -29,6 +29,8 @@ target_link_libraries(swiftguistd
)
if(APPLE)
target_link_libraries(swiftguistd PUBLIC input)
set_target_properties(swiftguistd PROPERTIES MACOSX_BUNDLE TRUE)
set_target_properties(swiftguistd PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Info.plist)
set(RESOURCE_FILES swift.icns qt.conf)

View File

@@ -28,6 +28,10 @@
#include "blackmisc/threadutils.h"
#include "blackconfig/buildconfig.h"
#if defined(Q_OS_MACOS)
# include "blackinput/macos/macosinpututils.h"
#endif
#include "swiftguistd.h"
#include <QAction>
#include <QDateTime>
@@ -475,6 +479,16 @@ void SwiftGuiStd::verifyPrerequisites()
msgs.push_back(sGui->getIContextSimulator()->verifyPrerequisites());
}
#if defined(Q_OS_MACOS)
if (!BlackInput::CMacOSInputUtils::hasAccess())
{
// A log message about missing permissions is already emitted when initializing the keyboard.
// But this happens way before initializing the GUI. Hence do the check here again to show an error message
// to the user
msgs.push_back(CLogMessage(this).error(u"Cannot access the keyboard. Is \"Input Monitoring\" for swift enabled?"));
}
#endif
if (msgs.hasWarningOrErrorMessages())
{
if (msgs.size() > 1) { this->displayInOverlayWindow(msgs); }