Skip to content

Commit

Permalink
MacOS: Don't use usage numbers as button ids on OSX.
Browse files Browse the repository at this point in the history
Add a compatibility config option, "[compatibility] joystick_version=xx.yy.zz"
if it is necessary to restore the old behavior.

Fixes liballeg#1527
  • Loading branch information
SiegeLord committed Feb 11, 2024
1 parent e1343ad commit f0a5c26
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions allegro5.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,7 @@ automatic_menu_display_resize = true
# to a graphical application. This may be undesirable for console applications. Set
# this to false to disable this behavior.
osx_tell_dock_outside_bundle = true

# To restore behavior of older code versions, specify this value to the
# Allegro version that had the desired old behavior.
# joystick_version = 5.2.9
1 change: 1 addition & 0 deletions include/allegro5/internal/aintern_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ AL_VAR(_AL_DTOR_LIST *, _al_dtor_list);
AL_FUNC(void *, _al_open_library, (const char *filename));
AL_FUNC(void *, _al_import_symbol, (void *library, const char *symbol));
AL_FUNC(void, _al_close_library, (void *library));
AL_FUNC(uint32_t, _al_get_joystick_compat_version, (void));

#ifdef __cplusplus
}
Expand Down
19 changes: 19 additions & 0 deletions src/joynu.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,25 @@ void al_get_joystick_state(ALLEGRO_JOYSTICK *joy, ALLEGRO_JOYSTICK_STATE *ret_st
new_joystick_driver->get_joystick_state(joy, ret_state);
}



uint32_t _al_get_joystick_compat_version(void)
{
ALLEGRO_CONFIG *system_config = al_get_system_config();
const char* compat_version = al_get_config_value(system_config, "compatibility", "joystick_version");
if (!compat_version)
return al_get_allegro_version();
/* 4 2 digit numbers + 3 dot separators */
if (strlen(compat_version) > 4 * 2 + 3)
return al_get_allegro_version();
int version = 0;
int sub_version = 0;
int wip_version = 0;
/* Ignore the release number, we don't expect that to make a difference */
sscanf(compat_version, "%d.%d.%d", &version, &sub_version, &wip_version);
return AL_ID(version, sub_version, wip_version, 0);
}

/*
* Local Variables:
* c-basic-offset: 3
Expand Down
21 changes: 16 additions & 5 deletions src/macosx/hidjoy.m
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ static void add_elements(CFArrayRef elements, ALLEGRO_JOYSTICK_OSX *joy)
char default_name[100];
int stick_class = -1;
int axis_index = 0;
bool old_style = _al_get_joystick_compat_version() < AL_ID(5, 2, 10, 0);

joy_null(joy);

Expand All @@ -199,15 +200,25 @@ static void add_elements(CFArrayRef elements, ALLEGRO_JOYSTICK_OSX *joy)
);

int usage = IOHIDElementGetUsage(elem);
int usage_page = IOHIDElementGetUsagePage(elem);
if (IOHIDElementGetType(elem) == kIOHIDElementTypeInput_Button) {
if (usage >= 0 && usage < _AL_MAX_JOYSTICK_BUTTONS &&
!joy->buttons[usage-1]) {
joy->buttons[usage-1] = elem;
sprintf(default_name, "Button %d", usage-1);
int idx;
if (old_style)
idx = usage - 1;
else {
idx = joy->parent.info.num_buttons;
// 0x09 is the Button Page.
if (usage_page != 0x09)
continue;
}
if (idx >= 0 && idx < _AL_MAX_JOYSTICK_BUTTONS &&
!joy->buttons[idx]) {
joy->buttons[idx] = elem;
sprintf(default_name, "Button %d", idx);
const char *name = get_element_name(elem, default_name);
char *str = al_malloc(strlen(name)+1);
strcpy(str, name);
joy->parent.info.button[usage-1].name = str;
joy->parent.info.button[idx].name = str;
joy->parent.info.num_buttons++;
}
}
Expand Down

0 comments on commit f0a5c26

Please sign in to comment.