144 lines
7.5 KiB
C++
144 lines
7.5 KiB
C++
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
#include <windows.h>
|
|
#include <uiautomation.h>
|
|
#include <cstdio>
|
|
|
|
static HWND window;
|
|
static int focusedIndex = 1, selectedIndex = 3, step = 0;
|
|
static ToggleState checkboxState = ToggleState_Off;
|
|
static bool checkboxEnabled = true;
|
|
|
|
class Provider : public IRawElementProviderSimple,
|
|
public IRawElementProviderFragment, public IRawElementProviderFragmentRoot, public IToggleProvider {
|
|
public:
|
|
int index;
|
|
explicit Provider(int value) : index(value) {}
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **out) override {
|
|
*out = nullptr;
|
|
if (IsEqualIID(iid, IID_IUnknown) || IsEqualIID(iid, IID_IRawElementProviderSimple))
|
|
*out = static_cast<IRawElementProviderSimple *>(this);
|
|
else if (IsEqualIID(iid, IID_IRawElementProviderFragment))
|
|
*out = static_cast<IRawElementProviderFragment *>(this);
|
|
else if (index == 0 && IsEqualIID(iid, IID_IRawElementProviderFragmentRoot))
|
|
*out = static_cast<IRawElementProviderFragmentRoot *>(this);
|
|
else if (index == 7 && IsEqualIID(iid, IID_IToggleProvider))
|
|
*out = static_cast<IToggleProvider *>(this);
|
|
return *out ? S_OK : E_NOINTERFACE;
|
|
}
|
|
ULONG STDMETHODCALLTYPE AddRef() override { return 2; }
|
|
ULONG STDMETHODCALLTYPE Release() override { return 1; }
|
|
HRESULT STDMETHODCALLTYPE get_ProviderOptions(ProviderOptions *out) override {
|
|
*out = ProviderOptions_ServerSideProvider; return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE GetPatternProvider(PATTERNID id, IUnknown **out) override {
|
|
*out = index == 7 && id == UIA_TogglePatternId ? static_cast<IToggleProvider *>(this) : nullptr;
|
|
return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE Toggle() override { ExitProcess(9); }
|
|
HRESULT STDMETHODCALLTYPE get_ToggleState(ToggleState *out) override { *out = checkboxState; return S_OK; }
|
|
HRESULT STDMETHODCALLTYPE GetPropertyValue(PROPERTYID id, VARIANT *out) override {
|
|
VariantInit(out);
|
|
static const WCHAR *names[] = {L"Bootstrapper fixture", L"Select a Language", L"Continue",
|
|
L"English (United States)", L"French", L"Password", L"Unknown protection", L"Launch on startup"};
|
|
if (id == UIA_NamePropertyId) {
|
|
out->vt = VT_BSTR; out->bstrVal = SysAllocString(names[index]);
|
|
} else if (id == UIA_ControlTypePropertyId) {
|
|
out->vt = VT_I4;
|
|
out->lVal = index == 7 ? UIA_CheckBoxControlTypeId : index == 0 ? UIA_WindowControlTypeId : index == 1 ? UIA_GroupControlTypeId :
|
|
index == 2 ? UIA_ButtonControlTypeId : index < 5 ? UIA_RadioButtonControlTypeId : UIA_EditControlTypeId;
|
|
} else if (id == UIA_HasKeyboardFocusPropertyId || id == UIA_IsKeyboardFocusablePropertyId ||
|
|
id == UIA_IsOffscreenPropertyId || id == UIA_IsEnabledPropertyId ||
|
|
id == UIA_IsPasswordPropertyId) {
|
|
if (id == UIA_IsPasswordPropertyId && index == 6) return S_OK;
|
|
out->vt = VT_BOOL;
|
|
bool value = id == UIA_HasKeyboardFocusPropertyId ? (index == focusedIndex || index == selectedIndex) :
|
|
id == UIA_IsKeyboardFocusablePropertyId ? index != 0 :
|
|
id == UIA_IsOffscreenPropertyId ? false :
|
|
id == UIA_IsPasswordPropertyId ? index == 5 : index != 7 || checkboxEnabled;
|
|
out->boolVal = value ? VARIANT_TRUE : VARIANT_FALSE;
|
|
} else if (id == UIA_ValueValuePropertyId && (index == 5 || index == 6)) {
|
|
// Protected and unknown-protection values must never even be queried.
|
|
ExitProcess(8);
|
|
}
|
|
return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE get_HostRawElementProvider(IRawElementProviderSimple **out) override {
|
|
*out = nullptr; return index == 0 ? UiaHostProviderFromHwnd(window, out) : S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE Navigate(NavigateDirection direction, IRawElementProviderFragment **out) override;
|
|
HRESULT STDMETHODCALLTYPE GetRuntimeId(SAFEARRAY **out) override {
|
|
*out = nullptr;
|
|
if (index == 0) return S_OK;
|
|
*out = SafeArrayCreateVector(VT_I4, 0, 2);
|
|
LONG position = 0, value = UiaAppendRuntimeId;
|
|
SafeArrayPutElement(*out, &position, &value);
|
|
position = 1; value = index; SafeArrayPutElement(*out, &position, &value); return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE get_BoundingRectangle(UiaRect *out) override {
|
|
*out = {100, 100.0 + index * 20, 200, 20}; return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE GetEmbeddedFragmentRoots(SAFEARRAY **out) override { *out = nullptr; return S_OK; }
|
|
HRESULT STDMETHODCALLTYPE SetFocus() override { return E_NOTIMPL; }
|
|
HRESULT STDMETHODCALLTYPE get_FragmentRoot(IRawElementProviderFragmentRoot **out) override;
|
|
HRESULT STDMETHODCALLTYPE ElementProviderFromPoint(double, double, IRawElementProviderFragment **out) override {
|
|
*out = nullptr; return S_OK;
|
|
}
|
|
HRESULT STDMETHODCALLTYPE GetFocus(IRawElementProviderFragment **out) override;
|
|
};
|
|
static Provider providers[] = {Provider(0), Provider(1), Provider(2), Provider(3), Provider(4), Provider(5), Provider(6), Provider(7)};
|
|
HRESULT Provider::Navigate(NavigateDirection direction, IRawElementProviderFragment **out) {
|
|
*out = nullptr;
|
|
if (direction == NavigateDirection_Parent && index) *out = &providers[0];
|
|
else if (direction == NavigateDirection_FirstChild && index == 0) *out = &providers[1];
|
|
else if (direction == NavigateDirection_LastChild && index == 0) *out = &providers[7];
|
|
else if (direction == NavigateDirection_NextSibling && index > 0 && index < 7) *out = &providers[index + 1];
|
|
else if (direction == NavigateDirection_PreviousSibling && index > 1) *out = &providers[index - 1];
|
|
return S_OK;
|
|
}
|
|
HRESULT Provider::get_FragmentRoot(IRawElementProviderFragmentRoot **out) { *out = &providers[0]; return S_OK; }
|
|
HRESULT Provider::GetFocus(IRawElementProviderFragment **out) { *out = &providers[focusedIndex]; return S_OK; }
|
|
|
|
static LRESULT CALLBACK window_proc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) {
|
|
if (message == WM_GETOBJECT && static_cast<LONG>(lp) == UiaRootObjectId)
|
|
return UiaReturnRawElementProvider(hwnd, wp, lp, &providers[0]);
|
|
if (message == WM_TIMER) {
|
|
switch (++step) {
|
|
case 1: break;
|
|
case 2: selectedIndex = 4; break;
|
|
case 3: focusedIndex = 2; break;
|
|
case 4: focusedIndex = 1; break;
|
|
case 5: focusedIndex = 5; break;
|
|
case 6: focusedIndex = 6; break;
|
|
case 7: focusedIndex = 1; selectedIndex = 3; break;
|
|
case 8: focusedIndex = 7; break;
|
|
case 9: checkboxState = ToggleState_On; break;
|
|
case 10: checkboxState = ToggleState_Off; break;
|
|
case 11: checkboxState = ToggleState_Indeterminate; checkboxEnabled = false; break;
|
|
case 12: focusedIndex = 2; break;
|
|
case 13: focusedIndex = 7; break;
|
|
case 14: ShowWindow(hwnd, SW_HIDE); checkboxState = ToggleState_On; checkboxEnabled = true; break;
|
|
case 15: ShowWindow(hwnd, SW_SHOW); SetForegroundWindow(hwnd); SetFocus(hwnd); break;
|
|
default: DestroyWindow(hwnd); break;
|
|
}
|
|
return 0;
|
|
}
|
|
if (message == WM_DESTROY) { PostQuitMessage(0); return 0; }
|
|
return DefWindowProcW(hwnd, message, wp, lp);
|
|
}
|
|
int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int) {
|
|
if (FAILED(CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED))) return 1;
|
|
WNDCLASSW cls{};
|
|
cls.hInstance = instance; cls.lpfnWndProc = window_proc; cls.lpszClassName = L"Blizzard Bootstrapper";
|
|
RegisterClassW(&cls);
|
|
window = CreateWindowW(cls.lpszClassName, L"Bootstrapper fixture", WS_OVERLAPPEDWINDOW,
|
|
100, 100, 400, 300, nullptr, nullptr, instance, nullptr);
|
|
ShowWindow(window, SW_SHOW); SetForegroundWindow(window); SetFocus(window);
|
|
SetTimer(window, 1, 800, nullptr);
|
|
MSG message{};
|
|
while (GetMessageW(&message, nullptr, 0, 0) > 0) {
|
|
TranslateMessage(&message); DispatchMessageW(&message);
|
|
}
|
|
CoUninitialize();
|
|
return 0;
|
|
}
|