Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

winemac: release held keys when focus moves away, so game movement keys stop sticking

mikey9228b726836304
diff --git a/docs/DIAGNOSIS.md b/docs/DIAGNOSIS.md
index cc5227727e4..75a8180cf96 100644
--- a/docs/DIAGNOSIS.md
+++ b/docs/DIAGNOSIS.md
@@ -131,6 +131,55 @@ winemac 패치의 `WINE_DOCK_REOPEN_CMD`(보이는 창이 없을 때 독 클릭
- Epic: 트레이 더블클릭 시퀀스 재현(`tools/soju-epic-restore.c`, 위 참조).
- GOG: `RestoreClientMessage` 직접 전송(`tools/soju-gog-restore.c`, 위 참조).
+## 게임에서 이동키가 눌린 채로 고정되는 이유 (2026-08-31)
+
+### 증상
+
+호그와트 레거시에서 앞으로 가는 키(W)가 눌린 상태로 굳어 캐릭터가 계속 걸어간다. 그 키를 한 번
+눌렀다 떼면 풀린다. 자주 발생한다.
+
+### 근본 원인
+
+macOS는 키를 뗀 이벤트를 **그 순간 포커스를 가진 앱**에 보낸다. 키를 누른 채로 Command-Tab을
+하거나, 입력 소스를 바꾸거나, 알림이 포커스를 가져가면 key up이 wine에 도착하지 않는다.
+
+winemac 드라이버는 어떤 키가 눌려 있는지 자체 비트맵(`pressedKeyCodes`)으로 들고 있는데, 이걸
+비워주는 곳이 key up 처리 한 군데뿐이다. `applicationDidResignActive:`는 커서 클립을 풀고 마우스
+캡처를 놓아주지만 키는 건드리지 않는다. `macdrv_app_deactivated()`도 마찬가지다. 그래서 Windows
+쪽에는 키가 계속 눌려 있는 상태로 남는다.
+
+놓치는 경로가 하나 더 있다. `cocoa_app.m`의 key up 처리는 이렇게 되어 있다.
+
+```objc
+else if (type == NSEventTypeKeyUp)
+{
+ uint16_t keyCode = [anEvent keyCode];
+ if ([self isKeyPressed:keyCode])
+ {
+ WineWindow* window = (WineWindow*)[anEvent window];
+ [self noteKey:keyCode pressed:FALSE];
+ if ([window isKindOfClass:[WineWindow class]])
+ [window postKeyEvent:anEvent];
+ }
+}
+```
+
+이벤트에 우리 창이 붙어 있지 않으면(전체화면 게임, 눌린 중에 창이 정리된 경우) **자체 표시만 지우고
+이벤트는 버린다.** wine은 뗀 사실을 영영 모른다.
+
+업스트림 wine master도 같은 코드다. CrossOver 소스만의 문제가 아니라 wine 자체의 빈틈이다.
+
+### 해결책 (`patches/winemac-release-keys-on-focus-loss.patch`)
+
+- `releasePressedKeys`를 추가해 아직 눌린 것으로 표시된 모든 키에 대해 KEY_RELEASE를 만들어 앞쪽
+ Wine 창의 큐에 넣고 비트맵을 비운다. `applicationDidResignActive:`에서 호출한다.
+- 창 없이 도착한 key up은 버리지 말고 앞쪽 Wine 창으로 보낸다.
+
+### 상태
+
+엔진 빌드와 런처 구동은 확인했다(GOG 정상 실행, 독 아이콘·커스텀 프레임 패치도 함께 적용된 바이너리).
+게임에서 실제로 고정 현상이 사라지는지는 플레이로 확인해야 한다.
+
## 켜둔 런처가 먹통이 되는 이유: wineserver를 잃은 고아 프로세스 (2026-08-31)
### 증상
diff --git a/patches/winemac-release-keys-on-focus-loss.patch b/patches/winemac-release-keys-on-focus-loss.patch
new file mode 100644
index 00000000000..4db526a8309
--- /dev/null
+++ b/patches/winemac-release-keys-on-focus-loss.patch
@@ -0,0 +1,93 @@
+Release keys that were held when the app lost focus, and deliver a key up that
+arrives without a window of ours.
+
+macOS sends a key up to whichever application has focus at the time, so a key
+still held when focus moves elsewhere (Command-Tab, switching input source, a
+notification stealing focus) never reaches Wine. The driver keeps its own idea
+of which keys are down and nothing ever clears it, so as far as the Windows side
+is concerned the key stays down: in a game the character keeps walking forward
+until the key is pressed and released again.
+
+There is a second way to lose a release. A key up whose NSEvent has no
+WineWindow behind it, which happens with a full screen game or a window torn
+down mid-press, clears the driver's own record and then drops the event instead
+of sending it on, with the same result.
+
+So release everything still marked as held when the application is deactivated,
+and fall back to the front window for a key up that arrives without one.
+
+--- a/dlls/winemac.drv/cocoa_app.h
++++ b/dlls/winemac.drv/cocoa_app.h
+@@ -122,6 +122,7 @@
+
+ - (void) computeEventTimeAdjustmentFromTicks:(unsigned long long)tickcount uptime:(uint64_t)uptime_ns;
+ - (double) ticksForEventTime:(NSTimeInterval)eventTime;
++ - (void) releasePressedKeys;
+
+ - (void) windowGotFocus:(WineWindow*)window;
+
+--- a/dlls/winemac.drv/cocoa_app.m
++++ b/dlls/winemac.drv/cocoa_app.m
+@@ -1402,6 +1402,39 @@
+ int index = keyCode / bits;
+ uint32_t mask = 1 << (keyCode % bits);
+ return (pressedKeyCodes[index] & mask) != 0;
++ }
++
++ /* Tell Wine that every key we still believe is held has been released.
++ *
++ * macOS delivers the key up to whoever has focus at the time, so a key held
++ * across a focus change (Command-Tab, an input source switch, a notification
++ * taking focus) is never released as far as Wine is concerned, and a game
++ * goes on walking forward until the key is pressed and let go again. */
++ - (void) releasePressedKeys
++ {
++ WineWindow* window = [self frontWineWindow];
++ NSUInteger modifiers = [NSEvent modifierFlags];
++ double time_ms = [self ticksForEventTime:[[NSProcessInfo processInfo] systemUptime]];
++ int bits = sizeof(pressedKeyCodes[0]) * 8;
++ unsigned int i;
++
++ for (i = 0; i < sizeof(pressedKeyCodes) / sizeof(pressedKeyCodes[0]) * bits; i++)
++ {
++ macdrv_event* event;
++
++ if (!pressedKeyCodes[i / bits]) { i += bits - 1; continue; }
++ if (![self isKeyPressed:i]) continue;
++
++ [self noteKey:i pressed:FALSE];
++ if (!window) continue;
++
++ event = macdrv_create_event(KEY_RELEASE, window);
++ event->key.keycode = i;
++ event->key.modifiers = modifiers;
++ event->key.time_ms = time_ms;
++ [window.queue postEvent:event];
++ macdrv_release_event(event);
++ }
+ }
+
+ - (void) noteKey:(uint16_t)keyCode pressed:(BOOL)pressed
+@@ -1965,7 +1998,13 @@
+ {
+ WineWindow* window = (WineWindow*)[anEvent window];
+ [self noteKey:keyCode pressed:FALSE];
+- if ([window isKindOfClass:[WineWindow class]])
++ /* An event that arrives without a window of ours behind it (a
++ * full screen game, a window torn down mid-press) would drop the
++ * release and leave the key stuck down in Wine. Send it to the
++ * front window instead. */
++ if (![window isKindOfClass:[WineWindow class]])
++ window = [self frontWineWindow];
++ if (window)
+ [window postKeyEvent:anEvent];
+ }
+ }
+@@ -2513,6 +2552,7 @@
+ WineEventQueue* queue;
+
+ [self invalidateGotFocusEvents];
++ [self releasePressedKeys];
+
+ if (!temporarilyIgnoreResignEventsForDialog)
+ {
diff --git a/scripts/build-engine.sh b/scripts/build-engine.sh
index 17daa6e276c..5e9acd29631 100755
--- a/scripts/build-engine.sh
+++ b/scripts/build-engine.sh
@@ -39,6 +39,10 @@ if ! grep -q "soju_append_chromium_flags" "$WINE/dlls/kernelbase/process.c"; the
echo "==> Applying patches/chromium-flags-append.patch (SOJU_CHROMIUM_FLAGS hook for Qt launchers)"
patch -p1 -d "$WINE" < "$SCRIPT_DIR/../patches/chromium-flags-append.patch"
fi
+if ! grep -q "releasePressedKeys" "$WINE/dlls/winemac.drv/cocoa_app.m"; then
+ echo "==> Applying patches/winemac-release-keys-on-focus-loss.patch (keys stuck down after a focus change)"
+ patch -p1 -d "$WINE" < "$SCRIPT_DIR/../patches/winemac-release-keys-on-focus-loss.patch"
+fi
if ! grep -q "KEY_STORE_MAGIC" "$WINE/dlls/ncrypt/main.c"; then
echo "==> Applying patches/ncrypt-persisted-keys.patch (named CNG keys survive the process)"
patch -p1 -d "$WINE" < "$SCRIPT_DIR/../patches/ncrypt-persisted-keys.patch"