mirror of
https://github.com/RetroDECK/ES-DE.git
synced 2024-11-27 00:25:38 +00:00
dc4870f543
There are still some issues on Windows, especially with Dual Shock controllers, but a larger rewrite is required anyway of the input handling so it's not worthwhile trying to fix it at this time.
161 lines
3.8 KiB
C++
161 lines
3.8 KiB
C++
//
|
|
// InputConfig.h
|
|
//
|
|
// Input device configuration functions.
|
|
//
|
|
|
|
#pragma once
|
|
#ifndef ES_CORE_INPUT_CONFIG_H
|
|
#define ES_CORE_INPUT_CONFIG_H
|
|
|
|
#include <CECInput.h>
|
|
#include <map>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
#if defined(__linux__) || defined(_WIN64)
|
|
#include <SDL2/SDL_joystick.h>
|
|
#include <SDL2/SDL_keyboard.h>
|
|
#else
|
|
#include "SDL_joystick.h"
|
|
#include "SDL_keyboard.h"
|
|
#endif
|
|
|
|
#define DEVICE_KEYBOARD -1
|
|
#define DEVICE_CEC -2
|
|
|
|
enum InputType {
|
|
TYPE_AXIS,
|
|
TYPE_BUTTON,
|
|
TYPE_HAT,
|
|
TYPE_KEY,
|
|
TYPE_CEC_BUTTON,
|
|
TYPE_COUNT
|
|
};
|
|
|
|
namespace pugi
|
|
{
|
|
class xml_node;
|
|
}
|
|
|
|
struct Input
|
|
{
|
|
public:
|
|
int device;
|
|
InputType type;
|
|
int id;
|
|
int value;
|
|
bool configured;
|
|
|
|
Input()
|
|
{
|
|
device = DEVICE_KEYBOARD;
|
|
configured = false;
|
|
id = -1;
|
|
value = -999;
|
|
type = TYPE_COUNT;
|
|
}
|
|
|
|
Input(
|
|
int dev,
|
|
InputType t,
|
|
int i,
|
|
int val,
|
|
bool conf)
|
|
: device(dev),
|
|
type(t),id(i),
|
|
value(val),
|
|
configured(conf)
|
|
{
|
|
}
|
|
|
|
std::string getHatDir(int val)
|
|
{
|
|
if (val & SDL_HAT_UP)
|
|
return "up";
|
|
else if (val & SDL_HAT_DOWN)
|
|
return "down";
|
|
else if (val & SDL_HAT_LEFT)
|
|
return "left";
|
|
else if (val & SDL_HAT_RIGHT)
|
|
return "right";
|
|
return "neutral?";
|
|
}
|
|
|
|
std::string getCECButtonName(int keycode)
|
|
{
|
|
return CECInput::getKeyCodeString(keycode);
|
|
}
|
|
|
|
std::string string()
|
|
{
|
|
std::stringstream stream;
|
|
switch (type) {
|
|
case TYPE_BUTTON:
|
|
stream << "Button " << id;
|
|
break;
|
|
case TYPE_AXIS:
|
|
stream << "Axis " << id << (value > 0 ? "+" : "-");
|
|
break;
|
|
case TYPE_HAT:
|
|
stream << "Hat " << id << " " << getHatDir(value);
|
|
break;
|
|
case TYPE_KEY:
|
|
stream << "Key " << SDL_GetKeyName((SDL_Keycode)id);
|
|
break;
|
|
case TYPE_CEC_BUTTON:
|
|
stream << "CEC-Button " << getCECButtonName(id);
|
|
break;
|
|
default:
|
|
stream << "Input to string error";
|
|
break;
|
|
}
|
|
|
|
return stream.str();
|
|
}
|
|
};
|
|
|
|
class InputConfig
|
|
{
|
|
public:
|
|
InputConfig(int deviceId, const std::string& deviceName, const std::string& deviceGUID);
|
|
|
|
void clear();
|
|
void mapInput(const std::string& name, Input input);
|
|
void unmapInput(const std::string& name); // Unmap all Inputs mapped to this name.
|
|
|
|
inline int getDeviceId() const { return mDeviceId; };
|
|
inline const std::string& getDeviceName() { return mDeviceName; }
|
|
inline const std::string& getDeviceGUIDString() { return mDeviceGUID; }
|
|
|
|
void setDefaultConfigFlag() { mDefaultConfigFlag = true; };
|
|
void unsetDefaultConfigFlag() { mDefaultConfigFlag = false; };
|
|
bool getDefaultConfigFlag() { return mDefaultConfigFlag; };
|
|
|
|
// Returns true if Input is mapped to this name, false otherwise.
|
|
bool isMappedTo(const std::string& name, Input input);
|
|
bool isMappedLike(const std::string& name, Input input);
|
|
|
|
// Returns a list of names this input is mapped to.
|
|
std::vector<std::string> getMappedTo(Input input);
|
|
|
|
// Returns true if there is an Input mapped to this name, false otherwise.
|
|
// Writes Input mapped to this name to result if true.
|
|
bool getInputByName(const std::string& name, Input* result);
|
|
int getInputIDByName(const std::string& name);
|
|
|
|
void loadFromXML(pugi::xml_node& root);
|
|
void writeToXML(pugi::xml_node& parent);
|
|
|
|
bool isConfigured();
|
|
|
|
private:
|
|
std::map<std::string, Input> mNameMap;
|
|
const int mDeviceId;
|
|
const std::string mDeviceName;
|
|
const std::string mDeviceGUID;
|
|
bool mDefaultConfigFlag;
|
|
};
|
|
|
|
#endif // ES_CORE_INPUT_CONFIG_H
|