[Win32, Keyboard] Lift redispatching to messages, via SendMessage. (flutter/engine#31200)

This commit is contained in:
Tong Mu
2022-02-02 15:30:10 -08:00
committed by GitHub
parent 5a5efe7a33
commit 973b9ef231
8 changed files with 338 additions and 512 deletions

View File

@@ -93,13 +93,9 @@ class SpyTextInputPlugin : public TextInputPlugin,
std::unique_ptr<TextInputPlugin> real_implementation_;
};
class MockFlutterWindowWin32 : public FlutterWindowWin32,
public MockMessageQueue {
class MockFlutterWindowWin32 : public FlutterWindowWin32 {
public:
MockFlutterWindowWin32(WPARAM virtual_key = 0, bool is_printable = true)
: virtual_key_(virtual_key),
is_printable_(is_printable),
FlutterWindowWin32(800, 600) {
MockFlutterWindowWin32() : FlutterWindowWin32(800, 600) {
ON_CALL(*this, GetDpiScale())
.WillByDefault(Return(this->FlutterWindowWin32::GetDpiScale()));
}
@@ -112,25 +108,6 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32,
// Wrapper for GetCurrentDPI() which is a protected method.
UINT GetDpi() { return GetCurrentDPI(); }
// Simulates a WindowProc message from the OS.
LRESULT InjectWindowMessage(UINT const message,
WPARAM const wparam,
LPARAM const lparam) {
return Win32SendMessage(message, wparam, lparam);
}
void InjectMessages(int count, Win32Message message1, ...) {
Win32Message messages[count];
messages[0] = message1;
va_list args;
va_start(args, message1);
for (int i = 1; i < count; i += 1) {
messages[i] = va_arg(args, Win32Message);
}
va_end(args);
InjectMessageList(count, messages);
}
MOCK_METHOD1(OnDpiScale, void(unsigned int));
MOCK_METHOD2(OnResize, void(unsigned int, unsigned int));
MOCK_METHOD4(OnPointerMove,
@@ -147,17 +124,11 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32,
MOCK_METHOD0(IsVisible, bool());
MOCK_METHOD1(UpdateCursorRect, void(const Rect&));
MOCK_METHOD0(OnResetImeComposing, void());
MOCK_METHOD3(Win32DispatchMessage, UINT(UINT, WPARAM, LPARAM));
MOCK_METHOD4(Win32PeekMessage, BOOL(LPMSG, UINT, UINT, UINT));
MOCK_METHOD1(Win32MapVkToChar, uint32_t(uint32_t));
protected:
// |KeyboardManagerWin32::WindowDelegate|
BOOL Win32PeekMessage(LPMSG lpMsg,
UINT wMsgFilterMin,
UINT wMsgFilterMax,
UINT wRemoveMsg) override {
return MockMessageQueue::Win32PeekMessage(lpMsg, wMsgFilterMin,
wMsgFilterMax, wRemoveMsg);
}
// |KeyboardManagerWin32::WindowDelegate|
LRESULT Win32DefWindowProc(HWND hWnd,
UINT Msg,
@@ -165,52 +136,6 @@ class MockFlutterWindowWin32 : public FlutterWindowWin32,
LPARAM lParam) override {
return kWmResultDefault;
}
// |KeyboardManagerWin32::WindowDelegate|
UINT Win32DispatchEvent(UINT cInputs, LPINPUT pInputs, int cbSize) override {
for (UINT input_idx = 0; input_idx < cInputs; input_idx += 1) {
SendInput(pInputs[input_idx].ki);
}
return 1;
}
// |MockMessageQueue|
LRESULT Win32SendMessage(UINT const message,
WPARAM const wparam,
LPARAM const lparam) override {
return HandleMessage(message, wparam, lparam);
}
private:
UINT SendInput(KEYBDINPUT kbdinput) {
// Simulate the event loop by just sending the event sent to
// "SendInput" directly to the window.
const bool is_key_up = kbdinput.dwFlags & KEYEVENTF_KEYUP;
const UINT message = is_key_up ? WM_KEYUP : WM_KEYDOWN;
const LPARAM lparam = CreateKeyEventLparam(
kbdinput.wScan, kbdinput.dwFlags & KEYEVENTF_EXTENDEDKEY, is_key_up);
// Windows would normally fill in the virtual key code for us, so we
// simulate it for the test with the key we know is in the test. The
// KBDINPUT we're passed doesn't have it filled in (on purpose, so that
// Windows will fill it in).
//
// TODO(dkwingsmt): Don't check the message results for redispatched
// messages for now, because making them work takes non-trivial rework
// to our current structure. https://github.com/flutter/flutter/issues/87843
// If this is resolved, change them to kWmResultDefault.
pending_responds_.push_back(
Win32Message{message, virtual_key_, lparam, kWmResultDontCheck});
if (is_printable_ && (kbdinput.dwFlags & KEYEVENTF_KEYUP) == 0) {
pending_responds_.push_back(
Win32Message{WM_CHAR, virtual_key_, lparam, kWmResultDontCheck});
}
return 1;
}
std::vector<Win32Message> pending_responds_;
WPARAM virtual_key_;
bool is_printable_;
};
class MockWindowBindingHandlerDelegate : public WindowBindingHandlerDelegate {

View File

@@ -133,55 +133,31 @@ KeyboardManagerWin32::KeyboardManagerWin32(WindowDelegate* delegate)
last_key_is_ctrl_left_down(false),
should_synthesize_ctrl_left_up(false) {}
void KeyboardManagerWin32::DispatchEvent(const PendingEvent& event) {
assert(event.action != WM_SYSKEYDOWN && event.action != WM_SYSKEYUP &&
"Unexpectedly dispatching a SYS event. SYS events can't be dispatched "
"and should have been prevented in earlier code.");
char32_t character = event.character;
INPUT input_event{
.type = INPUT_KEYBOARD,
.ki =
KEYBDINPUT{
.wVk = static_cast<WORD>(event.key),
.wScan = static_cast<WORD>(event.scancode),
.dwFlags = static_cast<WORD>(
KEYEVENTF_SCANCODE |
(event.extended ? KEYEVENTF_EXTENDEDKEY : 0x0) |
(event.action == WM_KEYUP ? KEYEVENTF_KEYUP : 0x0)),
},
};
UINT accepted = window_delegate_->Win32DispatchEvent(1, &input_event,
sizeof(input_event));
if (accepted != 1) {
std::cerr << "Unable to synthesize event for keyboard event with scancode "
<< event.scancode;
if (character != 0) {
std::cerr << " (character " << character << ")";
}
std::cerr << std::endl;
}
}
void KeyboardManagerWin32::RedispatchEvent(
std::unique_ptr<PendingEvent> event) {
DispatchEvent(*event);
for (const Win32Message& message : event->session) {
pending_redispatches_.push_back(message);
UINT result = window_delegate_->Win32DispatchMessage(
message.action, message.wparam, message.lparam);
if (result != 0) {
std::cerr << "Unable to synthesize event for keyboard event."
<< std::endl;
}
}
if (pending_redispatches_.size() > kMaxPendingEvents) {
std::cerr
<< "There are " << pending_redispatches_.size()
<< " keyboard events that have not yet received a response from the "
<< "framework. Are responses being sent?" << std::endl;
}
pending_redispatches_.push_back(std::move(event));
}
bool KeyboardManagerWin32::RemoveRedispatchedEvent(
const PendingEvent& incoming) {
bool KeyboardManagerWin32::RemoveRedispatchedMessage(UINT const action,
WPARAM const wparam,
LPARAM const lparam) {
for (auto iter = pending_redispatches_.begin();
iter != pending_redispatches_.end(); ++iter) {
if ((*iter)->Hash() == incoming.Hash()) {
if (action == iter->action && wparam == iter->wparam) {
pending_redispatches_.erase(iter);
return true;
}
@@ -189,12 +165,8 @@ bool KeyboardManagerWin32::RemoveRedispatchedEvent(
return false;
}
bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
void KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
OnKeyCallback callback) {
if (RemoveRedispatchedEvent(*event)) {
return false;
}
if (IsKeyDownAltRight(event->action, event->key, event->extended)) {
if (last_key_is_ctrl_left_down) {
should_synthesize_ctrl_left_up = true;
@@ -210,13 +182,11 @@ bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
if (IsKeyUpAltRight(event->action, event->key, event->extended)) {
if (should_synthesize_ctrl_left_up) {
should_synthesize_ctrl_left_up = false;
PendingEvent ctrl_left_up{
.key = VK_LCONTROL,
.scancode = ctrl_left_scancode,
.action = WM_KEYUP,
.was_down = true,
};
DispatchEvent(ctrl_left_up);
const LPARAM lParam =
(1 /* repeat_count */ << 0) | (ctrl_left_scancode << 16) |
(0 /* extended */ << 24) | (1 /* prev_state */ << 30) |
(1 /* transition */ << 31);
window_delegate_->Win32DispatchMessage(WM_KEYUP, VK_LCONTROL, lParam);
}
}
@@ -228,14 +198,21 @@ bool KeyboardManagerWin32::OnKey(std::unique_ptr<PendingEvent> event,
callback(std::unique_ptr<PendingEvent>(event),
handled);
});
return true;
}
void KeyboardManagerWin32::DispatchReadyTexts() {
auto front = pending_texts_.begin();
for (; front != pending_texts_.end() && front->ready; ++front) {
window_delegate_->OnText(front->content);
}
pending_texts_.erase(pending_texts_.begin(), front);
}
void KeyboardManagerWin32::HandleOnKeyResult(
std::unique_ptr<PendingEvent> event,
bool handled,
int char_action,
std::u16string text) {
std::list<PendingText>::iterator pending_text) {
// First, patch |handled|, because some key events must always be treated as
// handled.
//
@@ -253,6 +230,9 @@ void KeyboardManagerWin32::HandleOnKeyResult(
// For handled events, that's all.
if (real_handled) {
if (pending_text != pending_texts_.end()) {
pending_texts_.erase(pending_text);
}
return;
}
@@ -263,8 +243,10 @@ void KeyboardManagerWin32::HandleOnKeyResult(
// will be incorporated into a later WM_CHAR with the full character.
// Non-printable event characters have been filtered out before being passed
// to OnKey.
if (char_action == WM_CHAR && event->character != 0) {
window_delegate_->OnText(text);
if (char_action == WM_CHAR && event->character != 0 &&
pending_text != pending_texts_.end()) {
pending_text->ready = true;
DispatchReadyTexts();
}
RedispatchEvent(std::move(event));
@@ -273,6 +255,9 @@ void KeyboardManagerWin32::HandleOnKeyResult(
bool KeyboardManagerWin32::HandleMessage(UINT const action,
WPARAM const wparam,
LPARAM const lparam) {
if (RemoveRedispatchedMessage(action, wparam, lparam)) {
return false;
}
switch (action) {
case WM_DEADCHAR:
case WM_SYSDEADCHAR:
@@ -314,7 +299,6 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
// OnText if the key down event is not handled.
if (current_session_.front().IsGeneralKeyDown()) {
const Win32Message first_message = current_session_.front();
current_session_.clear();
const uint8_t scancode = (lparam >> 16) & 0xff;
const uint16_t key_code = first_message.wparam;
const bool extended = ((lparam >> 24) & 0x01) == 0x01;
@@ -342,16 +326,22 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
.was_down = was_down,
.session = std::move(current_session_),
});
const bool is_unmet_event = OnKey(
std::move(event),
[this, char_action = action, text](
std::unique_ptr<PendingEvent> event, bool handled) {
HandleOnKeyResult(std::move(event), handled, char_action, text);
});
const bool is_syskey = action == WM_SYSCHAR;
// For system characters, always pass them to the default WndProc so
// that system keys like the ALT-TAB are processed correctly.
return is_unmet_event && !is_syskey;
pending_texts_.push_back(PendingText{
.ready = false,
.content = text,
});
auto pending_text = std::prev(pending_texts_.end());
// SYS messages must not be handled by `HandleMessage` or be
// redispatched.
const bool is_syskey = action == WM_SYSCHAR || action == WM_SYSDEADCHAR;
OnKey(std::move(event),
[this, char_action = action, pending_text, is_syskey](
std::unique_ptr<PendingEvent> event, bool handled) {
bool real_handled = handled || is_syskey;
HandleOnKeyResult(std::move(event), handled, char_action,
pending_text);
});
return !is_syskey;
}
// If the charcter session is not preceded by a key down message, dispatch
@@ -365,7 +355,11 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
// events for all control key shortcuts.
current_session_.clear();
if (action == WM_CHAR && IsPrintable(wparam)) {
window_delegate_->OnText(text);
pending_texts_.push_back(PendingText{
.ready = true,
.content = text,
});
DispatchReadyTexts();
}
return true;
}
@@ -415,15 +409,16 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
.was_down = was_down,
.session = std::move(current_session_),
});
const bool is_unmet_event = OnKey(
std::move(event),
[this](std::unique_ptr<PendingEvent> event, bool handled) {
HandleOnKeyResult(std::move(event), handled, 0, std::u16string());
});
// SYS messages must not be handled by `HandleMessage` or be
// redispatched.
const bool is_syskey = action == WM_SYSKEYDOWN || action == WM_SYSKEYUP;
// For system keys, always pass them to the default WndProc so that keys
// like the ALT-TAB or Kanji switches are processed correctly.
return is_unmet_event && !is_syskey;
OnKey(std::move(event), [this, is_syskey](
std::unique_ptr<PendingEvent> event,
bool handled) {
bool real_handled = handled || is_syskey;
HandleOnKeyResult(std::move(event), handled, 0, pending_texts_.end());
});
return !is_syskey;
}
default:
assert(false);

View File

@@ -75,17 +75,17 @@ class KeyboardManagerWin32 {
// Used to process key messages.
virtual uint32_t Win32MapVkToChar(uint32_t virtual_key) = 0;
// Win32's |SendInput|.
// Win32's |SendMessage|.
//
// Used to synthesize key events.
virtual UINT Win32DispatchEvent(UINT cInputs,
LPINPUT pInputs,
int cbSize) = 0;
// Used to synthesize key messages.
virtual UINT Win32DispatchMessage(UINT Msg,
WPARAM wParam,
LPARAM lParam) = 0;
};
using KeyEventCallback = WindowDelegate::KeyEventCallback;
KeyboardManagerWin32(WindowDelegate* delegate);
explicit KeyboardManagerWin32(WindowDelegate* delegate);
// Processes Win32 messages related to keyboard and text.
//
@@ -103,11 +103,11 @@ class KeyboardManagerWin32 {
WPARAM const wparam,
LPARAM const lparam);
private:
protected:
struct Win32Message {
UINT const action;
WPARAM const wparam;
LPARAM const lparam;
UINT action;
WPARAM wparam;
LPARAM lparam;
bool IsHighSurrogate() const { return IS_HIGH_SURROGATE(wparam); }
@@ -127,22 +127,35 @@ class KeyboardManagerWin32 {
bool was_down;
std::vector<Win32Message> session;
// A value calculated out of critical event information that can be used
// to identify redispatched events.
uint64_t Hash() const { return ComputeEventHash(*this); }
};
virtual void RedispatchEvent(std::unique_ptr<PendingEvent> event);
private:
using OnKeyCallback =
std::function<void(std::unique_ptr<PendingEvent>, bool)>;
// Returns true if it's a new event, or false if it's a redispatched event.
bool OnKey(std::unique_ptr<PendingEvent> event, OnKeyCallback callback);
struct PendingText {
bool ready;
std::u16string content;
};
// Returns true if it's a new event, or false if it's a redispatched event.
void OnKey(std::unique_ptr<PendingEvent> event, OnKeyCallback callback);
// From `pending_texts_`, pop all front elements that are ready, dispatch
// them to |OnText|, and remove them.
void DispatchReadyTexts();
// Handle the result of |OnKey|, which might dispatch the text result to
// |OnText|.
//
// The `pending_text` is either a valid iterator of `pending_texts`, or its
// end(). In the latter case, this OnKey message does not contain a text.
void HandleOnKeyResult(std::unique_ptr<PendingEvent> event,
bool handled,
int char_action,
std::u16string text);
std::list<PendingText>::iterator pending_text);
// Returns the type of the next WM message.
//
@@ -152,22 +165,20 @@ class KeyboardManagerWin32 {
// If there's no message, returns 0.
UINT PeekNextMessageType(UINT wMsgFilterMin, UINT wMsgFilterMax);
void DispatchEvent(const PendingEvent& event);
// Find an event in the redispatch list that matches the given one.
//
// If an matching event is found, removes the matching event from the
// redispatch list, and returns true. Otherwise, returns false;
bool RemoveRedispatchedEvent(const PendingEvent& incoming);
void RedispatchEvent(std::unique_ptr<PendingEvent> event);
bool RemoveRedispatchedMessage(UINT action, WPARAM wparam, LPARAM lparam);
WindowDelegate* window_delegate_;
// Keeps track of all messages during the current session.
//
// At the end of a session, it is moved to the `PendingEvent`, which is
// passed to `OnKey`.
std::vector<Win32Message> current_session_;
std::map<uint16_t, std::u16string> text_for_scancode_on_redispatch_;
// Whether the last event is a CtrlLeft key down.
//
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
@@ -183,9 +194,17 @@ class KeyboardManagerWin32 {
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
bool should_synthesize_ctrl_left_up;
// The queue of key events that have been redispatched to the system but have
// A queue of potential texts derived from char messages.
//
// The text might or might not be ready when they're added, and they might
// become ready or removed later. `DispatchReadyTexts` is used to dispatch all
// ready texts from the front to `OnText`. This queue is used to ensure
// they're dispatched in their arrival order.
std::list<PendingText> pending_texts_;
// The queue of messages that have been redispatched to the system but have
// not yet been received for a second time.
std::deque<std::unique_ptr<PendingEvent>> pending_redispatches_;
std::deque<Win32Message> pending_redispatches_;
// Calculate a hash based on event data for fast comparison for a redispatched
// event.

View File

@@ -34,7 +34,8 @@ Win32Message WmKeyUpInfo::Build(LRESULT expected_result, HWND hWnd) {
Win32Message WmCharInfo::Build(LRESULT expected_result, HWND hWnd) {
uint32_t lParam = (repeat_count << 0) | (scan_code << 16) | (extended << 24) |
(context << 30) | (prev_state << 30) | (transition << 31);
(bit25 << 25) | (context << 29) | (prev_state << 30) |
(transition << 31);
return Win32Message{
.message = WM_CHAR,
.wParam = char_code,

View File

@@ -111,6 +111,11 @@ typedef struct WmCharInfo {
uint16_t repeat_count = 1;
// The 25th bit of the LParam.
//
// Some messages are sent with bit25 set. Its meaning is yet unknown.
bool bit25 = 0;
Win32Message Build(LRESULT expected_result = kWmResultDontCheck,
HWND hWnd = NULL);
} WmCharInfo;

View File

@@ -551,10 +551,8 @@ uint32_t WindowWin32::Win32MapVkToChar(uint32_t virtual_key) {
return ::MapVirtualKey(virtual_key, MAPVK_VK_TO_CHAR);
}
UINT WindowWin32::Win32DispatchEvent(UINT cInputs,
LPINPUT pInputs,
int cbSize) {
return ::SendInput(cInputs, pInputs, cbSize);
UINT WindowWin32::Win32DispatchMessage(UINT Msg, WPARAM wParam, LPARAM lParam) {
return ::SendMessage(window_handle_, Msg, wParam, lParam);
}
} // namespace flutter

View File

@@ -49,9 +49,9 @@ class WindowWin32 : public KeyboardManagerWin32::WindowDelegate {
virtual uint32_t Win32MapVkToChar(uint32_t virtual_key) override;
// |KeyboardManagerWin32::WindowDelegate|
virtual UINT Win32DispatchEvent(UINT cInputs,
LPINPUT pInputs,
int cbSize) override;
virtual UINT Win32DispatchMessage(UINT Msg,
WPARAM wParam,
LPARAM lParam) override;
protected:
// Converts a c string to a wide unicode string.