OS X raw mouse experiment


Currently, SDL_MOUSEMOTION events on OS X seem to arrive at maximum of 60Hz. I guess something in the OS is throttling them to that rate (energy saving reasons?). I thought it might be nice for FPS games to get raw mouse events, e.g. if they?re running at over 60 FPS with tearing, and I guess the OS?s throttling of mouse events to 60Hz must add some latency.

Here is an experimental patch that uses the IOHIDManager API to get raw motion events, which are used to create SDL_MOUSEMOTION events in relative mouse mode. These arrive at the full rate of the mouse (e.g. 1000Hz for my logitech g400) and have no acceleration!

Some notes on the code:
-In a first iteration, I tried running the IOHIDManager callback on the main thread. This had a problem; if you didn?t call SDL_PumpEvents often enough, some of the IOHIDManager callbacks would be lost. Presumably some internal event queue in CFRunLoop or IOHIDManager was getting filled up and dropping events?

-The ugly part of the patch is that it starts a background thread the first time you enable relative mouse mode. This just runs a CFRunLoop waiting for events from the mouse. The problem is it seems to take a fair bit of CPU time. Based on a quick look at the Xcode profiler running my project (QuakeSpasm), 15% of the cpu time was spent in the raw mouse thread, almost all of it in CFRunLoop/IOHIDManager internals, which seems pretty excessive. I guess it?s not surprising given that the events are coming at 1000Hz.

-Based on the above, I?m not sure if this is a good idea for general use. But I though I would just send it out anyway in case it?s useful to anyone.

I borrowed some of the IOHIDManager setup code from SDL_sysjoystick.c, and the IOHIDManager callback is based on the one from https://icculus.org/manymouse/ (zlib license).

-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL-osx-rawmouse.patch
Type: application/octet-stream
Size: 10281 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20141010/464006cd/attachment-0001.obj