From 25cfdf0a342a64a01710c1b6fbe3b1b04f28975e Mon Sep 17 00:00:00 2001
From: kub <derkub@gmail.com>
Date: Wed, 26 Jan 2022 19:36:32 +0000
Subject: [PATCH] add support for 4 player inputs

---
 config_file.c | 22 +++++++++++++++++++---
 input.h       |  1 +
 menu.c        | 10 ++++++----
 3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/config_file.c b/config_file.c
index a1e67ae..248025d 100644
--- a/config_file.c
+++ b/config_file.c
@@ -92,6 +92,22 @@ void config_write_keys(FILE *f)
 				}
 			}
 
+			kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_PLAYER34)];
+			for (i = 0; kbinds && me_ctrl_actions[i].name != NULL; i++) {
+				mask = me_ctrl_actions[i].mask;
+				if (mask & kbinds) {
+					strncpy(act, me_ctrl_actions[i].name, 31);
+					fprintf(f, "bind %s = player3 %s\n", name, mystrip(act));
+					kbinds &= ~mask;
+				}
+				mask = me_ctrl_actions[i].mask << 16;
+				if (mask & kbinds) {
+					strncpy(act, me_ctrl_actions[i].name, 31);
+					fprintf(f, "bind %s = player4 %s\n", name, mystrip(act));
+					kbinds &= ~mask;
+				}
+			}
+
 			kbinds = binds[IN_BIND_OFFS(k, IN_BINDTYPE_EMU)];
 			for (i = 0; kbinds && emuctrl_actions[i].name != NULL; i++) {
 				mask = emuctrl_actions[i].mask;
@@ -126,12 +142,12 @@ static int parse_bind_val(const char *val, int *type)
 		int player, shift = 0;
 		player = atoi(val + 6) - 1;
 
-		if ((unsigned int)player > 1)
+		if ((unsigned int)player > 3)
 			return -1;
-		if (player == 1)
+		if (player & 1)
 			shift = 16;
 
-		*type = IN_BINDTYPE_PLAYER12;
+		*type = IN_BINDTYPE_PLAYER12 + (player >> 1);
 		for (i = 0; me_ctrl_actions[i].name != NULL; i++) {
 			if (strncasecmp(me_ctrl_actions[i].name, val + 8, strlen(val + 8)) == 0)
 				return me_ctrl_actions[i].mask << shift;
diff --git a/input.h b/input.h
index 360b65b..ee1ad9f 100644
--- a/input.h
+++ b/input.h
@@ -70,6 +70,7 @@ enum {
 	IN_BINDTYPE_NONE = -1,
 	IN_BINDTYPE_EMU = 0,
 	IN_BINDTYPE_PLAYER12,
+	IN_BINDTYPE_PLAYER34,
 	IN_BINDTYPE_COUNT
 };
 
diff --git a/menu.c b/menu.c
index 50138f3..3cd69d2 100644
--- a/menu.c
+++ b/menu.c
@@ -1319,9 +1319,9 @@ static char *action_binds(int player_idx, int action_mask, int dev_id)
 	type = IN_BINDTYPE_EMU;
 	if (player_idx >= 0) {
 		can_combo = 0;
-		type = IN_BINDTYPE_PLAYER12;
+		type = IN_BINDTYPE_PLAYER12 + (player_idx >> 1);
 	}
-	if (player_idx == 1)
+	if (player_idx & 1)
 		action_mask <<= 16;
 
 	if (dev_id >= 0)
@@ -1459,9 +1459,11 @@ static void key_config_loop(const me_bind_action *opts, int opt_cnt, int player_
 
 	dev_id = -1; // show all
 	mask_shift = 0;
-	if (player_idx == 1)
+	if (player_idx & 1)
 		mask_shift = 16;
-	bindtype = player_idx >= 0 ? IN_BINDTYPE_PLAYER12 : IN_BINDTYPE_EMU;
+	bindtype = IN_BINDTYPE_EMU;
+	if (player_idx >= 0)
+	       bindtype = IN_BINDTYPE_PLAYER12 + (player_idx >> 1);
 
 	for (;;)
 	{
-- 
2.39.5