@@ -15,11 +15,14 @@ namespace PokemonAutomation{
1515
1616
1717void ControllerSession::add_listener (Listener& listener){
18- std::lock_guard<std::mutex> lg0 (m_state_lock);
18+ std::lock_guard<std::mutex> lg (m_state_lock);
1919 m_listeners.add (listener);
20+ // if (m_connection && m_connection->is_ready()){
21+ signal_controller_changed (m_option.m_controller_type , m_available_controllers);
22+ // }
2023}
2124void ControllerSession::remove_listener (Listener& listener){
22- std::lock_guard<std::mutex> lg0 (m_state_lock);
25+ std::lock_guard<std::mutex> lg (m_state_lock);
2326 m_listeners.remove (listener);
2427}
2528
@@ -73,6 +76,10 @@ std::shared_ptr<const ControllerDescriptor> ControllerSession::descriptor() cons
7376 std::lock_guard<std::mutex> lg (m_state_lock);
7477 return m_descriptor;
7578}
79+ ControllerType ControllerSession::controller_type () const {
80+ std::lock_guard<std::mutex> lg (m_state_lock);
81+ return m_option.m_controller_type ;
82+ }
7683std::string ControllerSession::status_text () const {
7784 std::lock_guard<std::mutex> lg (m_state_lock);
7885 if (!m_connection){
@@ -157,7 +164,29 @@ bool ControllerSession::set_device(const std::shared_ptr<const ControllerDescrip
157164 m_option.m_descriptor = device;
158165 m_descriptor = device;
159166 }
160- signal_controller_changed (device);
167+ signal_descriptor_changed (device);
168+ signal_status_text_changed (status_text ());
169+ return true ;
170+ }
171+ bool ControllerSession::set_controller (ControllerType controller_type){
172+ // cout << "set_controller()" << endl;
173+ {
174+ std::lock_guard<std::mutex> lg (m_state_lock);
175+ if (m_option.m_controller_type == controller_type){
176+ return true ;
177+ }
178+ if (m_options_locked){
179+ return false ;
180+ }
181+ m_option.m_controller_type = controller_type;
182+
183+ m_controller.reset ();
184+ m_connection.reset ();
185+ m_connection = m_descriptor->open_connection (m_logger);
186+ if (m_connection){
187+ m_connection->add_status_listener (*this );
188+ }
189+ }
161190 signal_status_text_changed (status_text ());
162191 return true ;
163192}
@@ -190,34 +219,58 @@ void ControllerSession::pre_not_ready(){
190219 m_listeners.run_method_unique (&Listener::pre_not_ready);
191220}
192221void ControllerSession::post_ready (const std::map<ControllerType, std::set<ControllerFeature>>& controllers){
193- if (controllers.empty ()){
194- return ;
195- }
196-
197- auto iter = controllers.begin ();
198-
199- if (controllers.size () == 1 ){
200- // Only one controller available. Force the option to it.
201- m_option.m_controller_type = iter->first ;
202- }else {
203- // Otherwise, try to use the option setting. If it's not valid,
204- // clear it.
205- iter = controllers.find (m_option.m_controller_type );
206- if (iter == controllers.end ()){
207- m_option.m_controller_type = ControllerType::None;
208- m_controller.reset ();
222+ std::vector<ControllerType> available_controllers;
223+
224+ if (controllers.size () > 1 ){
225+ available_controllers.emplace_back (ControllerType::None);
226+ }
227+
228+ ControllerType selected_controller = ControllerType::None;
229+ bool ready = false ;
230+ {
231+ std::lock_guard<std::mutex> lg (m_state_lock);
232+
233+ for (const auto & item : controllers){
234+ available_controllers.emplace_back (item.first );
235+ }
236+ m_available_controllers = available_controllers;
237+ m_controller.reset ();
238+
239+ if (controllers.empty ()){
209240 return ;
210241 }
211- }
212242
213- // cout << "post_ready()" << endl;
243+ auto iter = controllers.begin ();
244+
245+ if (controllers.size () == 1 ){
246+ // Only one controller available. Force the option to it.
247+ selected_controller = iter->first ;
248+ }else {
249+ // Keep the current controller only if it exists.
250+ iter = controllers.find (m_option.m_controller_type );
251+ if (iter != controllers.end ()){
252+ selected_controller = m_option.m_controller_type ;
253+ }
254+ }
255+
256+ // cout << "post_ready()" << endl;
257+
258+ if (selected_controller != ControllerType::None){
259+ m_controller = m_descriptor->make_controller (m_logger, *m_connection, selected_controller, m_requirements);
260+ }
261+ m_option.m_controller_type = selected_controller;
214262
215- m_controller.reset ();
216- m_controller = m_descriptor->make_controller (m_logger, *m_connection, iter->first , m_requirements);
217- m_listeners.run_method_unique (&Listener::post_ready, controllers);
218- signal_ready_changed (m_controller->is_ready ());
263+ ready = m_controller && m_controller->is_ready ();
264+ signal_controller_changed (selected_controller, available_controllers);
265+ }
266+ if (ready){
267+ m_listeners.run_method_unique (&Listener::post_ready, controllers);
268+ signal_ready_changed (m_controller->is_ready ());
269+ }
219270}
220271void ControllerSession::post_status_text_changed (const std::string& text){
272+ std::lock_guard<std::mutex> lg (m_state_lock);
273+
221274 do {
222275 if (m_controller == nullptr ){
223276 break ;
@@ -239,8 +292,16 @@ void ControllerSession::post_status_text_changed(const std::string& text){
239292void ControllerSession::signal_ready_changed (bool ready){
240293 m_listeners.run_method_unique (&Listener::ready_changed, ready);
241294}
242- void ControllerSession::signal_controller_changed (const std::shared_ptr<const ControllerDescriptor>& descriptor){
243- m_listeners.run_method_unique (&Listener::controller_changed, descriptor);
295+ void ControllerSession::signal_descriptor_changed (
296+ const std::shared_ptr<const ControllerDescriptor>& descriptor
297+ ){
298+ m_listeners.run_method_unique (&Listener::descriptor_changed, descriptor);
299+ }
300+ void ControllerSession::signal_controller_changed (
301+ ControllerType controller_type,
302+ const std::vector<ControllerType>& available_controllers
303+ ){
304+ m_listeners.run_method_unique (&Listener::controller_changed, controller_type, available_controllers);
244305}
245306void ControllerSession::signal_status_text_changed (const std::string& text){
246307 m_listeners.run_method_unique (&Listener::post_status_text_changed, text);
0 commit comments