Windows Touch and Pen disambiguation

So I happened to implement multi-touch for PPSSPP and to do that I consulted the Windows documentation which made me request touch input with RegisterTouchWindow and then handle the resulting WM_TOUCH events, the only issue is that Windows still sends the emulated WM_LBUTTONDOWN WM_MOUSEMOVE and WM_LBUTTONUP, so I needed to filter the events that came from touch to not handle them twice.

The docs recommendeded I filter with

#define MOUSEEVENTF_FROMTOUCH 0xFF515700
#define MOUSEEVENTF_MASK 0xFFFFFF00
//...
//...
case WM_LBUTTONDOWN:
if ((GetMessageExtraInfo() & MOUSEEVENTF_MASK) != MOUSEEVENTF_FROMTOUCH)
{
//handle mouse events here
}
break;
case WM_TOUCH:
//handle touch here

But turns out this silently makes pen inputs disappear in a black hole as they trigger MOUSEEVENTF_FROMTOUCH but don’t generate a WM_TOUCH event. So I added the 0x80 bit to the mask which is 1 when the source is touch and 0 when the source is a pen/stylus.

#define MOUSEEVENTF_FROMTOUCH_NOPEN 0xFF515780 
#define MOUSEEVENTF_MASK_PLUS_PENTOUCH 0xFFFFFF80
//...
//...
case WM_LBUTTONDOWN:
  if ((GetMessageExtraInfo() & MOUSEEVENTF_MASK_PLUS_PENTOUCH) != MOUSEEVENTF_FROMTOUCH_NOPEN)
  {
    //handle mouse events here
  }
  break;
case WM_TOUCH:
  //handle touch here