diff --git a/src/eglapp.cpp b/src/eglapp.cpp index c685ade7..a3c427e5 100644 --- a/src/eglapp.cpp +++ b/src/eglapp.cpp @@ -24,9 +24,20 @@ # include #endif +#if defined USE_SDL +# if defined HAVE_SDL_SDL_H +# include +# else +# include +# endif +#endif + #include "core.h" #include "lolgl.h" #include "eglapp.h" +#if defined USE_SDL +# include "platform/sdl/sdlinput.h" +#endif namespace lol { @@ -239,6 +250,10 @@ EglApp::EglApp(char const *title, ivec2 res, float fps) : data->screen_size = ivec2(gwa.width, gwa.height); # endif +# if defined USE_SDL + new SdlInput(); +# endif + /* Initialise everything */ Ticker::Setup(fps); Video::Setup((ivec2)data->screen_size); diff --git a/src/platform/sdl/sdlinput.cpp b/src/platform/sdl/sdlinput.cpp index 32488123..b0700d8e 100644 --- a/src/platform/sdl/sdlinput.cpp +++ b/src/platform/sdl/sdlinput.cpp @@ -23,6 +23,10 @@ #include "core.h" #include "sdlinput.h" +/* We force joystick polling because no events are received when + * there is no SDL display (eg. on the Raspberry Pi). */ +#define SDL_FORCE_POLL_JOYSTICK 1 + namespace lol { @@ -53,9 +57,14 @@ SdlInput::SdlInput() #if defined USE_SDL SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK); +# if SDL_FORCE_POLL_JOYSTICK + SDL_JoystickEventState(SDL_QUERY); +# else + SDL_JoystickEventState(SDL_ENABLE); +# endif + /* Register all the joysticks we can find, and let the input * system decide what it wants to track. */ - SDL_JoystickEventState(SDL_ENABLE); for (int i = 0; i < SDL_NumJoysticks(); i++) { SDL_Joystick *sdlstick = SDL_JoystickOpen(i); @@ -126,6 +135,18 @@ void SdlInputData::Tick(float seconds) ivec2 mouse = SdlInputData::GetMousePos();; Input::SetMousePos(mouse); +# if SDL_FORCE_POLL_JOYSTICK + /* Pump all joystick events because no event is coming to us. */ + SDL_JoystickUpdate(); + for (int j = 0; j < m_joysticks.Count(); j++) + { + for (int i = 0; i < SDL_JoystickNumButtons(m_joysticks[j].m1); i++) + m_joysticks[j].m2->SetButton(i, SDL_JoystickGetButton(m_joysticks[j].m1, i)); + for (int i = 0; i < SDL_JoystickNumAxes(m_joysticks[j].m1); i++) + m_joysticks[j].m2->SetAxis(i, (float)SDL_JoystickGetAxis(m_joysticks[j].m1, i) / 32768.f); + } +# endif + /* Handle keyboard and WM events */ SDL_Event event; while (SDL_PollEvent(&event)) @@ -153,6 +174,7 @@ void SdlInputData::Tick(float seconds) break; } +# if !SDL_FORCE_POLL_JOYSTICK case SDL_JOYAXISMOTION: m_joysticks[event.jaxis.which].m2->SetAxis(event.jaxis.axis, (float)event.jaxis.value / 32768.f); break; @@ -161,6 +183,7 @@ void SdlInputData::Tick(float seconds) case SDL_JOYBUTTONDOWN: m_joysticks[event.jbutton.which].m2->SetButton(event.jbutton.button, event.jbutton.state); break; +# endif } }