idac, idz, swdc: recover dinput device during gameplay (#134)

This PR adds a feature that lets the segatools reacquire dinput device if it disconnects during gameplay. Before this if dinput device disconnects for any reason then it would require user to restart the game since segatools doesn't attempt to reacquire the same dinput device it launched with.

Reviewed-on: https://gitea.tendokyu.moe/TeamTofuShop/segatools/pulls/134
Co-authored-by: puniru <puniru@posteo.jp>
This commit is contained in:
puniru
2026-09-08 13:47:23 +00:00
committed by Dniel97
parent 9bdceedc8b
commit bbe37bfb40
6 changed files with 92 additions and 3 deletions
+30
View File
@@ -14,6 +14,13 @@ static IDirectInputDevice8W* idac_di_dev = NULL;
static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx);
static bool idac_di_dev_state_lost(HRESULT hr)
{
return hr == DIERR_INPUTLOST
|| hr == DIERR_NOTACQUIRED
|| hr == DIERR_UNPLUGGED;
}
/* Effect handles */
static IDirectInputEffect* idac_di_fx_constant = NULL;
static IDirectInputEffect* idac_di_fx_rumble = NULL;
@@ -115,6 +122,29 @@ HRESULT idac_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd,
}
hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st);
if (FAILED(hr) && idac_di_dev_state_lost(hr)) {
HRESULT acquire_hr;
dprintf("DirectInput: Device lost (%08x), attempting to reacquire\n",
(int) hr);
acquire_hr = IDirectInputDevice8_Acquire(dev);
if (SUCCEEDED(acquire_hr)) {
hr = IDirectInputDevice8_GetDeviceState(
dev,
sizeof(out->st),
&out->st);
if (SUCCEEDED(hr)) {
dprintf("DirectInput: Device reacquired\n");
}
} else {
dprintf("DirectInput: Acquire failed: %08x\n", (int) acquire_hr);
}
}
if (FAILED(hr)) {
dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr);
}
+1 -1
View File
@@ -104,7 +104,7 @@ void idac_io_get_shifter(uint8_t *gear)
void idac_io_get_analogs(struct idac_io_analog_state *out)
{
struct idac_io_analog_state tmp;
struct idac_io_analog_state tmp = { 0 };
assert(out != NULL);
assert(idac_io_backend != NULL);
+29
View File
@@ -13,6 +13,13 @@ static IDirectInputDevice8W* idz_di_dev = NULL;
static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr, REFGUID guid, DIEFFECT* fx);
static bool idz_di_dev_state_lost(HRESULT hr)
{
return hr == DIERR_INPUTLOST
|| hr == DIERR_NOTACQUIRED
|| hr == DIERR_UNPLUGGED;
}
/* Individual DI Effects */
static IDirectInputEffect* idz_di_fx_constant = NULL;
static IDirectInputEffect* idz_di_fx_rumble = NULL;
@@ -122,6 +129,28 @@ HRESULT idz_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd,
hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st);
if (FAILED(hr) && idz_di_dev_state_lost(hr)) {
HRESULT acquire_hr;
dprintf("DirectInput: Device lost (%08x), attempting to reacquire\n",
(int) hr);
acquire_hr = IDirectInputDevice8_Acquire(dev);
if (SUCCEEDED(acquire_hr)) {
hr = IDirectInputDevice8_GetDeviceState(
dev,
sizeof(out->st),
&out->st);
if (SUCCEEDED(hr)) {
dprintf("DirectInput: Device reacquired\n");
}
} else {
dprintf("DirectInput: Acquire failed: %08x\n", (int) acquire_hr);
}
}
if (FAILED(hr)) {
dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr);
}
+1 -1
View File
@@ -88,7 +88,7 @@ void idz_io_jvs_read_shifter(uint8_t *gear)
void idz_io_jvs_read_analogs(struct idz_io_analog_state *out)
{
struct idz_io_analog_state tmp;
struct idz_io_analog_state tmp = { 0 };
assert(out != NULL);
assert(idz_io_backend != NULL);
+30
View File
@@ -14,6 +14,13 @@ static IDirectInputDevice8W* swdc_di_dev = NULL;
static void update_or_create_effect(LPDIRECTINPUTEFFECT* effect_ptr,
REFGUID guid, DIEFFECT* fx);
static bool swdc_di_dev_state_lost(HRESULT hr)
{
return hr == DIERR_INPUTLOST
|| hr == DIERR_NOTACQUIRED
|| hr == DIERR_UNPLUGGED;
}
/* Individual DI Effects */
static IDirectInputEffect* swdc_di_fx_constant = NULL;
static IDirectInputEffect* swdc_di_fx_rumble = NULL;
@@ -116,6 +123,29 @@ HRESULT swdc_di_dev_poll(IDirectInputDevice8W* dev, HWND wnd,
}
hr = IDirectInputDevice8_GetDeviceState(dev, sizeof(out->st), &out->st);
if (FAILED(hr) && swdc_di_dev_state_lost(hr)) {
HRESULT acquire_hr;
dprintf("DirectInput: Device lost (%08x), attempting to reacquire\n",
(int) hr);
acquire_hr = IDirectInputDevice8_Acquire(dev);
if (SUCCEEDED(acquire_hr)) {
hr = IDirectInputDevice8_GetDeviceState(
dev,
sizeof(out->st),
&out->st);
if (SUCCEEDED(hr)) {
dprintf("DirectInput: Device reacquired\n");
}
} else {
dprintf("DirectInput: Acquire failed: %08x\n", (int) acquire_hr);
}
}
if (FAILED(hr)) {
dprintf("DirectInput: GetDeviceState error: %08x\n", (int)hr);
}
+1 -1
View File
@@ -96,7 +96,7 @@ void swdc_io_get_gamebtns(uint16_t *gamebtn_out)
void swdc_io_get_analogs(struct swdc_io_analog_state *out)
{
struct swdc_io_analog_state tmp;
struct swdc_io_analog_state tmp = { 0 };
assert(out != NULL);
assert(swdc_io_backend != NULL);