CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ControlMapper.h
Views: 1401
#pragma once12#include "Common/Input/InputState.h"3#include "Core/KeyMap.h"45#include <functional>6#include <cstring>7#include <mutex>89// Utilities for mapping input events to PSP inputs and virtual keys.10// Main use is of course from EmuScreen.cpp, but also useful from control settings etc.11class ControlMapper {12public:13void Update(double now);1415// Inputs to the table-based mapping16// These functions are free-threaded.17bool Key(const KeyInput &key, bool *pauseTrigger);18void Axis(const AxisInput *axes, size_t count);1920// Required callbacks.21// TODO: These are so many now that a virtual interface might be more appropriate..22void SetCallbacks(23std::function<void(int, bool)> onVKey,24std::function<void(int, float)> onVKeyAnalog,25std::function<void(uint32_t, uint32_t)> updatePSPButtons,26std::function<void(int, float, float)> setPSPAnalog,27std::function<void(int, float, float)> setRawAnalog);2829// Inject raw PSP key input directly, such as from touch screen controls.30// Combined with the mapped input. Unlike __Ctrl APIs, this supports31// virtual key codes, including analog mappings.32void PSPKey(int deviceId, int pspKeyCode, int flags);3334// Toggle swapping DPAD and Analog. Useful on some input devices with few buttons.35void ToggleSwapAxes();3637// Call this when a Vkey press triggers leaving the screen you're using the controlmapper on. This can cause38// the loss of key-up events, which will confuse things later when you're back.39// Might replace this later by allowing through "key-up" and similar events to lower screens.40void ForceReleaseVKey(int vkey);4142// Call when the emu screen gets pushed behind some other screen, like the pause screen, to release all "down" inputs.43void ReleaseAll();4445void GetDebugString(char *buffer, size_t bufSize) const;4647struct InputSample {48float value;49double timestamp;50};5152private:53bool UpdatePSPState(const InputMapping &changedMapping, double now);54float MapAxisValue(float value, int vkId, const InputMapping &mapping, const InputMapping &changedMapping, bool *oppositeTouched);55void SwapMappingIfEnabled(uint32_t *vkey);5657void SetPSPAxis(int deviceId, int stick, char axis, float value);58void UpdateAnalogOutput(int stick);5960void onVKey(int vkey, bool down);61void onVKeyAnalog(int deviceId, int vkey, float value);6263void UpdateCurInputAxis(const InputMapping &mapping, float value, double timestamp);64float GetDeviceAxisThreshold(int device, const InputMapping &mapping);6566// To track mappable virtual keys. We can have as many as we want.67float virtKeys_[VIRTKEY_COUNT]{};68bool virtKeyOn_[VIRTKEY_COUNT]{}; // Track boolean output separaately since thresholds may differ.6970// This is only used for co-axis (analog stick to buttons), so not bothering to track separately71// per device.72float rawAxisValue_[JOYSTICK_AXIS_MAX]{};7374double deviceTimestamps_[(size_t)DEVICE_ID_COUNT]{};7576int lastNonDeadzoneDeviceID_[2]{};7778float history_[2][2]{};79float converted_[2][2]{}; // for debug display8081// Mappable auto-rotation. Useful for keyboard/dpad->analog in a few games.82bool autoRotatingAnalogCW_ = false;83bool autoRotatingAnalogCCW_ = false;8485bool swapAxes_ = false;8687// Protects basically all the state.88// TODO: Maybe we should piggyback on the screenmanager mutex - it's always locked89// when events come in here.90std::mutex mutex_;9192std::map<InputMapping, InputSample> curInput_;9394// Callbacks95std::function<void(int, bool)> onVKey_;96std::function<void(int, float)> onVKeyAnalog_;97std::function<void(uint32_t, uint32_t)> updatePSPButtons_;98std::function<void(int, float, float)> setPSPAnalog_;99std::function<void(int, float, float)> setRawAnalog_;100};101102void ConvertAnalogStick(float x, float y, float *outX, float *outY);103float GetDeviceAxisThreshold(int device, const InputMapping &mapping);104105106