From 46cbe7c54c445176f02303a319fd7ea481604956 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Sun, 13 Feb 2022 06:53:05 -0500 Subject: [PATCH] adding code to get windows dpi --- build/windows/fenster/Fenster.java | 33 +++++++++++++++++++ build/windows/fenster/Makefile | 26 +++++++++++++++ build/windows/fenster/fenster.cpp | 26 +++++++++++++++ .../processing_core_platform_Fenster.cpp | 32 ++++++++++++++++++ .../processing_core_platform_Fenster.h | 29 ++++++++++++++++ 5 files changed, 146 insertions(+) create mode 100644 build/windows/fenster/Fenster.java create mode 100644 build/windows/fenster/Makefile create mode 100644 build/windows/fenster/fenster.cpp create mode 100644 build/windows/fenster/processing_core_platform_Fenster.cpp create mode 100644 build/windows/fenster/processing_core_platform_Fenster.h diff --git a/build/windows/fenster/Fenster.java b/build/windows/fenster/Fenster.java new file mode 100644 index 000000000..c7873129e --- /dev/null +++ b/build/windows/fenster/Fenster.java @@ -0,0 +1,33 @@ +package processing.core.platform; + +public class Fenster { + static void load() { + System.out.println("about to load"); + System.loadLibrary("fenster"); + System.out.println("loaded"); + } + + static void info() { + System.out.println("about to init"); + Fenster f = new Fenster(); + // f.sayHello(); + System.out.println("about to call"); + int ppi = f.getLogPixels(); + System.out.println("getLogPixels = " + ppi); + System.out.println("aka " + (ppi / 96f)); + System.out.println("done"); + } + + public static void main(String[] args) { + new Thread(() -> { + // java.awt.EventQueue.invokeLater(() -> { + load(); + info(); + // }); + }).start(); + } + + private native void sayHello(); + + private native int getLogPixels(); +} diff --git a/build/windows/fenster/Makefile b/build/windows/fenster/Makefile new file mode 100644 index 000000000..94d2691d9 --- /dev/null +++ b/build/windows/fenster/Makefile @@ -0,0 +1,26 @@ +JAVA_HOME=/c/Program\ Files/Eclipse\ Adoptium/jdk-17.0.1.12-hotspot + +headers: + ${JAVA_HOME}/bin/javac -h . -d . Fenster.java + #rm Fenster.class + +default: + #ls ${JAVA_HOME} + g++ -c \ + -I${JAVA_HOME}/include \ + -I${JAVA_HOME}/include/win32 \ + processing_core_platform_Fenster.cpp \ + -o processing_core_platform_Fenster.o + #-mwindows -lmingw64 -lgdi64 \ + g++ -shared -o fenster.dll \ + processing_core_platform_Fenster.o \ + -lgdi32 \ + -Wl,--add-stdcall-alias + +run: + #${JAVA_HOME}/bin/javac -d . Fenster.java + ${JAVA_HOME}/bin/java -cp . processing.core.platform.Fenster +# java -cp . -Djava\.library\.path=/NATIVE_SHARED_LIB_FOLDER processing.core.platform.Fenster + +fest: + g++ fester.cpp -o fenster -lgdi32 diff --git a/build/windows/fenster/fenster.cpp b/build/windows/fenster/fenster.cpp new file mode 100644 index 000000000..bc5f35212 --- /dev/null +++ b/build/windows/fenster/fenster.cpp @@ -0,0 +1,26 @@ +#include +#include + + +int main() { + // MinGW could not find this variant. There's probably a #define or + // another arg to make it work, but the Vista-era version is working fine. + //SetProcessDpiAwareness(PROCESS_SYSTEM_DPI_AWARE); // Windows 8.1 and later + + // https://docs.microsoft.com/en-us/windows/win32/hidpi/setting-the-default-dpi-awareness-for-a-process + SetProcessDPIAware(); // Windows Vista and later + + // https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings + // also done in the JDK https://hg.openjdk.java.net/jdk/jdk/rev/b7a958df3992 + HDC hdc = GetDC(NULL); + if (hdc) { + INT horizontalDPI = GetDeviceCaps(hdc, LOGPIXELSX); + INT verticalDPI = GetDeviceCaps(hdc, LOGPIXELSY); + //printf("%d %d", horizontalDPI, verticalDPI); + // god help us if horizontal != vertical + printf("%d", horizontalDPI); + ReleaseDC(NULL, hdc); + return 0; + } + return 1; +} diff --git a/build/windows/fenster/processing_core_platform_Fenster.cpp b/build/windows/fenster/processing_core_platform_Fenster.cpp new file mode 100644 index 000000000..e332c4853 --- /dev/null +++ b/build/windows/fenster/processing_core_platform_Fenster.cpp @@ -0,0 +1,32 @@ +#include "processing_core_platform_Fenster.h" + +// only for the std::cout debugging stuff / remove later +//#include +//#include + +#include + +// had to manually modify JDK_HOME/include/win32/jni_md.h to change +// typedef __int64 jlong -> __int64_t +// was also necessary to modify the permissions of the file in Windows + + +JNIEXPORT void JNICALL Java_processing_core_platform_Fenster_sayHello + (JNIEnv *, jobject) { + //std::cout << "Well at least this part is working" << std::endl; + } + +JNIEXPORT jint JNICALL Java_processing_core_platform_Fenster_getLogPixels + (JNIEnv *, jobject) { + // https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings + // also done in the JDK https://hg.openjdk.java.net/jdk/jdk/rev/b7a958df3992 + HDC hdc = GetDC(NULL); + if (hdc) { + INT horizontalDPI = GetDeviceCaps(hdc, LOGPIXELSX); + ReleaseDC(NULL, hdc); + // INT verticalDPI = GetDeviceCaps(desktopDc, LOGPIXELSY); + // god help us if horizontal != vertical + return (jint) horizontalDPI; + } + return 0; + } diff --git a/build/windows/fenster/processing_core_platform_Fenster.h b/build/windows/fenster/processing_core_platform_Fenster.h new file mode 100644 index 000000000..dcbbdafd4 --- /dev/null +++ b/build/windows/fenster/processing_core_platform_Fenster.h @@ -0,0 +1,29 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class processing_core_platform_Fenster */ + +#ifndef _Included_processing_core_platform_Fenster +#define _Included_processing_core_platform_Fenster +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: processing_core_platform_Fenster + * Method: sayHello + * Signature: ()V + */ +JNIEXPORT void JNICALL Java_processing_core_platform_Fenster_sayHello + (JNIEnv *, jobject); + +/* + * Class: processing_core_platform_Fenster + * Method: getLogPixels + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_processing_core_platform_Fenster_getLogPixels + (JNIEnv *, jobject); + +#ifdef __cplusplus +} +#endif +#endif