Skip to content

Commit

Permalink
Windows: upgrade to JDK 22
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Feb 18, 2024
1 parent 3c58363 commit dd50175
Show file tree
Hide file tree
Showing 71 changed files with 6,364 additions and 4,006 deletions.
38 changes: 16 additions & 22 deletions java-does-usb/jextract/windows/gen_win.cmd
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
set JEXTRACT=..\..\..\..\jextract\build\jextract\bin\jextract.bat
set JEXTRACT=..\..\..\..\jextract-22\bin\jextract.bat
set SDK_DIR=C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
-l Kernel32 ^
--header-class-name Kernel32 ^
--header-class-name Kernel32 ^
--target-package net.codecrete.usb.windows.gen.kernel32 ^
--include-function CloseHandle ^
--include-function GetModuleHandleW ^
Expand All @@ -32,12 +32,10 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-constant FORMAT_MESSAGE_FROM_HMODULE ^
--include-constant INFINITE ^
--include-struct _GUID ^
--include-typedef GUID ^
--include-struct _OVERLAPPED ^
--include-typedef OVERLAPPED ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand All @@ -47,13 +45,10 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-function SetupDiDestroyDeviceInfoList ^
--include-function SetupDiDeleteDeviceInterfaceData ^
--include-struct _SP_DEVINFO_DATA ^
--include-typedef SP_DEVINFO_DATA ^
--include-struct _SP_DEVICE_INTERFACE_DATA ^
--include-typedef SP_DEVICE_INTERFACE_DATA ^
--include-struct _SP_DEVICE_INTERFACE_DETAIL_DATA_W ^
--include-typedef SP_DEVICE_INTERFACE_DETAIL_DATA_W ^
--include-struct _DEVPROPKEY ^
--include-typedef DEVPROPKEY ^
--include-struct _GUID ^
--include-constant DIGCF_PRESENT ^
--include-constant DIGCF_DEVICEINTERFACE ^
--include-constant DEVPROP_TYPE_UINT32 ^
Expand All @@ -63,21 +58,22 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-constant DIREG_DEV ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
--header-class-name USBIoctl ^
--target-package net.codecrete.usb.windows.gen.usbioctl ^
--include-struct _USB_NODE_CONNECTION_INFORMATION_EX ^
--include-typedef USB_NODE_CONNECTION_INFORMATION_EX ^
--include-struct _USB_DESCRIPTOR_REQUEST ^
--include-typedef USB_DESCRIPTOR_REQUEST ^
--include-struct _USB_DEVICE_DESCRIPTOR ^
--include-struct _USB_ENDPOINT_DESCRIPTOR ^
--include-struct _USB_PIPE_INFO ^
--include-constant IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX ^
--include-constant IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand All @@ -92,16 +88,14 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-constant DBT_DEVICEREMOVECOMPLETE ^
--include-constant DBT_DEVTYP_DEVICEINTERFACE ^
--include-struct tagMSG ^
--include-typedef MSG ^
--include-struct tagPOINT ^
--include-struct tagWNDCLASSEXW ^
--include-typedef WNDCLASSEXW ^
--include-struct _DEV_BROADCAST_HDR ^
--include-typedef DEV_BROADCAST_HDR ^
--include-struct _DEV_BROADCAST_DEVICEINTERFACE_W ^
--include-typedef DEV_BROADCAST_DEVICEINTERFACE_W ^
--include-struct _GUID ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand All @@ -113,7 +107,7 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-constant RAW_IO ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand All @@ -125,7 +119,7 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-constant KEY_READ ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand All @@ -135,7 +129,7 @@ call %JEXTRACT% --source --output ../../src/main/java ^
--include-function CLSIDFromString ^
windows_headers.h

call %JEXTRACT% --source --output ../../src/main/java ^
call %JEXTRACT% --output ../../src/main/java ^
-D _AMD64_ -D _M_AMD64=100 -D UNICODE -D _UNICODE ^
-I "%SDK_DIR%\um" ^
-I "%SDK_DIR%\shared" ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private DeviceInfoSet(InfoSetCreator creator) {

// allocate SP_DEVINFO_DATA (will receive device details)
devInfoData = _SP_DEVINFO_DATA.allocate(arena);
_SP_DEVINFO_DATA.cbSize$set(devInfoData, (int) _SP_DEVINFO_DATA.$LAYOUT().byteSize());
_SP_DEVINFO_DATA.cbSize(devInfoData, (int) _SP_DEVINFO_DATA.layout().byteSize());

} catch (Exception e) {
arena.close();
Expand Down Expand Up @@ -154,7 +154,7 @@ private void addDevicePath(String devicePath) {

// load device information into dev info set
var intfData = _SP_DEVICE_INTERFACE_DATA.allocate(arena);
_SP_DEVICE_INTERFACE_DATA.cbSize$set(intfData, (int) intfData.byteSize());
_SP_DEVICE_INTERFACE_DATA.cbSize(intfData, (int) intfData.byteSize());
var devicePathSegment = Win.createSegmentFromString(devicePath, arena);
if (SetupAPI2.SetupDiOpenDeviceInterfaceW(devInfoSet, devicePathSegment, 0, intfData, errorState) == 0)
throwLastError(errorState, "internal error (SetupDiOpenDeviceInterfaceW)");
Expand Down Expand Up @@ -360,7 +360,7 @@ static String getDevicePath(String instanceId, MemorySegment interfaceGuid) {
private String getDevicePathForGuid(MemorySegment interfaceGuid) {
// retrieve first element of enumeration
devIntfData = _SP_DEVICE_INTERFACE_DATA.allocate(arena);
_SP_DEVICE_INTERFACE_DATA.cbSize$set(devIntfData, (int) devIntfData.byteSize());
_SP_DEVICE_INTERFACE_DATA.cbSize(devIntfData, (int) devIntfData.byteSize());
if (SetupAPI2.SetupDiEnumDeviceInterfaces(devInfoSet, NULL, interfaceGuid, 0, devIntfData,
errorState) == 0)
throwLastError(errorState, "internal error (SetupDiEnumDeviceInterfaces)");
Expand All @@ -370,7 +370,7 @@ private String getDevicePathForGuid(MemorySegment interfaceGuid) {
// the device path fits)
final var devicePathOffset = 4;
var intfDetailData = arena.allocate(4L + 260 * 2);
_SP_DEVICE_INTERFACE_DETAIL_DATA_W.cbSize$set(intfDetailData,
_SP_DEVICE_INTERFACE_DETAIL_DATA_W.cbSize(intfDetailData,
(int) _SP_DEVICE_INTERFACE_DETAIL_DATA_W.sizeof());
if (SetupAPI2.SetupDiGetDeviceInterfaceDetailW(devInfoSet, devIntfData, intfDetailData,
(int) intfDetailData.byteSize(), NULL, NULL, errorState) == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ private static MemorySegment createDEVPROPKEY(int data1, short data2, short data
byte data4_2, byte data4_3, byte data4_4, byte data4_5,
byte data4_6, byte data4_7, int pid) {
@SuppressWarnings("resource")
var propKey = Arena.global().allocate(_DEVPROPKEY.$LAYOUT());
Win.setGUID(_DEVPROPKEY.fmtid$slice(propKey), data1, data2, data3, data4_0, data4_1, data4_2, data4_3, data4_4
var propKey = Arena.global().allocate(_DEVPROPKEY.layout());
Win.setGUID(_DEVPROPKEY.fmtid(propKey), data1, data2, data3, data4_0, data4_1, data4_2, data4_3, data4_4
, data4_5, data4_6, data4_7);
_DEVPROPKEY.pid$set(propKey, pid);
_DEVPROPKEY.pid(propKey, pid);
return propKey;
}
}
17 changes: 10 additions & 7 deletions java-does-usb/src/main/java/net/codecrete/usb/windows/Win.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.lang.foreign.ValueLayout.JAVA_CHAR;
import static java.lang.foreign.ValueLayout.JAVA_INT;

/**
* Windows helpers.
Expand Down Expand Up @@ -49,7 +50,9 @@ static MemorySegment allocateErrorState(Arena arena) {
* @return the error code
*/
public static int getLastError(MemorySegment callState) {
return (int) callState_GetLastError$VH.get(callState);
return callState.get(JAVA_INT, 0);
// TODO: revert to varhandle
// return (int) callState_GetLastError$VH.get(callState);
}

/**
Expand Down Expand Up @@ -135,22 +138,22 @@ public static List<String> createStringListFromSegment(MemorySegment segment) {
* @param data4_7 Byte 7 of group 4
* @return GUID as memory segment
*/
@SuppressWarnings({"java:S117", "java:S107"})
@SuppressWarnings({"java:S117", "java:S107", "resource"})
public static MemorySegment createGUID(int data1, short data2, short data3, byte data4_0, byte data4_1,
byte data4_2, byte data4_3, byte data4_4, byte data4_5, byte data4_6,
byte data4_7) {
var guid = Arena.global().allocate(_GUID.$LAYOUT());
var guid = Arena.global().allocate(_GUID.layout());
setGUID(guid, data1, data2, data3, data4_0, data4_1, data4_2, data4_3, data4_4, data4_5, data4_6, data4_7);
return guid;
}

@SuppressWarnings({"java:S117", "java:S107"})
public static void setGUID(MemorySegment guid, int data1, short data2, short data3, byte data4_0, byte data4_1,
byte data4_2, byte data4_3, byte data4_4, byte data4_5, byte data4_6, byte data4_7) {
_GUID.Data1$set(guid, data1);
_GUID.Data2$set(guid, data2);
_GUID.Data3$set(guid, data3);
var data4 = _GUID.Data4$slice(guid);
_GUID.Data1(guid, data1);
_GUID.Data2(guid, data2);
_GUID.Data3(guid, data3);
var data4 = _GUID.Data4(guid);
data4.set(JAVA_BYTE, 0, data4_0);
data4.set(JAVA_BYTE, 1, data4_1);
data4.set(JAVA_BYTE, 2, data4_2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ private synchronized void completeTransfer(long overlappedAddr) {
if (transfer == null)
return;

transfer.setResultCode((int) _OVERLAPPED.Internal$get(transfer.overlapped()));
transfer.setResultSize((int) _OVERLAPPED.InternalHigh$get(transfer.overlapped()));
transfer.setResultCode((int) _OVERLAPPED.Internal(transfer.overlapped()));
transfer.setResultSize((int) _OVERLAPPED.InternalHigh(transfer.overlapped()));

availableOverlappedStructs.add(transfer.overlapped());
transfer.setOverlapped(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ synchronized void configureForAsyncIo(UsbDirection direction, int endpointNumber
throwLastError(errorState, "setting timeout failed");

var rawIoHolder = arena.allocate(JAVA_BYTE);
rawIoHolder.setAtIndex(JAVA_BYTE, 1, (byte) 1);
rawIoHolder.setAtIndex(JAVA_BYTE, 0, (byte) 1);
if (WinUSB2.WinUsb_SetPipePolicy(intfHandle.winusbHandle, endpoint.endpointAddress(), WinUSB.RAW_IO(),
(int) rawIoHolder.byteSize(), rawIoHolder, errorState) == 0)
throwLastError(errorState, "setting raw IO failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class WindowsUsbDeviceRegistry extends UsbDeviceRegistry {
private static final System.Logger LOG = System.getLogger(WindowsUsbDeviceRegistry.class.getName());

private static final long REQUEST_DATA_OFFSET
= _USB_DESCRIPTOR_REQUEST.$LAYOUT().byteOffset(PathElement.groupElement("Data"));
= _USB_DESCRIPTOR_REQUEST.layout().byteOffset(PathElement.groupElement("Data"));

@Override
protected void monitorDevices() {
Expand All @@ -93,10 +93,10 @@ protected void monitorDevices() {

// register window class
var wx = tagWNDCLASSEXW.allocate(arena);
tagWNDCLASSEXW.cbSize$set(wx, (int) wx.byteSize());
tagWNDCLASSEXW.lpfnWndProc$set(wx, handleWindowMessageStub);
tagWNDCLASSEXW.hInstance$set(wx, instance);
tagWNDCLASSEXW.lpszClassName$set(wx, className);
tagWNDCLASSEXW.cbSize(wx, (int) wx.byteSize());
tagWNDCLASSEXW.lpfnWndProc(wx, handleWindowMessageStub);
tagWNDCLASSEXW.hInstance(wx, instance);
tagWNDCLASSEXW.lpszClassName(wx, className);
var atom = User32B.RegisterClassExW(wx, errorState);
if (atom == 0)
throwLastError(errorState, "internal error (RegisterClassExW)");
Expand All @@ -109,10 +109,10 @@ protected void monitorDevices() {

// configure notifications
var notificationFilter = _DEV_BROADCAST_DEVICEINTERFACE_W.allocate(arena);
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_size$set(notificationFilter, (int) notificationFilter.byteSize());
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_devicetype$set(notificationFilter,
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_size(notificationFilter, (int) notificationFilter.byteSize());
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_devicetype(notificationFilter,
User32.DBT_DEVTYP_DEVICEINTERFACE());
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_classguid$slice(notificationFilter).copyFrom(GUID_DEVINTERFACE_USB_DEVICE);
_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_classguid(notificationFilter).copyFrom(GUID_DEVINTERFACE_USB_DEVICE);

var notifyHandle = User32B.RegisterDeviceNotificationW(hwnd, notificationFilter,
User32.DEVICE_NOTIFY_WINDOW_HANDLE(), errorState);
Expand Down Expand Up @@ -206,15 +206,15 @@ private UsbDevice createDevice(String devicePath, boolean isComposite, MemorySeg

// get device descriptor
var connInfo = _USB_NODE_CONNECTION_INFORMATION_EX.allocate(arena);
_USB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex$set(connInfo, usbPortNum);
_USB_NODE_CONNECTION_INFORMATION_EX.ConnectionIndex(connInfo, usbPortNum);
var sizeHolder = arena.allocate(JAVA_INT);
var errorState = allocateErrorState(arena);
if (Kernel32B.DeviceIoControl(hubHandle, USBIoctl.IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX(),
connInfo, (int) connInfo.byteSize(), connInfo, (int) connInfo.byteSize(), sizeHolder, NULL,
errorState) == 0)
throwLastError(errorState, "internal error (getting device descriptor failed)");

var descriptorSegment = _USB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor$slice(connInfo);
var descriptorSegment = _USB_NODE_CONNECTION_INFORMATION_EX.DeviceDescriptor(connInfo);
var deviceDescriptor = new DeviceDescriptor(descriptorSegment);

var vendorId = deviceDescriptor.vendorID();
Expand Down Expand Up @@ -243,8 +243,8 @@ private MemorySegment getDescriptor(MemorySegment hubHandle, int usbPortNumber,

// create descriptor requests
var descriptorRequest = arena.allocate(size);
_USB_DESCRIPTOR_REQUEST.ConnectionIndex$set(descriptorRequest, usbPortNumber);
var setupPacket = new SetupPacket(_USB_DESCRIPTOR_REQUEST.SetupPacket$slice(descriptorRequest));
_USB_DESCRIPTOR_REQUEST.ConnectionIndex(descriptorRequest, usbPortNumber);
var setupPacket = new SetupPacket(_USB_DESCRIPTOR_REQUEST.SetupPacket(descriptorRequest));
setupPacket.setRequestType(0x80); // device-to-host / type standard / recipient device
setupPacket.setRequest(UsbConstants.USB_REQUEST_GET_DESCRIPTOR);
setupPacket.setValue((descriptorType << 8) | index);
Expand Down Expand Up @@ -302,11 +302,11 @@ private long handleWindowMessage(MemorySegment hWnd, int uMsg, long wParam, long
// check for message related to connecting/disconnecting devices
if (uMsg == User32.WM_DEVICECHANGE() && (wParam == User32.DBT_DEVICEARRIVAL() || wParam == User32.DBT_DEVICEREMOVECOMPLETE())) {
var data = MemorySegment.ofAddress(lParam).reinterpret(_DEV_BROADCAST_DEVICEINTERFACE_W.sizeof());
if (_DEV_BROADCAST_HDR.dbch_devicetype$get(data) == User32.DBT_DEVTYP_DEVICEINTERFACE()) {
if (_DEV_BROADCAST_HDR.dbch_devicetype(data) == User32.DBT_DEVTYP_DEVICEINTERFACE()) {

// get device path
var nameSlice =
MemorySegment.ofAddress(_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_name$slice(data).address()).reinterpret(500);
MemorySegment.ofAddress(_DEV_BROADCAST_DEVICEINTERFACE_W.dbcc_name(data).address()).reinterpret(500);
var devicePath = Win.createStringFromSegment(nameSlice);
if (wParam == User32.DBT_DEVICEARRIVAL())
onDeviceConnected(devicePath);
Expand Down
Loading

0 comments on commit dd50175

Please sign in to comment.