#include "app.h" #include "configuration.h" #include "ui_scroll.h" #include "ui_geometry.h" #include "screenshot.h" #include "ocr.h" #include #include #include #include "input_pause.h" #include "settings_ui.h" #include "../resources/resource.h" static int settingsCloseChoice = IDNO; static int settingsClosePrompts = 0; static void CALLBACK answerSettingsClose(HWND, UINT, UINT_PTR, DWORD) { HWND dialog = FindWindowW(L"#32770", L"KSwitcher — Unsaved settings"); DWORD process{}; if (dialog && GetWindowThreadProcessId(dialog, &process) == GetCurrentThreadId() && process == GetCurrentProcessId()) { ++settingsClosePrompts; SendMessageW(dialog, WM_COMMAND, settingsCloseChoice, 0); } } LRESULT CALLBACK countControlMoves(HWND w, UINT message, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR data) { if (message == WM_WINDOWPOSCHANGING) ++*reinterpret_cast(data); return DefSubclassProc(w, message, wp, lp); } namespace ks { struct InputRegressionAccess { static void finishHistoryPreviews(App &app) { std::deque> jobs; { std::lock_guard lock(app.jobsMutex); jobs.swap(app.jobs); } for (const auto &job : jobs) job(); } static LRESULT CALLBACK probeInput(HWND w, UINT msg, WPARAM wp, LPARAM lp, UINT_PTR, DWORD_PTR context) { auto &app = *reinterpret_cast(context); static bool queuedSnippet = false; // Queue a real expansion just before TrackPopupMenu returns the pause command. if (msg == WM_EXITMENULOOP && app.inputState == App::InputState::Active) queuedSnippet = app.queueSnippet(w, 0, 2, w); if (msg == ProbeTrayIcon) return trayIconMatchesInput(app.trayBaseIcon(16, 16), wp != 0); if (msg != ProbeInputState) return DefSubclassProc(w, msg, wp, lp); // Also exercise settings/layout updates that would normally re-register shortcuts. if (wp && app.inputState == App::InputState::Paused) { app.applySettings(app.settings); app.reloadLayouts(); app.dispatch(Toggle); app.pasteEntry(L"fixture", false); const auto version = app.inputVersion.load(); KBDLLHOOKSTRUCT key{}; key.vkCode = 'A'; if (app.handleKeyboard(key, true, 0) || app.inputVersion != version) return 0; const std::string status = R"({"action":"status"})"; const auto response = app.lanCommand(Bytes(status.begin(), status.end())); if (std::string(response.begin(), response.end()).find("Input processing paused") == std::string::npos) return 0; } const bool bound = app.isHotkey(VK_F22, MOD_CONTROL | MOD_SHIFT); if (app.inputState == App::InputState::Active && app.keyboardHook && app.mouseHook && app.filesHotkeyId && bound) return 1; if (app.inputState == App::InputState::Paused && !app.keyboardHook && !app.mouseHook && !app.filesHotkeyId && !bound && !app.busy && app.snippetPending == -1 && !app.snippetTyping && queuedSnippet && !app.transformAttempt) return 2; return 0; } static void CALLBACK installProbe(HWND, UINT, UINT_PTR timer, DWORD) { auto app = App::current; if (app && app->window && SetWindowSubclass(app->window, probeInput, 1, reinterpret_cast(app))) { KillTimer(app->window, 5); // Keep the input fixture independent of update checks. SetTimer(app->window, 2, 60000, nullptr); KillTimer(nullptr, timer); } } static int snippetChoice(App &a, const std::vector &snippets) { a.settings.snippets=snippets; a.settings.autoCorrect=false; a.busy=false; a.snippetPending=-1; a.typingTarget=nullptr; a.snippetTyping=false; auto target=GetForegroundWindow(); a.run.keys=L";si"; a.run.window=target; a.run.layout=windowLayout(target); a.run.at=GetTickCount64(); KBDLLHOOKSTRUCT key{};key.vkCode='G';key.scanCode=0x22; a.keyboard(key,0); int chosen=a.snippetPending; a.busy=false;a.snippetPending=-1;a.typingTarget=nullptr;a.snippetTyping=false; return chosen; } static bool idleUndo(App &a) { a.last=LastTransform{}; a.last->at=100; a.releaseIdleMemory(120100); if (!a.last) return false; a.busy=true; a.releaseIdleMemory(120101); if (!a.last) { a.busy=false; return false; } a.busy=false; a.releaseIdleMemory(120101); return !a.last; } static bool releasedSync(const App &a) { return a.syncSnapshot.capacity()==0 && a.syncResult.settings.capacity()==0 && a.syncResult.base.capacity()==0 && a.syncResult.conflicts.capacity()==0; } static bool completedSync(App &a, int mode) { sampleConflicts(a); auto conflicts=encodeSyncConflicts(a.pendingSyncConflicts); a.syncConnection=loadSyncConfig(a.data); a.syncSnapshot=encodeSettings(a.settings); a.syncResult={mode!=1,sharedSyncSettings(a.settings),2,L"test"}; a.syncResult.base=a.syncResult.settings; a.syncResult.conflicts=a.pendingSyncConflicts; if(mode==2) a.syncResult.settings={1,2,3}; // Decode exception after joining. a.syncWorker=std::thread([]{}); a.syncRunning=true; a.syncDone=true; a.busy=true; a.pollSync(); bool retained=!a.syncSnapshot.empty() && a.syncRunning; a.busy=false; a.pollSync(); return retained && releasedSync(a) && !a.syncRunning && encodeSyncConflicts(a.pendingSyncConflicts)==conflicts; } static inline bool delivery = true; static inline int notifications = 0; static bool notify(const std::wstring &) { ++notifications; return delivery; } static void sampleConflicts(App &a) { a.conflictConnection=loadSyncConfig(a.data); a.pendingSyncConflicts={{"/autoCorrectMinLength",L"Minimum word length",{'4'},{'5'}}}; } static bool staleSync(App &a, bool connectionChanged) { auto before=encodeSettings(a.settings); a.syncConnection=loadSyncConfig(a.data); a.syncSnapshot=before; if(connectionChanged) a.syncConnection.enabled=!a.syncConnection.enabled; else a.syncSnapshot.clear(); auto remote=a.settings; remote.recent=!remote.recent; a.syncResult={true,sharedSyncSettings(remote),2,L"test"}; a.syncWorker=std::thread([]{}); a.syncRunning=true; a.syncDone=true; a.pollSync(); return encodeSettings(a.settings)==before && !a.syncRunning && releasedSync(a) && !std::filesystem::exists(a.data/L"sync-base.bin"); } static bool syncBackoff(App &a) { a.syncRetryAfter=GetTickCount64()+15*60000; auto previous=a.settings; auto next=previous; next.toggle=next.casing=next.translit=next.undo=next.clipboard=next.screenshot=L""; next.direct.clear(); next.directIds.clear(); // This harness deliberately has no global keyboard hook. a.applySettings(next); bool preserved=a.nextSync==a.syncRetryAfter; a.settings=previous; a.syncRetryAfter=0; return preserved; } static void notificationSetup(App &a) { a.updateNotifier = notify; } static void setup(App &a) { a.hookBindings = {{Toggle,{MOD_CONTROL,VK_F12}}, {Case,{MOD_CONTROL|MOD_SHIFT,'U'}}}; } static LRESULT routedKey(int code, UINT message, const KBDLLHOOKSTRUCT &key, LRESULT routed) { return App::keyboardAfterHooks(code,message,(LPARAM)&key,routed); } static bool updateTimer(App &a) { a.updateRunning=true; // Exercise timer scheduling without launching a network request. App::windowProc(a.window,WM_TIMER,5,0); bool first=KillTimer(a.window,5)!=FALSE; App::windowProc(a.window,WM_TIMER,5,0); bool repeated=KillTimer(a.window,5)!=FALSE; a.updateRunning=false; return first && repeated && !a.updateWorker.joinable(); } static std::wstring updateResult(App &a,const wchar_t *state,const wchar_t *version) { auto result=new UpdateResult; result->state=state; result->version=version; App::windowProc(a.window,UpdateMessage,0,(LPARAM)result); return a.notifiedUpdate; } static void click() { MSLLHOOKSTRUCT m{}; m.flags=LLMHF_INJECTED; App::mouseProc(HC_ACTION,WM_LBUTTONDOWN,(LPARAM)&m); } static bool wheelQueuesAction(App &app) { MSLLHOOKSTRUCT mouse{}; if (!GetCursorPos(&mouse.pt) || !WindowFromPoint(mouse.pt)) return false; auto previous = app.settings; app.settings.customMouseEvents[L"Wheel down"] = L"Wheel test"; app.settings.mouseBindings[GlobalMouseProfile][L"Wheel down"] = {MouseActionType::Mute}; mouse.mouseData = MAKEWPARAM(0, static_cast(-30)); const auto wheel = [&] { return App::mouseProc(HC_ACTION, WM_MOUSEWHEEL, reinterpret_cast(&mouse)); }; mouse.flags = LLMHF_INJECTED; wheel(); const bool ignoredInjection = app.mouseActions.empty(); mouse.flags = 0; const bool partial = wheel() == 1 && app.mouseActions.empty(); mouse.mouseData = MAKEWPARAM(0, static_cast(-90)); const bool queued = wheel() == 1 && app.mouseActions.size() == 1 && app.mouseActions.front().action.type == MouseActionType::Mute; app.mouseActions.clear(); app.settings = std::move(previous); return ignoredInjection && partial && queued; } static bool volumeHoldStopsOnRelease(App &app) { constexpr int button = 3; auto &press = app.mousePresses[button]; press = {app.window, {}, GetTickCount64(), {MouseActionType::VolumeUp}, true, false}; app.beginMouseVolume(button); const bool onDown = app.mouseActions.size() == 1 && app.volumeButtonHeld(); app.repeatMouseVolume(button); const bool initialDelay = app.mouseActions.size() == 1; press.started -= 2100; app.repeatMouseVolume(button); const bool repeated = app.mouseActions.size() == 2; MSLLHOOKSTRUCT mouse{}; mouse.mouseData = MAKEWPARAM(0, XBUTTON1); const bool consumed = App::mouseProc(HC_ACTION, WM_XBUTTONUP, reinterpret_cast(&mouse)) == 1; app.repeatMouseVolume(button); const bool stopped = !app.volumeButtonHeld() && app.mouseActions.size() == 2; app.mouseActions.clear(); return onDown && initialDelay && repeated && consumed && stopped; } }; } template static void historyLayoutChecks(ks::App &app, const std::filesystem::path &temp, Require require, bool capturePreview = false) { if (!app.history) app.history=std::make_unique(temp/L"history-layout"); ks::showHistory(app,false); auto history=app.historyWindow; require(history&&!IsWindowVisible(history),"history layout tested without changing focus"); const auto historyIcon = reinterpret_cast(SendMessageW(history, WM_GETICON, ICON_BIG, 0)); require(historyIcon && historyIcon != LoadIconW(app.instance, MAKEINTRESOURCEW(IDI_KSWITCHER)) && SendMessageW(history, WM_GETICON, ICON_SMALL, 0), "clipboard history supplies separate taskbar and caption icons"); require(!GetDlgItem(history,105),"history creates no RichEdit control before formatted content is displayed"); { app.history=std::make_unique(temp/L"history-search"); app.history->add(ks::textClip(L"згенеруй приклад"),L"test",app.settings); app.history->add(ks::textClip(L"unrelated entry"),L"test",app.settings); // This App has no general worker: a search on that queue can never finish. auto originalFocus=GetFocus(); auto pumpUntil=[&](int count) { auto deadline=GetTickCount64()+3000; do { MSG message{}; while(PeekMessageW(&message,nullptr,0,0,PM_REMOVE)) { TranslateMessage(&message); DispatchMessageW(&message); } if(SendDlgItemMessageW(history,102,LB_GETCOUNT,0,0)==count) return true; Sleep(5); } while(GetTickCount64()