Added windows JNA util classes

This commit is contained in:
nathan 2017-05-10 15:20:29 +02:00
parent fe9c43c02b
commit 8a529731b7
5 changed files with 285 additions and 0 deletions

View File

@ -0,0 +1,44 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.jna;
import java.util.HashMap;
import java.util.Map;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
/**
* Helper method to get the library info from JNA when registering via direct map
*/
public
class JnaHelper {
@SuppressWarnings("unchecked")
public static
NativeLibrary register(final String libraryName, final Class<?> clazz) throws IllegalArgumentException {
final Map<String, Object> options = new HashMap<String, Object>();
options.put(Library.OPTION_CLASSLOADER, clazz.getClassLoader());
final NativeLibrary library = NativeLibrary.getInstance(libraryName, options);
if (library == null) {
throw new IllegalArgumentException(libraryName + " doesn't exist or cannot be loaded.");
}
Native.register(clazz, library);
return library;
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.jna.windows;
import com.sun.jna.Pointer;
import dorkbox.util.jna.JnaHelper;
/**
* bindings for GDI32
*
* Direct-mapping, See: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md
*/
public
class Gdi32 {
static {
JnaHelper.register("gdi32", Gdi32.class);
}
/**
* Number of pixels per logical inch along the screen width. In a system with multiple display monitors, this value is the same for
* all monitors.
*/
public static final int LOGPIXELSX = 88;
/**
* The GetDeviceCaps function retrieves device-specific information for the specified device.
*
* https://msdn.microsoft.com/en-us/library/dd144877(v=vs.85).aspx
*
* @param handle A handle to the DC.
* @param nIndex The item to be returned.
*/
public static native int GetDeviceCaps(Pointer handle, int nIndex);
}

View File

@ -0,0 +1,64 @@
/*
* Copyright 2016 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.jna.windows;
import java.util.Arrays;
import java.util.List;
import com.sun.jna.Structure;
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd162805(v=vs.85).aspx
*/
public
class POINT extends Structure {
public long x;
public long y;
public
POINT() {
}
public
POINT(final long x, final long y) {
this.x = x;
this.y = y;
}
public
POINT.ByValue asValue() {
POINT.ByValue copy = new POINT.ByValue();
copy.x = this.x;
copy.y = this.y;
return copy;
}
@Override
protected
List<String> getFieldOrder() {
return Arrays.asList("x", "y");
}
@Override
public
String toString() {
return x + ":" + y;
}
static public
class ByValue extends POINT implements Structure.ByValue {}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright 2017 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.jna.windows;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import dorkbox.util.jna.JnaHelper;
/**
* bindings for ShCore.dll
*
* Minimum supported client
* Windows 8.1 [desktop apps only]
*
* Minimum supported server
* Windows Server 2012 R2 [desktop apps only]
*
* <p>
* Direct-mapping, See: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md
*/
public
class ShCore {
static {
JnaHelper.register("shcore", ShCore.class);
}
/**
* https://msdn.microsoft.com/de-de/library/windows/desktop/dn280510(v=vs.85).aspx
*
* @param hmonitor A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire
* screen.
*
* @param dpiType
* MDT_EFFECTIVE_DPI = 0,
* The effective DPI. This value should be used when determining the correct scale factor for scaling UI elements. This
* incorporates the scale factor set by the user for this specific display.
* MDT_ANGULAR_DPI = 1,
* The angular DPI. This DPI ensures rendering at a compliant angular resolution on the screen. This does not include the
* scale factor set by the user for this specific display.
* MDT_RAW_DPI = 2,
* The raw DPI. This value is the linear DPI of the screen as measured on the screen itself. Use this value when you want
* to read the pixel density and not the recommended scaling setting. This does not include the scale factor set by the user
* for this specific display and is not guaranteed to be a supported DPI value.
* MDT_DEFAULT = MDT_EFFECTIVE_DPI
*
*
* @return if the function succeeds, the return value is a handle to the DC for the specified window's client area. If the function
* fails, the return value is NULL.
*/
public static native
Pointer GetDpiForMonitor(Pointer hmonitor, int dpiType, IntByReference dpiX, IntByReference dpiY);
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2015 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dorkbox.util.jna.windows;
import com.sun.jna.Pointer;
import dorkbox.util.jna.JnaHelper;
/**
* bindings for User32
* <p>
* Direct-mapping, See: https://github.com/java-native-access/jna/blob/master/www/DirectMapping.md
*/
public
class User32 {
static {
JnaHelper.register("user32", User32.class);
}
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd144871(v=vs.85).aspx
*
* @param shouldBeNull A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire
* screen.
*
* @return if the function succeeds, the return value is a handle to the DC for the specified window's client area. If the function
* fails, the return value is NULL.
*/
public static native
Pointer GetDC(Pointer shouldBeNull);
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd162920(v=vs.85).aspx
*/
public static native
void ReleaseDC(Pointer shouldBeNull, Pointer dcHandle);
/**
* https://msdn.microsoft.com/en-us/library/windows/desktop/dd145062(v=vs.85).aspx
*/
public static native
Pointer MonitorFromPoint(POINT.ByValue pt, int shouldBeOne);
}