Files
segatools/mai2io/mai2io.c
Mahuyo a1611afffc Mai2: Add touch and led15070 hook (#55)
In this PR, I have added the `mai2` touch and `led15070` hooks to provide an example for handling custom peripherals. This change allows users to implement the touch and `led15070` logic by writing appropriate `mai2io` scripts.

#### **Touch Hook**:
- The touch hook simulates touch points based on keyboard combinations. For example, to trigger the A1 touch point, the user must press the A and 1 keys on the keyboard. Input for the 1p requires Caps Lock to be off, while 2p requires Caps Lock to be on.
- The hook allows for independent control of whether device simulation is enabled for "1p" and "2p" and whether keyboard input mapping is enabled.
- **Note**: The current touch hook is not yet functional as it requires modifications to the `capnhook` for proper completion of the `sinmai` hook.

#### **LED15070 Hook**:
- This hook implements basic device simulation. Peripherals requiring lighting data should complete the logic as needed.
- **Note**: The LED data refresh can flood the console logs, so I’ve added a `DEBUG` flag to control whether the debug logging is enabled or not.

#### **Other Changes**:
- In certain versions of `sinmai`, key inputs for 1p and 2p can be directly read from the keyboard without requiring simulation via the `amdaemon io4` hook. I’ve added a switch to control this behavior to prevent redundant input.
- **Benefit**: This ensures that key input is only read when `sinmai` is in the foreground.

If you'd like to learn more about the touch and `led15070` features, my research findings are available here:
[Mai2Touch](https://github.com/Sucareto/Mai2Touch)

Co-authored-by: Sucareto <28331534+Sucareto@users.noreply.github.com>
Reviewed-on: TeamTofuShop/segatools#55
Co-authored-by: Mahuyo <mahuyo@noreply.gitea.tendokyu.moe>
Co-committed-by: Mahuyo <mahuyo@noreply.gitea.tendokyu.moe>
2025-02-16 12:49:58 +00:00

300 lines
7.3 KiB
C

#include <process.h>
#include <limits.h>
#include "mai2io/mai2io.h"
#include "mai2io/config.h"
#include "mai2hook/touch.h"
#include "util/env.h"
static uint8_t mai2_opbtn;
static uint16_t mai2_player1_btn;
static uint16_t mai2_player2_btn;
static struct mai2_io_config mai2_io_cfg;
static bool mai2_io_coin;
mai2_io_touch_callback_t _callback;
static HANDLE mai2_io_touch_1p_thread;
static bool mai2_io_touch_1p_stop_flag;
static HANDLE mai2_io_touch_2p_thread;
static bool mai2_io_touch_2p_stop_flag;
uint16_t mai2_io_get_api_version(void)
{
return 0x0101;
}
HRESULT mai2_io_init(void)
{
mai2_io_config_load(&mai2_io_cfg, get_config_path());
return S_OK;
}
HRESULT mai2_io_poll(void)
{
mai2_opbtn = 0;
mai2_player1_btn = 0;
mai2_player2_btn = 0;
if (GetAsyncKeyState(mai2_io_cfg.vk_test) & 0x8000) {
mai2_opbtn |= MAI2_IO_OPBTN_TEST;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_service) & 0x8000) {
mai2_opbtn |= MAI2_IO_OPBTN_SERVICE;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_coin) & 0x8000) {
if (!mai2_io_coin) {
mai2_io_coin = true;
mai2_opbtn |= MAI2_IO_OPBTN_COIN;
}
} else {
mai2_io_coin = false;
}
// If sinmai has enabled DebugInput, there is no need to input buttons through hook amdaemon.
if (!mai2_io_cfg.vk_btn_enable) {
return S_OK;
}
//Player 1
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[0])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_1;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[1])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_2;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[2])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_3;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[3])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_4;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[4])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_5;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[5])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_6;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[6])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_7;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[7])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_8;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_btn[8])) {
mai2_player1_btn |= MAI2_IO_GAMEBTN_SELECT;
}
//Player 2
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[0])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_1;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[1])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_2;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[2])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_3;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[3])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_4;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[4])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_5;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[5])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_6;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[6])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_7;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[7])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_8;
}
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_btn[8])) {
mai2_player2_btn |= MAI2_IO_GAMEBTN_SELECT;
}
return S_OK;
}
void mai2_io_get_opbtns(uint8_t *opbtn)
{
if (opbtn != NULL) {
*opbtn = mai2_opbtn;
}
}
void mai2_io_get_gamebtns(uint16_t *player1, uint16_t *player2)
{
if (player1 != NULL) {
*player1 = mai2_player1_btn;
}
if (player2 != NULL ){
*player2 = mai2_player2_btn;
}
}
HRESULT mai2_io_touch_init(mai2_io_touch_callback_t callback)
{
_callback = callback;
return S_OK;
}
void mai2_io_touch_set_sens(uint8_t *bytes)
{
#if 0
dprintf("Mai2 touch side %c: set sensor %s sensitivity to %d\n", bytes[1], sensor_to_str(bytes[2]), bytes[4]);
#endif
return;
}
void mai2_io_touch_update(bool player1, bool player2)
{
if (mai2_io_cfg.debug_input_1p)
{
if (player1 && mai2_io_touch_1p_thread == NULL)
{
mai2_io_touch_1p_thread = (HANDLE)_beginthreadex(
NULL,
0,
mai2_io_touch_1p_thread_proc,
_callback,
0,
NULL);
}
else if (!player1 && mai2_io_touch_1p_thread != NULL)
{
mai2_io_touch_1p_stop_flag = true;
WaitForSingleObject(mai2_io_touch_1p_thread, INFINITE);
CloseHandle(mai2_io_touch_1p_thread);
mai2_io_touch_1p_thread = NULL;
mai2_io_touch_1p_stop_flag = false;
}
}
if (mai2_io_cfg.debug_input_2p)
{
if (player2 && mai2_io_touch_2p_thread == NULL)
{
mai2_io_touch_2p_thread = (HANDLE)_beginthreadex(
NULL,
0,
mai2_io_touch_2p_thread_proc,
_callback,
0,
NULL);
}
else if (!player2 && mai2_io_touch_2p_thread != NULL)
{
mai2_io_touch_2p_stop_flag = true;
WaitForSingleObject(mai2_io_touch_2p_thread, INFINITE);
CloseHandle(mai2_io_touch_2p_thread);
mai2_io_touch_2p_thread = NULL;
mai2_io_touch_2p_stop_flag = false;
}
}
}
static unsigned int __stdcall mai2_io_touch_1p_thread_proc(void *ctx)
{
mai2_io_touch_callback_t callback = ctx;
while (!mai2_io_touch_1p_stop_flag)
{
uint8_t state[7] = {0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < 34; i++)
{
if (GetAsyncKeyState(mai2_io_cfg.vk_1p_touch[i]))
{
int byteIndex = i / 5;
int bitIndex = i % 5;
state[byteIndex] |= (1 << bitIndex);
}
}
callback(1, state);
Sleep(1);
}
return 0;
}
static unsigned int __stdcall mai2_io_touch_2p_thread_proc(void *ctx)
{
mai2_io_touch_callback_t callback = ctx;
while (!mai2_io_touch_2p_stop_flag)
{
uint8_t state[7] = {0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < 34; i++)
{
if (GetAsyncKeyState(mai2_io_cfg.vk_2p_touch[i]))
{
int byteIndex = i / 5;
int bitIndex = i % 5;
state[byteIndex] |= (1 << bitIndex);
}
}
callback(2, state);
Sleep(1);
}
return 0;
}
HRESULT mai2_io_led_init(void)
{
return S_OK;
}
void mai2_io_led_set_fet_output(const uint8_t *rgb)
{
#if 0
dprintf("mai2 LED: BodyLed brightness: %d%%\n", (rgb[0] * 100) / 255);
dprintf("mai2 LED: ExtLed brightness: %d%%\n", (rgb[1] * 100) / 255);
dprintf("mai2 LED: SideLed brightness: %d%%\n", (rgb[2] * 100) / 255);
#endif
return;
}
void mai2_io_led_dc_update(const uint8_t *rgb)
{
#if 0
for (int i = 0; i < 10; i++) {
dprintf("mai2 LED: LED %d: %02X %02X %02X Speed: %02X\n",
i, rgb[i * 4], rgb[i * 4 + 1], rgb[i * 4 + 2], rgb[i * 4 + 3]);
}
#endif
return;
}
void mai2_io_led_gs_update(const uint8_t *rgb)
{
#if 0
for (int i = 0; i < 8; i++) {
dprintf("mai2 LED: LED %d: %02X %02X %02X Speed: %02X\n",
i, rgb[i * 4], rgb[i * 4 + 1], rgb[i * 4 + 2], rgb[i * 4 + 3]);
}
#endif
return;
}