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
14 changes: 12 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 @@ -138,9 +144,9 @@ 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();
Expand All @@ -163,14 +169,18 @@ main(int argc, char *argv[])
moved.write_reports();
}

moved_monitor_free(monitor);
#else
while (true) {
moved.handle_request();

Copy link
Copy Markdown
Contributor

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?

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
19 changes: 19 additions & 0 deletions src/daemon/moved_monitor_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved_monitor_get_fd() returns -1, so poll() ignores it. Blocking waits forever and nonblocking always returns false, preventing the CFRunLoop from processing controller events. Could this wait through the CFRunLoop instead?

}

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

@adangert adangert Jul 31, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This returns true even when a nonblocking wait returns WAIT_TIMEOUT. Could this return true only for WAIT_OBJECT_0 or a fallback rescan, and false for a nonblocking timeout?

}


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