Skip to content
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
29 changes: 29 additions & 0 deletions src/platform/macos/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ const KeyCodeMap kKeyCodesMap[] = {
}
}

void reveal_cursor(input_t &input);

void keyboard_update(input_t &input, uint16_t modcode, bool release, uint8_t flags) {
auto key = keysym(modcode);

Expand Down Expand Up @@ -370,6 +372,7 @@ const KeyCodeMap kKeyCodesMap[] = {
CGEventSetFlags(event, macos_input->kb_flags);
CGEventPost(kCGSessionEventTap, event);
CFRelease(event);
reveal_cursor(input);
}

void unicode(input_t &input, char *utf8, int size) {
Expand Down Expand Up @@ -402,6 +405,32 @@ const KeyCodeMap kKeyCodesMap[] = {
};
}

/**
* @brief Nudge macOS into making the cursor visible after keyboard input.
*
* @param input Platform input context.
*/
void reveal_cursor(input_t &input) {
auto macos_input = static_cast<macos_input_t *>(input.get());
auto current = get_mouse_loc(input);
auto location = CGPoint {current.x, current.y};

CGAssociateMouseAndMouseCursorPosition(true);
CGDisplayShowCursor(macos_input->display);

auto event = CGEventCreateMouseEvent(macos_input->source, kCGEventMouseMoved, location, kCGMouseButtonLeft);
if (!event) {
return;
}

CGEventSetDoubleValueField(event, kCGMouseEventDeltaX, 0);
CGEventSetDoubleValueField(event, kCGMouseEventDeltaY, 0);
CGEventSetFlags(event, macos_input->kb_flags);
CGEventPost(kCGHIDEventTap, event);
CFRelease(event);
CGWarpMouseCursorPosition(location);
}

/**
* @brief Post a mouse event at the clamped display location.
*
Expand Down
21 changes: 20 additions & 1 deletion src/platform/macos/misc.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// platform includes
#include <arpa/inet.h>
#include <dlfcn.h>
#include <ApplicationServices/ApplicationServices.h>
#include <Foundation/Foundation.h>
#include <mach-o/dyld.h>
#include <net/if_dl.h>
Expand Down Expand Up @@ -258,7 +259,25 @@ void set_thread_name(const std::string &name) {
}

void enable_mouse_keys() {
// Unimplemented
if (CGAssociateMouseAndMouseCursorPosition(true) != kCGErrorSuccess) {
BOOST_LOG(debug) << "Unable to associate mouse movement with cursor position"sv;
}

uint32_t display_count = 0;
if (CGGetOnlineDisplayList(0, nullptr, &display_count) != kCGErrorSuccess || display_count == 0) {
CGDisplayShowCursor(kCGNullDirectDisplay);
return;
}

std::vector<CGDirectDisplayID> displays(display_count);
if (CGGetOnlineDisplayList(display_count, displays.data(), &display_count) != kCGErrorSuccess) {
CGDisplayShowCursor(kCGNullDirectDisplay);
return;
}

for (uint32_t i = 0; i < display_count; ++i) {
CGDisplayShowCursor(displays[i]);
}
}

void streaming_will_start() {
Expand Down