Debian Codegraph @

Contrib/Makensisw/wndspy.cpp

raw
1 // Copyright (C) 2018-2026 Anders Kjersem
2 //
3 // This file is a part of NSIS.
4 //
5 // Licensed under the zlib/libpng license (the "License");
6 // you may not use this file except in compliance with the License.
7 //
8 // Licence details can be found in the file COPYING.
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty.
12
13 #include "makensisw.h"
14 #include <windows.h>
15 #include <windowsx.h>
16 #include "utils.h"
17 #include "resource.h"
18
19 #define InitializeApiFuncWithFallback(mn, fn) { FARPROC f = GetSysProcAddr((mn), (#fn)); g_##fn = Compat_##fn; if (f) (FARPROC&) g_##fn = f; }
20 #define InitializeApiFuncEx(mn, fn, ptr) ( (FARPROC&)(g_##ptr) = GetSysProcAddr((mn), (#fn)) )
21 #define InitializeApiFunc(mn, fn) ( (FARPROC&)(g_##fn) = GetSysProcAddr((mn), (#fn)) )
22 #define CallApiFunc(fn) ( g_##fn )
23 #define HasApiFunc(fn) ( !!(g_##fn) )
24
25 INT_PTR(WINAPI*g_SetThreadDpiAwarenessContext)(INT_PTR);
26 UINT(WINAPI*g_GetDpiForWindow)(HWND);
27 BOOL(WINAPI*g_LogicalToPhysicalPoint)(HWND hWnd, LPPOINT lpPoint);
28 BOOL(WINAPI*g_PhysicalToLogicalPoint)(HWND hWnd, LPPOINT lpPoint);
29
30 typedef struct _DPI {
31 enum { a_invalid = -1, a_unaware = 0, a_system = 1, a_pm = 2 };
32 enum { ac_invalid = 0, ac_unaware = -1, ac_system = -2, ac_pm1 = -3, ac_pm2 = -4, ac_count = ac_pm2 * -1 };
33 static inline INT_PTR SetThreadDpiAwarenessContext(INT_PTR a1) { return g_SetThreadDpiAwarenessContext(a1); }
34 static inline UINT GetDpiForWindow(HWND a1) { return HasApiFunc(GetDpiForWindow) ? CallApiFunc(GetDpiForWindow)(a1) : 0; }
35 static inline UINT GetDpiForMonitor(HWND hWnd) { return DpiGetForMonitor(hWnd); }
36 } DPI;
37
38 static INT_PTR WINAPI Compat_SetThreadDpiAwarenessContext(INT_PTR AC) { return 0; }
39 static BOOL WINAPI Compat_LogicalToPhysicalPoint(HWND hWnd, LPPOINT lpPoint) { return TRUE; }
40 static BOOL WINAPI Compat_PhysicalToLogicalPoint(HWND hWnd, LPPOINT lpPoint) { return TRUE; }
41
42 static void InitializeDpiApi()
43 {
44 InitializeApiFuncWithFallback("USER32", SetThreadDpiAwarenessContext);
45 InitializeApiFunc("USER32", GetDpiForWindow);
46 InitializeApiFuncEx("USER32", LogicalToPhysicalPointForPerMonitorDPI, LogicalToPhysicalPoint);
47 if (!HasApiFunc(LogicalToPhysicalPoint)) InitializeApiFuncWithFallback("USER32", LogicalToPhysicalPoint);
48 InitializeApiFuncEx("USER32", PhysicalToLogicalPointForPerMonitorDPI, PhysicalToLogicalPoint);
49 if (!HasApiFunc(PhysicalToLogicalPoint)) InitializeApiFuncWithFallback("USER32", PhysicalToLogicalPoint);
50 }
51
52 static BOOL LogicalToPhysical(HWND hWnd, POINT&Pt) { return CallApiFunc(LogicalToPhysicalPoint)(hWnd, &Pt); }
53 static BOOL PhysicalToLogical(HWND hWnd, POINT&Pt) { return CallApiFunc(PhysicalToLogicalPoint)(hWnd, &Pt); }
54
55 static void LogicalToPhysical(HWND hWnd, RECT&Rect)
56 {
57 POINT *p = (POINT*) &Rect;
58 LogicalToPhysical(hWnd, p[0]), LogicalToPhysical(hWnd, p[1]);
59 }
60 static void PhysicalToLogical(HWND hWnd, RECT&Rect)
61 {
62 POINT *p = (POINT*) &Rect;
63 PhysicalToLogical(hWnd, p[0]), PhysicalToLogical(hWnd, p[1]);
64 }
65
66 static BOOL GetPhysicalWindowRect(HWND hWnd, RECT&Rect, HWND hWndCaller)
67 {
68 BOOL succ = GetWindowRect(hWnd, &Rect);
69 LogicalToPhysical(hWndCaller, Rect);
70 return succ;
71 }
72
73 static BOOL GetPhysicalClientRect(HWND hWnd, RECT&Rect, HWND hWndCaller)
74 {
75 BOOL succ = GetClientRect(hWnd, &Rect);
76 LogicalToPhysical(hWndCaller, Rect);
77 return succ;
78 }
79
80 static inline void PhysicalToLogical(HWND hWnd, RECT&Rect, HWND hWndCaller)
81 {
82 PhysicalToLogical(hWndCaller, Rect);
83 }
84
85 static void ConvertLogicalToLogical(RECT&Rect, HWND hSrc, HWND hDst)
86 {
87 LogicalToPhysical(hSrc, Rect), PhysicalToLogical(hDst, Rect);
88 }
89
90 static BOOL GetLogicalWindowRect(HWND hWnd, RECT&Rect, HWND hWndCaller)
91 {
92 BOOL succ = GetWindowRect(hWnd, &Rect);
93 if (succ) ConvertLogicalToLogical(Rect, hWndCaller, hWnd);
94 return succ;
95 }
96
97 static HWND GetParentWindow(HWND hWnd)
98 {
99 HWND r = GetParent(hWnd); // Parent or owner
100 return r != GetWindow(hWnd, GW_OWNER) ? r : NULL;
101 }
102
103 static HWND GetAncestorRoot(HWND hWnd)
104 {
105 if (!SupportsWNT4() && !SupportsW95()) return GetAncestor(hWnd, GA_ROOT);
106 for (HWND hTmp; (hTmp = GetParentWindow(hWnd)); ) hWnd = hTmp;
107 return hWnd;
108 }
109
110 typedef struct _DIALOGDATA {
111 BOOL Dragging;
112 HWND hWndTarget, hWndOutline;
113 int DialogAwarenessContext; // Canonical DPI awareness context
114 _DIALOGDATA() : hWndOutline(0) {}
115 static struct _DIALOGDATA* Get(HWND hDlg) { return (struct _DIALOGDATA*) GetWindowLongPtr(hDlg, DWLP_USER); }
116 static void Set(HWND hDlg, void*pDD) { SetWindowLongPtr(hDlg, DWLP_USER, (LONG_PTR) pDD); }
117 } DIALOGDATA;
118
119 struct FINDCHILDDATA {
120 HWND hWnd;
121 POINT pt;
122 ULONG Area;
123 bool IncludeHidden;
124 void Init(POINT pt, bool IncludeHidden) { Area = 0, Area = ~Area, this->pt = pt, this->IncludeHidden = IncludeHidden; }
125 };
126
127 static BOOL CALLBACK FindChildWindowFromPointProc(HWND hWnd, LPARAM LParam)
128 {
129 RECT r;
130 FINDCHILDDATA*pFCD = (FINDCHILDDATA*) LParam;
131 if (GetWindowRect(hWnd, &r) && PtInRect(&r, pFCD->pt)) // TODO: Region?
132 {
133 ULONG area = (r.right - r.left) * (r.bottom - r.top);
134 if (area < pFCD->Area && (pFCD->IncludeHidden || IsWindowVisible(hWnd)))
135 pFCD->Area = area, pFCD->hWnd = hWnd;
136 }
137 return TRUE;
138 }
139
140 static HWND FindChildWindowFromPoint(HWND hWnd, FINDCHILDDATA*pFCD)
141 {
142 HWND hParent = GetParentWindow(hWnd), hOrg;
143 if (!hParent)
144 hParent = hWnd;
145 recurse:
146 hOrg = pFCD->hWnd = hWnd;
147 EnumChildWindows(hParent, FindChildWindowFromPointProc, (LPARAM) pFCD);
148 if (hOrg && hOrg != pFCD->hWnd)
149 {
150 hWnd = hParent = pFCD->hWnd;
151 goto recurse;
152 }
153 return pFCD->hWnd;
154 }
155
156 static HWND GetChildWindowFromPointHelper(POINT pt)
157 {
158 const bool includeHidden = false;
159 HWND hWnd = WindowFromPoint(pt), hWnd2;
160 if (!hWnd)
161 return hWnd;
162 while(!includeHidden && hWnd && !IsWindowVisible(hWnd))
163 if ((hWnd2 = GetParentWindow(hWnd)))
164 hWnd = hWnd2;
165
166 FINDCHILDDATA fcd;
167 fcd.Init(pt, includeHidden);
168 hWnd = FindChildWindowFromPoint(hWnd, &fcd);
169 return hWnd;
170 }
171
172 #if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64_VERSION_MAJOR) && (!defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500))
173 WINUSERAPI BOOL WINAPI IsHungAppWindow(HWND); // MinGW is wrong, IsHungAppWindow was added in WinNT4. MinGW < 3.20? does not even have it in their .lib!
174 #endif
175 static BOOL IsHung(HWND hWnd)
176 {
177 #if !(defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64_VERSION_MAJOR) && (__MINGW32_MINOR_VERSION <= 15))
178 if (sizeof(void*) > 4 || sizeof(TCHAR) > 1)
179 {
180 return IsHungAppWindow(hWnd);
181 }
182 else
183 #endif
184 {
185 static FARPROC g_func = GetSysProcAddr("USER32", "IsHungAppWindow");
186 if (g_func) return ((BOOL(WINAPI*)(HWND))g_func)(hWnd);
187 DWORD_PTR mr;
188 LRESULT rv = SendMessageTimeout(hWnd, WM_NULL, 0, 0, SMTO_ABORTIFHUNG, 500, &mr);
189 return rv == 0;
190 }
191 }
192
193 static LRESULT CALLBACK OutlineWindowProc(HWND hWnd, UINT Msg, WPARAM WParam, LPARAM LParam)
194 {
195 WNDPROC orgProc = (WNDPROC) GetWindowLongPtr(hWnd, GWLP_USERDATA);
196 switch(Msg)
197 {
198 case WM_WINDOWPOSCHANGED:
199 {
200 WINDOWPOS wp = *(WINDOWPOS*) LParam;
201 int size = GetSystemMetrics(SM_CXBORDER) * 2, sizeX = size, sizeY = size;
202 if (sizeX >= wp.cx) sizeX = 1;
203 if (sizeY >= wp.cy) sizeY = 1;
204 HRGN hRgn = CreateRectRgn(0, 0, wp.cx, wp.cy);
205 HRGN hRgnInner = CreateRectRgn(sizeX, sizeY, wp.cx - sizeX, wp.cy - sizeY);
206 if (sizeX * 2 < wp.cx && sizeY * 2 < wp.cy) CombineRgn(hRgn, hRgn, hRgnInner, RGN_XOR);
207 DeleteObject(hRgnInner);
208 SetWindowRgn(hWnd, hRgn, TRUE);
209 }
210 return 0;
211 case WM_PAINT:
212 {
213 PAINTSTRUCT ps;
214 HDC hDC = BeginPaint(hWnd, &ps);
215 FillRectColor(hDC, ps.rcPaint, RGB(255, 0, 255));
216 EndPaint(hWnd, &ps);
217 }
218 return 0;
219 }
220 return orgProc ? CallWindowProc(orgProc, hWnd, Msg, WParam, LParam) : DefWindowProc(hWnd, Msg, WParam, LParam);
221 }
222
223 static void SetDragSourceImage(HWND hDlg, INT_PTR Dragging = false)
224 {
225 HCURSOR hCur = Dragging ? NULL : LoadCursor(NULL, IDC_CROSS);
226 SendDlgItemMessage(hDlg, IDC_SPYDRAG, STM_SETIMAGE, IMAGE_CURSOR, (LPARAM) hCur);
227 }
228
229 static void ShowWindowInfo(HWND hDlg, HWND hWnd)
230 {
231 const TCHAR *strFmtPtr = sizeof(void*) > 4 ? _T("%#.16I64x") : _T("%#.8x");
232 const TCHAR *strFmtPtr2 = sizeof(void*) > 4 ? _T("%#.16I64x & %#.16I64x") : _T("%#.8x & %#.8x");
233 const TCHAR *strIntPtr = sizeof(void*) > 4 ? _T("%I64d") : _T("%d");
234 TCHAR buf[300];
235 LONG_PTR style = GetWindowLongPtr(hWnd, GWL_STYLE);
236
237 wsprintf(buf, strIntPtr, GetWindowLongPtr(hWnd, GWLP_ID));
238 SetDlgItemText(hDlg, IDC_WNDID, buf);
239
240 wsprintf(buf, strFmtPtr, hWnd);
241 SetDlgItemText(hDlg, IDC_HWND, buf);
242
243 if (!GetClassName(hWnd, buf, COUNTOF(buf))) *buf = _T('\0');
244 SetDlgItemText(hDlg, IDC_WNDCLASS, buf);
245
246 wsprintf(buf, strFmtPtr2, GetWindowLongPtr(hWnd, GWLP_USERDATA), GetWindowLongPtr(hWnd, DWLP_USER));
247 SetDlgItemText(hDlg, IDC_WNDUSERDATA, buf);
248
249 wsprintf(buf, strFmtPtr2, (LONG_PTR) style, GetWindowLongPtr(hWnd, GWL_EXSTYLE));
250 SetDlgItemText(hDlg, IDC_WNDSTYLE, buf);
251
252 RECT rw, rc;
253 GetLogicalWindowRect(hWnd, rw, hDlg);
254 wsprintf(buf, _T("%dx%d..%dx%d \x2248 %dx%d"), rw.left, rw.top, rw.right, rw.bottom, rw.right - rw.left, rw.bottom - rw.top); // '\x2245' is not present on XP
255 SetDlgItemText(hDlg, IDC_WNDLOGISIZE, hWnd ? buf : NULL);
256
257 GetPhysicalWindowRect(hWnd, rw, hDlg);
258 GetPhysicalClientRect(hWnd, rc, hDlg);
259 wsprintf(buf, _T("%dx%d..%dx%d \x2248 %dx%d (%dx%d)"), rw.left, rw.top, rw.right, rw.bottom, rw.right - rw.left, rw.bottom - rw.top, rc.right - rc.left, rc.bottom - rc.top);
260 SetDlgItemText(hDlg, IDC_WNDPHYSSIZE, hWnd ? buf : NULL);
261
262 *buf = _T('\0');
263 if (IsWindowUnicode(hWnd)) lstrcat(buf, _T("Unicode"));
264 if (IsWindowVisible(hWnd)) lstrcat(*buf ? lstrcat(buf, _T(", ")) : buf, _T("Visible")); // IsWindowVisible is not exactly the same as WS_VISIBLE
265 if (!(style & WS_DISABLED)) lstrcat(*buf ? lstrcat(buf, _T(", ")) : buf, _T("Enabled"));
266 if (GetWindow(hWnd, GW_OWNER)) lstrcat(*buf ? lstrcat(buf, _T(", ")) : buf, _T("Owned"));
267 if (IsHung(hWnd)) lstrcat(*buf ? lstrcat(buf, _T(", ")) : buf, _T("Hung"));
268 SetDlgItemText(hDlg, IDC_WNDINFO, hWnd ? buf : NULL);
269
270 UINT dpi = DPI::GetDpiForWindow(hWnd), mondpi = DPI::GetDpiForMonitor(hWnd);
271 #ifndef _M_ARM64 // Not i386, AMD64 nor ARM(32-bit)
272 if (!dpi)
273 {
274 OSVERSIONINFO osv;
275 GetVersionEx((osv.dwOSVersionInfoSize = sizeof(osv), &osv));
276 if (MAKELONG(osv.dwMinorVersion, osv.dwMajorVersion) < MAKELONG(3, 6))
277 dpi = mondpi; // <= 8.0 has the same DPI on all monitors (blogs.windows.com/windowsexperience/2013/07/15/windows-8-1-dpi-scaling-enhancements/)
278 }
279 #endif
280 UINT cch = wsprintf(buf, dpi ? _T("%u") : _T("?"), dpi);
281 if ((DpiAwarePerMonitor() || dpi) && dpi != mondpi) wsprintf(buf + cch, _T(" (on %u)"), mondpi); // Don't display on >= 8.1 && < 10FU1607 (because we are not PMv1). GetDpiForWindow will also fail on those systems.
282 SetDlgItemText(hDlg, IDC_WNDDPI, hWnd ? buf : NULL);
283 }
284
285 static INT_PTR CALLBACK SpyDlgProc(HWND hDlg, UINT Msg, WPARAM WParam, LPARAM LParam)
286 {
287 enum { TID_OUTLINE = 1 };
288 DIALOGDATA*pDD = DIALOGDATA::Get(hDlg);
289 switch(Msg)
290 {
291 case WM_SIZE:
292 if (WParam == SIZE_MAXIMIZED) ShowWindow(hDlg, SW_SHOWNOACTIVATE); // Disallow STARTF_USESHOWWINDOW+SW_MAXIMIZE
293 break;
294 case WM_INITDIALOG:
295 DIALOGDATA::Set(hDlg, (pDD = (DIALOGDATA*) LParam));
296 CenterOnParent(hDlg);
297 // On >= 10FU1703 we are PMv2 and Windows scales our dialog and child controls.
298 // On >= 10FU1607 && < 10FU1703 we are System aware but try to upgrade this thread to
299 // PMv1 to reduce compatibility handling in other USER32 functions. We don't want
300 // the dialog HWND to be PMv1 because we are not going to manually reposition our controls.
301 // On < 10FU1607 we only have the process awareness (not ideal).
302 if (pDD->DialogAwarenessContext > DPI::ac_pm2) // Is the dialog AC < PMv2? Note: The canonical AC numbers are negative.
303 DPI::SetThreadDpiAwarenessContext(DPI::ac_pm1);
304 SendMessage(hDlg, WM_CAPTURECHANGED, 0, 0);
305 break;
306 case WM_CLOSE: close:
307 return EndDialog(hDlg, 0);
308 case WM_COMMAND:
309 switch(WParam)
310 {
311 case IDCANCEL:
312 goto close;
313 case MAKELONG(IDC_SPYDRAG, STN_CLICKED):
314 SetCapture(hDlg);
315 pDD->hWndTarget = 0;
316 pDD->Dragging++;
317 SetDragSourceImage(hDlg, (INT_PTR) hDlg);
318 SetCursor(LoadCursor(NULL, IDC_CROSS));
319 break;
320 }
321 break;
322 case WM_MOUSEMOVE:
323 if (pDD->Dragging)
324 {
325 POINT pt;
326 GetCursorPos(&pt);
327 HWND hWnd = GetChildWindowFromPointHelper(pt);
328 if (hWnd == pDD->hWndTarget)
329 break;
330
331 pDD->hWndTarget = hWnd;
332 if (GetAncestorRoot(hWnd) == hDlg)
333 hWnd = 0;
334
335 ShowWindowInfo(hDlg, hWnd);
336 }
337 break;
338 case WM_LBUTTONUP:
339 if (pDD->Dragging && pDD->hWndTarget)
340 {
341 if (!pDD->hWndOutline)
342 {
343 pDD->hWndOutline = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST, WC_STATIC, NULL, WS_POPUP|WS_DISABLED|SS_BLACKRECT, 0, 0, 0, 0, hDlg, 0, 0, 0);
344 SetWindowLongPtr(pDD->hWndOutline, GWLP_USERDATA, SetWindowLongPtr(pDD->hWndOutline, GWLP_WNDPROC, (LONG_PTR) OutlineWindowProc));
345 }
346 SetWindowPos(pDD->hWndOutline, HWND_BOTTOM, -32767, -32767, 1, 1, SWP_SHOWWINDOW|SWP_NOCOPYBITS|SWP_NOACTIVATE|SWP_NOOWNERZORDER); // MSDN says LogicalToPhysicalPoint requires a visible window
347 RECT r;
348 GetPhysicalWindowRect(pDD->hWndTarget, r, hDlg), PhysicalToLogical(pDD->hWndOutline, r, hDlg);
349 if (GetAncestorRoot(pDD->hWndTarget) != hDlg)
350 {
351 SetWindowPos(pDD->hWndOutline, HWND_TOPMOST, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_HIDEWINDOW|SWP_NOCOPYBITS|SWP_NOACTIVATE|SWP_NOOWNERZORDER);
352 ShowWindow(pDD->hWndOutline, SW_SHOWNA); // To avoid a small Windows redraw bug, don't show the window until after it has the correct size
353 }
354 SetTimer(hDlg, TID_OUTLINE, 2 * 1000, NULL);
355 }
356 ReleaseCapture();
357 break;
358 case WM_CAPTURECHANGED:
359 SetFocus(GetDlgItem(hDlg, IDC_SPYDRAG));
360 pDD->Dragging = 0;
361 SetDragSourceImage(hDlg, FALSE);
362 break;
363 case WM_TIMER:
364 if (WParam == TID_OUTLINE)
365 {
366 KillTimer(hDlg, WParam);
367 ShowWindow(pDD->hWndOutline, SW_HIDE);
368 }
369 break;
370 }
371 return FALSE;
372 }
373
374 struct ScopedThreadDpiAwarenessContext { // Note: Assumes InitializeDpiApi() has been called!
375 struct List {
376 List() : m_l(0) {}
377 List operator <<(INT_PTR in) { List r = *this; r.m_l |= (1 << (in < 0 ? -in : in)); return r; }
378 UINT GetBits() const { return m_l; }
379 UINT m_l;
380 };
381 ScopedThreadDpiAwarenessContext(List ACList) : m_OrgAC(0)
382 {
383 for (UINT s = DPI::ac_count, list = ACList.GetBits(); (m_AC = -(int)s); --s)
384 if ((1 << s) & list)
385 if ((m_OrgAC = DPI::SetThreadDpiAwarenessContext((INT_PTR) m_AC)))
386 break;
387 }
388 ~ScopedThreadDpiAwarenessContext()
389 {
390 DPI::SetThreadDpiAwarenessContext(m_OrgAC);
391 }
392 int GetCanonicalActiveAwarenessContext() const { return m_AC; }
393 INT_PTR m_OrgAC;
394 int m_AC; // Canonical "active" DPI_AWARENESS_CONTEXT
395 };
396
397 INT_PTR ShowWndSpy(HWND hOwner)
398 {
399 InitializeDpiApi();
400 ScopedThreadDpiAwarenessContext::List aclist;
401 ScopedThreadDpiAwarenessContext stdac(aclist << DPI::ac_pm2 << DPI::ac_system);
402 DIALOGDATA dd;
403 dd.DialogAwarenessContext = stdac.GetCanonicalActiveAwarenessContext();
404 return DialogBoxParam(HINST_APPLICATION, MAKEINTRESOURCE(DLG_WNDSPY), hOwner, SpyDlgProc, (LPARAM) &dd);
405 }