2019-02-12 10:30:24 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 flyinghead
|
|
|
|
|
|
|
|
This file is part of reicast.
|
|
|
|
|
|
|
|
reicast is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
reicast is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with reicast. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "types.h"
|
|
|
|
#include "mapping.h"
|
|
|
|
|
2020-03-28 16:58:01 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-02-12 10:30:24 +00:00
|
|
|
class GamepadDevice
|
|
|
|
{
|
|
|
|
public:
|
2021-01-21 20:00:46 +00:00
|
|
|
typedef void (*input_detected_cb)(u32 code);
|
|
|
|
|
2019-02-12 14:56:44 +00:00
|
|
|
const std::string& api_name() { return _api_name; }
|
|
|
|
const std::string& name() { return _name; }
|
2021-05-31 17:44:55 +00:00
|
|
|
int maple_port() const { return _maple_port; }
|
2021-08-23 12:02:12 +00:00
|
|
|
virtual void set_maple_port(int port) { _maple_port = port; }
|
2019-03-29 16:19:18 +00:00
|
|
|
const std::string& unique_id() { return _unique_id; }
|
2019-02-13 19:29:49 +00:00
|
|
|
virtual bool gamepad_btn_input(u32 code, bool pressed);
|
|
|
|
bool gamepad_axis_input(u32 code, int value);
|
2021-03-14 17:52:04 +00:00
|
|
|
virtual ~GamepadDevice() = default;
|
2019-02-21 13:49:27 +00:00
|
|
|
|
2021-01-21 20:00:46 +00:00
|
|
|
virtual void detect_btn_input(input_detected_cb button_pressed);
|
2019-03-28 17:28:29 +00:00
|
|
|
void detect_axis_input(input_detected_cb axis_moved);
|
2021-01-21 20:00:46 +00:00
|
|
|
virtual void cancel_detect_input()
|
2019-02-12 10:30:24 +00:00
|
|
|
{
|
2020-04-17 15:55:43 +00:00
|
|
|
_input_detected = nullptr;
|
2019-02-12 10:30:24 +00:00
|
|
|
}
|
2020-03-12 15:09:05 +00:00
|
|
|
std::shared_ptr<InputMapping> get_input_mapping() { return input_mapper; }
|
2019-02-12 10:30:24 +00:00
|
|
|
void save_mapping();
|
2021-06-27 10:49:47 +00:00
|
|
|
void save_mapping(int system);
|
|
|
|
|
|
|
|
void verify_or_create_system_mappings();
|
|
|
|
|
2020-11-20 21:10:14 +00:00
|
|
|
virtual const char *get_button_name(u32 code) { return nullptr; }
|
|
|
|
virtual const char *get_axis_name(u32 code) { return nullptr; }
|
2020-03-12 15:09:05 +00:00
|
|
|
bool remappable() { return _remappable && input_mapper; }
|
2019-03-04 23:54:01 +00:00
|
|
|
virtual bool is_virtual_gamepad() { return false; }
|
2019-02-12 10:30:24 +00:00
|
|
|
|
2019-02-22 18:23:03 +00:00
|
|
|
virtual void rumble(float power, float inclination, u32 duration_ms) {}
|
|
|
|
virtual void update_rumble() {}
|
2021-05-31 17:44:55 +00:00
|
|
|
bool is_rumble_enabled() const { return _rumble_enabled; }
|
2019-02-22 18:23:03 +00:00
|
|
|
|
2021-03-13 13:13:39 +00:00
|
|
|
static void Register(const std::shared_ptr<GamepadDevice>& gamepad);
|
2019-02-21 13:49:27 +00:00
|
|
|
|
2021-03-13 13:13:39 +00:00
|
|
|
static void Unregister(const std::shared_ptr<GamepadDevice>& gamepad);
|
2019-02-21 13:49:27 +00:00
|
|
|
|
2019-02-12 10:30:24 +00:00
|
|
|
static int GetGamepadCount();
|
2019-02-21 13:49:27 +00:00
|
|
|
static std::shared_ptr<GamepadDevice> GetGamepad(int index);
|
2019-03-29 16:19:18 +00:00
|
|
|
static void SaveMaplePorts();
|
2019-02-12 10:30:24 +00:00
|
|
|
|
2021-06-27 10:49:47 +00:00
|
|
|
static void load_system_mappings(int system = settings.platform.system);
|
|
|
|
bool find_mapping(int system);
|
2019-02-12 10:30:24 +00:00
|
|
|
protected:
|
2019-02-13 19:29:49 +00:00
|
|
|
GamepadDevice(int maple_port, const char *api_name, bool remappable = true)
|
2021-09-07 08:54:00 +00:00
|
|
|
: _api_name(api_name), _maple_port(maple_port), _input_detected(nullptr), _remappable(remappable),
|
|
|
|
digitalToAnalogState{}
|
2019-02-13 19:29:49 +00:00
|
|
|
{
|
2019-02-12 10:30:24 +00:00
|
|
|
}
|
2021-05-19 16:13:52 +00:00
|
|
|
|
2020-04-17 15:55:43 +00:00
|
|
|
bool find_mapping(const char *custom_mapping = nullptr);
|
2021-05-19 16:13:52 +00:00
|
|
|
void loadMapping() {
|
|
|
|
if (!find_mapping())
|
|
|
|
input_mapper = getDefaultMapping();
|
|
|
|
}
|
|
|
|
virtual std::shared_ptr<InputMapping> getDefaultMapping() {
|
|
|
|
return std::make_shared<IdentityInputMapping>();
|
|
|
|
}
|
2019-02-12 10:30:24 +00:00
|
|
|
|
|
|
|
virtual void load_axis_min_max(u32 axis) {}
|
2020-11-20 21:10:14 +00:00
|
|
|
bool is_detecting_input() { return _input_detected != nullptr; }
|
2019-02-12 10:30:24 +00:00
|
|
|
|
2019-02-12 14:56:44 +00:00
|
|
|
std::string _name;
|
2021-03-14 18:36:25 +00:00
|
|
|
std::string _unique_id;
|
2020-03-12 15:09:05 +00:00
|
|
|
std::shared_ptr<InputMapping> input_mapper;
|
2019-02-12 10:30:24 +00:00
|
|
|
std::map<u32, int> axis_min_values;
|
|
|
|
std::map<u32, unsigned int> axis_ranges;
|
2019-02-22 23:17:59 +00:00
|
|
|
bool _rumble_enabled = true;
|
2019-02-12 10:30:24 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
int get_axis_min_value(u32 axis);
|
|
|
|
unsigned int get_axis_range(u32 axis);
|
2020-04-17 15:55:43 +00:00
|
|
|
std::string make_mapping_filename(bool instance = false);
|
2021-06-27 10:49:47 +00:00
|
|
|
std::string make_mapping_filename(bool instance, int system);
|
2019-02-12 10:30:24 +00:00
|
|
|
|
2019-02-12 14:56:44 +00:00
|
|
|
std::string _api_name;
|
2019-02-12 10:30:24 +00:00
|
|
|
int _maple_port;
|
|
|
|
bool _detecting_button = false;
|
2020-03-12 15:09:05 +00:00
|
|
|
double _detection_start_time = 0.0;
|
2019-02-12 10:30:24 +00:00
|
|
|
input_detected_cb _input_detected;
|
2019-02-13 19:29:49 +00:00
|
|
|
bool _remappable;
|
2021-09-07 08:54:00 +00:00
|
|
|
u32 digitalToAnalogState[4];
|
2019-02-12 10:30:24 +00:00
|
|
|
|
2019-02-21 13:49:27 +00:00
|
|
|
static std::vector<std::shared_ptr<GamepadDevice>> _gamepads;
|
2019-02-12 10:30:24 +00:00
|
|
|
static std::mutex _gamepads_mutex;
|
|
|
|
};
|
2019-06-23 10:17:24 +00:00
|
|
|
|
|
|
|
#ifdef TEST_AUTOMATION
|
|
|
|
void replay_input();
|
|
|
|
#endif
|
|
|
|
|
2020-12-02 13:40:50 +00:00
|
|
|
extern u32 kcode[4];
|
2020-03-20 15:57:50 +00:00
|
|
|
extern u8 rt[4], lt[4];
|
|
|
|
extern s8 joyx[4], joyy[4];
|
|
|
|
extern s8 joyrx[4], joyry[4];
|
2020-12-02 13:40:50 +00:00
|
|
|
|
|
|
|
void UpdateVibration(u32 port, float power, float inclination, u32 duration_ms);
|
2021-05-19 16:13:52 +00:00
|
|
|
|
|
|
|
class MouseInputMapping : public InputMapping
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MouseInputMapping()
|
|
|
|
{
|
|
|
|
name = "Mouse";
|
|
|
|
set_button(DC_BTN_A, 2); // Left
|
|
|
|
set_button(DC_BTN_B, 1); // Right
|
|
|
|
set_button(DC_BTN_START, 3); // Middle
|
|
|
|
|
|
|
|
dirty = false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Mouse : public GamepadDevice
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
Mouse(const char *apiName, int maplePort = 0) : GamepadDevice(maplePort, apiName) {
|
|
|
|
this->_name = "Mouse";
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::shared_ptr<InputMapping> getDefaultMapping() override {
|
|
|
|
return std::make_shared<MouseInputMapping>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum Button {
|
|
|
|
LEFT_BUTTON = 2,
|
|
|
|
RIGHT_BUTTON = 1,
|
|
|
|
MIDDLE_BUTTON = 3,
|
|
|
|
BUTTON_4 = 4,
|
|
|
|
BUTTON_5 = 5
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual const char *get_button_name(u32 code) override
|
|
|
|
{
|
|
|
|
switch((Button)code)
|
|
|
|
{
|
|
|
|
case LEFT_BUTTON:
|
|
|
|
return "Left Button";
|
|
|
|
case RIGHT_BUTTON:
|
|
|
|
return "Right Button";
|
|
|
|
case MIDDLE_BUTTON:
|
|
|
|
return "Middle Button";
|
|
|
|
case BUTTON_4:
|
|
|
|
return "Button 4";
|
|
|
|
case BUTTON_5:
|
|
|
|
return "Button 5";
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAbsPos(int x, int y, int width, int height);
|
|
|
|
void setRelPos(int deltax, int deltay);
|
|
|
|
void setButton(Button button, bool pressed);
|
|
|
|
void setWheel(int delta);
|
|
|
|
};
|
|
|
|
|
|
|
|
class SystemMouse : public Mouse
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
SystemMouse(const char *apiName, int maplePort = 0) : Mouse(apiName, maplePort) {}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void setAbsPos(int x, int y, int width, int height);
|
|
|
|
void setButton(Button button, bool pressed);
|
|
|
|
void setWheel(int delta);
|
|
|
|
};
|