Skip to content

Commit

Permalink
Add options to swap A/B & X/Y buttons and L/ZL & R/ZR buttons (#11)
Browse files Browse the repository at this point in the history
* Add config options for swapping buttons

* Handle buttons swaps in input functions
  • Loading branch information
RoblKyogre authored Mar 16, 2024
1 parent 2916e2c commit 5795e74
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
22 changes: 22 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ static struct option long_options[] = {
{"port", required_argument, NULL, '6'},
{"hdr", no_argument, NULL, '7'},
{"hwdecode", required_argument, NULL, '8'},
{"swapfacebuttons", required_argument, NULL, 'A'},
{"swaptriggersandshoulders", required_argument, NULL, 'B'},
{0, 0, 0, 0},
};

Expand Down Expand Up @@ -304,6 +306,22 @@ void parse_argument(int c, char* value, PCONFIGURATION config) {
config->hwdecode = false;
}
break;
case 'A':
if ((value != NULL) && (strcmp(value, "true") == 0)) {
config->swap_face_buttons = true;
}
else {
config->swap_face_buttons = false;
}
break;
case 'B':
if ((value != NULL) && (strcmp(value, "true") == 0)) {
config->swap_triggers_and_shoulders = true;
}
else {
config->swap_triggers_and_shoulders = false;
}
break;
case 1:
if (config->action == NULL)
config->action = value;
Expand Down Expand Up @@ -366,6 +384,8 @@ void config_save(char* filename, PCONFIGURATION config) {
write_config_bool(fd, "viewonly", config->viewonly);
write_config_int(fd, "rotate", config->rotate);
write_config_bool(fd, "hwdecode", config->hwdecode);
write_config_bool(fd, "swapfacebuttons", config->swap_face_buttons);
write_config_bool(fd, "swaptriggersandshoulders", config->swap_triggers_and_shoulders);
write_config_bool(fd, "debug", config->debug_level);

if (strcmp(config->app, "Steam") != 0)
Expand Down Expand Up @@ -437,6 +457,8 @@ void config_parse(int argc, char* argv[], PCONFIGURATION config) {
config->stream.fps = 30;
config->stream.encryptionFlags = ENCFLG_NONE;
config->hwdecode = true;
config->swap_face_buttons = false;
config->swap_triggers_and_shoulders = false;

char* config_file = (char*) MOONLIGHT_3DS_PATH "/moonlight.conf";
if (config_file)
Expand Down
2 changes: 2 additions & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ typedef struct _CONFIGURATION {
int pin;
unsigned short port;
bool hwdecode;
bool swap_face_buttons;
bool swap_triggers_and_shoulders;
} CONFIGURATION, *PCONFIGURATION;

extern bool inputAdded;
Expand Down
44 changes: 33 additions & 11 deletions src/input/n3ds_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ static GAMEPAD_STATE gamepad_state, previous_state;

static const int activeGamepadMask = 1;

static u32 SWAP_A = KEY_A;
static u32 SWAP_B = KEY_B;
static u32 SWAP_X = KEY_X;
static u32 SWAP_Y = KEY_Y;

static u32 SWAP_L = KEY_L;
static u32 SWAP_R = KEY_R;
static u32 SWAP_ZL = KEY_ZL;
static u32 SWAP_ZR = KEY_ZR;

static void add_gamepad() {
unsigned short capabilities = 0;
unsigned char type = LI_CTYPE_NINTENDO;
Expand All @@ -63,10 +73,22 @@ static void remove_gamepad() {
LiSendMultiControllerEvent(0, ~activeGamepadMask, 0, 0, 0, 0, 0, 0, 0);
}

void n3dsinput_init() {
void n3dsinput_init(bool swap_face_buttons, bool swap_triggers_and_shoulders) {
hidInit();
add_gamepad();
gamepad_state.ttype = DEBUG_PRINT;
if (swap_face_buttons) {
SWAP_A = KEY_B;
SWAP_B = KEY_A;
SWAP_X = KEY_Y;
SWAP_Y = KEY_X;
}
if (swap_triggers_and_shoulders) {
SWAP_L = KEY_ZL;
SWAP_R = KEY_ZR;
SWAP_ZL = KEY_L;
SWAP_ZR = KEY_R;
}
gfxSetDoubleBuffering(GFX_BOTTOM, false);
}

Expand All @@ -80,18 +102,18 @@ static inline int n3ds_to_li_button(u32 key_in, u32 key_n3ds, int key_li) {

static inline int n3ds_to_li_buttons(u32 key_n3ds) {
int li_out = 0;
li_out |= n3ds_to_li_button(key_n3ds, KEY_A, A_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_B, B_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_A, A_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_B, B_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_SELECT, BACK_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_START, PLAY_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_DRIGHT, RIGHT_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_DLEFT, LEFT_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_DUP, UP_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_DDOWN, DOWN_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_R, RB_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_L, LB_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_X, X_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, KEY_Y, Y_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_R, RB_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_L, LB_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_X, X_FLAG);
li_out |= n3ds_to_li_button(key_n3ds, SWAP_Y, Y_FLAG);
return li_out;
}

Expand Down Expand Up @@ -226,13 +248,13 @@ int n3dsinput_handle_event() {

if (kDown) {
gamepad_state.buttons |= n3ds_to_li_buttons(kDown);
gamepad_state.leftTrigger |= n3ds_to_li_trigger(kDown, KEY_ZL);
gamepad_state.rightTrigger |= n3ds_to_li_trigger(kDown, KEY_ZR);
gamepad_state.leftTrigger |= n3ds_to_li_trigger(kDown, SWAP_ZL);
gamepad_state.rightTrigger |= n3ds_to_li_trigger(kDown, SWAP_ZR);
}
if (kUp) {
gamepad_state.buttons &= ~n3ds_to_li_buttons(kUp);
gamepad_state.leftTrigger &= ~n3ds_to_li_trigger(kUp, KEY_ZL);
gamepad_state.rightTrigger &= ~n3ds_to_li_trigger(kUp, KEY_ZR);
gamepad_state.leftTrigger &= ~n3ds_to_li_trigger(kUp, SWAP_ZL);
gamepad_state.rightTrigger &= ~n3ds_to_li_trigger(kUp, SWAP_ZR);
}

if ((gamepad_state.buttons & QUIT_BUTTONS) == QUIT_BUTTONS)
Expand Down
2 changes: 1 addition & 1 deletion src/input/n3ds_input.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

enum N3dsTouchType { GAMEPAD, MOUSEPAD, DEBUG_PRINT };

void n3dsinput_init();
void n3dsinput_init(bool set_face_swap, bool set_trigger_swap);
void n3dsinput_cleanup();
void n3dsinput_set_touch(enum N3dsTouchType ttype);
int n3dsinput_handle_event();
22 changes: 20 additions & 2 deletions src/n3ds_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ static void prompt_for_stream_settings(PCONFIGURATION config)
"quitappafter",
"viewonly",
"hwdecode",
"swapfacebuttons",
"swaptriggersandshoulders",
"debug",
};
char argument_ids[] = {
Expand All @@ -251,6 +253,8 @@ static void prompt_for_stream_settings(PCONFIGURATION config)
'1',
'2',
'8',
'A',
'B',
'Z',
};
int settings_len = sizeof(argument_ids);
Expand Down Expand Up @@ -333,6 +337,18 @@ static void prompt_for_stream_settings(PCONFIGURATION config)
sprintf(setting_buff, bool_str);
}
}
else if (strcmp("swapfacebuttons", setting_names[idx]) == 0) {
char* bool_str = prompt_for_boolean("Swaps A/B and X/Y to match Xbox controller layout", config->swap_face_buttons);
if (bool_str != NULL) {
sprintf(setting_buff, bool_str);
}
}
else if (strcmp("swaptriggersandshoulders", setting_names[idx]) == 0) {
char* bool_str = prompt_for_boolean("Swaps L/ZL and R/ZR for a more natural feel", config->swap_triggers_and_shoulders);
if (bool_str != NULL) {
sprintf(setting_buff, bool_str);
}
}
else if (strcmp("debug", setting_names[idx]) == 0) {
char* bool_str = prompt_for_boolean("Enable debug logs", config->debug_level);
if (bool_str != NULL) {
Expand Down Expand Up @@ -464,7 +480,7 @@ static void stream(PSERVER_DATA server, PCONFIGURATION config, int appId) {
PDECODER_RENDERER_CALLBACKS video_callbacks = config->hwdecode ? &decoder_callbacks_n3ds_mvd : &decoder_callbacks_n3ds;

printf("Loading...\nStream %dx%d, %dfps, %dkbps, sops=%d, localaudio=%d, quitappafter=%d,\
viewonly=%d, rotate=%d, encryption=%x, hwdecode=%d, debug=%d\n",
viewonly=%d, rotate=%d, encryption=%x, hwdecode=%d, swapfacebuttons=%d, swaptriggersandshoulders=%d, debug=%d\n",
config->stream.width,
config->stream.height,
config->stream.fps,
Expand All @@ -476,6 +492,8 @@ static void stream(PSERVER_DATA server, PCONFIGURATION config, int appId) {
config->rotate,
config->stream.encryptionFlags,
config->hwdecode,
config->swap_face_buttons,
config->swap_triggers_and_shoulders,
config->debug_level
);

Expand Down Expand Up @@ -568,7 +586,7 @@ int main(int argc, char* argv[]) {
if (config.debug_level > 0)
printf("View-only mode enabled, no input will be sent to the host computer\n");
} else {
n3dsinput_init();
n3dsinput_init(config.swap_face_buttons, config.swap_triggers_and_shoulders);
}
stream(&server, &config, appId);

Expand Down

0 comments on commit 5795e74

Please sign in to comment.