Skip to content

Commit 14b2327

Browse files
committed
More refactor to prepare for new input types.
1 parent 321caf3 commit 14b2327

File tree

6 files changed

+149
-34
lines changed

6 files changed

+149
-34
lines changed

SerialPrograms/Source/CommonFramework/Main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include "Startup/NewVersionCheck.h"
2929
#include "CommonFramework/VideoPipeline/Backends/CameraImplementations.h"
3030
#include "CommonTools/OCR/OCR_RawOCR.h"
31-
#include "ControllerInput/Keyboard/GlobalKeyboardHidTracker.h"
31+
#include "ControllerInput/ControllerInput.h"
3232
#include "Windows/MainWindow.h"
3333

3434
#include <iostream>
@@ -178,7 +178,7 @@ int main(int argc, char *argv[]){
178178
#endif
179179

180180
// Stop the controllers.
181-
global_keyboard_tracker().stop();
181+
global_input_stop();
182182

183183
// Force stop the thread pool
184184
PokemonAutomation::GlobalThreadPools::realtime_inference().stop();

SerialPrograms/Source/ControllerInput/ControllerInput.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,78 @@
44
*
55
*/
66

7+
#include <vector>
78
#include "ControllerInput.h"
89

10+
#include "Keyboard/GlobalKeyboardHidTracker.h"
11+
912
namespace PokemonAutomation{
1013

1114

1215

16+
class GlobalInputSources{
17+
public:
18+
GlobalInputSources(){
19+
//
20+
// When we add more input types, add them here.
21+
//
22+
23+
m_input_sources.emplace_back(&global_keyboard_tracker());
24+
25+
}
26+
27+
void stop(){
28+
for (ControllerInputSource* source : m_input_sources){
29+
source->stop();
30+
}
31+
}
32+
void clear_state(){
33+
for (ControllerInputSource* source : m_input_sources){
34+
source->clear_state();
35+
}
36+
}
37+
void add_listener(ControllerInputListener& listener){
38+
for (ControllerInputSource* source : m_input_sources){
39+
source->add_listener(listener);
40+
}
41+
}
42+
void remove_listener(ControllerInputListener& listener){
43+
for (ControllerInputSource* source : m_input_sources){
44+
source->remove_listener(listener);
45+
}
46+
}
47+
48+
49+
private:
50+
std::vector<ControllerInputSource*> m_input_sources;
51+
};
52+
53+
54+
55+
56+
57+
58+
GlobalInputSources& global_input_sources(){
59+
static GlobalInputSources sources;
60+
return sources;
61+
}
62+
63+
64+
65+
66+
void global_input_stop(){
67+
global_input_sources().stop();
68+
}
69+
void global_input_clear_state(){
70+
global_input_sources().clear_state();
71+
}
72+
void global_input_add_listener(ControllerInputListener& listener){
73+
global_input_sources().add_listener(listener);
74+
}
75+
void global_input_remove_listener(ControllerInputListener& listener){
76+
global_input_sources().remove_listener(listener);
77+
}
78+
1379

1480

1581

SerialPrograms/Source/ControllerInput/ControllerInput.h

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define PokemonAutomation_ControllerInput_ControllerInput_H
99

1010
#include "Common/Compiler.h"
11+
#include "Common/Cpp/ListenerSet.h"
1112

1213
namespace PokemonAutomation{
1314

@@ -42,13 +43,69 @@ class ControllerInputState{
4243

4344

4445

45-
4646
struct ControllerInputListener{
4747
virtual void run_controller_input(ControllerInputState& state){}
4848
};
4949

5050

5151

5252

53+
54+
class ControllerInputSource{
55+
public:
56+
// This called before program shutdown. Child classes should shutdown any
57+
// threads they own to avoid static-deinit shenanigans.
58+
virtual void stop() = 0;
59+
60+
// User has switched away from the current console.
61+
// Release all buttons and reset back to neutral state.
62+
virtual void clear_state() = 0;
63+
64+
// These are called when controller inputs are enabled/disabled.
65+
void add_listener(ControllerInputListener& listener){
66+
m_listeners.add(listener);
67+
}
68+
void remove_listener(ControllerInputListener& listener){
69+
m_listeners.remove(listener);
70+
}
71+
72+
protected:
73+
ListenerSet<ControllerInputListener> m_listeners;
74+
};
75+
76+
77+
78+
// This is called at the end of main() before program shutdown. This allows all
79+
// inputs to end their threads to avoid static-deinit shenanigans.
80+
void global_input_stop();
81+
82+
// This is called whenever the user switches away from the current console.
83+
// Release all buttons and reset back to neutral state.
84+
void global_input_clear_state();
85+
86+
// These are called when controller inputs are enabled/disabled as a result
87+
// of switching to/from a console or starting/stopping a program.
88+
void global_input_add_listener(ControllerInputListener& listener);
89+
void global_input_remove_listener(ControllerInputListener& listener);
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
100+
53101
}
54102
#endif
103+
104+
105+
106+
107+
108+
109+
110+
111+

SerialPrograms/Source/ControllerInput/Keyboard/GlobalKeyboardHidTracker.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <set>
1111
#include <condition_variable>
12-
#include "Common/Cpp/ListenerSet.h"
1312
#include "Common/Cpp/Concurrency/SpinLock.h"
1413
#include "Common/Cpp/Concurrency/Thread.h"
1514
#include "CommonFramework/Logging/Logger.h"
@@ -23,23 +22,14 @@ class QKeyEvent;
2322
namespace PokemonAutomation{
2423

2524

26-
class KeyboardHidTracker{
27-
public:
28-
void add_listener(ControllerInputListener& listener){
29-
m_listeners.add(listener);
30-
}
31-
void remove_listener(ControllerInputListener& listener){
32-
m_listeners.remove(listener);
33-
}
34-
35-
25+
class KeyboardHidTracker : public ControllerInputSource{
3626
public:
3727
~KeyboardHidTracker();
3828
KeyboardHidTracker();
3929

40-
void stop();
30+
virtual void stop() override;
4131

42-
void clear_state();
32+
virtual void clear_state() override;
4333

4434
void on_key_press(const QKeyEvent& key);
4535
void on_key_release(const QKeyEvent& key);
@@ -62,8 +52,6 @@ class KeyboardHidTracker{
6252
std::mutex m_sleep_lock;
6353
std::condition_variable m_cv;
6454
Thread m_thread;
65-
66-
ListenerSet<ControllerInputListener> m_listeners;
6755
};
6856

6957

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "CommonFramework/Options/Environment/ThemeSelectorOption.h"
1111
#include "CommonFramework/Panels/ConsoleSettingsStretch.h"
1212
#include "CommonFramework/Recording/StreamHistoryOption.h"
13+
#include "ControllerInput/ControllerInput.h"
1314
#include "ControllerInput/Keyboard/GlobalKeyboardHidTracker.h"
1415
#include "NintendoSwitch_CommandRow.h"
1516

@@ -24,7 +25,7 @@ namespace NintendoSwitch{
2425

2526

2627
CommandRow::~CommandRow(){
27-
global_keyboard_tracker().remove_listener(*this);
28+
global_input_remove_listener(*this);
2829
m_controller.remove_listener(*this);
2930
m_session.remove_listener(*this);
3031
}
@@ -180,6 +181,10 @@ CommandRow::CommandRow(
180181
m_controller.add_listener(*this);
181182
}
182183

184+
185+
bool CommandRow::allow_controller_input() const{
186+
return m_allow_commands_while_running || m_last_known_state == ProgramState::STOPPED;
187+
}
183188
void CommandRow::run_controller_input(ControllerInputState& state){
184189
if (!m_last_known_focus){
185190
m_controller.logger().log("Keyboard Command Suppressed: Not in focus.", COLOR_RED);
@@ -190,23 +195,23 @@ void CommandRow::run_controller_input(ControllerInputState& state){
190195
m_controller.logger().log("Keyboard Command Suppressed: Controller is null.", COLOR_RED);
191196
return;
192197
}
193-
if (!m_allow_commands_while_running && m_last_known_state != ProgramState::STOPPED){
198+
if (!allow_controller_input()){
194199
m_controller.logger().log("Keyboard Command Suppressed: Program is running.", COLOR_RED);
195200
return;
196201
}
197202
controller->run_controller_input(state);
198203
}
199204
void CommandRow::set_focus(bool focused){
200205
if (focused){
201-
global_keyboard_tracker().add_listener(*this);
206+
if (allow_controller_input()){
207+
global_input_add_listener(*this);
208+
}
202209
}else{
203-
global_keyboard_tracker().clear_state();
204-
global_keyboard_tracker().remove_listener(*this);
210+
global_input_clear_state();
211+
global_input_remove_listener(*this);
205212

206213
AbstractController* controller = m_controller.controller();
207-
if (controller != nullptr &&
208-
(m_allow_commands_while_running || m_last_known_state == ProgramState::STOPPED)
209-
){
214+
if (controller != nullptr && allow_controller_input()){
210215
controller->cancel_all_commands();
211216
}
212217
}
@@ -268,14 +273,11 @@ void CommandRow::update_ui(){
268273

269274
void CommandRow::on_state_changed(ProgramState state){
270275
m_last_known_state = state;
271-
if (m_allow_commands_while_running || state == ProgramState::STOPPED){
272-
global_keyboard_tracker().clear_state();
273-
#if 0
274-
AbstractController* controller = m_controller.controller();
275-
if (controller != nullptr){
276-
controller->keyboard_release_all();
277-
}
278-
#endif
276+
if (allow_controller_input()){
277+
global_input_clear_state();
278+
global_input_add_listener(*this);
279+
}else{
280+
global_input_remove_listener(*this);
279281
}
280282
update_ui();
281283
}

SerialPrograms/Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class CommandRow :
5454
void on_state_changed(ProgramState state);
5555

5656
private:
57+
bool allow_controller_input() const;
58+
5759
virtual void run_controller_input(ControllerInputState& state) override;
5860

5961
virtual void on_overlay_enabled_stats (bool enabled) override;

0 commit comments

Comments
 (0)