Skip to content

Commit 3b1df74

Browse files
committed
[Win32] Correct DPI context awareness value retrieval
The DPI_AWARENESS_CONTEXT values are currently defined as constants, but they are actually defined as typedefed handles instead of compile time constants in Windows' windef.h. So the constants defined in SWT may not fit the actual values as used by the OS. This currently seems to be the case for the System awareness value, which does not fit the OS value. This change adapts the definition of the values to retrieve the actual current values from the OS instead of hardcoding them in constants.
1 parent 2166ca6 commit 3b1df74

File tree

8 files changed

+171
-20
lines changed

8 files changed

+171
-20
lines changed

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2025 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -161,6 +161,26 @@ JNIEXPORT jboolean JNICALL OS_NATIVE(Arc)
161161
}
162162
#endif
163163

164+
#ifndef NO_AreDpiAwarenessContextsEqual
165+
JNIEXPORT jboolean JNICALL OS_NATIVE(AreDpiAwarenessContextsEqual)
166+
(JNIEnv *env, jclass that, jlong arg0, jlong arg1)
167+
{
168+
jboolean rc = 0;
169+
OS_NATIVE_ENTER(env, that, AreDpiAwarenessContextsEqual_FUNC);
170+
/*
171+
rc = (jboolean)AreDpiAwarenessContextsEqual((DPI_AWARENESS_CONTEXT)arg0, (DPI_AWARENESS_CONTEXT)arg1);
172+
*/
173+
{
174+
OS_LOAD_FUNCTION(fp, AreDpiAwarenessContextsEqual)
175+
if (fp) {
176+
rc = (jboolean)((jboolean (CALLING_CONVENTION*)(DPI_AWARENESS_CONTEXT, DPI_AWARENESS_CONTEXT))fp)((DPI_AWARENESS_CONTEXT)arg0, (DPI_AWARENESS_CONTEXT)arg1);
177+
}
178+
}
179+
OS_NATIVE_EXIT(env, that, AreDpiAwarenessContextsEqual_FUNC);
180+
return rc;
181+
}
182+
#endif
183+
164184
#ifndef NO_AssocQueryString
165185
JNIEXPORT jint JNICALL OS_NATIVE(AssocQueryString)
166186
(JNIEnv *env, jclass that, jint arg0, jint arg1, jcharArray arg2, jcharArray arg3, jcharArray arg4, jintArray arg5)

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os_custom.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,103 @@ JNIEXPORT jboolean JNICALL OS_NATIVE(IsDarkModeAvailable)
343343
return isAvailable;
344344
}
345345
#endif
346+
347+
#ifndef NO_DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE
348+
JNIEXPORT jlong JNICALL OS_NATIVE(DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE)
349+
(JNIEnv *env, jclass that)
350+
{
351+
/* Suppress warnings about unreferenced parameters */
352+
(void)env;
353+
(void)that;
354+
355+
jlong rc = 0;
356+
OS_NATIVE_ENTER(env, that, DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_FUNC);
357+
#ifdef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE
358+
rc = (jlong)(LONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE;
359+
#else
360+
rc = 0;
361+
#endif
362+
OS_NATIVE_EXIT(env, that, DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_FUNC);
363+
return rc;
364+
}
365+
#endif
366+
367+
#ifndef NO_DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_1V2
368+
JNIEXPORT jlong JNICALL OS_NATIVE(DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_1V2)
369+
(JNIEnv *env, jclass that)
370+
{
371+
/* Suppress warnings about unreferenced parameters */
372+
(void)env;
373+
(void)that;
374+
375+
jlong rc = 0;
376+
OS_NATIVE_ENTER(env, that, DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_1V2_FUNC);
377+
#ifdef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
378+
rc = (jlong)(LONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2;
379+
#else
380+
rc = 0;
381+
#endif
382+
OS_NATIVE_EXIT(env, that, DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_1V2_FUNC);
383+
return rc;
384+
}
385+
#endif
386+
387+
#ifndef NO_DPI_1AWARENESS_1CONTEXT_1SYSTEM_1AWARE
388+
JNIEXPORT jlong JNICALL OS_NATIVE(DPI_1AWARENESS_1CONTEXT_1SYSTEM_1AWARE)
389+
(JNIEnv *env, jclass that)
390+
{
391+
/* Suppress warnings about unreferenced parameters */
392+
(void)env;
393+
(void)that;
394+
395+
jlong rc = 0;
396+
OS_NATIVE_ENTER(env, that, DPI_1AWARENESS_1CONTEXT_1SYSTEM_1AWARE_FUNC);
397+
#ifdef DPI_AWARENESS_CONTEXT_SYSTEM_AWARE
398+
rc = (jlong)(LONG_PTR)DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
399+
#else
400+
rc = 0;
401+
#endif
402+
OS_NATIVE_EXIT(env, that, DPI_1AWARENESS_1CONTEXT_1SYSTEM_1AWARE_FUNC);
403+
return rc;
404+
}
405+
#endif
406+
407+
#ifndef NO_DPI_1AWARENESS_1CONTEXT_1UNAWARE
408+
JNIEXPORT jlong JNICALL OS_NATIVE(DPI_1AWARENESS_1CONTEXT_1UNAWARE)
409+
(JNIEnv *env, jclass that)
410+
{
411+
/* Suppress warnings about unreferenced parameters */
412+
(void)env;
413+
(void)that;
414+
415+
jlong rc = 0;
416+
OS_NATIVE_ENTER(env, that, DPI_1AWARENESS_1CONTEXT_1UNAWARE_FUNC);
417+
#ifdef DPI_AWARENESS_CONTEXT_UNAWARE
418+
rc = (jlong)(LONG_PTR)DPI_AWARENESS_CONTEXT_UNAWARE;
419+
#else
420+
rc = 0;
421+
#endif
422+
OS_NATIVE_EXIT(env, that, DPI_1AWARENESS_1CONTEXT_1UNAWARE_FUNC);
423+
return rc;
424+
}
425+
#endif
426+
427+
#ifndef NO_DPI_1AWARENESS_1CONTEXT_1UNAWARE_1GDISCALED
428+
JNIEXPORT jlong JNICALL OS_NATIVE(DPI_1AWARENESS_1CONTEXT_1UNAWARE_1GDISCALED)
429+
(JNIEnv *env, jclass that)
430+
{
431+
/* Suppress warnings about unreferenced parameters */
432+
(void)env;
433+
(void)that;
434+
435+
jlong rc = 0;
436+
OS_NATIVE_ENTER(env, that, DPI_1AWARENESS_1CONTEXT_1UNAWARE_1GDISCALED_FUNC);
437+
#ifdef DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED
438+
rc = (jlong)(LONG_PTR)DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED;
439+
#else
440+
rc = 0;
441+
#endif
442+
OS_NATIVE_EXIT(env, that, DPI_1AWARENESS_1CONTEXT_1UNAWARE_1GDISCALED_FUNC);
443+
return rc;
444+
}
445+
#endif

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os_custom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
#define OpenThemeDataForDpi_LIB "uxtheme.dll"
2525
#define GetThreadDpiAwarenessContext_LIB "user32.dll"
2626
#define SetThreadDpiAwarenessContext_LIB "user32.dll"
27+
#define AreDpiAwarenessContextsEqual_LIB "user32.dll"

bundles/org.eclipse.swt/Eclipse SWT PI/win32/library/os_stats.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2025 IBM Corporation and others.
2+
* Copyright (c) 2000, 2026 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -31,6 +31,7 @@ typedef enum {
3131
AllowSetForegroundWindow_FUNC,
3232
AlphaBlend_FUNC,
3333
Arc_FUNC,
34+
AreDpiAwarenessContextsEqual_FUNC,
3435
AssocQueryString_FUNC,
3536
BITMAPINFOHEADER_1sizeof_FUNC,
3637
BITMAP_1sizeof_FUNC,
@@ -100,6 +101,11 @@ typedef enum {
100101
DIBSECTION_1sizeof_FUNC,
101102
DOCHOSTUIINFO_1sizeof_FUNC,
102103
DOCINFO_1sizeof_FUNC,
104+
DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_FUNC,
105+
DPI_1AWARENESS_1CONTEXT_1PER_1MONITOR_1AWARE_1V2_FUNC,
106+
DPI_1AWARENESS_1CONTEXT_1SYSTEM_1AWARE_FUNC,
107+
DPI_1AWARENESS_1CONTEXT_1UNAWARE_FUNC,
108+
DPI_1AWARENESS_1CONTEXT_1UNAWARE_1GDISCALED_FUNC,
103109
DRAWITEMSTRUCT_1sizeof_FUNC,
104110
DROPFILES_1sizeof_FUNC,
105111
DefFrameProc_FUNC,

bundles/org.eclipse.swt/Eclipse SWT PI/win32/org/eclipse/swt/internal/win32/OS.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ public class OS extends C {
354354
public static final short DMDUP_SIMPLEX = 1;
355355
public static final short DMDUP_VERTICAL = 2;
356356
public static final short DMDUP_HORIZONTAL = 3;
357-
public static final int DPI_AWARENESS_CONTEXT_UNAWARE = 24592;
358-
public static final int DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = 1073766416;
359-
public static final int DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = 24593;
360-
public static final int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = 18;
361-
public static final int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = 34;
357+
public static final long DPI_AWARENESS_CONTEXT_UNAWARE = DPI_AWARENESS_CONTEXT_UNAWARE();
358+
public static final long DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED();
359+
public static final long DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = DPI_AWARENESS_CONTEXT_SYSTEM_AWARE();
360+
public static final long DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE();
361+
public static final long DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2();
362362
public static final int DSTINVERT = 0x550009;
363363
public static final int DT_BOTTOM = 0x8;
364364
public static final int DT_CALCRECT = 0x400;
@@ -2349,6 +2349,12 @@ public static int HRESULT_FROM_WIN32(int x) {
23492349
public static final native boolean AlphaBlend(long hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, long hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, BLENDFUNCTION blendFunction);
23502350
/** @param hdc cast=(HDC) */
23512351
public static final native boolean Arc (long hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nXStartArc, int nYStartArc, int nXEndArc, int nYEndArc);
2352+
/**
2353+
* @method flags=dynamic
2354+
* @param dpiContextA cast=(DPI_AWARENESS_CONTEXT)
2355+
* @param dpiContextB cast=(DPI_AWARENESS_CONTEXT)
2356+
*/
2357+
public static final native boolean AreDpiAwarenessContextsEqual (long dpiContextA, long dpiContextB);
23522358
/**
23532359
* @param pszAssoc flags=no_out
23542360
* @param pszExtra flags=no_out
@@ -2573,6 +2579,16 @@ public static int HRESULT_FROM_WIN32(int x) {
25732579
* @param pDevModeInput cast=(PDEVMODEW)
25742580
*/
25752581
public static final native int DocumentProperties (long hWnd, long hPrinter, char[] pDeviceName, long pDevModeOutput, long pDevModeInput, int fMode);
2582+
/** @method flags=no_gen */
2583+
public static final native long DPI_AWARENESS_CONTEXT_UNAWARE();
2584+
/** @method flags=no_gen */
2585+
public static final native long DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED();
2586+
/** @method flags=no_gen */
2587+
public static final native long DPI_AWARENESS_CONTEXT_SYSTEM_AWARE();
2588+
/** @method flags=no_gen */
2589+
public static final native long DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE();
2590+
/** @method flags=no_gen */
2591+
public static final native long DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2();
25762592
/**
25772593
* @param hwnd cast=(HWND)
25782594
* @param pt flags=struct

bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/widgets/DisplayWin32Test.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package org.eclipse.swt.widgets;
22

33
import static org.eclipse.swt.internal.DPIUtil.setMonitorSpecificScaling;
4-
import static org.junit.jupiter.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
56

67
import org.eclipse.swt.internal.*;
78
import org.eclipse.swt.internal.win32.*;
@@ -17,7 +18,14 @@ public void monitorSpecificScaling_activate() {
1718
setMonitorSpecificScaling(true);
1819
Display display = Display.getDefault();
1920
assertTrue(display.isRescalingAtRuntime());
20-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
21+
22+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
23+
}
24+
25+
private static void assertExpectedDpiAwareness(long expected) {
26+
long currentDpiAwareness = OS.GetThreadDpiAwarenessContext();
27+
boolean hasExpectedDpiAwareness = OS.AreDpiAwarenessContextsEqual(expected,currentDpiAwareness);
28+
assertTrue(hasExpectedDpiAwareness, "unexpected DPI awareness: " + currentDpiAwareness);
2129
}
2230

2331
@Test
@@ -34,7 +42,7 @@ public void monitorSpecificScaling_withCustomDpiAwareness() {
3442
setMonitorSpecificScaling(true);
3543
Display display = Display.getDefault();
3644
assertTrue(display.isRescalingAtRuntime());
37-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
45+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
3846
} finally {
3947
System.clearProperty(Win32DPIUtils.CUSTOM_DPI_AWARENESS_PROPERTY);
4048
}
@@ -45,7 +53,7 @@ public void customDpiAwareness_System() {
4553
System.setProperty(Win32DPIUtils.CUSTOM_DPI_AWARENESS_PROPERTY, "System");
4654
try {
4755
Display.getDefault();
48-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
56+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
4957
} finally {
5058
System.clearProperty(Win32DPIUtils.CUSTOM_DPI_AWARENESS_PROPERTY);
5159
}
@@ -56,7 +64,7 @@ public void customDpiAwareness_PerMonitorV2() {
5664
System.setProperty(Win32DPIUtils.CUSTOM_DPI_AWARENESS_PROPERTY, "PerMonitorV2");
5765
try {
5866
Display.getDefault();
59-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
67+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
6068
} finally {
6169
System.clearProperty(Win32DPIUtils.CUSTOM_DPI_AWARENESS_PROPERTY);
6270
}
@@ -68,7 +76,7 @@ public void setRescaleAtRuntime_activate() {
6876
Display display = Display.getDefault();
6977
display.setRescalingAtRuntime(true);
7078
assertTrue(display.isRescalingAtRuntime());
71-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
79+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
7280
}
7381

7482
@Test
@@ -77,7 +85,7 @@ public void setRescaleAtRuntime_deactivate() {
7785
Display display = Display.getDefault();
7886
display.setRescalingAtRuntime(false);
7987
assertFalse(display.isRescalingAtRuntime());
80-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
88+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
8189
}
8290

8391
@Test
@@ -86,13 +94,13 @@ public void setRescaleAtRuntime_toggling() {
8694
Display display = Display.getDefault();
8795
display.setRescalingAtRuntime(false);
8896
assertFalse(display.isRescalingAtRuntime());
89-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
97+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
9098
display.setRescalingAtRuntime(true);
9199
assertTrue(display.isRescalingAtRuntime());
92-
assertEquals(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, OS.GetThreadDpiAwarenessContext());
100+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
93101
display.setRescalingAtRuntime(false);
94102
assertFalse(display.isRescalingAtRuntime());
95-
assertEquals(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE, OS.GetThreadDpiAwarenessContext());
103+
assertExpectedDpiAwareness(OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
96104
}
97105

98106
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Win32DPIUtils {
4242
*/
4343
public static final String CUSTOM_DPI_AWARENESS_PROPERTY = "org.eclipse.swt.internal.win32.dpiAwareness";
4444

45-
private static int customDpiAwareness = -1;
45+
private static long customDpiAwareness = -1;
4646

4747
public static void initializeCustomDpiAwareness() {
4848
String customDpiAwareness = System.getProperty(CUSTOM_DPI_AWARENESS_PROPERTY);
@@ -63,7 +63,7 @@ public static void initializeCustomDpiAwareness() {
6363
}
6464
}
6565

66-
public static boolean setDPIAwareness(int desiredDpiAwareness) {
66+
public static boolean setDPIAwareness(long desiredDpiAwareness) {
6767
if (desiredDpiAwareness == OS.GetThreadDpiAwarenessContext()) {
6868
return true;
6969
}

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5408,7 +5408,7 @@ public boolean setRescalingAtRuntime(boolean activate) {
54085408
}
54095409

54105410
private boolean setMonitorSpecificScaling(boolean activate) {
5411-
int desiredApiAwareness = activate ? OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 : OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
5411+
long desiredApiAwareness = activate ? OS.DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 : OS.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
54125412
if (Win32DPIUtils.setDPIAwareness(desiredApiAwareness)) {
54135413
rescalingAtRuntime = activate;
54145414
coordinateSystemMapper = activate ? new MultiZoomCoordinateSystemMapper(this, this::getMonitors) : new SingleZoomCoordinateSystemMapper(this);

0 commit comments

Comments
 (0)