|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- //
- // Lol Engine
- //
- // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the Do What The Fuck You Want To
- // Public License, Version 2, as published by Sam Hocevar. See
- // http://www.wtfpl.net/ for more details.
- //
-
- #include <lol/engine-internal.h>
-
- #if USE_SDL
- # if HAVE_SDL_SDL_H
- # include <SDL/SDL.h>
- # else
- # include <SDL.h>
- # endif
- #endif
-
- #include "sdlinput.h"
-
- #include "input/input_internal.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
- #if EMSCRIPTEN
- #define MOUSE_SPEED_MOD 10.f
- #else
- #define MOUSE_SPEED_MOD 100.f
- #endif
-
- namespace lol
- {
-
- /*
- * SDL Input implementation class
- */
-
- class SdlInputData
- {
- friend class SdlInput;
-
- private:
- void Tick(float seconds);
-
- static ivec2 GetMousePos();
- static void SetMousePos(ivec2 position);
-
- #if USE_SDL
- SdlInputData(int app_w, int app_h, int screen_w, int screen_h) :
- m_prevmouse(ivec2::zero),
- m_app(vec2((float)app_w, (float)app_h)),
- m_screen(vec2((float)screen_w, (float)screen_h)),
- m_mousecapture(false)
- { }
-
- array<SDL_Joystick*, InputDeviceInternal*> m_joysticks;
- InputDeviceInternal* m_mouse;
- InputDeviceInternal* m_keyboard;
-
- ivec2 m_prevmouse;
- vec2 m_app;
- vec2 m_screen;
- bool m_mousecapture;
- #endif // USE_SDL
- };
-
- /*
- * Public SdlInput class
- */
-
- SdlInput::SdlInput(int app_w, int app_h, int screen_w, int screen_h)
- #if USE_SDL
- : m_data(new SdlInputData(app_w, app_h, screen_w, screen_h))
- #endif //USE_SDL
- {
- #if USE_SDL && !USE_SDL2
- /* Enable Unicode translation of keyboard events */
- SDL_EnableUNICODE(1);
- #endif
-
- #if USE_SDL
- SDL_Init(SDL_INIT_TIMER | SDL_INIT_JOYSTICK);
-
- m_data->m_keyboard = InputDeviceInternal::CreateStandardKeyboard();
- m_data->m_mouse = InputDeviceInternal::CreateStandardMouse();
-
- # if !EMSCRIPTEN
- # if SDL_FORCE_POLL_JOYSTICK
- SDL_JoystickEventState(SDL_QUERY);
- # else
- SDL_JoystickEventState(SDL_ENABLE);
- # endif //SDL_FORCE_POLL_JOYSTICK
-
- /* Register all the joysticks we can find, and let the input
- * system decide what it wants to track. */
- for (int i = 0; i < SDL_NumJoysticks(); i++)
- {
- SDL_Joystick *sdlstick = SDL_JoystickOpen(i);
-
- /* Blacklist some devices:
- * - HDAPS, it's not a real joystick.
- * - X360 controllers, Xinput handles them better since
- * it won't think there is only one trigger axis. */
- # if USE_SDL2
- char const *name = SDL_JoystickName(sdlstick);
- # else
- char const *name = SDL_JoystickName(i);
- # endif
- if (strstr(name, "HDAPS")
- # if USE_XINPUT
- || strstr(name, "XBOX 360 For Windows")
- # endif //USE_XINPUT
- || false)
- {
- SDL_JoystickClose(sdlstick);
- continue;
- }
-
- InputDeviceInternal* stick = new InputDeviceInternal(String::Printf("Joystick%d", i+1).C());
- for (int j = 0; j < SDL_JoystickNumAxes(sdlstick); ++j)
- stick->AddAxis(String::Printf("Axis%d", j + 1).C());
- for (int j = 0; j < SDL_JoystickNumButtons(sdlstick); ++j)
- stick->AddKey(String::Printf("Button%d", j + 1).C());
-
- m_data->m_joysticks.Push(sdlstick, stick);
- }
- # endif //EMSCRIPTEN
- #else
- UNUSED(app_w, app_h, screen_w, screen_h);
- #endif //USE_SDL
-
- m_gamegroup = GAMEGROUP_BEFORE;
- }
-
- SdlInput::~SdlInput()
- {
- #if USE_SDL && !EMSCRIPTEN
- /* Unregister all the joysticks we added */
- while (m_data->m_joysticks.Count())
- {
- SDL_JoystickClose(m_data->m_joysticks[0].m1);
- delete m_data->m_joysticks[0].m2;
- m_data->m_joysticks.Remove(0);
- }
- #endif
- delete m_data;
- }
-
- void SdlInput::TickGame(float seconds)
- {
- Entity::TickGame(seconds);
-
- #if !_WIN32
- m_data->Tick(seconds);
- #endif
- }
-
- void SdlInput::TickDraw(float seconds, Scene &scene)
- {
- Entity::TickDraw(seconds, scene);
-
- #if _WIN32
- m_data->Tick(seconds);
- #endif //_WIN32
- }
-
- void SdlInputData::Tick(float seconds)
- {
- #if USE_SDL
- /* Pump all joystick events because no event is coming to us. */
- # if SDL_FORCE_POLL_JOYSTICK && !EMSCRIPTEN
- 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->SetKey(i, SDL_JoystickGetButton(m_joysticks[j].m1, i) != 0);
- 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
-
- m_mouse->SetAxis(4, 0);
-
- /* Handle keyboard and WM events */
- SDL_Event event;
- while (SDL_PollEvent(&event))
- {
- switch (event.type)
- {
- case SDL_QUIT:
- Ticker::Shutdown();
- break;
- #if 0
- case SDL_KEYDOWN:
- if (event.key.keysym.unicode)
- fprintf(stderr, "%c (0x%04X)\n", event.key.keysym.unicode, event.key.keysym.unicode);
- break;
- #endif
- case SDL_MOUSEBUTTONDOWN:
- case SDL_MOUSEBUTTONUP:
- {
- # if !USE_SDL2
- if (event.button.button != SDL_BUTTON_WHEELUP && event.button.button != SDL_BUTTON_WHEELDOWN)
- m_mouse->SetKey(event.button.button - 1, event.type == SDL_MOUSEBUTTONDOWN);
- else
- m_mouse->SetAxis(4, (event.button.button != SDL_BUTTON_WHEELUP) ? (1) : (-1));
- # endif
- // TODO: mouse wheel as axis
- 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;
-
- case SDL_JOYBUTTONUP:
- case SDL_JOYBUTTONDOWN:
- m_joysticks[event.jbutton.which].m2->SetKey(event.jbutton.button, event.jbutton.state);
- break;
- # endif
- }
- }
-
- /* Handle mouse input */
- ivec2 mouse = SdlInputData::GetMousePos();
- if (InputDeviceInternal::GetMouseCapture() != m_mousecapture)
- {
- m_mousecapture = InputDeviceInternal::GetMouseCapture();
- # if USE_SDL2
- SDL_SetRelativeMouseMode(m_mousecapture ? SDL_TRUE : SDL_FALSE);
- # else
- SDL_WM_GrabInput(m_mousecapture ? SDL_GRAB_ON : SDL_GRAB_OFF);
- # endif
- mouse = (ivec2)m_app / 2;
- SdlInputData::SetMousePos(mouse);
- //SDL_ShowCursor(m_mousecapture ? SDL_DISABLE : SDL_ENABLE);
- }
-
- if (mouse.x >= 0 && mouse.x < m_app.x && mouse.y >= 0 && mouse.y < m_app.y)
- {
- //We need the max if we want coherent mouse speed between axis
- float max_screen_size = lol::max(m_screen.x, m_screen.y);
- vec2 vmouse = vec2(mouse);
- vec2 vprevmouse = vec2(m_prevmouse);
- m_mouse->SetCursor(0, vmouse / m_app, mouse);
- // Note: 100.0f is an arbitrary value that makes it feel about the same than an xbox controller joystick
- m_mouse->SetAxis(0, (mouse.x - vprevmouse.x) * MOUSE_SPEED_MOD / max_screen_size);
- // Y Axis is also negated to match the usual joystick Y axis (negatives values are for the upper direction)
- m_mouse->SetAxis(1,-(mouse.y - vprevmouse.y) * MOUSE_SPEED_MOD / max_screen_size);
- //Pixel movement
- m_mouse->SetAxis(2, (mouse.x - vprevmouse.x));
- m_mouse->SetAxis(3,-(mouse.y - vprevmouse.y));
- }
-
- //Mouse is focused, Validate the InScreen Key
- //Hardcoded 3, not very nice.
- # if !EMSCRIPTEN && !USE_SDL2
- m_mouse->SetKey(3, !!(SDL_GetAppState() & SDL_APPMOUSEFOCUS));
- #else
- // Emscripten doesn't seem to handle SDL_APPMOUSEFOCUS
- // SDL2 doesn't have SDL_APPMOUSEFOCUS either
- m_mouse->SetKey(3, true);
- #endif
-
- if (m_mousecapture)
- {
- mouse = ivec2(m_app * .5f);
- SdlInputData::SetMousePos(mouse);
- }
-
- m_prevmouse = mouse;
-
- # if USE_SDL2
- Uint8 const *sdlstate = SDL_GetKeyboardState(nullptr);
- # else
- Uint8 *sdlstate = SDL_GetKeyState(nullptr);
- # endif
-
- int keyindex = 0;
- # define KEY_FUNC(name, index) \
- m_keyboard->SetKey(keyindex++, sdlstate[index] != 0);
- /* FIXME: we ignore SDLK_WORLD_0, which means our list of
- * keys and SDL's list of keys could be out of sync. */
- # include "input/keys.h"
- # undef KEY_FUNC
-
- #else
- UNUSED(seconds);
- #endif //USE_SDL
- }
-
- // NOTE: these two functions are pointless now and could be inlined directly
- ivec2 SdlInputData::GetMousePos()
- {
- ivec2 ret(-1, -1);
-
- #if USE_SDL
- # if !EMSCRIPTEN && !USE_SDL2
- if (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
- # endif
- {
- SDL_GetMouseState(&ret.x, &ret.y);
- ret.y = Video::GetSize().y - 1 - ret.y;
- }
- #endif //USE_SDL
- return ret;
- }
-
- void SdlInputData::SetMousePos(ivec2 position)
- {
- #if USE_SDL2
- // FIXME: how do I warped mouse?
- #elif USE_SDL
- SDL_WarpMouse((uint16_t)position.x, (uint16_t)position.y);
- #else
- UNUSED(position);
- #endif //USE_SDL
- }
-
- } /* namespace lol */
-
|