adding code to get windows dpi

This commit is contained in:
Ben Fry
2022-02-13 06:53:05 -05:00
parent cebdb8f3d7
commit 46cbe7c54c
5 changed files with 146 additions and 0 deletions
+33
View File
@@ -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();
}
+26
View File
@@ -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
+26
View File
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <windows.h>
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;
}
@@ -0,0 +1,32 @@
#include "processing_core_platform_Fenster.h"
// only for the std::cout debugging stuff / remove later
//#include <ostream>
//#include <iostream>
#include <windows.h>
// 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;
}
@@ -0,0 +1,29 @@
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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