Skip to content

Windows screen DPI autodetect #5556

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

Merged
merged 6 commits into from
Nov 21, 2016
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
2 changes: 1 addition & 1 deletion app/src/processing/app/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static public int getScale() {
return scale;
} catch (NumberFormatException ignore) {
}
return 100;
return BaseNoGui.getPlatform().getSystemDPI() * 100 / 96;
}

static public int scale(int size) {
Expand Down
5 changes: 5 additions & 0 deletions arduino-core/src/processing/app/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,9 @@ public void chmod(File file, int mode) throws IOException, InterruptedException
public void fixSettingsLocation() throws Exception {
//noop
}

public int getSystemDPI() {
return 96;
}

}
54 changes: 54 additions & 0 deletions arduino-core/src/processing/app/windows/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import java.util.LinkedList;
import java.util.List;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public class Platform extends processing.app.Platform {

Expand Down Expand Up @@ -240,4 +244,54 @@ public void fixSettingsLocation() throws Exception {

Files.move(oldSettingsFolder, settingsFolder.toPath());
}

// Need to extend com.sun.jna.platform.win32.User32 to access
// Win32 function GetDpiForSystem()
interface ExtUser32 extends StdCallLibrary, com.sun.jna.platform.win32.User32 {
ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary("user32", ExtUser32.class, W32APIOptions.DEFAULT_OPTIONS);

public int GetDpiForSystem();

public int SetProcessDpiAwareness(int value);

public final int DPI_AWARENESS_INVALID = -1;
public final int DPI_AWARENESS_UNAWARE = 0;
public final int DPI_AWARENESS_SYSTEM_AWARE = 1;
public final int DPI_AWARENESS_PER_MONITOR_AWARE = 2;

public Pointer SetThreadDpiAwarenessContext(Pointer dpiContext);

public final Pointer DPI_AWARENESS_CONTEXT_UNAWARE = new Pointer(-1);
public final Pointer DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new Pointer(-2);
public final Pointer DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = new Pointer(-3);
}

private static int detected = detectSystemDPI();

@Override
public int getSystemDPI() {
if (detected == -1)
return super.getSystemDPI();
return detected;
}

public static int detectSystemDPI() {
try {
ExtUser32.INSTANCE.SetProcessDpiAwareness(ExtUser32.DPI_AWARENESS_SYSTEM_AWARE);
} catch (Throwable e) {
// Ignore error
}
try {
ExtUser32.INSTANCE.SetThreadDpiAwarenessContext(ExtUser32.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
} catch (Throwable e) {
// Ignore error (call valid only on Windows 10)
}
try {
return ExtUser32.INSTANCE.GetDpiForSystem();
} catch (Throwable e) {
// DPI detection failed, fall back with default
System.out.println("DPI detection failed, fallback to 96 dpi");
return -1;
}
}
}
10 changes: 5 additions & 5 deletions build/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -926,17 +926,17 @@

<target name="download-launch4j-windows">
<antcall target="unzip-with-ant-task">
<param name="archive_file" value="windows/launch4j-3.8-win32.zip"/>
<param name="archive_url" value="http://downloads.sourceforge.net/project/launch4j/launch4j-3/3.8/launch4j-3.8-win32.zip"/>
<param name="archive_file" value="windows/launch4j-3.9-win32.zip"/>
<param name="archive_url" value="http://downloads.sourceforge.net/project/launch4j/launch4j-3/3.9/launch4j-3.9-win32.zip"/>
<param name="final_folder" value="windows/launcher/launch4j"/>
<param name="dest_folder" value="windows/launcher/"/>
</antcall>
</target>

<target name="download-launch4j-linux">
<antcall target="untar">
<param name="archive_file" value="windows/launch4j-3.8-linux.tgz"/>
<param name="archive_url" value="http://downloads.sourceforge.net/project/launch4j/launch4j-3/3.8/launch4j-3.8-linux.tgz"/>
<param name="archive_file" value="windows/launch4j-3.9-linux.tgz"/>
<param name="archive_url" value="http://downloads.sourceforge.net/project/launch4j/launch4j-3/3.9/launch4j-3.9-linux.tgz"/>
<param name="final_folder" value="windows/launcher/launch4j"/>
<param name="dest_folder" value="windows/launcher/"/>
</antcall>
Expand Down Expand Up @@ -1026,7 +1026,7 @@

<copy todir="windows/work">
<fileset dir="windows/launcher"
includes="application.ico, config.xml, config_debug.xml, arduino.l4j.ini"/>
includes="application.ico, config.xml, config_debug.xml, arduino.l4j.ini, wrapper-manifest.xml"/>
</copy>
<launch4j configFile="windows/work/config.xml" fileVersion="${revision}.0" txtFileVersion="${revision}" productVersion="${revision}.0" txtProductVersion="${revision}"/>
<launch4j configFile="windows/work/config_debug.xml" fileVersion="${revision}.0" txtFileVersion="${revision}" productVersion="${revision}.0" txtProductVersion="${revision}"/>
Expand Down
1 change: 0 additions & 1 deletion build/windows/launch4j-3.8-linux.tgz.sha

This file was deleted.

1 change: 0 additions & 1 deletion build/windows/launch4j-3.8-win32.zip.sha

This file was deleted.

1 change: 1 addition & 0 deletions build/windows/launch4j-3.9-linux.tgz.sha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9d4c377af0149389da9ad3c3f1394fc5a655f540
1 change: 1 addition & 0 deletions build/windows/launch4j-3.9-win32.zip.sha
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
da6645d030137672647f576c8358427bc663282e
1 change: 1 addition & 0 deletions build/windows/launcher/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@
<launcherErr>The registry refers to a nonexistent Java Development Kit installation or the runtime is corrupted.</launcherErr>
<instanceAlreadyExistsMsg>An application instance is already running.</instanceAlreadyExistsMsg>
</messages>
<manifest>wrapper-manifest.xml</manifest>
</launch4jConfig>
1 change: 1 addition & 0 deletions build/windows/launcher/config_debug.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@
<launcherErr>The registry refers to a nonexistent Java Development Kit installation or the runtime is corrupted.</launcherErr>
<instanceAlreadyExistsMsg>An application instance is already running.</instanceAlreadyExistsMsg>
</messages>
<manifest>wrapper-manifest.xml</manifest>
</launch4jConfig>
9 changes: 9 additions & 0 deletions build/windows/launcher/wrapper-manifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">

<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>

</assembly>