Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

11.9 KB
Raw Blame
/**
 * Windows Fast Paste for OpenWhispr
 *
 * Detects the foreground window, checks if it's a terminal emulator,
 * and simulates the appropriate paste keystroke using Win32 SendInput:
 *   - Ctrl+V for normal applications
 *   - Ctrl+Shift+V for terminal emulators
 *
 * Terminal detection uses two strategies:
 *   1. Window class name (fast, works for native terminals)
 *   2. Executable name (fallback, catches Electron-based terminals like Termius)
 *
 * Focus restore (issue #859): the caller captures the target window handle at
 * record start (the hex "TARGET %p" line that --detect-only prints), then
 * passes it back as --restore-window <hwnd> at paste time. We re-activate that
 * window before sending the keystroke so dictation lands in the field the user
 * was in, even when something stole the foreground during transcription. This
 * mirrors the macOS PID capture/activate path and the Linux --window path.
 *
 * Compile with: cl /O2 windows-fast-paste.c /Fe:windows-fast-paste.exe user32.lib
 * Or with MinGW: gcc -O2 windows-fast-paste.c -o windows-fast-paste.exe -luser32
 */

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

static const char* TERMINAL_CLASSES[] = {
    "ConsoleWindowClass",
    "CASCADIA_HOSTING_WINDOW_CLASS",
    "mintty",
    "VirtualConsoleClass",
    "PuTTY",
    "Alacritty",
    "org.wezfurlong.wezterm",
    "Hyper",
    "TMobaXterm",
    "kitty",
    NULL
};

/* Electron-based terminals share Chrome_WidgetWin_1 as window class,
   so we detect them by executable name instead */
static const char* TERMINAL_EXES[] = {
    "termius.exe",
    "tabby.exe",
    "wave.exe",
    "rio.exe",
    NULL
};

static BOOL IsTerminalClass(const char* className) {
    for (int i = 0; TERMINAL_CLASSES[i] != NULL; i++) {
        if (_stricmp(className, TERMINAL_CLASSES[i]) == 0) {
            return TRUE;
        }
    }
    return FALSE;
}

static BOOL GetExeName(HWND hwnd, char* exeName, DWORD exeNameSize) {
    DWORD pid = 0;
    GetWindowThreadProcessId(hwnd, &pid);
    if (pid == 0) return FALSE;

    HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
    if (!hProcess) return FALSE;

    char exePath[MAX_PATH];
    DWORD pathLen = MAX_PATH;
    BOOL ok = QueryFullProcessImageNameA(hProcess, 0, exePath, &pathLen);
    CloseHandle(hProcess);

    if (!ok || pathLen == 0) return FALSE;

    const char* baseName = strrchr(exePath, '\\');
    baseName = baseName ? baseName + 1 : exePath;

    strncpy(exeName, baseName, exeNameSize - 1);
    exeName[exeNameSize - 1] = '\0';
    return TRUE;
}

/* The window title as one UTF-8 line, for the Tone & Style app classifier: a
   browser's title names the site it shows. Control characters and the
   Unicode line separators become spaces, so a title can't start a line of
   its own in this output. */
static BOOL GetWindowTitleUtf8(HWND hwnd, char* title, int titleSize) {
    wchar_t wideTitle[512];
    int wideLen = GetWindowTextW(hwnd, wideTitle, 512);
    if (wideLen <= 0) return FALSE;
    for (int i = 0; i < wideLen; i++) {
        if (wideTitle[i] < 0x20 || wideTitle[i] == 0x2028 || wideTitle[i] == 0x2029) {
            wideTitle[i] = L' ';
        }
    }

    int len = WideCharToMultiByte(CP_UTF8, 0, wideTitle, wideLen, title, titleSize - 1, NULL, NULL);
    if (len <= 0) return FALSE;
    title[len] = '\0';
    return TRUE;
}

static BOOL IsTerminalExe(const char* exeName) {
    for (int i = 0; TERMINAL_EXES[i] != NULL; i++) {
        if (_stricmp(exeName, TERMINAL_EXES[i]) == 0) {
            return TRUE;
        }
    }
    return FALSE;
}

static void SetKey(INPUT* input, WORD vk, DWORD flags) {
    input->type = INPUT_KEYBOARD;
    input->ki.wVk = vk;
    input->ki.wScan = (WORD)MapVirtualKeyA(vk, MAPVK_VK_TO_VSC);
    input->ki.dwFlags = flags;
}

/* Modifier keys that may interfere with SendInput if held by the user's
   hotkey.  We check left/right-specific variants since GetAsyncKeyState
   reports physical key state per side. */
static const WORD MODIFIER_VKS[] = {
    VK_LCONTROL, VK_RCONTROL,
    VK_LSHIFT,   VK_RSHIFT,
    VK_LMENU,    VK_RMENU,
    VK_LWIN,     VK_RWIN,
};
#define NUM_MODIFIERS (sizeof(MODIFIER_VKS) / sizeof(MODIFIER_VKS[0]))

/* Release any modifier keys that are currently held down so they don't
   contaminate the paste keystroke.  Returns the count of keys released;
   the caller must pass the same arrays back to RestoreModifiers(). */
static int ReleaseModifiers(INPUT* released, WORD* releasedVKs) {
    int count = 0;
    for (int i = 0; i < (int)NUM_MODIFIERS; i++) {
        if (GetAsyncKeyState(MODIFIER_VKS[i]) & 0x8000) {
            releasedVKs[count] = MODIFIER_VKS[i];
            SetKey(&released[count], MODIFIER_VKS[i], KEYEVENTF_KEYUP);
            count++;
        }
    }
    if (count > 0) {
        SendInput((UINT)count, released, sizeof(INPUT));
    }
    return count;
}

/* Re-press modifier keys that were released by ReleaseModifiers(). */
static void RestoreModifiers(WORD* releasedVKs, int count) {
    if (count == 0) return;
    INPUT restore[NUM_MODIFIERS];
    ZeroMemory(restore, sizeof(restore));
    for (int i = 0; i < count; i++) {
        SetKey(&restore[i], releasedVKs[i], 0);
    }
    SendInput((UINT)count, restore, sizeof(INPUT));
}

static int SendPasteNormal(void) {
    INPUT inputs[4];
    ZeroMemory(inputs, sizeof(inputs));

    SetKey(&inputs[0], VK_LCONTROL, 0);
    SetKey(&inputs[1], 'V', 0);
    SetKey(&inputs[2], 'V', KEYEVENTF_KEYUP);
    SetKey(&inputs[3], VK_LCONTROL, KEYEVENTF_KEYUP);

    UINT sent = SendInput(4, inputs, sizeof(INPUT));
    return (sent == 4) ? 0 : 1;
}

/* Bring a captured target window back to the foreground before pasting.
   SetForegroundWindow alone is blocked by Windows' foreground lock when the
   calling process isn't the current foreground owner, so we attach this
   thread's input queue to both the current foreground thread and the target
   thread first — the standard workaround, and what nircmd's "win activate"
   does internally. Returns TRUE if the target ended up foreground. */
static BOOL RestoreForegroundWindow(HWND target) {
    if (!target || !IsWindow(target)) return FALSE;

    if (GetForegroundWindow() == target) return TRUE;

    if (IsIconic(target)) {
        ShowWindow(target, SW_RESTORE);
    }

    HWND fg = GetForegroundWindow();
    DWORD thisThread = GetCurrentThreadId();
    DWORD targetThread = GetWindowThreadProcessId(target, NULL);
    DWORD fgThread = fg ? GetWindowThreadProcessId(fg, NULL) : 0;

    BOOL attachedTarget = FALSE;
    BOOL attachedFg = FALSE;
    if (targetThread && targetThread != thisThread) {
        attachedTarget = AttachThreadInput(thisThread, targetThread, TRUE);
    }
    if (fgThread && fgThread != thisThread && fgThread != targetThread) {
        attachedFg = AttachThreadInput(thisThread, fgThread, TRUE);
    }

    BringWindowToTop(target);
    BOOL ok = SetForegroundWindow(target);
    SetFocus(target);

    if (attachedFg) AttachThreadInput(thisThread, fgThread, FALSE);
    if (attachedTarget) AttachThreadInput(thisThread, targetThread, FALSE);

    return ok && GetForegroundWindow() == target;
}

static int SendPasteTerminal(void) {
    INPUT inputs[6];
    ZeroMemory(inputs, sizeof(inputs));

    SetKey(&inputs[0], VK_LCONTROL, 0);
    SetKey(&inputs[1], VK_LSHIFT, 0);
    SetKey(&inputs[2], 'V', 0);
    SetKey(&inputs[3], 'V', KEYEVENTF_KEYUP);
    SetKey(&inputs[4], VK_LSHIFT, KEYEVENTF_KEYUP);
    SetKey(&inputs[5], VK_LCONTROL, KEYEVENTF_KEYUP);

    UINT sent = SendInput(6, inputs, sizeof(INPUT));
    return (sent == 6) ? 0 : 1;
}

static int SendCopyNormal(void) {
    INPUT inputs[4];
    ZeroMemory(inputs, sizeof(inputs));

    SetKey(&inputs[0], VK_LCONTROL, 0);
    SetKey(&inputs[1], 'C', 0);
    SetKey(&inputs[2], 'C', KEYEVENTF_KEYUP);
    SetKey(&inputs[3], VK_LCONTROL, KEYEVENTF_KEYUP);

    return (SendInput(4, inputs, sizeof(INPUT)) == 4) ? 0 : 1;
}

static int SendCopyTerminal(void) {
    INPUT inputs[6];
    ZeroMemory(inputs, sizeof(inputs));

    SetKey(&inputs[0], VK_LCONTROL, 0);
    SetKey(&inputs[1], VK_LSHIFT, 0);
    SetKey(&inputs[2], 'C', 0);
    SetKey(&inputs[3], 'C', KEYEVENTF_KEYUP);
    SetKey(&inputs[4], VK_LSHIFT, KEYEVENTF_KEYUP);
    SetKey(&inputs[5], VK_LCONTROL, KEYEVENTF_KEYUP);

    return (SendInput(6, inputs, sizeof(INPUT)) == 6) ? 0 : 1;
}

int main(int argc, char* argv[]) {
    BOOL detectOnly = FALSE;
    BOOL copyMode = FALSE;
    BOOL capabilitiesOnly = FALSE;
    HWND restoreWindow = NULL;

    for (int i = 1; i < argc; i++) {
        if (strcmp(argv[i], "--detect-only") == 0) {
            detectOnly = TRUE;
        } else if (strcmp(argv[i], "--copy") == 0) {
            copyMode = TRUE;
        } else if (strcmp(argv[i], "--capabilities") == 0) {
            capabilitiesOnly = TRUE;
        } else if (strcmp(argv[i], "--restore-window") == 0 && i + 1 < argc) {
            /* Base 16: the handle comes back exactly as --detect-only printed
               it with "TARGET %p" (hex, with or without an 0x prefix). */
            restoreWindow = (HWND)(uintptr_t)strtoull(argv[++i], NULL, 16);
        }
    }

    if (capabilitiesOnly) {
        printf("paste-v1 selection-copy-v1 target-identity-v1 focus-restore-v1\n");
        return 0;
    }

    /* Restore the captured target to the foreground before pasting. If it is
       gone or can't be restored we fall through to whatever is foreground now,
       which is the pre-#859 behavior. The settle sleep only applies when a
       window switch actually happened — in the common case where the target
       never lost the foreground, the paste goes out immediately. */
    if (restoreWindow && GetForegroundWindow() != restoreWindow &&
        RestoreForegroundWindow(restoreWindow)) {
        Sleep(20);
    }

    HWND hwnd = GetForegroundWindow();
    if (!hwnd) {
        fprintf(stderr, "ERROR: No foreground window found\n");
        return 2;
    }

    char className[256];
    int classLen = GetClassNameA(hwnd, className, sizeof(className));
    if (classLen == 0) {
        fprintf(stderr, "ERROR: Could not get window class name (error %lu)\n", GetLastError());
        return 1;
    }

    BOOL isTerminal = IsTerminalClass(className);

    char exeName[MAX_PATH] = {0};
    BOOL gotExeName = GetExeName(hwnd, exeName, sizeof(exeName));

    if (!isTerminal && gotExeName) {
        isTerminal = IsTerminalExe(exeName);
    }

    if (detectOnly) {
        printf("TARGET %p\n", (void*)hwnd);
        printf("WINDOW_CLASS %s\n", className);
        if (gotExeName) {
            printf("EXE_NAME %s\n", exeName);
        }
        /* 511 UTF-16 units take at most 1533 UTF-8 bytes. */
        char title[1536];
        if (GetWindowTitleUtf8(hwnd, title, sizeof(title))) {
            printf("WINDOW_TITLE %s\n", title);
        }
        printf("IS_TERMINAL %s\n", isTerminal ? "true" : "false");
        fflush(stdout);
        return 0;
    }

    Sleep(10);

    /* Release any modifier keys held by the user's hotkey so they don't
       contaminate the paste keystroke (e.g. Ctrl+Win held → Ctrl+Win+V). */
    INPUT releasedInputs[NUM_MODIFIERS];
    WORD  releasedVKs[NUM_MODIFIERS];
    ZeroMemory(releasedInputs, sizeof(releasedInputs));
    int releasedCount = ReleaseModifiers(releasedInputs, releasedVKs);

    int result;
    if (copyMode && isTerminal) {
        result = SendCopyTerminal();
    } else if (copyMode) {
        result = SendCopyNormal();
    } else if (isTerminal) {
        result = SendPasteTerminal();
    } else {
        result = SendPasteNormal();
    }

    RestoreModifiers(releasedVKs, releasedCount);

    if (result != 0) {
        fprintf(stderr, "ERROR: SendInput failed (error %lu)\n", GetLastError());
        return 1;
    }

    Sleep(20);

    if (copyMode) {
        printf("COPY_OK %p %s %s\n", (void*)hwnd, className,
               isTerminal ? "ctrl+shift+c" : "ctrl+c");
    } else {
        printf("PASTE_OK %s %s\n", className, isTerminal ? "ctrl+shift+v" : "ctrl+v");
    }
    fflush(stdout);

    return 0;
}