Work started on editor support.

This commit is contained in:
Storm Dragon committed 2026-10-09 15:57:45 -04:00
1 parent 0992ffee0d
commit 6f6d4040c4
9 files changed
+401 -5

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "edit_hook.h"
#include "menu_bridge.h"
#include <commctrl.h>
#include <stdlib.h>
#define EDIT_SUBCLASS_ID 0x57415431
#define EDIT_STATE_PROPERTY L"CthulhuWineEditState"
// Bound memory and work inside applications. Navigation reuses the snapshot;
// only text changes refresh it. Large documents retain line/character reading.
#define MAX_SNAPSHOT_UNITS (1024 * 1024)
typedef struct EditState {
WCHAR *text;
DWORD length, start, end;
BOOL busy, known, destroyed;
} EditState;
static void selection(HWND window, DWORD *start, DWORD *end) {
SendMessageW(window, EM_GETSEL, (WPARAM)start, (LPARAM)end);
}
static void snapshot(HWND window, EditState *state) {
free(state->text);
state->text = NULL;
state->length = GetWindowTextLengthW(window);
if (state->length <= MAX_SNAPSHOT_UNITS) {
state->text = calloc((size_t)state->length + 1, sizeof(WCHAR));
if (state->text)
state->length = GetWindowTextW(window, state->text, state->length + 1);
}
}
static void copy_range(WCHAR *output, const WCHAR *text, DWORD start, DWORD end) {
UINT used = 0;
while (start < end && used < 1023) output[used++] = text[start++];
// Never leave half a UTF-16 surrogate pair at the truncation boundary.
if (used && output[used - 1] >= 0xd800 && output[used - 1] <= 0xdbff) --used;
output[used] = 0;
}
static void current_line(HWND window, DWORD caret, WCHAR *output) {
LRESULT line = SendMessageW(window, EM_LINEFROMCHAR, caret, 0);
// EM_GETLINE expects the buffer capacity in its first WORD.
output[0] = 1023;
LRESULT copied = SendMessageW(window, EM_GETLINE, line, (LPARAM)output);
if (copied < 0 || copied > 1023) copied = 0;
output[copied] = 0;
}
static BOOL word_space(WCHAR ch) {
return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n';
}
static void notify_text(HWND window, UINT kind, const WCHAR *text, DWORD start, DWORD end) {
HWND owner = GetAncestor(window, GA_ROOT);
HWND bridge = FindWindowW(WINE_ACCESS_MENU_CLASS, NULL);
if (!bridge || owner != GetAncestor(GetForegroundWindow(), GA_ROOT) || GetFocus() != window)
return;
WineAccessTextEvent event = {0};
event.owner = (ULONGLONG)(UINT_PTR)owner;
event.control = (ULONGLONG)(UINT_PTR)window;
event.kind = kind; event.start = start; event.end = end;
lstrcpynW(event.text, text, 1024);
GetWindowTextW(owner, event.title, 256);
COPYDATASTRUCT data = {WINE_ACCESS_TEXT_TAG, sizeof(event), &event};
DWORD_PTR result;
SendMessageTimeoutW(bridge, WM_COPYDATA, (WPARAM)window, (LPARAM)&data,
SMTO_ABORTIFHUNG | SMTO_BLOCK, 50, &result);
}
static LRESULT CALLBACK edit_proc(HWND window, UINT message, WPARAM wp, LPARAM lp,
UINT_PTR id, DWORD_PTR reference) {
EditState *state = (EditState *)reference;
if (message == WM_NCDESTROY) {
RemoveWindowSubclass(window, edit_proc, id);
RemovePropW(window, EDIT_STATE_PROPERTY);
state->destroyed = TRUE;
if (!state->busy) { free(state->text); free(state); }
return DefSubclassProc(window, message, wp, lp);
}
if (state->busy) return DefSubclassProc(window, message, wp, lp);
BOOL navigation = message == WM_KEYDOWN &&
(wp == VK_LEFT || wp == VK_RIGHT || wp == VK_UP || wp == VK_DOWN ||
wp == VK_HOME || wp == VK_END || wp == VK_PRIOR || wp == VK_NEXT);
BOOL changing = message == WM_CHAR || (message == WM_KEYDOWN && wp == VK_DELETE) ||
message == WM_PASTE || message == WM_CUT || message == WM_CLEAR ||
message == WM_UNDO || message == EM_REPLACESEL || message == WM_SETTEXT;
BOOL focus = message == WM_SETFOCUS;
BOOL selected = message == EM_SETSEL;
if (!navigation && !changing && !focus && !selected && message != EM_SETPASSWORDCHAR)
return DefSubclassProc(window, message, wp, lp);
state->busy = TRUE;
DWORD old_start = 0, old_end = 0;
selection(window, &old_start, &old_end);
BOOL password = (GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD) != 0;
if (!password && (!state->known || focus)) snapshot(window, state);
WCHAR removed[1024] = {0};
if (!password && state->text && changing) {
DWORD first = old_start, last = old_end;
if (first == last) {
if (message == WM_CHAR && wp == VK_BACK && first) {
--first;
if (first && state->text[first] >= 0xdc00 && state->text[first] <= 0xdfff) --first;
} else if (message == WM_KEYDOWN && wp == VK_DELETE && last < state->length) {
++last;
if (last < state->length && state->text[last - 1] >= 0xd800 && state->text[last - 1] <= 0xdbff) ++last;
}
}
if (last <= state->length) copy_range(removed, state->text, first, last);
}
LRESULT result = DefSubclassProc(window, message, wp, lp);
if (state->destroyed) {
free(state->text); free(state);
return result;
}
DWORD start = 0, end = 0;
selection(window, &start, &end);
password = (GetWindowLongPtrW(window, GWL_STYLE) & ES_PASSWORD) != 0;
if (password) {
free(state->text); state->text = NULL; state->known = FALSE;
if (focus) notify_text(window, WINE_ACCESS_TEXT_PASSWORD, L"", start, end);
state->busy = FALSE;
return result;
}
DWORD old_length = state->length;
if (changing || message == EM_SETPASSWORDCHAR) snapshot(window, state);
state->known = TRUE;
WCHAR text[1024] = {0};
UINT kind = WINE_ACCESS_TEXT_READ;
BOOL report = FALSE;
if (focus) {
current_line(window, start, text);
kind = WINE_ACCESS_TEXT_FOCUS; report = TRUE;
} else if ((navigation || selected) && (start != old_start || end != old_end)) {
if (start != end && state->text && end <= state->length) {
// Speak the changed portion when extending or shrinking a selection.
DWORD first = start, last = end;
kind = WINE_ACCESS_TEXT_SELECTED;
if (old_start != old_end && start == old_start) {
first = end < old_end ? end : old_end;
last = end > old_end ? end : old_end;
if (end < old_end) kind = WINE_ACCESS_TEXT_UNSELECTED;
} else if (old_start != old_end && end == old_end) {
first = start < old_start ? start : old_start;
last = start > old_start ? start : old_start;
if (start > old_start) kind = WINE_ACCESS_TEXT_UNSELECTED;
}
copy_range(text, state->text, first, last);
} else if (old_start != old_end && state->text && old_end <= state->length) {
copy_range(text, state->text, old_start, old_end);
kind = WINE_ACCESS_TEXT_UNSELECTED;
} else if (navigation && (wp == VK_UP || wp == VK_DOWN || wp == VK_PRIOR || wp == VK_NEXT)) {
current_line(window, start, text);
} else if (state->text && start < state->length) {
DWORD last = start + 1;
if (GetKeyState(VK_CONTROL) & 0x8000) {
while (last < state->length && !word_space(state->text[last])) ++last;
} else if (state->text[start] >= 0xd800 && state->text[start] <= 0xdbff && last < state->length) {
++last;
}
copy_range(text, state->text, start, last);
} else if (!state->text) {
current_line(window, start, text);
}
report = TRUE;
} else if (changing && removed[0] && old_length > state->length) {
lstrcpynW(text, removed, 1024);
kind = WINE_ACCESS_TEXT_DELETED; report = TRUE;
}
if (report) notify_text(window, kind, text, start, end);
state->start = start; state->end = end;
state->busy = FALSE;
return result;
}
void wine_access_watch_edit(HWND window) {
WCHAR class_name[64];
if (!window || !GetClassNameW(window, class_name, 64) || lstrcmpW(class_name, L"Edit")) return;
// Wine's comctl32 v5 exports GetWindowSubclass only by ordinal. Our own
// property also keeps attachment independent of the application's manifest.
if (GetPropW(window, EDIT_STATE_PROPERTY)) return;
EditState *state = calloc(1, sizeof(*state));
if (!state) return;
if (!SetWindowSubclass(window, edit_proc, EDIT_SUBCLASS_ID, (DWORD_PTR)state)) {
free(state); return;
}
if (!SetPropW(window, EDIT_STATE_PROPERTY, state)) {
RemoveWindowSubclass(window, edit_proc, EDIT_SUBCLASS_ID);
free(state); return;
}
// Subclass callbacks can outlive the reader. Keep their module mapped until
// the application exits; each control frees its state on WM_NCDESTROY.
HMODULE module;
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN,
(LPCWSTR)(UINT_PTR)edit_proc, &module);
}
__declspec(dllexport) LRESULT CALLBACK WineAccessInputHook(int code, WPARAM wp, LPARAM lp) {
if (code >= 0 && wp == PM_REMOVE) wine_access_watch_edit(GetFocus());
return CallNextHookEx(NULL, code, wp, lp);
}