segatools/common/board
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
..
aime-dll.c aimeio: Implement full aime reader function in aimeio (#94) 2026-02-08 11:01:01 +00:00
aime-dll.h aimeio: Implement full aime reader function in aimeio (#94) 2026-02-08 11:01:01 +00:00
config.c Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
config.h Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
elo-cmd.h diva: add ELO touch support, thanks @emihiok 2026-02-08 18:17:53 +01:00
elo-frame.c diva: small touch controller improvements 2026-02-09 21:29:36 +01:00
elo-frame.h diva: add ELO touch support, thanks @emihiok 2026-02-08 18:17:53 +01:00
ffb.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
ffb.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
guid.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
guid.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
io3.c Add setting to only allow game input in foreground (#83) 2025-11-12 22:33:32 +00:00
io3.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
io4.c mai2io/led15070: add LED control APIs and PWM/fade handling (#89) 2025-12-25 20:10:59 +00:00
io4.h mai2io/led15070: add LED control APIs and PWM/fade handling (#89) 2025-12-25 20:10:59 +00:00
led15070-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15070-frame.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15070-frame.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15070.c Fix mai2 memory leak via LED 15070 rate-limiting and enhance touch handling (#101) 2026-04-03 15:13:27 +00:00
led15070.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15093-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15093-frame.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15093-frame.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15093.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
led15093.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
meson.build Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
sg-cmd.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-frame.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-frame.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-led-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-led.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-led.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-nfc-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
sg-nfc.c aimeio: Implement full aime reader function in aimeio (#94) 2026-02-08 11:01:01 +00:00
sg-nfc.h aimeio: Implement full aime reader function in aimeio (#94) 2026-02-08 11:01:01 +00:00
sg-reader-queue.c Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
sg-reader-queue.h Add support for Sangokushi Taisen and Eiketsu Taisen (#85) 2026-02-26 19:31:45 +00:00
sg-reader.c aimeio: Implement full aime reader function in aimeio (#94) 2026-02-08 11:01:01 +00:00
sg-reader.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
slider-cmd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
slider-frame.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
slider-frame.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
vfd-cmd.h aimeio-vfd: Add VFD text forwarding and state callbacks to AIME IO (#90) 2026-01-04 08:43:52 +00:00
vfd-frame.c refactor all common parts and games 2025-04-17 19:40:40 +02:00
vfd-frame.h refactor all common parts and games 2025-04-17 19:40:40 +02:00
vfd.c aimeio-vfd: Add VFD text forwarding and state callbacks to AIME IO (#90) 2026-01-04 08:43:52 +00:00
vfd.h refactor all common parts and games 2025-04-17 19:40:40 +02:00