segatools/common
Gl0w1amp 2c2b8b11b4 Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101)
## Overview
This PR addresses the severe memory leak and performance issues observed in `mai2`, while also introducing improvements to the touch emulation logic.

## Root Cause Analysis
As discussed previously, the root cause of the `mai2` memory leak is not a global `segatools` buffer bug. Instead, the game aggressively spams overlapped empty reads specifically on the `LED 15070` UART. On real hardware, the serial driver naturally throttles this. Under emulation, without a throttle, it hits approximately **260kHz of empty async reads**, which causes the memory usage to explode.

## The Fix
Instead of introducing complex locking mechanisms and condition variables globally in `uart.c`, this PR applies a targeted fix:
* Added a local `Sleep(1)` directly in `common/board/led15070.c` to rate-limit empty reads on the LED path.
* Because this is isolated to LED communications, it completely resolves the memory leak without introducing any lag, livelocks, or overhead to other critical inputs.

## Additional Changes in this PR
Alongside the memory leak fix, this PR includes a few touch-related improvements (as touch emulation was reviewed during the debugging process):
* Enhanced touch input handling and improved auto-scan state management.
* Implemented IOCTL handling for touch input to properly manage communication status.

## Testing
* **mai2:** Tested successfully on multiple machines. The memory leak is completely gone, and the game runs smoothly.
* **chusan:** Tested to ensure no regressions. Sliders and inputs work flawlessly without the lag.

Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/101
Co-authored-by: Gl0w1amp <gl0w1amp@noreply.gitea.tendokyu.moe>
Co-committed-by: Gl0w1amp <gl0w1amp@noreply.gitea.tendokyu.moe>
2026-04-03 15:13:27 +00:00
..
aimeio Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
amex Add setting to only allow game input in foreground (#83) 2025-11-12 22:33:32 +00:00
board Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101) 2026-04-03 15:13:27 +00:00
gfxhook refactor all common parts and games 2025-04-17 19:40:40 +02:00
hooklib Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
iccard refactor all common parts and games 2025-04-17 19:40:40 +02:00
jvs refactor all common parts and games 2025-04-17 19:40:40 +02:00
platform misc: add ability to re-enable a few things (#100) 2026-03-04 07:58:01 +00:00
unityhook Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
util mercury: update mystery cmds, add fg detect, cleanup 2026-01-19 19:51:48 +01:00
y3io Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00