20130325 - Understanding WIN32 GetRawInputBuffer()


This is a re-post, for some reason bringing back the prior link is no longer working, have not figured out why yet. Direct link to docs: GetRawInputBuffer().

On XP at least (have not tested Vista/Win7/Win8 yet),
The entire RawInput system is built on-top of WM_INPUT events in the standard Window's message queue. When processing messages, WM_INPUT messages must not be removed in order to use GetRawInputBuffer() as GetRawInputBuffer() will use then remove those events. In my usage case I'm doing this to drain out the message queue without disrupting GetRawInputBuffer(), and relying on the fact that for events like WM_ACTIVATEAPP (ALT+TAB), PeekMessage() automatically calls the WinProc() without returning the message to the application directly,

while(1) {
if(PeekMessage(&msg, 0, 0, WM_INPUT-1, PM_NOYIELD | PM_REMOVE) == 0) break;
DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam); }
while(1) {
if(PeekMessage(&msg, 0, WM_INPUT+1, 0xFFFFFFFF, PM_NOYIELD | PM_REMOVE) == 0) break;
DefWindowProc(msg.hwnd, msg.message, msg.wParam, msg.lParam); }

GetRawInputBuffer()
Docs on GetRawInputBuffer() are confusing, here is a less confusing example,

// Some temp global buffer, 1KB is overkill.
static uint64_t rawBuffer[1024/8];
...
// Then in some function,
const uint32_t bytes = sizeof(rawBuffer);
// Loop through reading raw input until no events are left,
while(1) {
// Fill up buffer,
int32_t count = GetRawInputBuffer((PRAWINPUT)rawBuffer, &bytes, sizeof(RAWINPUTHEADER));
if(count <= 0) return;
...
// Process all the events,
const RAWINPUT* restrict raw = (const RAWINPUT* restrict) rawBuffer;
while(1) {
// Process raw event.
...
// Goto next raw event.
count--;
if(count <= 0) break;
raw = NEXTRAWINPUTBLOCK(raw); } }