annotate dep/animia/src/win/win32.cc @ 237:a7d0d543b334

*: make OS X builds succeed new script: deploy_build.sh, creates the app bundle
author Paper <paper@paper.us.eu.org>
date Fri, 19 Jan 2024 11:14:44 -0500
parents bc1ae1810855
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
191
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
1 /*
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
2 * win/win32.cc: support for Windows
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
3 *
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
4 * Surprisingly, this is the one time where Microsoft actually
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
5 * does it fairly OK. Everything has a pretty simple API, despite
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
6 * the stupid wide string stuff.
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
7 */
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 #include "animia/win/win32.h"
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
9 #include "animia.h"
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 #include "animia/util/win32.h"
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 #include "animia/win.h"
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13 #include <set>
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 #include <string>
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 #include <windows.h>
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 namespace animia::internal::win32 {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 static std::wstring GetWindowClassName(HWND hwnd) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 // The maximum size for lpszClassName, according to the documentation of
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 // WNDCLASSEX structure
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 constexpr int kMaxSize = 256;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 std::wstring buffer(kMaxSize, L'\0');
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 const auto size = ::GetClassNameW(hwnd, &buffer.front(), buffer.length());
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
27 /* for some reason GetClassName returns the actual size of the buffer *with* the
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
28 terminating NULL byte */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
29 buffer.resize(size);
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 return buffer;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 static std::wstring GetWindowText(HWND hwnd) {
191
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
34 const auto estimated_size = ::GetWindowTextLengthW(hwnd);
0fc126d52de4 animia: multiple stylistic choices
Paper <mrpapersonic@gmail.com>
parents: 156
diff changeset
35 std::wstring buffer(estimated_size + 1, L'\0');
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
37 const auto size = ::GetWindowTextW(hwnd, &buffer.front(), buffer.length());
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
38 /* GetWindowTextLength docs:
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
39 "Under certain conditions, the GetWindowTextLength function may return a value
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
40 that is larger than the actual length of the text." */
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
41 buffer.resize(size);
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 return buffer;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 static DWORD GetWindowProcessId(HWND hwnd) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 DWORD process_id = 0;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 ::GetWindowThreadProcessId(hwnd, &process_id);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 return process_id;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 static std::wstring GetProcessPath(DWORD process_id) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 // If we try to open a SYSTEM process, this function fails and the last error
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53 // code is ERROR_ACCESS_DENIED.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 //
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 // Note that if we requested PROCESS_QUERY_INFORMATION access right instead
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 // of PROCESS_QUERY_LIMITED_INFORMATION, this function would fail when used
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 // to open an elevated process.
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
58 Handle process_handle(::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, process_id));
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 if (!process_handle)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 return std::wstring();
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63 std::wstring buffer(MAX_PATH, L'\0');
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 DWORD buf_size = buffer.length();
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
65
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
66 // Note that this function requires Windows Vista or above. You may use
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
67 // GetProcessImageFileName or GetModuleFileNameEx on earlier versions.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
68 if (!::QueryFullProcessImageNameW(process_handle.get(), 0, &buffer.front(), &buf_size))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
69 return std::wstring();
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
70
153
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
71 buffer.resize(buf_size);
bd439dd6ffc5 *: make win stuff actually work, rename bsd.cc to xnu.cc
Paper <mrpapersonic@gmail.com>
parents: 152
diff changeset
72 return buffer;
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
73 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
74
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
75 ////////////////////////////////////////////////////////////////////////////////
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
76
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
77 static bool VerifyWindowStyle(HWND hwnd) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
78 const auto window_style = ::GetWindowLong(hwnd, GWL_STYLE);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
79 const auto window_ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
80
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
81 auto has_style = [&window_style](DWORD style) { return (window_style & style) != 0; };
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
82 auto has_ex_style = [&window_ex_style](DWORD ex_style) { return (window_ex_style & ex_style) != 0; };
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
83
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
84 // Toolbars, tooltips and similar topmost windows
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
85 if (has_style(WS_POPUP) && has_ex_style(WS_EX_TOOLWINDOW))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
86 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
87 if (has_ex_style(WS_EX_TOPMOST) && has_ex_style(WS_EX_TOOLWINDOW))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
88 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
89
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
90 return true;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
91 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
92
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
93 static bool VerifyClassName(const std::wstring& name) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
94 static const std::set<std::wstring> invalid_names = {
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
95 // System classes
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
96 L"#32770", // Dialog box
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
97 L"CabinetWClass", // Windows Explorer
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
98 L"ComboLBox",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
99 L"DDEMLEvent",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
100 L"DDEMLMom",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
101 L"DirectUIHWND",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
102 L"GDI+ Hook Window Class",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
103 L"IME",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
104 L"Internet Explorer_Hidden",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
105 L"MSCTFIME UI",
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
106 L"tooltips_class32",
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
107 };
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
109 return !name.empty() && !invalid_names.count(name);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
110 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
111
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
112 static bool VerifyProcessPath(const std::wstring& path) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
113 return !path.empty() && !IsSystemDirectory(path);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
114 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
115
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
116 static bool VerifyProcessFileName(const std::wstring& name) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
117 static const std::set<std::wstring> invalid_names = {
156
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
118 // System files
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
119 L"explorer", // Windows Explorer
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
120 L"taskeng", // Task Scheduler Engine
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
121 L"taskhost", // Host Process for Windows Tasks
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
122 L"taskhostex", // Host Process for Windows Tasks
cdf79282d647 dep/animia: add VERY early x11 window stuff
Paper <mrpapersonic@gmail.com>
parents: 153
diff changeset
123 L"Taskmgr", // Task Manager
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
124 };
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
125
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
126 return !name.empty() && !invalid_names.count(name);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
127 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
128
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
129 ////////////////////////////////////////////////////////////////////////////////
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
130
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
131 static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param) {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
132 if (!::IsWindowVisible(hwnd))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
133 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
134
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
135 if (!VerifyWindowStyle(hwnd))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
136 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
137
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
138 Window window;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
139 window.id = static_cast<unsigned int>(reinterpret_cast<ULONG_PTR>(hwnd));
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
140 window.text = ToUtf8String(GetWindowText(hwnd));
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
141
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
142 {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
143 std::wstring class_name = GetWindowClassName(hwnd);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
144 window.class_name = ToUtf8String(class_name);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
145 if (!VerifyClassName(class_name))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
146 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
147 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
148
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
149 Process process;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
150 process.pid = GetWindowProcessId(hwnd);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
151
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
152 const auto path = GetProcessPath(process.pid);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
153 if (!VerifyProcessPath(path))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
154 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
155
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
156 {
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
157 std::wstring name = GetFileNameWithoutExtension(GetFileNameFromPath(path));
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
158 process.name = ToUtf8String(name);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
159 if (!VerifyProcessFileName(name))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
160 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
161 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
162
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
163 auto& window_proc = *reinterpret_cast<window_proc_t*>(param);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
164 if (!window_proc(process, window))
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
165 return FALSE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
166
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
167 return TRUE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
168 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
169
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 191
diff changeset
170 bool EnumerateWindows(window_proc_t window_proc) {
152
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
171 if (!window_proc)
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
172 return false;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
173
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
174 const auto param = reinterpret_cast<LPARAM>(&window_proc);
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
175
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
176 // Note that EnumWindows enumerates only top-level windows of desktop apps
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
177 // (as opposed to UWP apps) on Windows 8 and above.
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
178 return ::EnumWindows(EnumWindowsProc, param) != FALSE;
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
179 }
8700806c2cc2 dep/animia: awesome new breaking changes!
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
180
198
bc1ae1810855 dep/animia: switch from using classes to global functions
Paper <mrpapersonic@gmail.com>
parents: 191
diff changeset
181 } // namespace animia::internal::win32