| 1 | /* |
| 2 | * Copyright (C) 2016-2017 Sam Lantinga |
| 3 | * Copyright (C) 2014-2021 Garrett Brown |
| 4 | * Copyright (C) 2014-2021 Team Kodi |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 7 | * See LICENSE.md for more information. |
| 8 | */ |
| 9 | |
| 10 | #include "JoystickSDL.h" |
| 11 | #include "api/JoystickTypes.h" |
| 12 | #include "log/Log.h" |
| 13 | |
| 14 | #include <SDL2/SDL.h> |
| 15 | |
| 16 | using namespace JOYSTICK; |
| 17 | |
| 18 | #define MAX_AXIS 32768 |
| 19 | |
| 20 | CJoystickSDL::CJoystickSDL(unsigned int index) : |
| 21 | CJoystick(EJoystickInterface::SDL), |
| 22 | m_index(index), |
| 23 | m_pController(nullptr) |
| 24 | { |
| 25 | SetName("SDL Game Controller"); |
| 26 | SetButtonCount(SDL_CONTROLLER_BUTTON_MAX); |
| 27 | SetAxisCount(SDL_CONTROLLER_AXIS_MAX); |
| 28 | } |
| 29 | |
| 30 | bool CJoystickSDL::Equals(const CJoystick* rhs) const |
| 31 | { |
| 32 | if (rhs == nullptr) |
| 33 | return false; |
| 34 | |
| 35 | const CJoystickSDL* rhsSDL = dynamic_cast<const CJoystickSDL*>(rhs); |
| 36 | if (rhsSDL == nullptr) |
| 37 | return false; |
| 38 | |
| 39 | return m_index == rhsSDL->m_index; |
| 40 | } |
| 41 | |
| 42 | bool CJoystickSDL::Initialize(void) |
| 43 | { |
| 44 | bool bSuccess = false; |
| 45 | |
| 46 | if (CJoystick::Initialize()) |
| 47 | { |
| 48 | if (m_pController == nullptr) |
| 49 | m_pController = SDL_GameControllerOpen(m_index); |
| 50 | |
| 51 | if (m_pController != nullptr) |
| 52 | { |
| 53 | const char* controllerName = SDL_GameControllerNameForIndex(m_index); |
| 54 | isyslog("%s %d initialized: \"%s\"", Name().c_str(), m_index, |
| 55 | controllerName ? controllerName : ""); |
| 56 | bSuccess = true; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return bSuccess; |
| 61 | } |
| 62 | |
| 63 | void CJoystickSDL::Deinitialize(void) |
| 64 | { |
| 65 | if (m_pController != nullptr) |
| 66 | { |
| 67 | SDL_GameControllerClose(m_pController); |
| 68 | m_pController = nullptr; |
| 69 | } |
| 70 | |
| 71 | CJoystick::Deinitialize(); |
| 72 | } |
| 73 | |
| 74 | bool CJoystickSDL::ScanEvents(void) |
| 75 | { |
| 76 | if (m_pController == nullptr) |
| 77 | return false; |
| 78 | |
| 79 | SetButtonValue(0, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_A) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 80 | SetButtonValue(1, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_B) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 81 | SetButtonValue(2, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_X) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 82 | SetButtonValue(3, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_Y) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 83 | SetButtonValue(4, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_LEFTSHOULDER) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 84 | SetButtonValue(5, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 85 | SetButtonValue(6, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_BACK) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 86 | SetButtonValue(7, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_START) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 87 | SetButtonValue(8, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_LEFTSTICK) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 88 | SetButtonValue(9, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_RIGHTSTICK) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 89 | SetButtonValue(10, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_DPAD_UP) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 90 | SetButtonValue(11, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_DPAD_RIGHT) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 91 | SetButtonValue(12, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_DPAD_DOWN) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 92 | SetButtonValue(13, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_DPAD_LEFT) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 93 | SetButtonValue(14, SDL_GameControllerGetButton(m_pController, SDL_CONTROLLER_BUTTON_GUIDE) ? JOYSTICK_STATE_BUTTON_PRESSED : JOYSTICK_STATE_BUTTON_UNPRESSED); |
| 94 | |
| 95 | SetAxisValue(0, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_LEFTX), MAX_AXIS); |
| 96 | SetAxisValue(1, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_LEFTY), MAX_AXIS); |
| 97 | SetAxisValue(2, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_RIGHTX), MAX_AXIS); |
| 98 | SetAxisValue(3, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_RIGHTY), MAX_AXIS); |
| 99 | SetAxisValue(4, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_TRIGGERLEFT), MAX_AXIS); |
| 100 | SetAxisValue(5, (long)SDL_GameControllerGetAxis(m_pController, SDL_CONTROLLER_AXIS_TRIGGERRIGHT), MAX_AXIS); |
| 101 | |
| 102 | return true; |
| 103 | } |