Skip to content

Commit

Permalink
connected device name list
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Jan 19, 2025
1 parent 5dba443 commit ab9ca34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ ConnectedPhysicalDeviceManager::GetConnectedSDLGamepadsForPort(uint8_t portIndex
return mConnectedSDLGamepads;
}

std::vector<std::string> ConnectedPhysicalDeviceManager::GetConnectedSDLGamepadNames() {
return mConnectedSDLGamepadNames;
}

void ConnectedPhysicalDeviceManager::HandlePhysicalDeviceConnect(int32_t sdlDeviceIndex) {
RefreshConnectedSDLGamepads();
}
Expand All @@ -23,6 +27,7 @@ void ConnectedPhysicalDeviceManager::HandlePhysicalDeviceDisconnect(int32_t sdlJ

void ConnectedPhysicalDeviceManager::RefreshConnectedSDLGamepads() {
mConnectedSDLGamepads.clear();
mConnectedSDLGamepadNames.clear();
for (int32_t i = 0; i < SDL_NumJoysticks(); i++) {
// skip if this SDL joystick isn't a Gamepad
if (!SDL_IsGameController(i)) {
Expand All @@ -31,8 +36,10 @@ void ConnectedPhysicalDeviceManager::RefreshConnectedSDLGamepads() {

auto gamepad = SDL_GameControllerOpen(i);
auto instanceId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad));
auto name = SDL_GameControllerName(gamepad);

mConnectedSDLGamepads[instanceId] = gamepad;
mConnectedSDLGamepadNames.push_back(name);
}
}
} // namespace Ship
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <unordered_map>
#include <vector>
#include <string>
#include <SDL2/SDL.h>

namespace Ship {
Expand All @@ -11,12 +13,14 @@ class ConnectedPhysicalDeviceManager {
~ConnectedPhysicalDeviceManager();

std::unordered_map<int32_t, SDL_GameController*> GetConnectedSDLGamepadsForPort(uint8_t portIndex);
std::vector<std::string> GetConnectedSDLGamepadNames();

void HandlePhysicalDeviceConnect(int32_t sdlDeviceIndex);
void HandlePhysicalDeviceDisconnect(int32_t sdlJoystickInstanceId);
void RefreshConnectedSDLGamepads();

private:
std::unordered_map<int32_t, SDL_GameController*> mConnectedSDLGamepads;
std::vector<std::string> mConnectedSDLGamepadNames;
};
} // namespace Ship

0 comments on commit ab9ca34

Please sign in to comment.