diff --git a/bin/purejavahidapi-src.jar b/bin/purejavahidapi-src.jar index ed5f7ee..6be2c2f 100644 Binary files a/bin/purejavahidapi-src.jar and b/bin/purejavahidapi-src.jar differ diff --git a/bin/purejavahidapi.jar b/bin/purejavahidapi.jar index ee14f2b..8cc869e 100644 Binary files a/bin/purejavahidapi.jar and b/bin/purejavahidapi.jar differ diff --git a/build-jars.xml b/build-jars.xml index 645f164..63b1a4b 100644 --- a/build-jars.xml +++ b/build-jars.xml @@ -15,11 +15,13 @@ - + + - + + diff --git a/lib/jna-4.0.0.jar b/lib/jna-4.0.0.jar deleted file mode 100644 index 9038048..0000000 Binary files a/lib/jna-4.0.0.jar and /dev/null differ diff --git a/lib/jna-5.5.0.jar b/lib/jna-5.5.0.jar new file mode 100644 index 0000000..ff423c8 Binary files /dev/null and b/lib/jna-5.5.0.jar differ diff --git a/lib/jna-platform-5.5.0.jar b/lib/jna-platform-5.5.0.jar new file mode 100644 index 0000000..d180baa Binary files /dev/null and b/lib/jna-platform-5.5.0.jar differ diff --git a/pom.xml b/pom.xml index 9e46eca..7127d6f 100644 --- a/pom.xml +++ b/pom.xml @@ -66,10 +66,15 @@ + + net.java.dev.jna + jna-platform + 5.5.0 + net.java.dev.jna jna - 4.0.0 + 5.5.0 diff --git a/src/purejavahidapi/linux/CLibrary.java b/src/purejavahidapi/linux/CLibrary.java index 8aff496..dcfc549 100644 --- a/src/purejavahidapi/linux/CLibrary.java +++ b/src/purejavahidapi/linux/CLibrary.java @@ -11,7 +11,7 @@ import com.sun.jna.Structure; public class CLibrary { - static CLibraryInterface INSTANCE = (CLibraryInterface) Native.loadLibrary("c", CLibraryInterface.class); + static CLibraryInterface INSTANCE = (CLibraryInterface) Native.load("c", CLibraryInterface.class); public static short POLLIN = 0x0001; diff --git a/src/purejavahidapi/linux/UdevLibrary.java b/src/purejavahidapi/linux/UdevLibrary.java index d9ab65f..094984d 100644 --- a/src/purejavahidapi/linux/UdevLibrary.java +++ b/src/purejavahidapi/linux/UdevLibrary.java @@ -43,7 +43,7 @@ public class UdevLibrary { // OBSOLETE class: hid_device - static UdevInterface INSTANCE = (UdevInterface) Native.loadLibrary("udev", UdevInterface.class); + static UdevInterface INSTANCE = (UdevInterface) Native.load("udev", UdevInterface.class); public final static int BUS_USB = 0x03; public final static int BUS_BLUETOOTH = 0x05; diff --git a/src/purejavahidapi/macosx/CoreFoundationLibrary.java b/src/purejavahidapi/macosx/CoreFoundationLibrary.java index 695f665..2a6df6b 100644 --- a/src/purejavahidapi/macosx/CoreFoundationLibrary.java +++ b/src/purejavahidapi/macosx/CoreFoundationLibrary.java @@ -38,7 +38,7 @@ public class CoreFoundationLibrary { private static String m_NativeLibName = "CoreFoundation"; - private static CoreFoundationLibraryInterface INSTANCE = (CoreFoundationLibraryInterface) Native.loadLibrary(m_NativeLibName, CoreFoundationLibraryInterface.class); + private static CoreFoundationLibraryInterface INSTANCE = (CoreFoundationLibraryInterface) Native.load(m_NativeLibName, CoreFoundationLibraryInterface.class); private static NativeLibrary NINSTANCE = NativeLibrary.getInstance(m_NativeLibName); // -------------------------------------------------------------------------------- diff --git a/src/purejavahidapi/macosx/IOHIDManagerLibrary.java b/src/purejavahidapi/macosx/IOHIDManagerLibrary.java index 1cda94f..731cd82 100644 --- a/src/purejavahidapi/macosx/IOHIDManagerLibrary.java +++ b/src/purejavahidapi/macosx/IOHIDManagerLibrary.java @@ -40,7 +40,7 @@ import com.sun.jna.*; public class IOHIDManagerLibrary { - private static IOHIDManagerLib INSTANCE = (IOHIDManagerLib) Native.loadLibrary("IOKit", IOHIDManagerLib.class); + private static IOHIDManagerLib INSTANCE = (IOHIDManagerLib) Native.load("IOKit", IOHIDManagerLib.class); public static final int kIOHIDOptionsTypeNone = 0; public static final int kIOHIDOptionsTypeSeizeDevice = 1; diff --git a/src/purejavahidapi/windows/CfgmgrLibrary.java b/src/purejavahidapi/windows/CfgmgrLibrary.java deleted file mode 100644 index 84edb2b..0000000 --- a/src/purejavahidapi/windows/CfgmgrLibrary.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright (c) 2016, Kustaa Nyholm / SpareTimeLabs - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -package purejavahidapi.windows; - -import com.sun.jna.Library; -import com.sun.jna.Native; -import com.sun.jna.win32.W32APIOptions; - -public class CfgmgrLibrary { - public static int CR_SUCCESS = 0; - static CfgmgrInterface INSTANCE = (CfgmgrInterface) Native.loadLibrary("CfgMgr32", CfgmgrInterface.class,W32APIOptions.UNICODE_OPTIONS); - - public interface CfgmgrInterface extends Library { - int CM_Get_Parent(int[] pdnDevInst, int dnDevInst, int ulFlags); - - int CM_Get_Device_ID(int dnDevInst, char[] Buffer, int BufferLen, int ulFlags); - - int CM_Get_Device_ID_Size(int[] pulLen, int dnDevInst, int ulFlags); - - } - - public static int CM_Get_Parent(int[] pdnDevInst, int dnDevInst, int ulFlags) { - return INSTANCE.CM_Get_Parent(pdnDevInst, dnDevInst, ulFlags); - } - - public static int CM_Get_Device_ID(int dnDevInst, char[] Buffer, int BufferLen, int ulFlags) { - return INSTANCE.CM_Get_Device_ID(dnDevInst, Buffer, BufferLen, ulFlags); - } - - public static int CM_Get_Device_ID_Size(int[] pulLen, int dnDevInst, int ulFlags) { - return INSTANCE.CM_Get_Device_ID_Size(pulLen, dnDevInst, ulFlags); - } - -} diff --git a/src/purejavahidapi/windows/DeviceRemovalHandler.java b/src/purejavahidapi/windows/DeviceRemovalHandler.java index 629b148..907c23a 100644 --- a/src/purejavahidapi/windows/DeviceRemovalHandler.java +++ b/src/purejavahidapi/windows/DeviceRemovalHandler.java @@ -29,16 +29,24 @@ */ package purejavahidapi.windows; +import static com.sun.jna.platform.win32.DBT.*; +import static com.sun.jna.platform.win32.Kernel32.INSTANCE; +import static com.sun.jna.platform.win32.User32.DEVICE_NOTIFY_WINDOW_HANDLE; +import static com.sun.jna.platform.win32.User32.WS_EX_TOPMOST; +import static com.sun.jna.platform.win32.WinBase.INVALID_HANDLE_VALUE; +import static com.sun.jna.platform.win32.WinUser.WM_DESTROY; +import static com.sun.jna.platform.win32.WinUser.WM_DEVICECHANGE; +import static com.sun.jna.platform.win32.Wtsapi32.NOTIFY_FOR_THIS_SESSION; import static purejavahidapi.windows.SetupApiLibrary.*; -import com.sun.jna.WString; +import com.sun.jna.platform.win32.DBT.*; +import com.sun.jna.platform.win32.User32; +import com.sun.jna.platform.win32.WinUser.*; +import com.sun.jna.platform.win32.WinDef.*; + +import com.sun.jna.platform.win32.Wtsapi32; import purejavahidapi.shared.SyncPoint; -import purejavahidapi.windows.WinDef.*; -import static purejavahidapi.windows.WinDef.*; -import static purejavahidapi.windows.Kernel32Library.*; -import static purejavahidapi.windows.User32Library.*; -import static purejavahidapi.windows.WtsApi32Library.*; import static purejavahidapi.windows.WindowsBackend.reportLastError; public class DeviceRemovalHandler implements WindowProc { @@ -50,8 +58,8 @@ public DeviceRemovalHandler(WindowsBackend windowsBackend) { m_StartupSync = new SyncPoint(2); Runnable threadRunnable = new Runnable() { public void run() { - WString wndClassName = new WString("WindowClass"); - HMODULE hInst = GetModuleHandle(null); + String wndClassName = new String("WindowClass"); + HMODULE hInst = INSTANCE.GetModuleHandle(null); if (hInst == null) reportLastError(); @@ -60,16 +68,16 @@ public void run() { wndClassEx.lpfnWndProc = DeviceRemovalHandler.this; wndClassEx.lpszClassName = wndClassName; - ATOM wndClassRef = RegisterClassEx(wndClassEx); + ATOM wndClassRef = User32.INSTANCE.RegisterClassEx(wndClassEx); if (wndClassRef == null) reportLastError(); - HWND hWnd = CreateWindowEx(WS_EX_TOPMOST, "WindowClass", "", 0, 0, 0, 0, 0, null, null, hInst, null); + HWND hWnd = User32.INSTANCE.CreateWindowEx(WS_EX_TOPMOST, "WindowClass", "", 0, 0, 0, 0, 0, null, null, hInst, null); if (hWnd == null) reportLastError(); - if (!WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION)) + if (!Wtsapi32.INSTANCE.WTSRegisterSessionNotification(hWnd, NOTIFY_FOR_THIS_SESSION)) reportLastError(); DEV_BROADCAST_DEVICEINTERFACE notificationFilter = new DEV_BROADCAST_DEVICEINTERFACE(); @@ -77,28 +85,28 @@ public void run() { notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; notificationFilter.dbcc_classguid = GUID_DEVINTERFACE_USB_DEVICE; - HDEVNOTIFY hDevNotify = RegisterDeviceNotification(hWnd, notificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); + HDEVNOTIFY hDevNotify = User32.INSTANCE.RegisterDeviceNotification(hWnd, notificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE); if (hDevNotify == null) reportLastError(); m_StartupSync.waitAndSync(); MSG msg = new MSG(); - while (GetMessage(msg, hWnd, 0, 0) != 0) { - TranslateMessage(msg); - DispatchMessage(msg); + while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) != 0) { + User32.INSTANCE.TranslateMessage(msg); + User32.INSTANCE.DispatchMessage(msg); } - if (!UnregisterDeviceNotification(hDevNotify)) + if (!User32.INSTANCE.UnregisterDeviceNotification(hDevNotify)) reportLastError(); - if (!WTSUnRegisterSessionNotification(hWnd)) + if (!Wtsapi32.INSTANCE.WTSUnRegisterSessionNotification(hWnd)) reportLastError(); - if (!UnregisterClass(wndClassRef /* "WindowClass" */, hInst)) + if (!User32.INSTANCE.UnregisterClass(wndClassRef.toString() /* "WindowClass" */, hInst)) reportLastError(); - if (!DestroyWindow(hWnd)) + if (!User32.INSTANCE.DestroyWindow(hWnd)) reportLastError(); } }; @@ -111,15 +119,15 @@ public void run() { public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_DESTROY: { - PostQuitMessage(0); + User32.INSTANCE.PostQuitMessage(0); return new LRESULT(0); } case WM_DEVICECHANGE: { LRESULT lResult = this.onDeviceChange(wParam, lParam); - return lResult != null ? lResult : DefWindowProc(hwnd, uMsg, wParam, lParam); + return lResult != null ? lResult : User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam); } default: - return DefWindowProc(hwnd, uMsg, wParam, lParam); + return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam); } } @@ -162,7 +170,7 @@ protected LRESULT onDeviceChangeArrivalOrRemoveComplete(LPARAM lParam, String ac switch (bhdr.dbch_devicetype) { case DBT_DEVTYP_DEVICEINTERFACE: { DEV_BROADCAST_DEVICEINTERFACE bdif = new DEV_BROADCAST_DEVICEINTERFACE(bhdr.getPointer()); - handleDeviceRemoval(bdif.get_dbcc_name()); + handleDeviceRemoval(bdif.getDbcc_name()); break; } default: diff --git a/src/purejavahidapi/windows/HidDevice.java b/src/purejavahidapi/windows/HidDevice.java index f25df94..b4a7bae 100644 --- a/src/purejavahidapi/windows/HidDevice.java +++ b/src/purejavahidapi/windows/HidDevice.java @@ -29,29 +29,37 @@ */ package purejavahidapi.windows; +import static com.sun.jna.platform.win32.WinBase.*; +import static com.sun.jna.platform.win32.WinBase.INFINITE; +import static com.sun.jna.platform.win32.WinBase.WAIT_OBJECT_0; +import static com.sun.jna.platform.win32.WinError.ERROR_DEVICE_NOT_CONNECTED; +import static com.sun.jna.platform.win32.WinError.ERROR_IO_PENDING; +import static com.sun.jna.platform.win32.WinError.ERROR_OPERATION_ABORTED; import static purejavahidapi.windows.HidLibrary.HidD_FreePreparsedData; import static purejavahidapi.windows.HidLibrary.HidD_GetAttributes; import static purejavahidapi.windows.HidLibrary.HidD_GetPreparsedData; import static purejavahidapi.windows.HidLibrary.HidD_SetFeature; import static purejavahidapi.windows.HidLibrary.HidD_SetOutputReport; import static purejavahidapi.windows.HidLibrary.HidP_GetCaps; -import static purejavahidapi.windows.Kernel32Library.*; import static purejavahidapi.windows.SetupApiLibrary.HIDP_STATUS_SUCCESS; -import static purejavahidapi.windows.WinDef.INVALID_HANDLE_VALUE; +import static purejavahidapi.windows.Kernel32Library.*; import com.sun.jna.Memory; import com.sun.jna.NativeLong; import com.sun.jna.Pointer; +import com.sun.jna.platform.win32.Kernel32; +import com.sun.jna.platform.win32.WinBase.*; +import com.sun.jna.platform.win32.WinNT.HANDLE; + import purejavahidapi.shared.SyncPoint; import purejavahidapi.windows.HidLibrary.HIDD_ATTRIBUTES; import purejavahidapi.windows.HidLibrary.HIDP_CAPS; import purejavahidapi.windows.HidLibrary.HIDP_PREPARSED_DATA; -import purejavahidapi.windows.WinDef.HANDLE; -import purejavahidapi.windows.WinDef.OVERLAPPED; import java.io.BufferedWriter; import java.io.FileWriter; +import java.util.Arrays; public class HidDevice extends purejavahidapi.HidDevice { private WindowsBackend m_Backend; @@ -66,6 +74,7 @@ public class HidDevice extends purejavahidapi.HidDevice { private SyncPoint m_SyncShutdown; private boolean m_StopThread; private boolean m_ForceControlOutput; + private byte[] m_OutputReportArray; /* package */ HidDevice(purejavahidapi.HidDeviceInfo deviceInfo, WindowsBackend backend) { HANDLE handle = WindowsBackend.openDeviceHandle(deviceInfo.getPath(), false); @@ -83,18 +92,20 @@ public class HidDevice extends purejavahidapi.HidDevice { HIDP_PREPARSED_DATA[] ppd = new HIDP_PREPARSED_DATA[1]; res = HidD_GetPreparsedData(handle, ppd); if (!res) { - CloseHandle(handle); + Kernel32.INSTANCE.CloseHandle(handle); return; } HIDP_CAPS caps = new HIDP_CAPS(); int nt_res = HidP_GetCaps(ppd[0], caps); if (nt_res != HIDP_STATUS_SUCCESS) { - CloseHandle(handle); + Kernel32.INSTANCE.CloseHandle(handle); return; } m_OutputReportLength = caps.OutputReportByteLength; - if (m_OutputReportLength > 0) + if (m_OutputReportLength > 0) { m_OutputReportMemory = new Memory(m_OutputReportLength); + m_OutputReportArray = new byte[m_OutputReportLength]; + } m_OutputReportOverlapped = new OVERLAPPED(); m_OutputReportBytesWritten = new int[] { 0 @@ -126,7 +137,7 @@ public void run() { m_ForceControlOutput = System.getProperty("purejavahidapi.forceControlOutput") != null; // bManualReset parameter has to be set to true to work with WaitForSingleObject - m_OutputReportOverlapped.hEvent = CreateEvent(null, true, false, null); + m_OutputReportOverlapped.hEvent = Kernel32.INSTANCE.CreateEvent(null, true, false, null); } @Override @@ -140,8 +151,8 @@ synchronized public void close() { m_Thread.interrupt(); m_SyncShutdown.waitAndSync(); } - CloseHandle(m_OutputReportOverlapped.hEvent); - CloseHandle(m_Handle); + Kernel32.INSTANCE.CloseHandle(m_OutputReportOverlapped.hEvent); + Kernel32.INSTANCE.CloseHandle(m_Handle); m_Backend.removeDevice(m_HidDeviceInfo.getDeviceId()); m_Open = false; } @@ -154,13 +165,13 @@ synchronized public int setOutputReport(byte reportID, byte[] data, int length) throw new IllegalArgumentException("this device supports no output reports"); // In Windows writeFile() to HID device data has to be preceded with the report // number, regardless - m_OutputReportMemory.write(0, new byte[] { - reportID - }, 0, 1); - m_OutputReportMemory.write(1, data, 0, length); + Arrays.fill(m_OutputReportArray, (byte)0); + m_OutputReportArray[0] = reportID; + System.arraycopy(data, 0, m_OutputReportArray, 1, length); + if (!m_ForceControlOutput) { - ResetEvent(m_OutputReportOverlapped.hEvent); + Kernel32.INSTANCE.ResetEvent(m_OutputReportOverlapped.hEvent); m_OutputReportOverlapped.Internal = null; m_OutputReportOverlapped.InternalHigh = null; m_OutputReportOverlapped.Offset = 0; @@ -168,16 +179,16 @@ synchronized public int setOutputReport(byte reportID, byte[] data, int length) // In windows always attempt to write as many bytes as there are in the longest // report plus one for the report number (even if zero ie not used) - if (!WriteFile(m_Handle, m_OutputReportMemory, m_OutputReportLength, null, m_OutputReportOverlapped)) { - if (GetLastError() != ERROR_IO_PENDING) { + if (!Kernel32.INSTANCE.WriteFile(m_Handle, m_OutputReportArray, m_OutputReportLength, null, m_OutputReportOverlapped)) { + if (Kernel32.INSTANCE.GetLastError() != ERROR_IO_PENDING) { // WriteFile() failed. Return error. // register_error(dev, "WriteFile"); return -1; } } - if (WAIT_OBJECT_0 != WaitForSingleObject(m_OutputReportOverlapped.hEvent, 1000)) { - return -1; + if (WAIT_OBJECT_0 != Kernel32.INSTANCE.WaitForSingleObject(m_OutputReportOverlapped.hEvent, 1000)) { + return -2; } // Update structure from native code @@ -186,15 +197,20 @@ synchronized public int setOutputReport(byte reportID, byte[] data, int length) if (!GetOverlappedResult(m_Handle, m_OutputReportOverlapped, m_OutputReportBytesWritten, false/* don't need to wait */)) { // The Write operation failed. // register_error(dev, "WriteFile"); - return -1; + return -3; } return m_OutputReportBytesWritten[0] - 1; } else { + m_OutputReportMemory.write(0, new byte[] { + reportID + }, 0, 1); + m_OutputReportMemory.write(1, data, 0, length); + if (!HidD_SetOutputReport(m_Handle, m_OutputReportMemory.getByteArray(0, length + 1), length + 1)) { // HidD_SetOutputReport() failed. Return error. // register_error(dev, "HidD_SetOutputReport"); - return -1; + return -4; } return length; @@ -241,7 +257,7 @@ synchronized public int getFeatureReport(byte[] data, int length) { Pointer buffer = new Memory(data.length); if (!DeviceIoControl(m_Handle, IOCTL_HID_GET_FEATURE, buffer, length, buffer, length, bytes, ol)) { // System.out.println(GetLastError()); - if (GetLastError() != ERROR_IO_PENDING) + if (Kernel32.INSTANCE.GetLastError() != ERROR_IO_PENDING) return -1; } @@ -258,11 +274,11 @@ private void runReadOnBackground() { int[] numBytesRead = { 0 }; OVERLAPPED overlapped = new OVERLAPPED(); - overlapped.hEvent = CreateEvent(null, true, true, null); + overlapped.hEvent = Kernel32.INSTANCE.CreateEvent(null, true, true, null); Memory readBuffer = new Memory(m_InputReportLength); while (!m_StopThread) { - ResetEvent(overlapped.hEvent); + Kernel32.INSTANCE.ResetEvent(overlapped.hEvent); numBytesRead[0] = 0; overlapped.Internal = null; overlapped.InternalHigh = null; @@ -275,33 +291,33 @@ private void runReadOnBackground() { // is always preceded with the report number (even if not used in // case of which it is zero) if (!ReadFile(m_Handle, readBuffer, m_InputReportLength, numBytesRead, overlapped)) { - if (GetLastError() == ERROR_DEVICE_NOT_CONNECTED) + if (Kernel32.INSTANCE.GetLastError() == ERROR_DEVICE_NOT_CONNECTED) break; // early exit if the device disappears - if (GetLastError() != ERROR_IO_PENDING) { + if (Kernel32.INSTANCE.GetLastError() != ERROR_IO_PENDING) { CancelIo(m_Handle); - System.err.println("ReadFile failed with GetLastError()==" + GetLastError()); + System.err.println("ReadFile failed with GetLastError()==" + Kernel32.INSTANCE.GetLastError()); } if (WAIT_OBJECT_0 != WaitForSingleObject(overlapped.hEvent, INFINITE)) { - System.err.println("WaitForSingleObject failed with GetLastError()==" + GetLastError()); + System.err.println("WaitForSingleObject failed with GetLastError()==" + Kernel32.INSTANCE.GetLastError()); } // Update structure from native code overlapped.read(); if (!GetOverlappedResult(m_Handle, overlapped, numBytesRead, false/* don't need to wait */)) { - if (GetLastError() == ERROR_DEVICE_NOT_CONNECTED) + if (Kernel32.INSTANCE.GetLastError() == ERROR_DEVICE_NOT_CONNECTED) break; // early exit if the device disappears - if (m_StopThread && GetLastError() == ERROR_OPERATION_ABORTED) + if (m_StopThread && Kernel32.INSTANCE.GetLastError() == ERROR_OPERATION_ABORTED) break; // on close - System.err.println("GetOverlappedResult failed with GetLastError()==" + GetLastError()); + System.err.println("GetOverlappedResult failed with GetLastError()==" + Kernel32.INSTANCE.GetLastError()); } } processInputReport(readBuffer, numBytesRead[0]); } - CloseHandle(overlapped.hEvent); + Kernel32.INSTANCE.CloseHandle(overlapped.hEvent); m_SyncShutdown.waitAndSync(); } diff --git a/src/purejavahidapi/windows/HidDeviceInfo.java b/src/purejavahidapi/windows/HidDeviceInfo.java index 5d787fd..7e6e8e4 100644 --- a/src/purejavahidapi/windows/HidDeviceInfo.java +++ b/src/purejavahidapi/windows/HidDeviceInfo.java @@ -42,7 +42,7 @@ import purejavahidapi.windows.HidLibrary.HIDD_ATTRIBUTES; import purejavahidapi.windows.HidLibrary.HIDP_CAPS; import purejavahidapi.windows.HidLibrary.HIDP_PREPARSED_DATA; -import purejavahidapi.windows.WinDef.HANDLE; +import com.sun.jna.platform.win32.WinNT.HANDLE; /* package*/class HidDeviceInfo extends purejavahidapi.HidDeviceInfo { diff --git a/src/purejavahidapi/windows/HidLibrary.java b/src/purejavahidapi/windows/HidLibrary.java index 3e00d4e..f9250b0 100644 --- a/src/purejavahidapi/windows/HidLibrary.java +++ b/src/purejavahidapi/windows/HidLibrary.java @@ -40,10 +40,10 @@ import com.sun.jna.Union; import com.sun.jna.win32.StdCallLibrary; -import purejavahidapi.windows.WinDef.HANDLE; +import com.sun.jna.platform.win32.WinNT.HANDLE; public class HidLibrary { - static HidLibraryInterface INSTANCE = (HidLibraryInterface) Native.loadLibrary("hid", HidLibraryInterface.class); + static HidLibraryInterface INSTANCE = (HidLibraryInterface) Native.load("hid", HidLibraryInterface.class); static public class HIDP_PREPARSED_DATA extends PointerType { } diff --git a/src/purejavahidapi/windows/Kernel32Library.java b/src/purejavahidapi/windows/Kernel32Library.java index 82ac4f6..2d416e4 100644 --- a/src/purejavahidapi/windows/Kernel32Library.java +++ b/src/purejavahidapi/windows/Kernel32Library.java @@ -34,13 +34,13 @@ import com.sun.jna.win32.StdCallLibrary; import com.sun.jna.win32.W32APIOptions; -import purejavahidapi.windows.WinDef.HANDLE; -import purejavahidapi.windows.WinDef.HMODULE; -import purejavahidapi.windows.WinDef.OVERLAPPED; -import purejavahidapi.windows.WinDef.SECURITY_ATTRIBUTES; +import com.sun.jna.platform.win32.WinNT.HANDLE; +import com.sun.jna.platform.win32.WinDef.HMODULE; +import com.sun.jna.platform.win32.WinBase.OVERLAPPED; +import com.sun.jna.platform.win32.WinBase.SECURITY_ATTRIBUTES; public class Kernel32Library { - static Kernel32Interface INSTANCE = (Kernel32Interface) Native.loadLibrary("kernel32", Kernel32Interface.class, W32APIOptions.UNICODE_OPTIONS); + static Kernel32Interface INSTANCE = (Kernel32Interface) Native.load("kernel32", Kernel32Interface.class, W32APIOptions.UNICODE_OPTIONS); public static final int ERROR_INSUFFICIENT_BUFFER = 122; public static final int ERROR_INVALID_USER_BUFFER = 1784; diff --git a/src/purejavahidapi/windows/SetupApiLibrary.java b/src/purejavahidapi/windows/SetupApiLibrary.java index 40afd64..f69accc 100644 --- a/src/purejavahidapi/windows/SetupApiLibrary.java +++ b/src/purejavahidapi/windows/SetupApiLibrary.java @@ -127,11 +127,11 @@ import com.sun.jna.Structure; import com.sun.jna.win32.W32APIOptions; -import purejavahidapi.windows.WinDef.HANDLE; -import purejavahidapi.windows.WinDef.HWND; +import com.sun.jna.platform.win32.WinNT.HANDLE; +import com.sun.jna.platform.win32.WinDef.HWND; public class SetupApiLibrary { - static SetupApiInterface INSTANCE = (SetupApiInterface) Native.loadLibrary("setupapi", SetupApiInterface.class, + static SetupApiInterface INSTANCE = (SetupApiInterface) Native.load("setupapi", SetupApiInterface.class, W32APIOptions.UNICODE_OPTIONS); public static final int DIGCF_PRESENT = 2; @@ -227,7 +227,7 @@ protected List getFieldOrder() { final static int ANYSIZE_ARRAY = 1; static public class SP_DEVICE_INTERFACE_DETAIL_DATA_A extends Structure { - public int cbSize = Pointer.SIZE == 8 ? 8 : 5; // Note 1 + public int cbSize = Native.POINTER_SIZE == 8 ? 8 : 5; // Note 1 // Note 1, I believe this structure is packed in Windows API and as this field // is initialized with sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) it gets the size // 5 in 32 bit process but 8 in 64 bit process...i think! Nasty little detail @@ -292,7 +292,7 @@ boolean SetupDiOpenDeviceInterface(HDEVINFO DeviceInfoSet, String DevicePath, in boolean SetupDiDeleteDeviceInterfaceData(HDEVINFO DeviceInfoSet, SP_DEVICE_INTERFACE_DATA DeviceInterfaceData); boolean SetupDiOpenDeviceInfo(HDEVINFO DeviceInfoSet, String DeviceInstanceId, HWND hwndParent, int OpenFlags, - SP_DEVINFO_DATA DeviceInfoData); + SP_DEVINFO_DATA DeviceInfoData); } public static HDEVINFO SetupDiCreateDeviceInfoList(GUID ClassGuid, HWND hwndParent) { diff --git a/src/purejavahidapi/windows/User32Library.java b/src/purejavahidapi/windows/User32Library.java deleted file mode 100644 index 92108df..0000000 --- a/src/purejavahidapi/windows/User32Library.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * Copyright (c) 2016, Kustaa Nyholm / SpareTimeLabs - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -package purejavahidapi.windows; - -import java.util.Arrays; -import java.util.List; - -import com.sun.jna.Callback; -import com.sun.jna.Native; -import com.sun.jna.Pointer; -import com.sun.jna.Structure; -import com.sun.jna.WString; -import com.sun.jna.win32.StdCallLibrary; -import com.sun.jna.win32.W32APIOptions; - -import purejavahidapi.windows.WinDef.ATOM; -import purejavahidapi.windows.WinDef.HANDLE; -import purejavahidapi.windows.WinDef.HBRUSH; -import purejavahidapi.windows.WinDef.HCURSOR; -import purejavahidapi.windows.WinDef.HICON; -import purejavahidapi.windows.WinDef.HINSTANCE; -import purejavahidapi.windows.WinDef.HMENU; -import purejavahidapi.windows.WinDef.HWND; -import purejavahidapi.windows.WinDef.LPARAM; -import purejavahidapi.windows.WinDef.LPVOID; -import purejavahidapi.windows.WinDef.LRESULT; -import purejavahidapi.windows.WinDef.POINT; -import purejavahidapi.windows.WinDef.PVOID; -import purejavahidapi.windows.WinDef.WPARAM; - -@SuppressWarnings("serial") -public class User32Library { - static User32Interface INSTANCE = (User32Interface) Native.loadLibrary("user32", User32Interface.class, W32APIOptions.UNICODE_OPTIONS); - - public static final int WS_EX_TOPMOST = 0x00000008; - public static final int DEVICE_NOTIFY_WINDOW_HANDLE = 0x00000000; - - public static final int WM_CREATE = 0x0001; - public static final int WM_SIZE = 0x0005; - public static final int WM_DESTROY = 0x0002; - public static final int WM_DEVICECHANGE = 0x0219; - - - public static class HDEVNOTIFY extends PVOID { - public HDEVNOTIFY() { - - } - - public HDEVNOTIFY(Pointer p) { - super(p); - } - } - - public static class WNDCLASSEX extends Structure { - - public static class ByReference extends WNDCLASSEX implements Structure.ByReference { - } - - public WNDCLASSEX() { - } - - public WNDCLASSEX(Pointer memory) { - super(memory); - read(); - } - - public int cbSize = this.size(); - - public int style; - - public Callback lpfnWndProc; - - public int cbClsExtra; - - public int cbWndExtra; - - /** The h instance. */ - public HINSTANCE hInstance; - - public HICON hIcon; - - public HCURSOR hCursor; - - public HBRUSH hbrBackground; - - public String lpszMenuName; - - public WString lpszClassName; - - public HICON hIconSm; - - @Override - protected List getFieldOrder() { - return Arrays.asList(new String[] { "cbSize", "style", "lpfnWndProc", "cbClsExtra", "cbWndExtra", "hInstance", "hIcon", "hCursor", "hbrBackground", "lpszMenuName", "lpszClassName", "hIconSm" }); - } - } - - public static class MSG extends Structure { - public HWND hWnd; - public int message; - public WPARAM wParam; - public LPARAM lParam; - public int time; - public POINT pt; - - @Override - protected List getFieldOrder() { - return Arrays.asList(new String[] { "hWnd", "message", "wParam", "lParam", "time", "pt" }); - } - } - - interface User32Interface extends StdCallLibrary { - ATOM RegisterClassEx(WNDCLASSEX lpwcx); - - HWND CreateWindowEx(int dwExStyle, String lpClassName, String lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam); - - HDEVNOTIFY RegisterDeviceNotification(HANDLE hRecipient, Structure notificationFilter, int Flags); - - int GetMessage(MSG lpMsg, HWND hWnd, int wMsgFilterMin, int wMsgFilterMax); - - boolean TranslateMessage(MSG msg); - - LRESULT DispatchMessage(MSG msg); - - boolean UnregisterDeviceNotification(HDEVNOTIFY arg0); - - boolean UnregisterClass(String arg0, HINSTANCE arg1); - - boolean UnregisterClass(WString arg0, HINSTANCE arg1); - - boolean UnregisterClass(int arg0, HINSTANCE arg1); - - boolean DestroyWindow(HWND hWnd); - - void PostQuitMessage(int nExitCode); - - LRESULT DefWindowProc(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam); - - } - - public static ATOM RegisterClassEx(WNDCLASSEX lpwcx) { - return INSTANCE.RegisterClassEx(lpwcx); - } - - public static HWND CreateWindowEx(int dwExStyle, String lpClassName, String lpWindowName, int dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam) { - return INSTANCE.CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, hMenu, hInstance, lpParam); - } - - public static HDEVNOTIFY RegisterDeviceNotification(HANDLE hRecipient, Structure notificationFilter, int Flags){ - return INSTANCE.RegisterDeviceNotification( hRecipient, notificationFilter, Flags); - } - - public static int GetMessage(MSG lpMsg, HWND hWnd, int wMsgFilterMin, int wMsgFilterMax){ - return INSTANCE.GetMessage( lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax); - } - - public static boolean TranslateMessage(MSG msg){ - return INSTANCE.TranslateMessage( msg); - } - - public static LRESULT DispatchMessage(MSG msg){ - return INSTANCE.DispatchMessage( msg); - } - - public static boolean UnregisterDeviceNotification(HDEVNOTIFY arg0){ - return INSTANCE.UnregisterDeviceNotification(arg0); - } - - public static boolean UnregisterClass(String arg0, HINSTANCE arg1){ - return INSTANCE.UnregisterClass( arg0, arg1); - } - - public static boolean UnregisterClass(WString arg0, HINSTANCE arg1){ - return INSTANCE.UnregisterClass( arg0, arg1); - } - - public static boolean UnregisterClass(ATOM arg0, HINSTANCE arg1){ - return INSTANCE.UnregisterClass( arg0.intValue(), arg1); - } - - public static boolean DestroyWindow(HWND hWnd){ - return INSTANCE.DestroyWindow( hWnd); - } - - public static void PostQuitMessage(int nExitCode) { - INSTANCE.PostQuitMessage(nExitCode); - } - - public static LRESULT DefWindowProc(HWND hWnd, int Msg, WPARAM wParam, LPARAM lParam) { - return INSTANCE.DefWindowProc( hWnd, Msg, wParam, lParam); - } - -} \ No newline at end of file diff --git a/src/purejavahidapi/windows/WinDef.java b/src/purejavahidapi/windows/WinDef.java deleted file mode 100644 index de118fd..0000000 --- a/src/purejavahidapi/windows/WinDef.java +++ /dev/null @@ -1,394 +0,0 @@ -/* - * Copyright (c) 2016, Kustaa Nyholm / SpareTimeLabs - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -package purejavahidapi.windows; - -import java.util.Arrays; -import java.util.List; - -import purejavahidapi.windows.SetupApiLibrary.GUID; - -import com.sun.jna.FromNativeContext; -import com.sun.jna.IntegerType; -import com.sun.jna.Pointer; -import com.sun.jna.PointerType; -import com.sun.jna.Structure; -import com.sun.jna.win32.StdCallLibrary.StdCallCallback; - -public class WinDef { - public static final int DBT_DEVICEREMOVECOMPLETE = 0x8004; - - public static final int DBT_DEVTYP_DEVICEINTERFACE = 0x00000005; - public static final GUID GUID_DEVINTERFACE_USB_DEVICE = new GUID(0xA5DCBF10, 0x6530, 0x11D2, 0x90,0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED); - - public static HANDLE INVALID_HANDLE_VALUE = new HANDLE(invalidPointerValue()); - public static HANDLE NULL = new HANDLE(Pointer.createConstant(0)); - - private static Pointer invalidPointerValue() { - return Pointer.createConstant(Pointer.SIZE == 8 ? -1 : 0xFFFFFFFFL); - } - - public static class PVOID extends PointerType { - - public PVOID() { - super(); - } - - public PVOID(Pointer pointer) { - super(pointer); - } - } - - public static class LPVOID extends PointerType { - public LPVOID() { - super(); - } - - public LPVOID(Pointer p) { - super(p); - } - } - - public static class HMENU extends HANDLE { - public HMENU() { - - } - - public HMENU(Pointer p) { - super(p); - } - } - - public static class HINSTANCE extends HANDLE { - - } - - public static class HMODULE extends HINSTANCE { - - } - - public static class HWND extends HANDLE { - public HWND() { - - } - - public HWND(Pointer p) { - super(p); - } - } - - public static class LONG_PTR extends IntegerType { - public LONG_PTR() { - this(0); - } - - public LONG_PTR(long value) { - super(Pointer.SIZE, value); - } - - public Pointer toPointer() { - return Pointer.createConstant(longValue()); - } - } - - public static class LPARAM extends LONG_PTR { - public LPARAM() { - this(0); - } - - public LPARAM(long value) { - super(value); - } - } - - public static class LRESULT extends LONG_PTR { - public LRESULT() { - this(0); - } - - public LRESULT(long value) { - super(value); - } - } - - public static class UINT_PTR extends IntegerType { - - public UINT_PTR() { - super(Pointer.SIZE); - } - - public UINT_PTR(long value) { - super(Pointer.SIZE, value, true); - } - - public Pointer toPointer() { - return Pointer.createConstant(longValue()); - } - } - - public static class WPARAM extends UINT_PTR { - public WPARAM() { - this(0); - } - - public WPARAM(long value) { - super(value); - } - } - - public static class WORD extends IntegerType { - public static final int SIZE = 2; - - public WORD() { - this(0); - } - - public WORD(long value) { - super(SIZE, value, true); - } - } - - public static class ATOM extends WORD { - public ATOM() { - this(0); - } - - public ATOM(long value) { - super(value); - } - } - - public static class HICON extends HANDLE { - public HICON() { - - } - - public HICON(HANDLE handle) { - this(handle.getPointer()); - } - - public HICON(Pointer p) { - super(p); - } - } - - public static class HCURSOR extends HICON { - public HCURSOR() { - - } - - public HCURSOR(Pointer p) { - super(p); - } - } - - public static class HBRUSH extends HANDLE { - public HBRUSH() { - - } - - public HBRUSH(Pointer p) { - super(p); - } - } - - public static class POINT extends Structure { - public int x; - public int y; - - public POINT() { - super(); - } - - @Override - protected List getFieldOrder() { - return Arrays.asList("x", "y"); - - } - } - - public static class DEV_BROADCAST_HDR extends Structure { - public int dbch_size = size(); - - public int dbch_devicetype; - - public int dbch_reserved; - - public DEV_BROADCAST_HDR() { - super(); - } - - public DEV_BROADCAST_HDR(long pointer) { - this(new Pointer(pointer)); - } - - public DEV_BROADCAST_HDR(Pointer memory) { - super(memory); - read(); - } - - @Override - protected List getFieldOrder() { - return Arrays.asList("dbch_size", "dbch_devicetype", "dbch_reserved"); - - } - } - - public static class DEV_BROADCAST_DEVICEINTERFACE extends Structure { - public int dbcc_size; - public int dbcc_devicetype; - public int dbcc_reserved; - public GUID dbcc_classguid; - public char[] dbcc_name = new char[1]; - - @Override - protected List getFieldOrder() { - return Arrays.asList("dbcc_size", "dbcc_devicetype", "dbcc_reserved", "dbcc_classguid", "dbcc_name"); - - } - - public DEV_BROADCAST_DEVICEINTERFACE() { - super(); - } - - public String get_dbcc_name() { - return new String(dbcc_name); - } - - public DEV_BROADCAST_DEVICEINTERFACE(Pointer memory) { - super(memory); - this.dbcc_size = (Integer) this.readField("dbcc_size"); - int len = 1 + this.dbcc_size - size(); - this.dbcc_name = new char[len]; - read(); - } - } - - public static class HANDLE extends PointerType { - private boolean immutable; - - public HANDLE() { - } - - public HANDLE(Pointer p) { - setPointer(p); - immutable = true; - } - - @Override - public Object fromNative(Object nativeValue, FromNativeContext context) { - Object o = super.fromNative(nativeValue, context); - if (INVALID_HANDLE_VALUE.equals(o)) { - return INVALID_HANDLE_VALUE; - } - return o; - } - - @Override - public void setPointer(Pointer p) { - if (immutable) { - throw new UnsupportedOperationException("immutable reference"); - } - - super.setPointer(p); - } - - @Override - public String toString() { - return String.valueOf(getPointer()); - } - } - - public static class OVERLAPPED extends Structure { - - private static boolean TRACE; - - public Pointer Internal; - - public Pointer InternalHigh; - - public int Offset; - - public int OffsetHigh; - - public HANDLE hEvent; - - @Override - protected List getFieldOrder() { - - return Arrays.asList("Internal",// - - "InternalHigh",// - - "Offset",// - - "OffsetHigh",// - - "hEvent"// - - ); - - } - - public OVERLAPPED() { - - setAutoSynch(true); - - } - - public String toString() { - - return String.format(// - - "[Offset %d OffsetHigh %d hEvent %s]",// - - Offset, OffsetHigh, hEvent.toString()); - - } - - } - - public static class SECURITY_ATTRIBUTES extends Structure { - public int nLength; - public Pointer lpSecurityDescriptor; - public boolean bInheritHandle; - - @Override - protected List getFieldOrder() { - return Arrays.asList("nLength",// - "lpSecurityDescriptor",// - "bInheritHandle"// - ); - } - } - - public interface WindowProc extends StdCallCallback { - LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam); - } -} diff --git a/src/purejavahidapi/windows/WindowsBackend.java b/src/purejavahidapi/windows/WindowsBackend.java index 52f34d7..99a129d 100644 --- a/src/purejavahidapi/windows/WindowsBackend.java +++ b/src/purejavahidapi/windows/WindowsBackend.java @@ -33,20 +33,26 @@ import java.util.LinkedList; import java.util.List; +import com.sun.jna.*; +import com.sun.jna.platform.win32.Cfgmgr32; +import com.sun.jna.ptr.IntByReference; import purejavahidapi.shared.Backend; import purejavahidapi.windows.HidLibrary.*; import purejavahidapi.windows.SetupApiLibrary.HDEVINFO; import purejavahidapi.windows.SetupApiLibrary.SP_DEVICE_INTERFACE_DATA; import purejavahidapi.windows.SetupApiLibrary.SP_DEVINFO_DATA; -import purejavahidapi.windows.WinDef.HANDLE; -import static purejavahidapi.windows.CfgmgrLibrary.*; -import static purejavahidapi.windows.WinDef.INVALID_HANDLE_VALUE; +import com.sun.jna.platform.win32.WinNT.HANDLE; + +import static com.sun.jna.platform.win32.Kernel32.INSTANCE; +import static com.sun.jna.platform.win32.WinBase.INVALID_HANDLE_VALUE; +import static com.sun.jna.platform.win32.Kernel32.*; +import static com.sun.jna.platform.win32.Cfgmgr32.*; + +import com.sun.jna.Memory; import com.sun.jna.Native; -import com.sun.jna.NativeLong; import static purejavahidapi.windows.SetupApiLibrary.*; -import static purejavahidapi.windows.Kernel32Library.*; import static purejavahidapi.windows.HidLibrary.*; public class WindowsBackend extends Backend { @@ -98,14 +104,14 @@ public List enumerateDevices() { HANDLE devHandle = INVALID_HANDLE_VALUE; if (!SetupDiEnumDeviceInterfaces(device_info_set, null, InterfaceClassGuid, deviceIndex, device_interface_data)) { - if (GetLastError() == ERROR_NO_MORE_ITEMS) + if (INSTANCE.GetLastError() == ERROR_NO_MORE_ITEMS) break; reportLastError(); } int[] required_size = { 0 }; if (!SetupDiGetDeviceInterfaceDetail(device_info_set, device_interface_data, null, 0, required_size, null)) { - if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) + if (INSTANCE.GetLastError() != ERROR_INSUFFICIENT_BUFFER) reportLastError(); } @@ -116,7 +122,7 @@ public List enumerateDevices() { if (SetupDiGetDeviceInterfaceDetail(device_info_set, device_interface_data, device_interface_detail_data, required_size[0], null, null)) break; // ok, we guessed right so we can move on device_interface_detail_data = null; - if (GetLastError() == ERROR_INVALID_USER_BUFFER) + if (INSTANCE.GetLastError() == ERROR_INVALID_USER_BUFFER) continue; // guessed from, try again with next value reportLastError(); // something else went wrong, report it } @@ -141,7 +147,7 @@ public List enumerateDevices() { String drivername = new String(driverNameChars, 0, driverNameLen - 1); if ("HIDClass".equals(drivername)) { if (!SetupDiGetDeviceRegistryProperty(device_info_set, devinfo_data, SPDRP_DRIVER, null, driverNameChars, driverNameChars.length, null)) {// ok, found a driver - if (GetLastError() != ERROR_INVALID_DATA) // Invalid data is legitime from code point of view, maybe the device does not have this property or the device is faulty + if (INSTANCE.GetLastError() != ERROR_INVALID_DATA) // Invalid data is legitime from code point of view, maybe the device does not have this property or the device is faulty reportLastError(); continue; } @@ -151,16 +157,22 @@ public List enumerateDevices() { reportLastError(); String deviceId = new String(deviceIdChars); - int[] parent = { devinfo_data.DevInst }; - while (CM_Get_Parent(parent, parent[0], 0) == 0) { - int[] parentIdLen = { 0 }; - if (CM_Get_Device_ID_Size(parentIdLen, parent[0], 0) != CR_SUCCESS) + int parent = devinfo_data.DevInst; + IntByReference rParent = new IntByReference(); + + while (Cfgmgr32.INSTANCE.CM_Get_Parent(rParent, parent, 0) == 0) { + parent = rParent.getValue(); + + IntByReference iParentIdLen = new IntByReference(0); + if (Cfgmgr32.INSTANCE.CM_Get_Device_ID_Size(iParentIdLen, parent, 0) != CR_SUCCESS) reportLastError(); - parentIdLen[0]++; - char[] parentIdChars = new char[parentIdLen[0]]; - if (CM_Get_Device_ID(parent[0], parentIdChars, parentIdLen[0], 0) != CR_SUCCESS) + int parentIdLen = iParentIdLen.getValue() + 1; + + Memory mIdChars = new Memory(parentIdLen * 2); + if (Cfgmgr32.INSTANCE.CM_Get_Device_ID(parent, mIdChars, parentIdLen, 0) != CR_SUCCESS) reportLastError(); - String parentId = new String(parentIdChars, 0, parentIdLen[0] - 1); + String parentId = mIdChars.getString(0); + if (parentId.startsWith("USB\\")) { deviceId = parentId; break; @@ -180,7 +192,7 @@ public List enumerateDevices() { HidD_GetAttributes(devHandle, attrib); list.add(new HidDeviceInfo(path, deviceId, devHandle, attrib)); - CloseHandle(devHandle); + INSTANCE.CloseHandle(devHandle); } } @@ -204,7 +216,7 @@ public purejavahidapi.HidDevice openDevice(purejavahidapi.HidDeviceInfo deviceIn int desired_access = (enumerate) ? 0 : (GENERIC_WRITE | GENERIC_READ); int share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE; - handle = CreateFile(path, desired_access, share_mode, null, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, null); + handle = INSTANCE.CreateFile(path, desired_access, share_mode, null, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, null); return handle; } diff --git a/src/purejavahidapi/windows/WtsApi32Library.java b/src/purejavahidapi/windows/WtsApi32Library.java deleted file mode 100644 index 6f2388c..0000000 --- a/src/purejavahidapi/windows/WtsApi32Library.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (c) 2016, Kustaa Nyholm / SpareTimeLabs - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * Redistributions of source code must retain the above copyright notice, this list - * of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, this - * list of conditions and the following disclaimer in the documentation and/or other - * materials provided with the distribution. - * - * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its - * contributors may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, - * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - */ -package purejavahidapi.windows; - -import purejavahidapi.windows.WinDef.HWND; - -import com.sun.jna.Native; -import com.sun.jna.win32.StdCallLibrary; -import com.sun.jna.win32.W32APIOptions; - -public class WtsApi32Library { - public static final int NOTIFY_FOR_THIS_SESSION = 0; - - static WtsApi32Interface INSTANCE = (WtsApi32Interface) Native.loadLibrary("wtsapi32", WtsApi32Interface.class, W32APIOptions.UNICODE_OPTIONS); - - interface WtsApi32Interface extends StdCallLibrary { - boolean WTSRegisterSessionNotification(HWND hWnd, int dwFlags); - boolean WTSUnRegisterSessionNotification(HWND hWnd); - - } - - public static boolean WTSRegisterSessionNotification(HWND hWnd, int dwFlags) { - return INSTANCE.WTSRegisterSessionNotification( hWnd, dwFlags); - } - - public static boolean WTSUnRegisterSessionNotification(HWND hWnd) { - return INSTANCE.WTSUnRegisterSessionNotification(hWnd); - } -}