Skip to content

Commit

Permalink
Add confirmation popup support on Exit App key
Browse files Browse the repository at this point in the history
This adds a new mechanism so we can delay "vkey" events until the next
frame, making for safer code. Will move a bunch of the virtkeys to this
later.

Fixes #20020
  • Loading branch information
hrydgard committed Feb 24, 2025
1 parent 9043f75 commit 4b77cfd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 23 deletions.
22 changes: 11 additions & 11 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ void ConvertAnalogStick(float x, float y, float *outX, float *outY) {
}

void ControlMapper::SetCallbacks(
std::function<void(int, bool)> onVKey,
std::function<void(int, float)> onVKeyAnalog,
std::function<void(VirtKey, bool)> onVKey,
std::function<void(VirtKey, float)> onVKeyAnalog,
std::function<void(uint32_t, uint32_t)> updatePSPButtons,
std::function<void(int, float, float)> setPSPAnalog,
std::function<void(int, float, float)> setRawAnalog) {
Expand Down Expand Up @@ -421,7 +421,7 @@ bool ControlMapper::UpdatePSPState(const InputMapping &changedMapping, double no
// OK, handle all the virtual keys next. For these we need to do deltas here and send events.
// Note that virtual keys include the analog directions, as they are driven by them.
for (int i = 0; i < VIRTKEY_COUNT; i++) {
int vkId = i + VIRTKEY_FIRST;
VirtKey vkId = (VirtKey)(i + VIRTKEY_FIRST);

uint32_t idForMapping = vkId;
SwapMappingIfEnabled(&idForMapping);
Expand Down Expand Up @@ -578,7 +578,7 @@ void ControlMapper::ToggleSwapAxes() {

updatePSPButtons_(0, CTRL_LEFT | CTRL_RIGHT | CTRL_UP | CTRL_DOWN);

for (uint32_t vkey = VIRTKEY_FIRST; vkey < VIRTKEY_LAST; vkey++) {
for (VirtKey vkey = VIRTKEY_FIRST; vkey < VIRTKEY_LAST; vkey = (VirtKey)(vkey + 1)) {
if (IsSwappableVKey(vkey)) {
if (virtKeyOn_[vkey - VIRTKEY_FIRST]) {
onVKey_(vkey, false);
Expand Down Expand Up @@ -668,13 +668,13 @@ void ControlMapper::PSPKey(int deviceId, int pspKeyCode, int flags) {
int vk = pspKeyCode - VIRTKEY_FIRST;
if (flags & KEY_DOWN) {
virtKeys_[vk] = 1.0f;
onVKey(pspKeyCode, true);
onVKeyAnalog(deviceId, pspKeyCode, 1.0f);
onVKey((VirtKey)pspKeyCode, true);
onVKeyAnalog(deviceId, (VirtKey)pspKeyCode, 1.0f);
}
if (flags & KEY_UP) {
virtKeys_[vk] = 0.0f;
onVKey(pspKeyCode, false);
onVKeyAnalog(deviceId, pspKeyCode, 0.0f);
onVKey((VirtKey)pspKeyCode, false);
onVKeyAnalog(deviceId, (VirtKey)pspKeyCode, 0.0f);
}
} else {
// INFO_LOG(Log::System, "pspKey %d %d", pspKeyCode, flags);
Expand All @@ -685,7 +685,7 @@ void ControlMapper::PSPKey(int deviceId, int pspKeyCode, int flags) {
}
}

void ControlMapper::onVKeyAnalog(int deviceId, int vkey, float value) {
void ControlMapper::onVKeyAnalog(int deviceId, VirtKey vkey, float value) {
// Unfortunately, for digital->analog inputs to work sanely, we need to sum up
// with the opposite value too.
int stick = 0;
Expand Down Expand Up @@ -716,7 +716,7 @@ void ControlMapper::onVKeyAnalog(int deviceId, int vkey, float value) {
SetPSPAxis(deviceId, stick, axis, sign * value);
}

void ControlMapper::onVKey(int vkey, bool down) {
void ControlMapper::onVKey(VirtKey vkey, bool down) {
switch (vkey) {
case VIRTKEY_ANALOG_ROTATE_CW:
if (down) {
Expand Down Expand Up @@ -745,7 +745,7 @@ void ControlMapper::onVKey(int vkey, bool down) {

void ControlMapper::GetDebugString(char *buffer, size_t bufSize) const {
std::stringstream str;
for (auto iter : curInput_) {
for (auto &iter : curInput_) {
char temp[256];
iter.first.FormatDebug(temp, sizeof(temp));
str << temp << ": " << iter.second.value << std::endl;
Expand Down
12 changes: 6 additions & 6 deletions Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ControlMapper {
// Required callbacks.
// TODO: These are so many now that a virtual interface might be more appropriate..
void SetCallbacks(
std::function<void(int, bool)> onVKey,
std::function<void(int, float)> onVKeyAnalog,
std::function<void(VirtKey, bool)> onVKey,
std::function<void(VirtKey, float)> onVKeyAnalog,
std::function<void(uint32_t, uint32_t)> updatePSPButtons,
std::function<void(int, float, float)> setPSPAnalog,
std::function<void(int, float, float)> setRawAnalog);
Expand Down Expand Up @@ -58,8 +58,8 @@ class ControlMapper {
void SetPSPAxis(int deviceId, int stick, char axis, float value);
void UpdateAnalogOutput(int stick);

void onVKey(int vkey, bool down);
void onVKeyAnalog(int deviceId, int vkey, float value);
void onVKey(VirtKey vkey, bool down);
void onVKeyAnalog(int deviceId, VirtKey vkey, float value);

void UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp);
float GetDeviceAxisThreshold(int device, const InputMapping &mapping);
Expand Down Expand Up @@ -93,8 +93,8 @@ class ControlMapper {
std::map<InputMapping, InputSample> curInput_;

// Callbacks
std::function<void(int, bool)> onVKey_;
std::function<void(int, float)> onVKeyAnalog_;
std::function<void(VirtKey, bool)> onVKey_;
std::function<void(VirtKey, float)> onVKeyAnalog_;
std::function<void(uint32_t, uint32_t)> updatePSPButtons_;
std::function<void(int, float, float)> setPSPAnalog_;
std::function<void(int, float, float)> setRawAnalog_;
Expand Down
2 changes: 1 addition & 1 deletion Core/KeyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#define KEYMAP_ERROR_UNKNOWN_KEY 0

// Don't change any of these - it'll break backwards compatibility with configs.
enum {
enum VirtKey {
VIRTKEY_FIRST = 0x40000001,
VIRTKEY_AXIS_X_MIN = 0x40000001,
VIRTKEY_AXIS_Y_MIN = 0x40000002,
Expand Down
36 changes: 33 additions & 3 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ static void ShowFpsLimitNotice() {
g_OSD.Show(OSDType::TRANSPARENT_STATUS, temp, "", "I_FASTFORWARD", 1.5f, "altspeed");
}

void EmuScreen::onVKey(int virtualKeyCode, bool down) {
void EmuScreen::onVKey(VirtKey virtualKeyCode, bool down) {
auto sc = GetI18NCategory(I18NCat::SCREEN);
auto mc = GetI18NCategory(I18NCat::MAPPABLECONTROLS);

Expand Down Expand Up @@ -953,13 +953,41 @@ void EmuScreen::onVKey(int virtualKeyCode, bool down) {
"%s: %s", n->T("Enable networking"), g_Config.bEnableWlan ? di->T("Enabled") : di->T("Disabled")), 2.0, "toggle_wlan");
}
break;
default:
// To make sure we're not in an async context.
if (down) {
queuedVirtKey_ = virtualKeyCode;
}
break;
}
}

void EmuScreen::ProcessQueuedVKeys() {
switch (queuedVirtKey_) {
case VIRTKEY_EXIT_APP:
System_ExitApp();
{
std::string confirmExitMessage = GetConfirmExitMessage();
if (!confirmExitMessage.empty()) {
auto di = GetI18NCategory(I18NCat::DIALOG);
confirmExitMessage += '\n';
confirmExitMessage += di->T("Are you sure you want to exit?");
screenManager()->push(new PromptScreen(gamePath_, confirmExitMessage, di->T("Yes"), di->T("No"), [=](bool result) {
if (result) {
System_ExitApp();
}
}));
} else {
System_ExitApp();
}
break;
}
default:
break;
}
queuedVirtKey_ = (VirtKey)0;
}

void EmuScreen::onVKeyAnalog(int virtualKeyCode, float value) {
void EmuScreen::onVKeyAnalog(VirtKey virtualKeyCode, float value) {
if (virtualKeyCode != VIRTKEY_SPEED_ANALOG) {
return;
}
Expand Down Expand Up @@ -1478,6 +1506,8 @@ ScreenRenderFlags EmuScreen::render(ScreenRenderMode mode) {

GamepadUpdateOpacity();

ProcessQueuedVKeys();

bool skipBufferEffects = g_Config.bSkipBufferEffects;

bool framebufferBound = false;
Expand Down
8 changes: 6 additions & 2 deletions UI/EmuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,14 @@ class EmuScreen : public UIScreen {
void runImDebugger();
void renderImDebugger();

void onVKey(int virtualKeyCode, bool down);
void onVKeyAnalog(int virtualKeyCode, float value);
void onVKey(VirtKey virtualKeyCode, bool down);
void onVKeyAnalog(VirtKey virtualKeyCode, float value);

void autoLoad();
bool checkPowerDown();

void ProcessQueuedVKeys();

UI::Event OnDevMenu;
UI::Event OnChatMenu;
bool bootPending_ = true;
Expand Down Expand Up @@ -144,6 +146,8 @@ class EmuScreen : public UIScreen {
bool keyAltRight_ = false;

bool lastImguiEnabled_ = false;

VirtKey queuedVirtKey_ = (VirtKey)0;
};

bool MustRunBehind();
Expand Down

0 comments on commit 4b77cfd

Please sign in to comment.