-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathw11-accent-fetcher.wh.cpp
More file actions
309 lines (259 loc) · 10.2 KB
/
Copy pathw11-accent-fetcher.wh.cpp
File metadata and controls
309 lines (259 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// ==WindhawkMod==
// @id w11-accent-fetcher
// @name Firefox WinUI Accent Fetcher
// @description Fetches W11 Immersive colors and writes them to your Firefox chrome folder's win11-accents.css.
// @version 1.0
// @author Lockframe
// @github https://github.com/Lockframe
// @include windhawk.exe
// ==/WindhawkMod==
#include <sec_api/stdio_s.h>
#include <windows.h>
// Undocumented uxtheme.dll functions used to fetch WinUI immersive colors
typedef DWORD(WINAPI* PGETIMMERSIVEUSERCOLORSETPREFERENCE)(BOOL, BOOL);
typedef DWORD(WINAPI* PGETIMMERSIVECOLORTYPEFROMNAME)(PCWSTR);
typedef DWORD(WINAPI* PGETIMMERSIVECOLORFROMCOLORSETEX)(UINT, UINT, BOOL, UINT);
PGETIMMERSIVEUSERCOLORSETPREFERENCE pGetImmersiveUserColorSetPreference = nullptr;
PGETIMMERSIVECOLORTYPEFROMNAME pGetImmersiveColorTypeFromName = nullptr;
PGETIMMERSIVECOLORFROMCOLORSETEX pGetImmersiveColorFromColorSetEx = nullptr;
// Adjust to match your profile
const char* cssPath = "C:\\Users\\given\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\ctug3z21.default-release\\chrome\\win11-accents.css";
HANDLE g_hThread = NULL;
HANDLE g_hExitEvent = NULL;
void UpdateCSS() {
if (!pGetImmersiveUserColorSetPreference || !pGetImmersiveColorTypeFromName || !pGetImmersiveColorFromColorSetEx) return;
UINT colorSet = pGetImmersiveUserColorSetPreference(FALSE, FALSE);
// WinUI uses Dark1 for Light Mode contrast, and Light2 for Dark Mode contrast
UINT typeDark1 = pGetImmersiveColorTypeFromName(L"ImmersiveSystemAccentDark1");
UINT typeLight2 = pGetImmersiveColorTypeFromName(L"ImmersiveSystemAccentLight2");
// The API returns a COLORREF in 0x00bbggrr format
DWORD colorDark1 = pGetImmersiveColorFromColorSetEx(colorSet, typeDark1, FALSE, 0);
DWORD colorLight2 = pGetImmersiveColorFromColorSetEx(colorSet, typeLight2, FALSE, 0);
// Extract standard RGB
int rD1 = colorDark1 & 0xFF;
int gD1 = (colorDark1 >> 8) & 0xFF;
int bD1 = (colorDark1 >> 16) & 0xFF;
int rL2 = colorLight2 & 0xFF;
int gL2 = (colorLight2 >> 8) & 0xFF;
int bL2 = (colorLight2 >> 16) & 0xFF;
char cssContent[512];
int len = wsprintfA(cssContent,
"/* Generated by Windhawk Tool Mod */\n"
":root {\n"
" --accentLight: #%02X%02X%02X !important;\n"
" --accentDark: #%02X%02X%02X !important;\n"
"}\n",
rD1, gD1, bD1, rL2, gL2, bL2);
HANDLE hFile = CreateFileA(cssPath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE) {
DWORD bytesWritten;
WriteFile(hFile, cssContent, (DWORD)len, &bytesWritten, NULL);
CloseHandle(hFile);
}
}
DWORD WINAPI ThreadProc(LPVOID lpParam) {
HMODULE hUxTheme = LoadLibraryW(L"uxtheme.dll");
if (hUxTheme) {
pGetImmersiveUserColorSetPreference = (PGETIMMERSIVEUSERCOLORSETPREFERENCE)GetProcAddress(hUxTheme, MAKEINTRESOURCEA(98));
pGetImmersiveColorTypeFromName = (PGETIMMERSIVECOLORTYPEFROMNAME)GetProcAddress(hUxTheme, MAKEINTRESOURCEA(96));
pGetImmersiveColorFromColorSetEx = (PGETIMMERSIVECOLORFROMCOLORSETEX)GetProcAddress(hUxTheme, MAKEINTRESOURCEA(95));
}
// Initial write on startup
UpdateCSS();
HKEY hKey;
// Watch the specific DWM registry key that updates when accent colors change
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\DWM", 0, KEY_NOTIFY, &hKey) == ERROR_SUCCESS) {
HANDLE hRegEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (hRegEvent) {
HANDLE events[2] = { g_hExitEvent, hRegEvent };
while (true) {
// Ask Windows to signal hRegEvent the moment the registry key changes
if (RegNotifyChangeKeyValue(hKey, FALSE, REG_NOTIFY_CHANGE_LAST_SET, hRegEvent, TRUE) != ERROR_SUCCESS) {
break;
}
// Put the thread to sleep until either Windhawk unloads us, or the colors change
DWORD waitRes = WaitForMultipleObjects(2, events, FALSE, INFINITE);
if (waitRes == WAIT_OBJECT_0) {
// Windhawk triggered the exit event. Break the loop and die gracefully.
break;
} else if (waitRes == WAIT_OBJECT_0 + 1) {
// The registry changed! Update the CSS and reset the event for the next loop.
UpdateCSS();
ResetEvent(hRegEvent);
} else {
break;
}
}
CloseHandle(hRegEvent);
}
RegCloseKey(hKey);
}
return 0;
}
BOOL WhTool_ModInit() {
g_hExitEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
if (!g_hExitEvent) return FALSE;
g_hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
if (!g_hThread) {
CloseHandle(g_hExitEvent);
return FALSE;
}
return TRUE;
}
void WhTool_ModUninit() {
if (g_hExitEvent) {
SetEvent(g_hExitEvent); // Signal the thread to break its sleep loop
if (g_hThread) {
WaitForSingleObject(g_hThread, 3000);
CloseHandle(g_hThread);
g_hThread = NULL;
}
CloseHandle(g_hExitEvent);
g_hExitEvent = NULL;
}
}
////////////////////////////////////////////////////////////////////////////////
// Windhawk tool mod implementation for mods which don't need to inject to other
// processes or hook other functions. Context:
// https://github.com/ramensoftware/windhawk/wiki/Mods-as-tools:-Running-mods-in-a-dedicated-process
//
// The mod will load and run in a dedicated windhawk.exe process.
//
// Paste the code below as part of the mod code, and use these callbacks:
// * WhTool_ModInit
// * WhTool_ModSettingsChanged
// * WhTool_ModUninit
//
// Currently, other callbacks are not supported.
bool g_isToolModProcessLauncher;
HANDLE g_toolModProcessMutex;
void WINAPI EntryPoint_Hook() {
Wh_Log(L">");
ExitThread(0);
}
BOOL Wh_ModInit() {
bool isExcluded = false;
bool isToolModProcess = false;
bool isCurrentToolModProcess = false;
int argc;
LPWSTR* argv = CommandLineToArgvW(GetCommandLine(), &argc);
if (!argv) {
Wh_Log(L"CommandLineToArgvW failed");
return FALSE;
}
for (int i = 1; i < argc; i++) {
if (wcscmp(argv[i], L"-service") == 0 ||
wcscmp(argv[i], L"-service-start") == 0 ||
wcscmp(argv[i], L"-service-stop") == 0) {
isExcluded = true;
break;
}
}
for (int i = 1; i < argc - 1; i++) {
if (wcscmp(argv[i], L"-tool-mod") == 0) {
isToolModProcess = true;
if (wcscmp(argv[i + 1], WH_MOD_ID) == 0) {
isCurrentToolModProcess = true;
}
break;
}
}
LocalFree(argv);
if (isExcluded) {
return FALSE;
}
if (isCurrentToolModProcess) {
g_toolModProcessMutex =
CreateMutex(nullptr, TRUE, L"windhawk-tool-mod_" WH_MOD_ID);
if (!g_toolModProcessMutex) {
Wh_Log(L"CreateMutex failed");
ExitProcess(1);
}
if (GetLastError() == ERROR_ALREADY_EXISTS) {
Wh_Log(L"Tool mod already running (%s)", WH_MOD_ID);
ExitProcess(1);
}
if (!WhTool_ModInit()) {
ExitProcess(1);
}
IMAGE_DOS_HEADER* dosHeader =
(IMAGE_DOS_HEADER*)GetModuleHandle(nullptr);
IMAGE_NT_HEADERS* ntHeaders =
(IMAGE_NT_HEADERS*)((BYTE*)dosHeader + dosHeader->e_lfanew);
DWORD entryPointRVA = ntHeaders->OptionalHeader.AddressOfEntryPoint;
void* entryPoint = (BYTE*)dosHeader + entryPointRVA;
Wh_SetFunctionHook(entryPoint, (void*)EntryPoint_Hook, nullptr);
return TRUE;
}
if (isToolModProcess) {
return FALSE;
}
g_isToolModProcessLauncher = true;
return TRUE;
}
void Wh_ModAfterInit() {
if (!g_isToolModProcessLauncher) {
return;
}
WCHAR currentProcessPath[MAX_PATH];
switch (GetModuleFileName(nullptr, currentProcessPath,
ARRAYSIZE(currentProcessPath))) {
case 0:
case ARRAYSIZE(currentProcessPath):
Wh_Log(L"GetModuleFileName failed");
return;
}
WCHAR
commandLine[MAX_PATH + 2 +
(sizeof(L" -tool-mod \"" WH_MOD_ID "\"") / sizeof(WCHAR)) - 1];
swprintf_s(commandLine, L"\"%s\" -tool-mod \"%s\"", currentProcessPath,
WH_MOD_ID);
HMODULE kernelModule = GetModuleHandle(L"kernelbase.dll");
if (!kernelModule) {
kernelModule = GetModuleHandle(L"kernel32.dll");
if (!kernelModule) {
Wh_Log(L"No kernelbase.dll/kernel32.dll");
return;
}
}
using CreateProcessInternalW_t = BOOL(WINAPI*)(
HANDLE hUserToken, LPCWSTR lpApplicationName, LPWSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes, WINBOOL bInheritHandles,
DWORD dwCreationFlags, LPVOID lpEnvironment, LPCWSTR lpCurrentDirectory,
LPSTARTUPINFOW lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation,
PHANDLE hRestrictedUserToken);
CreateProcessInternalW_t pCreateProcessInternalW =
(CreateProcessInternalW_t)GetProcAddress(kernelModule,
"CreateProcessInternalW");
if (!pCreateProcessInternalW) {
Wh_Log(L"No CreateProcessInternalW");
return;
}
STARTUPINFO si{
.cb = sizeof(STARTUPINFO),
.dwFlags = STARTF_FORCEOFFFEEDBACK,
};
PROCESS_INFORMATION pi;
if (!pCreateProcessInternalW(nullptr, currentProcessPath, commandLine,
nullptr, nullptr, FALSE, NORMAL_PRIORITY_CLASS,
nullptr, nullptr, &si, &pi, nullptr)) {
Wh_Log(L"CreateProcess failed");
return;
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
void Wh_ModSettingsChanged() {
if (g_isToolModProcessLauncher) {
return;
}
}
void Wh_ModUninit() {
if (g_isToolModProcessLauncher) {
return;
}
WhTool_ModUninit();
ExitProcess(0);
}