UI hacking.

This commit is contained in:
Ben Vanik
2014-12-20 13:54:55 -08:00
parent c1df273600
commit d839359b4a
28 changed files with 1235 additions and 714 deletions

View File

@@ -0,0 +1,13 @@
# Copyright 2014 Ben Vanik. All Rights Reserved.
{
'sources': [
'win32_control.cc',
'win32_control.h',
'win32_loop.cc',
'win32_loop.h',
'win32_menu_item.cc',
'win32_menu_item.h',
'win32_window.cc',
'win32_window.h',
],
}

View File

@@ -0,0 +1,314 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <poly/ui/win32/win32_control.h>
namespace poly {
namespace ui {
namespace win32 {
LRESULT CALLBACK
Win32Control::WndProcThunk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
Win32Control* control = 0;
if (message == WM_NCCREATE) {
auto create_struct = reinterpret_cast<LPCREATESTRUCT>(lParam);
control = reinterpret_cast<Win32Control*>(create_struct->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (__int3264)(LONG_PTR)control);
} else {
control =
reinterpret_cast<Win32Control*>(GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
if (control) {
return control->WndProc(hWnd, message, wParam, lParam);
} else {
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
Win32Control::Win32Control(Control* parent, uint32_t flags)
: Control(parent, flags), hwnd_(nullptr) {}
Win32Control::~Win32Control() {
if (hwnd_) {
CloseWindow(hwnd_);
hwnd_ = nullptr;
}
}
void Win32Control::OnCreate() {
Control::OnCreate();
if (!is_cursor_visible_) {
ShowCursor(FALSE);
}
if (!is_enabled_) {
EnableWindow(hwnd_, false);
}
if (!is_visible_) {
ShowWindow(hwnd_, SW_HIDE);
}
if (has_focus_) {
SetFocus(hwnd_);
}
}
void Win32Control::Resize(int32_t width, int32_t height) {
MoveWindow(hwnd_, 0, 0, width, height, TRUE);
}
void Win32Control::Resize(int32_t left, int32_t top, int32_t right,
int32_t bottom) {
MoveWindow(hwnd_, left, top, right - left, bottom - top, TRUE);
}
void Win32Control::ResizeToFill(int32_t pad_left, int32_t pad_top,
int32_t pad_right, int32_t pad_bottom) {
if (!parent_) {
// TODO(benvanik): screen?
return;
}
RECT parent_rect;
auto parent_control = static_cast<Win32Control*>(parent_);
GetClientRect(parent_control->hwnd(), &parent_rect);
MoveWindow(hwnd_, parent_rect.left + pad_left, parent_rect.top + pad_top,
parent_rect.right - pad_right - pad_left,
parent_rect.right - pad_bottom - pad_top, TRUE);
}
void Win32Control::set_cursor_visible(bool value) {
if (is_cursor_visible_ == value) {
return;
}
if (value) {
ShowCursor(TRUE);
SetCursor(nullptr);
} else {
ShowCursor(FALSE);
}
}
void Win32Control::set_enabled(bool value) {
if (is_enabled_ == value) {
return;
}
if (hwnd_) {
EnableWindow(hwnd_, value);
} else {
is_enabled_ = value;
}
}
void Win32Control::set_visible(bool value) {
if (is_visible_ == value) {
return;
}
if (hwnd_) {
ShowWindow(hwnd_, value ? SW_SHOWNOACTIVATE : SW_HIDE);
} else {
is_visible_ = value;
}
}
void Win32Control::set_focus(bool value) {
if (has_focus_ == value) {
return;
}
if (hwnd_) {
if (value) {
SetFocus(hwnd_);
} else {
SetFocus(nullptr);
}
} else {
has_focus_ = value;
}
}
LRESULT Win32Control::WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
if (message >= WM_MOUSEFIRST && message <= WM_MOUSELAST) {
if (HandleMouse(message, wParam, lParam)) {
return 0;
} else {
return DefWindowProc(hWnd, message, wParam, lParam);
}
} else if (message >= WM_KEYFIRST && message <= WM_KEYLAST) {
if (HandleKeyboard(message, wParam, lParam)) {
return 0;
} else {
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
switch (message) {
case WM_NCCREATE:
hwnd_ = hWnd;
break;
case WM_CREATE:
OnCreate();
break;
case WM_DESTROY:
OnDestroy();
break;
case WM_MOVING:
break;
case WM_MOVE:
break;
case WM_SIZING:
break;
case WM_SIZE: {
RECT client_rect;
GetClientRect(hwnd_, &client_rect);
int32_t width = client_rect.right - client_rect.left;
int32_t height = client_rect.bottom - client_rect.top;
if (width != width_ || height != height_) {
width_ = width;
height_ = height;
auto e = UIEvent(this);
OnResize(e);
}
break;
}
case WM_PAINT:
if (flags_ & kFlagOwnPaint) {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
}
break;
case WM_ERASEBKGND:
if (flags_ & kFlagOwnPaint) {
return 0; // ignore
}
break;
case WM_DISPLAYCHANGE:
break;
case WM_SHOWWINDOW: {
if (wParam == TRUE) {
auto e = UIEvent(this);
OnVisible(e);
} else {
auto e = UIEvent(this);
OnHidden(e);
}
break;
}
case WM_KILLFOCUS: {
has_focus_ = false;
auto e = UIEvent(this);
OnLostFocus(e);
break;
}
case WM_SETFOCUS: {
has_focus_ = true;
auto e = UIEvent(this);
OnGotFocus(e);
break;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
bool Win32Control::HandleMouse(UINT message, WPARAM wParam, LPARAM lParam) {
int32_t x = GET_X_LPARAM(lParam);
int32_t y = GET_Y_LPARAM(lParam);
if (message == WM_MOUSEWHEEL) {
POINT pt = {x, y};
ScreenToClient(hwnd_, &pt);
x = pt.x;
y = pt.y;
}
MouseEvent::Button button = MouseEvent::Button::kNone;
int32_t dx = 0;
int32_t dy = 0;
switch (message) {
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
button = MouseEvent::Button::kLeft;
break;
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
button = MouseEvent::Button::kRight;
break;
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
button = MouseEvent::Button::kMiddle;
break;
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
switch (GET_XBUTTON_WPARAM(wParam)) {
case XBUTTON1:
button = MouseEvent::Button::kX1;
break;
case XBUTTON2:
button = MouseEvent::Button::kX2;
break;
default:
return false;
}
break;
case WM_MOUSEMOVE:
button = MouseEvent::Button::kNone;
break;
case WM_MOUSEWHEEL:
button = MouseEvent::Button::kNone;
dx = 0; // ?
dy = GET_WHEEL_DELTA_WPARAM(wParam);
break;
default:
// Double click/etc?
return true;
}
auto e = MouseEvent(this, button, x, y, dx, dy);
switch (message) {
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
OnMouseDown(e);
break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
case WM_XBUTTONUP:
OnMouseUp(e);
break;
case WM_MOUSEMOVE:
OnMouseMove(e);
break;
case WM_MOUSEWHEEL:
OnMouseWheel(e);
break;
}
return true;
}
bool Win32Control::HandleKeyboard(UINT message, WPARAM wParam, LPARAM lParam) {
auto e = KeyEvent(this, (int)wParam);
switch (message) {
case WM_KEYDOWN:
OnKeyDown(e);
return true;
case WM_KEYUP:
OnKeyUp(e);
return true;
default:
return false;
}
}
} // namespace win32
} // namespace ui
} // namespace poly

View File

@@ -0,0 +1,62 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_UI_WIN32_WIN32_CONTROL_H_
#define POLY_UI_WIN32_WIN32_CONTROL_H_
#include <windows.h>
#include <windowsx.h>
#include <poly/ui/control.h>
namespace poly {
namespace ui {
namespace win32 {
class Win32Control : public Control {
public:
~Win32Control() override;
HWND hwnd() const { return hwnd_; }
void Resize(int32_t width, int32_t height) override;
void Resize(int32_t left, int32_t top, int32_t right,
int32_t bottom) override;
void ResizeToFill(int32_t pad_left, int32_t pad_top, int32_t pad_right,
int32_t pad_bottom) override;
void set_cursor_visible(bool value) override;
void set_enabled(bool value) override;
void set_visible(bool value) override;
void set_focus(bool value) override;
protected:
Win32Control(Control* parent, uint32_t flags);
virtual bool CreateHWND() = 0;
void OnCreate() override;
static LRESULT CALLBACK
WndProcThunk(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
virtual LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam);
HWND hwnd_;
private:
bool HandleMouse(UINT message, WPARAM wParam, LPARAM lParam);
bool HandleKeyboard(UINT message, WPARAM wParam, LPARAM lParam);
};
} // namespace win32
} // namespace ui
} // namespace poly
#endif // POLY_UI_WIN32_WIN32_CONTROL_H_

View File

@@ -0,0 +1,72 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <poly/ui/win32/win32_loop.h>
namespace poly {
namespace ui {
namespace win32 {
const DWORD kWmWin32LoopPost = WM_APP + 0x100;
const DWORD kWmWin32LoopQuit = WM_APP + 0x101;
class PostedFn {
public:
explicit PostedFn(std::function<void()> fn) : fn_(std::move(fn)) {}
void Call() { fn_(); }
private:
std::function<void()> fn_;
};
Win32Loop::Win32Loop() : thread_id_(0) {
thread_ = std::thread([this]() {
thread_id_ = GetCurrentThreadId();
ThreadMain();
});
}
Win32Loop::~Win32Loop() = default;
void Win32Loop::ThreadMain() {
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
switch (msg.message) {
case kWmWin32LoopPost:
if (msg.wParam == reinterpret_cast<WPARAM>(this)) {
auto posted_fn = reinterpret_cast<PostedFn*>(msg.lParam);
posted_fn->Call();
delete posted_fn;
}
break;
case kWmWin32LoopQuit:
if (msg.wParam == reinterpret_cast<WPARAM>(this)) {
return;
}
break;
}
}
}
void Win32Loop::Post(std::function<void()> fn) {
PostThreadMessage(thread_id_, kWmWin32LoopPost,
reinterpret_cast<WPARAM>(this),
reinterpret_cast<LPARAM>(new PostedFn(std::move(fn))));
}
void Win32Loop::Quit() {
PostThreadMessage(thread_id_, kWmWin32LoopQuit,
reinterpret_cast<WPARAM>(this), 0);
}
} // namespace win32
} // namespace ui
} // namespace poly

View File

@@ -0,0 +1,44 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_UI_WIN32_WIN32_LOOP_H_
#define POLY_UI_WIN32_WIN32_LOOP_H_
#include <windows.h>
#include <windowsx.h>
#include <thread>
#include <poly/ui/loop.h>
namespace poly {
namespace ui {
namespace win32 {
class Win32Loop : public Loop {
public:
Win32Loop();
~Win32Loop();
void Post(std::function<void()> fn) override;
void Quit() override;
private:
void ThreadMain();
std::thread thread_;
DWORD thread_id_;
};
} // namespace win32
} // namespace ui
} // namespace poly
#endif // POLY_UI_WIN32_WIN32_LOOP_H_

View File

@@ -0,0 +1,37 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <poly/ui/win32/win32_menu_item.h>
namespace poly {
namespace ui {
namespace win32 {
Win32MenuItem::Win32MenuItem(Type type)
: MenuItem(type), handle_(CreateMenu()) {}
Win32MenuItem::~Win32MenuItem() {
if (handle_) {
DestroyMenu(handle_);
}
}
void Win32MenuItem::OnChildAdded(MenuItem* generic_child_item) {
auto child_item = static_cast<Win32MenuItem*>(generic_child_item);
//
}
void Win32MenuItem::OnChildRemoved(MenuItem* generic_child_item) {
auto child_item = static_cast<Win32MenuItem*>(generic_child_item);
//
}
} // namespace win32
} // namespace ui
} // namespace poly

View File

@@ -0,0 +1,40 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_UI_WIN32_WIN32_MENU_ITEM_H_
#define POLY_UI_WIN32_WIN32_MENU_ITEM_H_
#include <windows.h>
#include <windowsx.h>
#include <poly/ui/menu_item.h>
namespace poly {
namespace ui {
namespace win32 {
class Win32MenuItem : public MenuItem {
public:
~Win32MenuItem() override;
protected:
void OnChildAdded(MenuItem* child_item) override;
void OnChildRemoved(MenuItem* child_item) override;
private:
Win32MenuItem(Type type);
HMENU handle_;
};
} // namespace win32
} // namespace ui
} // namespace poly
#endif // POLY_UI_WIN32_WIN32_MENU_ITEM_H_

View File

@@ -0,0 +1,189 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include <poly/ui/win32/win32_window.h>
#include <dwmapi.h>
#include <tpcshrd.h>
#include <windowsx.h>
#include <poly/logging.h>
namespace poly {
namespace ui {
namespace win32 {
Win32Window::Win32Window(const std::wstring& title)
: Window(title), main_menu_(nullptr), closing_(false) {}
Win32Window::~Win32Window() {}
bool Win32Window::CreateHWND() {
HINSTANCE hInstance = GetModuleHandle(nullptr);
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_OWNDC;
wcex.lpfnWndProc = Win32Control::WndProcThunk;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = nullptr; // LoadIcon(hInstance, (LPCTSTR)IDI_TUTORIAL1);
wcex.hIconSm = nullptr; // LoadIcon(hInstance, (LPCTSTR)IDI_TUTORIAL1);
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"XeniaWindowClass";
if (!RegisterClassEx(&wcex)) {
PLOGE("RegisterClassEx failed");
return false;
}
// Setup initial size.
DWORD window_style = WS_OVERLAPPEDWINDOW;
DWORD window_ex_style = WS_EX_APPWINDOW;
RECT rc = {0, 0, width_, height_};
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
// Create window.
hwnd_ = CreateWindowEx(window_ex_style, L"XeniaWindowClass", L"Xenia",
window_style, CW_USEDEFAULT, CW_USEDEFAULT,
rc.right - rc.left, rc.bottom - rc.top, nullptr,
nullptr, hInstance, this);
if (!hwnd_) {
PLOGE("CreateWindow failed");
return false;
}
main_menu_ = CreateMenu();
SetMenu(hwnd_, main_menu_);
// Disable flicks.
ATOM atom = GlobalAddAtom(L"MicrosoftTabletPenServiceProperty");
const DWORD_PTR dwHwndTabletProperty =
TABLET_DISABLE_PRESSANDHOLD | // disables press and hold (right-click)
// gesture
TABLET_DISABLE_PENTAPFEEDBACK | // disables UI feedback on pen up (waves)
TABLET_DISABLE_PENBARRELFEEDBACK | // disables UI feedback on pen button
// down (circle)
TABLET_DISABLE_FLICKS | // disables pen flicks (back, forward, drag down,
// drag up)
TABLET_DISABLE_TOUCHSWITCH | TABLET_DISABLE_SMOOTHSCROLLING |
TABLET_DISABLE_TOUCHUIFORCEON | TABLET_ENABLE_MULTITOUCHDATA;
SetProp(hwnd_, L"MicrosoftTabletPenServiceProperty",
reinterpret_cast<HANDLE>(dwHwndTabletProperty));
GlobalDeleteAtom(atom);
// Enable DWM elevation.
EnableMMCSS();
ShowWindow(hwnd_, SW_SHOWNORMAL);
UpdateWindow(hwnd_);
return 0;
}
void Win32Window::EnableMMCSS() {
HMODULE hLibrary = LoadLibrary(L"DWMAPI.DLL");
if (!hLibrary) {
return;
}
typedef HRESULT(__stdcall * PDwmEnableMMCSS)(BOOL);
PDwmEnableMMCSS pDwmEnableMMCSS =
(PDwmEnableMMCSS)GetProcAddress(hLibrary, "DwmEnableMMCSS");
if (pDwmEnableMMCSS) {
pDwmEnableMMCSS(TRUE);
}
typedef HRESULT(__stdcall * PDwmSetPresentParameters)(
HWND, DWM_PRESENT_PARAMETERS*);
PDwmSetPresentParameters pDwmSetPresentParameters =
(PDwmSetPresentParameters)GetProcAddress(hLibrary,
"DwmSetPresentParameters");
if (pDwmSetPresentParameters) {
DWM_PRESENT_PARAMETERS pp;
memset(&pp, 0, sizeof(DWM_PRESENT_PARAMETERS));
pp.cbSize = sizeof(DWM_PRESENT_PARAMETERS);
pp.fQueue = FALSE;
pp.cBuffer = 2;
pp.fUseSourceRate = FALSE;
pp.cRefreshesPerFrame = 1;
pp.eSampling = DWM_SOURCE_FRAME_SAMPLING_POINT;
pDwmSetPresentParameters(hwnd_, &pp);
}
FreeLibrary(hLibrary);
}
bool Win32Window::set_title(const std::wstring& title) {
if (!Window::set_title(title)) {
return false;
}
SetWindowText(hwnd_, title.c_str());
return true;
}
// bool Win32Window::SetSize(uint32_t width, uint32_t height) {
// RECT rc = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
// AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
// // TODO(benvanik): center?
// MoveWindow(handle_, 0, 0, rc.right - rc.left, rc.bottom - rc.top, TRUE);
// return true;
// }
void Win32Window::OnClose() {
if (!closing_ && hwnd_) {
closing_ = true;
CloseWindow(hwnd_);
}
}
LRESULT Win32Window::WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) {
switch (message) {
case WM_ACTIVATEAPP:
if (wParam) {
// Made active.
OnShow();
} else {
// Made inactive.
OnHide();
}
break;
case WM_CLOSE:
closing_ = true;
Close();
break;
case WM_TABLET_QUERYSYSTEMGESTURESTATUS:
return TABLET_DISABLE_PRESSANDHOLD | // disables press and hold
// (right-click) gesture
TABLET_DISABLE_PENTAPFEEDBACK | // disables UI feedback on pen up
// (waves)
TABLET_DISABLE_PENBARRELFEEDBACK | // disables UI feedback on pen
// button down (circle)
TABLET_DISABLE_FLICKS | // disables pen flicks (back, forward,
// drag down, drag up)
TABLET_DISABLE_TOUCHSWITCH | TABLET_DISABLE_SMOOTHSCROLLING |
TABLET_DISABLE_TOUCHUIFORCEON | TABLET_ENABLE_MULTITOUCHDATA;
case WM_COMMAND:
switch (LOWORD(wParam)) {
// TODO(benvanik): dispatch to menu.
}
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
} // namespace win32
} // namespace ui
} // namespace poly

View File

@@ -0,0 +1,48 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2014 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef POLY_UI_WIN32_WIN32_WINDOW_H_
#define POLY_UI_WIN32_WIN32_WINDOW_H_
#include <string>
#include <poly/ui/win32/win32_control.h>
#include <poly/ui/window.h>
namespace poly {
namespace ui {
namespace win32 {
class Win32Window : public Window<Win32Control> {
public:
Win32Window(const std::wstring& title);
~Win32Window() override;
bool set_title(const std::wstring& title) override;
protected:
bool CreateHWND() override;
void OnClose() override;
LRESULT WndProc(HWND hWnd, UINT message, WPARAM wParam,
LPARAM lParam) override;
private:
void EnableMMCSS();
HMENU main_menu_;
bool closing_;
};
} // namespace win32
} // namespace ui
} // namespace poly
#endif // POLY_UI_WIN32_WIN32_WINDOW_H_