Skip to content

Commit 8558457

Browse files
author
Lyor Goldstein
committed
Added GetRawInputDeviceList definition and utility to User32 interface
1 parent 0e57990 commit 8558457

File tree

6 files changed

+546
-415
lines changed

6 files changed

+546
-415
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Features
5151
* [#481] (https://github.com/twall/jna/pull/481): Added volume management functions to 'com.sun.jna.platform.win32' - [@lgoldstein](https://github.com/lgoldstein).
5252
* [#483] (https://github.com/twall/jna/pull/483): Found and fixed duplicate method definitions for the same API in 'com.sun.jna.platform.win32' - [@lgoldstein](https://github.com/lgoldstein).
5353
* [#485] (https://github.com/twall/jna/pull/485): Implemented Comparable interface for many of the base types in 'com.sun.jna.platform.win32.WinDef' - [@lgoldstein](https://github.com/lgoldstein).
54+
* [#488] (https://github.com/twall/jna/pull/488): Added GetRawInputDeviceList definition and utility to 'com.sun.jna.platform.win32' User32 and User32Util - [@lgoldstein](https://github.com/lgoldstein).
5455

5556
Bug Fixes
5657
---------

contrib/platform/src/com/sun/jna/platform/win32/User32.java

+24
Original file line numberDiff line numberDiff line change
@@ -1983,4 +1983,28 @@ long SendMessageTimeout(HWND hWnd, int msg, long wParam, long lParam,
19831983
* error information, call {@link Kernel32#GetLastError()}.
19841984
*/
19851985
long GetClassLongPtr(HWND hWnd, int nIndex);
1986+
1987+
/**
1988+
* @param pRawInputDeviceList
1989+
* An array of {@link RAWINPUTDEVICELIST} structures for the devices
1990+
* attached to the system. If (@code null}, the number of devices is
1991+
* returned in <tt>puiNumDevices</tt>
1992+
* @param puiNumDevices
1993+
* If <tt>pRawInputDeviceList</tt> is {@code null}, the function populates
1994+
* this variable with the number of devices attached to the system;
1995+
* otherwise, this variable specifies the number of {@link RAWINPUTDEVICELIST}
1996+
* structures that can be contained in the buffer to which <tt>pRawInputDeviceList</tt>
1997+
* points. If this value is less than the number of devices attached to
1998+
* the system, the function returns the actual number of devices in this
1999+
* variable and fails with ERROR_INSUFFICIENT_BUFFER.
2000+
* @param cbSize
2001+
* The size of a {@link RAWINPUTDEVICELIST} structure, in bytes.
2002+
* @return If the function is successful, the return value is the number of devices
2003+
* stored in the buffer pointed to by <tt>pRawInputDeviceList</tt>. On
2004+
* any other error, the function returns -1 and {@code GetLastError}
2005+
* returns the error indication.
2006+
* @see RAWINPUTDEVICELIST#sizeof()
2007+
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms645598(v=vs.85).aspx">GetRawInputDeviceList</A>
2008+
*/
2009+
int GetRawInputDeviceList(RAWINPUTDEVICELIST[] pRawInputDeviceList, IntByReference puiNumDevices, int cbSize);
19862010
}

contrib/platform/src/com/sun/jna/platform/win32/User32Util.java

+29
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
*/
1010

1111
package com.sun.jna.platform.win32;
12+
import java.util.Arrays;
13+
import java.util.List;
14+
1215
import com.sun.jna.WString;
1316
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
1417
import com.sun.jna.platform.win32.WinDef.HMENU;
1518
import com.sun.jna.platform.win32.WinDef.HWND;
1619
import com.sun.jna.platform.win32.WinDef.LPVOID;
20+
import com.sun.jna.platform.win32.WinUser.RAWINPUTDEVICELIST;
21+
import com.sun.jna.ptr.IntByReference;
1722

1823

1924
/**
@@ -48,4 +53,28 @@ public static final void destroyWindow(final HWND hWnd) {
4853
if (!User32.INSTANCE.DestroyWindow(hWnd))
4954
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
5055
}
56+
57+
public static final List<RAWINPUTDEVICELIST> GetRawInputDeviceList() {
58+
IntByReference puiNumDevices = new IntByReference(0);
59+
RAWINPUTDEVICELIST placeholder = new RAWINPUTDEVICELIST();
60+
int cbSize = placeholder.sizeof();
61+
// first call is with NULL so we query the expected number of devices
62+
int returnValue = User32.INSTANCE.GetRawInputDeviceList(null, puiNumDevices, cbSize);
63+
if (returnValue != 0) {
64+
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
65+
}
66+
67+
int deviceCount = puiNumDevices.getValue();
68+
RAWINPUTDEVICELIST[] records = (RAWINPUTDEVICELIST[]) placeholder.toArray(deviceCount);
69+
returnValue = User32.INSTANCE.GetRawInputDeviceList(records, puiNumDevices, cbSize);
70+
if (returnValue == (-1)) {
71+
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
72+
}
73+
74+
if (returnValue != records.length) {
75+
throw new IllegalStateException("Mismatched allocated (" + records.length + ") vs. received devices count (" + returnValue + ")");
76+
}
77+
78+
return Arrays.asList(records);
79+
}
5180
}

contrib/platform/src/com/sun/jna/platform/win32/WinNT.java

+5
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,11 @@ public void setPointer(Pointer p) {
12401240

12411241
super.setPointer(p);
12421242
}
1243+
1244+
@Override
1245+
public String toString() {
1246+
return String.valueOf(getPointer());
1247+
}
12431248
}
12441249

12451250
/**

0 commit comments

Comments
 (0)