LCOV - code coverage report
Current view: top level - source/ps - Joystick.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 3 37 8.1 %
Date: 2026-12-03 01:19:39 Functions: 1 4 25.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "Joystick.h"
      21             : 
      22             : #include "ps/CLogger.h"
      23             : #include "ps/ConfigDB.h"
      24             : 
      25             : #include <SDL.h>
      26             : #include <SDL_error.h>
      27             : #include <SDL_events.h>
      28             : #include <cstddef>
      29             : #include <cstdint>
      30             : 
      31             : CJoystick g_Joystick;
      32             : 
      33           0 : CJoystick::CJoystick() :
      34           0 :     m_Joystick(NULL), m_Deadzone(0)
      35             : {
      36           0 : }
      37             : 
      38           1 : void CJoystick::Initialise()
      39             : {
      40           1 :     if (!CConfigDB::GetIfInitialised("joystick.enable", false))
      41           1 :         return;
      42             : 
      43           0 :     m_Deadzone = g_ConfigDB.Get("joystick.deadzone", 0);
      44             : 
      45           0 :     if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
      46             :     {
      47           0 :         LOGERROR("CJoystick::Initialise failed to initialize joysticks (\"{}\")", SDL_GetError());
      48           0 :         return;
      49             :     }
      50             : 
      51           0 :     int numJoysticks = SDL_NumJoysticks();
      52             : 
      53           0 :     LOGMESSAGE("Found {} joystick(s)", numJoysticks);
      54             : 
      55           0 :     for (int i = 0; i < numJoysticks; ++i)
      56             :     {
      57           0 :         SDL_Joystick* stick = SDL_JoystickOpen(i);
      58           0 :         if (!stick)
      59             :         {
      60           0 :             LOGERROR("CJoystick::Initialise failed to open joystick {} (\"{}\")", i, SDL_GetError());
      61           0 :             continue;
      62           0 :         }
      63           0 :         const char* name = SDL_JoystickName(stick);
      64           0 :         LOGMESSAGE("Joystick {}: {}", i, name);
      65           0 :         SDL_JoystickClose(stick);
      66             :     }
      67             : 
      68           0 :     if (numJoysticks)
      69             :     {
      70           0 :         SDL_JoystickEventState(SDL_ENABLE);
      71             : 
      72             :         // Always pick the first joystick, and assume that's the right one
      73           0 :         m_Joystick = SDL_JoystickOpen(0);
      74           0 :         if (!m_Joystick)
      75           0 :             LOGERROR("CJoystick::Initialise failed to open joystick (\"{}\")", SDL_GetError());
      76             :     }
      77             : }
      78             : 
      79           0 : bool CJoystick::IsEnabled()
      80             : {
      81           0 :     return (m_Joystick != NULL);
      82             : }
      83             : 
      84           0 : float CJoystick::GetAxisValue(int axis)
      85             : {
      86           0 :     if (!m_Joystick || axis < 0 || axis >= SDL_JoystickNumAxes(m_Joystick))
      87           0 :         return 0.f;
      88             : 
      89           0 :     int16_t val = SDL_JoystickGetAxis(m_Joystick, axis);
      90             : 
      91             :     // Normalize values into the range [-1, +1] excluding the deadzone around 0
      92           0 :     float norm;
      93           0 :     if (val > m_Deadzone)
      94           0 :         norm = (val - m_Deadzone) / (float)(32767 - m_Deadzone);
      95           0 :     else if (val < -m_Deadzone)
      96           0 :         norm = (val + m_Deadzone) / (float)(32767 - m_Deadzone);
      97             :     else
      98             :         norm = 0.f;
      99             : 
     100             :     return norm;
     101             : }

Generated by: LCOV version 1.16