Skip to content

Commit

Permalink
Bind to "msvcrt>memcpy" for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulMcClernan committed Mar 5, 2021
1 parent 56a1662 commit 1dfdf30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Binary file modified HIDAPI Tester.livecode
Binary file not shown.
34 changes: 28 additions & 6 deletions HIDAPI.lcb
Expand Up @@ -56,7 +56,9 @@ private type hid_device is Pointer
public foreign type HIDDevices binds to "MCAggregateTypeInfo:rddrdrrddHr"
public foreign type HIDByteBuffer binds to "MCAggregateTypeInfo:hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
__safe foreign handler PointerToHIDDevices(out rAggregate as HIDDevices, in pPointer as Pointer, in pSize as UIntSize) returns nothing binds to "c:memcpy"
__safe foreign handler win_PointerToHIDDevices(out rAggregate as HIDDevices, in pPointer as Pointer, in pSize as UIntSize) returns nothing binds to "msvcrt>memcpy"
__safe foreign handler PointerBufferToData(out rData as HIDByteBuffer, in pPointer as Pointer, in pSize as UIntSize) returns nothing binds to "c:memcpy"
__safe foreign handler win_PointerBufferToData(out rData as HIDByteBuffer, in pPointer as Pointer, in pSize as UIntSize) returns nothing binds to "msvcrt>memcpy"

private variable mDevices as optional hid_device_infos
private variable mHIDDevicesArray as Array
Expand Down Expand Up @@ -174,7 +176,11 @@ public handler HIDAPIRead() returns optional any
if rBytesRead is -1 then
return "error reading from device" -- && tReturnString
else
PointerBufferToData(rIncomingData,rBufferPtr,64)
if the operating system is "windows" then
win_PointerBufferToData(rIncomingData,rBufferPtr,64)
else
PointerBufferToData(rIncomingData,rBufferPtr,64)
end if
-- log [rBytesRead,rBufferPtr,rIncomingData]
repeat with tByteCounter from 1 up to rBytesRead
put rIncomingData[tByteCounter] formatted as string &" " after tReturnString
Expand Down Expand Up @@ -214,7 +220,11 @@ public handler HIDAPIOpenAndReadFromSystemPath(in pSysPath as ZStringNative, in
return "error reading from device" -- && tReturnString
else
hid_close(mSelectedDevice)
PointerBufferToData(rIncomingData,rBufferPtr,64)
if the operating system is "windows" then
win_PointerBufferToData(rIncomingData,rBufferPtr,64)
else
PointerBufferToData(rIncomingData,rBufferPtr,64)
end if
-- log [rBytesRead,rBufferPtr,rIncomingData]
repeat with tByteCounter from 1 up to rBytesRead
put rIncomingData[tByteCounter] formatted as string &" " after tReturnString
Expand Down Expand Up @@ -247,7 +257,11 @@ public handler HIDAPIEnumerate() returns optional any
-- put "DEVICE PATH\t"&"VendorID(hex)\t"&"ProductID(hex)\t"&"Manufacturer\t"&"Product\t"&"UsagePage\t"&"Usage\n" into rReturnString
put the empty string into rReturnString
put hid_enumerate(0,0) into mDevices -- pass 0,0 (VendorID,DeviceID) to get all connected HID devices
PointerToHIDDevices(rHIDDevices,mDevices, 1024)
if the operating system is "windows" then
win_PointerToHIDDevices(rHIDDevices,mDevices, 1024)
else
PointerToHIDDevices(rHIDDevices,mDevices, 1024)
end if
put element 1 of rHIDDevices into tStrPtr
if MCStringCreateWithCString(tStrPtr, tSysPath) then
put tSysPath & "\t" after rReturnString
Expand Down Expand Up @@ -298,9 +312,13 @@ public handler HIDAPIEnumerate() returns optional any
exit repeat
else
put "\n" after rReturnString
-- log ["Next Dev Pointer=", tDevice]
end if
if the operating system is "windows" then
win_PointerToHIDDevices(rNextHIDDevice,mDevices, 1024)
else
PointerToHIDDevices(rNextHIDDevice,mDevices, 1024)
end if
-- log ["Next Dev Pointer=", tDevice]
PointerToHIDDevices(rNextHIDDevice, tDevice, 1024)
put element 1 of rNextHIDDevice into tStrPtr
if MCStringCreateWithCString(tStrPtr, tSysPath) then
put tSysPath & "\t" after rReturnString
Expand Down Expand Up @@ -354,7 +372,11 @@ end handler
public handler getWCharStringSizeOf(in pPointer as Pointer) returns optional Number
variable rData as HIDByteBuffer
variable tNum as Number
PointerBufferToData(rData,pPointer,256)
if the operating system is "windows" then
win_PointerBufferToData(rData,pPointer,256)
else
PointerBufferToData(rData,pPointer,256)
end if
repeat with tNum from 1 up to 252
if element tNum of rData is 0 and element tNum+1 of rData is 0 and element tNum+2 of rData is 0 and element tNum+3 of rData is 0 then -- find *wchar_t null termination?
return tNum
Expand Down

0 comments on commit 1dfdf30

Please sign in to comment.