| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UpdaterCommon/UI.h" |
| 5 | |
| 6 | #include <cstdlib> |
| 7 | #include <string> |
| 8 | #include <thread> |
| 9 | |
| 10 | #include <Windows.h> |
| 11 | #include <CommCtrl.h> |
| 12 | #include <ShObjIdl.h> |
| 13 | #include <ShlObj.h> |
| 14 | #include <shellapi.h> |
| 15 | #include <wrl/client.h> |
| 16 | |
| 17 | #include "Common/Event.h" |
| 18 | #include "Common/ScopeGuard.h" |
| 19 | #include "Common/StringUtil.h" |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | HWND window_handle = nullptr; |
| 24 | HWND label_handle = nullptr; |
| 25 | HWND total_progressbar_handle = nullptr; |
| 26 | HWND current_progressbar_handle = nullptr; |
| 27 | Microsoft::WRL::ComPtr<ITaskbarList3> taskbar_list; |
| 28 | |
| 29 | std::thread ui_thread; |
| 30 | Common::Event window_created_event; |
| 31 | |
| 32 | int GetWindowHeight(HWND hwnd) |
| 33 | { |
| 34 | RECT rect; |
| 35 | GetWindowRect(hwnd, &rect); |
| 36 | |
| 37 | return rect.bottom - rect.top; |
| 38 | } |
| 39 | |
| 40 | LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) |
| 41 | { |
| 42 | switch (uMsg) |
| 43 | { |
| 44 | case WM_DESTROY: |
| 45 | PostQuitMessage(0); |
| 46 | return 0; |
| 47 | } |
| 48 | return DefWindowProc(hwnd, uMsg, wParam, lParam); |
| 49 | } |
| 50 | } // namespace |
| 51 | |
| 52 | constexpr int PROGRESSBAR_FLAGS = WS_VISIBLE | WS_CHILD | PBS_SMOOTH | PBS_SMOOTHREVERSE; |
| 53 | constexpr int WINDOW_FLAGS = WS_CLIPCHILDREN; |
| 54 | constexpr int PADDING_HEIGHT = 5; |
| 55 | |
| 56 | namespace UI |
| 57 | { |
| 58 | static bool InitWindow() |
| 59 | { |
| 60 | InitCommonControls(); |
| 61 | |
| 62 | // Notify main thread we're done creating the window when we return |
| 63 | Common::ScopeGuard ui_guard{[] { window_created_event.Set(); }}; |
| 64 | |
| 65 | WNDCLASS wndcl = {}; |
| 66 | wndcl.lpfnWndProc = WindowProc; |
| 67 | wndcl.hbrBackground = GetSysColorBrush(COLOR_MENU); |
| 68 | wndcl.lpszClassName = L"UPDATER"; |
| 69 | |
| 70 | if (!RegisterClass(&wndcl)) |
| 71 | return false; |
| 72 | |
| 73 | window_handle = |
| 74 | CreateWindow(L"UPDATER", L"Dolphin Updater", WINDOW_FLAGS, CW_USEDEFAULT, CW_USEDEFAULT, 500, |
| 75 | 100, nullptr, nullptr, GetModuleHandle(nullptr), 0); |
| 76 | |
| 77 | if (!window_handle) |
| 78 | return false; |
| 79 | |
| 80 | if (SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER, |
| 81 | IID_PPV_ARGS(&taskbar_list)))) |
| 82 | { |
| 83 | if (FAILED(taskbar_list->HrInit())) |
| 84 | { |
| 85 | taskbar_list.Reset(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | int y = PADDING_HEIGHT; |
| 90 | |
| 91 | label_handle = CreateWindow(L"STATIC", nullptr, WS_VISIBLE | WS_CHILD, 5, y, 500, 25, |
| 92 | window_handle, nullptr, nullptr, 0); |
| 93 | |
| 94 | if (!label_handle) |
| 95 | return false; |
| 96 | |
| 97 | // Get the default system font |
| 98 | NONCLIENTMETRICS metrics = {}; |
| 99 | metrics.cbSize = sizeof(NONCLIENTMETRICS); |
| 100 | |
| 101 | if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0)) |
| 102 | return false; |
| 103 | |
| 104 | SendMessage(label_handle, WM_SETFONT, |
| 105 | reinterpret_cast<WPARAM>(CreateFontIndirect(&metrics.lfMessageFont)), 0); |
| 106 | |
| 107 | y += GetWindowHeight(label_handle) + PADDING_HEIGHT; |
| 108 | |
| 109 | total_progressbar_handle = CreateWindow(PROGRESS_CLASS, nullptr, PROGRESSBAR_FLAGS, 5, y, 470, 25, |
| 110 | window_handle, nullptr, nullptr, 0); |
| 111 | |
| 112 | y += GetWindowHeight(total_progressbar_handle) + PADDING_HEIGHT; |
| 113 | |
| 114 | if (!total_progressbar_handle) |
| 115 | return false; |
| 116 | |
| 117 | current_progressbar_handle = CreateWindow(PROGRESS_CLASS, nullptr, PROGRESSBAR_FLAGS, 5, y, 470, |
| 118 | 25, window_handle, nullptr, nullptr, 0); |
| 119 | |
| 120 | y += GetWindowHeight(current_progressbar_handle) + PADDING_HEIGHT; |
| 121 | |
| 122 | if (!current_progressbar_handle) |
| 123 | return false; |
| 124 | |
| 125 | RECT rect; |
| 126 | GetWindowRect(window_handle, &rect); |
| 127 | |
| 128 | // Account for the title bar |
| 129 | y += GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION) + |
| 130 | GetSystemMetrics(SM_CXPADDEDBORDER); |
| 131 | // ...and window border |
| 132 | y += GetSystemMetrics(SM_CYBORDER); |
| 133 | |
| 134 | // Add some padding for good measure |
| 135 | y += PADDING_HEIGHT * 3; |
| 136 | |
| 137 | SetWindowPos(window_handle, HWND_TOPMOST, 0, 0, rect.right - rect.left, y, SWP_NOMOVE); |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | void SetTotalMarquee(bool marquee) |
| 143 | { |
| 144 | SetWindowLong(total_progressbar_handle, GWL_STYLE, |
| 145 | PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0)); |
| 146 | SendMessage(total_progressbar_handle, PBM_SETMARQUEE, marquee, 0); |
| 147 | if (taskbar_list) |
| 148 | { |
| 149 | taskbar_list->SetProgressState(window_handle, marquee ? TBPF_INDETERMINATE : TBPF_NORMAL); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ResetTotalProgress() |
| 154 | { |
| 155 | SendMessage(total_progressbar_handle, PBM_SETPOS, 0, 0); |
| 156 | SetCurrentMarquee(true); |
| 157 | } |
| 158 | |
| 159 | void SetTotalProgress(int current, int total) |
| 160 | { |
| 161 | SendMessage(total_progressbar_handle, PBM_SETRANGE32, 0, total); |
| 162 | SendMessage(total_progressbar_handle, PBM_SETPOS, current, 0); |
| 163 | if (taskbar_list) |
| 164 | { |
| 165 | taskbar_list->SetProgressValue(window_handle, current, total); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void SetCurrentMarquee(bool marquee) |
| 170 | { |
| 171 | SetWindowLong(current_progressbar_handle, GWL_STYLE, |
| 172 | PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0)); |
| 173 | SendMessage(current_progressbar_handle, PBM_SETMARQUEE, marquee, 0); |
| 174 | } |
| 175 | |
| 176 | void ResetCurrentProgress() |
| 177 | { |
| 178 | SendMessage(current_progressbar_handle, PBM_SETPOS, 0, 0); |
| 179 | SetCurrentMarquee(true); |
| 180 | } |
| 181 | |
| 182 | void Error(const std::string& text) |
| 183 | { |
| 184 | auto message = L"A fatal error occurred and the updater cannot continue:\n " + |
| 185 | UTF8ToWString(text) + L"\n" + |
| 186 | L"If the issue persists, please manually download the latest version from " |
| 187 | L"dolphin-emu.org/download and extract it overtop your existing installation.\n" + |
| 188 | L"Also consider filing a bug at bugs.dolphin-emu.org/projects/emulator"; |
| 189 | |
| 190 | MessageBox(nullptr, message.c_str(), L"Error", MB_ICONERROR); |
| 191 | |
| 192 | if (taskbar_list) |
| 193 | { |
| 194 | taskbar_list->SetProgressState(window_handle, TBPF_ERROR); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | void SetCurrentProgress(int current, int total) |
| 199 | { |
| 200 | SendMessage(current_progressbar_handle, PBM_SETRANGE32, 0, total); |
| 201 | SendMessage(current_progressbar_handle, PBM_SETPOS, current, 0); |
| 202 | } |
| 203 | |
| 204 | void SetDescription(const std::string& text) |
| 205 | { |
| 206 | SetWindowText(label_handle, UTF8ToWString(text).c_str()); |
| 207 | } |
| 208 | |
| 209 | static void MessageLoop() |
| 210 | { |
| 211 | HRESULT result = CoInitializeEx(nullptr, COINIT_MULTITHREADED); |
| 212 | |
| 213 | Common::ScopeGuard ui_guard{[result] { |
| 214 | taskbar_list.Reset(); |
| 215 | if (SUCCEEDED(result)) |
| 216 | CoUninitialize(); |
| 217 | }}; |
| 218 | |
| 219 | if (!InitWindow()) |
| 220 | { |
| 221 | MessageBox(nullptr, L"Window init failed!", L"", MB_ICONERROR); |
| 222 | // Destroying the parent (if exists) destroys all children windows |
| 223 | if (window_handle) |
| 224 | { |
| 225 | DestroyWindow(window_handle); |
| 226 | window_handle = nullptr; |
| 227 | } |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | SetTotalMarquee(true); |
| 232 | SetCurrentMarquee(true); |
| 233 | |
| 234 | MSG msg; |
| 235 | while (GetMessage(&msg, nullptr, 0, 0)) |
| 236 | { |
| 237 | TranslateMessage(&msg); |
| 238 | DispatchMessage(&msg); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void Init() |
| 243 | { |
| 244 | ui_thread = std::thread(MessageLoop); |
| 245 | |
| 246 | // Wait for UI thread to finish creating the window (or at least attempting to) |
| 247 | window_created_event.Wait(); |
| 248 | } |
| 249 | |
| 250 | void Stop() |
| 251 | { |
| 252 | if (window_handle) |
| 253 | PostMessage(window_handle, WM_CLOSE, 0, 0); |
| 254 | |
| 255 | ui_thread.join(); |
| 256 | } |
| 257 | |
| 258 | bool IsTestMode() |
| 259 | { |
| 260 | return std::getenv("DOLPHIN_UPDATE_SERVER_URL") != nullptr; |
| 261 | } |
| 262 | |
| 263 | void LaunchApplication(std::string path) |
| 264 | { |
| 265 | const auto wpath = UTF8ToWString(path); |
| 266 | if (IsUserAnAdmin()) |
| 267 | { |
| 268 | // Indirectly start the application via explorer. This effectively drops admin privileges |
| 269 | // because explorer is running as current user. |
| 270 | ShellExecuteW(nullptr, nullptr, L"explorer.exe", wpath.c_str(), nullptr, SW_SHOW); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | std::wstring cmdline = L"\"" + wpath + L"\""; |
| 275 | STARTUPINFOW startup_info{.cb = sizeof(startup_info)}; |
| 276 | PROCESS_INFORMATION process_info; |
| 277 | if (IsTestMode()) |
| 278 | SetEnvironmentVariableA("DOLPHIN_UPDATE_TEST_DONE", "1"); |
| 279 | if (CreateProcessW(wpath.c_str(), cmdline.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr, |
| 280 | &startup_info, &process_info)) |
| 281 | { |
| 282 | CloseHandle(process_info.hThread); |
| 283 | CloseHandle(process_info.hProcess); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void Sleep(int sleep) |
| 289 | { |
| 290 | ::Sleep(sleep * 1000); |
| 291 | } |
| 292 | |
| 293 | void WaitForPID(u32 pid) |
| 294 | { |
| 295 | HANDLE parent_handle = OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid)); |
| 296 | if (parent_handle) |
| 297 | { |
| 298 | WaitForSingleObject(parent_handle, INFINITE); |
| 299 | CloseHandle(parent_handle); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void SetVisible(bool visible) |
| 304 | { |
| 305 | ShowWindow(window_handle, visible ? SW_SHOW : SW_HIDE); |
| 306 | } |
| 307 | |
| 308 | } // namespace UI |