Skip to content

Commit ed4ce95

Browse files
authored
Merge pull request #690 from jw098/record-keypress
Record Keyboard Controller
2 parents edd3d79 + 80a67b9 commit ed4ce95

28 files changed

+1184
-76
lines changed

Common/Cpp/Json/JsonObject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class JsonObject{
1818
JsonObject() = default;
1919
JsonObject(JsonObject&& x) = default;
2020
JsonObject& operator=(JsonObject&& x) = default;
21+
22+
bool operator==(const JsonObject& x){ // TODO: implement == properly. For JsonValue as well.
23+
return dump() == x.dump();
24+
}
25+
bool operator!=(const JsonObject& x){
26+
return !(*this == *this);
27+
}
2128
private:
2229
// Private to avoid accidental copying.
2330
JsonObject(const JsonObject& x);

Common/Cpp/StringTools.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <stdint.h>
88
#include <stdexcept>
9+
#include <vector>
910
#include "StringTools.h"
1011

1112
namespace PokemonAutomation{
@@ -48,6 +49,21 @@ size_t to_size_t(const std::string& str){
4849
}
4950
}
5051

52+
std::vector<std::string> split(const std::string& str, const std::string& delimiter) {
53+
std::vector<std::string> tokens;
54+
size_t start = 0;
55+
size_t end = str.find(delimiter);
56+
57+
while (end != std::string::npos) {
58+
tokens.push_back(str.substr(start, end - start));
59+
start = end + delimiter.length();
60+
end = str.find(delimiter, start);
61+
}
62+
63+
tokens.push_back(str.substr(start));
64+
return tokens;
65+
}
66+
5167

5268
}
5369
}

Common/Cpp/StringTools.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ std::string strip(const std::string& str);
2121
// Parse str to size_t. Return SIZE_MAX if parsing fails
2222
size_t to_size_t(const std::string& str);
2323

24+
std::vector<std::string> split(const std::string& str, const std::string& delimiter);
25+
2426
}
2527
}
2628
#endif

Common/Qt/Options/BoxFloatWidget.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <QHBoxLayout>
88
#include <QVBoxLayout>
99
#include <QLabel>
10+
#include "Common/Cpp/StringTools.h"
1011
#include "BoxFloatWidget.h"
1112

1213
//#include <iostream>
@@ -24,21 +25,6 @@ ConfigWidget* BoxFloatOption::make_QtWidget(QWidget& parent){
2425

2526

2627

27-
std::vector<std::string> split(const std::string& str, const std::string& delimiter) {
28-
std::vector<std::string> tokens;
29-
size_t start = 0;
30-
size_t end = str.find(delimiter);
31-
32-
while (end != std::string::npos) {
33-
tokens.push_back(str.substr(start, end - start));
34-
start = end + delimiter.length();
35-
end = str.find(delimiter, start);
36-
}
37-
38-
tokens.push_back(str.substr(start));
39-
return tokens;
40-
}
41-
4228

4329

4430
BoxFloatWidget::~BoxFloatWidget(){
@@ -167,7 +153,7 @@ BoxFloatWidget::BoxFloatWidget(QWidget& parent, BoxFloatOption& value)
167153
connect(
168154
m_array, &QLineEdit::editingFinished,
169155
this, [this](){
170-
std::vector<std::string> all_coords = split(m_array->text().toStdString(), ", ");
156+
std::vector<std::string> all_coords = StringTools::split(m_array->text().toStdString(), ", ");
171157
if (all_coords.size() != 4){
172158
return;
173159
}

SerialPrograms/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,8 @@ file(GLOB MAIN_SOURCES
701701
Source/Controllers/JoystickTools.h
702702
Source/Controllers/KeyboardInput/GlobalQtKeyMap.cpp
703703
Source/Controllers/KeyboardInput/GlobalQtKeyMap.h
704+
Source/Controllers/KeyboardInput/KeyboardEventHandler.cpp
705+
Source/Controllers/KeyboardInput/KeyboardEventHandler.h
704706
Source/Controllers/KeyboardInput/KeyboardInput.cpp
705707
Source/Controllers/KeyboardInput/KeyboardInput.h
706708
Source/Controllers/KeyboardInput/KeyboardStateTracker.cpp
@@ -1119,6 +1121,8 @@ file(GLOB MAIN_SOURCES
11191121
Source/NintendoSwitch/Programs/NintendoSwitch_PreventSleep.h
11201122
Source/NintendoSwitch/Programs/NintendoSwitch_PushJoySticks.cpp
11211123
Source/NintendoSwitch/Programs/NintendoSwitch_PushJoySticks.h
1124+
Source/NintendoSwitch/Programs/NintendoSwitch_RecordKeyboardController.cpp
1125+
Source/NintendoSwitch/Programs/NintendoSwitch_RecordKeyboardController.h
11221126
Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.cpp
11231127
Source/NintendoSwitch/Programs/NintendoSwitch_SnapshotDumper.h
11241128
Source/NintendoSwitch/Programs/NintendoSwitch_SwitchViewer.cpp

SerialPrograms/Source/Controllers/Controller.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "Common/Compiler.h"
1111
#include "Common/Cpp/AbstractLogger.h"
1212
#include "Common/Cpp/Time.h"
13+
#include "Controllers/KeyboardInput/KeyboardEventHandler.h"
1314
#include "Common/Cpp/CancellableScope.h"
1415

1516
class QKeyEvent;
@@ -19,6 +20,7 @@ namespace PokemonAutomation{
1920
class RecursiveThrottler;
2021
enum class ControllerType;
2122
enum class ControllerPerformanceClass;
23+
enum class ControllerClass;
2224

2325

2426

@@ -64,6 +66,7 @@ class AbstractController{
6466

6567
virtual const char* name() = 0;
6668
virtual ControllerType controller_type() const = 0;
69+
virtual ControllerClass controller_class() const = 0;
6770
virtual ControllerPerformanceClass performance_class() const = 0;
6871

6972
// If the controller is polled at a fixed interval, this is that interval.
@@ -203,6 +206,9 @@ class AbstractController{
203206
virtual void keyboard_release_all(){}
204207
virtual void keyboard_press(const QKeyEvent& event){}
205208
virtual void keyboard_release(const QKeyEvent& event){}
209+
210+
virtual void add_keyboard_listener(KeyboardEventHandler::KeyboardListener& keyboard_listener){};
211+
virtual void remove_keyboard_listener(KeyboardEventHandler::KeyboardListener& keyboard_listener){};
206212
};
207213

208214

@@ -294,6 +300,7 @@ class ControllerContext final : public CancellableScope{
294300
};
295301

296302

303+
using AbstractControllerContext = ControllerContext<AbstractController>;
297304

298305
using AbstractControllerContext = ControllerContext<AbstractController>;
299306

SerialPrograms/Source/Controllers/ControllerTypeStrings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ const EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS{
3232
{ControllerType::NintendoSwitch2_RightJoycon, "NS2: Right Joycon"},
3333
};
3434

35+
const EnumStringMap<ControllerClass>& CONTROLLER_CLASS_STRINGS(){
36+
static EnumStringMap<ControllerClass> database{
37+
{ControllerClass::NONE, "none"},
38+
{ControllerClass::LEFT_JOYCON, "left-joycon"},
39+
{ControllerClass::RIGHT_JOYCON, "right-joycon"},
40+
{ControllerClass::PRO_CONTROLLER, "pro-controller"},
41+
};
42+
return database;
43+
}
44+
3545

3646

3747

SerialPrograms/Source/Controllers/ControllerTypeStrings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace PokemonAutomation{
1616

1717
extern const EnumStringMap<ControllerInterface> CONTROLLER_INTERFACE_STRINGS;
1818
extern const EnumStringMap<ControllerType> CONTROLLER_TYPE_STRINGS;
19+
const EnumStringMap<ControllerClass>& CONTROLLER_CLASS_STRINGS();
1920

2021

2122

SerialPrograms/Source/Controllers/ControllerTypes.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ enum class ControllerPerformanceClass{
4444
SysbotBase,
4545
};
4646

47+
enum class ControllerClass{
48+
NONE,
49+
LEFT_JOYCON,
50+
RIGHT_JOYCON,
51+
PRO_CONTROLLER,
52+
KEYBOARD,
53+
};
54+
4755

4856

4957

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* Keyboard Event Handler
2+
*
3+
*/
4+
#include "Common/Cpp/ListenerSet.h"
5+
#include "Common/Cpp/Containers/Pimpl.tpp"
6+
#include "Controllers/KeyboardInput/KeyboardInput.h"
7+
#include "KeyboardEventHandler.h"
8+
9+
namespace PokemonAutomation{
10+
11+
12+
13+
struct KeyboardEventHandler::Data{
14+
ListenerSet<KeyboardListener> m_listeners;
15+
};
16+
17+
KeyboardEventHandler::KeyboardEventHandler()
18+
: m_data(CONSTRUCT_TOKEN)
19+
{}
20+
21+
KeyboardEventHandler::~KeyboardEventHandler() = default;
22+
23+
void KeyboardEventHandler::add_listener(KeyboardListener& listener){
24+
auto scope = m_lifetime_sanitizer.check_scope();
25+
Data& data = *m_data;
26+
data.m_listeners.add(listener);
27+
}
28+
void KeyboardEventHandler::remove_listener(KeyboardListener& listener){
29+
auto scope = m_lifetime_sanitizer.check_scope();
30+
Data& data = *m_data;
31+
data.m_listeners.remove(listener);
32+
}
33+
34+
void KeyboardEventHandler::report_keyboard_command_sent(WallClock time_stamp, const ControllerState& state){
35+
auto scope = m_lifetime_sanitizer.check_scope();
36+
Data& data = *m_data;
37+
data.m_listeners.run_method_unique(&KeyboardListener::on_keyboard_command_sent, time_stamp, state);
38+
}
39+
40+
void KeyboardEventHandler::report_keyboard_command_stopped(WallClock time_stamp){
41+
auto scope = m_lifetime_sanitizer.check_scope();
42+
Data& data = *m_data;
43+
data.m_listeners.run_method_unique(&KeyboardListener::on_keyboard_command_stopped, time_stamp);
44+
}
45+
46+
47+
}

0 commit comments

Comments
 (0)