Line data Source code
1 : /* Copyright (C) 2021 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 "lib/external_libraries/libsdl.h"
23 : #include "ps/CLogger.h"
24 : #include "ps/ConfigDB.h"
25 :
26 1 : CJoystick g_Joystick;
27 :
28 1 : CJoystick::CJoystick() :
29 1 : m_Joystick(NULL), m_Deadzone(0)
30 : {
31 1 : }
32 :
33 1 : void CJoystick::Initialise()
34 : {
35 1 : bool joystickEnable = false;
36 1 : if (!CConfigDB::IsInitialised())
37 2 : return;
38 0 : CFG_GET_VAL("joystick.enable", joystickEnable);
39 0 : if (!joystickEnable)
40 0 : return;
41 :
42 0 : CFG_GET_VAL("joystick.deadzone", m_Deadzone);
43 :
44 0 : if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
45 : {
46 0 : LOGERROR("CJoystick::Initialise failed to initialise joysticks (\"%s\")", SDL_GetError());
47 0 : return;
48 : }
49 :
50 0 : int numJoysticks = SDL_NumJoysticks();
51 :
52 0 : LOGMESSAGE("Found %d joystick(s)", numJoysticks);
53 :
54 0 : for (int i = 0; i < numJoysticks; ++i)
55 : {
56 0 : SDL_Joystick* stick = SDL_JoystickOpen(i);
57 0 : if (!stick)
58 : {
59 0 : LOGERROR("CJoystick::Initialise failed to open joystick %d (\"%s\")", i, SDL_GetError());
60 0 : continue;
61 : }
62 0 : const char* name = SDL_JoystickName(stick);
63 0 : LOGMESSAGE("Joystick %d: %s", i, name);
64 0 : SDL_JoystickClose(stick);
65 : }
66 :
67 0 : if (numJoysticks)
68 : {
69 0 : SDL_JoystickEventState(SDL_ENABLE);
70 :
71 : // Always pick the first joystick, and assume that's the right one
72 0 : m_Joystick = SDL_JoystickOpen(0);
73 0 : if (!m_Joystick)
74 0 : LOGERROR("CJoystick::Initialise failed to open joystick (\"%s\")", SDL_GetError());
75 : }
76 : }
77 :
78 0 : bool CJoystick::IsEnabled()
79 : {
80 0 : return (m_Joystick != NULL);
81 : }
82 :
83 0 : float CJoystick::GetAxisValue(int axis)
84 : {
85 0 : if (!m_Joystick || axis < 0 || axis >= SDL_JoystickNumAxes(m_Joystick))
86 0 : return 0.f;
87 :
88 0 : int16_t val = SDL_JoystickGetAxis(m_Joystick, axis);
89 :
90 : // Normalize values into the range [-1, +1] excluding the deadzone around 0
91 : float norm;
92 0 : if (val > m_Deadzone)
93 0 : norm = (val - m_Deadzone) / (float)(32767 - m_Deadzone);
94 0 : else if (val < -m_Deadzone)
95 0 : norm = (val + m_Deadzone) / (float)(32767 - m_Deadzone);
96 : else
97 0 : norm = 0.f;
98 :
99 0 : return norm;
100 3 : }
|