-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathConsoleSystemSession.cpp
More file actions
266 lines (225 loc) · 8.33 KB
/
Copy pathConsoleSystemSession.cpp
File metadata and controls
266 lines (225 loc) · 8.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* Console System Session
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/ColoredText.h"
#include "Common/Cpp/EarlyShutdown.h"
#include "Common/Cpp/Containers/FixedLimitVector.tpp"
#include "CommonFramework/VideoPipeline/Stats/MemoryUtilizationStats.h"
#include "CommonFramework/VideoPipeline/Stats/CpuUtilizationStats.h"
#include "CommonFramework/VideoPipeline/Stats/ThreadUtilizationStats.h"
#include "Integrations/ProgramTracker.h"
#include "ConsoleSystemSession.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace GameConsole{
bool ConsoleSystemSession::try_shutdown() noexcept{
bool success = true;
global_input_remove_listener(*this);
m_video.remove_state_listener(m_history);
m_video.remove_state_listener(m_history);
m_audio.remove_stream_listener(m_history);
m_audio.remove_state_listener(m_history);
m_overlay.remove_stat(*m_main_thread_utilization);
m_overlay.remove_stat(*m_cpu_utilization);
m_overlay.remove_stat(m_memory_usage->m_process);
m_overlay.remove_stat(m_memory_usage->m_system);
success &= m_video.try_shutdown();
return success;
}
ConsoleSystemSession::~ConsoleSystemSession(){
ProgramTracker::instance().remove_console(m_console_tracking_id);
blocking_shutdown(
m_logger,
"ConsoleSystemSession",
[this]{ return ConsoleSystemSession::try_shutdown(); }
);
}
ConsoleSystemSession::ConsoleSystemSession(
Logger& logger,
ConsoleSystemOption& option,
bool allow_commands_while_locked,
size_t console_number,
std::optional<uint64_t> program_tracking_id
)
: m_allow_commands_while_locked(allow_commands_while_locked)
, m_console_number(console_number)
, m_logger(logger, "Console " + std::to_string(console_number))
, m_option(option)
, m_video(m_logger, option.m_video)
, m_audio(m_logger, option.m_audio)
, m_overlay(m_logger, option.m_overlay)
, m_controllers(option.m_controllers.size())
, m_history(m_logger)
, m_memory_usage(new MemoryUtilizationStats())
, m_cpu_utilization(new CpuUtilizationStat())
, m_main_thread_utilization(new ThreadUtilizationStat(current_thread_handle(), "Main Qt Thread:"))
{
if (option.m_controllers.size() == 1){
m_controllers.emplace_back(m_logger, option.m_controllers[0]);
}else{
for (ControllerOption& controller : option.m_controllers){
m_controllers.emplace_back(m_logger, m_controllers.size(), controller);
}
}
update_status();
m_history.start(m_audio.input_format(), m_video.current_source() != nullptr);
try{
m_overlay.add_stat(m_memory_usage->m_system);
m_overlay.add_stat(m_memory_usage->m_process);
m_overlay.add_stat(*m_cpu_utilization);
m_overlay.add_stat(*m_main_thread_utilization);
m_audio.add_state_listener(m_history);
m_audio.add_stream_listener(m_history);
m_video.add_state_listener(m_history);
m_video.add_frame_listener(m_history);
m_overlay.add_hid_listener(*this);
}catch (...){
ConsoleSystemSession::try_shutdown();
throw;
}
m_console_tracking_id = ProgramTracker::instance().add_console(program_tracking_id, *this);
}
std::string ConsoleSystemSession::status() const{
std::lock_guard<Mutex> lg(m_lock);
return m_status_text;
}
JsonValue ConsoleSystemSession::to_json() const{
std::lock_guard<Mutex> lg(m_lock);
return m_option.to_json();
}
void ConsoleSystemSession::load_json(const JsonValue& json){
std::lock_guard<Mutex> lg(m_lock);
m_option.load_json(json);
m_video.load(m_option.m_video);
m_audio.load(m_option.m_audio);
m_overlay.load(m_option.m_overlay);
size_t c = 0;
size_t stop = m_option.m_controllers.size();
for (; c < stop; c++){
m_controllers[c].session.load(m_option.m_controllers[c]);
}
}
void ConsoleSystemSession::lock_controllers(std::string reason){
std::string status;
{
std::lock_guard<Mutex> lg(m_lock);
m_lock_controllers_reason = std::move(reason);
for (ControllerEntry& controller : m_controllers){
controller.session.set_options_locked(true);
}
status = update_status();
}
m_listeners.run_method(&Listener::on_lock_controllers);
m_listeners.run_method(&Listener::on_input_status_change, status);
}
void ConsoleSystemSession::unlock_controllers(){
std::string status;
{
std::lock_guard<Mutex> lg(m_lock);
m_lock_controllers_reason.clear();
global_input_clear_state();
for (ControllerEntry& controller : m_controllers){
controller.session.set_options_locked(false);
}
status = update_status();
}
m_listeners.run_method(&Listener::on_unlock_controllers);
m_listeners.run_method(&Listener::on_input_status_change, status);
}
void ConsoleSystemSession::save_history(const std::string& filename){
m_history.save(filename);
}
std::string ConsoleSystemSession::update_status(){
// Must call under the lock.
// No controllers are ready.
bool ok = false;
for (ControllerEntry& controller : m_controllers){
if (controller.session.ready()){
ok = true;
break;
}
}
if (!ok){
m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_RED);
return m_status_text;
}
// Controllers are locked. (program is probably running)
if (!m_lock_controllers_reason.empty() && !allow_commands_while_locked()){
m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_PURPLE);
return m_status_text;
}
// Not focused.
if (!m_focused){
m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_ORANGE);
return m_status_text;
}
// Ready.
m_status_text = "Keyboard: " + html_color_text("⬤", COLOR_DARKGREEN);
return m_status_text;
}
void ConsoleSystemSession::on_focus_in(){
// cout << "ConsoleSystemSession::on_focus_in()" << endl;
std::string status;
{
std::lock_guard<Mutex> lg(m_lock);
if (m_focused){
return;
}
m_focused = true;
global_input_add_listener(*this);
status = update_status();
}
m_listeners.run_method(&Listener::on_input_status_change, status);
}
void ConsoleSystemSession::on_focus_out(){
// cout << "ConsoleSystemSession::on_focus_out()" << endl;
std::string status;
{
std::lock_guard<Mutex> lg(m_lock);
if (!m_focused){
return;
}
m_focused = false;
if (!m_lock_controllers_reason.empty() && !allow_commands_while_locked()){
return;
}
global_input_clear_state();
global_input_remove_listener(*this);
for (ControllerEntry& controller : m_controllers){
std::string error = controller.session.try_run<AbstractController>([](AbstractController& controller){
controller.cancel_all_commands();
});
if (!error.empty()){
controller.session.logger().log(error, COLOR_RED);
}
}
status = update_status();
}
m_listeners.run_method(&Listener::on_input_status_change, status);
}
void ConsoleSystemSession::run_controller_input(ControllerInputState& state){
std::lock_guard<Mutex> lg(m_lock);
if (!m_focused){
m_logger.log("Keyboard Command Suppressed: Not in focus.", COLOR_RED);
return;
}
if (!m_lock_controllers_reason.empty() && !allow_commands_while_locked()){
m_logger.log("Keyboard Command Suppressed: " + m_lock_controllers_reason, COLOR_RED);
return;
}
// cout << "ConsoleSystemSession::run_controller_input()" << endl;
for (ControllerEntry& controller : m_controllers){
std::string error = controller.session.try_run<AbstractController>([&](AbstractController& controller){
controller.run_controller_input(state);
});
if (!error.empty()){
controller.session.logger().log("Keyboard Command Failed: " + error, COLOR_RED);
}
}
}
}
}