@@ -33,6 +33,8 @@ void RenderLoop::Run()
3333
3434 _projectMWrapper.DisplayInitialPreset ();
3535
36+ _controller = _sdlRenderingWindow.FindController ();
37+
3638 while (!_wantsToQuit)
3739 {
3840 limiter.TargetFPS (_projectMWrapper.TargetFPS ());
@@ -52,6 +54,10 @@ void RenderLoop::Run()
5254 _projectMWrapper.UpdateRealFPS (limiter.FPS ());
5355 }
5456
57+ // Close game controller
58+ SDL_GameControllerClose (_controller);
59+ _controller = nullptr ;
60+
5561 notificationCenter.removeObserver (_quitNotificationObserver);
5662
5763 projectm_playlist_set_preset_switched_event_callback (_playlistHandle, nullptr , nullptr );
@@ -106,6 +112,26 @@ void RenderLoop::PollEvents()
106112
107113 break ;
108114
115+ case SDL_CONTROLLERDEVICEADDED:
116+ ControllerAdd (event.cdevice .which );
117+
118+ break ;
119+
120+ case SDL_CONTROLLERDEVICEREMOVED:
121+ ControllerRemove (event.cdevice .which );
122+
123+ break ;
124+
125+ case SDL_CONTROLLERBUTTONDOWN:
126+ ControllerDownEvent (event);
127+
128+ break ;
129+
130+ case SDL_CONTROLLERBUTTONUP:
131+ ControllerUpEvent (event);
132+
133+ break ;
134+
109135 case SDL_DROPFILE: {
110136 char * droppedFilePath = event.drop .file ;
111137
@@ -415,6 +441,110 @@ void RenderLoop::MouseUpEvent(const SDL_MouseButtonEvent& event)
415441 }
416442}
417443
444+ void RenderLoop::ControllerAdd (const int id )
445+ {
446+ if (!_controller)
447+ {
448+ _controller = SDL_GameControllerOpen (id);
449+ }
450+ poco_debug (_logger, " Controller added!" );
451+ }
452+
453+ void RenderLoop::ControllerRemove (const int id )
454+ {
455+ if (_controller && id == SDL_JoystickInstanceID (SDL_GameControllerGetJoystick (_controller)))
456+ {
457+ SDL_GameControllerClose (_controller);
458+ _controller = _sdlRenderingWindow.FindController ();
459+ }
460+ poco_debug (_logger, " Controller removed!" );
461+ }
462+
463+ void RenderLoop::ControllerDownEvent (const SDL_Event& event)
464+ {
465+ if (!_controller || event.cdevice .which != SDL_JoystickInstanceID (SDL_GameControllerGetJoystick (_controller)))
466+ {
467+ poco_debug (_logger, " No controller initialized" );
468+ return ;
469+ }
470+
471+ switch (event.cbutton .button )
472+ {
473+ case SDL_CONTROLLER_BUTTON_A:
474+ _sdlRenderingWindow.ToggleFullscreen ();
475+ poco_debug (_logger, " A pressed!" );
476+ break ;
477+
478+ case SDL_CONTROLLER_BUTTON_B:
479+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::RandomPreset, _keyStates._shiftPressed ));
480+ poco_debug (_logger, " B pressed!" );
481+ break ;
482+
483+ case SDL_CONTROLLER_BUTTON_X:
484+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::TogglePresetLocked));
485+ poco_debug (_logger, " X pressed!" );
486+ break ;
487+
488+ case SDL_CONTROLLER_BUTTON_Y:
489+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::ToggleShuffle));
490+ poco_debug (_logger, " Y pressed!" );
491+ break ;
492+
493+ case SDL_CONTROLLER_BUTTON_BACK:
494+ _wantsToQuit = true ;
495+ poco_debug (_logger, " Back pressed!" );
496+ break ;
497+
498+ case SDL_CONTROLLER_BUTTON_GUIDE:
499+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::LastPreset, _keyStates._shiftPressed ));
500+ poco_debug (_logger, " Guide pressed!" );
501+ break ;
502+
503+ case SDL_CONTROLLER_BUTTON_START:
504+ _projectMGui.Toggle ();
505+ _sdlRenderingWindow.ShowCursor (_projectMGui.Visible ());
506+ poco_debug (_logger, " Start pressed!" );
507+ break ;
508+
509+ case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
510+ _audioCapture.NextAudioDevice ();
511+ poco_debug (_logger, " Shoulder left pressed!" );
512+ break ;
513+
514+ case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
515+ _sdlRenderingWindow.NextDisplay ();
516+ poco_debug (_logger, " Shoulder right pressed!" );
517+ break ;
518+
519+ case SDL_CONTROLLER_BUTTON_DPAD_UP:
520+ // Increase beat sensitivity
521+ _projectMWrapper.ChangeBeatSensitivity (0 .05f );
522+ poco_debug (_logger, " DPad up pressed!" );
523+ break ;
524+
525+ case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
526+ // Decrease beat sensitivity
527+ _projectMWrapper.ChangeBeatSensitivity (-0 .05f );
528+ poco_debug (_logger, " DPad down pressed!" );
529+ break ;
530+
531+ case SDL_CONTROLLER_BUTTON_DPAD_LEFT:
532+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::PreviousPreset, _keyStates._shiftPressed ));
533+ poco_debug (_logger, " DPad left pressed!" );
534+ break ;
535+
536+ case SDL_CONTROLLER_BUTTON_DPAD_RIGHT:
537+ Poco::NotificationCenter::defaultCenter ().postNotification (new PlaybackControlNotification (PlaybackControlNotification::Action::NextPreset, _keyStates._shiftPressed ));
538+ poco_debug (_logger, " DPad right pressed!" );
539+ break ;
540+ }
541+ }
542+
543+ void RenderLoop::ControllerUpEvent (const SDL_Event& event)
544+ {
545+
546+ }
547+
418548void RenderLoop::QuitNotificationHandler (const Poco::AutoPtr<QuitNotification>& notification)
419549{
420550 _wantsToQuit = true ;
0 commit comments