Skip to content

Commit

Permalink
Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ergo720 committed Nov 21, 2021
1 parent 909f6a6 commit a1c838a
Show file tree
Hide file tree
Showing 19 changed files with 71 additions and 82 deletions.
2 changes: 1 addition & 1 deletion import/XbSymbolDatabase
Submodule XbSymbolDatabase updated 0 files
2 changes: 0 additions & 2 deletions src/common/input/Button.cpp
Expand Up @@ -78,7 +78,6 @@ LRESULT CALLBACK ButtonDukeSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPA
Button *button = reinterpret_cast<Button *>(dwRefData);
if (wParam & MK_SHIFT) {
static_cast<DukeInputWindow *>(button->GetWnd())->SwapMoCursorAxis(button);
static_cast<DukeInputWindow *>(button->GetWnd())->UpdateProfile(std::string(), BUTTON_SWAP);
}
else if (!(wParam & ~MK_RBUTTON)) {
button->ClearText();
Expand Down Expand Up @@ -109,7 +108,6 @@ LRESULT CALLBACK ButtonSbcSubclassProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR
Button *button = reinterpret_cast<Button *>(dwRefData);
if (wParam & MK_SHIFT) {
static_cast<SbcInputWindow *>(button->GetWnd())->SwapMoCursorAxis(button);
static_cast<SbcInputWindow *>(button->GetWnd())->UpdateProfile(std::string(), BUTTON_SWAP);
}
else if (!(wParam & ~MK_RBUTTON)) {
button->ClearText();
Expand Down
66 changes: 25 additions & 41 deletions src/common/input/InputManager.cpp
Expand Up @@ -37,7 +37,6 @@


#include <core\kernel\exports\xboxkrnl.h> // For PKINTERRUPT, etc.
#include "D3dx9math.h" // For the matrix math functions
#include "SdlJoystick.h"
#include "XInputPad.h"
#include "RawDevice.h"
Expand Down Expand Up @@ -515,9 +514,6 @@ bool InputDeviceManager::UpdateInputLightgun(std::shared_ptr<InputDevice> &Devic

case 2:
g_devs[Port_num].info.ligthgun.laser ^= 1;
if (g_devs[Port_num].info.ligthgun.laser) {

}
break;
}
}
Expand Down Expand Up @@ -585,13 +581,17 @@ bool InputDeviceManager::UpdateInputLightgun(std::shared_ptr<InputDevice> &Devic
ControlState state = state_plus ? state_plus * 0x7FFF : state_minus ? -state_minus * 0x8000 : 0.0;
switch (i)
{
case 10:
in_buf->sThumbLX = static_cast<int16_t>(state) + g_devs[Port_num].info.ligthgun.offset_x;
break;
case 10: {
xbox::short_xt offset = std::abs(state) > 16383.0 ? g_devs[Port_num].info.ligthgun.offset_upp_x : g_devs[Port_num].info.ligthgun.offset_x;
in_buf->sThumbLX = static_cast<int16_t>(state) + offset;
}
break;

case 12:
in_buf->sThumbLY = static_cast<int16_t>(state) + g_devs[Port_num].info.ligthgun.offset_y;
break;
case 12: {
xbox::short_xt offset = std::abs(state) > 16383.0 ? g_devs[Port_num].info.ligthgun.offset_upp_y : g_devs[Port_num].info.ligthgun.offset_y;
in_buf->sThumbLY = static_cast<int16_t>(state) + offset;
}
break;

}
}
Expand Down Expand Up @@ -907,41 +907,25 @@ ImVec2 InputDeviceManager::CalcLaserPos(int port)

// If somebody else is currently holding the lock, we won't wait and instead we report the last known laser position
if (m_Mtx.try_lock()) {
static D3DXVECTOR4 coeff_vec;

// If the rendering window was not resized, we can skip calculating the conversion matrix
if (g_bRenderWindowResized) {
g_bRenderWindowResized = false;

// We convert the laser input coordinates given by xinput (in the sThumbLXY members of XpadInput) with the procedure described in the link below
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/jj635757(v=vs.85)?redirectedfrom=MSDN
// NOTE: the d3d math functions work even when d3d was not intialized, which happens when running with LLE GPU turned on
/*
xinput -> screen
v = M^-1 * u
width 0 0 1 0 a
height = 0 0 0 1 * b
0 -32768 32767 1 0 c
0 -32767 -32768 0 1 d
*/

// NOTE: even when switching to faux fullscreen, imgui will still use the original window size. Because of this, we only need to do this once.
// If in the future the above is fixed, then this code will have to recalculate the new window size after a resizing has occured.
static long width, height = -1;
if (height == -1) {

RECT rect;
GetClientRect(m_hwnd, &rect);
const auto width = std::max(rect.right - rect.left, 1l);
const auto height = std::max(rect.bottom - rect.top, 1l);
D3DXMATRIX inverted_mtx, transposed_mtx;
D3DXMATRIX src_mtx(0, 0, 1, 0, 0, 0, 0, 1, -32768, 32767, 1, 0, -32767, -32768, 0, 1);
D3DXVECTOR4 screen_vec(width, height, 0, 0);
D3DXMatrixInverse(&inverted_mtx, nullptr, &src_mtx);
D3DXMatrixTranspose(&transposed_mtx, &inverted_mtx);
D3DXVec4Transform(&coeff_vec, &screen_vec, &transposed_mtx);
}

// x' = ax + by + c
// y' = bx - ay + d
width = std::max(rect.right - rect.left, 1l);
height = std::max(rect.bottom - rect.top, 1l);
}

// We convert the laser input coordinates given by xinput (in the sThumbLXY members of XpadInput) with linear interpolation y = y0 + (x - x0) * (y1 - y0) / (x1 - x0)
// For laser_x x0 = -32768; x1 = 32767; y0 = 0; y1 = width
// For laser_y x0 = -32768; x1 = 32767; y0 = height; y1 = 0
int16_t laser_x = g_devs[port].info.buff.ctrl.InBuffer.sThumbLX;
int16_t laser_y = g_devs[port].info.buff.ctrl.InBuffer.sThumbLY;
laser_coord[port].x = coeff_vec.x * laser_x + coeff_vec.y * laser_y + coeff_vec.z;
laser_coord[port].y = coeff_vec.y * laser_x - coeff_vec.x * laser_y + coeff_vec.w;
laser_coord[port].x = ((laser_x + 32768) * width) / 65535.0f;
laser_coord[port].y = height - ((laser_y + 32768) * height) / 65535.0f;

m_Mtx.unlock();
}
Expand Down
2 changes: 2 additions & 0 deletions src/common/input/InputManager.h
Expand Up @@ -158,6 +158,8 @@ struct XidSBCOutput {
struct LightGunData {
xbox::short_xt offset_x;
xbox::short_xt offset_y;
xbox::short_xt offset_upp_x;
xbox::short_xt offset_upp_y;
uint8_t last_in_state;
uint8_t last_turbo;
uint8_t turbo_delay;
Expand Down
12 changes: 9 additions & 3 deletions src/common/input/InputWindow.cpp
Expand Up @@ -166,7 +166,7 @@ int InputWindow::EnableDefaultButton()
}
}

void InputWindow::BindButton(int ControlID)
void InputWindow::BindButton(int ControlID, bool auto_swap)
{
// Check if binding thread is still active
// testcase: spacebar and enter keys; without this fix will cause repeat binding result.
Expand All @@ -179,7 +179,7 @@ void InputWindow::BindButton(int ControlID)
m_bIsBinding = true;

// Don't block the message processing loop
std::thread([this, dev, ControlID]() {
std::thread([this, dev, ControlID, auto_swap]() {
EnableWindow(m_hwnd_window, FALSE);
char current_text[HOST_BUTTON_NAME_LENGTH];
Button* xbox_button = m_DeviceConfig->FindButtonById(ControlID);
Expand All @@ -189,6 +189,9 @@ void InputWindow::BindButton(int ControlID)
InputDevice::Input* dev_button = fut.get();
if (dev_button) {
xbox_button->UpdateText(dev_button->GetName().c_str());
if (auto_swap) {
SwapMoCursorAxis(xbox_button);
}
m_bHasChanges = true;
}
else {
Expand Down Expand Up @@ -217,7 +220,6 @@ void InputWindow::UpdateProfile(const std::string &name, int command)
break;

case BUTTON_CLEAR:
case BUTTON_SWAP:
m_bHasChanges = true;
break;
}
Expand Down Expand Up @@ -364,6 +366,7 @@ void InputWindow::SwapMoCursorAxis(Button *button)
else {
button->UpdateText("Cursor X-");
}
m_bHasChanges = true;
break;

case 'Y':
Expand All @@ -373,6 +376,7 @@ void InputWindow::SwapMoCursorAxis(Button *button)
else {
button->UpdateText("Cursor Y+");
}
m_bHasChanges = true;
break;

}
Expand All @@ -390,6 +394,7 @@ void InputWindow::SwapMoCursorAxis(Button *button)
else {
button->UpdateText("Axis X-");
}
m_bHasChanges = true;
break;

case 'Y':
Expand All @@ -399,6 +404,7 @@ void InputWindow::SwapMoCursorAxis(Button *button)
else {
button->UpdateText("Axis Y+");
}
m_bHasChanges = true;
break;

}
Expand Down
4 changes: 1 addition & 3 deletions src/common/input/InputWindow.h
Expand Up @@ -39,7 +39,6 @@
#define RUMBLE_TEST 6
#define RUMBLE_CLEAR 7
#define BUTTON_CLEAR 8
#define BUTTON_SWAP 9

#define XINPUT_DEFAULT 0
#define DINPUT_DEFAULT 1
Expand All @@ -56,7 +55,7 @@ class InputWindow
virtual void Initialize(HWND hwnd, int port_num, int dev_type) = 0;
~InputWindow();
virtual void UpdateDeviceList();
virtual void BindButton(int ControlID);
void BindButton(int ControlID, bool auto_swap = false);
virtual void ClearBindings() = 0;
virtual void UpdateProfile(const std::string& name, int command);
void UpdateCurrentDevice();
Expand Down Expand Up @@ -162,6 +161,5 @@ class LightgunInputWindow : public InputWindow
void Initialize(HWND hwnd, int port_num, int dev_type) override;
~LightgunInputWindow();
void BindDefault();
void BindButton(int ControlID) override;
void ClearBindings() override;
};
1 change: 1 addition & 0 deletions src/common/win32/EmuShared.cpp
Expand Up @@ -152,6 +152,7 @@ EmuShared::EmuShared()
m_bEmulating_status = false;
m_bFirstLaunch = false;
m_bClipCursor = false;
m_LightgunLaser = 1; // laser on by default

std::memset(m_DeviceControlNames, '\0', sizeof(m_DeviceControlNames));
std::memset(m_DeviceName, '\0', sizeof(m_DeviceName));
Expand Down
7 changes: 7 additions & 0 deletions src/common/win32/EmuShared.h
Expand Up @@ -257,6 +257,12 @@ class EmuShared : public Mutex
void GetClipCursorFlag(bool *value) { Lock(); *value = m_bClipCursor; Unlock(); }
void SetClipCursorFlag(const bool value) { Lock(); m_bClipCursor = value; Unlock(); }

// ******************************************************************
// * LightgunLaser flag Accessors
// ******************************************************************
void GetLightgunLaser(uint8_t *value) { Lock(); *value = m_LightgunLaser; Unlock(); }
void SetLightgunLaser(const uint8_t *value) { Lock(); m_LightgunLaser = *value; Unlock(); }

// ******************************************************************
// * ImGui Accessors
// ******************************************************************
Expand Down Expand Up @@ -364,6 +370,7 @@ class EmuShared : public Mutex
#else
unsigned int m_Reserved;
#endif
uint8_t m_LightgunLaser;
bool m_bFirstLaunch;
bool m_bClipCursor;
unsigned int m_dwKrnlProcID; // Only used for kernel mode level.
Expand Down
5 changes: 2 additions & 3 deletions src/core/common/imgui/ui.cpp
Expand Up @@ -22,7 +22,7 @@ const ImColor ImGuiUI::m_laser_col[4] = {
ImColor(ImVec4(1.0f, 1.0f, 0.0f, 1.0f)) // ply4: yellow
};

bool ImGuiUI::Initialize(int backbuffer_scale)
bool ImGuiUI::Initialize()
{
IMGUI_CHECKVERSION();
m_imgui_context = ImGui::CreateContext();
Expand Down Expand Up @@ -55,7 +55,6 @@ bool ImGuiUI::Initialize(int backbuffer_scale)

// Internal initialize (when necessary, move into its own function.)
fps_counter = 30.0f;
m_backbuffer_scale = backbuffer_scale;

// Miscs
m_audio.Initialize();
Expand Down Expand Up @@ -203,7 +202,7 @@ void ImGuiUI::DrawLightgunLaser(int port)
{
ImGui::Begin("Laser", nullptr, ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoDecoration);

ImGui::GetForegroundDrawList()->AddCircleFilled(g_InputDeviceManager.CalcLaserPos(port), 10 * m_backbuffer_scale, m_laser_col[port], 32);
ImGui::GetForegroundDrawList()->AddCircleFilled(g_InputDeviceManager.CalcLaserPos(port), 5, m_laser_col[port], 0);

ImGui::End();
}
3 changes: 1 addition & 2 deletions src/core/common/imgui/ui.hpp
Expand Up @@ -36,7 +36,7 @@ class ImGuiUI

protected:

bool Initialize(int backbuffer_scale);
bool Initialize();
void Shutdown();

template<class C, class T>
Expand All @@ -62,7 +62,6 @@ class ImGuiUI
overlay_settings m_settings;
unsigned int m_lle_flags;
float fps_counter;
int m_backbuffer_scale;
static const ImColor m_laser_col[4];
// Make them as settings storage.
/*bool m_show_fps;
Expand Down
4 changes: 2 additions & 2 deletions src/core/common/video/RenderBase.cpp
Expand Up @@ -16,9 +16,9 @@

std::unique_ptr<RenderBase> g_renderbase;

bool RenderBase::Initialize(int backbuffer_scale)
bool RenderBase::Initialize()
{
if (!ImGuiUI::Initialize(backbuffer_scale)) {
if (!ImGuiUI::Initialize()) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/common/video/RenderBase.hpp
Expand Up @@ -16,7 +16,7 @@ class RenderBase : public ImGuiUI
RenderBase() = default;
virtual ~RenderBase() = default;

virtual bool Initialize(int backbuffer_scale);
virtual bool Initialize();
virtual void Shutdown();

template<class C, class T>
Expand Down
4 changes: 1 addition & 3 deletions src/core/hle/D3D8/Direct3D9/Direct3D9.cpp
Expand Up @@ -87,7 +87,6 @@ using namespace std::literals::chrono_literals;

// Global(s)
HWND g_hEmuWindow = NULL; // rendering window
bool g_bRenderWindowResized = true; // indicates that the rendering window has had its size changed
bool g_bClipCursor = false; // indicates that the mouse cursor should be confined inside the rendering window
IDirect3DDevice9Ex *g_pD3DDevice = nullptr; // Direct3D Device

Expand Down Expand Up @@ -669,7 +668,7 @@ void CxbxInitWindow(bool bFullInit)

SetFocus(g_hEmuWindow);
g_renderbase = std::unique_ptr<RenderBase>(new RenderBase());
g_renderbase->Initialize(g_RenderUpscaleFactor);
g_renderbase->Initialize();

ImGui_ImplWin32_Init(g_hEmuWindow);
g_renderbase->SetWindowRelease([] {
Expand Down Expand Up @@ -2038,7 +2037,6 @@ static LRESULT WINAPI EmuMsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPar

case WM_SIZE:
{
g_bRenderWindowResized = true;
switch(wParam)
{
case SIZE_RESTORED:
Expand Down
9 changes: 6 additions & 3 deletions src/core/hle/XAPI/Xapi.cpp
Expand Up @@ -59,7 +59,7 @@ std::atomic<bool> g_bIsDevicesEmulating = false;
std::atomic<bool> g_bXppGuard = false;

// Allocate enough memory for the max number of devices we can support simultaneously
// 4 duke / S / sbc / arcade joystick (mutually exclusive) + 8 memory units
// 4 duke / S / sbc / arcade joystick / lightgun (mutually exclusive) + 8 memory units
DeviceState g_devs[MAX_DEVS];

xbox::ulong_xt g_Mounted_MUs = 0;
Expand Down Expand Up @@ -264,10 +264,11 @@ void ConstructHleInputDevice(DeviceState *dev, DeviceState *upstream, int type,
dev->info.ucSubType = XINPUT_DEVSUBTYPE_GC_LIGHTGUN;
dev->info.ucInputStateSize = sizeof(XpadInput);
dev->info.ucFeedbackSize = sizeof(XpadOutput);
dev->info.ligthgun.offset_x = dev->info.ligthgun.offset_x = 0;
dev->info.ligthgun.offset_x = dev->info.ligthgun.offset_y = 0;
dev->info.ligthgun.offset_upp_x = dev->info.ligthgun.offset_upp_x = 0;
dev->info.ligthgun.last_in_state = dev->info.ligthgun.turbo_delay = 0;
dev->info.ligthgun.turbo = dev->info.ligthgun.last_turbo = 0;
dev->info.ligthgun.laser = 1; // laser on by default
g_EmuShared->GetLightgunLaser(&dev->info.ligthgun.laser);
break;

case to_underlying(XBOX_INPUT_DEVICE::STEEL_BATTALION_CONTROLLER):
Expand Down Expand Up @@ -871,6 +872,8 @@ xbox::dword_xt WINAPI xbox::EMUPATCH(XInputSetLightgunCalibration)
if (g_devs[port].type == XBOX_INPUT_DEVICE::LIGHTGUN) {
g_devs[port].info.ligthgun.offset_x = pCalibrationOffsets->wCenterX;
g_devs[port].info.ligthgun.offset_y = pCalibrationOffsets->wCenterY;
g_devs[port].info.ligthgun.offset_upp_x = pCalibrationOffsets->wUpperLeftX;
g_devs[port].info.ligthgun.offset_upp_y = pCalibrationOffsets->wUpperLeftY;
ret = ERROR_SUCCESS;
}
else {
Expand Down
14 changes: 7 additions & 7 deletions src/core/hle/XAPI/Xapi.h
Expand Up @@ -276,9 +276,9 @@ XINPUT_FEEDBACK, *PXINPUT_FEEDBACK;
// ******************************************************************
typedef struct _XINPUT_DEVICE_DESCRIPTION
{
WORD wVendorID;
WORD wProductID;
WORD wVersion;
xbox::ushort_xt wVendorID;
xbox::ushort_xt wProductID;
xbox::ushort_xt wVersion;
}
XINPUT_DEVICE_DESCRIPTION, *PXINPUT_DEVICE_DESCRIPTION;

Expand All @@ -287,10 +287,10 @@ XINPUT_DEVICE_DESCRIPTION, *PXINPUT_DEVICE_DESCRIPTION;
// ******************************************************************
typedef struct _XINPUT_LIGHTGUN_CALIBRATION_OFFSETS
{
WORD wCenterX;
WORD wCenterY;
WORD wUpperLeftX;
WORD wUpperLeftY;
xbox::short_xt wCenterX;
xbox::short_xt wCenterY;
xbox::short_xt wUpperLeftX;
xbox::short_xt wUpperLeftY;
}
XINPUT_LIGHTGUN_CALIBRATION_OFFSETS, *PXINPUT_LIGHTGUN_CALIBRATION_OFFSETS;

Expand Down

0 comments on commit a1c838a

Please sign in to comment.