| 1 | // (_||_/ |
| 2 | // ( ) |
| 3 | // ( o 0 ) |
| 4 | //-OOO°--(_)---°OOO--------------------------------------- |
| 5 | // Copyright (C) 2026 By Johnny Jazeix |
| 6 | // .OOOo oOOO. [email protected] |
| 7 | //-( )------( )--------------------------------------- |
| 8 | // ( ( ) / |
| 9 | // (_) (_/ |
| 10 | |
| 11 | // This program is free software; you can redistribute it and/or modify |
| 12 | // it under the terms of the GNU General Public License as published by |
| 13 | // the Free Software Foundation; either version 2 or version 3 of the License. |
| 14 | |
| 15 | // This program is distributed in the hope that it will be useful, |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | // GNU General Public License for more details. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License along |
| 21 | // with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | |
| 24 | #include <SDL2/SDL_log.h> // for SDL_LogInfo, SDL_LOG_CATEGORY_APPLICATION |
| 25 | |
| 26 | #include "gamepad.h" |
| 27 | #include "utils.h" |
| 28 | |
| 29 | void Gamepad::Initialize() |
| 30 | { |
| 31 | char PathFile[512]; |
| 32 | // Controller |
| 33 | strcpy(PathFile, "gamecontrollerdb.txt"); |
| 34 | Utils::GetPath(PathFile); |
| 35 | SDL_GameControllerAddMappingsFromFile(PathFile); |
| 36 | // Check if there is already a controller connected |
| 37 | m_controller = findController(); |
| 38 | } |
| 39 | |
| 40 | SDL_GameController *Gamepad::findController() |
| 41 | { |
| 42 | for (int i = 0; i < SDL_NumJoysticks(); i++) { |
| 43 | if (SDL_IsGameController(i)) { |
| 44 | return SDL_GameControllerOpen(i); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return nullptr; |
| 49 | } |
| 50 | |
| 51 | SDL_JoystickID Gamepad::getControllerInstanceID(SDL_GameController *controller) |
| 52 | { |
| 53 | return SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(controller)); |
| 54 | } |
| 55 | |
| 56 | void Gamepad::OverrideEvent(SDL_Event &event, int key) |
| 57 | { |
| 58 | event.type = SDL_KEYDOWN; |
| 59 | event.key.state = SDL_PRESSED; |
| 60 | event.key.keysym.sym = key; |
| 61 | event.key.repeat = 0; |
| 62 | } |
| 63 | |
| 64 | void Gamepad::GetEvent(SDL_Event &event) |
| 65 | { |
| 66 | constexpr int DEADZONE = 9000; |
| 67 | |
| 68 | switch (event.type) { |
| 69 | case SDL_CONTROLLERDEVICEADDED: |
| 70 | if (!m_controller) { |
| 71 | m_controller = SDL_GameControllerOpen(event.cdevice.which); |
| 72 | } |
| 73 | break; |
| 74 | case SDL_CONTROLLERDEVICEREMOVED: |
| 75 | if (m_controller && event.cdevice.which == getControllerInstanceID(m_controller)) { |
| 76 | SDL_GameControllerClose(m_controller); |
| 77 | m_controller = findController(); |
| 78 | } |
| 79 | break; |
| 80 | case SDL_CONTROLLERBUTTONDOWN: |
| 81 | if (m_controller && event.cdevice.which == getControllerInstanceID(m_controller)) { |
| 82 | switch (event.cbutton.button) { |
| 83 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_X: |
| 84 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_A: |
| 85 | OverrideEvent(event, SDLK_RETURN); |
| 86 | break; |
| 87 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_Y: |
| 88 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_B: |
| 89 | OverrideEvent(event, SDLK_ESCAPE); |
| 90 | break; |
| 91 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_RIGHT: |
| 92 | OverrideEvent(event, SDLK_RIGHT); |
| 93 | break; |
| 94 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_LEFT: |
| 95 | OverrideEvent(event, SDLK_LEFT); |
| 96 | break; |
| 97 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_UP: |
| 98 | OverrideEvent(event, SDLK_UP); |
| 99 | break; |
| 100 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_DPAD_DOWN: |
| 101 | OverrideEvent(event, SDLK_DOWN); |
| 102 | break; |
| 103 | case SDL_GameControllerButton::SDL_CONTROLLER_BUTTON_START: |
| 104 | OverrideEvent(event, SDLK_p); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | break; |
| 109 | case SDL_CONTROLLERAXISMOTION: |
| 110 | if (m_controller && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX) { |
| 111 | // The deadzone ensures we don't trigger the event if the joystick is not press far enough |
| 112 | // The m_lastValuesPerDirection ensures we don't have multiple repeats of the same event. |
| 113 | if (event.caxis.value > DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] < DEADZONE) { |
| 114 | OverrideEvent(event, SDLK_RIGHT); |
| 115 | } |
| 116 | else if (event.caxis.value < -DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] > -DEADZONE) { |
| 117 | OverrideEvent(event, SDLK_LEFT); |
| 118 | } |
| 119 | m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTX] = event.caxis.value; |
| 120 | } |
| 121 | else if (m_controller && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY) { |
| 122 | if (event.caxis.value > DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] < DEADZONE) { |
| 123 | OverrideEvent(event, SDLK_DOWN); |
| 124 | } |
| 125 | else if (event.caxis.value < -DEADZONE && m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] > -DEADZONE) { |
| 126 | OverrideEvent(event, SDLK_UP); |
| 127 | } |
| 128 | m_lastValuesPerDirection[SDL_CONTROLLER_AXIS_LEFTY] = event.caxis.value; |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | } |