diff options
Diffstat (limited to 'src/thmviewer/load.cpp')
-rw-r--r-- | src/thmviewer/load.cpp | 219 |
1 files changed, 219 insertions, 0 deletions
diff --git a/src/thmviewer/load.cpp b/src/thmviewer/load.cpp new file mode 100644 index 00000000..cd48caff --- /dev/null +++ b/src/thmviewer/load.cpp | |||
@@ -0,0 +1,219 @@ | |||
1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. | ||
2 | |||
3 | #include "precomp.h" | ||
4 | |||
5 | struct LOAD_THREAD_CONTEXT | ||
6 | { | ||
7 | HWND hWnd; | ||
8 | LPCWSTR wzThemePath; | ||
9 | LPCWSTR wzWxlPath; | ||
10 | |||
11 | HANDLE hInit; | ||
12 | }; | ||
13 | |||
14 | static DWORD WINAPI LoadThreadProc( | ||
15 | __in LPVOID pvContext | ||
16 | ); | ||
17 | |||
18 | |||
19 | extern "C" HRESULT LoadStart( | ||
20 | __in_z LPCWSTR wzThemePath, | ||
21 | __in_z_opt LPCWSTR wzWxlPath, | ||
22 | __in HWND hWnd, | ||
23 | __out HANDLE* phThread | ||
24 | ) | ||
25 | { | ||
26 | HRESULT hr = S_OK; | ||
27 | HANDLE rgHandles[2] = { }; | ||
28 | LOAD_THREAD_CONTEXT context = { }; | ||
29 | |||
30 | rgHandles[0] = ::CreateEventW(NULL, TRUE, FALSE, NULL); | ||
31 | ExitOnNullWithLastError(rgHandles[0], hr, "Failed to create load init event."); | ||
32 | |||
33 | context.hWnd = hWnd; | ||
34 | context.wzThemePath = wzThemePath; | ||
35 | context.wzWxlPath = wzWxlPath; | ||
36 | context.hInit = rgHandles[0]; | ||
37 | |||
38 | rgHandles[1] = ::CreateThread(NULL, 0, LoadThreadProc, &context, 0, NULL); | ||
39 | ExitOnNullWithLastError(rgHandles[1], hr, "Failed to create load thread."); | ||
40 | |||
41 | ::WaitForMultipleObjects(countof(rgHandles), rgHandles, FALSE, INFINITE); | ||
42 | |||
43 | *phThread = rgHandles[1]; | ||
44 | rgHandles[1] = NULL; | ||
45 | |||
46 | LExit: | ||
47 | ReleaseHandle(rgHandles[1]); | ||
48 | ReleaseHandle(rgHandles[0]); | ||
49 | return hr; | ||
50 | } | ||
51 | |||
52 | |||
53 | static DWORD WINAPI LoadThreadProc( | ||
54 | __in LPVOID pvContext | ||
55 | ) | ||
56 | { | ||
57 | HRESULT hr = S_OK; | ||
58 | WIX_LOCALIZATION* pWixLoc = NULL; | ||
59 | LPWSTR sczThemePath = NULL; | ||
60 | LPWSTR sczWxlPath = NULL; | ||
61 | BOOL fComInitialized = FALSE; | ||
62 | HANDLE hDirectory = INVALID_HANDLE_VALUE; | ||
63 | LPWSTR sczDirectory = NULL; | ||
64 | LPWSTR wzFileName = NULL; | ||
65 | |||
66 | THEME* pTheme = NULL; | ||
67 | HANDLE_THEME* pHandle = NULL; | ||
68 | |||
69 | LOAD_THREAD_CONTEXT* pContext = static_cast<LOAD_THREAD_CONTEXT*>(pvContext); | ||
70 | HWND hWnd = pContext->hWnd; | ||
71 | |||
72 | hr = StrAllocString(&sczThemePath, pContext->wzThemePath, 0); | ||
73 | ExitOnFailure(hr, "Failed to copy path to initial theme file."); | ||
74 | |||
75 | if (pContext->wzWxlPath) | ||
76 | { | ||
77 | hr = StrAllocString(&sczWxlPath, pContext->wzWxlPath, 0); | ||
78 | ExitOnFailure(hr, "Failed to copy .wxl path to initial file."); | ||
79 | } | ||
80 | |||
81 | // We can signal the initialization event as soon as we have copied the context | ||
82 | // values into local variables. | ||
83 | ::SetEvent(pContext->hInit); | ||
84 | |||
85 | hr = ::CoInitialize(NULL); | ||
86 | ExitOnFailure(hr, "Failed to initialize COM on load thread."); | ||
87 | fComInitialized = TRUE; | ||
88 | |||
89 | // Open a handle to the directory so we can put a notification on it. | ||
90 | hr = PathGetDirectory(sczThemePath, &sczDirectory); | ||
91 | ExitOnFailure(hr, "Failed to get path directory."); | ||
92 | |||
93 | wzFileName = PathFile(sczThemePath); | ||
94 | |||
95 | hDirectory = ::CreateFileW(sczDirectory, FILE_LIST_DIRECTORY, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | ||
96 | if (INVALID_HANDLE_VALUE == hDirectory) | ||
97 | { | ||
98 | ExitWithLastError(hr, "Failed to open directory: %ls", sczDirectory); | ||
99 | } | ||
100 | |||
101 | BOOL fUpdated = FALSE; | ||
102 | do | ||
103 | { | ||
104 | // Get the last modified time on the file we're loading for verification that the | ||
105 | // file actually gets changed down below. | ||
106 | FILETIME ftModified = { }; | ||
107 | FileGetTime(sczThemePath, NULL, NULL, &ftModified); | ||
108 | |||
109 | // Try to load the theme file. | ||
110 | hr = ThemeLoadFromFile(sczThemePath, &pTheme); | ||
111 | if (FAILED(hr)) | ||
112 | { | ||
113 | ::SendMessageW(hWnd, WM_THMVWR_THEME_LOAD_ERROR, 0, hr); | ||
114 | } | ||
115 | else | ||
116 | { | ||
117 | if (sczWxlPath) | ||
118 | { | ||
119 | hr = LocLoadFromFile(sczWxlPath, &pWixLoc); | ||
120 | ExitOnFailure(hr, "Failed to load loc file from path: %ls", sczWxlPath); | ||
121 | |||
122 | hr = ThemeLocalize(pTheme, pWixLoc); | ||
123 | ExitOnFailure(hr, "Failed to localize theme: %ls", sczWxlPath); | ||
124 | } | ||
125 | |||
126 | hr = AllocHandleTheme(pTheme, &pHandle); | ||
127 | ExitOnFailure(hr, "Failed to allocate handle to theme"); | ||
128 | |||
129 | ::SendMessageW(hWnd, WM_THMVWR_NEW_THEME, 0, reinterpret_cast<LPARAM>(pHandle)); | ||
130 | pHandle = NULL; | ||
131 | } | ||
132 | |||
133 | fUpdated = FALSE; | ||
134 | do | ||
135 | { | ||
136 | DWORD rgbNotifications[1024]; | ||
137 | DWORD cbRead = 0; | ||
138 | if (!::ReadDirectoryChangesW(hDirectory, rgbNotifications, sizeof(rgbNotifications), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE, &cbRead, NULL, NULL)) | ||
139 | { | ||
140 | ExitWithLastError(hr, "Failed while watching directory: %ls", sczDirectory); | ||
141 | } | ||
142 | |||
143 | // Wait for half a second to let all the file handles get closed to minimize access | ||
144 | // denied errors. | ||
145 | ::Sleep(500); | ||
146 | |||
147 | FILE_NOTIFY_INFORMATION* pNotification = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(rgbNotifications); | ||
148 | while (pNotification) | ||
149 | { | ||
150 | // If our file was updated, check to see if the modified time really changed. The notifications | ||
151 | // are often trigger happy thinking the file changed two or three times in a row. Maybe it's AV | ||
152 | // software creating the problems but actually checking the modified date works well. | ||
153 | if (CSTR_EQUAL == ::CompareStringW(LOCALE_NEUTRAL, NORM_IGNORECASE, pNotification->FileName, pNotification->FileNameLength / sizeof(WCHAR), wzFileName, -1)) | ||
154 | { | ||
155 | FILETIME ft = { }; | ||
156 | FileGetTime(sczThemePath, NULL, NULL, &ft); | ||
157 | |||
158 | fUpdated = (ftModified.dwHighDateTime < ft.dwHighDateTime) || (ftModified.dwHighDateTime == ft.dwHighDateTime && ftModified.dwLowDateTime < ft.dwLowDateTime); | ||
159 | break; | ||
160 | } | ||
161 | |||
162 | pNotification = pNotification->NextEntryOffset ? reinterpret_cast<FILE_NOTIFY_INFORMATION*>(reinterpret_cast<BYTE*>(pNotification) + pNotification->NextEntryOffset) : NULL; | ||
163 | } | ||
164 | } while (!fUpdated); | ||
165 | } while(fUpdated); | ||
166 | |||
167 | LExit: | ||
168 | if (fComInitialized) | ||
169 | { | ||
170 | ::CoUninitialize(); | ||
171 | } | ||
172 | |||
173 | LocFree(pWixLoc); | ||
174 | ReleaseFileHandle(hDirectory); | ||
175 | ReleaseStr(sczDirectory); | ||
176 | ReleaseStr(sczThemePath); | ||
177 | ReleaseStr(sczWxlPath); | ||
178 | return hr; | ||
179 | } | ||
180 | |||
181 | extern "C" HRESULT AllocHandleTheme( | ||
182 | __in THEME* pTheme, | ||
183 | __out HANDLE_THEME** ppHandle | ||
184 | ) | ||
185 | { | ||
186 | HRESULT hr = S_OK; | ||
187 | HANDLE_THEME* pHandle = NULL; | ||
188 | |||
189 | pHandle = static_cast<HANDLE_THEME*>(MemAlloc(sizeof(HANDLE_THEME), TRUE)); | ||
190 | ExitOnNull(pHandle, hr, E_OUTOFMEMORY, "Failed to allocate theme handle."); | ||
191 | |||
192 | pHandle->cReferences = 1; | ||
193 | pHandle->pTheme = pTheme; | ||
194 | |||
195 | *ppHandle = pHandle; | ||
196 | pHandle = NULL; | ||
197 | |||
198 | LExit: | ||
199 | ReleaseMem(pHandle); | ||
200 | return hr; | ||
201 | } | ||
202 | |||
203 | extern "C" void IncrementHandleTheme( | ||
204 | __in HANDLE_THEME* pHandle | ||
205 | ) | ||
206 | { | ||
207 | ::InterlockedIncrement(reinterpret_cast<LONG*>(&pHandle->cReferences)); | ||
208 | } | ||
209 | |||
210 | extern "C" void DecrementHandleTheme( | ||
211 | __in HANDLE_THEME* pHandle | ||
212 | ) | ||
213 | { | ||
214 | if (pHandle && 0 == ::InterlockedDecrement(reinterpret_cast<LONG*>(&pHandle->cReferences))) | ||
215 | { | ||
216 | ThemeFree(pHandle->pTheme); | ||
217 | MemFree(pHandle); | ||
218 | } | ||
219 | } | ||