diff --git a/src/gba/gba-input.c b/src/gba/gba-input.c index ad70d6168..f6fcc753d 100644 --- a/src/gba/gba-input.c +++ b/src/gba/gba-input.c @@ -2,8 +2,11 @@ #include "util/configuration.h" +#include + #define SECTION_NAME_MAX 128 #define KEY_NAME_MAX 32 +#define KEY_VALUE_MAX 16 struct GBAInputMapImpl { int* map; @@ -17,6 +20,7 @@ static void _loadKey(struct GBAInputMap* map, uint32_t type, const struct Config char keyKey[KEY_NAME_MAX]; snprintf(keyKey, KEY_NAME_MAX, "key%s", keyName); + keyKey[KEY_NAME_MAX - 1] = '\0'; const char* value = ConfigurationGetValue(config, sectionName, keyKey); if (!value) { @@ -30,6 +34,22 @@ static void _loadKey(struct GBAInputMap* map, uint32_t type, const struct Config GBAInputBindKey(map, type, intValue, key); } +static void _saveKey(const struct GBAInputMap* map, uint32_t type, struct Configuration* config, enum GBAKey key, const char* keyName) { + char sectionName[SECTION_NAME_MAX]; + snprintf(sectionName, SECTION_NAME_MAX, "input.%c%c%c%c", type >> 24, type >> 16, type >> 8, type); + sectionName[SECTION_NAME_MAX - 1] = '\0'; + + char keyKey[KEY_NAME_MAX]; + snprintf(keyKey, KEY_NAME_MAX, "key%s", keyName); + keyKey[KEY_NAME_MAX - 1] = '\0'; + + int value = GBAInputQueryBinding(map, type, key); + char keyValue[KEY_VALUE_MAX]; + snprintf(keyValue, KEY_VALUE_MAX, "%" PRIi32, value); + + ConfigurationSetValue(config, sectionName, keyKey, keyValue); +} + void GBAInputMapInit(struct GBAInputMap* map) { map->maps = 0; map->numMaps = 0; @@ -129,7 +149,6 @@ int GBAInputQueryBinding(const struct GBAInputMap* map, uint32_t type, enum GBAK return impl->map[input]; } - void GBAInputMapLoad(struct GBAInputMap* map, uint32_t type, const struct Configuration* config) { _loadKey(map, type, config, GBA_KEY_A, "A"); _loadKey(map, type, config, GBA_KEY_B, "B"); @@ -142,3 +161,16 @@ void GBAInputMapLoad(struct GBAInputMap* map, uint32_t type, const struct Config _loadKey(map, type, config, GBA_KEY_LEFT, "Left"); _loadKey(map, type, config, GBA_KEY_RIGHT, "Right"); } + +void GBAInputMapSave(const struct GBAInputMap* map, uint32_t type, struct Configuration* config) { + _saveKey(map, type, config, GBA_KEY_A, "A"); + _saveKey(map, type, config, GBA_KEY_B, "B"); + _saveKey(map, type, config, GBA_KEY_L, "L"); + _saveKey(map, type, config, GBA_KEY_R, "R"); + _saveKey(map, type, config, GBA_KEY_START, "Start"); + _saveKey(map, type, config, GBA_KEY_SELECT, "Select"); + _saveKey(map, type, config, GBA_KEY_UP, "Up"); + _saveKey(map, type, config, GBA_KEY_DOWN, "Down"); + _saveKey(map, type, config, GBA_KEY_LEFT, "Left"); + _saveKey(map, type, config, GBA_KEY_RIGHT, "Right"); +} diff --git a/src/gba/gba-input.h b/src/gba/gba-input.h index f7e7cc51a..73cd58953 100644 --- a/src/gba/gba-input.h +++ b/src/gba/gba-input.h @@ -18,5 +18,6 @@ void GBAInputBindKey(struct GBAInputMap*, uint32_t type, int key, enum GBAKey in int GBAInputQueryBinding(const struct GBAInputMap*, uint32_t type, enum GBAKey input); void GBAInputMapLoad(struct GBAInputMap*, uint32_t type, const struct Configuration*); +void GBAInputMapSave(const struct GBAInputMap*, uint32_t type, struct Configuration*); #endif diff --git a/src/platform/qt/ConfigController.h b/src/platform/qt/ConfigController.h index a817f7bf1..33f4000b2 100644 --- a/src/platform/qt/ConfigController.h +++ b/src/platform/qt/ConfigController.h @@ -57,9 +57,6 @@ public: const GBAOptions* options() const { return &m_opts; } bool parseArguments(GBAArguments* args, int argc, char* argv[]); - const Configuration* configuration() const { return &m_config.configTable; } - const Configuration* defaults() const { return &m_config.defaultsTable; } - ConfigOption* addOption(const char* key); void updateOption(const char* key); @@ -73,6 +70,11 @@ public slots: void write(); private: + Configuration* configuration() { return &m_config.configTable; } + Configuration* defaults() { return &m_config.defaultsTable; } + + friend class InputController; // TODO: Do this without friends + GBAConfig m_config; GBAOptions m_opts; diff --git a/src/platform/qt/GBAKeyEditor.cpp b/src/platform/qt/GBAKeyEditor.cpp index 4f80f5c73..759b731c8 100644 --- a/src/platform/qt/GBAKeyEditor.cpp +++ b/src/platform/qt/GBAKeyEditor.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "InputController.h" #include "KeyEditor.h" @@ -20,6 +21,8 @@ const qreal GBAKeyEditor::DPAD_HEIGHT = 0.1; GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* parent) : QWidget(parent) + , m_type(type) + , m_controller(controller) { setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint); setMinimumSize(300, 300); @@ -47,58 +50,29 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren m_keyL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_L)); m_keyR->setValue(GBAInputQueryBinding(map, type, GBA_KEY_R)); - connect(m_keyDU, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_UP); - setNext(); - }); + connect(m_keyDU, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyDD, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyDL, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyDR, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keySelect, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyStart, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyA, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyB, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyL, SIGNAL(valueChanged(int)), this, SLOT(setNext())); + connect(m_keyR, SIGNAL(valueChanged(int)), this, SLOT(setNext())); - connect(m_keyDD, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_DOWN); - setNext(); - }); + m_buttons = new QWidget(this); + QVBoxLayout* layout = new QVBoxLayout; + m_buttons->setLayout(layout); - connect(m_keyDL, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_LEFT); - setNext(); - }); + QPushButton* setAll = new QPushButton(tr("Set all")); + connect(setAll, SIGNAL(pressed()), this, SLOT(setAll())); + layout->addWidget(setAll); - connect(m_keyDR, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_RIGHT); - setNext(); - }); - - connect(m_keySelect, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_SELECT); - setNext(); - }); - - connect(m_keyStart, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_START); - setNext(); - }); - - connect(m_keyA, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_A); - setNext(); - }); - - connect(m_keyB, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_B); - setNext(); - }); - - connect(m_keyL, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_L); - setNext(); - }); - - connect(m_keyR, &KeyEditor::valueChanged, [this, type, controller](int key) { - controller->bindKey(type, key, GBA_KEY_R); - setNext(); - }); - - m_setAll = new QPushButton(tr("Set all"), this); - connect(m_setAll, SIGNAL(pressed()), this, SLOT(setAll())); + QPushButton* save = new QPushButton(tr("Save")); + connect(save, SIGNAL(pressed()), this, SLOT(save())); + layout->addWidget(save); + layout->setSpacing(6); m_keyOrder = QList{ m_keyDU, @@ -116,6 +90,8 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren m_currentKey = m_keyOrder.end(); m_background.load(":/res/keymap.qpic"); + + setAll->setFocus(); } void GBAKeyEditor::setAll() { @@ -124,7 +100,7 @@ void GBAKeyEditor::setAll() { } void GBAKeyEditor::resizeEvent(QResizeEvent* event) { - setLocation(m_setAll, 0.5, 0.2); + setLocation(m_buttons, 0.5, 0.2); setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT); setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT); setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y); @@ -160,6 +136,20 @@ void GBAKeyEditor::setNext() { } } +void GBAKeyEditor::save() { + m_controller->bindKey(m_type, m_keyDU->value(), GBA_KEY_UP); + m_controller->bindKey(m_type, m_keyDD->value(), GBA_KEY_DOWN); + m_controller->bindKey(m_type, m_keyDL->value(), GBA_KEY_LEFT); + m_controller->bindKey(m_type, m_keyDR->value(), GBA_KEY_RIGHT); + m_controller->bindKey(m_type, m_keySelect->value(), GBA_KEY_SELECT); + m_controller->bindKey(m_type, m_keyStart->value(), GBA_KEY_START); + m_controller->bindKey(m_type, m_keyA->value(), GBA_KEY_A); + m_controller->bindKey(m_type, m_keyB->value(), GBA_KEY_B); + m_controller->bindKey(m_type, m_keyL->value(), GBA_KEY_L); + m_controller->bindKey(m_type, m_keyR->value(), GBA_KEY_R); + m_controller->saveConfiguration(m_type); +} + void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) { QSize s = size(); QSize hint = widget->sizeHint(); diff --git a/src/platform/qt/GBAKeyEditor.h b/src/platform/qt/GBAKeyEditor.h index 06fdb8345..1430d379d 100644 --- a/src/platform/qt/GBAKeyEditor.h +++ b/src/platform/qt/GBAKeyEditor.h @@ -5,8 +5,6 @@ #include #include -class QPushButton; - namespace QGBA { class InputController; @@ -25,17 +23,19 @@ protected: virtual void resizeEvent(QResizeEvent*) override; virtual void paintEvent(QPaintEvent*) override; +private slots: + void setNext(); + void save(); + private: static const qreal DPAD_CENTER_X; static const qreal DPAD_CENTER_Y; static const qreal DPAD_WIDTH; static const qreal DPAD_HEIGHT; - void setNext(); - void setLocation(QWidget* widget, qreal x, qreal y); - QPushButton* m_setAll; + QWidget* m_buttons; KeyEditor* m_keyDU; KeyEditor* m_keyDD; KeyEditor* m_keyDL; @@ -49,6 +49,7 @@ private: QList m_keyOrder; QList::iterator m_currentKey; + uint32_t m_type; InputController* m_controller; QPicture m_background; diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index da5a1b696..3df2b1e6e 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -1,6 +1,6 @@ #include "InputController.h" -#include +#include "ConfigController.h" extern "C" { #include "util/configuration.h" @@ -37,15 +37,21 @@ InputController::~InputController() { #endif } -void InputController::loadDefaultConfiguration(const Configuration* config) { - loadConfiguration(KEYBOARD, config); +void InputController::setConfiguration(ConfigController* config) { + m_config = config; + loadConfiguration(KEYBOARD); #ifdef BUILD_SDL - loadConfiguration(SDL_BINDING_BUTTON, config); + loadConfiguration(SDL_BINDING_BUTTON); #endif } -void InputController::loadConfiguration(uint32_t type, const Configuration* config) { - GBAInputMapLoad(&m_inputMap, type, config); +void InputController::loadConfiguration(uint32_t type) { + GBAInputMapLoad(&m_inputMap, type, m_config->configuration()); +} + +void InputController::saveConfiguration(uint32_t type) { + GBAInputMapSave(&m_inputMap, type, m_config->configuration()); + m_config->write(); } GBAKey InputController::mapKeyboard(int key) const { diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index ea54b88cc..c092f463b 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -9,10 +9,10 @@ extern "C" { #endif } -struct Configuration; - namespace QGBA { +class ConfigController; + class InputController { public: static const uint32_t KEYBOARD = 0x51545F4B; @@ -20,8 +20,9 @@ public: InputController(); ~InputController(); - void loadDefaultConfiguration(const Configuration* config); - void loadConfiguration(uint32_t type, const Configuration* config); + void setConfiguration(ConfigController* config); + void loadConfiguration(uint32_t type); + void saveConfiguration(uint32_t type = KEYBOARD); GBAKey mapKeyboard(int key) const; @@ -35,6 +36,7 @@ public: private: GBAInputMap m_inputMap; + ConfigController* m_config; #ifdef BUILD_SDL GBASDLEvents m_sdlEvents; diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index e64fb7ec0..550411d36 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -109,7 +109,7 @@ void Window::loadConfig() { m_screenWidget->setSizeHint(QSize(opts->width, opts->height)); } - m_inputController.loadDefaultConfiguration(m_config->configuration()); + m_inputController.setConfiguration(m_config); } void Window::saveConfig() {