uhexen2 icon indicating copy to clipboard operation
uhexen2 copied to clipboard

Joypad control in menus

Open twojstaryzdomu opened this issue 3 years ago • 3 comments

Working for a cheap generic DragonRise game pad:

  1. SELECT & START (event.jbutton.button values 8 & 9 respectively) have been hardcoded to ESCAPE & ENTER keys for menu control (without needing to make changes in menu.c).
  2. CL_SendCmd required a minor sequence update for out-of-game menu control.

twojstaryzdomu avatar Jun 20 '22 12:06 twojstaryzdomu

2. CL_SendCmd required a minor sequence update for out-of-game menu control.

Not sure how safe is that?

sezero avatar Jun 21 '22 01:06 sezero

At the very least, -Wdeclaration-after-statement should be avoided, i.e. C90 compatibility. And some unnecessary changes can be reverted and cleaned up. Something like the following, on top of your patch:

diff --git a/engine/h2shared/in_sdl.c b/engine/h2shared/in_sdl.c
index b62fd4f..2aac20c 100644
--- a/engine/h2shared/in_sdl.c
+++ b/engine/h2shared/in_sdl.c
@@ -52,7 +52,7 @@ static int buttonremap[] =
 static	SDL_Joystick	*joy_id = NULL;
 static	int		joy_available;
 
-static	cvar_t	in_joystick = {"joystick", "1", CVAR_ARCHIVE};		/* enable/disable joystick */
+static	cvar_t	in_joystick = {"joystick", "0", CVAR_ARCHIVE};		/* enable/disable joystick */
 
 static	cvar_t	joy_index = {"joy_index", "0", CVAR_NONE};		/* joystick to use when have multiple */
 static	cvar_t	joy_axisforward = {"joy_axisforward", "1", CVAR_NONE};	/* axis for forward/backward movement */
@@ -75,7 +75,7 @@ static	cvar_t	joy_sensitivityyaw = {"joy_sensitivityyaw", "-1", CVAR_NONE};		/*
 /* hack to generate uparrow/leftarrow etc. key events
  * for axes if the stick driver isn't generating them. */
 /* might be useful for menu navigation etc. */
-#define JOY_KEYEVENT_FOR_AXES 1 /* not for now */
+#define JOY_KEYEVENT_FOR_AXES 1
 #if (JOY_KEYEVENT_FOR_AXES)
 static	cvar_t	joy_axiskeyevents = {"joy_axiskeyevents", "1", CVAR_ARCHIVE};
 static	cvar_t	joy_axiskeyevents_deadzone = {"joy_axiskeyevents_deadzone", "0.5", CVAR_ARCHIVE};
@@ -812,6 +812,7 @@ void IN_SendKeyEvents (void)
 {
 	SDL_Event event;
 	int sym, state, modstate;
+	int joy2key;
 	qboolean gamekey;
 
 	if ((gamekey = Key_IsGameKey()) != prev_gamekey)
@@ -1102,14 +1103,14 @@ void IN_SendKeyEvents (void)
 		case SDL_JOYBUTTONUP:
 			if (in_mode_set)
 				break;
-			uint8_t key = 0;
+			joy2key = 0;
 			switch (K_JOY1 + event.jbutton.button) {
 			/* Hard code SELECT & START buttons for menu control */
 			case K_AUX5:
-				key = K_ESCAPE;
+				joy2key = K_ESCAPE;
 				break;
 			case K_AUX6:
-				key = K_ENTER;
+				joy2key = K_ENTER;
 			}
 			Con_DPrintf ("Pressed joystick button %s\n", Key_KeynumToString(K_JOY1 + event.jbutton.button));
 			if (event.jbutton.button > K_AUX28 - K_JOY1)
@@ -1118,7 +1119,7 @@ void IN_SendKeyEvents (void)
 							event.jbutton.button);
 				break;
 			}
-			Key_Event(key ? key : (K_JOY1 + event.jbutton.button), event.jbutton.state == SDL_PRESSED);
+			Key_Event(joy2key ? joy2key : (K_JOY1 + event.jbutton.button), event.jbutton.state == SDL_PRESSED);
 			break;
 
 		/* mouse/trackball motion handled by IN_MouseMove() */

sezero avatar Jun 21 '22 01:06 sezero

2. CL_SendCmd required a minor sequence update for out-of-game menu control.

Not sure how safe is that?

I haven't seen any side effects. When not in game, for joystick direction to work in the menus, IN_Move(), which triggers IN_JoyMove(), needs to run. The hook so happens to be in CL_SendCmd. Admittedly, it is hacky but it's better than nothing. Without this fix, I'm unable to have any control over menus, including quitting the game, without a physical keyboard and mouse attached. Joypad is my sole means of control.

At the very least, -Wdeclaration-after-statement should be avoided, i.e. C90 compatibility. And some unnecessary changes can be reverted and cleaned up. Something like the following, on top of your patch:

diff --git a/engine/h2shared/in_sdl.c b/engine/h2shared/in_sdl.c
index b62fd4f..2aac20c 100644
--- a/engine/h2shared/in_sdl.c
+++ b/engine/h2shared/in_sdl.c
@@ -52,7 +52,7 @@ static int buttonremap[] =
 static	SDL_Joystick	*joy_id = NULL;
 static	int		joy_available;
 
-static	cvar_t	in_joystick = {"joystick", "1", CVAR_ARCHIVE};		/* enable/disable joystick */
+static	cvar_t	in_joystick = {"joystick", "0", CVAR_ARCHIVE};		/* enable/disable joystick */
 
 static	cvar_t	joy_index = {"joy_index", "0", CVAR_NONE};		/* joystick to use when have multiple */
 static	cvar_t	joy_axisforward = {"joy_axisforward", "1", CVAR_NONE};	/* axis for forward/backward movement */
@@ -75,7 +75,7 @@ static	cvar_t	joy_sensitivityyaw = {"joy_sensitivityyaw", "-1", CVAR_NONE};		/*
 /* hack to generate uparrow/leftarrow etc. key events
  * for axes if the stick driver isn't generating them. */
 /* might be useful for menu navigation etc. */
-#define JOY_KEYEVENT_FOR_AXES 1 /* not for now */
+#define JOY_KEYEVENT_FOR_AXES 1
 #if (JOY_KEYEVENT_FOR_AXES)
 static	cvar_t	joy_axiskeyevents = {"joy_axiskeyevents", "1", CVAR_ARCHIVE};
 static	cvar_t	joy_axiskeyevents_deadzone = {"joy_axiskeyevents_deadzone", "0.5", CVAR_ARCHIVE};
@@ -812,6 +812,7 @@ void IN_SendKeyEvents (void)
 {
 	SDL_Event event;
 	int sym, state, modstate;
+	int joy2key;
 	qboolean gamekey;
 
 	if ((gamekey = Key_IsGameKey()) != prev_gamekey)
@@ -1102,14 +1103,14 @@ void IN_SendKeyEvents (void)
 		case SDL_JOYBUTTONUP:
 			if (in_mode_set)
 				break;
-			uint8_t key = 0;
+			joy2key = 0;
 			switch (K_JOY1 + event.jbutton.button) {
 			/* Hard code SELECT & START buttons for menu control */
 			case K_AUX5:
-				key = K_ESCAPE;
+				joy2key = K_ESCAPE;
 				break;
 			case K_AUX6:
-				key = K_ENTER;
+				joy2key = K_ENTER;
 			}
 			Con_DPrintf ("Pressed joystick button %s\n", Key_KeynumToString(K_JOY1 + event.jbutton.button));
 			if (event.jbutton.button > K_AUX28 - K_JOY1)
@@ -1118,7 +1119,7 @@ void IN_SendKeyEvents (void)
 							event.jbutton.button);
 				break;
 			}
-			Key_Event(key ? key : (K_JOY1 + event.jbutton.button), event.jbutton.state == SDL_PRESSED);
+			Key_Event(joy2key ? joy2key : (K_JOY1 + event.jbutton.button), event.jbutton.state == SDL_PRESSED);
 			break;
 
 		/* mouse/trackball motion handled by IN_MouseMove() */

I'm fine with this. Do you want to commit including your changes?

twojstaryzdomu avatar Jun 21 '22 08:06 twojstaryzdomu