Skip to content

Fix SDL3 joystick enumeration using device indices as instance IDs#182

Merged
HexDecimal merged 4 commits into
libtcod:mainfrom
pinghedm:fix-sdl3-joystick-instance-ids
Jun 4, 2026
Merged

Fix SDL3 joystick enumeration using device indices as instance IDs#182
HexDecimal merged 4 commits into
libtcod:mainfrom
pinghedm:fix-sdl3-joystick-instance-ids

Conversation

@pinghedm
Copy link
Copy Markdown
Contributor

@pinghedm pinghedm commented Jun 4, 2026

get_joysticks/get_controllers/_get_all passed range(count) indices into
SDL_OpenJoystick/SDL_OpenGamepad/SDL_IsGamepad, which on SDL3 take an
SDL_JoystickID instance ID. _get_number discarded the instance-ID array
that SDL_GetJoysticks returns, so enumeration opened the wrong device or
failed once instance IDs diverged from device indices (e.g. after a pad
was reconnected).

Replace _get_number with _get_instance_ids, which keeps and frees the
SDL_GetJoysticks array, and enumerate by instance ID.

Fixes #181

pinghedm and others added 2 commits June 3, 2026 20:51
  get_joysticks/get_controllers/_get_all passed range(count) indices into
  SDL_OpenJoystick/SDL_OpenGamepad/SDL_IsGamepad, which on SDL3 take an
  SDL_JoystickID instance ID. _get_number discarded the instance-ID array
  that SDL_GetJoysticks returns, so enumeration opened the wrong device or
  failed once instance IDs diverged from device indices (e.g. after a pad
  was reconnected).

  Replace _get_number with _get_instance_ids, which keeps and frees the
  SDL_GetJoysticks array, and enumerate by instance ID.

  Fixes libtcod#181
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2026

Codecov Report

❌ Patch coverage is 30.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.03%. Comparing base (efb077c) to head (6c24410).

Files with missing lines Patch % Lines
tcod/sdl/joystick.py 30.00% 7 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #182   +/-   ##
=======================================
  Coverage   86.03%   86.03%           
=======================================
  Files          29       29           
  Lines        7583     7583           
=======================================
  Hits         6524     6524           
  Misses       1059     1059           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@HexDecimal HexDecimal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clearly missed the details of SDL_GetJoysticks. Thanks for debugging this and making this PR.

Don't worry about code coverage. The code being short and clean is enough for now.

Comment thread tcod/sdl/joystick.py Outdated
pinghedm and others added 2 commits June 3, 2026 21:33
Co-authored-by: Kyle Benesch <4b796c65+github@gmail.com>
@pinghedm
Copy link
Copy Markdown
Contributor Author

pinghedm commented Jun 4, 2026

Don't worry about code coverage. The code being short and clean is enough for now.

Roger

I clearly missed the details of SDL_GetJoysticks. Thanks for debugging this and making this PR.

Thanks for all your work on the library!

@HexDecimal HexDecimal merged commit 16fdccf into libtcod:main Jun 4, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_joysticks() / get_controllers() pass device indices to SDL3 functions that expect instance IDs

2 participants