Skip to content
Merged
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
22 changes: 20 additions & 2 deletions src/daemon/moved.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@

#include "psmove.h"

#if defined(__APPLE__)
# include <sys/poll.h>
#elif defined(__linux)
# include <poll.h>
#endif

#include <vector>

struct move_daemon;
Expand Down Expand Up @@ -144,14 +150,16 @@ main(int argc, char *argv[])
moved.handle_connection(NULL, NULL);
}

#if defined(__linux) || defined(__APPLE__)
moved_monitor *monitor = moved_monitor_new(on_monitor_update_moved, &moved);

#if defined(__linux) || defined(__APPLE__)
struct pollfd pfd[2];

pfd[0].fd = moved.get_socket();
pfd[0].events = POLLIN;

// FIXME: On macOS, moved_monitor_get_fd() always returns -1,
// so the file descriptor cannot be polled.
pfd[1].fd = moved_monitor_get_fd(monitor);
pfd[1].events = POLLIN;

Expand All @@ -169,14 +177,24 @@ main(int argc, char *argv[])
moved.write_reports();
}

moved_monitor_free(monitor);
#else
while (true) {
// In this fallback case (currently used on Windows), the
// moved.handle_request() function blocks in recvfrom(),
// so any monitor events might only be visible to clients
// once they send UDP requests. In the future, using
// select() from WinSock2 might be an option (or using
// threads and blocking I/O).
moved.handle_request();
Comment thread
thp marked this conversation as resolved.
if (moved_monitor_wait(monitor, false)) {
moved_monitor_poll(monitor);
}
moved.write_reports();
}
#endif

moved_monitor_free(monitor);

return 0;
}

Expand Down
20 changes: 13 additions & 7 deletions src/daemon/moved_monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern "C" {
#endif

#include <wchar.h>
#include <stdbool.h>

enum MonitorEvent {
EVENT_DEVICE_ADDED,
Expand All @@ -59,13 +60,18 @@ typedef struct _moved_monitor moved_monitor;
ADDAPI moved_monitor *
ADDCALL moved_monitor_new(moved_event_callback callback, void *user_data);

#ifdef _WIN32
// Block until Windows signals a device change or the fallback rescan is due.
// Call moved_monitor_poll() after this returns to process any controller
// additions or removals.
ADDAPI void
ADDCALL moved_monitor_wait(moved_monitor *monitor);
#endif
// If blocking is true:
// Block until the OS signals a device change (or in some cases, a fallback
// mechanism is used, e.g. interval-based polling), and return true.
//
// If blocking is false:
// Will immediately return true if there are outstanding device changes,
// or immediately return false otherwise.
//
// Call moved_monitor_poll() after this returns true to process any
// controller additions or removals.
ADDAPI bool
ADDCALL moved_monitor_wait(moved_monitor *monitor, bool blocking);

ADDAPI void
ADDCALL moved_monitor_poll(moved_monitor *monitor);
Expand Down
17 changes: 17 additions & 0 deletions src/daemon/moved_monitor_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@

#include <libudev.h>
#include <linux/input.h>
#include <linux/limits.h>
#include <poll.h>

#include "moved_monitor.h"

Expand Down Expand Up @@ -215,6 +217,21 @@ moved_monitor_poll(moved_monitor *monitor)
}
}

bool
moved_monitor_wait(moved_monitor *monitor, bool blocking)
{
psmove_return_val_if_fail(monitor != NULL, 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);
}

void
moved_monitor_free(moved_monitor *monitor)
{
Expand Down
35 changes: 35 additions & 0 deletions src/daemon/moved_monitor_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#import <IOKit/hid/IOHIDManager.h>
#import <CoreFoundation/CoreFoundation.h>

#include <sys/syslimits.h>
#include <sys/stat.h>

// Convenience functions copied from hidapi
#include "moved_monitor_osx_hidapi.mm"

Expand Down Expand Up @@ -91,6 +94,10 @@ void pump_loop()
}
}

bool has_events() const {
return !events.empty();
}

private:
void
make_event(enum MonitorEvent event, IOHIDDeviceRef device)
Expand Down Expand Up @@ -160,6 +167,34 @@ 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);

/* Blocking wait for events */
while (blocking && !monitor->has_events()) {
switch (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 60.0, TRUE)) {
case kCFRunLoopRunFinished:
case kCFRunLoopRunStopped:
return false;
case kCFRunLoopRunTimedOut:
/* Timeout hit, try again */
break;
case kCFRunLoopRunHandledSource:
/* One source was handled, potentially events */
break;
}
}

if (!monitor->has_events()) {
/* Poll once, ignore result, as we peek at has_events() below */
(void)CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, TRUE);
}

return monitor->has_events();
}

void
moved_monitor_free(moved_monitor *monitor)
{
Expand Down
19 changes: 12 additions & 7 deletions src/daemon/moved_monitor_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ 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);

const auto now = GetTickCount64();
if (monitor->rescan_requested.load() || now >= monitor->next_rescan) {
return;
return true;
}

const auto remaining = monitor->next_rescan - now;
Expand All @@ -319,18 +319,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 (result == WAIT_OBJECT_0);
}


Expand Down
2 changes: 0 additions & 2 deletions src/psmove_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@
# include <unistd.h>
# include <sys/syslimits.h>
# include <sys/stat.h>
# include <sys/poll.h>
#endif

#ifdef __linux
# include <unistd.h>
# include <linux/limits.h>
# include <poll.h>
#endif

#ifdef _WIN32
Expand Down
12 changes: 1 addition & 11 deletions src/psmoveapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,9 @@ PSMoveAPI::~PSMoveAPI()
void
PSMoveAPI::update()
{
if (moved_monitor_get_fd(monitor) == -1) {
if (moved_monitor_wait(monitor, false)) {
moved_monitor_poll(monitor);
}
#ifndef _WIN32
else {
struct pollfd pfd;
pfd.fd = moved_monitor_get_fd(monitor);
pfd.events = POLLIN;
while (poll(&pfd, 1, 0) > 0) {
moved_monitor_poll(monitor);
}
}
#endif

for (auto &c: controllers) {
c->update_connection_flags();
Expand Down
26 changes: 2 additions & 24 deletions src/utils/psmovepair.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,37 +106,15 @@ on_monitor_update_pair(enum MonitorEvent event,

int run_daemon()
{
#if defined(_WIN32)
moved_monitor *monitor = moved_monitor_new(on_monitor_update_pair, NULL);

while (1) {
moved_monitor_wait(monitor);
moved_monitor_poll(monitor);
}
moved_monitor_free(monitor);
#elif defined(__linux) || defined(__APPLE__)
// TODO: Use a blocking monitor wait here after runtime testing it on
// Linux and macOS.
moved_monitor *monitor = moved_monitor_new(on_monitor_update_pair, NULL);
int monitor_fd = moved_monitor_get_fd(monitor);
struct pollfd pfd;

pfd.fd = monitor_fd;
pfd.events = POLLIN;

while (1) {
if (poll(&pfd, 1, 0) > 0) {
while (true) {
if (moved_monitor_wait(monitor, true)) {
moved_monitor_poll(monitor);
}
}

moved_monitor_free(monitor);
#else
for(;;) {
psmove_port_sleep_ms(5000);
pair(NULL);
}
#endif

return 0;
}
Expand Down
Loading