Create a gist now

Instantly share code, notes, and snippets.

キーボードマニア専用コントローラの入力を取得するHSPモジュール
/*
キーボードマニア専用コントローラ入力取得モジュール
24鍵とスタート・セレクトボタン、ホイール上下の入力、複数のコントローラ対応
license: NYSL 0.9982 (http://www.kmonos.net/nysl/)
tested: HSP 3.5b4
*/
#module kminput
#define MAX_DEVS 4
#define INPUT_BYTE 9
#uselib "kernel32"
#func CloseHandle "CloseHandle" int
#func CreateFile "CreateFileA" str, int, int, int, int, int, nullptr
#func ReadFile "ReadFile" int, var, int, var, int
#func GetLastError "GetLastError"
#func GetOverlappedResult "GetOverlappedResult" int, var, var, int
#func CancelIo "CancelIo" int
#uselib "hid"
#func HidD_GetHidGuid "HidD_GetHidGuid" var
#func HidD_GetAttributes "HidD_GetAttributes" int, var
#uselib "setupapi"
#func SetupDiGetClassDevs "SetupDiGetClassDevsA" var, int, int, int
#func SetupDiEnumDeviceInterfaces "SetupDiEnumDeviceInterfaces" int, int, var, int, var
#func SetupDiDestroyDeviceInfoList "SetupDiDestroyDeviceInfoList" int
#func SetupDiGetDeviceInterfaceDetail "SetupDiGetDeviceInterfaceDetailA" int, var, int, int, var, int
#deffunc km_init
; 鍵盤(左から)・SELECT・START・ホイール上下の入力データのビット位置
bitTable(0) = 16, 17, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28
bitTable(12) = 29, 32, 33, 34, 35, 36, 37, 40, 41, 42, 43, 44
bitTable(24) = 30, 38, 45, 46
dim hHid, MAX_DEVS
devNum = 0
sdim hidGuid, 16
HidD_GetHidGuid hidGuid
SetupDiGetClassDevs hidGuid, 0, 0, $12 ; DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
hDevInfo = stat
dim devData, 7 ; SP_DEVICE_INTERFACE_DATA
devData = 28 ; cbSize
dim devAttri, 4 ; HIDD_ATTRIBUTES
devAttri = 10 ; cbSize
repeat
SetupDiEnumDeviceInterfaces hDevInfo, 0, hidGuid, cnt, devData
if (stat == 0) : break
reqSize = 0
SetupDiGetDeviceInterfaceDetail hDevInfo, devData, 0, 0, reqSize, 0
size = reqSize
sdim devDetail, reqSize + 4 ; SP_DEVICE_INTERFACE_DETAIL_DATA
lpoke devDetail, 0, 5 ; cbSize
SetupDiGetDeviceInterfaceDetail hDevInfo, devData, varptr(devDetail), size, reqSize, 0
sdim path, 260
memcpy path, devDetail, size, 0, 4
CreateFile path, $c0000000, 3, 0, 3, $40000000 ; read/write, OPEN_EXISTING, FILE_FLAG_OVERLAPPED
if (stat != -1) {
h = stat
HidD_GetAttributes h, devAttri
vid = devAttri(1) & $ffff
pid = devAttri(1) >> 16 & $ffff
; ver = devAttri(2) & $ffff
if (vid == $507 && pid == $10) {
hHid(devNum) = h
devNum++
if (devNum >= MAX_DEVS) : break
} else {
CloseHandle h
}
}
loop
SetupDiDestroyDeviceInfoList hDevInfo
dim lpbyte
dim overLap, 5, devNum
sdim bufStat, INPUT_BYTE, devNum
dim pending, devNum
return devNum
#deffunc km_update int _f
res = 0
repeat devNum : id = cnt
if (pending(id)) {
GetOverlappedResult hHid(id), overLap(0, id), lpbyte, 0
if (stat) {
pending(id) = 0
CancelIo hHid(id)
}
}
if (pending(id) == 0) {
repeat 1 - (_f == 0) * 2
ReadFile hHid(id), bufStat(id), INPUT_BYTE, lpbyte, varptr(overLap(0, id))
if (stat) {
if (_f) {
res |= 1 << id
break
} else {
continue
}
} else {
GetLastError
switch stat
case 997 ; ERROR_IO_PENDING
pending(id) = 1
case 1167 ; ERROR_DEVICE_NOT_CONNECTED
break
default
dialog "km_update: ReadFile error: " + stat
end
swend
}
loop
}
loop
return res
#defcfunc km_get int _i, int _id
return (peek(bufStat(_id), bitTable(_i) / 8) & (1 << (bitTable(_i) & 7))) > 0
#deffunc km_free onexit
repeat devNum
if (pending(cnt)) {
pending(cnt) = 0
CancelIo hHid(cnt)
}
CloseHandle hHid(cnt)
loop
devNum = 0
return
#global
#if 0
km_init
if (stat == 0) {
dialog "キーボードマニア専用コントローラの接続が確認できません"
end
}
devnum = stat
*mainLoop
km_update
redraw 0
color : boxf
color 255, 255, 255
pos 0, 0
mes f
f++
repeat devnum : i = cnt
repeat 28
pos cnt * 20 + 10, 20 * i + 30
mes km_get(cnt, i)
loop
loop
redraw
await 15
goto *mainLoop
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment