Merge pull request #2497 from wwylele/input-2

Refactor input emulation & add SDL gamepad support
This commit is contained in:
bunnei 2017-03-17 14:59:39 -04:00 • committed by GitHub
commit 0180121d53
40 changed files with 1244 additions and 574 deletions

View file

@ -5,6 +5,7 @@ add_subdirectory(common)
add_subdirectory(core)
add_subdirectory(video_core)
add_subdirectory(audio_core)
add_subdirectory(input_common)
add_subdirectory(tests)
if (ENABLE_SDL2)
add_subdirectory(citra)

View file

@ -18,7 +18,7 @@ create_directory_groups(${SRCS} ${HEADERS})
include_directories(${SDL2_INCLUDE_DIR})
add_executable(citra ${SRCS} ${HEADERS})
target_link_libraries(citra core video_core audio_core common)
target_link_libraries(citra core video_core audio_core common input_common)
target_link_libraries(citra ${SDL2_LIBRARY} ${OPENGL_gl_LIBRARY} inih glad)
if (MSVC)
target_link_libraries(citra getopt)

View file

@ -8,8 +8,10 @@
#include "citra/default_ini.h"
#include "common/file_util.h"
#include "common/logging/log.h"
#include "common/param_package.h"
#include "config.h"
#include "core/settings.h"
#include "input_common/main.h"
Config::Config() {
// TODO: Don't hardcode the path; let the frontend decide where to put the config files.
@ -37,25 +39,40 @@ bool Config::LoadINI(const std::string& default_contents, bool retry) {
return true;
}
static const std::array<int, Settings::NativeInput::NUM_INPUTS> defaults = {
// directly mapped keys
SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_Q, SDL_SCANCODE_W,
SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_M, SDL_SCANCODE_N, SDL_SCANCODE_B, SDL_SCANCODE_T,
SDL_SCANCODE_G, SDL_SCANCODE_F, SDL_SCANCODE_H, SDL_SCANCODE_I, SDL_SCANCODE_K, SDL_SCANCODE_J,
SDL_SCANCODE_L,
// indirectly mapped keys
SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_D,
static const std::array<int, Settings::NativeButton::NumButtons> default_buttons = {
SDL_SCANCODE_A, SDL_SCANCODE_S, SDL_SCANCODE_Z, SDL_SCANCODE_X, SDL_SCANCODE_T,
SDL_SCANCODE_G, SDL_SCANCODE_F, SDL_SCANCODE_H, SDL_SCANCODE_Q, SDL_SCANCODE_W,
SDL_SCANCODE_M, SDL_SCANCODE_N, SDL_SCANCODE_1, SDL_SCANCODE_2, SDL_SCANCODE_B,
};
static const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> default_analogs{{
{
SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_D,
},
{
SDL_SCANCODE_I, SDL_SCANCODE_K, SDL_SCANCODE_J, SDL_SCANCODE_L, SDL_SCANCODE_D,
},
}};
void Config::ReadValues() {
// Controls
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
Settings::values.input_mappings[Settings::NativeInput::All[i]] =
sdl2_config->GetInteger("Controls", Settings::NativeInput::Mapping[i], defaults[i]);
for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
Settings::values.buttons[i] =
sdl2_config->Get("Controls", Settings::NativeButton::mapping[i], default_param);
if (Settings::values.buttons[i].empty())
Settings::values.buttons[i] = default_param;
}
for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
default_analogs[i][3], default_analogs[i][4], 0.5f);
Settings::values.analogs[i] =
sdl2_config->Get("Controls", Settings::NativeAnalog::mapping[i], default_param);
if (Settings::values.analogs[i].empty())
Settings::values.analogs[i] = default_param;
}
Settings::values.pad_circle_modifier_scale =
(float)sdl2_config->GetReal("Controls", "pad_circle_modifier_scale", 0.5);
// Core
Settings::values.use_cpu_jit = sdl2_config->GetBoolean("Core", "use_cpu_jit", true);

View file

@ -8,34 +8,47 @@ namespace DefaultINI {
const char* sdl2_config_file = R"(
[Controls]
pad_start =
pad_select =
pad_home =
pad_dup =
pad_ddown =
pad_dleft =
pad_dright =
pad_a =
pad_b =
pad_x =
pad_y =
pad_l =
pad_r =
pad_zl =
pad_zr =
pad_cup =
pad_cdown =
pad_cleft =
pad_cright =
pad_circle_up =
pad_circle_down =
pad_circle_left =
pad_circle_right =
pad_circle_modifier =
# The input devices and parameters for each 3DS native input
# It should be in the format of "engine:[engine_name],[param1]:[value1],[param2]:[value2]..."
# Escape characters $0 (for ':'), $1 (for ',') and $2 (for '$') can be used in values
# The applied modifier scale to circle pad.
# Must be in range of 0.0-1.0. Defaults to 0.5
pad_circle_modifier_scale =
# for button input, the following devices are avaible:
# - "keyboard" (default) for keyboard input. Required parameters:
# - "code": the code of the key to bind
# - "sdl" for joystick input using SDL. Required parameters:
# - "joystick": the index of the joystick to bind
# - "button"(optional): the index of the button to bind
# - "hat"(optional): the index of the hat to bind as direction buttons
# - "direction"(only used for hat): the direction name of the hat to bind. Can be "up", "down", "left" or "right"
button_a=
button_b=
button_x=
button_y=
button_up=
button_down=
button_left=
button_right=
button_l=
button_r=
button_start=
button_select=
button_zl=
button_zr=
button_home=
# for analog input, the following devices are avaible:
# - "analog_from_button" (default) for emulating analog input from direction buttons. Required parameters:
# - "up", "down", "left", "right": sub-devices for each direction.
# Should be in the format as a button input devices using escape characters, for example, "engine$0keyboard$1code$00"
# - "modifier": sub-devices as a modifier.
# - "modifier_scale": a float number representing the applied modifier scale to the analog input.
# Must be in range of 0.0-1.0. Defaults to 0.5
# - "sdl" for joystick input using SDL. Required parameters:
# - "joystick": the index of the joystick to bind
# - "axis_x": the index of the axis to bind as x-axis (default to 0)
# - "axis_y": the index of the axis to bind as y-axis (default to 1)
circle_pad=
c_stick=
[Core]
# Whether to use the Just-In-Time (JIT) compiler for CPU emulation

View file

@ -12,9 +12,9 @@
#include "common/logging/log.h"
#include "common/scm_rev.h"
#include "common/string_util.h"
#include "core/frontend/key_map.h"
#include "core/hle/service/hid/hid.h"
#include "core/settings.h"
#include "input_common/keyboard.h"
#include "input_common/main.h"
#include "video_core/video_core.h"
void EmuWindow_SDL2::OnMouseMotion(s32 x, s32 y) {
@ -40,9 +40,9 @@ void EmuWindow_SDL2::OnMouseButton(u32 button, u8 state, s32 x, s32 y) {
void EmuWindow_SDL2::OnKeyEvent(int key, u8 state) {
if (state == SDL_PRESSED) {
KeyMap::PressKey(*this, {key, keyboard_id});
InputCommon::GetKeyboard()->PressKey(key);
} else if (state == SDL_RELEASED) {
KeyMap::ReleaseKey(*this, {key, keyboard_id});
InputCommon::GetKeyboard()->ReleaseKey(key);
}
}
@ -57,9 +57,8 @@ void EmuWindow_SDL2::OnResize() {
}
EmuWindow_SDL2::EmuWindow_SDL2() {
keyboard_id = KeyMap::NewDeviceId();
InputCommon::Init();
ReloadSetKeymaps();
motion_emu = std::make_unique<Motion::MotionEmu>(*this);
SDL_SetMainReady();
@ -117,6 +116,7 @@ EmuWindow_SDL2::~EmuWindow_SDL2() {
SDL_GL_DeleteContext(gl_context);
SDL_Quit();
motion_emu = nullptr;
InputCommon::Shutdown();
}
void EmuWindow_SDL2::SwapBuffers() {
@ -169,15 +169,6 @@ void EmuWindow_SDL2::DoneCurrent() {
SDL_GL_MakeCurrent(render_window, nullptr);
}
void EmuWindow_SDL2::ReloadSetKeymaps() {
KeyMap::ClearKeyMapping(keyboard_id);
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
KeyMap::SetKeyMapping(
{Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id},
KeyMap::mapping_targets[i]);
}
}
void EmuWindow_SDL2::OnMinimalClientAreaChangeRequest(
const std::pair<unsigned, unsigned>& minimal_size) {

View file

@ -31,9 +31,6 @@ public:
/// Whether the window is still open, and a close request hasn't yet been sent
bool IsOpen() const;
/// Load keymap from configuration
void ReloadSetKeymaps() override;
private:
/// Called by PollEvents when a key is pressed or released.
void OnKeyEvent(int key, u8 state);
@ -61,9 +58,6 @@ private:
/// The OpenGL context associated with the window
SDL_GLContext gl_context;
/// Device id of keyboard for use with KeyMap
int keyboard_id;
/// Motion sensors emulation
std::unique_ptr<Motion::MotionEmu> motion_emu;
};

View file

@ -97,7 +97,7 @@ if (APPLE)
else()
add_executable(citra-qt ${SRCS} ${HEADERS} ${UI_HDRS})
endif()
target_link_libraries(citra-qt core video_core audio_core common)
target_link_libraries(citra-qt core video_core audio_core common input_common)
target_link_libraries(citra-qt ${OPENGL_gl_LIBRARY} ${CITRA_QT_LIBS})
target_link_libraries(citra-qt ${PLATFORM_LIBRARIES} Threads::Threads)

View file

@ -13,7 +13,9 @@
#include "common/scm_rev.h"
#include "common/string_util.h"
#include "core/core.h"
#include "core/frontend/key_map.h"
#include "core/settings.h"
#include "input_common/keyboard.h"
#include "input_common/main.h"
#include "video_core/debug_utils/debug_utils.h"
#include "video_core/video_core.h"
@ -99,14 +101,17 @@ private:
};
GRenderWindow::GRenderWindow(QWidget* parent, EmuThread* emu_thread)
: QWidget(parent), child(nullptr), keyboard_id(0), emu_thread(emu_thread) {
: QWidget(parent), child(nullptr), emu_thread(emu_thread) {
std::string window_title = Common::StringFromFormat("Citra %s| %s-%s", Common::g_build_name,
Common::g_scm_branch, Common::g_scm_desc);
setWindowTitle(QString::fromStdString(window_title));
keyboard_id = KeyMap::NewDeviceId();
ReloadSetKeymaps();
InputCommon::Init();
}
GRenderWindow::~GRenderWindow() {
InputCommon::Shutdown();
}
void GRenderWindow::moveContext() {
@ -197,11 +202,11 @@ void GRenderWindow::closeEvent(QCloseEvent* event) {
}
void GRenderWindow::keyPressEvent(QKeyEvent* event) {
KeyMap::PressKey(*this, {event->key(), keyboard_id});
InputCommon::GetKeyboard()->PressKey(event->key());
}
void GRenderWindow::keyReleaseEvent(QKeyEvent* event) {
KeyMap::ReleaseKey(*this, {event->key(), keyboard_id});
InputCommon::GetKeyboard()->ReleaseKey(event->key());
}
void GRenderWindow::mousePressEvent(QMouseEvent* event) {
@ -230,14 +235,7 @@ void GRenderWindow::mouseReleaseEvent(QMouseEvent* event) {
motion_emu->EndTilt();
}
void GRenderWindow::ReloadSetKeymaps() {
KeyMap::ClearKeyMapping(keyboard_id);
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
KeyMap::SetKeyMapping(
{Settings::values.input_mappings[Settings::NativeInput::All[i]], keyboard_id},
KeyMap::mapping_targets[i]);
}
}
void GRenderWindow::ReloadSetKeymaps() {}
void GRenderWindow::OnClientAreaResized(unsigned width, unsigned height) {
NotifyClientAreaSizeChanged(std::make_pair(width, height));

View file

@ -104,6 +104,7 @@ class GRenderWindow : public QWidget, public EmuWindow {
public:
GRenderWindow(QWidget* parent, EmuThread* emu_thread);
~GRenderWindow();
// EmuWindow implementation
void SwapBuffers() override;
@ -127,7 +128,7 @@ public:
void mouseMoveEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
void ReloadSetKeymaps() override;
void ReloadSetKeymaps();
void OnClientAreaResized(unsigned width, unsigned height);
@ -152,9 +153,6 @@ private:
QByteArray geometry;
/// Device id of keyboard for use with KeyMap
int keyboard_id;
EmuThread* emu_thread;
/// Motion sensors emulation

View file

@ -6,6 +6,7 @@
#include "citra_qt/config.h"
#include "citra_qt/ui_settings.h"
#include "common/file_util.h"
#include "input_common/main.h"
Config::Config() {
// TODO: Don't hardcode the path; let the frontend decide where to put the config files.
@ -16,25 +17,46 @@ Config::Config() {
Reload();
}
const std::array<QVariant, Settings::NativeInput::NUM_INPUTS> Config::defaults = {
// directly mapped keys
Qt::Key_A, Qt::Key_S, Qt::Key_Z, Qt::Key_X, Qt::Key_Q, Qt::Key_W, Qt::Key_1, Qt::Key_2,
Qt::Key_M, Qt::Key_N, Qt::Key_B, Qt::Key_T, Qt::Key_G, Qt::Key_F, Qt::Key_H, Qt::Key_I,
Qt::Key_K, Qt::Key_J, Qt::Key_L,
// indirectly mapped keys
Qt::Key_Up, Qt::Key_Down, Qt::Key_Left, Qt::Key_Right, Qt::Key_D,
const std::array<int, Settings::NativeButton::NumButtons> Config::default_buttons = {
Qt::Key_A, Qt::Key_S, Qt::Key_Z, Qt::Key_X, Qt::Key_T, Qt::Key_G, Qt::Key_F, Qt::Key_H,
Qt::Key_Q, Qt::Key_W, Qt::Key_M, Qt::Key_N, Qt::Key_1, Qt::Key_2, Qt::Key_B,
};
const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> Config::default_analogs{{
{
Qt::Key_Up, Qt::Key_Down, Qt::Key_Left, Qt::Key_Right, Qt::Key_D,
},
{
Qt::Key_I, Qt::Key_K, Qt::Key_J, Qt::Key_L, Qt::Key_D,
},
}};
void Config::ReadValues() {
qt_config->beginGroup("Controls");
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
Settings::values.input_mappings[Settings::NativeInput::All[i]] =
qt_config->value(QString::fromStdString(Settings::NativeInput::Mapping[i]), defaults[i])
.toInt();
for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
std::string default_param = InputCommon::GenerateKeyboardParam(default_buttons[i]);
Settings::values.buttons[i] =
qt_config
->value(Settings::NativeButton::mapping[i], QString::fromStdString(default_param))
.toString()
.toStdString();
if (Settings::values.buttons[i].empty())
Settings::values.buttons[i] = default_param;
}
Settings::values.pad_circle_modifier_scale =
qt_config->value("pad_circle_modifier_scale", 0.5).toFloat();
for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
std::string default_param = InputCommon::GenerateAnalogParamFromKeys(
default_analogs[i][0], default_analogs[i][1], default_analogs[i][2],
default_analogs[i][3], default_analogs[i][4], 0.5f);
Settings::values.analogs[i] =
qt_config
->value(Settings::NativeAnalog::mapping[i], QString::fromStdString(default_param))
.toString()
.toStdString();
if (Settings::values.analogs[i].empty())
Settings::values.analogs[i] = default_param;
}
qt_config->endGroup();
qt_config->beginGroup("Core");
@ -155,12 +177,14 @@ void Config::ReadValues() {
void Config::SaveValues() {
qt_config->beginGroup("Controls");
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
qt_config->setValue(QString::fromStdString(Settings::NativeInput::Mapping[i]),
Settings::values.input_mappings[Settings::NativeInput::All[i]]);
for (int i = 0; i < Settings::NativeButton::NumButtons; ++i) {
qt_config->setValue(QString::fromStdString(Settings::NativeButton::mapping[i]),
QString::fromStdString(Settings::values.buttons[i]));
}
for (int i = 0; i < Settings::NativeAnalog::NumAnalogs; ++i) {
qt_config->setValue(QString::fromStdString(Settings::NativeAnalog::mapping[i]),
QString::fromStdString(Settings::values.analogs[i]));
}
qt_config->setValue("pad_circle_modifier_scale",
(double)Settings::values.pad_circle_modifier_scale);
qt_config->endGroup();
qt_config->beginGroup("Core");

View file

@ -4,6 +4,7 @@
#pragma once
#include <array>
#include <string>
#include <QVariant>
#include "core/settings.h"
@ -23,5 +24,7 @@ public:
void Reload();
void Save();
static const std::array<QVariant, Settings::NativeInput::NUM_INPUTS> defaults;
static const std::array<int, Settings::NativeButton::NumButtons> default_buttons;
static const std::array<std::array<int, 5>, Settings::NativeAnalog::NumAnalogs> default_analogs;
};

View file

@ -2,13 +2,21 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <memory>
#include <utility>
#include <QTimer>
#include "citra_qt/config.h"
#include "citra_qt/configure_input.h"
#include "common/param_package.h"
#include "input_common/main.h"
static QString getKeyName(Qt::Key key_code) {
const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
ConfigureInput::analog_sub_buttons{{
"up", "down", "left", "right", "modifier",
}};
static QString getKeyName(int key_code) {
switch (key_code) {
case Qt::Key_Shift:
return QObject::tr("Shift");
@ -23,6 +31,20 @@ static QString getKeyName(Qt::Key key_code) {
}
}
static void SetButtonKey(int key, Common::ParamPackage& button_param) {
button_param = Common::ParamPackage{InputCommon::GenerateKeyboardParam(key)};
}
static void SetAnalogKey(int key, Common::ParamPackage& analog_param,
const std::string& button_name) {
if (analog_param.Get("engine", "") != "analog_from_button") {
analog_param = {
{"engine", "analog_from_button"}, {"modifier_scale", "0.5"},
};
}
analog_param.Set(button_name, InputCommon::GenerateKeyboardParam(key));
}
ConfigureInput::ConfigureInput(QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
timer(std::make_unique<QTimer>()) {
@ -31,36 +53,41 @@ ConfigureInput::ConfigureInput(QWidget* parent)
setFocusPolicy(Qt::ClickFocus);
button_map = {
{Settings::NativeInput::Values::A, ui->buttonA},
{Settings::NativeInput::Values::B, ui->buttonB},
{Settings::NativeInput::Values::X, ui->buttonX},
{Settings::NativeInput::Values::Y, ui->buttonY},
{Settings::NativeInput::Values::L, ui->buttonL},
{Settings::NativeInput::Values::R, ui->buttonR},
{Settings::NativeInput::Values::ZL, ui->buttonZL},
{Settings::NativeInput::Values::ZR, ui->buttonZR},
{Settings::NativeInput::Values::START, ui->buttonStart},
{Settings::NativeInput::Values::SELECT, ui->buttonSelect},
{Settings::NativeInput::Values::HOME, ui->buttonHome},
{Settings::NativeInput::Values::DUP, ui->buttonDpadUp},
{Settings::NativeInput::Values::DDOWN, ui->buttonDpadDown},
{Settings::NativeInput::Values::DLEFT, ui->buttonDpadLeft},
{Settings::NativeInput::Values::DRIGHT, ui->buttonDpadRight},
{Settings::NativeInput::Values::CUP, ui->buttonCStickUp},
{Settings::NativeInput::Values::CDOWN, ui->buttonCStickDown},
{Settings::NativeInput::Values::CLEFT, ui->buttonCStickLeft},
{Settings::NativeInput::Values::CRIGHT, ui->buttonCStickRight},
{Settings::NativeInput::Values::CIRCLE_UP, ui->buttonCircleUp},
{Settings::NativeInput::Values::CIRCLE_DOWN, ui->buttonCircleDown},
{Settings::NativeInput::Values::CIRCLE_LEFT, ui->buttonCircleLeft},
{Settings::NativeInput::Values::CIRCLE_RIGHT, ui->buttonCircleRight},
{Settings::NativeInput::Values::CIRCLE_MODIFIER, ui->buttonCircleMod},
ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY, ui->buttonDpadUp,
ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight, ui->buttonL, ui->buttonR,
ui->buttonStart, ui->buttonSelect, ui->buttonZL, ui->buttonZR, ui->buttonHome,
};
for (const auto& entry : button_map) {
const Settings::NativeInput::Values input_id = entry.first;
connect(entry.second, &QPushButton::released,
[this, input_id]() { handleClick(input_id); });
analog_map = {{
{
ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight,
ui->buttonCircleMod,
},
{
ui->buttonCStickUp, ui->buttonCStickDown, ui->buttonCStickLeft, ui->buttonCStickRight,
nullptr,
},
}};
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
if (button_map[button_id])
connect(button_map[button_id], &QPushButton::released, [=]() {
handleClick(button_map[button_id],
[=](int key) { SetButtonKey(key, buttons_param[button_id]); });
});
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
if (analog_map[analog_id][sub_button_id] != nullptr) {
connect(analog_map[analog_id][sub_button_id], &QPushButton::released, [=]() {
handleClick(analog_map[analog_id][sub_button_id], [=](int key) {
SetAnalogKey(key, analogs_param[analog_id],
analog_sub_buttons[sub_button_id]);
});
});
}
}
}
connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
@ -69,50 +96,93 @@ ConfigureInput::ConfigureInput(QWidget* parent)
connect(timer.get(), &QTimer::timeout, [this]() {
releaseKeyboard();
releaseMouse();
current_input_id = boost::none;
key_setter = boost::none;
updateButtonLabels();
});
this->loadConfiguration();
// TODO(wwylele): enable these when the input emulation for them is implemented
ui->buttonZL->setEnabled(false);
ui->buttonZR->setEnabled(false);
ui->buttonHome->setEnabled(false);
ui->buttonCStickUp->setEnabled(false);
ui->buttonCStickDown->setEnabled(false);
ui->buttonCStickLeft->setEnabled(false);
ui->buttonCStickRight->setEnabled(false);
}
void ConfigureInput::applyConfiguration() {
for (const auto& input_id : Settings::NativeInput::All) {
const size_t index = static_cast<size_t>(input_id);
Settings::values.input_mappings[index] = static_cast<int>(key_map[input_id]);
}
std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
[](const Common::ParamPackage& param) { return param.Serialize(); });
std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
[](const Common::ParamPackage& param) { return param.Serialize(); });
Settings::Apply();
}
void ConfigureInput::loadConfiguration() {
for (const auto& input_id : Settings::NativeInput::All) {
const size_t index = static_cast<size_t>(input_id);
key_map[input_id] = static_cast<Qt::Key>(Settings::values.input_mappings[index]);
}
std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
buttons_param.begin(),
[](const std::string& str) { return Common::ParamPackage(str); });
std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(),
analogs_param.begin(),
[](const std::string& str) { return Common::ParamPackage(str); });
updateButtonLabels();
}
void ConfigureInput::restoreDefaults() {
for (const auto& input_id : Settings::NativeInput::All) {
const size_t index = static_cast<size_t>(input_id);
key_map[input_id] = static_cast<Qt::Key>(Config::defaults[index].toInt());
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]);
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
SetAnalogKey(Config::default_analogs[analog_id][sub_button_id],
analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
}
}
updateButtonLabels();
applyConfiguration();
}
void ConfigureInput::updateButtonLabels() {
for (const auto& input_id : Settings::NativeInput::All) {
button_map[input_id]->setText(getKeyName(key_map[input_id]));
QString non_keyboard(tr("[non-keyboard]"));
auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) {
if (param.Get("engine", "") != "keyboard") {
return non_keyboard;
} else {
return getKeyName(param.Get("code", 0));
}
};
for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
button_map[button]->setText(KeyToText(buttons_param[button]));
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") {
for (QPushButton* button : analog_map[analog_id]) {
if (button)
button->setText(non_keyboard);
}
} else {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
Common::ParamPackage param(
analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], ""));
if (analog_map[analog_id][sub_button_id])
analog_map[analog_id][sub_button_id]->setText(KeyToText(param));
}
}
}
}
void ConfigureInput::handleClick(Settings::NativeInput::Values input_id) {
QPushButton* button = button_map[input_id];
void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) {
button->setText(tr("[press key]"));
button->setFocus();
current_input_id = input_id;
key_setter = new_key_setter;
grabKeyboard();
grabMouse();
@ -123,23 +193,13 @@ void ConfigureInput::keyPressEvent(QKeyEvent* event) {
releaseKeyboard();
releaseMouse();
if (!current_input_id || !event)
if (!key_setter || !event)
return;
if (event->key() != Qt::Key_Escape)
setInput(*current_input_id, static_cast<Qt::Key>(event->key()));
(*key_setter)(event->key());
updateButtonLabels();
current_input_id = boost::none;
key_setter = boost::none;
timer->stop();
}
void ConfigureInput::setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed) {
// Remove duplicates
for (auto& pair : key_map) {
if (pair.second == key_pressed)
pair.second = Qt::Key_unknown;
}
key_map[input_id] = key_pressed;
}

View file

@ -4,10 +4,14 @@
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <string>
#include <QKeyEvent>
#include <QWidget>
#include <boost/optional.hpp>
#include "common/param_package.h"
#include "core/settings.h"
#include "ui_configure_input.h"
@ -31,15 +35,25 @@ public:
private:
std::unique_ptr<Ui::ConfigureInput> ui;
/// This input is currently awaiting configuration.
/// (i.e.: its corresponding QPushButton has been pressed.)
boost::optional<Settings::NativeInput::Values> current_input_id;
std::unique_ptr<QTimer> timer;
/// Each input is represented by a QPushButton.
std::map<Settings::NativeInput::Values, QPushButton*> button_map;
/// Each input is configured to respond to the press of a Qt::Key.
std::map<Settings::NativeInput::Values, Qt::Key> key_map;
/// This will be the the setting function when an input is awaiting configuration.
boost::optional<std::function<void(int)>> key_setter;
std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param;
std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param;
static constexpr int ANALOG_SUB_BUTTONS_NUM = 5;
/// Each button input is represented by a QPushButton.
std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map;
/// Each analog input is represented by five QPushButtons which represents up, down, left, right
/// and modifier
std::array<std::array<QPushButton*, ANALOG_SUB_BUTTONS_NUM>, Settings::NativeAnalog::NumAnalogs>
analog_map;
static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons;
/// Load configuration settings.
void loadConfiguration();
@ -48,10 +62,8 @@ private:
/// Update UI to reflect current configuration.
void updateButtonLabels();
/// Called when the button corresponding to input_id was pressed.
void handleClick(Settings::NativeInput::Values input_id);
/// Called when the button was pressed.
void handleClick(QPushButton* button, std::function<void(int)> new_key_setter);
/// Handle key press events.
void keyPressEvent(QKeyEvent* event) override;
/// Configure input input_id to respond to key key_pressed.
void setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed);
};

View file

@ -35,6 +35,7 @@ set(SRCS
memory_util.cpp
microprofile.cpp
misc.cpp
param_package.cpp
scm_rev.cpp
string_util.cpp
symbols.cpp
@ -66,6 +67,7 @@ set(HEADERS
memory_util.h
microprofile.h
microprofileui.h
param_package.h
platform.h
quaternion.h
scm_rev.h

View file

@ -71,6 +71,7 @@ namespace Log {
CLS(Audio) \
SUB(Audio, DSP) \
SUB(Audio, Sink) \
CLS(Input) \
CLS(Loader)
// GetClassName is a macro defined by Windows.h, grrr...

View file

@ -89,6 +89,7 @@ enum class Class : ClassType {
Audio_DSP, ///< The HLE implementation of the DSP
Audio_Sink, ///< Emulator audio output backend
Loader, ///< ROM loader
Input, ///< Input emulation
Count ///< Total number of logging classes
};

View file

@ -0,0 +1,120 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <array>
#include <vector>
#include "common/logging/log.h"
#include "common/param_package.h"
#include "common/string_util.h"
namespace Common {
constexpr char KEY_VALUE_SEPARATOR = ':';
constexpr char PARAM_SEPARATOR = ',';
constexpr char ESCAPE_CHARACTER = '$';
const std::string KEY_VALUE_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '0'};
const std::string PARAM_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '1'};
const std::string ESCAPE_CHARACTER_ESCAPE{ESCAPE_CHARACTER, '2'};
ParamPackage::ParamPackage(const std::string& serialized) {
std::vector<std::string> pairs;
Common::SplitString(serialized, PARAM_SEPARATOR, pairs);
for (const std::string& pair : pairs) {
std::vector<std::string> key_value;
Common::SplitString(pair, KEY_VALUE_SEPARATOR, key_value);
if (key_value.size() != 2) {
LOG_ERROR(Common, "invalid key pair %s", pair.c_str());
continue;
}
for (std::string& part : key_value) {
part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR});
part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR});
part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER});
}
Set(key_value[0], key_value[1]);
}
}
ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
std::string ParamPackage::Serialize() const {
if (data.empty())
return "";
std::string result;
for (const auto& pair : data) {
std::array<std::string, 2> key_value{{pair.first, pair.second}};
for (std::string& part : key_value) {
part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE);
part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE);
part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE);
}
result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR;
}
result.pop_back(); // discard the trailing PARAM_SEPARATOR
return result;
}
std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
auto pair = data.find(key);
if (pair == data.end()) {
LOG_DEBUG(Common, "key %s not found", key.c_str());
return default_value;
}
return pair->second;
}
int ParamPackage::Get(const std::string& key, int default_value) const {
auto pair = data.find(key);
if (pair == data.end()) {
LOG_DEBUG(Common, "key %s not found", key.c_str());
return default_value;
}
try {
return std::stoi(pair->second);
} catch (const std::logic_error&) {
LOG_ERROR(Common, "failed to convert %s to int", pair->second.c_str());
return default_value;
}
}
float ParamPackage::Get(const std::string& key, float default_value) const {
auto pair = data.find(key);
if (pair == data.end()) {
LOG_DEBUG(Common, "key %s not found", key.c_str());
return default_value;
}
try {
return std::stof(pair->second);
} catch (const std::logic_error&) {
LOG_ERROR(Common, "failed to convert %s to float", pair->second.c_str());
return default_value;
}
}
void ParamPackage::Set(const std::string& key, const std::string& value) {
data[key] = value;
}
void ParamPackage::Set(const std::string& key, int value) {
data[key] = std::to_string(value);
}
void ParamPackage::Set(const std::string& key, float value) {
data[key] = std::to_string(value);
}
bool ParamPackage::Has(const std::string& key) const {
return data.find(key) != data.end();
}
} // namespace Common

View file

@ -0,0 +1,40 @@
// Copyright 2017 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <initializer_list>
#include <string>
#include <unordered_map>
namespace Common {
/// A string-based key-value container supporting serializing to and deserializing from a string
class ParamPackage {
public:
using DataType = std::unordered_map<std::string, std::string>;
ParamPackage() = default;
explicit ParamPackage(const std::string& serialized);
ParamPackage(std::initializer_list<DataType::value_type> list);
ParamPackage(const ParamPackage& other) = default;
ParamPackage(ParamPackage&& other) = default;
ParamPackage& operator=(const ParamPackage& other) = default;
ParamPackage& operator=(ParamPackage&& other) = default;
std::string Serialize() const;
std::string Get(const std::string& key, const std::string& default_value) const;
int Get(const std::string& key, int default_value) const;
float Get(const std::string& key, float default_value) const;
void Set(const std::string& key, const std::string& value);
void Set(const std::string& key, int value);
void Set(const std::string& key, float value);
bool Has(const std::string& key) const;
private:
DataType data;
};
} // namespace Common

View file

@ -34,7 +34,6 @@ set(SRCS
frontend/camera/factory.cpp
frontend/camera/interface.cpp
frontend/emu_window.cpp
frontend/key_map.cpp
frontend/motion_emu.cpp
gdbstub/gdbstub.cpp
hle/config_mem.cpp
@ -218,7 +217,7 @@ set(HEADERS
frontend/camera/factory.h
frontend/camera/interface.h
frontend/emu_window.h
frontend/key_map.h
frontend/input.h
frontend/motion_emu.h
gdbstub/gdbstub.h
hle/config_mem.h

View file

@ -7,33 +7,9 @@
#include "common/assert.h"
#include "core/core.h"
#include "core/frontend/emu_window.h"
#include "core/frontend/key_map.h"
#include "core/settings.h"
#include "video_core/video_core.h"
void EmuWindow::ButtonPressed(Service::HID::PadState pad) {
pad_state.hex |= pad.hex;
}
void EmuWindow::ButtonReleased(Service::HID::PadState pad) {
pad_state.hex &= ~pad.hex;
}
void EmuWindow::CirclePadUpdated(float x, float y) {
constexpr int MAX_CIRCLEPAD_POS = 0x9C; // Max value for a circle pad position
// Make sure the coordinates are in the unit circle,
// otherwise normalize it.
float r = x * x + y * y;
if (r > 1) {
r = std::sqrt(r);
x /= r;
y /= r;
}
circle_pad_x = static_cast<s16>(x * MAX_CIRCLEPAD_POS);
circle_pad_y = static_cast<s16>(y * MAX_CIRCLEPAD_POS);
}
/**
* Check if the given x/y coordinates are within the touchpad specified by the framebuffer layout
* @param layout FramebufferLayout object describing the framebuffer size and screen positions

Some files were not shown because too many files have changed in this diff Show more