Skip to content

Commit 6526843

Browse files
connorjclarkSiegeLord
authored andcommitted
osx: fix off-by-one error for hat switches
1 parent fead757 commit 6526843

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/macosx/hidjoy.m

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ static void osx_joy_generate_button_event(ALLEGRO_JOYSTICK_OSX *joy, int button,
443443
_al_event_source_emit_event(es, &event);
444444
}
445445

446-
#define MAX_HAT_DIRECTIONS 9
446+
#define MAX_HAT_DIRECTIONS 8
447447
struct HAT_MAPPING {
448448
int axisV;
449449
int axisH;
@@ -456,7 +456,6 @@ static void osx_joy_generate_button_event(ALLEGRO_JOYSTICK_OSX *joy, int button,
456456
{ 1, -1 }, // 5
457457
{ 0, -1 }, // 6
458458
{ -1, -1 }, // 7
459-
{ 0, 0 }, // 8
460459
};
461460

462461
static void value_callback(
@@ -496,9 +495,12 @@ static void value_callback(
496495
int int_value = IOHIDValueGetIntegerValue(value);
497496

498497
if (joy->dpad == elem){
499-
if (int_value >= 0 && int_value < MAX_HAT_DIRECTIONS) {
500-
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_vert, (float)hat_mapping[int_value].axisV);
501-
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_horiz, (float)hat_mapping[int_value].axisH);
498+
if (int_value > 0 && int_value <= MAX_HAT_DIRECTIONS) {
499+
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_vert, (float)hat_mapping[int_value-1].axisV);
500+
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_horiz, (float)hat_mapping[int_value-1].axisH);
501+
} else if (joy->min[joy->dpad_stick][1] > int_value || joy->max[joy->dpad_stick][1] < int_value) {
502+
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_vert, 0);
503+
osx_joy_generate_axis_event(joy, joy->dpad_stick, joy->dpad_axis_horiz, 0);
502504
}
503505
goto done;
504506
}

0 commit comments

Comments
 (0)