Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/ControlMapper.h
5654 views
1
#pragma once
2
3
#include "Common/Input/InputState.h"
4
#include "Core/KeyMap.h"
5
6
#include <functional>
7
#include <cstring>
8
#include <mutex>
9
10
struct DisplayLayoutConfig;
11
// Utilities for mapping input events to PSP inputs and virtual keys.
12
// Main use is of course from EmuScreen.cpp, but also useful from control settings etc.
13
class ControlMapper {
14
public:
15
void Update(const DisplayLayoutConfig &config, double now);
16
17
// Inputs to the table-based mapping
18
// These functions are free-threaded.
19
bool Key(const KeyInput &key, bool *pauseTrigger);
20
void Axis(const AxisInput *axes, size_t count);
21
22
// Required callbacks.
23
// TODO: These are so many now that a virtual interface might be more appropriate..
24
void SetCallbacks(
25
std::function<void(VirtKey, bool)> onVKey,
26
std::function<void(VirtKey, float)> onVKeyAnalog,
27
std::function<void(uint32_t, uint32_t)> updatePSPButtons,
28
std::function<void(int, int, float, float)> setPSPAnalog,
29
std::function<void(int, float, float)> setRawAnalog);
30
31
// Inject raw PSP key input directly, such as from touch screen controls.
32
// Combined with the mapped input. Unlike __Ctrl APIs, this supports
33
// virtual key codes, including analog mappings.
34
void PSPKey(int deviceId, int pspKeyCode, KeyInputFlags flags);
35
36
// Toggle swapping DPAD and Analog. Useful on some input devices with few buttons.
37
void ToggleSwapAxes();
38
39
// Call this when a Vkey press triggers leaving the screen you're using the controlmapper on. This can cause
40
// the loss of key-up events, which will confuse things later when you're back.
41
// Might replace this later by allowing through "key-up" and similar events to lower screens.
42
void ForceReleaseVKey(int vkey);
43
44
// Call when the emu screen gets pushed behind some other screen, like the pause screen, to release all "down" inputs.
45
void ReleaseAll();
46
47
void GetDebugString(char *buffer, size_t bufSize) const;
48
49
struct InputSample {
50
float value;
51
double timestamp;
52
};
53
54
private:
55
bool UpdatePSPState(const InputMapping &changedMapping, double now);
56
float MapAxisValue(float value, int vkId, const InputMapping &mapping, const InputMapping &changedMapping, bool *oppositeTouched);
57
void SwapMappingIfEnabled(uint32_t *vkey);
58
59
void SetPSPAxis(int deviceId, int stick, char axis, float value);
60
void UpdateAnalogOutput(int stick);
61
62
void onVKey(VirtKey vkey, bool down);
63
void onVKeyAnalog(int deviceId, VirtKey vkey, float value);
64
65
void UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp);
66
float GetDeviceAxisThreshold(int device, const InputMapping &mapping);
67
68
// To track mappable virtual keys. We can have as many as we want.
69
float virtKeys_[VIRTKEY_COUNT]{};
70
bool virtKeyOn_[VIRTKEY_COUNT]{}; // Track boolean output separaately since thresholds may differ.
71
72
// This is only used for co-axis (analog stick to buttons), so not bothering to track separately
73
// per device.
74
float rawAxisValue_[JOYSTICK_AXIS_MAX]{};
75
76
double deviceTimestamps_[(size_t)DEVICE_ID_COUNT]{};
77
78
int lastNonDeadzoneDeviceID_[2]{};
79
80
float history_[2][2]{};
81
float converted_[2][2]{}; // for debug display
82
83
// Mappable auto-rotation. Useful for keyboard/dpad->analog in a few games.
84
bool autoRotatingAnalogCW_ = false;
85
bool autoRotatingAnalogCCW_ = false;
86
87
bool swapAxes_ = false;
88
89
int iInternalScreenRotationCached_ = 0;
90
91
// Protects basically all the state.
92
// TODO: Maybe we should piggyback on the screenmanager mutex - it's always locked
93
// when events come in here.
94
std::mutex mutex_;
95
96
std::map<InputMapping, InputSample> curInput_;
97
98
// Callbacks
99
std::function<void(VirtKey, bool)> onVKey_;
100
std::function<void(VirtKey, float)> onVKeyAnalog_;
101
std::function<void(uint32_t, uint32_t)> updatePSPButtons_;
102
std::function<void(int, int, float, float)> setPSPAnalog_;
103
std::function<void(int, float, float)> setRawAnalog_;
104
};
105
106
void ConvertAnalogStick(float x, float y, float *outX, float *outY);
107
float GetDeviceAxisThreshold(int device, const InputMapping &mapping);
108
109