Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added GetRawInputDeviceList definition and utility to User32 interface #488

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Features
* [#481] (https://github.com/twall/jna/pull/481): Added volume management functions to 'com.sun.jna.platform.win32' - [@lgoldstein](https://github.com/lgoldstein).
* [#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).
* [#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).
* [#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).

Bug Fixes
---------
Expand Down
24 changes: 24 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/User32.java
Original file line number Diff line number Diff line change
Expand Up @@ -1983,4 +1983,28 @@ long SendMessageTimeout(HWND hWnd, int msg, long wParam, long lParam,
* error information, call {@link Kernel32#GetLastError()}.
*/
long GetClassLongPtr(HWND hWnd, int nIndex);

/**
* @param pRawInputDeviceList
* An array of {@link RAWINPUTDEVICELIST} structures for the devices
* attached to the system. If (@code null}, the number of devices is
* returned in <tt>puiNumDevices</tt>
* @param puiNumDevices
* If <tt>pRawInputDeviceList</tt> is {@code null}, the function populates
* this variable with the number of devices attached to the system;
* otherwise, this variable specifies the number of {@link RAWINPUTDEVICELIST}
* structures that can be contained in the buffer to which <tt>pRawInputDeviceList</tt>
* points. If this value is less than the number of devices attached to
* the system, the function returns the actual number of devices in this
* variable and fails with ERROR_INSUFFICIENT_BUFFER.
* @param cbSize
* The size of a {@link RAWINPUTDEVICELIST} structure, in bytes.
* @return If the function is successful, the return value is the number of devices
* stored in the buffer pointed to by <tt>pRawInputDeviceList</tt>. On
* any other error, the function returns -1 and {@code GetLastError}
* returns the error indication.
* @see RAWINPUTDEVICELIST#sizeof()
* @see <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms645598(v=vs.85).aspx">GetRawInputDeviceList</A>
*/
int GetRawInputDeviceList(RAWINPUTDEVICELIST[] pRawInputDeviceList, IntByReference puiNumDevices, int cbSize);
}
29 changes: 29 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/User32Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
*/

package com.sun.jna.platform.win32;
import java.util.Arrays;
import java.util.List;

import com.sun.jna.WString;
import com.sun.jna.platform.win32.WinDef.HINSTANCE;
import com.sun.jna.platform.win32.WinDef.HMENU;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPVOID;
import com.sun.jna.platform.win32.WinUser.RAWINPUTDEVICELIST;
import com.sun.jna.ptr.IntByReference;


/**
Expand Down Expand Up @@ -48,4 +53,28 @@ public static final void destroyWindow(final HWND hWnd) {
if (!User32.INSTANCE.DestroyWindow(hWnd))
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

public static final List<RAWINPUTDEVICELIST> GetRawInputDeviceList() {
IntByReference puiNumDevices = new IntByReference(0);
RAWINPUTDEVICELIST placeholder = new RAWINPUTDEVICELIST();
int cbSize = placeholder.sizeof();
// first call is with NULL so we query the expected number of devices
int returnValue = User32.INSTANCE.GetRawInputDeviceList(null, puiNumDevices, cbSize);
if (returnValue != 0) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

int deviceCount = puiNumDevices.getValue();
RAWINPUTDEVICELIST[] records = (RAWINPUTDEVICELIST[]) placeholder.toArray(deviceCount);
returnValue = User32.INSTANCE.GetRawInputDeviceList(records, puiNumDevices, cbSize);
if (returnValue == (-1)) {
throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
}

if (returnValue != records.length) {
throw new IllegalStateException("Mismatched allocated (" + records.length + ") vs. received devices count (" + returnValue + ")");
}

return Arrays.asList(records);
}
}
5 changes: 5 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/win32/WinNT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,11 @@ public void setPointer(Pointer p) {

super.setPointer(p);
}

@Override
public String toString() {
return String.valueOf(getPointer());
}
}

/**
Expand Down
Loading