/* client/joystick.cc This file is part of the Osirion project and is distributed under the terms and conditions of the GNU General Public License version 2 */ #include "SDL2/SDL.h" #include "auxiliary/functions.h" #include "client/joystick.h" #include "sys/sys.h" namespace client { // configured joystick number core::Cvar *Joystick::input_joystick = 0; // joystick axes core::Cvar *Joystick::input_axisdirection = 0; core::Cvar *Joystick::input_axispitch = 0; core::Cvar *Joystick::input_axisroll = 0; core::Cvar *Joystick::input_axisthrottle = 0; // current joystick number int current_joystick_number = 0; SDL_Joystick *current_joystick = 0; void Joystick::init() { SDL_InitSubSystem(SDL_INIT_JOYSTICK); int nbjoysticks = SDL_NumJoysticks(); if (nbjoysticks) { for (int i = 0; i < nbjoysticks; i++) { SDL_Joystick *joystick = SDL_JoystickOpen(i); if (joystick) { con_print << " joystick " << i + 1 << ": " << SDL_JoystickName(joystick) << " " << SDL_JoystickNumAxes(joystick) << " axes " << SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; SDL_JoystickClose(joystick); } } } input_joystick = core::Cvar::get("input_joystick", 0.0f, core::Cvar::Archive); input_joystick->set_info("[int] set joystick number to use"); input_axisdirection = core::Cvar::get("input_axisdirection", 1.0f, core::Cvar::Archive); input_axisdirection->set_info("[int] joystick axis for direction control, 0 disables"); input_axispitch = core::Cvar::get("input_axispitch", 2.0f, core::Cvar::Archive); input_axispitch->set_info("[int] joystick axis for pitch control, 0 disables"); input_axisroll = core::Cvar::get("input_axisroll", 3.0f, core::Cvar::Archive); input_axisroll->set_info("[int] joystick axis for roll control, 0 disables"); input_axisthrottle = core::Cvar::get("input_axisthrottle", 4.0f, core::Cvar::Archive); input_axisthrottle->set_info("[int] joystick axis for throttle control, 0 disables"); current_joystick_number = 0; SDL_JoystickEventState(SDL_IGNORE); } void Joystick::shutdown() { if (current_joystick) { SDL_JoystickEventState(SDL_IGNORE); SDL_JoystickClose(current_joystick); current_joystick = 0; } SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } void Joystick::list() { if (current_joystick) { SDL_JoystickEventState(SDL_IGNORE); SDL_JoystickClose(current_joystick); current_joystick = 0; } int nbjoysticks = SDL_NumJoysticks(); if (nbjoysticks) { for (int i = 0; i < nbjoysticks; i++) { SDL_Joystick *joystick = SDL_JoystickOpen(i); if (joystick) { con_print << " joystick " << i + 1 << ": " << SDL_JoystickName(joystick) << " " << SDL_JoystickNumAxes(joystick) << " axes " << SDL_JoystickNumButtons(joystick) << " buttons " << std::endl; SDL_JoystickClose(joystick); } } } current_joystick_number = (int) input_joystick->value(); if ((current_joystick_number < 1) || (current_joystick_number > nbjoysticks)) { current_joystick_number = 0; } (*input_joystick) = (float) current_joystick_number; if (current_joystick_number) { current_joystick = SDL_JoystickOpen(current_joystick_number - 1); } if (current_joystick) { con_debug << " using joystick " << SDL_JoystickName(current_joystick) << std::endl; SDL_JoystickEventState(SDL_ENABLE); } } bool Joystick::is_enabled() { return (current_joystick != 0); } void Joystick::frame() { if (current_joystick_number != (int) input_joystick->value()) { if (current_joystick) { SDL_JoystickEventState(SDL_IGNORE); SDL_JoystickClose(current_joystick); current_joystick = 0; } current_joystick_number = (int) input_joystick->value(); if ((current_joystick_number < 1) || (current_joystick_number > SDL_NumJoysticks())) { current_joystick_number = 0; } (*input_joystick) = (float) current_joystick_number; if (current_joystick_number) { current_joystick = SDL_JoystickOpen(current_joystick_number - 1); } if (current_joystick) { con_debug << " using joystick " << SDL_JoystickName(current_joystick) << std::endl; SDL_JoystickEventState(SDL_ENABLE); } } } }