1 // Copyright (c) 2015 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4
5 #include "tests/shared/browser/util_win.h"
6
7 #include "include/base/cef_logging.h"
8
9 namespace client {
10
11 namespace {
12
13 LARGE_INTEGER qi_freq_ = {};
14
15 } // namespace
16
GetTimeNow()17 uint64_t GetTimeNow() {
18 if (!qi_freq_.HighPart && !qi_freq_.LowPart) {
19 QueryPerformanceFrequency(&qi_freq_);
20 }
21 LARGE_INTEGER t = {};
22 QueryPerformanceCounter(&t);
23 return static_cast<uint64_t>((t.QuadPart / double(qi_freq_.QuadPart)) *
24 1000000);
25 }
26
SetUserDataPtr(HWND hWnd,void * ptr)27 void SetUserDataPtr(HWND hWnd, void* ptr) {
28 SetLastError(ERROR_SUCCESS);
29 LONG_PTR result =
30 ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(ptr));
31 CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
32 }
33
SetWndProcPtr(HWND hWnd,WNDPROC wndProc)34 WNDPROC SetWndProcPtr(HWND hWnd, WNDPROC wndProc) {
35 WNDPROC old =
36 reinterpret_cast<WNDPROC>(::GetWindowLongPtr(hWnd, GWLP_WNDPROC));
37 CHECK(old != nullptr);
38 LONG_PTR result = ::SetWindowLongPtr(hWnd, GWLP_WNDPROC,
39 reinterpret_cast<LONG_PTR>(wndProc));
40 CHECK(result != 0 || GetLastError() == ERROR_SUCCESS);
41 return old;
42 }
43
GetResourceString(UINT id)44 std::wstring GetResourceString(UINT id) {
45 #define MAX_LOADSTRING 100
46 TCHAR buff[MAX_LOADSTRING] = {0};
47 LoadString(::GetModuleHandle(nullptr), id, buff, MAX_LOADSTRING);
48 return buff;
49 }
50
GetCefMouseModifiers(WPARAM wparam)51 int GetCefMouseModifiers(WPARAM wparam) {
52 int modifiers = 0;
53 if (wparam & MK_CONTROL)
54 modifiers |= EVENTFLAG_CONTROL_DOWN;
55 if (wparam & MK_SHIFT)
56 modifiers |= EVENTFLAG_SHIFT_DOWN;
57 if (IsKeyDown(VK_MENU))
58 modifiers |= EVENTFLAG_ALT_DOWN;
59 if (wparam & MK_LBUTTON)
60 modifiers |= EVENTFLAG_LEFT_MOUSE_BUTTON;
61 if (wparam & MK_MBUTTON)
62 modifiers |= EVENTFLAG_MIDDLE_MOUSE_BUTTON;
63 if (wparam & MK_RBUTTON)
64 modifiers |= EVENTFLAG_RIGHT_MOUSE_BUTTON;
65
66 // Low bit set from GetKeyState indicates "toggled".
67 if (::GetKeyState(VK_NUMLOCK) & 1)
68 modifiers |= EVENTFLAG_NUM_LOCK_ON;
69 if (::GetKeyState(VK_CAPITAL) & 1)
70 modifiers |= EVENTFLAG_CAPS_LOCK_ON;
71 return modifiers;
72 }
73
GetCefKeyboardModifiers(WPARAM wparam,LPARAM lparam)74 int GetCefKeyboardModifiers(WPARAM wparam, LPARAM lparam) {
75 int modifiers = 0;
76 if (IsKeyDown(VK_SHIFT))
77 modifiers |= EVENTFLAG_SHIFT_DOWN;
78 if (IsKeyDown(VK_CONTROL))
79 modifiers |= EVENTFLAG_CONTROL_DOWN;
80 if (IsKeyDown(VK_MENU))
81 modifiers |= EVENTFLAG_ALT_DOWN;
82
83 // Low bit set from GetKeyState indicates "toggled".
84 if (::GetKeyState(VK_NUMLOCK) & 1)
85 modifiers |= EVENTFLAG_NUM_LOCK_ON;
86 if (::GetKeyState(VK_CAPITAL) & 1)
87 modifiers |= EVENTFLAG_CAPS_LOCK_ON;
88
89 switch (wparam) {
90 case VK_RETURN:
91 if ((lparam >> 16) & KF_EXTENDED)
92 modifiers |= EVENTFLAG_IS_KEY_PAD;
93 break;
94 case VK_INSERT:
95 case VK_DELETE:
96 case VK_HOME:
97 case VK_END:
98 case VK_PRIOR:
99 case VK_NEXT:
100 case VK_UP:
101 case VK_DOWN:
102 case VK_LEFT:
103 case VK_RIGHT:
104 if (!((lparam >> 16) & KF_EXTENDED))
105 modifiers |= EVENTFLAG_IS_KEY_PAD;
106 break;
107 case VK_NUMLOCK:
108 case VK_NUMPAD0:
109 case VK_NUMPAD1:
110 case VK_NUMPAD2:
111 case VK_NUMPAD3:
112 case VK_NUMPAD4:
113 case VK_NUMPAD5:
114 case VK_NUMPAD6:
115 case VK_NUMPAD7:
116 case VK_NUMPAD8:
117 case VK_NUMPAD9:
118 case VK_DIVIDE:
119 case VK_MULTIPLY:
120 case VK_SUBTRACT:
121 case VK_ADD:
122 case VK_DECIMAL:
123 case VK_CLEAR:
124 modifiers |= EVENTFLAG_IS_KEY_PAD;
125 break;
126 case VK_SHIFT:
127 if (IsKeyDown(VK_LSHIFT))
128 modifiers |= EVENTFLAG_IS_LEFT;
129 else if (IsKeyDown(VK_RSHIFT))
130 modifiers |= EVENTFLAG_IS_RIGHT;
131 break;
132 case VK_CONTROL:
133 if (IsKeyDown(VK_LCONTROL))
134 modifiers |= EVENTFLAG_IS_LEFT;
135 else if (IsKeyDown(VK_RCONTROL))
136 modifiers |= EVENTFLAG_IS_RIGHT;
137 break;
138 case VK_MENU:
139 if (IsKeyDown(VK_LMENU))
140 modifiers |= EVENTFLAG_IS_LEFT;
141 else if (IsKeyDown(VK_RMENU))
142 modifiers |= EVENTFLAG_IS_RIGHT;
143 break;
144 case VK_LWIN:
145 modifiers |= EVENTFLAG_IS_LEFT;
146 break;
147 case VK_RWIN:
148 modifiers |= EVENTFLAG_IS_RIGHT;
149 break;
150 }
151 return modifiers;
152 }
153
IsKeyDown(WPARAM wparam)154 bool IsKeyDown(WPARAM wparam) {
155 return (GetKeyState(wparam) & 0x8000) != 0;
156 }
157
GetDeviceScaleFactor()158 float GetDeviceScaleFactor() {
159 static float scale_factor = 1.0;
160 static bool initialized = false;
161
162 if (!initialized) {
163 // This value is safe to cache for the life time of the app since the user
164 // must logout to change the DPI setting. This value also applies to all
165 // screens.
166 HDC screen_dc = ::GetDC(nullptr);
167 int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
168 scale_factor = static_cast<float>(dpi_x) / 96.0f;
169 ::ReleaseDC(nullptr, screen_dc);
170 initialized = true;
171 }
172
173 return scale_factor;
174 }
175
176 } // namespace client
177