Skip to content

Commit 792912a

Browse files
committed
Migrate procon and joycon to new controller input API.
1 parent 65974d5 commit 792912a

24 files changed

+819
-982
lines changed

SerialPrograms/Source/ControllerInput/Keyboard/KeyBindingOption.cpp

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,75 +13,6 @@ namespace PokemonAutomation{
1313

1414

1515

16-
struct KeyBindingCell::Data{
17-
const uint32_t m_default;
18-
19-
mutable SpinLock m_lock;
20-
uint32_t m_current;
21-
std::string m_current_text;
22-
23-
Data(uint32_t default_value)
24-
: m_default(default_value)
25-
, m_current(default_value)
26-
{}
27-
};
28-
29-
30-
31-
KeyBindingCell::~KeyBindingCell(){
32-
33-
}
34-
KeyBindingCell::KeyBindingCell(LockMode lock_while_program_is_running)
35-
: ConfigOption(lock_while_program_is_running)
36-
, m_data(CONSTRUCT_TOKEN, 0)
37-
{}
38-
39-
40-
KeyBindingCell::operator uint32_t() const{
41-
ReadSpinLock lg(m_data->m_lock);
42-
return m_data->m_current;
43-
}
44-
KeyBindingCell::operator std::string() const{
45-
ReadSpinLock lg(m_data->m_lock);
46-
return m_data->m_current_text;
47-
}
48-
void KeyBindingCell::set(uint32_t key){
49-
{
50-
ReadSpinLock lg(m_data->m_lock);
51-
m_data->m_current = key;
52-
}
53-
report_value_changed(this);
54-
}
55-
void KeyBindingCell::set(std::string text){
56-
ReadSpinLock lg(m_data->m_lock);
57-
m_data->m_current_text = std::move(text);
58-
}
59-
60-
61-
62-
void KeyBindingCell::load_json(const JsonValue& json){
63-
set((uint32_t)json.to_integer_default(0));
64-
}
65-
JsonValue KeyBindingCell::to_json() const{
66-
ReadSpinLock lg(m_data->m_lock);
67-
return m_data->m_current;
68-
}
69-
70-
void KeyBindingCell::restore_defaults(){
71-
set(m_data->m_default);
72-
}
73-
74-
75-
76-
77-
78-
79-
80-
81-
82-
83-
84-
8516
struct KeyboardHidBindingCell::Data{
8617
const KeyboardKey m_default;
8718

SerialPrograms/Source/ControllerInput/Keyboard/KeyBindingOption.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,6 @@ namespace PokemonAutomation{
1515

1616

1717

18-
class KeyBindingCell : public ConfigOption{
19-
public:
20-
~KeyBindingCell();
21-
KeyBindingCell(LockMode lock_while_program_is_running);
22-
23-
operator uint32_t() const;
24-
operator std::string() const;
25-
void set(uint32_t key);
26-
27-
virtual void load_json(const JsonValue& json) override;
28-
virtual JsonValue to_json() const override;
29-
30-
virtual void restore_defaults() override;
31-
32-
virtual ConfigWidget* make_QtWidget(QWidget& parent) override;
33-
34-
35-
public:
36-
// UI Functions
37-
void set(std::string text);
38-
39-
40-
private:
41-
struct Data;
42-
Pimpl<Data> m_data;
43-
};
44-
45-
46-
4718

4819
class KeyboardHidBindingCell : public ConfigOption{
4920
public:

SerialPrograms/Source/ControllerInput/Keyboard/KeyBindingWidget.cpp

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include <QKeyEvent>
8+
#include "CommonFramework/Logging/Logger.h"
89
#include "ControllerInput/Keyboard/GlobalQtKeyMap.h"
910
#include "ControllerInput/Keyboard/KeyboardInput_KeyMappings.h"
1011
#include "KeyBindingWidget.h"
@@ -17,49 +18,6 @@ namespace PokemonAutomation{
1718

1819

1920

20-
ConfigWidget* KeyBindingCell::make_QtWidget(QWidget& parent){
21-
return new KeyBindingCellWidget(parent, *this);
22-
}
23-
24-
25-
26-
KeyBindingCellWidget::~KeyBindingCellWidget(){
27-
m_value.remove_listener(*this);
28-
}
29-
KeyBindingCellWidget::KeyBindingCellWidget(QWidget& parent, KeyBindingCell& value)
30-
: QLineEdit(&parent)
31-
, ConfigWidget(value, *this)
32-
, m_value(value)
33-
{
34-
KeyBindingCellWidget::update_value();
35-
m_value.add_listener(*this);
36-
}
37-
38-
39-
void KeyBindingCellWidget::keyPressEvent(QKeyEvent* event){
40-
m_value.set(event->key());
41-
}
42-
43-
44-
void KeyBindingCellWidget::update_value(){
45-
QKeySequence seq((Qt::Key)(uint32_t)m_value);
46-
// cout << (uint32_t)m_value << endl;
47-
this->setText(seq.toString());
48-
}
49-
void KeyBindingCellWidget::on_config_value_changed(void* object){
50-
QMetaObject::invokeMethod(this, [this]{
51-
update_value();
52-
}, Qt::QueuedConnection);
53-
}
54-
55-
56-
57-
58-
59-
60-
61-
62-
6321
ConfigWidget* KeyboardHidBindingCell::make_QtWidget(QWidget& parent){
6422
return new KeyboardHidBindingCellWidget(parent, *this);
6523
}
@@ -89,6 +47,7 @@ void KeyboardHidBindingCellWidget::keyPressEvent(QKeyEvent* event){
8947
));
9048

9149
if (iter == MAP.end()){
50+
global_logger_tagged().log("Unable to map Qt::Key to HID ID: " + std::to_string(event->key()));
9251
return;
9352
}
9453

@@ -100,9 +59,9 @@ void KeyboardHidBindingCellWidget::update_value(){
10059
const std::map<KeyboardKey, std::string>& MAP = KEYBOARDKEY_TO_STRING();
10160
auto iter = MAP.find(m_value);
10261
if (iter != MAP.end()){
103-
this->setText(QString::fromStdString("HID: " + std::to_string((int)m_value) + " - " + iter->second));
62+
this->setText(QString::fromStdString(std::to_string((int)m_value) + ": " + iter->second));
10463
}else{
105-
this->setText(QString::fromStdString("HID: " + std::to_string((int)m_value)));
64+
this->setText(QString::fromStdString(std::to_string((int)m_value)));
10665
}
10766
}
10867
void KeyboardHidBindingCellWidget::on_config_value_changed(void* object){

SerialPrograms/Source/ControllerInput/Keyboard/KeyBindingWidget.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,6 @@ namespace PokemonAutomation{
1515

1616

1717

18-
class KeyBindingCellWidget : public QLineEdit, public ConfigWidget{
19-
public:
20-
~KeyBindingCellWidget();
21-
KeyBindingCellWidget(QWidget& parent, KeyBindingCell& value);
22-
23-
virtual void update_value() override;
24-
virtual void on_config_value_changed(void* object) override;
25-
26-
virtual void keyPressEvent(QKeyEvent* event) override;
27-
28-
private:
29-
KeyBindingCell& m_value;
30-
};
31-
32-
33-
34-
35-
3618
class KeyboardHidBindingCellWidget : public QLineEdit, public ConfigWidget{
3719
public:
3820
~KeyboardHidBindingCellWidget();

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings_AZERTY.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_AZERTY(){
5050
{Qt::Key::Key_Ccedilla, KeyboardKey::KEY_9}, // c key -> HID 9 position
5151
{Qt::Key::Key_Agrave, KeyboardKey::KEY_0}, // a key -> HID 0 position
5252

53+
{Qt::Key::Key_Return, KeyboardKey::KEY_ENTER},
5354
{Qt::Key::Key_Enter, KeyboardKey::KEY_ENTER},
5455
{Qt::Key::Key_Escape, KeyboardKey::KEY_ESC},
5556
{Qt::Key::Key_Backspace, KeyboardKey::KEY_BACKSPACE},

SerialPrograms/Source/ControllerInput/Keyboard/KeyboardInput_KeyMappings_QWERTY.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const std::map<QtKeyMap::QtKey, KeyboardKey>& KEYID_TO_HID_QWERTY(){
5050
{Qt::Key::Key_9, KeyboardKey::KEY_9},
5151
{Qt::Key::Key_0, KeyboardKey::KEY_0},
5252

53+
{Qt::Key::Key_Return, KeyboardKey::KEY_ENTER},
5354
{Qt::Key::Key_Enter, KeyboardKey::KEY_ENTER},
5455
{Qt::Key::Key_Escape, KeyboardKey::KEY_ESC},
5556
{Qt::Key::Key_Backspace, KeyboardKey::KEY_BACKSPACE},

SerialPrograms/Source/Controllers/KeyboardInput/KeyboardEventHandler.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class KeyboardEventHandler{
2828
void add_listener(KeyboardListener& listener);
2929
void remove_listener(KeyboardListener& listener);
3030

31-
protected:
3231
// Report that the keyboard state has changed. This will be pushed to
3332
// all listeners.
3433
void report_keyboard_command_sent(WallClock time_stamp, const ControllerState& state);
@@ -43,4 +42,4 @@ class KeyboardEventHandler{
4342
};
4443

4544
}
46-
#endif
45+
#endif

SerialPrograms/Source/NintendoSwitch/Controllers/Joycon/NintendoSwitch_Joycon.cpp

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "Common/Cpp/Containers/Pimpl.tpp"
88
#include "CommonTools/Async/InterruptableCommands.tpp"
99
#include "CommonTools/Async/SuperControlSession.tpp"
10+
#include "ControllerInput/ControllerInput.h"
11+
#include "ControllerInput/Keyboard/KeyboardInput_State.h"
1012
#include "Controllers/ControllerTypes.h"
1113
#include "Controllers/KeyboardInput/KeyboardInput.h"
1214
#include "NintendoSwitch/NintendoSwitch_Settings.h"
@@ -35,92 +37,73 @@ const char RightJoycon::NAME[] = "Nintendo Switch: Right Joycon";
3537

3638

3739

38-
class JoyconController::KeyboardManager final :
39-
public PokemonAutomation::KeyboardManager<JoyconState, JoyconDeltas>
40-
{
41-
public:
42-
KeyboardManager(Logger& logger, JoyconController& controller, ControllerClass controller_class)
43-
: PokemonAutomation::KeyboardManager<JoyconState, JoyconDeltas>(logger, controller)
44-
{
45-
std::vector<std::shared_ptr<EditableTableRow>> mapping;
46-
switch (controller_class){
47-
case ControllerClass::NintendoSwitch_LeftJoycon:
48-
mapping = ConsoleSettings::instance().KEYBOARD_MAPPINGS.LEFT_JOYCON0.current_refs();
49-
break;
50-
case ControllerClass::NintendoSwitch_RightJoycon:
51-
mapping = ConsoleSettings::instance().KEYBOARD_MAPPINGS.RIGHT_JOYCON0.current_refs();
52-
break;
53-
default:;
54-
}
55-
for (const auto& deltas : mapping){
56-
const JoyconKeyMapTableRow& row = static_cast<const JoyconKeyMapTableRow&>(*deltas);
57-
m_mapping[(Qt::Key)(uint32_t)row.key] += row.snapshot();
58-
}
59-
start();
60-
}
61-
~KeyboardManager(){
62-
stop();
63-
}
64-
virtual void send_state(const ControllerState& state) override{
65-
66-
const JoyconState& switch_state = static_cast<const JoyconState&>(state);
67-
#if 0
68-
m_controller->logger().log(
69-
"VirtualController: (" + button_to_string(switch_state.buttons) +
70-
"), LJ(" + std::to_string(switch_state.joystick_x) + "," + std::to_string(switch_state.joystick_y) +
71-
")",
72-
COLOR_DARKGREEN
73-
);
74-
#endif
75-
WriteSpinLock lg(m_lock);
76-
if (m_controller == nullptr){
77-
return;
78-
}
79-
Milliseconds ticksize = m_controller->ticksize();
80-
WallClock time_stamp = current_time();
81-
static_cast<JoyconController*>(m_controller)->issue_full_controller_state(
82-
nullptr,
83-
false,
84-
ticksize == Milliseconds::zero() ? 2000ms : ticksize * 255,
85-
switch_state.buttons,
86-
switch_state.joystick_x,
87-
switch_state.joystick_y
88-
);
89-
report_keyboard_command_sent(time_stamp, switch_state);
90-
}
40+
struct JoyconController::Data{
41+
std::map<KeyboardKey, JoyconDeltas> m_keyboard_mapping;
42+
KeyboardEventHandler m_input_sniffer;
9143
};
9244

9345

9446

47+
9548
JoyconController::JoyconController(Logger& logger, ControllerClass controller_class)
96-
: m_keyboard_manager(CONSTRUCT_TOKEN, logger, *this, controller_class)
49+
: m_data(CONSTRUCT_TOKEN)
9750
{
98-
51+
std::vector<std::shared_ptr<EditableTableRow>> mapping =
52+
controller_class == ControllerClass::NintendoSwitch_LeftJoycon
53+
? ConsoleSettings::instance().KEYBOARD_MAPPINGS.LEFT_JOYCON.current_refs()
54+
: ConsoleSettings::instance().KEYBOARD_MAPPINGS.RIGHT_JOYCON.current_refs();
55+
56+
for (const auto& deltas : mapping){
57+
const JoyconFromKeyboardTableRow& row = static_cast<const JoyconFromKeyboardTableRow&>(*deltas);
58+
m_data->m_keyboard_mapping[row.key] += row.snapshot();
59+
}
9960
}
10061
JoyconController::~JoyconController(){
101-
102-
}
103-
void JoyconController::stop() noexcept{
104-
m_keyboard_manager->stop();
10562
}
10663

64+
void JoyconController::controller_input_state(const ControllerInputState& state){
10765

108-
void JoyconController::keyboard_release_all(){
109-
m_keyboard_manager->clear_state();
110-
}
111-
void JoyconController::keyboard_press(const QKeyEvent& event){
112-
m_keyboard_manager->on_key_press(event);
113-
}
114-
void JoyconController::keyboard_release(const QKeyEvent& event){
115-
m_keyboard_manager->on_key_release(event);
66+
if (state.type() != ControllerInputType::HID_Keyboard){
67+
return;
68+
}
69+
70+
JoyconDeltas deltas;
71+
72+
const KeyboardInputState& lstate = static_cast<const KeyboardInputState&>(state);
73+
const std::map<KeyboardKey, JoyconDeltas>& map = m_data->m_keyboard_mapping;
74+
75+
// cout << "keys() = " << lstate.keys().size() << endl;
76+
77+
for (KeyboardKey key : lstate.keys()){
78+
auto iter = map.find(key);
79+
if (iter != map.end()){
80+
deltas += iter->second;
81+
}
82+
}
83+
84+
replace_on_next_command();
85+
86+
WallClock timestamp = current_time();
87+
88+
JoyconState controller_state;
89+
deltas.to_state(controller_state);
90+
issue_full_controller_state(
91+
nullptr,
92+
false,
93+
2000ms,
94+
controller_state.buttons,
95+
controller_state.joystick_x,
96+
controller_state.joystick_y
97+
);
98+
99+
m_data->m_input_sniffer.report_keyboard_command_sent(timestamp, controller_state);
116100
}
117101

118102
void JoyconController::add_keyboard_listener(KeyboardEventHandler::KeyboardListener& keyboard_listener){
119-
m_keyboard_manager->add_listener(keyboard_listener);
103+
m_data->m_input_sniffer.add_listener(keyboard_listener);
120104
}
121-
122105
void JoyconController::remove_keyboard_listener(KeyboardEventHandler::KeyboardListener& keyboard_listener){
123-
m_keyboard_manager->remove_listener(keyboard_listener);
106+
m_data->m_input_sniffer.remove_listener(keyboard_listener);
124107
}
125108

126109

0 commit comments

Comments
 (0)