Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mutex to keyEvents in TermControlAutomationPeer #14694

Merged
1 commit merged into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions src/cascadia/TerminalControl/TermControlAutomationPeer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
if (const auto keyEventChar{ gsl::narrow_cast<wchar_t>(charCode) }; IsReadable({ &keyEventChar, 1 }))
{
_keyEvents.emplace_back(keyEventChar);
_keyEvents.lock()->emplace_back(keyEventChar);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lhecker this is an acceptable use of lock(), right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the guard should get destroyed at ; (= after the insertion finished).

}
}
}
Expand Down Expand Up @@ -202,27 +202,30 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void TermControlAutomationPeer::NotifyNewOutput(std::wstring_view newOutput)
{
// Try to suppress any events (or event data)
// that is just the keypress the user made
auto sanitized{ Sanitize(newOutput) };
while (!_keyEvents.empty() && IsReadable(sanitized))
// Try to suppress any events (or event data)
// that are just the keypresses the user made
{
if (til::toupper_ascii(sanitized.front()) == _keyEvents.front())
{
// the key event's character (i.e. the "A" key) matches
// the output character (i.e. "a" or "A" text).
// We can assume that the output character resulted from
// the pressed key, so we can ignore it.
sanitized = sanitized.substr(1);
_keyEvents.pop_front();
}
else
auto keyEvents = _keyEvents.lock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need to make this auto& or even (C++14) decltype(auto). I think that auto keyEvents will result in a copy of keyEvents (thanks to how auto works).

image

image

See how the first one compiles down to escape(9) even though we just set the value to 7 through auto m!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, but this returns a proxy object... so really it doesn't matter. You're actually using a guard<deque>, which is fine. Ignore me! You have not been bitten by C++; in fact, I have!

while (!keyEvents->empty() && IsReadable(sanitized))
{
// The output doesn't match,
// so clear the input stack and
// move on to fire the event.
_keyEvents.clear();
break;
if (til::toupper_ascii(sanitized.front()) == keyEvents->front())
{
// the key event's character (i.e. the "A" key) matches
// the output character (i.e. "a" or "A" text).
// We can assume that the output character resulted from
// the pressed key, so we can ignore it.
sanitized = sanitized.substr(1);
keyEvents->pop_front();
}
else
{
// The output doesn't match,
// so clear the input stack and
// move on to fire the event.
keyEvents->clear();
break;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalControl/TermControlAutomationPeer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
private:
winrt::weak_ref<Microsoft::Terminal::Control::implementation::TermControl> _termControl;
Control::InteractivityAutomationPeer _contentAutomationPeer;
std::deque<wchar_t> _keyEvents;
til::shared_mutex<std::deque<wchar_t>> _keyEvents;
};
}
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ TRACELOGGING_DECLARE_PROVIDER(g_hTerminalControlProvider);
#include <WinUser.h>

#include "til.h"
#include <til/mutex.h>

#include "ThrottledFunc.h"

Expand Down