Skip to content

Commit

Permalink
feat(ui): Show warning if Input Monitoring is not allowed
Browse files Browse the repository at this point in the history
Related to #125
  • Loading branch information
ltoenning committed Dec 14, 2023
1 parent 45229f1 commit 46e6eab
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/blackinput/macos/macosinpututils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
13 changes: 13 additions & 0 deletions src/blackinput/macos/macosinpututils.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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, *))
Expand Down
2 changes: 2 additions & 0 deletions src/swiftguistandard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/swiftguistandard/swiftguistd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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); }
Expand Down

0 comments on commit 46e6eab

Please sign in to comment.