Skip to content

Commit 7f0f02d

Browse files
committed
ConnectedPhysicalDeviceManager
1 parent e7d28a5 commit 7f0f02d

8 files changed

+99
-7
lines changed

src/controller/controldeck/ControlDeck.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace Ship {
1212

1313
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) : mSinglePlayerMappingMode(false) {
1414
mDeviceIndexMappingManager = std::make_shared<ShipDeviceIndexMappingManager>();
15+
mConnectedPhysicalDeviceManager = std::make_shared<ConnectedPhysicalDeviceManager>();
1516
}
1617

1718
ControlDeck::~ControlDeck() {
@@ -109,6 +110,10 @@ std::shared_ptr<ShipDeviceIndexMappingManager> ControlDeck::GetDeviceIndexMappin
109110
return mDeviceIndexMappingManager;
110111
}
111112

113+
std::shared_ptr<ConnectedPhysicalDeviceManager> ControlDeck::GetConnectedPhysicalDeviceManager() {
114+
return mConnectedPhysicalDeviceManager;
115+
}
116+
112117
void ControlDeck::SetSinglePlayerMappingMode(bool singlePlayer) {
113118
mSinglePlayerMappingMode = singlePlayer;
114119
}

src/controller/controldeck/ControlDeck.h

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <config/Config.h>
66
#include "controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h"
77
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
8+
#include "controller/physicaldevice/ConnectedPhysicalDeviceManager.h"
89

910
namespace Ship {
1011

@@ -28,6 +29,7 @@ class ControlDeck {
2829
bool ProcessMouseButtonEvent(bool isPressed, MouseBtn button);
2930

3031
std::shared_ptr<ShipDeviceIndexMappingManager> GetDeviceIndexMappingManager();
32+
std::shared_ptr<ConnectedPhysicalDeviceManager> GetConnectedPhysicalDeviceManager();
3133

3234
protected:
3335
bool AllGameInputBlocked();
@@ -38,6 +40,7 @@ class ControlDeck {
3840
bool mSinglePlayerMappingMode;
3941
std::unordered_map<int32_t, bool> mGameInputBlockers;
4042
std::shared_ptr<ShipDeviceIndexMappingManager> mDeviceIndexMappingManager;
43+
std::shared_ptr<ConnectedPhysicalDeviceManager> mConnectedPhysicalDeviceManager;
4144
};
4245
} // namespace Ship
4346

src/controller/physicaldevice/ConnectedPhysicalDeviceManager.cpp

+24-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,31 @@ ConnectedPhysicalDeviceManager::ConnectedPhysicalDeviceManager() {
77
ConnectedPhysicalDeviceManager::~ConnectedPhysicalDeviceManager() {
88
}
99

10-
std::vector<SDL_GameController*> ConnectedPhysicalDeviceManager::GetConnectedSDLGamepadsForPort(uint8_t portIndex) {
10+
std::unordered_map<int32_t, SDL_GameController*> ConnectedPhysicalDeviceManager::GetConnectedSDLGamepadsForPort(uint8_t portIndex) {
1111
// todo: filter somehow
1212
return mConnectedSDLGamepads;
1313
}
14+
15+
void ConnectedPhysicalDeviceManager::HandlePhysicalDeviceConnect(int32_t sdlDeviceIndex) {
16+
RefreshConnectedSDLGamepads();
17+
}
18+
19+
void ConnectedPhysicalDeviceManager::HandlePhysicalDeviceDisconnect(int32_t sdlJoystickInstanceId) {
20+
RefreshConnectedSDLGamepads();
21+
}
22+
23+
void ConnectedPhysicalDeviceManager::RefreshConnectedSDLGamepads() {
24+
mConnectedSDLGamepads.clear();
25+
for (int32_t i = 0; i < SDL_NumJoysticks(); i++) {
26+
// skip if this SDL joystick isn't a Gamepad
27+
if (!SDL_IsGameController(i)) {
28+
continue;
29+
}
30+
31+
auto gamepad = SDL_GameControllerOpen(i);
32+
auto instanceId = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(gamepad));
33+
34+
mConnectedSDLGamepads[instanceId] = gamepad;
35+
}
36+
}
1437
} // namespace Ship
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#include <vector>
3+
#include <unordered_map>
44
#include <SDL2/SDL.h>
55

66
namespace Ship {
@@ -10,9 +10,13 @@ class ConnectedPhysicalDeviceManager {
1010
ConnectedPhysicalDeviceManager();
1111
~ConnectedPhysicalDeviceManager();
1212

13-
std::vector<SDL_GameController*> GetConnectedSDLGamepadsForPort(uint8_t portIndex);
13+
std::unordered_map<int32_t, SDL_GameController*> GetConnectedSDLGamepadsForPort(uint8_t portIndex);
14+
15+
void HandlePhysicalDeviceConnect(int32_t sdlDeviceIndex);
16+
void HandlePhysicalDeviceDisconnect(int32_t sdlJoystickInstanceId);
17+
void RefreshConnectedSDLGamepads();
1418

1519
private:
16-
std::vector<SDL_GameController*> mConnectedSDLGamepads;
20+
std::unordered_map<int32_t, SDL_GameController*> mConnectedSDLGamepads;
1721
};
1822
} // namespace Ship
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "SDLAddRemoveDeviceEventHandler.h"
2+
#include <SDL2/SDL.h>
3+
#include "Context.h"
4+
5+
namespace Ship {
6+
7+
SDLAddRemoveDeviceEventHandler::~SDLAddRemoveDeviceEventHandler() {
8+
}
9+
10+
void SDLAddRemoveDeviceEventHandler::InitElement() {
11+
}
12+
13+
void SDLAddRemoveDeviceEventHandler::DrawElement() {
14+
}
15+
16+
void SDLAddRemoveDeviceEventHandler::UpdateElement() {
17+
SDL_PumpEvents();
18+
SDL_Event event;
19+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED) > 0) {
20+
// from https://wiki.libsdl.org/SDL2/SDL_ControllerDeviceEvent: which - the joystick device index for
21+
// the SDL_CONTROLLERDEVICEADDED event
22+
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->HandlePhysicalDeviceConnect(
23+
event.cdevice.which);
24+
}
25+
26+
while (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMOVED) > 0) {
27+
// from https://wiki.libsdl.org/SDL2/SDL_ControllerDeviceEvent: which - the [...] instance id for the
28+
// SDL_CONTROLLERDEVICEREMOVED [...] event
29+
Context::GetInstance()->GetControlDeck()->GetConnectedPhysicalDeviceManager()->HandlePhysicalDeviceDisconnect(
30+
event.cdevice.which);
31+
}
32+
}
33+
} // namespace Ship
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "window/gui/GuiWindow.h"
4+
5+
namespace Ship {
6+
7+
class SDLAddRemoveDeviceEventHandler : public GuiWindow {
8+
public:
9+
using GuiWindow::GuiWindow;
10+
~SDLAddRemoveDeviceEventHandler();
11+
12+
protected:
13+
void InitElement() override;
14+
void DrawElement() override;
15+
void UpdateElement() override;
16+
};
17+
} // namespace Ship

src/window/gui/Gui.cpp

+9-3
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ Gui::Gui(std::vector<std::shared_ptr<GuiWindow>> guiWindows) : mNeedsConsoleVari
7272
AddGuiWindow(std::make_shared<InputEditorWindow>(CVAR_CONTROLLER_CONFIGURATION_WINDOW_OPEN, "Input Editor"));
7373
}
7474

75-
if (GetGuiWindow("Controller Disconnected") == nullptr) {
76-
AddGuiWindow(std::make_shared<ControllerDisconnectedWindow>(CVAR_CONTROLLER_DISCONNECTED_WINDOW_OPEN,
77-
"Controller Disconnected"));
75+
// if (GetGuiWindow("Controller Disconnected") == nullptr) {
76+
// AddGuiWindow(std::make_shared<ControllerDisconnectedWindow>(CVAR_CONTROLLER_DISCONNECTED_WINDOW_OPEN,
77+
// "Controller Disconnected"));
78+
// }
79+
80+
if (GetGuiWindow("SDLAddRemoveDeviceEventHandler") == nullptr) {
81+
AddGuiWindow(std::make_shared<SDLAddRemoveDeviceEventHandler>("gOpenWindows.SDLAddRemoveDeviceEventHandler",
82+
"SDLAddRemoveDeviceEventHandler"));
7883
}
7984

85+
8086
if (GetGuiWindow("Controller Reordering") == nullptr) {
8187
AddGuiWindow(std::make_shared<ControllerReorderingWindow>(CVAR_CONTROLLER_REORDERING_WINDOW_OPEN,
8288
"Controller Reordering"));

src/window/gui/Gui.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "window/gui/InputEditorWindow.h"
1515
#include "controller/deviceindex/ControllerDisconnectedWindow.h"
1616
#include "controller/deviceindex/ControllerReorderingWindow.h"
17+
#include "controller/physicaldevice/SDLAddRemoveDeviceEventHandler.h"
1718
#include "window/gui/IconsFontAwesome4.h"
1819
#include "window/gui/GameOverlay.h"
1920
#include "window/gui/StatsWindow.h"

0 commit comments

Comments
 (0)