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

Mouse events are double-encoded with win32-input-mode #17851

Closed
lhecker opened this issue Sep 3, 2024 · 0 comments · Fixed by #17833
Closed

Mouse events are double-encoded with win32-input-mode #17851

lhecker opened this issue Sep 3, 2024 · 0 comments · Fixed by #17833
Labels
In-PR This issue has a related PR Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Priority-2 A description (P2) Product-Conpty For console issues specifically related to conpty Product-Terminal The new Windows Terminal.

Comments

@lhecker
Copy link
Member

lhecker commented Sep 3, 2024

Windows Terminal version

1.23.2421.0

Windows build number

No response

Other Software

No response

Steps to reproduce

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

int main() {
    const auto inputHandle = GetStdHandle(STD_INPUT_HANDLE);
    const auto outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);

    const auto previousInputCP = GetConsoleCP();
    const auto previousOutputCP = GetConsoleOutputCP();
    DWORD previousInputMode = 0;
    DWORD previousOutputMode = 0;
    GetConsoleMode(inputHandle, &previousInputMode);
    GetConsoleMode(outputHandle, &previousOutputMode);

    SetConsoleCP(CP_UTF8);
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleMode(inputHandle, ENABLE_PROCESSED_INPUT | ENABLE_WINDOW_INPUT | ENABLE_QUICK_EDIT_MODE | ENABLE_EXTENDED_FLAGS | ENABLE_VIRTUAL_TERMINAL_INPUT);
    SetConsoleMode(outputHandle, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN);

    static bool exit;
    SetConsoleCtrlHandler([](DWORD) -> BOOL {
        exit = true;
        return TRUE;
    }, TRUE);

    WriteFile(outputHandle, "\033[?9001h\x1b[?1003;1006h", 21, nullptr, nullptr);

    char buffer[4096];
    while (!exit) {
        DWORD read;
        ReadFile(inputHandle, &buffer[0], sizeof(buffer), &read, nullptr);
        if (read == 0 || (read == 1 && (buffer[0] == 'q' || buffer[0] == 'Q'))) {
            break;
        }

        // Expand buffer and replace Esc with "\\x1b"
        char expanded[4 * ARRAYSIZE(buffer)];
        size_t expendedLength = 0;
        for (size_t i = 0; i < read; i++) {
            if (buffer[i] == 27) {
                expanded[expendedLength++] = '\\';
                expanded[expendedLength++] = 'x';
                expanded[expendedLength++] = '1';
                expanded[expendedLength++] = 'b';
            } else {
                expanded[expendedLength++] = buffer[i];
            }
        }

        expanded[expendedLength++] = '\r';
        expanded[expendedLength++] = '\n';
        WriteFile(outputHandle, expanded, expendedLength, nullptr, nullptr);
    }

    WriteFile(outputHandle, "\033[?9001l\x1b[?1003;1006l", 21, nullptr, nullptr);
    
    SetConsoleMode(inputHandle, previousInputMode);
    SetConsoleMode(outputHandle, previousOutputMode);
    SetConsoleCP(previousInputCP);
    SetConsoleOutputCP(previousOutputCP);
    return 0;
}

Expected Behavior

No response

Actual Behavior

Double-encoding!

@lhecker lhecker added Product-Conpty For console issues specifically related to conpty Issue-Bug It either shouldn't be doing this or needs an investigation. Product-Terminal The new Windows Terminal. Priority-2 A description (P2) labels Sep 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Sep 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR This issue has a related PR label Sep 3, 2024
@lhecker lhecker closed this as completed in 7b50f12 Sep 4, 2024
DHowett pushed a commit that referenced this issue Sep 4, 2024
`GetChar` checks if the vkey is VK_ESCAPE. `CharToKeyEvents` however
tries really hard to figure out the vkeys of all characters.
To avoid these issues all we need to do is to simply use the existing
`WriteString` function we already use for all other VT responses.
If it's good for conhost responses, it's good for ConPTY responses.

Additionally, this removes another `IsVtInputEnabled` which was
redundant with `WriteString` which worked exactly the same internally.

Closes #17813
Closes #17851
Probably also related to #17823

* Wrote a small app to send and receive a DA1 request. It works ✅
* WSL already worked to begin with (and still works now) ✅
* No double-encoding of mouse input events ✅

(cherry picked from commit 7b50f12)
Service-Card-Id: PVTI_lADOAF3p4s4AmhmQzgSa4RU PVTI_lADOAF3p4s4AmhmQzgStRA0
Service-Version: 1.22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR This issue has a related PR Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Priority-2 A description (P2) Product-Conpty For console issues specifically related to conpty Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant