// rts_contact_repro.cpp - minimal repro: RealTimeStylus touch WIDTH/HEIGHT vs. WM_POINTER rcContactRaw. // // rts_contact_repro.exe RTS mode: enables RTS on the window and prints // - the PROPERTY_METRICS of X/Y/WIDTH/HEIGHT and the scales S and C of every tablet context // - raw WIDTH/HEIGHT of each touch packet and two candidate conversions to pixels // - every WM_POINTER message the same window still receives // rts_contact_repro.exe pointer Pointer mode: RTS is not created; prints the rcContactRaw size of each // touch frame (used as the pixel reference) // // Run both modes one after the other and press the same finger at the same spot with similar force. // // Build from a Visual Studio Developer Command Prompt: // cl /std:c++20 /EHsc /W4 rts_contact_repro.cpp // Run it from that console and touch the window with one finger. #define UNICODE #define _UNICODE #include #include // tpcshrd.h uses _T() for MICROSOFT_TABLETPENSERVICE_PROPERTY #include #include #include #include #include #include #include #pragma comment(lib, "ole32.lib") #pragma comment(lib, "user32.lib") namespace { const GUID kProps[] = {GUID_PACKETPROPERTY_GUID_X, GUID_PACKETPROPERTY_GUID_Y, GUID_PACKETPROPERTY_GUID_WIDTH, GUID_PACKETPROPERTY_GUID_HEIGHT}; const char* const kNames[] = {"X", "Y", "WIDTH", "HEIGHT"}; enum { kX, kY, kW, kH, kCount }; struct Context { ULONG packetSize = 0; int index[kCount] = {-1, -1, -1, -1}; // position of each property in a packet, -1 if absent PROPERTY_METRICS metrics[kCount] = {}; float cx = 0, cy = 0; // C: ink-to-device scale of this context }; double CountsPerCm(const PROPERTY_METRICS& m) { if (m.Units == PROPERTY_UNITS_CENTIMETERS) return m.fResolution; if (m.Units == PROPERTY_UNITS_INCHES) return m.fResolution / 2.54; return 0; } class Plugin final : public IStylusSyncPlugin { LONG refs_ = 1; IUnknown* ftm_ = nullptr; std::mutex mutex_; std::map contexts_; float sx_ = 0, sy_ = 0; // S: scale of the first context; packet X * S = client pixels void Load(IRealTimeStylus* rts) { std::lock_guard lock(mutex_); contexts_.clear(); ULONG count = 0; TABLET_CONTEXT_ID* ids = nullptr; if (FAILED(rts->GetAllTabletContextIds(&count, &ids))) return; for (ULONG i = 0; i < count; ++i) { Context c; PACKET_PROPERTY* props = nullptr; if (FAILED(rts->GetPacketDescriptionData(ids[i], &c.cx, &c.cy, &c.packetSize, &props))) continue; if (i == 0) { sx_ = c.cx; sy_ = c.cy; } for (ULONG j = 0; j < c.packetSize; ++j) for (int k = 0; k < kCount; ++k) if (IsEqualGUID(props[j].guid, kProps[k])) { c.index[k] = static_cast(j); c.metrics[k] = props[j].PropertyMetrics; } CoTaskMemFree(props); printf("context tcid=%lu C=(%g, %g)\n", ids[i], c.cx, c.cy); for (int k = 0; k < kCount; ++k) if (c.index[k] >= 0) printf(" %-6s units=%d resolution=%g logical=[%ld, %ld]\n", kNames[k], static_cast(c.metrics[k].Units), c.metrics[k].fResolution, c.metrics[k].nLogicalMin, c.metrics[k].nLogicalMax); contexts_[ids[i]] = c; } CoTaskMemFree(ids); printf("S=(%g, %g)\n", sx_, sy_); } void Print(const char* phase, const StylusInfo* info, ULONG count, ULONG total, const LONG* data) { std::lock_guard lock(mutex_); const auto it = contexts_.find(info->tcid); if (it == contexts_.end() || count == 0 || total / count != it->second.packetSize) return; const Context& c = it->second; // Mouse and pen contexts usually have no WIDTH/HEIGHT; only touch packets are printed. if (c.index[kW] < 0 || c.index[kH] < 0 || c.cx == 0 || c.cy == 0) return; if (CountsPerCm(c.metrics[kW]) == 0 || CountsPerCm(c.metrics[kH]) == 0) return; // spanToAxis: WIDTH/HEIGHT counts converted to X/Y counts using the declared resolutions. const double spanToAxisW = CountsPerCm(c.metrics[kX]) / CountsPerCm(c.metrics[kW]); const double spanToAxisH = CountsPerCm(c.metrics[kY]) / CountsPerCm(c.metrics[kH]); for (ULONG p = 0; p < count; ++p) { const LONG* packet = data + static_cast(p) * c.packetSize; const double w = packet[c.index[kW]], h = packet[c.index[kH]]; printf("RTS %-6s cid=%lu raw=%.0f x %.0f A=%.1f x %.1f px B=%.1f x %.1f px\n", phase, info->cid, w, h, w * spanToAxisW * std::fabs(sx_), h * spanToAxisH * std::fabs(sy_), // A: raw * spanToAxis * S w * std::fabs(sx_ / c.cx), h * std::fabs(sy_ / c.cy)); // B: raw * S / C } } public: HRESULT Init() { return CoCreateFreeThreadedMarshaler(this, &ftm_); } ~Plugin() { if (ftm_) ftm_->Release(); } // IUnknown. A sync plugin must aggregate the free-threaded marshaler. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void** object) override { if (!object) return E_POINTER; *object = nullptr; if (iid == __uuidof(IMarshal) && ftm_) return ftm_->QueryInterface(iid, object); if (iid != __uuidof(IUnknown) && iid != __uuidof(IStylusPlugin) && iid != __uuidof(IStylusSyncPlugin)) return E_NOINTERFACE; *object = static_cast(this); AddRef(); return S_OK; } ULONG STDMETHODCALLTYPE AddRef() override { return InterlockedIncrement(&refs_); } ULONG STDMETHODCALLTYPE Release() override { const ULONG n = InterlockedDecrement(&refs_); if (n == 0) delete this; return n; } // IStylusPlugin HRESULT STDMETHODCALLTYPE DataInterest(RealTimeStylusDataInterest* interest) override { *interest = static_cast(RTSDI_RealTimeStylusEnabled | RTSDI_UpdateMapping | RTSDI_TabletAdded | RTSDI_TabletRemoved | RTSDI_StylusDown | RTSDI_StylusUp | RTSDI_Packets); return S_OK; } HRESULT STDMETHODCALLTYPE RealTimeStylusEnabled(IRealTimeStylus* rts, ULONG, const TABLET_CONTEXT_ID*) override { Load(rts); return S_OK; } HRESULT STDMETHODCALLTYPE UpdateMapping(IRealTimeStylus* rts) override { Load(rts); return S_OK; } HRESULT STDMETHODCALLTYPE TabletAdded(IRealTimeStylus* rts, IInkTablet*) override { Load(rts); return S_OK; } HRESULT STDMETHODCALLTYPE TabletRemoved(IRealTimeStylus* rts, LONG) override { Load(rts); return S_OK; } HRESULT STDMETHODCALLTYPE StylusDown(IRealTimeStylus*, const StylusInfo* info, ULONG size, LONG* packet, LONG**) override { Print("down", info, 1, size, packet); return S_OK; } HRESULT STDMETHODCALLTYPE StylusUp(IRealTimeStylus*, const StylusInfo* info, ULONG size, LONG* packet, LONG**) override { Print("up", info, 1, size, packet); return S_OK; } HRESULT STDMETHODCALLTYPE Packets(IRealTimeStylus*, const StylusInfo* info, ULONG count, ULONG total, LONG* packets, ULONG*, LONG**) override { Print("move", info, count, total, packets); return S_OK; } HRESULT STDMETHODCALLTYPE RealTimeStylusDisabled(IRealTimeStylus*, ULONG, const TABLET_CONTEXT_ID*) override { return S_OK; } HRESULT STDMETHODCALLTYPE StylusInRange(IRealTimeStylus*, TABLET_CONTEXT_ID, STYLUS_ID) override { return S_OK; } HRESULT STDMETHODCALLTYPE StylusOutOfRange(IRealTimeStylus*, TABLET_CONTEXT_ID, STYLUS_ID) override { return S_OK; } HRESULT STDMETHODCALLTYPE StylusButtonDown(IRealTimeStylus*, STYLUS_ID, const GUID*, POINT*) override { return S_OK; } HRESULT STDMETHODCALLTYPE StylusButtonUp(IRealTimeStylus*, STYLUS_ID, const GUID*, POINT*) override { return S_OK; } HRESULT STDMETHODCALLTYPE InAirPackets(IRealTimeStylus*, const StylusInfo*, ULONG, ULONG, LONG*, ULONG*, LONG**) override { return S_OK; } HRESULT STDMETHODCALLTYPE CustomStylusDataAdded(IRealTimeStylus*, const GUID*, ULONG, const BYTE*) override { return S_OK; } HRESULT STDMETHODCALLTYPE SystemEvent(IRealTimeStylus*, TABLET_CONTEXT_ID, STYLUS_ID, SYSTEM_EVENT, SYSTEM_EVENT_DATA) override { return S_OK; } HRESULT STDMETHODCALLTYPE Error(IRealTimeStylus*, IStylusPlugin*, RealTimeStylusDataInterest, HRESULT, LONG_PTR*) override { return S_OK; } }; // Prints every pointer message the window receives. In RTS mode this shows which WM_POINTER messages // still reach a window that has RTS enabled. The messages are only read and then passed to DefWindowProc. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { const char* name = msg == WM_POINTERDOWN ? "down" : msg == WM_POINTERUPDATE ? "update" : msg == WM_POINTERUP ? "up" : nullptr; if (name) { const UINT32 id = GET_POINTERID_WPARAM(wp); POINTER_INPUT_TYPE type{}; POINTER_TOUCH_INFO touch{}; UINT32 cursor = 0; GetPointerType(id, &type); GetPointerCursorId(id, &cursor); if (type == PT_TOUCH && GetPointerTouchInfo(id, &touch) && (touch.touchMask & TOUCH_MASK_CONTACTAREA)) printf("POINTER %-6s id=%u cursor=%u rcContactRaw=%ld x %ld px\n", name, id, cursor, touch.rcContactRaw.right - touch.rcContactRaw.left, touch.rcContactRaw.bottom - touch.rcContactRaw.top); else printf("POINTER %-6s id=%u cursor=%u type=%d (no touch contact area)\n", name, id, cursor, static_cast(type)); } else if (msg == WM_POINTERCAPTURECHANGED) { printf("POINTER capture_changed id=%u\n", GET_POINTERID_WPARAM(wp)); } else if (msg == WM_DESTROY) { PostQuitMessage(0); return 0; } return DefWindowProcW(hwnd, msg, wp, lp); } bool Check(const char* step, HRESULT hr) { if (FAILED(hr)) printf("%s failed: 0x%08lX\n", step, static_cast(hr)); return SUCCEEDED(hr); } } int main(int argc, char** argv) { const bool pointerMode = argc > 1 && strcmp(argv[1], "pointer") == 0; // Per-monitor DPI awareness, so that S, rcContactRaw and the window all use physical pixels. SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); if (!Check("CoInitializeEx", CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) return 1; WNDCLASSW wc{}; wc.lpfnWndProc = WndProc; wc.hInstance = GetModuleHandleW(nullptr); wc.hCursor = LoadCursorW(nullptr, IDC_ARROW); wc.hbrBackground = reinterpret_cast(COLOR_WINDOW + 1); wc.lpszClassName = L"RtsContactRepro"; RegisterClassW(&wc); HWND hwnd = CreateWindowW(wc.lpszClassName, L"RTS contact repro", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1000, 700, nullptr, nullptr, wc.hInstance, nullptr); if (!hwnd) return 1; printf("mode=%s window DPI=%u\n", pointerMode ? "pointer" : "rts", GetDpiForWindow(hwnd)); IRealTimeStylus* rts = nullptr; IRealTimeStylus3* rts3 = nullptr; Plugin* plugin = new Plugin(); bool ok = true; if (!pointerMode) { // Ask the tablet service for multi-touch data and turn off press-and-hold / flicks on this window. SetPropW(hwnd, MICROSOFT_TABLETPENSERVICE_PROPERTY, reinterpret_cast(static_cast(TABLET_ENABLE_MULTITOUCHDATA | TABLET_DISABLE_PRESSANDHOLD | TABLET_DISABLE_FLICKS))); ok = Check("CoCreateInstance", CoCreateInstance(__uuidof(RealTimeStylus), nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&rts))) && Check("put_HWND", rts->put_HWND(reinterpret_cast(hwnd))) && Check("SetAllTabletsMode", rts->SetAllTabletsMode(TRUE)) && Check("SetDesiredPacketDescription", rts->SetDesiredPacketDescription(kCount, kProps)) && Check("QueryInterface(IRealTimeStylus3)", rts->QueryInterface(IID_PPV_ARGS(&rts3))) && Check("put_MultiTouchEnabled", rts3->put_MultiTouchEnabled(TRUE)) && Check("CoCreateFreeThreadedMarshaler", plugin->Init()) && Check("AddStylusSyncPlugin", rts->AddStylusSyncPlugin(0, plugin)) && Check("put_Enabled", rts->put_Enabled(TRUE)); } if (ok) { MSG msg{}; while (GetMessageW(&msg, nullptr, 0, 0) > 0) { TranslateMessage(&msg); DispatchMessageW(&msg); } } if (rts) { rts->put_Enabled(FALSE); rts->RemoveAllStylusSyncPlugins(); rts->Release(); } if (rts3) rts3->Release(); plugin->Release(); CoUninitialize(); return ok ? 0 : 1; }