2015-01-22 18:55:55 +01:00
|
|
|
// Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/util_win.h"
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
#include "include/base/cef_logging.h"
|
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
2018-09-26 13:25:58 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
LARGE_INTEGER qi_freq_ = {};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
uint64_t GetTimeNow() {
|
|
|
|
if (!qi_freq_.HighPart && !qi_freq_.LowPart) {
|
|
|
|
QueryPerformanceFrequency(&qi_freq_);
|
|
|
|
}
|
|
|
|
LARGE_INTEGER t = {};
|
|
|
|
QueryPerformanceCounter(&t);
|
|
|
|
return static_cast<uint64_t>((t.QuadPart / double(qi_freq_.QuadPart)) *
|
|
|
|
1000000);
|
|
|
|
}
|
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
void SetUserDataPtr(HWND hWnd, void* ptr) {
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
2017-05-17 11:29:28 +02:00
|
|
|
LONG_PTR result =
|
|
|
|
::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ptr));
|
2015-01-22 18:55:55 +01:00
|
|
|
CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
WNDPROC SetWndProcPtr(HWND hWnd, WNDPROC wndProc) {
|
|
|
|
WNDPROC old =
|
|
|
|
reinterpret_cast<WNDPROC>(::GetWindowLongPtr(hWnd, GWLP_WNDPROC));
|
2021-06-17 22:08:01 +02:00
|
|
|
CHECK(old != nullptr);
|
2017-05-17 11:29:28 +02:00
|
|
|
LONG_PTR result = ::SetWindowLongPtr(hWnd, GWLP_WNDPROC,
|
|
|
|
reinterpret_cast<LONG_PTR>(wndProc));
|
2015-01-22 18:55:55 +01:00
|
|
|
CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
|
|
|
|
return old;
|
|
|
|
}
|
|
|
|
|
2015-01-27 01:03:25 +01:00
|
|
|
std::wstring GetResourceString(UINT id) {
|
2017-05-17 11:29:28 +02:00
|
|
|
#define MAX_LOADSTRING 100
|
2015-01-27 01:03:25 +01:00
|
|
|
TCHAR buff[MAX_LOADSTRING] = {0};
|
2021-06-17 22:08:01 +02:00
|
|
|
LoadString(::GetModuleHandle(nullptr), id, buff, MAX_LOADSTRING);
|
2015-01-27 01:03:25 +01:00
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
2015-01-23 18:37:23 +01:00
|
|
|
int GetCefMouseModifiers(WPARAM wparam) {
|
|
|
|
int modifiers = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (wparam & MK_CONTROL) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_CONTROL_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (wparam & MK_SHIFT) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_SHIFT_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (IsKeyDown(VK_MENU)) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_ALT_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (wparam & MK_LBUTTON) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (wparam & MK_MBUTTON) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (wparam & MK_RBUTTON) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 18:37:23 +01:00
|
|
|
|
|
|
|
// Low bit set from GetKeyState indicates "toggled".
|
2023-01-02 23:59:03 +01:00
|
|
|
if (::GetKeyState(VK_NUMLOCK) & 1) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_NUM_LOCK_ON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (::GetKeyState(VK_CAPITAL) & 1) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_CAPS_LOCK_ON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 18:37:23 +01:00
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetCefKeyboardModifiers(WPARAM wparam, LPARAM lparam) {
|
|
|
|
int modifiers = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsKeyDown(VK_SHIFT)) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_SHIFT_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (IsKeyDown(VK_CONTROL)) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_CONTROL_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (IsKeyDown(VK_MENU)) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_ALT_DOWN;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 18:37:23 +01:00
|
|
|
|
|
|
|
// Low bit set from GetKeyState indicates "toggled".
|
2023-01-02 23:59:03 +01:00
|
|
|
if (::GetKeyState(VK_NUMLOCK) & 1) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_NUM_LOCK_ON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (::GetKeyState(VK_CAPITAL) & 1) {
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_CAPS_LOCK_ON;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-23 18:37:23 +01:00
|
|
|
|
|
|
|
switch (wparam) {
|
2017-05-17 11:29:28 +02:00
|
|
|
case VK_RETURN:
|
2023-01-02 23:59:03 +01:00
|
|
|
if ((lparam >> 16) & KF_EXTENDED) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_KEY_PAD;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_INSERT:
|
|
|
|
case VK_DELETE:
|
|
|
|
case VK_HOME:
|
|
|
|
case VK_END:
|
|
|
|
case VK_PRIOR:
|
|
|
|
case VK_NEXT:
|
|
|
|
case VK_UP:
|
|
|
|
case VK_DOWN:
|
|
|
|
case VK_LEFT:
|
|
|
|
case VK_RIGHT:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!((lparam >> 16) & KF_EXTENDED)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_KEY_PAD;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_NUMLOCK:
|
|
|
|
case VK_NUMPAD0:
|
|
|
|
case VK_NUMPAD1:
|
|
|
|
case VK_NUMPAD2:
|
|
|
|
case VK_NUMPAD3:
|
|
|
|
case VK_NUMPAD4:
|
|
|
|
case VK_NUMPAD5:
|
|
|
|
case VK_NUMPAD6:
|
|
|
|
case VK_NUMPAD7:
|
|
|
|
case VK_NUMPAD8:
|
|
|
|
case VK_NUMPAD9:
|
|
|
|
case VK_DIVIDE:
|
|
|
|
case VK_MULTIPLY:
|
|
|
|
case VK_SUBTRACT:
|
|
|
|
case VK_ADD:
|
|
|
|
case VK_DECIMAL:
|
|
|
|
case VK_CLEAR:
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_IS_KEY_PAD;
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_SHIFT:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsKeyDown(VK_LSHIFT)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_LEFT;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (IsKeyDown(VK_RSHIFT)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_RIGHT;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_CONTROL:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsKeyDown(VK_LCONTROL)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_LEFT;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (IsKeyDown(VK_RCONTROL)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_RIGHT;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_MENU:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (IsKeyDown(VK_LMENU)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_LEFT;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else if (IsKeyDown(VK_RMENU)) {
|
2017-05-17 11:29:28 +02:00
|
|
|
modifiers |= EVENTFLAG_IS_RIGHT;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_LWIN:
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_IS_LEFT;
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
case VK_RWIN:
|
2015-01-23 18:37:23 +01:00
|
|
|
modifiers |= EVENTFLAG_IS_RIGHT;
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
2015-01-23 18:37:23 +01:00
|
|
|
}
|
|
|
|
return modifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsKeyDown(WPARAM wparam) {
|
|
|
|
return (GetKeyState(wparam) & 0x8000) != 0;
|
|
|
|
}
|
|
|
|
|
2015-08-08 01:00:07 +02:00
|
|
|
float GetDeviceScaleFactor() {
|
|
|
|
static float scale_factor = 1.0;
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
// This value is safe to cache for the life time of the app since the user
|
|
|
|
// must logout to change the DPI setting. This value also applies to all
|
|
|
|
// screens.
|
2021-06-17 22:08:01 +02:00
|
|
|
HDC screen_dc = ::GetDC(nullptr);
|
2015-08-08 01:00:07 +02:00
|
|
|
int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
|
|
|
|
scale_factor = static_cast<float>(dpi_x) / 96.0f;
|
2021-06-17 22:08:01 +02:00
|
|
|
::ReleaseDC(nullptr, screen_dc);
|
2015-08-08 01:00:07 +02:00
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scale_factor;
|
|
|
|
}
|
|
|
|
|
2015-01-22 18:55:55 +01:00
|
|
|
} // namespace client
|