2018-03-27 16:28:40 -06:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2019-02-25 15:08:03 -07:00
|
|
|
#include "UpdaterCommon/UI.h"
|
2018-03-27 16:28:40 -06:00
|
|
|
|
2019-02-25 15:08:03 -07:00
|
|
|
#include <string>
|
2019-03-03 05:56:54 -07:00
|
|
|
#include <thread>
|
2019-02-25 15:08:03 -07:00
|
|
|
|
|
|
|
#include <Windows.h>
|
2018-03-27 16:28:40 -06:00
|
|
|
#include <CommCtrl.h>
|
2018-11-08 03:54:56 -07:00
|
|
|
#include <ShObjIdl.h>
|
2019-03-03 05:56:54 -07:00
|
|
|
#include <shellapi.h>
|
2018-03-27 16:28:40 -06:00
|
|
|
|
|
|
|
#include "Common/Flag.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
HWND window_handle = nullptr;
|
|
|
|
HWND label_handle = nullptr;
|
2018-11-15 01:01:29 -07:00
|
|
|
HWND total_progressbar_handle = nullptr;
|
|
|
|
HWND current_progressbar_handle = nullptr;
|
2018-11-08 03:54:56 -07:00
|
|
|
ITaskbarList3* taskbar_list = nullptr;
|
2018-03-27 16:28:40 -06:00
|
|
|
|
|
|
|
Common::Flag running;
|
|
|
|
Common::Flag request_stop;
|
2019-03-03 04:56:26 -07:00
|
|
|
|
|
|
|
int GetWindowHeight(HWND hwnd)
|
|
|
|
{
|
|
|
|
RECT rect;
|
|
|
|
GetWindowRect(hwnd, &rect);
|
|
|
|
|
|
|
|
return rect.bottom - rect.top;
|
|
|
|
}
|
2018-03-27 16:28:40 -06:00
|
|
|
}; // namespace
|
|
|
|
|
|
|
|
constexpr int PROGRESSBAR_FLAGS = WS_VISIBLE | WS_CHILD | PBS_SMOOTH | PBS_SMOOTHREVERSE;
|
2019-03-03 04:56:26 -07:00
|
|
|
constexpr int WINDOW_FLAGS = WS_VISIBLE | WS_CLIPCHILDREN;
|
|
|
|
constexpr int PADDING_HEIGHT = 5;
|
2018-03-27 16:28:40 -06:00
|
|
|
|
|
|
|
namespace UI
|
|
|
|
{
|
2019-03-03 05:56:54 -07:00
|
|
|
bool InitWindow()
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
|
|
|
InitCommonControls();
|
|
|
|
|
|
|
|
WNDCLASS wndcl = {};
|
|
|
|
wndcl.lpfnWndProc = DefWindowProcW;
|
|
|
|
wndcl.hbrBackground = GetSysColorBrush(COLOR_MENU);
|
|
|
|
wndcl.lpszClassName = L"UPDATER";
|
|
|
|
|
|
|
|
if (!RegisterClass(&wndcl))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
window_handle =
|
2019-03-03 04:56:26 -07:00
|
|
|
CreateWindow(L"UPDATER", L"Dolphin Updater", WINDOW_FLAGS, CW_USEDEFAULT, CW_USEDEFAULT, 500,
|
|
|
|
100, nullptr, nullptr, GetModuleHandle(nullptr), 0);
|
2018-03-27 16:28:40 -06:00
|
|
|
|
|
|
|
if (!window_handle)
|
|
|
|
return false;
|
|
|
|
|
2018-11-21 18:34:26 -07:00
|
|
|
if (FAILED(CoCreateInstance(CLSID_TaskbarList, NULL, CLSCTX_INPROC_SERVER,
|
|
|
|
IID_PPV_ARGS(&taskbar_list))))
|
|
|
|
{
|
|
|
|
taskbar_list = nullptr;
|
|
|
|
}
|
|
|
|
if (taskbar_list && FAILED(taskbar_list->HrInit()))
|
|
|
|
{
|
|
|
|
taskbar_list->Release();
|
|
|
|
taskbar_list = nullptr;
|
|
|
|
}
|
2018-11-08 03:54:56 -07:00
|
|
|
|
2019-03-03 04:56:26 -07:00
|
|
|
int y = PADDING_HEIGHT;
|
|
|
|
|
|
|
|
label_handle = CreateWindow(L"STATIC", NULL, WS_VISIBLE | WS_CHILD, 5, y, 500, 25, window_handle,
|
2018-03-27 16:28:40 -06:00
|
|
|
nullptr, nullptr, 0);
|
|
|
|
|
|
|
|
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 04:56:26 -07:00
|
|
|
y += GetWindowHeight(label_handle) + PADDING_HEIGHT;
|
|
|
|
|
|
|
|
total_progressbar_handle = CreateWindow(PROGRESS_CLASS, NULL, PROGRESSBAR_FLAGS, 5, y, 470, 25,
|
2018-11-15 01:01:29 -07:00
|
|
|
window_handle, nullptr, nullptr, 0);
|
2018-03-27 16:28:40 -06:00
|
|
|
|
2019-03-03 04:56:26 -07:00
|
|
|
y += GetWindowHeight(total_progressbar_handle) + PADDING_HEIGHT;
|
|
|
|
|
2018-11-15 01:01:29 -07:00
|
|
|
if (!total_progressbar_handle)
|
|
|
|
return false;
|
|
|
|
|
2019-03-03 04:56:26 -07:00
|
|
|
current_progressbar_handle = CreateWindow(PROGRESS_CLASS, NULL, PROGRESSBAR_FLAGS, 5, y, 470, 25,
|
2018-11-15 01:01:29 -07:00
|
|
|
window_handle, nullptr, nullptr, 0);
|
|
|
|
|
2019-03-03 04:56:26 -07:00
|
|
|
y += GetWindowHeight(current_progressbar_handle) + PADDING_HEIGHT;
|
|
|
|
|
2018-11-15 01:01:29 -07:00
|
|
|
if (!current_progressbar_handle)
|
2018-03-27 16:28:40 -06:00
|
|
|
return false;
|
|
|
|
|
2019-03-03 04:56:26 -07: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-27 16:28:40 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
DestroyWindow(window_handle);
|
|
|
|
DestroyWindow(label_handle);
|
2018-11-15 01:01:29 -07:00
|
|
|
DestroyWindow(total_progressbar_handle);
|
|
|
|
DestroyWindow(current_progressbar_handle);
|
2018-03-27 16:28:40 -06:00
|
|
|
}
|
|
|
|
|
2018-11-15 01:01:29 -07:00
|
|
|
void SetTotalMarquee(bool marquee)
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
2018-11-15 01:01:29 -07:00
|
|
|
SetWindowLong(total_progressbar_handle, GWL_STYLE,
|
|
|
|
PROGRESSBAR_FLAGS | (marquee ? PBS_MARQUEE : 0));
|
|
|
|
SendMessage(total_progressbar_handle, PBM_SETMARQUEE, marquee, 0);
|
2018-11-21 18:34:26 -07:00
|
|
|
if (taskbar_list)
|
|
|
|
{
|
|
|
|
taskbar_list->SetProgressState(window_handle, marquee ? TBPF_INDETERMINATE : TBPF_NORMAL);
|
|
|
|
}
|
2018-03-27 16:28:40 -06:00
|
|
|
}
|
|
|
|
|
2018-11-15 01:01:29 -07: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);
|
2018-11-21 18:34:26 -07:00
|
|
|
if (taskbar_list)
|
|
|
|
{
|
|
|
|
taskbar_list->SetProgressValue(window_handle, current, total);
|
|
|
|
}
|
2018-11-15 01:01:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetCurrentMarquee(bool marquee)
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
2018-11-15 01:01:29 -07: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-27 16:28:40 -06:00
|
|
|
}
|
|
|
|
|
2018-11-08 03:54:56 -07:00
|
|
|
void Error(const std::string& text)
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
2018-11-08 03:54:56 -07:00
|
|
|
auto wide_text = UTF8ToUTF16(text);
|
|
|
|
|
|
|
|
MessageBox(nullptr,
|
|
|
|
(L"A fatal error occured and the updater cannot continue:\n " + wide_text).c_str(),
|
|
|
|
L"Error", MB_ICONERROR);
|
|
|
|
|
2018-11-21 18:34:26 -07:00
|
|
|
if (taskbar_list)
|
|
|
|
{
|
|
|
|
taskbar_list->SetProgressState(window_handle, TBPF_ERROR);
|
|
|
|
}
|
2018-03-27 16:28:40 -06:00
|
|
|
}
|
|
|
|
|
2018-11-15 01:01:29 -07:00
|
|
|
void SetCurrentProgress(int current, int total)
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
2018-11-15 01:01:29 -07:00
|
|
|
SendMessage(current_progressbar_handle, PBM_SETRANGE32, 0, total);
|
|
|
|
SendMessage(current_progressbar_handle, PBM_SETPOS, current, 0);
|
2018-03-27 16:28:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetDescription(const std::string& text)
|
|
|
|
{
|
|
|
|
SetWindowText(label_handle, UTF8ToUTF16(text).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageLoop()
|
|
|
|
{
|
|
|
|
request_stop.Clear();
|
|
|
|
running.Set();
|
|
|
|
|
2019-03-03 05:56:54 -07:00
|
|
|
if (!InitWindow())
|
2018-03-27 16:28:40 -06:00
|
|
|
{
|
|
|
|
running.Clear();
|
|
|
|
MessageBox(nullptr, L"Window init failed!", L"", MB_ICONERROR);
|
|
|
|
}
|
|
|
|
|
2018-11-15 01:01:29 -07:00
|
|
|
SetTotalMarquee(true);
|
|
|
|
SetCurrentMarquee(true);
|
2018-03-27 16:28:40 -06:00
|
|
|
|
|
|
|
while (!request_stop.IsSet())
|
|
|
|
{
|
|
|
|
MSG msg;
|
|
|
|
while (PeekMessage(&msg, window_handle, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
running.Clear();
|
|
|
|
|
|
|
|
Destroy();
|
|
|
|
}
|
|
|
|
|
2019-03-03 05:56:54 -07:00
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
std::thread thread(MessageLoop);
|
|
|
|
thread.detach();
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:28:40 -06:00
|
|
|
void Stop()
|
|
|
|
{
|
|
|
|
if (!running.IsSet())
|
|
|
|
return;
|
|
|
|
|
|
|
|
request_stop.Set();
|
|
|
|
|
|
|
|
while (running.IsSet())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
2019-03-03 05:56:54 -07:00
|
|
|
|
|
|
|
void LaunchApplication(std::string path)
|
|
|
|
{
|
|
|
|
// Hack: Launching the updater over the explorer ensures that admin priviliges are dropped. Why?
|
|
|
|
// Ask Microsoft.
|
|
|
|
ShellExecuteW(nullptr, nullptr, L"explorer.exe", UTF8ToUTF16(path).c_str(), nullptr, SW_SHOW);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sleep(int sleep)
|
|
|
|
{
|
|
|
|
::Sleep(sleep * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WaitForPID(u32 pid)
|
|
|
|
{
|
|
|
|
HANDLE parent_handle = OpenProcess(SYNCHRONIZE, FALSE, static_cast<DWORD>(pid));
|
|
|
|
WaitForSingleObject(parent_handle, INFINITE);
|
|
|
|
CloseHandle(parent_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetVisible(bool visible)
|
|
|
|
{
|
|
|
|
ShowWindow(window_handle, visible ? SW_SHOW : SW_HIDE);
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:28:40 -06:00
|
|
|
}; // namespace UI
|