dolphin/Source/Core/WinUpdater/WinUI.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

309 lines
7.8 KiB
C++
Raw Normal View History

2018-03-28 00:28:40 +02:00
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-03-28 00:28:40 +02:00
#include "UpdaterCommon/UI.h"
2018-03-28 00:28:40 +02:00
#include <cstdlib>
#include <string>
#include <thread>
#include <Windows.h>
2018-03-28 00:28:40 +02:00
#include <CommCtrl.h>
2018-11-08 11:54:56 +01:00
#include <ShObjIdl.h>
#include <ShlObj.h>
#include <shellapi.h>
#include <wrl/client.h>
2018-03-28 00:28:40 +02:00
#include "Common/Event.h"
#include "Common/ScopeGuard.h"
2018-03-28 00:28:40 +02:00
#include "Common/StringUtil.h"
namespace
{
HWND window_handle = nullptr;
HWND label_handle = nullptr;
2018-11-15 09:01:29 +01:00
HWND total_progressbar_handle = nullptr;
HWND current_progressbar_handle = nullptr;
Microsoft::WRL::ComPtr<ITaskbarList3> taskbar_list;
2018-03-28 00:28:40 +02:00
std::thread ui_thread;
Common::Event window_created_event;
2019-03-03 12:56:26 +01:00
int GetWindowHeight(HWND hwnd)
{
RECT rect;
GetWindowRect(hwnd, &rect);
return rect.bottom - rect.top;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
2018-03-28 00:28:40 +02:00
}; // namespace
constexpr int PROGRESSBAR_FLAGS = WS_VISIBLE | WS_CHILD | PBS_SMOOTH | PBS_SMOOTHREVERSE;
constexpr int WINDOW_FLAGS = WS_CLIPCHILDREN;
2019-03-03 12:56:26 +01:00
constexpr int PADDING_HEIGHT = 5;
2018-03-28 00:28:40 +02:00
namespace UI
{
bool InitWindow()
2018-03-28 00:28:40 +02:00
{
InitCommonControls();
// Notify main thread we're done creating the window when we return
Common::ScopeGuard ui_guard{[] { window_created_event.Set(); }};
2018-03-28 00:28:40 +02:00
WNDCLASS wndcl = {};
wndcl.lpfnWndProc = WindowProc;
2018-03-28 00:28:40 +02:00
wndcl.hbrBackground = GetSysColorBrush(COLOR_MENU);
wndcl.lpszClassName = L"UPDATER";
if (!RegisterClass(&wndcl))
return false;
window_handle =
2019-03-03 12:56:26 +01:00
CreateWindow(L"UPDATER", L"Dolphin Updater", WINDOW_FLAGS, CW_USEDEFAULT, CW_USEDEFAULT, 500,
100, nullptr, nullptr, GetModuleHandle(nullptr), 0);
2018-03-28 00:28:40 +02:00
if (!window_handle)
return false;
2023-04-15 06:55:53 +02:00
if (SUCCEEDED(CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(taskbar_list.GetAddressOf()))))
{
if (FAILED(taskbar_list->HrInit()))
{
taskbar_list.Reset();
}
}
2018-11-08 11:54:56 +01:00
2019-03-03 12:56:26 +01:00
int y = PADDING_HEIGHT;
2023-04-15 06:55:53 +02:00
label_handle = CreateWindow(L"STATIC", nullptr, WS_VISIBLE | WS_CHILD, 5, y, 500, 25,
window_handle, nullptr, nullptr, 0);
2018-03-28 00:28:40 +02:00
if (!label_handle)
return false;
// Get the default system font
NONCLIENTMETRICS metrics = {};
metrics.cbSize = sizeof(NONCLIENTMETRICS);
if (!SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(metrics), &metrics, 0))
return false;
SendMessage(label_handle, WM_SETFONT,
reinterpret_cast<WPARAM>(CreateFontIndirect(&metrics.lfMessageFont)), 0);
2019-03-03 12:56:26 +01:00
y += GetWindowHeight(label_handle) + PADDING_HEIGHT;
2023-04-15 06:55:53 +02:00
total_progressbar_handle = CreateWindow(PROGRESS_CLASS, nullptr, PROGRESSBAR_FLAGS, 5, y, 470, 25,
2018-11-15 09:01:29 +01:00
window_handle, nullptr, nullptr, 0);
2018-03-28 00:28:40 +02:00
2019-03-03 12:56:26 +01:00
y += GetWindowHeight(total_progressbar_handle) + PADDING_HEIGHT;
2018-11-15 09:01:29 +01:00
if (!total_progressbar_handle)
return false;
2023-04-15 06:55:53 +02:00
current_progressbar_handle = CreateWindow(PROGRESS_CLASS, nullptr, PROGRESSBAR_FLAGS, 5, y, 470,
25, window_handle, nullptr, nullptr, 0);
2018-11-15 09:01:29 +01:00
2019-03-03 12:56:26 +01:00
y += GetWindowHeight(current_progressbar_handle) + PADDING_HEIGHT;
2018-11-15 09:01:29 +01:00
if (!current_progressbar_handle)
2018-03-28 00:28:40 +02:00
return false;
2019-03-03 12:56:26 +01:00
RECT rect;
GetWindowRect(window_handle, &rect);
// Account for the title bar
y += GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION) +
GetSystemMetrics(SM_CXPADDEDBORDER);
// ...and window border
y += GetSystemMetrics(SM_CYBORDER);
// Add some padding for good measure
y += PADDING_HEIGHT * 3;
SetWindowPos(window_handle, HWND_TOPMOST, 0, 0, rect.right - rect.left, y, SWP_NOMOVE);
2018-03-28 00:28:40 +02:00
return true;
}
2018-11-15 09:01:29 +01:00
void SetTotalMarquee(bool marquee)
2018-03-28 00:28:40 +02:00
{
2018-11-15 09:01:29 +01:00
SetWindowLong(total_progressbar_handle, GWL_STYLE,
PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0));
SendMessage(total_progressbar_handle, PBM_SETMARQUEE, marquee, 0);
if (taskbar_list)
{
taskbar_list->SetProgressState(window_handle, marquee ? TBPF_INDETERMINATE : TBPF_NORMAL);
}
2018-03-28 00:28:40 +02:00
}
2018-11-15 09:01:29 +01:00
void ResetTotalProgress()
{
SendMessage(total_progressbar_handle, PBM_SETPOS, 0, 0);
SetCurrentMarquee(true);
}
void SetTotalProgress(int current, int total)
{
SendMessage(total_progressbar_handle, PBM_SETRANGE32, 0, total);
SendMessage(total_progressbar_handle, PBM_SETPOS, current, 0);
if (taskbar_list)
{
taskbar_list->SetProgressValue(window_handle, current, total);
}
2018-11-15 09:01:29 +01:00
}
void SetCurrentMarquee(bool marquee)
2018-03-28 00:28:40 +02:00
{
2018-11-15 09:01:29 +01:00
SetWindowLong(current_progressbar_handle, GWL_STYLE,
PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0));
SendMessage(current_progressbar_handle, PBM_SETMARQUEE, marquee, 0);
}
void ResetCurrentProgress()
{
SendMessage(current_progressbar_handle, PBM_SETPOS, 0, 0);
SetCurrentMarquee(true);
2018-03-28 00:28:40 +02:00
}
2018-11-08 11:54:56 +01:00
void Error(const std::string& text)
2018-03-28 00:28:40 +02:00
{
auto message = L"A fatal error occurred and the updater cannot continue:\n " +
UTF8ToWString(text) + L"\n" +
L"If the issue persists, please manually download the latest version from "
L"dolphin-emu.org/download and extract it overtop your existing installation.\n" +
L"Also consider filing a bug at bugs.dolphin-emu.org/projects/emulator";
2018-11-08 11:54:56 +01:00
MessageBox(nullptr, message.c_str(), L"Error", MB_ICONERROR);
2018-11-08 11:54:56 +01:00
if (taskbar_list)
{
taskbar_list->SetProgressState(window_handle, TBPF_ERROR);
}
2018-03-28 00:28:40 +02:00
}
2018-11-15 09:01:29 +01:00
void SetCurrentProgress(int current, int total)
2018-03-28 00:28:40 +02:00
{
2018-11-15 09:01:29 +01:00
SendMessage(current_progressbar_handle, PBM_SETRANGE32, 0, total);
SendMessage(current_progressbar_handle, PBM_SETPOS, current, 0);
2018-03-28 00:28:40 +02:00
}
void SetDescription(const std::string& text)
{
SetWindowText(label_handle, UTF8ToWString(text).c_str());
2018-03-28 00:28:40 +02:00
}
void MessageLoop()
{
HRESULT result = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Common::ScopeGuard ui_guard{[result] {
taskbar_list.Reset();
if (SUCCEEDED(result))
CoUninitialize();
}};
if (!InitWindow())
2018-03-28 00:28:40 +02:00
{
MessageBox(nullptr, L"Window init failed!", L"", MB_ICONERROR);
// Destroying the parent (if exists) destroys all children windows
if (window_handle)
{
DestroyWindow(window_handle);
window_handle = nullptr;
}
return;
2018-03-28 00:28:40 +02:00
}
2018-11-15 09:01:29 +01:00
SetTotalMarquee(true);
SetCurrentMarquee(true);
2018-03-28 00:28:40 +02:00
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0))
2018-03-28 00:28:40 +02:00
{
TranslateMessage(&msg);
DispatchMessage(&msg);
2018-03-28 00:28:40 +02:00
}
}
void Init()
{
ui_thread = std::thread(MessageLoop);
// Wait for UI thread to finish creating the window (or at least attempting to)
window_created_event.Wait();
}
2018-03-28 00:28:40 +02:00
void Stop()
{
if (window_handle)
PostMessage(window_handle, WM_CLOSE, 0, 0);
2018-03-28 00:28:40 +02:00
ui_thread.join();
2018-03-28 00:28:40 +02:00
}
bool IsTestMode()
{
return std::getenv("DOLPHIN_UPDATE_SERVER_URL") != nullptr;
}
void LaunchApplication(std::string path)
{
const auto wpath = UTF8ToWString(path);
if (IsUserAnAdmin())
{
// Indirectly start the application via explorer. This effectively drops admin privileges
// because explorer is running as current user.
ShellExecuteW(nullptr, nullptr, L"explorer.exe", wpath.c_str(), nullptr, SW_SHOW);
}
else
{
std::wstring cmdline = L"\"" + wpath + L"\"";
STARTUPINFOW startup_info{.cb = sizeof(startup_info)};
PROCESS_INFORMATION process_info;
if (IsTestMode())
SetEnvironmentVariableA("DOLPHIN_UPDATE_TEST_DONE", "1");
if (CreateProcessW(wpath.c_str(), cmdline.data(), nullptr, nullptr, TRUE, 0, nullptr, nullptr,
&startup_info, &process_info))
{
CloseHandle(process_info.hThread);
CloseHandle(process_info.hProcess);
}
}
}
void Sleep(int sleep)
{
::Sleep(sleep * 1000);
}
void WaitForPID(u32 pid)
{
HANDLE parent_handle = OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid));
if (parent_handle)
{
WaitForSingleObject(parent_handle, INFINITE);
CloseHandle(parent_handle);
}
}
void SetVisible(bool visible)
{
ShowWindow(window_handle, visible ? SW_SHOW : SW_HIDE);
}
2018-03-28 00:28:40 +02:00
}; // namespace UI