2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-23 18:07:00 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-23 18:07:00 +00:00
|
|
|
// InputConfig.h
|
|
|
|
//
|
|
|
|
// Input device configuration functions.
|
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_INPUT_CONFIG_H
|
|
|
|
#define ES_CORE_INPUT_CONFIG_H
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2020-08-23 14:15:06 +00:00
|
|
|
#include <SDL2/SDL_joystick.h>
|
|
|
|
#include <SDL2/SDL_keyboard.h>
|
2017-11-19 23:06:04 +00:00
|
|
|
#include <CECInput.h>
|
2013-04-08 14:27:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <vector>
|
2013-04-08 14:27:38 +00:00
|
|
|
|
|
|
|
#define DEVICE_KEYBOARD -1
|
2017-11-08 22:22:15 +00:00
|
|
|
#define DEVICE_CEC -2
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
enum InputType {
|
|
|
|
TYPE_AXIS,
|
|
|
|
TYPE_BUTTON,
|
|
|
|
TYPE_KEY,
|
|
|
|
TYPE_CEC_BUTTON,
|
|
|
|
TYPE_COUNT
|
2013-04-08 14:27:38 +00:00
|
|
|
};
|
|
|
|
|
2020-06-26 16:03:55 +00:00
|
|
|
namespace pugi
|
|
|
|
{
|
|
|
|
class xml_node;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
struct Input
|
|
|
|
{
|
|
|
|
public:
|
2020-06-23 18:07:00 +00:00
|
|
|
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 getCECButtonName(int keycode)
|
|
|
|
{
|
|
|
|
return CECInput::getKeyCodeString(keycode);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string string()
|
|
|
|
{
|
|
|
|
std::stringstream stream;
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_AXIS:
|
|
|
|
stream << "Axis " << id << (value > 0 ? "+" : "-");
|
|
|
|
break;
|
2021-05-22 20:18:00 +00:00
|
|
|
case TYPE_BUTTON:
|
|
|
|
stream << "Button " << id;
|
2020-06-23 18:07:00 +00:00
|
|
|
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();
|
|
|
|
}
|
2013-04-08 14:27:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class InputConfig
|
|
|
|
{
|
|
|
|
public:
|
2020-06-23 18:07:00 +00:00
|
|
|
InputConfig(int deviceId, const std::string& deviceName, const std::string& deviceGUID);
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
// Utility functions.
|
|
|
|
std::string inputTypeToString(InputType type);
|
|
|
|
InputType stringToInputType(const std::string& type);
|
|
|
|
std::string toLower(std::string str);
|
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
void clear();
|
2021-05-22 20:18:00 +00:00
|
|
|
bool isConfigured();
|
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
void mapInput(const std::string& name, Input input);
|
|
|
|
void unmapInput(const std::string& name); // Unmap all Inputs mapped to this name.
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
// 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);
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
// Returns a list of names this input is mapped to.
|
|
|
|
std::vector<std::string> getMappedTo(Input input);
|
2013-04-08 14:27:38 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
// 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);
|
2020-07-24 16:24:04 +00:00
|
|
|
int getInputIDByName(const std::string& name);
|
2016-12-02 23:00:10 +00:00
|
|
|
|
2020-06-23 18:07:00 +00:00
|
|
|
void loadFromXML(pugi::xml_node& root);
|
|
|
|
void writeToXML(pugi::xml_node& parent);
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2021-05-22 20:18:00 +00:00
|
|
|
inline int getDeviceId() const { return mDeviceId; };
|
|
|
|
inline const std::string& getDeviceName() { return mDeviceName; }
|
|
|
|
inline const std::string& getDeviceGUIDString() { return mDeviceGUID; }
|
2014-03-22 01:12:57 +00:00
|
|
|
|
2013-04-08 14:27:38 +00:00
|
|
|
private:
|
2020-06-23 18:07:00 +00:00
|
|
|
std::map<std::string, Input> mNameMap;
|
|
|
|
const int mDeviceId;
|
|
|
|
const std::string mDeviceName;
|
|
|
|
const std::string mDeviceGUID;
|
2013-04-08 14:27:38 +00:00
|
|
|
};
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#endif // ES_CORE_INPUT_CONFIG_H
|