Skip to content

Commit

Permalink
let's try some CFRun magic
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazer committed May 23, 2024
1 parent f6a7880 commit e45b648
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
38 changes: 38 additions & 0 deletions src/platform/macos/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
#include "src/platform/common.h"
#include "src/utility.h"

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <CoreFoundation/CoreFoundation.h>
#include <ApplicationServices/ApplicationServices.h>

/**
* @brief Delay for a double click, in milliseconds.
* @todo Make this configurable.
Expand Down Expand Up @@ -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);

Check warning on line 547 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L545-L547

Added lines #L545 - L547 were not covered by tests

CFRunLoopRef runLoop = CFRunLoopGetCurrent();
CFRunLoopAddSource(runLoop, runLoopSource, kCFRunLoopDefaultMode);

Check warning on line 550 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L549-L550

Added lines #L549 - L550 were not covered by tests

{
std::lock_guard<std::mutex> lock(mtx);
ready = true;
cv.notify_one();

Check warning on line 555 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L553-L555

Added lines #L553 - L555 were not covered by tests
}

// Run the loop
CFRunLoopRun();

Check warning on line 559 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L559

Added line #L559 was not covered by tests

// Cleanup
CFRelease(runLoopSource);

Check warning on line 562 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L562

Added line #L562 was not covered by tests
}

input_t
input() {
input_t result { new macos_input_t() };

std::condition_variable cv;
bool ready = false;

Check warning on line 570 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L569-L570

Added lines #L569 - L570 were not covered by tests

std::thread([&cv, &ready]() {
runLoopThread(cv, ready);

Check warning on line 573 in src/platform/macos/input.cpp

View check run for this annotation

Codecov / codecov/patch

src/platform/macos/input.cpp#L573

Added line #L573 was not covered by tests
}).detach();

std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [&ready] { return ready; });

const auto macos_input = static_cast<macos_input_t *>(result.get());

// Default to main display
Expand Down
17 changes: 8 additions & 9 deletions tests/unit/test_mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit e45b648

Please sign in to comment.