CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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