From e45b6480927c75df21518587139a701ca495a344 Mon Sep 17 00:00:00 2001 From: Vithorio Polten Date: Wed, 22 May 2024 22:23:49 -0300 Subject: [PATCH] let's try some CFRun magic --- src/platform/macos/input.cpp | 38 ++++++++++++++++++++++++++++++++++++ tests/unit/test_mouse.cpp | 17 ++++++++-------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/platform/macos/input.cpp b/src/platform/macos/input.cpp index df06fef98bf..8d648864516 100644 --- a/src/platform/macos/input.cpp +++ b/src/platform/macos/input.cpp @@ -12,6 +12,13 @@ #include "src/platform/common.h" #include "src/utility.h" +#include +#include +#include +#include +#include +#include + /** * @brief Delay for a double click, in milliseconds. * @todo Make this configurable. @@ -534,10 +541,41 @@ const KeyCodeMap kKeyCodesMap[] = { // Unimplemented } + std::mutex mtx; + void runLoopThread(std::condition_variable& cv, bool& ready) { + CFRunLoopSourceContext context = {0}; + CFRunLoopSourceRef runLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); + + CFRunLoopRef runLoop = CFRunLoopGetCurrent(); + CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode); + + { + std::lock_guard lock(mtx); + ready = true; + cv.notify_one(); + } + + // Run the loop + CFRunLoopRun(); + + // Cleanup + CFRelease(runLoopSource); + } + input_t input() { input_t result { new macos_input_t() }; + std::condition_variable cv; + bool ready = false; + + std::thread([&cv, &ready]() { + runLoopThread(cv, ready); + }).detach(); + + std::unique_lock lock(mtx); + cv.wait(lock, [&ready] { return ready; }); + const auto macos_input = static_cast(result.get()); // Default to main display diff --git a/tests/unit/test_mouse.cpp b/tests/unit/test_mouse.cpp index 7f68bbccb87..8345145028a 100644 --- a/tests/unit/test_mouse.cpp +++ b/tests/unit/test_mouse.cpp @@ -44,20 +44,19 @@ TEST_P(MouseTest, MoveInputTest) { std::cout << "MoveInputTest:: init input" << std::endl; std::cout << "MoveInputTest:: get current mouse loc" << std::endl; - //auto old_loc = platf::get_mouse_loc(input); - //std::cout << "MoveInputTest:: got current mouse loc: " << old_loc << std::endl; + auto old_loc = platf::get_mouse_loc(input); + std::cout << "MoveInputTest:: got current mouse loc: " << old_loc << std::endl; std::cout << "MoveInputTest:: move: " << mouse_delta << std::endl; - //platf::move_mouse(input, mouse_delta.x, mouse_delta.y); + platf::move_mouse(input, mouse_delta.x, mouse_delta.y); std::this_thread::sleep_for(std::chrono::milliseconds(200)); std::cout << "MoveInputTest:: moved: " << mouse_delta << std::endl; std::cout << "MoveInputTest:: get updated mouse loc" << std::endl; - //auto new_loc = platf::get_mouse_loc(input); - //std::cout << "MoveInputTest:: got updated mouse loc: " << new_loc << std::endl; + auto new_loc = platf::get_mouse_loc(input); + std::cout << "MoveInputTest:: got updated mouse loc: " << new_loc << std::endl; - //bool has_input_moved = old_loc.x != new_loc.x && old_loc.y != new_loc.y; - bool has_input_moved = false; + bool has_input_moved = old_loc.x != new_loc.x && old_loc.y != new_loc.y; if (!has_input_moved) { std::cout << "MoveInputTest:: haven't moved" << std::endl; @@ -68,8 +67,8 @@ TEST_P(MouseTest, MoveInputTest) { EXPECT_TRUE(has_input_moved); // Verify we moved as much as we requested - // EXPECT_EQ(new_loc.x - old_loc.x, mouse_delta.x); - // EXPECT_EQ(new_loc.y - old_loc.y, mouse_delta.y); + EXPECT_EQ(new_loc.x - old_loc.x, mouse_delta.x); + EXPECT_EQ(new_loc.y - old_loc.y, mouse_delta.y); } // TEST_P(MouseTest, AbsMoveInputTest) {