-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathControllerSession.cpp
More file actions
506 lines (425 loc) · 16.1 KB
/
Copy pathControllerSession.cpp
File metadata and controls
506 lines (425 loc) · 16.1 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* Controller Interface
*
* From: https://github.com/PokemonAutomation/
*
*/
//#include "Common/Cpp/Exceptions.h"
//#include "ControllerTypeStrings.h"
#include "ControllerSession.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
void ControllerSession::add_listener(Listener& listener){
// WriteSpinLock lg(m_state_lock);
m_listeners.add(listener);
// if (m_connection && m_connection->is_ready()){
// signal_controller_changed(m_option.m_controller_type, m_available_controllers);
// }
}
void ControllerSession::remove_listener(Listener& listener){
// WriteSpinLock lg(m_state_lock);
m_listeners.remove(listener);
}
ControllerSession::~ControllerSession(){
std::unique_ptr<AbstractController> controller;
std::unique_ptr<ControllerConnection> connection;
{
WriteSpinLock lg(m_state_lock);
controller = std::move(m_controller);
connection = std::move(m_connection);
}
controller.reset();
connection.reset();
}
ControllerSession::ControllerSession(
Logger& logger,
ControllerOption& option,
std::optional<size_t> index
)
: m_logger(logger)
, m_option(option)
, m_index(index)
, m_options_locked(false)
, m_descriptor(option.descriptor())
, m_connection(m_descriptor->open_connection(logger))
{
if (!m_connection){
return;
}
// Add listener first so we don't miss the ready signal.
m_connection->add_status_listener(*this);
try{
// If we already missed it, run it ourselves.
if (m_connection->is_ready()){
// cout << "ControllerSession::ControllerSession() - early ready" << endl;
m_change_controller_on_ready = m_connection->current_controller();
ControllerSession::post_connection_ready(*m_connection);
}
}catch (...){
m_connection->remove_status_listener(*this);
throw;
}
}
std::vector<ControllerType> ControllerSession::available_controllers() const{
ReadSpinLock lg(m_state_lock);
if (!m_connection || !m_connection->is_ready()){
return {};
}
return m_connection->controller_list();
}
void ControllerSession::save(ControllerOption& option) const{
ReadSpinLock lg(m_state_lock);
option = m_option;
}
void ControllerSession::load(const ControllerOption& option){
set_device(option.descriptor());
}
bool ControllerSession::input_enabled() const{
ReadSpinLock lg(m_state_lock);
return m_option.m_enable_input;
}
void ControllerSession::set_input_enabled(bool enabled){
WriteSpinLock lg(m_state_lock);
m_option.m_enable_input = enabled;
}
bool ControllerSession::ready() const{
ReadSpinLock lg(m_state_lock);
if (!m_controller){
return false;
}
return m_controller->is_ready();
}
ControllerConnection::Status ControllerSession::connection_status() const{
ReadSpinLock lg(m_state_lock);
if (!m_connection){
return ControllerConnection::Status::NOT_CONNECTED;
}
return m_connection->status();
}
std::shared_ptr<ControllerDescriptor> ControllerSession::descriptor() const{
ReadSpinLock lg(m_state_lock);
return m_descriptor;
}
ControllerClass ControllerSession::controller_class() const noexcept{
ReadSpinLock lg(m_state_lock);
if (!m_controller){
return ControllerClass::None;
}
return m_controller->controller_class();
}
ControllerType ControllerSession::controller_type() const noexcept{
ReadSpinLock lg(m_state_lock);
if (!m_connection){
return ControllerType::None;
}
return m_connection->current_controller();
}
std::string ControllerSession::status_text() const{
ReadSpinLock lg(m_state_lock);
if (!m_connection){
return "<font color=\"red\">No controller selected.</font>";
}
return m_connection->status_text();
}
AbstractController* ControllerSession::controller() const{
return m_controller.get();
}
std::string ControllerSession::user_input_blocked() const{
ReadSpinLock lg(m_state_lock);
if (!m_connection){
return "<font color=\"red\">No controller selected.</font>";
}
if (!m_connection->is_ready()){
return "<font color=\"red\">Connection is not ready.</font>";
}
if (!m_controller->is_ready()){
return "<font color=\"red\">Controller is not ready.</font>";
}
return m_user_input_disallow_reason;
}
void ControllerSession::set_user_input_blocked(std::string disallow_reason){
WriteSpinLock lg(m_state_lock);
// cout << "set_user_input_blocked() = " << disallow_reason << endl;
m_user_input_disallow_reason = std::move(disallow_reason);
}
bool ControllerSession::options_locked() const{
ReadSpinLock lg(m_state_lock);
return m_options_locked;
}
void ControllerSession::set_options_locked(bool locked){
bool original_value;
{
WriteSpinLock lg(m_state_lock);
original_value = m_options_locked;
m_options_locked = locked;
}
if (original_value != locked){
signal_options_locked(locked);
}
}
void ControllerSession::make_controller(
std::optional<ControllerType> change_controller,
bool clear_settings
){
// cout << "ControllerSession::make_controller()" << endl;
// Must be called under "m_reset_lock".
bool ready = false;
{
WriteSpinLock lg(m_state_lock);
// cout << "clear_settings = " << clear_settings << endl;
m_connection = m_descriptor->open_connection(m_logger);
if (m_connection){
m_connection->add_status_listener(*this);
ready = m_connection->is_ready();
}
}
m_change_controller_on_ready = change_controller;
// If we already missed it, run it ourselves.
if (ready){
ControllerSession::post_connection_ready(*m_connection);
}
signal_descriptor_changed(m_descriptor);
}
bool ControllerSession::set_interface(ControllerInterface controller_interface){
std::shared_ptr<const ControllerDescriptor> device;
{
std::lock_guard<Mutex> lg0(m_reset_lock);
// Destroy the current connection+controller.
std::unique_ptr<AbstractController> controller;
std::unique_ptr<ControllerConnection> connection;
{
WriteSpinLock lg1(m_state_lock);
if (m_options_locked){
return false;
}
if (controller_interface == m_descriptor->interface_type){
return true;
}
// Move these out to indicate that we should no longer access them.
controller = std::move(m_controller);
connection = std::move(m_connection);
m_option.set_interface(controller_interface);
m_descriptor = m_option.descriptor();
}
// With the lock released, it is now safe to destroy them.
// We cannot destroy these under (m_state_lock) due to their asynchronous
// callbacks into this class which will also acquire the same lock.
controller.reset();
connection.reset();
make_controller({}, false);
}
// cout << "ControllerSession::set_interface() - signal"<< endl;
signal_descriptor_changed(device);
signal_status_text_changed(status_text());
return true;
}
bool ControllerSession::set_device(const std::shared_ptr<ControllerDescriptor>& device){
// cout << "ControllerSession::set_device() = " << device->display_name() << endl;
{
std::lock_guard<Mutex> lg0(m_reset_lock);
// Destroy the current connection+controller.
std::unique_ptr<AbstractController> controller;
std::unique_ptr<ControllerConnection> connection;
{
WriteSpinLock lg1(m_state_lock);
if (m_options_locked){
return false;
}
if (*m_descriptor == *device){
return true;
}
// Move these out to indicate that we should no longer access them.
controller = std::move(m_controller);
connection = std::move(m_connection);
// cout << "setting..." << endl;
m_option.set_descriptor(device);
m_descriptor = device;
}
// With the lock released, it is now safe to destroy them.
// We cannot destroy these under (m_state_lock) due to their asynchronous
// callbacks into this class which will also acquire the same lock.
controller.reset();
connection.reset();
make_controller({}, false);
}
// cout << "ControllerSession::set_device() - signal"<< endl;
signal_descriptor_changed(device);
signal_status_text_changed(status_text());
return true;
}
bool ControllerSession::set_controller(ControllerType controller_type){
// cout << "ControllerSession::set_controller()" << endl;
std::shared_ptr<const ControllerDescriptor> device;
{
std::lock_guard<Mutex> lg0(m_reset_lock);
// Destroy the current connection+controller.
std::unique_ptr<AbstractController> controller;
std::unique_ptr<ControllerConnection> connection;
{
WriteSpinLock lg1(m_state_lock);
if (m_options_locked){
return false;
}
if (m_connection){
// cout << (int)m_connection->current_controller() << endl;
if (m_connection->current_controller() == controller_type){
return true;
}
m_connection->try_set_controller_type(controller_type, false);
}
// Move these out to indicate that we should no longer access them.
controller = std::move(m_controller);
connection = std::move(m_connection);
}
// With the lock released, it is now safe to destroy them.
// We cannot destroy these under (m_state_lock) due to their asynchronous
// callbacks into this class which will also acquire the same lock.
controller.reset();
connection.reset();
make_controller(controller_type, false);
device = m_descriptor;
}
signal_descriptor_changed(device);
signal_status_text_changed(status_text());
return true;
}
std::string ControllerSession::reset(bool clear_settings){
// cout << "ControllerSession::reset()" << endl;
{
std::lock_guard<Mutex> lg0(m_reset_lock);
// std::optional<ControllerType> change_controller;
// Destroy the current connection+controller.
std::unique_ptr<AbstractController> controller;
std::unique_ptr<ControllerConnection> connection;
{
WriteSpinLock lg1(m_state_lock);
if (m_options_locked){
return "Options are locked.";
}
// if (!m_connection){
// cout << "ControllerSession::reset() - early return" << endl;
// return "No connection set.";
// }
// cout << "Checking readiness... " << (int)m_desired_controller << endl;
if (m_connection && m_connection->is_ready()){
m_connection->try_set_controller_type(m_connection->current_controller(), clear_settings);
}
// Move these out to indicate that we should no longer access them.
controller = std::move(m_controller);
connection = std::move(m_connection);
}
// With the lock released, it is now safe to destroy them.
// We cannot destroy these under (m_state_lock) due to their asynchronous
// callbacks into this class which will also acquire the same lock.
controller.reset();
connection.reset();
make_controller({}, clear_settings);
}
signal_status_text_changed(status_text());
return "";
}
//void ControllerSession::pre_connection_not_ready(ControllerConnection& connection){
// m_listeners.run_method(&Listener::pre_connection_not_ready, connection);
//}
void ControllerSession::post_connection_ready(ControllerConnection& connection){
// cout << "ControllerSession::post_connection_ready()" << endl;
// ControllerType current_controller = mode_status.current_controller;
// cout << "sleeping" << endl;
// Sleep(10000);
std::vector<ControllerType> supported_controllers;
ControllerType current_controller = ControllerType::None;
ControllerType controller_type;
std::unique_ptr<AbstractController> controller;
bool ready;
{
ReadSpinLock lg(m_state_lock);
// Connection has already been closed.
if (m_connection == nullptr){
return;
}
// Controller already constructed.
if (m_controller){
return;
}
if (m_change_controller_on_ready.has_value()){
controller_type = m_change_controller_on_ready.value();
}else{
controller_type = m_connection->current_controller();
}
supported_controllers = m_connection->controller_list();
current_controller = m_connection->current_controller();
}
signal_controller_changed(current_controller, supported_controllers);
// Construct the controller.
if (controller_type != ControllerType::None){
controller = m_descriptor->make_controller(
m_logger,
connection,
controller_type
);
}
{
WriteSpinLock lg(m_state_lock);
// Connection has been wiped. ABA is safe here.
if (m_connection.get() != &connection){
return;
}
m_controller = std::move(controller);
m_change_controller_on_ready.reset();
supported_controllers = m_connection->controller_list();
current_controller = m_connection->current_controller();
// Commit all changes.
// cout << "current_controller = " << CONTROLLER_TYPE_STRINGS.get_string(current_controller) << endl;
ready = m_controller && m_controller->is_ready();
}
signal_controller_changed(current_controller, supported_controllers);
signal_ready_changed(ready);
signal_status_text_changed(status_text());
}
void ControllerSession::status_text_changed(
ControllerConnection& connection,
const std::string& text
){
// cout << "ControllerSession::status_text_changed() = " << text << endl;
std::string controller_error;
{
WriteSpinLock lg(m_message_lock);
controller_error = m_controller_error;
}
// cout << "ControllerSession::status_text_changed(): controller_error = " << controller_error << endl;
if (controller_error.empty()){
m_listeners.run_method(&Listener::post_status_text_changed, text);
}else{
m_listeners.run_method(&Listener::post_status_text_changed, controller_error);
}
}
void ControllerSession::on_error(
ControllerConnection& connection,
const std::string& text
){
WriteSpinLock lg(m_message_lock);
m_controller_error = text;
}
void ControllerSession::signal_ready_changed(bool ready){
m_listeners.run_method(&Listener::ready_changed, ready);
}
void ControllerSession::signal_descriptor_changed(
const std::shared_ptr<const ControllerDescriptor>& descriptor
){
m_listeners.run_method(&Listener::descriptor_changed, descriptor);
}
void ControllerSession::signal_controller_changed(
ControllerType controller_type,
const std::vector<ControllerType>& available_controllers
){
m_listeners.run_method(&Listener::controller_changed, controller_type, available_controllers);
}
void ControllerSession::signal_status_text_changed(const std::string& text){
// cout << text << endl;
m_listeners.run_method(&Listener::post_status_text_changed, text);
}
void ControllerSession::signal_options_locked(bool locked){
m_listeners.run_method(&Listener::options_locked, locked);
}
}