27 #include "../client.h"
29 #include "../ui/ui_main.h"
30 #include "../ui/ui_nodes.h"
31 #include "../ui/node/ui_node_abstractoption.h"
33 static SDL_Joystick*
stick =
nullptr;
39 #if SDL_VERSION_ATLEAST(2,0,0)
40 #define SDL_JoystickName SDL_JoystickNameForIndex
78 unsigned int axes = 0;
79 unsigned int hats = 0;
96 int total = SDL_JoystickNumBalls(
stick);
100 for (
int i = 0;
i < total;
i++) {
103 SDL_JoystickGetBall(
stick,
i, &dx, &dy);
107 if (balldx || balldy) {
114 total = SDL_JoystickNumButtons(
stick);
118 for (
int i = 0;
i < total;
i++) {
119 const bool pressed = (SDL_JoystickGetButton(
stick,
i) != 0);
128 total = SDL_JoystickNumHats(
stick);
132 for (
int i = 0;
i < total;
i++)
133 ((Uint8 *)&hats)[
i] = SDL_JoystickGetHat(
stick,
i);
138 for (
int i = 0;
i < 4;
i++) {
139 if (((Uint8 *)&hats)[
i] != ((Uint8 *)&
stick_state.oldhats)[
i]) {
154 case SDL_HAT_RIGHTUP:
158 case SDL_HAT_RIGHTDOWN:
166 case SDL_HAT_LEFTDOWN:
174 switch (((Uint8 *)&hats)[
i]) {
187 case SDL_HAT_RIGHTUP:
191 case SDL_HAT_RIGHTDOWN:
199 case SDL_HAT_LEFTDOWN:
214 total = SDL_JoystickNumAxes(
stick);
217 for (
int i = 0;
i < 2;
i++) {
218 const Sint16 axis = SDL_JoystickGetAxis(
stick,
i);
219 const float velocity = ((float) axis) / 32767.0f;
220 if (velocity > -in_joystickThreshold->
value && velocity < in_joystickThreshold->value)
244 for (
int i = 2;
i < total;
i++) {
245 const Sint16 axis = SDL_JoystickGetAxis(
stick,
i);
246 const float f = ((float) axis) / 32767.0f;
247 if (f < -in_joystickThreshold->value) {
248 axes |= (1 << (
i * 2));
249 }
else if (f > in_joystickThreshold->
value) {
250 axes |= (1 << ((
i * 2) + 1));
258 for (
int i = 2;
i < 16;
i++) {
262 if (!(axes & (1 << i)) && (
stick_state.oldaxes & (1 << i)))
276 uiNode_t* joystickOptions =
nullptr;
277 const int total = SDL_NumJoysticks();
282 for (
int i = 0;
i < total;
i++)
293 in_joystick =
Cvar_Get(
"in_joystick",
"0",
CVAR_ARCHIVE,
"Activate or deactivate the use of a joystick");
294 in_joystickNo =
Cvar_Get(
"in_joystickNo",
"0",
CVAR_ARCHIVE,
"Joystick to use - 0 is the first - 1 is the second ...");
295 in_joystickThreshold =
Cvar_Get(
"in_joystickThreshold",
"0.05",
CVAR_ARCHIVE,
"The threshold for the joystick axes");
296 in_joystickSpeed =
Cvar_Get(
"in_joystickSpeed",
"20",
CVAR_ARCHIVE,
"The joystick speed for the cursor");
298 if (
stick !=
nullptr) {
299 Com_Printf(
"... closing already initialized joystick\n");
300 SDL_JoystickClose(
stick);
306 if (!SDL_WasInit(SDL_INIT_JOYSTICK)) {
308 if (SDL_Init(SDL_INIT_JOYSTICK) == -1) {
315 int total = SDL_NumJoysticks();
317 for (
int i = 0;
i < total;
i++)
320 if (in_joystickNo->
integer < 0 || in_joystickNo->
integer >= total)
326 if (
stick ==
nullptr) {
331 Com_Printf(
"joystick %d opened - set cvar in_joystickNo to change this\n", in_joystickNo->
integer);
338 SDL_JoystickEventState(SDL_QUERY);
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
This is a cvar definition. Cvars can be user modified and used in our menus e.g.
static const int joy_keys[16]
void Com_Printf(const char *const fmt,...)
static cvar_t * in_joystickSpeed
static cvar_t * in_joystickNo
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
static const int hat_keys[16]
void Com_DPrintf(int level, const char *fmt,...)
A Com_Printf that only shows up if the "developer" cvar is set.
Atomic structure used to define most of the UI.
static cvar_t * in_joystick
static struct @11 stick_state
void IN_JoystickInitMenu(void)
Adds joysticks to the options menu.
void IN_StartupJoystick(void)
Init available joysticks.
void IN_JoystickMove(void)
void UI_RegisterOption(int dataId, uiNode_t *option)
uiNode_t * UI_AddOption(uiNode_t **tree, const char *name, const char *label, const char *value)
Append an option to an option list.
cvar_t * Cvar_Set(const char *varName, const char *value,...)
Sets a cvar value.
static SDL_Joystick * stick
static cvar_t * in_joystickThreshold