From 96d3482475d7a750f09f4ffb40dd496465f05c57 Mon Sep 17 00:00:00 2001 From: Doug Coleman Date: Mon, 9 Mar 2020 19:39:32 -0500 Subject: [PATCH] windows: Fix user32/shcore DPI functions. --- basis/ui/backend/windows/windows.factor | 12 +++++ basis/windows/gdi32/gdi32.factor | 2 +- basis/windows/messages/messages.factor | 5 +- basis/windows/shcore/shcore.factor | 61 ++++++++++++++++++++++ basis/windows/user32/user32.factor | 45 +++++++++------- extra/windows/fullscreen/fullscreen.factor | 14 +++-- 6 files changed, 112 insertions(+), 27 deletions(-) diff --git a/basis/ui/backend/windows/windows.factor b/basis/ui/backend/windows/windows.factor index 8ce6463dab..0982ae3770 100644 --- a/basis/ui/backend/windows/windows.factor +++ b/basis/ui/backend/windows/windows.factor @@ -533,6 +533,9 @@ SYMBOL: wm-handlers wm-handlers [ H{ ${ WM_CLOSE [ handle-wm-close 0 ] } + ! ${ WM_NCCREATE [ [ 3drop EnableNonClientDpiScaling drop ] [ DefWindowProc ] 4bi ] } + ! ${ WM_GETDPISCALEDSIZE [ DefWindowProc ] } + ! ${ WM_DPICHANGED [ DefWindowProc ] } ${ WM_PAINT [ 4dup handle-wm-paint DefWindowProc ] } ${ WM_SIZE [ handle-wm-size 0 ] } @@ -603,6 +606,7 @@ M: windows-ui-backend do-events ] [ drop ] if ; : adjust-RECT ( RECT style ex-style -- ) + ! [ 0 ] dip GetDpiForSystem AdjustWindowRectExForDpi win32-error=0/f ; [ 0 ] dip AdjustWindowRectEx win32-error=0/f ; : make-RECT ( world -- RECT ) @@ -630,6 +634,14 @@ M: windows-ui-backend do-events dup ] change-global ; +: get-device-caps ( handle -- x y ) + GetDC + [ LOGPIXELSX GetDeviceCaps ] + [ LOGPIXELSY GetDeviceCaps ] bi ; + +: get-default-device-caps ( -- x y ) + f get-device-caps ; + :: create-window ( rect style ex-style -- hwnd ) rect style ex-style make-adjusted-RECT [ get-window-class f ] dip diff --git a/basis/windows/gdi32/gdi32.factor b/basis/windows/gdi32/gdi32.factor index 20fb2090b1..a39b4f427b 100644 --- a/basis/windows/gdi32/gdi32.factor +++ b/basis/windows/gdi32/gdi32.factor @@ -1640,7 +1640,7 @@ FUNCTION: BOOL GdiFlush ( ) ! FUNCTION: GetDCBrushColor ! FUNCTION: GetDCOrgEx ! FUNCTION: GetDCPenColor -! FUNCTION: GetDeviceCaps +FUNCTION: int GetDeviceCaps ( HDC hdc, int index ) ! FUNCTION: GetDeviceGammaRamp ! FUNCTION: GetDIBColorTable ! FUNCTION: GetDIBits diff --git a/basis/windows/messages/messages.factor b/basis/windows/messages/messages.factor index 26247c1431..422ed45f86 100644 --- a/basis/windows/messages/messages.factor +++ b/basis/windows/messages/messages.factor @@ -210,7 +210,10 @@ CONSTANT: WM_NCMOUSELEAVE 0x02A2 CONSTANT: WM_WTSSESSION_CHANGE 0x02B1 CONSTANT: WM_TABLET_FIRST 0x02c0 CONSTANT: WM_TABLET_LAST 0x02df -CONSTANT: WM_DPICHANGED 0x02E0 +CONSTANT: WM_DPICHANGED 0x02e0 +CONSTANT: WM_DPICHANGED_BEFOREPARENT 0x02e2 +CONSTANT: WM_DPICHANGED_AFTERPARENT 0x02e3 +CONSTANT: WM_GETDPISCALEDSIZE 0x2e4 CONSTANT: WM_CUT 0x0300 CONSTANT: WM_COPY 0x0301 CONSTANT: WM_PASTE 0x0302 diff --git a/basis/windows/shcore/shcore.factor b/basis/windows/shcore/shcore.factor index 63f58ae5f1..f36064d01a 100644 --- a/basis/windows/shcore/shcore.factor +++ b/basis/windows/shcore/shcore.factor @@ -11,4 +11,65 @@ ENUM: MONITOR_DPI_TYPE MDT_RAW_DPI { MDT_DEFAULT 0 } ; +ENUM: PROCESS_DPI_AWARENESS + { PROCESS_DPI_UNAWARE 0 } + { PROCESS_SYSTEM_DPI_AWARE 1 } + { PROCESS_PER_MONITOR_DPI_AWARE 2 } ; + +ENUM: SCALE_CHANGE_FLAGS + { SCF_VALUE_NONE 0 } + { SCF_SCALE 1 } + { SCF_PHYSICAL 2 } ; + FUNCTION: HRESULT GetDpiForMonitor ( HMONITOR hMonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT *dpiY ) + +ENUM: DEVICE_SCALE_FACTOR + { DEVICE_SCALE_FACTOR_INVALID 0 } + { SCALE_100_PERCENT 100 } + { SCALE_120_PERCENT 120 } + { SCALE_125_PERCENT 125 } + { SCALE_140_PERCENT 140 } + { SCALE_150_PERCENT 150 } + { SCALE_160_PERCENT 160 } + { SCALE_175_PERCENT 175 } + { SCALE_180_PERCENT 180 } + { SCALE_200_PERCENT 200 } + { SCALE_225_PERCENT 223 } + { SCALE_250_PERCENT 250 } + { SCALE_300_PERCENT 300 } + { SCALE_350_PERCENT 350 } + { SCALE_400_PERCENT 400 } + { SCALE_450_PERCENT 450 } + { SCALE_500_PERCENT 500 } ; + +FUNCTION: HRESULT GetScaleFactorForMonitor ( + HMONITOR hMon, + DEVICE_SCALE_FACTOR *pScale +) + +FUNCTION: HRESULT RegisterScaleChangeEvent ( + HANDLE hEvent, + DWORD_PTR *pdwCookie +) + +ENUM: DISPLAY_DEVICE_TYPE + { DEVICE_PRIMARY 0 } + { DEVICE_IMMERSIVE 1 } ; + + +FUNCTION: HRESULT RevokeScaleChangeNotifications ( + DISPLAY_DEVICE_TYPE displayDevice, + DWORD dwCookie +) + +FUNCTION: HRESULT UnregisterScaleChangeEvent ( + DWORD_PTR dwCookie +) + +FUNCTION: HRESULT GetProcessDpiAwareness ( HANDLE hprocess, PROCESS_DPI_AWARENESS* value ) +FUNCTION: HRESULT SetProcessDpiAwareness ( PROCESS_DPI_AWARENESS value ) + +ENUM: SHELL_UI_COMPONENT + { SHELL_UI_COMPONENT_TASKBARS 0 } + { SHELL_UI_COMPONENT_NOTIFICATIONAREA 1 } + { SHELL_UI_COMPONENT_DESKBAND 2 } ; \ No newline at end of file diff --git a/basis/windows/user32/user32.factor b/basis/windows/user32/user32.factor index b447943d5a..166cfc50c2 100644 --- a/basis/windows/user32/user32.factor +++ b/basis/windows/user32/user32.factor @@ -2263,11 +2263,6 @@ STRUCT: POWERBROADCAST_SETTING ! HighDPI TYPEDEF: HANDLE DPI_AWARENESS_CONTEXT -ENUM: PROCESS_DPI_AWARENESS - { PROCESS_DPI_UNAWARE 0 } - { PROCESS_SYSTEM_DPI_AWARE 1 } - { PROCESS_PER_MONITOR_DPI_AWARE 2 } ; - ENUM: DPI_AWARENESS { DPI_AWARENESS_INVALID -1 } { DPI_AWARENESS_UNAWARE 0 } @@ -2314,8 +2309,6 @@ FUNCTION: UINT GetDpiForSystem ( ) FUNCTION: UINT GetDpiForWindow ( HWND hwnd ) -FUNCTION: HRESULT GetProcessDpiAwareness ( HANDLE hprocess, PROCESS_DPI_AWARENESS* value ) - FUNCTION: UINT GetSystemDpiForProcess ( HANDLE hProcess ) @@ -2340,28 +2333,40 @@ FUNCTION: DPI_HOSTING_BEHAVIOR GetWindowDpiHostingBehavior ( ) FUNCTION: BOOL SetProcessDPIAware ( ) -FUNCTION: HRESULT SetProcessDpiAwareness ( PROCESS_DPI_AWARENESS value ) FUNCTION: BOOL SetProcessDpiAwarenessContext ( DPI_AWARENESS_CONTEXT value ) FUNCTION: DPI_AWARENESS_CONTEXT GetWindowDpiAwarenessContext ( HWND hwnd ) FUNCTION: DPI_AWARENESS GetAwarenessFromDpiAwarenessContext ( DPI_AWARENESS_CONTEXT value ) +: get-thread-dpi-awareness ( -- enum ) + GetThreadDpiAwarenessContext GetAwarenessFromDpiAwarenessContext ; + FUNCTION: BOOL IsValidDpiAwarenessContext ( DPI_AWARENESS_CONTEXT value ) -! Needs work -! GetThreadDpiAwarenessContext -8 swap IsValidDpiAwarenessContext ! 0, should be 1 -! : DPI_AWARENESS_CONTEXT_UNAWARE ( -- DPI_AWARENESS_CONTEXT ) -! GetThreadDpiAwarenessContext -1 swap ; -! : DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ( -- DPI_AWARENESS_CONTEXT ) -! GetThreadDpiAwarenessContext -2 swap ; -! : DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ( -- DPI_AWARENESS_CONTEXT ) -! GetThreadDpiAwarenessContext -3 swap ; -! : DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ( -- DPI_AWARENESS_CONTEXT ) -! GetThreadDpiAwarenessContext -4 swap ; -! : DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ( -- DPI_AWARENESS_CONTEXT ) -! GetThreadDpiAwarenessContext -5 swap ; +! DPI_AWARENESS_CONTEXT experimentally: +! USE: math.ranges -100 1000 [a,b] [ IsValidDpiAwarenessContext ] map-zip +! [ nip 0 > ] assoc-filter keys . +! { -5 -4 -3 -2 -1 17 18 34 273 529 785 } + +! -4 34 AreDpiAwarenessContextsEqual . ! t +! -5 -5 AreDpiAwarenessContextsEqual . ! t +! -6 -6 AreDpiAwarenessContextsEqual . ! f +: DPI_AWARENESS_CONTEXT_UNAWARE ( -- DPI_AWARENESS_CONTEXT ) + -1 ; + +: DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ( -- DPI_AWARENESS_CONTEXT ) + -2 ; + +: DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ( -- DPI_AWARENESS_CONTEXT ) + -3 ; + +: DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ( -- DPI_AWARENESS_CONTEXT ) + -4 ; + +: DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ( -- DPI_AWARENESS_CONTEXT ) + -5 ; FUNCTION: BOOL LogicalToPhysicalPointForPerMonitorDPI ( HWND hWnd, diff --git a/extra/windows/fullscreen/fullscreen.factor b/extra/windows/fullscreen/fullscreen.factor index 039caae071..dec93893d6 100644 --- a/extra/windows/fullscreen/fullscreen.factor +++ b/extra/windows/fullscreen/fullscreen.factor @@ -1,10 +1,10 @@ ! Copyright (C) 2010 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors alien.c-types arrays classes.struct fry kernel -literals locals make math math.bitwise multiline sequences -slots.syntax ui.backend.windows vocabs.loader windows.errors -windows.gdi32 windows.kernel32 windows.types windows.user32 -ui.gadgets.worlds ; +USING: accessors alien.c-types alien.data classes.struct +io.binary kernel literals locals make math math.bitwise +sequences slots.syntax ui.backend.windows ui.gadgets.worlds +windows.errors windows.gdi32 windows.shcore windows.types +windows.user32 ; IN: windows.fullscreen : hwnd>hmonitor ( HWND -- HMONITOR ) @@ -140,3 +140,7 @@ ERROR: unsupported-resolution triple ; : set-fullscreen ( gadget triple fullscreen? -- ) [ find-world ] 2dip (set-fullscreen) ; + +: get-desktop-scale-factor ( -- n ) + desktop-hmonitor 0 DEVICE_SCALE_FACTOR + [ GetScaleFactorForMonitor win32-error=0/f ] keep le> ; \ No newline at end of file -- 2.34.1