@@ -35,6 +35,8 @@ void SDLRenderingWindow::initialize(Poco::Util::Application& app)
3535 // Observe user configuration changes (set via the settings window)
3636 _userConfig->propertyChanged += Poco::delegate (this , &SDLRenderingWindow::OnConfigurationPropertyChanged);
3737 _userConfig->propertyRemoved += Poco::delegate (this , &SDLRenderingWindow::OnConfigurationPropertyRemoved);
38+
39+ _controller = FindController ();
3840}
3941
4042void SDLRenderingWindow::uninitialize ()
@@ -48,6 +50,12 @@ void SDLRenderingWindow::uninitialize()
4850 DestroySDLWindow ();
4951 _renderingWindow = nullptr ;
5052 }
53+
54+ if (_controller)
55+ {
56+ SDL_GameControllerClose (_controller);
57+ _controller = nullptr ;
58+ }
5159}
5260
5361void SDLRenderingWindow::GetDrawableSize (int & width, int & height) const
@@ -467,15 +475,63 @@ SDL_GameController* SDLRenderingWindow::FindController() {
467475 if ( SDL_NumJoysticks () < 1 )
468476 {
469477 poco_debug (_logger, " No joysticks connected" );
478+ return nullptr ;
470479 }
471480
472481 // For simplicity, we’ll only be setting up and tracking a single
473482 // controller at a time
474483 for (int i = 0 ; i < SDL_NumJoysticks (); i++) {
475484 if (SDL_IsGameController (i)) {
485+ poco_debug (_logger, " Adding first controller" );
476486 return SDL_GameControllerOpen (i);
477487 }
488+ else {
489+ poco_debug (_logger, " Connected joystick is not a SDL game controller" );
490+ }
478491 }
479492
480493 return nullptr ;
481494}
495+
496+ void SDLRenderingWindow::ControllerAdd (const int id )
497+ {
498+ if (!_controller)
499+ {
500+ if (SDL_IsGameController (id)) {
501+ _controller = SDL_GameControllerOpen (id);
502+ poco_debug (_logger, " Controller added!" );
503+ }
504+ else {
505+ poco_debug (_logger, " Connected joystick is not a SDL game controller" );
506+ }
507+ }
508+ }
509+
510+ void SDLRenderingWindow::ControllerRemove (const int id )
511+ {
512+ if (_controller && id == SDL_JoystickInstanceID (SDL_GameControllerGetJoystick (_controller)))
513+ {
514+ SDL_GameControllerClose (_controller);
515+ poco_debug (_logger, " Controller removed!" );
516+ _controller = FindController ();
517+ }
518+ }
519+
520+ bool SDLRenderingWindow::ControllerIsOurs (const int id )
521+ {
522+ int instance = SDL_JoystickInstanceID (SDL_GameControllerGetJoystick (_controller));
523+
524+ if (!_controller )
525+ {
526+ poco_debug (_logger, " No controller initialized" );
527+ return false ;
528+ }
529+
530+ if (id != instance)
531+ {
532+ poco_debug_f2 (_logger, " Use controller %?d instead of %?d. Currently only one controller is supported" , instance, id);
533+ return false ;
534+ }
535+
536+ return true ;
537+ }
0 commit comments