From 3409fbdaf0b9cea453c97259a4b3a1bd535a6a36 Mon Sep 17 00:00:00 2001 From: Pavel Sountsov Date: Sat, 10 Feb 2024 16:58:17 -0800 Subject: [PATCH] MacOS: Don't use usage numbers as button ids on OSX. Add a compatibility config option, "[compatibility] joystick_version=xx.yy.zz" if it is necessary to restore the old behavior. Fixes #1527 --- allegro5.cfg | 4 ++++ include/allegro5/internal/aintern_system.h | 1 + src/joynu.c | 16 ++++++++++++++++ src/macosx/hidjoy.m | 21 ++++++++++++++++----- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/allegro5.cfg b/allegro5.cfg index f87bd92f1a..77675f1246 100644 --- a/allegro5.cfg +++ b/allegro5.cfg @@ -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 diff --git a/include/allegro5/internal/aintern_system.h b/include/allegro5/internal/aintern_system.h index 01d6a36f4c..90f4756d62 100644 --- a/include/allegro5/internal/aintern_system.h +++ b/include/allegro5/internal/aintern_system.h @@ -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 } diff --git a/src/joynu.c b/src/joynu.c index ef3ced8dbc..912b47ad7c 100644 --- a/src/joynu.c +++ b/src/joynu.c @@ -300,6 +300,22 @@ 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 || strlen(compat_version) == 0) + 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, "%2d.%2d.%2d", &version, &sub_version, &wip_version); + return AL_ID(version, sub_version, wip_version, 0); +} + /* * Local Variables: * c-basic-offset: 3 diff --git a/src/macosx/hidjoy.m b/src/macosx/hidjoy.m index 3b7eeafe15..d14737ca3c 100644 --- a/src/macosx/hidjoy.m +++ b/src/macosx/hidjoy.m @@ -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); @@ -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++; } }