Skip to content

Commit

Permalink
Control: Add debug display, do assorted fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Mar 31, 2023
1 parent 025ec24 commit 48993f4
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 43 deletions.
10 changes: 10 additions & 0 deletions Common/Input/InputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ int GetAnalogYDirection(int deviceId) {
return configured->second;
return 0;
}

void InputMapping::FormatDebug(char *buffer, size_t bufSize) const {
if (IsAxis()) {
int direction;
int axis = Axis(&direction);
snprintf(buffer, bufSize, "Device: %d Axis: %d (%d)", deviceId, axis, direction);
} else {
snprintf(buffer, bufSize, "Device: %d Key: %d", deviceId, keyCode);
}
}
2 changes: 2 additions & 0 deletions Common/Input/InputState.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class InputMapping {
if (keyCode != other.keyCode) return false;
return true;
}

void FormatDebug(char *buffer, size_t bufSize) const;
};

enum {
Expand Down
2 changes: 2 additions & 0 deletions Core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,11 @@ struct Config {

std::string sThemeName;

// These aren't saved, just for instant debugging.
bool bLogFrameDrops;
bool bShowDebugStats;
bool bShowAudioDebug;
bool bShowControlDebug;
bool bShowGpuProfile;

// Analog stick tilting
Expand Down
64 changes: 41 additions & 23 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <algorithm>
#include <sstream>

#include "Common/Math/math_util.h"
#include "Common/TimeUtil.h"
#include "Common/StringUtils.h"
#include "Common/Log.h"

#include "Core/HLE/sceCtrl.h"
Expand Down Expand Up @@ -31,6 +33,11 @@ static int GetOppositeVKey(int vkey) {
}
}

static bool IsAxisVKey(int vkey) {
// Little hacky but works, of course.
return GetOppositeVKey(vkey) != 0;
}

static bool IsUnsignedMapping(int vkey) {
return vkey == VIRTKEY_SPEED_ANALOG;
}
Expand Down Expand Up @@ -131,6 +138,8 @@ void ControlMapper::SetPSPAxis(int device, int stick, char axis, float value) {
history_[stick][axisId] = value;
float x, y;
ConvertAnalogStick(history_[stick][0], history_[stick][1], &x, &y);
converted_[stick][0] = x;
converted_[stick][1] = y;
setPSPAnalog_(stick, x, y);
}
}
Expand Down Expand Up @@ -207,7 +216,6 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping) {
// mapping which gets a little hacky.
float threshold = 1.0f;
bool touchedByMapping = false;
bool zeroOpposite = false;
float value = 0.0f;
for (auto &mapping : inputMappings) {
if (mapping == changedMapping) {
Expand Down Expand Up @@ -242,7 +250,6 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping) {
value += iter->second;
}
} else {
zeroOpposite = true;
value += iter->second;
}
} else {
Expand All @@ -268,14 +275,6 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping) {
virtKeys_[i] = value;
}

if (zeroOpposite) {
// For analog stick events, always zero the "opposite" when we get a valid value.
// Otherwise, lingering small values can create strange offsets when summing up later.
int opposite = GetOppositeVKey(vkId);
if (opposite) {
virtKeys_[i] = 0.0f;
}
}
if (!bPrevValue && bValue) {
// INFO_LOG(G3D, "vkeyon %s", KeyMap::GetVirtKeyName(vkId));
onVKey(vkId, true);
Expand Down Expand Up @@ -319,22 +318,20 @@ bool ControlMapper::Key(const KeyInput &key, bool *pauseTrigger) {

void ControlMapper::Axis(const AxisInput &axis) {
std::lock_guard<std::mutex> guard(mutex_);
if (axis.value > 0) {
if (axis.value >= 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, 1);
InputMapping opposite(axis.deviceId, axis.axisId, -1);
curInput_[mapping] = axis.value;
curInput_[opposite] = 0.0f;
UpdatePSPState(mapping);
} else if (axis.value < 0) {
UpdatePSPState(opposite);
} else if (axis.value < 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, -1);
InputMapping opposite(axis.deviceId, axis.axisId, 1);
curInput_[mapping] = -axis.value;
curInput_[opposite] = 0.0f;
UpdatePSPState(mapping);
} else if (axis.value == 0.0f) { // Threshold?
// Both directions! Prevents sticking for digital input devices that are axises (like HAT)
InputMapping mappingPositive(axis.deviceId, axis.axisId, 1);
InputMapping mappingNegative(axis.deviceId, axis.axisId, -1);
curInput_[mappingPositive] = 0.0f;
curInput_[mappingNegative] = 0.0f;
UpdatePSPState(mappingPositive);
UpdatePSPState(mappingNegative);
UpdatePSPState(opposite);
}
}

Expand Down Expand Up @@ -381,7 +378,7 @@ void ControlMapper::onVKeyAnalog(int deviceId, int vkey, float value) {
// with the opposite value too.
int stick = 0;
int axis = 'X';
int opposite = GetOppositeVKey(vkey);
int oppositeVKey = GetOppositeVKey(vkey);
float sign = 1.0f;
switch (vkey) {
case VIRTKEY_AXIS_X_MIN: sign = -1.0f; break;
Expand All @@ -397,8 +394,12 @@ void ControlMapper::onVKeyAnalog(int deviceId, int vkey, float value) {
onVKeyAnalog_(vkey, value);
return;
}
if (opposite != 0) {
value -= virtKeys_[opposite - VIRTKEY_FIRST];
if (oppositeVKey != 0) {
float oppVal = virtKeys_[oppositeVKey - VIRTKEY_FIRST];
if (oppVal != 0.0f) {
value -= oppVal;
// NOTICE_LOG(SCECTRL, "Reducing %f by %f (from %08x : %s)", value, oppVal, oppositeVKey, KeyMap::GetPspButtonName(oppositeVKey).c_str());
}
}
SetPSPAxis(deviceId, stick, axis, sign * value);
}
Expand Down Expand Up @@ -429,3 +430,20 @@ void ControlMapper::onVKey(int vkey, bool down) {
break;
}
}

void ControlMapper::GetDebugString(char *buffer, size_t bufSize) const {
std::stringstream str;
for (auto iter : curInput_) {
char temp[256];
iter.first.FormatDebug(temp, sizeof(temp));
str << temp << ": " << iter.second << std::endl;
}
for (int i = 0; i < ARRAY_SIZE(virtKeys_); i++) {
int vkId = VIRTKEY_FIRST + i;
if ((vkId >= VIRTKEY_AXIS_X_MIN && vkId <= VIRTKEY_AXIS_Y_MAX) || vkId == VIRTKEY_ANALOG_LIGHTLY) {
str << KeyMap::GetPspButtonName(vkId) << ": " << virtKeys_[i] << std::endl;
}
}
str << "Lstick: " << converted_[0][0] << ", " << converted_[0][1] << std::endl;
truncate_cpy(buffer, bufSize, str.str().c_str());
}
3 changes: 3 additions & 0 deletions Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ControlMapper {
// virtual key codes, though not analog mappings.
void PSPKey(int deviceId, int pspKeyCode, int flags);

void GetDebugString(char *buffer, size_t bufSize) const;

private:
bool UpdatePSPState(const InputMapping &changedMapping);

Expand All @@ -47,6 +49,7 @@ class ControlMapper {
int lastNonDeadzoneDeviceID_[2]{};

float history_[2][2]{};
float converted_[2][2]{}; // for debug display

// Mappable auto-rotation. Useful for keyboard/dpad->analog in a few games.
bool autoRotatingAnalogCW_ = false;
Expand Down
3 changes: 2 additions & 1 deletion Core/KeyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,10 @@ const KeyMap_IntStrPair psp_button_names[] = {
};

static std::string FindName(int key, const KeyMap_IntStrPair list[], size_t size) {
for (size_t i = 0; i < size; i++)
for (size_t i = 0; i < size; i++) {
if (list[i].key == key)
return list[i].name;
}
return StringFromFormat("%02x?", key);
}

Expand Down
14 changes: 8 additions & 6 deletions UI/DevScreens.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,14 @@ void DevMenuScreen::CreatePopupContents(UI::ViewGroup *parent) {
items->Add(new Choice(dev->T("Toggle Freeze")))->OnClick.Handle(this, &DevMenuScreen::OnFreezeFrame);

items->Add(new Choice(dev->T("Dump next frame to log")))->OnClick.Handle(this, &DevMenuScreen::OnDumpFrame);
items->Add(new Choice(dev->T("Toggle Audio Debug")))->OnClick.Handle(this, &DevMenuScreen::OnToggleAudioDebug);
items->Add(new Choice(dev->T("Toggle Audio Debug")))->OnClick.Add([](UI::EventParams &) {
g_Config.bShowAudioDebug = !g_Config.bShowAudioDebug;
return UI::EVENT_DONE;
});
items->Add(new Choice(dev->T("Toggle Control Debug")))->OnClick.Add([](UI::EventParams &) {
g_Config.bShowControlDebug = !g_Config.bShowControlDebug;
return UI::EVENT_DONE;
});
#ifdef USE_PROFILER
items->Add(new CheckBox(&g_Config.bShowFrameProfiler, dev->T("Frame Profiler"), ""));
#endif
Expand All @@ -123,11 +130,6 @@ void DevMenuScreen::CreatePopupContents(UI::ViewGroup *parent) {
}
}

UI::EventReturn DevMenuScreen::OnToggleAudioDebug(UI::EventParams &e) {
g_Config.bShowAudioDebug = !g_Config.bShowAudioDebug;
return UI::EVENT_DONE;
}

UI::EventReturn DevMenuScreen::OnResetLimitedLogging(UI::EventParams &e) {
Reporting::ResetCounts();
return UI::EVENT_DONE;
Expand Down
1 change: 0 additions & 1 deletion UI/DevScreens.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class DevMenuScreen : public PopupScreen {
UI::EventReturn OnFreezeFrame(UI::EventParams &e);
UI::EventReturn OnDumpFrame(UI::EventParams &e);
UI::EventReturn OnDeveloperTools(UI::EventParams &e);
UI::EventReturn OnToggleAudioDebug(UI::EventParams &e);
UI::EventReturn OnResetLimitedLogging(UI::EventParams &e);

private:
Expand Down
47 changes: 35 additions & 12 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,23 @@ static void DrawAudioDebugStats(UIContext *ctx, const Bounds &bounds) {
ctx->RebindTexture();
}

static void DrawControlDebug(UIContext *ctx, const ControlMapper &mapper, const Bounds &bounds) {
FontID ubuntu24("UBUNTU24");

char statbuf[4096] = { 0 };
mapper.GetDebugString(statbuf, sizeof(statbuf));
// System_AudioGetDebugStats(statbuf, sizeof(statbuf));

ctx->Flush();
ctx->BindFontTexture();
ctx->Draw()->SetFontScale(0.5f, 0.5f);
ctx->Draw()->DrawTextRect(ubuntu24, statbuf, bounds.x + 11, bounds.y + 31, bounds.w - 20, bounds.h - 30, 0xc0000000, FLAG_DYNAMIC_ASCII);
ctx->Draw()->DrawTextRect(ubuntu24, statbuf, bounds.x + 10, bounds.y + 30, bounds.w - 20, bounds.h - 30, 0xFFFFFFFF, FLAG_DYNAMIC_ASCII);
ctx->Draw()->SetFontScale(1.0f, 1.0f);
ctx->Flush();
ctx->RebindTexture();
}

static void DrawFPS(UIContext *ctx, const Bounds &bounds) {
FontID ubuntu24("UBUNTU24");
float vps, fps, actual_fps;
Expand Down Expand Up @@ -1549,7 +1566,7 @@ bool EmuScreen::hasVisibleUI() {
if (g_Config.bEnableCardboardVR || g_Config.bEnableNetworkChat)
return true;
// Debug UI.
if (g_Config.bShowDebugStats || g_Config.bShowDeveloperMenu || g_Config.bShowAudioDebug || g_Config.bShowFrameProfiler)
if (g_Config.bShowDebugStats || g_Config.bShowDeveloperMenu || g_Config.bShowAudioDebug || g_Config.bShowFrameProfiler || g_Config.bShowControlDebug)
return true;

// Exception information.
Expand Down Expand Up @@ -1583,20 +1600,26 @@ void EmuScreen::renderUI() {
root_->Draw(*ctx);
}

if (g_Config.bShowDebugStats && !invalid_) {
DrawDebugStats(ctx, ctx->GetLayoutBounds());
}
if (!invalid_) {
if (g_Config.bShowDebugStats) {
DrawDebugStats(ctx, ctx->GetLayoutBounds());
}

if (g_Config.bShowAudioDebug && !invalid_) {
DrawAudioDebugStats(ctx, ctx->GetLayoutBounds());
}
if (g_Config.bShowAudioDebug) {
DrawAudioDebugStats(ctx, ctx->GetLayoutBounds());
}

if (g_Config.iShowStatusFlags && !invalid_) {
DrawFPS(ctx, ctx->GetLayoutBounds());
}
if (g_Config.iShowStatusFlags) {
DrawFPS(ctx, ctx->GetLayoutBounds());
}

if (g_Config.bDrawFrameGraph && !invalid_) {
DrawFrameTimes(ctx, ctx->GetLayoutBounds());
if (g_Config.bDrawFrameGraph) {
DrawFrameTimes(ctx, ctx->GetLayoutBounds());
}

if (g_Config.bShowControlDebug) {
DrawControlDebug(ctx, controlMapper_, ctx->GetLayoutBounds());
}
}

#if !PPSSPP_PLATFORM(UWP) && !PPSSPP_PLATFORM(SWITCH)
Expand Down

0 comments on commit 48993f4

Please sign in to comment.