2021-07-08 19:30:23 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Common/Input/InputState.h"
|
|
|
|
#include "Core/KeyMap.h"
|
|
|
|
|
|
|
|
#include <functional>
|
2023-03-29 08:21:49 +00:00
|
|
|
#include <cstring>
|
2023-03-30 13:28:08 +00:00
|
|
|
#include <mutex>
|
2021-07-08 19:30:23 +00:00
|
|
|
|
|
|
|
// Utilities for mapping input events to PSP inputs and virtual keys.
|
|
|
|
// Main use is of course from EmuScreen.cpp, but also useful from control settings etc.
|
|
|
|
class ControlMapper {
|
|
|
|
public:
|
2023-08-23 16:42:20 +00:00
|
|
|
void Update(double now);
|
2021-07-09 14:14:32 +00:00
|
|
|
|
2023-03-26 16:04:40 +00:00
|
|
|
// Inputs to the table-based mapping
|
2023-03-30 13:28:08 +00:00
|
|
|
// These functions are free-threaded.
|
2021-07-08 19:30:23 +00:00
|
|
|
bool Key(const KeyInput &key, bool *pauseTrigger);
|
2023-09-27 15:34:34 +00:00
|
|
|
void Axis(const AxisInput *axes, size_t count);
|
2021-07-08 19:30:23 +00:00
|
|
|
|
2023-03-31 09:11:46 +00:00
|
|
|
// Required callbacks.
|
|
|
|
// TODO: These are so many now that a virtual interface might be more appropriate..
|
2021-07-08 19:30:23 +00:00
|
|
|
void SetCallbacks(
|
2023-03-30 08:47:28 +00:00
|
|
|
std::function<void(int, bool)> onVKey,
|
2023-03-29 11:50:57 +00:00
|
|
|
std::function<void(int, float)> onVKeyAnalog,
|
2023-04-01 06:57:42 +00:00
|
|
|
std::function<void(uint32_t, uint32_t)> updatePSPButtons,
|
2023-03-31 09:11:46 +00:00
|
|
|
std::function<void(int, float, float)> setPSPAnalog,
|
|
|
|
std::function<void(int, float, float)> setRawAnalog);
|
2021-07-08 19:30:23 +00:00
|
|
|
|
2023-03-26 16:04:40 +00:00
|
|
|
// Inject raw PSP key input directly, such as from touch screen controls.
|
2023-03-31 09:11:46 +00:00
|
|
|
// Combined with the mapped input. Unlike __Ctrl APIs, this supports
|
2023-05-08 08:46:28 +00:00
|
|
|
// virtual key codes, including analog mappings.
|
2023-03-26 16:04:40 +00:00
|
|
|
void PSPKey(int deviceId, int pspKeyCode, int flags);
|
|
|
|
|
2023-04-19 22:00:46 +00:00
|
|
|
// Toggle swapping DPAD and Analog. Useful on some input devices with few buttons.
|
|
|
|
void ToggleSwapAxes();
|
|
|
|
|
2023-05-02 19:36:17 +00:00
|
|
|
// Call this when a Vkey press triggers leaving the screen you're using the controlmapper on. This can cause
|
|
|
|
// the loss of key-up events, which will confuse things later when you're back.
|
|
|
|
// Might replace this later by allowing through "key-up" and similar events to lower screens.
|
|
|
|
void ForceReleaseVKey(int vkey);
|
|
|
|
|
2024-01-29 17:41:43 +00:00
|
|
|
// Call when the emu screen gets pushed behind some other screen, like the pause screen, to release all "down" inputs.
|
|
|
|
void ReleaseAll();
|
|
|
|
|
2023-03-31 18:12:48 +00:00
|
|
|
void GetDebugString(char *buffer, size_t bufSize) const;
|
|
|
|
|
2023-08-23 15:52:30 +00:00
|
|
|
struct InputSample {
|
|
|
|
float value;
|
|
|
|
double timestamp;
|
|
|
|
};
|
|
|
|
|
2021-07-08 19:30:23 +00:00
|
|
|
private:
|
2023-08-23 16:42:20 +00:00
|
|
|
bool UpdatePSPState(const InputMapping &changedMapping, double now);
|
2023-04-01 07:07:29 +00:00
|
|
|
float MapAxisValue(float value, int vkId, const InputMapping &mapping, const InputMapping &changedMapping, bool *oppositeTouched);
|
2023-04-19 22:00:46 +00:00
|
|
|
void SwapMappingIfEnabled(uint32_t *vkey);
|
2023-03-29 08:21:49 +00:00
|
|
|
|
2023-03-29 11:50:57 +00:00
|
|
|
void SetPSPAxis(int deviceId, int stick, char axis, float value);
|
2023-04-15 15:36:55 +00:00
|
|
|
void UpdateAnalogOutput(int stick);
|
2021-07-08 19:30:23 +00:00
|
|
|
|
2023-03-29 11:50:57 +00:00
|
|
|
void onVKey(int vkey, bool down);
|
|
|
|
void onVKeyAnalog(int deviceId, int vkey, float value);
|
2021-07-08 19:30:23 +00:00
|
|
|
|
2023-05-06 14:38:46 +00:00
|
|
|
void UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp);
|
2023-12-31 12:53:29 +00:00
|
|
|
float GetDeviceAxisThreshold(int device, const InputMapping &mapping);
|
2023-05-06 14:38:46 +00:00
|
|
|
|
2021-07-08 19:30:23 +00:00
|
|
|
// To track mappable virtual keys. We can have as many as we want.
|
2023-03-29 11:50:57 +00:00
|
|
|
float virtKeys_[VIRTKEY_COUNT]{};
|
2023-04-15 15:36:55 +00:00
|
|
|
bool virtKeyOn_[VIRTKEY_COUNT]{}; // Track boolean output separaately since thresholds may differ.
|
2021-07-08 19:30:23 +00:00
|
|
|
|
2023-12-31 12:53:29 +00:00
|
|
|
// This is only used for co-axis (analog stick to buttons), so not bothering to track separately
|
|
|
|
// per device.
|
|
|
|
float rawAxisValue_[JOYSTICK_AXIS_MAX]{};
|
|
|
|
|
2023-09-27 15:36:05 +00:00
|
|
|
double deviceTimestamps_[(size_t)DEVICE_ID_COUNT]{};
|
2023-08-23 16:42:20 +00:00
|
|
|
|
2022-11-23 11:22:59 +00:00
|
|
|
int lastNonDeadzoneDeviceID_[2]{};
|
|
|
|
|
2023-03-26 16:04:40 +00:00
|
|
|
float history_[2][2]{};
|
2023-03-31 18:12:48 +00:00
|
|
|
float converted_[2][2]{}; // for debug display
|
2022-11-23 11:22:59 +00:00
|
|
|
|
2021-07-09 14:14:32 +00:00
|
|
|
// Mappable auto-rotation. Useful for keyboard/dpad->analog in a few games.
|
|
|
|
bool autoRotatingAnalogCW_ = false;
|
|
|
|
bool autoRotatingAnalogCCW_ = false;
|
|
|
|
|
2023-04-19 22:00:46 +00:00
|
|
|
bool swapAxes_ = false;
|
|
|
|
|
2023-03-30 13:28:08 +00:00
|
|
|
// Protects basically all the state.
|
2023-09-27 15:34:34 +00:00
|
|
|
// TODO: Maybe we should piggyback on the screenmanager mutex - it's always locked
|
|
|
|
// when events come in here.
|
2023-03-30 13:28:08 +00:00
|
|
|
std::mutex mutex_;
|
|
|
|
|
2023-08-23 15:52:30 +00:00
|
|
|
std::map<InputMapping, InputSample> curInput_;
|
2023-03-29 08:21:49 +00:00
|
|
|
|
2021-07-08 19:30:23 +00:00
|
|
|
// Callbacks
|
2023-03-30 08:47:28 +00:00
|
|
|
std::function<void(int, bool)> onVKey_;
|
2023-03-29 11:50:57 +00:00
|
|
|
std::function<void(int, float)> onVKeyAnalog_;
|
2023-04-01 06:57:42 +00:00
|
|
|
std::function<void(uint32_t, uint32_t)> updatePSPButtons_;
|
2021-07-09 09:01:56 +00:00
|
|
|
std::function<void(int, float, float)> setPSPAnalog_;
|
2021-07-09 11:10:16 +00:00
|
|
|
std::function<void(int, float, float)> setRawAnalog_;
|
2021-07-08 19:30:23 +00:00
|
|
|
};
|
2021-07-09 11:10:16 +00:00
|
|
|
|
2023-03-30 12:42:11 +00:00
|
|
|
void ConvertAnalogStick(float x, float y, float *outX, float *outY);
|
2023-12-28 12:36:03 +00:00
|
|
|
float GetDeviceAxisThreshold(int device, const InputMapping &mapping);
|