yaw-audio/lib/rasa/wintab.cpp

81 lines
2.1 KiB
C++
Raw Normal View History

2022-09-22 21:55:25 +00:00
#ifdef TAB_WINTAB
#include "tablet.h"
#define PACKETDATA (PK_X | PK_Y | PK_Z | PK_BUTTONS | PK_NORMAL_PRESSURE)
//#define PACKETMODE
#include "pktdef.h"
#include "wtutil.h"
Tablet::Tablet(uintptr_t handle)
{
HWND hwnd = reinterpret_cast<HWND>(handle);
if (!hwnd) { return; }
if (!LoadWintab() || !gpWTInfoA(0, 0, NULL)) {
errormsg = "Wintab not installed.";
return;
}
NewContext(hwnd);
}
Tablet::~Tablet() {
if (hctx) { gpWTClose(hctx); }
UnloadWintab();
}
bool Tablet::GetPacket( Packet& packet ) {
//Serial number of newest packet.
UINT oldest, newest;
//This function returns false when it fails
//which may happen with a full or empty queue.
if (!gpWTQueuePacketsEx(hctx, &oldest, &newest)) {
//Queue may be full, flush it all just in case.
gpWTPacketsGet(hctx, gpWTQueueSizeGet(hctx), nullptr);
return false;
}
//Store newest packet in pkt, flush older packets.
PACKET pkt;
bool newData = gpWTPacket(hctx, newest, &pkt);
if (!newData) return false;
packet.x = static_cast<float>(pkt.pkX) / ext.x;
packet.y = static_cast<float>(pkt.pkY) / ext.y;
packet.z = static_cast<float>(pkt.pkZ) / ext.z;
packet.p = static_cast<float>(pkt.pkNormalPressure) / ext.p;
packet.buttons = pkt.pkButtons;
return true;
}
void Tablet::NewContext(HWND hwnd) {
if (hctx) { gpWTClose(hctx); }
LOGCONTEXT ctx = {};
AXIS TabletX = { 0 };
AXIS TabletY = { 0 };
AXIS TabletZ = { 0 };
AXIS TabletPressure = { 0 };
gpWTInfoA(WTI_DEFCONTEXT, 0, &ctx);
ctx.lcOptions |= CXO_MESSAGES; //TODO: checker <20>ela
ctx.lcPktData = PACKETDATA;
ctx.lcPktMode = 0;
//Tablet extents.
gpWTInfoA(WTI_DEVICES, DVC_X, &TabletX);
gpWTInfoA(WTI_DEVICES, DVC_Y, &TabletY);
gpWTInfoA(WTI_DEVICES, DVC_Z, &TabletZ);
gpWTInfoA(WTI_DEVICES, DVC_NPRESSURE, &TabletPressure);
ext.x = static_cast<float>(TabletX.axMax);
ext.y = static_cast<float>(TabletY.axMax);
ext.z = static_cast<float>(TabletZ.axMax);
ext.p = static_cast<float>(TabletPressure.axMax);
hctx = gpWTOpenA(hwnd, &ctx, TRUE);
if (!hctx)
{
errormsg = "Could not open Wintab context.";
return;
}
initialized = true;
};
#endif