Skip to content

Commit

Permalink
Improve GameInput failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
walbourn committed Dec 2, 2022
1 parent cd7ae7b commit 776215b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 50 deletions.
39 changes: 28 additions & 11 deletions Src/GamePad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,29 @@ class GamePad::Impl

s_gamePad = this;

ThrowIfFailed(GameInputCreate(mGameInput.GetAddressOf()));

ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindGamepad,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
HRESULT hr = GameInputCreate(mGameInput.GetAddressOf());
if (SUCCEEDED(hr))
{
ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindGamepad,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
}
else
{
DebugTrace("ERROR: GameInputCreate [gamepad] failed with %08X\n", static_cast<unsigned int>(hr));
#ifdef _GAMING_XBOX
ThrowIfFailed(hr);
#elif defined(_DEBUG)
DebugTrace(
"\t**** Check that the 'GameInput Service' is running on this system. ****\n"
"\t**** NOTE: All calls to GetState will be reported as 'not connected'. ****\n");
#endif
}
}

Impl(Impl&&) = default;
Expand Down Expand Up @@ -153,6 +166,8 @@ class GamePad::Impl
device = mInputDevices[player].Get();
if (!device)
return;

assert(mGameInput != nullptr);
}
else if (player == c_MostRecent)
{
Expand All @@ -161,8 +176,10 @@ class GamePad::Impl
device = mInputDevices[player].Get();
if (!device)
return;

assert(mGameInput != nullptr);
}
else if (player != c_MergedInput)
else if (player != c_MergedInput || !mGameInput)
{
return;
}
Expand Down
37 changes: 27 additions & 10 deletions Src/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,30 @@ class Keyboard::Impl

s_keyboard = this;

ThrowIfFailed(GameInputCreate(mGameInput.GetAddressOf()));

ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindKeyboard,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
HRESULT hr = GameInputCreate(mGameInput.GetAddressOf());
if (SUCCEEDED(hr))
{
ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindKeyboard,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
}
else
{
DebugTrace("ERROR: GameInputCreate [keyboard] failed with %08X\n", static_cast<unsigned int>(hr));
#ifdef _GAMING_XBOX
ThrowIfFailed(hr);
#elif defined(_DEBUG)
DebugTrace(
"\t**** Check that the 'GameInput Service' is running on this system. ****\n"
"\t**** NOTE: No keys will be returned and IsConnected will return false. ****\n"
);
#endif
}
}

Impl(Impl&&) = default;
Expand Down Expand Up @@ -113,6 +127,9 @@ class Keyboard::Impl
{
state = {};

if (!mGameInput)
return;

ComPtr<IGameInputReading> reading;
if (SUCCEEDED(mGameInput->GetCurrentReading(GameInputKindKeyboard, nullptr, reading.GetAddressOf())))
{
Expand Down
75 changes: 46 additions & 29 deletions Src/Mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,30 @@ class Mouse::Impl

s_mouse = this;

ThrowIfFailed(GameInputCreate(mGameInput.GetAddressOf()));

ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindMouse,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
HRESULT hr = GameInputCreate(mGameInput.GetAddressOf());
if (SUCCEEDED(hr))
{
ThrowIfFailed(mGameInput->RegisterDeviceCallback(
nullptr,
GameInputKindMouse,
GameInputDeviceConnected,
GameInputBlockingEnumeration,
this,
OnGameInputDevice,
&mDeviceToken));
}
else
{
DebugTrace("ERROR: GameInputCreate [mouse] failed with %08X\n", static_cast<unsigned int>(hr));
#ifdef _GAMING_XBOX
ThrowIfFailed(hr);
#elif defined(_DEBUG)
DebugTrace(
"\t**** Check that the 'GameInput Service' is running on this system. ****\n"
"\t**** NOTE: No relative movement be returned and IsConnected will return false. ****\n"
);
#endif
}

mScrollWheelValue.reset(CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_MODIFY_STATE | SYNCHRONIZE));
if (!mScrollWheelValue)
Expand Down Expand Up @@ -134,29 +148,32 @@ class Mouse::Impl
{
state.x = state.y = 0;

ComPtr<IGameInputReading> reading;
if (SUCCEEDED(mGameInput->GetCurrentReading(GameInputKindMouse, nullptr, reading.GetAddressOf())))
if (mGameInput)
{
GameInputMouseState mouse;
if (reading->GetMouseState(&mouse))
ComPtr<IGameInputReading> reading;
if (SUCCEEDED(mGameInput->GetCurrentReading(GameInputKindMouse, nullptr, reading.GetAddressOf())))
{
state.leftButton = (mouse.buttons & GameInputMouseLeftButton) != 0;
state.middleButton = (mouse.buttons & GameInputMouseMiddleButton) != 0;
state.rightButton = (mouse.buttons & GameInputMouseRightButton) != 0;
state.xButton1 = (mouse.buttons & GameInputMouseButton4) != 0;
state.xButton2 = (mouse.buttons & GameInputMouseButton5) != 0;

if (mRelativeX != INT64_MAX)
GameInputMouseState mouse;
if (reading->GetMouseState(&mouse))
{
state.x = static_cast<int>(mouse.positionX - mRelativeX);
state.y = static_cast<int>(mouse.positionY - mRelativeY);
int scrollDelta = static_cast<int>(mouse.wheelY - mRelativeWheelY);
mScrollWheelCurrent += scrollDelta;
state.leftButton = (mouse.buttons & GameInputMouseLeftButton) != 0;
state.middleButton = (mouse.buttons & GameInputMouseMiddleButton) != 0;
state.rightButton = (mouse.buttons & GameInputMouseRightButton) != 0;
state.xButton1 = (mouse.buttons & GameInputMouseButton4) != 0;
state.xButton2 = (mouse.buttons & GameInputMouseButton5) != 0;

if (mRelativeX != INT64_MAX)
{
state.x = static_cast<int>(mouse.positionX - mRelativeX);
state.y = static_cast<int>(mouse.positionY - mRelativeY);
int scrollDelta = static_cast<int>(mouse.wheelY - mRelativeWheelY);
mScrollWheelCurrent += scrollDelta;
}

mRelativeX = mouse.positionX;
mRelativeY = mouse.positionY;
mRelativeWheelY = mouse.wheelY;
}

mRelativeX = mouse.positionX;
mRelativeY = mouse.positionY;
mRelativeWheelY = mouse.wheelY;
}
}
}
Expand Down

0 comments on commit 776215b

Please sign in to comment.