-
-
Notifications
You must be signed in to change notification settings - Fork 174
Unified moved_monitor_wait() for moved #520
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,10 @@ | |
| #import <IOKit/hid/IOHIDManager.h> | ||
| #import <CoreFoundation/CoreFoundation.h> | ||
|
|
||
| #include <sys/syslimits.h> | ||
| #include <sys/stat.h> | ||
| #include <sys/poll.h> | ||
|
|
||
| // Convenience functions copied from hidapi | ||
| #include "moved_monitor_osx_hidapi.mm" | ||
|
|
||
|
|
@@ -160,6 +164,21 @@ static void on_device_removal(void *context, IOReturn result, void *sender, IOHI | |
| monitor->pump_loop(); | ||
| } | ||
|
|
||
| bool | ||
| moved_monitor_wait(moved_monitor *monitor, bool blocking) | ||
| { | ||
| psmove_return_val_if_fail(monitor != nullptr, false); | ||
|
|
||
| int monitor_fd = moved_monitor_get_fd(monitor); | ||
|
|
||
| struct pollfd pfd; | ||
|
|
||
| pfd.fd = monitor_fd; | ||
| pfd.events = POLLIN; | ||
|
|
||
| return (poll(&pfd, 1, blocking ? -1 : 0) > 0); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| void | ||
| moved_monitor_free(moved_monitor *monitor) | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -303,14 +303,17 @@ moved_monitor_get_fd(moved_monitor *) | |
| return -1; | ||
| } | ||
|
|
||
| void | ||
| moved_monitor_wait(moved_monitor *monitor) | ||
| bool | ||
| moved_monitor_wait(moved_monitor *monitor, bool blocking) | ||
| { | ||
| psmove_return_if_fail(monitor != nullptr); | ||
| psmove_return_val_if_fail(monitor != nullptr, false); | ||
|
|
||
| // TODO: This function only returns "true" for now, causing a | ||
| // rescan all the time. Eventually implement returning "false". | ||
|
|
||
| const auto now = GetTickCount64(); | ||
| if (monitor->rescan_requested.load() || now >= monitor->next_rescan) { | ||
| return; | ||
| return true; | ||
| } | ||
|
|
||
| const auto remaining = monitor->next_rescan - now; | ||
|
|
@@ -319,18 +322,23 @@ moved_monitor_wait(moved_monitor *monitor) | |
| : static_cast<DWORD>(remaining); | ||
|
|
||
| if (monitor->device_event == nullptr) { | ||
| Sleep(timeout); | ||
| return; | ||
| if (blocking) { | ||
| // Simulated blocking based on timeout | ||
| Sleep(timeout); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // The notification callback signals the event immediately. The timeout | ||
| // preserves periodic rescanning if Windows misses a notification. | ||
| const auto result = WaitForSingleObject(monitor->device_event, timeout); | ||
| const auto result = WaitForSingleObject(monitor->device_event, blocking ? timeout : 0); | ||
| if (result == WAIT_FAILED) { | ||
| PSMOVE_WARNING( | ||
| "Could not wait for Windows device notification (%lu)", | ||
| static_cast<unsigned long>(GetLastError())); | ||
| } | ||
|
|
||
| return true; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This returns true even when a nonblocking wait returns |
||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handle_request()blocks in recvfrom(), so a controller notification alone cannot reach moved_monitor_wait(). Potentially the socket and monitor event could be multiplexed, or the socket made nonblocking, so monitor wakes the daemon without a UDP request?