Repository: microsoft/terminal
File: src/terminal/adapter/adaptDispatch.cpp (C++)

Code before:
void AdaptDispatch::_SetParserMode(const StateMachine::Mode mode, const bool enable)
{
    _api.GetStateMachine().SetParserMode(mode, enable);
}

// Routine Description:
// - Sets the various terminal input modes.
// Arguments:
// - mode - the input mode to change.
// - enable - set to true to enable the mode, false to disable it.
// Return Value:
// - true if successful. false otherwise.
bool AdaptDispatch::_SetInputMode(const TerminalInput::Mode mode, const bool enable)
{
    _terminalInput.SetInputMode(mode, enable);

    // If we're a conpty, AND WE'RE IN VT INPUT MODE, always pass input mode requests
    // The VT Input mode check is to work around ssh.exe v7.7, which uses VT
    // output, but not Input.
    // The original comment said, "Once the conpty supports these types of input,
    // this check can be removed. See GH#4911". Unfortunately, time has shown
    // us that SSH 7.7 _also_ requests mouse input and that can have a user interface
    // impact on the actual connected terminal. We can't remove this check,
    // because SSH <=7.7 is out in the wild on all versions of Windows <=2004.

    // GH#12799 - If the app requested that we disable focus events, DON'T pass
    // that through. ConPTY would _always_ like to know about focus events.

    return !_api.IsConsolePty() ||
           !_api.IsVtInputEnabled() ||
           (!enable && mode == TerminalInput::Mode::FocusEvent);

    // Another way of writing the above statement is:
    //
    // const bool inConpty = _api.IsConsolePty();
    // const bool shouldPassthrough = inConpty && _api.IsVtInputEnabled();
    // const bool disabledFocusEvents = inConpty && (!enable && mode == TerminalInput::Mode::FocusEvent);
    // return !shouldPassthrough || disabledFocusEvents;
    //
    // It's like a "filter" left to right. Due to the early return via
    // !IsConsolePty, once you're at the !enable part, IsConsolePty can only be
    // true anymore.
}

// Routine Description:
// - Support routine for routing private mode parameters to be set/reset as flags
// Arguments:
// - param - mode parameter to set/reset
// - enable - True for set, false for unset.
// Return Value:
// - True if handled successfully. False otherwise.

Review comment (suggestion):
Note to reviewers, if you didn't read the PR description, the additional check here that was specific to the `FocusEvent` mode has now been moved down into the `EnableFocusEventMode` method. I think this reduces some of the confusion that was introduced here, and also avoids unnecessary overhead in the other input mode operations.

This is isn't strictly related to `DECBKM` mode, though (other than as a minor optimisation), so if you'd prefer not to include this change in the PR, it wouldn't be a problem to revert it.

Code after:
void AdaptDispatch::_SetParserMode(const StateMachine::Mode mode, const bool enable)
{
    _api.GetStateMachine().SetParserMode(mode, enable);
}

// Routine Description:
// - Sets the various terminal input modes.
// Arguments:
// - mode - the input mode to change.
// - enable - set to true to enable the mode, false to disable it.
// Return Value:
// - true if successful. false otherwise.
bool AdaptDispatch::_SetInputMode(const TerminalInput::Mode mode, const bool enable)
{
    _terminalInput.SetInputMode(mode, enable);

    // If we're a conpty, AND WE'RE IN VT INPUT MODE, always pass input mode requests
    // The VT Input mode check is to work around ssh.exe v7.7, which uses VT
    // output, but not Input.
    // The original comment said, "Once the conpty supports these types of input,
    // this check can be removed. See GH#4911". Unfortunately, time has shown
    // us that SSH 7.7 _also_ requests mouse input and that can have a user interface
    // impact on the actual connected terminal. We can't remove this check,
    // because SSH <=7.7 is out in the wild on all versions of Windows <=2004.

    return !_api.IsConsolePty() || !_api.IsVtInputEnabled();
}

// Routine Description:
// - Support routine for routing private mode parameters to be set/reset as flags
// Arguments:
// - param - mode parameter to set/reset
// - enable - True for set, false for unset.
// Return Value:
// - True if handled successfully. False otherwise.
bool AdaptDispatch::_ModeParamsHelper(const DispatchTypes::ModeParams param, const bool enable)
{
    auto success = false;
    switch (param)
    {
    case DispatchTypes::ModeParams::DECCKM_CursorKeysMode:
        // set - Enable Application Mode, reset - Normal mode
        success = SetCursorKeysMode(enable);
        break;
    case DispatchTypes::ModeParams::DECANM_AnsiMode:
        success = SetAnsiMode(enable);
        break;
    case DispatchTypes::ModeParams::DECCOLM_SetNumberOfColumns:
        success = _DoDECCOLMHelper(enable ? DispatchTypes::s_sDECCOLMSetColumns : DispatchTypes::s_sDECCOLMResetColumns);
        break;
    case DispatchTypes::ModeParams::DECSCNM_ScreenMode: