Skip to content

Commit 77ed9c2

Browse files
authored
Connect to DreamConn only once registered (#1828)
1 parent 7cb0fe5 commit 77ed9c2

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

Diff for: core/input/gamepad_device.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ bool GamepadDevice::gamepad_axis_input(u32 code, int value)
329329
int threshold = 16384;
330330
if (code == leftTrigger || code == rightTrigger )
331331
threshold = 100;
332-
332+
333333
if (std::abs(v) < threshold)
334334
kcode[port] |= key; // button released
335335
else
@@ -554,6 +554,9 @@ void GamepadDevice::Register(const std::shared_ptr<GamepadDevice>& gamepad)
554554
Lock _(_gamepads_mutex);
555555
_gamepads.push_back(gamepad);
556556
MapleConfigMap::UpdateVibration = updateVibration;
557+
558+
gamepad->_is_registered = true;
559+
gamepad->registered();
557560
}
558561

559562
void GamepadDevice::Unregister(const std::shared_ptr<GamepadDevice>& gamepad)

Diff for: core/input/gamepad_device.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class GamepadDevice
4040
virtual bool gamepad_btn_input(u32 code, bool pressed);
4141
virtual bool gamepad_axis_input(u32 code, int value);
4242
virtual ~GamepadDevice() = default;
43-
43+
4444
void detect_btn_input(input_detected_cb button_pressed);
4545
void detect_axis_input(input_detected_cb axis_moved);
4646
void detectButtonOrAxisInput(input_detected_cb input_changed);
@@ -91,6 +91,7 @@ class GamepadDevice
9191
save_mapping();
9292
}
9393
}
94+
bool is_registered() const { return _is_registered; }
9495

9596
static void Register(const std::shared_ptr<GamepadDevice>& gamepad);
9697
static void Unregister(const std::shared_ptr<GamepadDevice>& gamepad);
@@ -145,6 +146,7 @@ class GamepadDevice
145146
u32 rightTrigger = ~0;
146147

147148
private:
149+
virtual void registered() {}
148150
bool handleButtonInput(int port, DreamcastKey key, bool pressed);
149151
std::string make_mapping_filename(bool instance, int system, bool perGame = false);
150152

@@ -189,6 +191,7 @@ class GamepadDevice
189191
u64 _detection_start_time = 0;
190192
input_detected_cb _input_detected;
191193
bool _remappable;
194+
bool _is_registered = false;
192195
u32 digitalToAnalogState[4];
193196
std::map<DreamcastKey, int> lastAxisValue[4];
194197
bool perGameMapping = false;

Diff for: core/sdl/dreamconn.cpp

+40-16
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,19 @@ class DreamcastControllerUsbPicoConnection : public DreamcastControllerConnectio
566566
}
567567
};
568568

569-
DreamConn::DreamConn(int bus, int dreamcastControllerType) : bus(bus), dreamcastControllerType(dreamcastControllerType) {
569+
DreamConn::DreamConn(int bus, int dreamcastControllerType, const std::string& name) :
570+
bus(bus), dreamcastControllerType(dreamcastControllerType), name(name)
571+
{
572+
change_bus(bus);
573+
}
574+
575+
DreamConn::~DreamConn() {
576+
disconnect();
577+
}
578+
579+
void DreamConn::change_bus(int bus) {
580+
disconnect();
581+
dcConnection.reset();
570582
switch (dreamcastControllerType)
571583
{
572584
case TYPE_DREAMCONN:
@@ -577,16 +589,14 @@ DreamConn::DreamConn(int bus, int dreamcastControllerType) : bus(bus), dreamcast
577589
dcConnection = std::make_unique<DreamcastControllerUsbPicoConnection>(bus);
578590
break;
579591
}
580-
581-
connect();
582-
}
583-
584-
DreamConn::~DreamConn() {
585-
disconnect();
586592
}
587593

588594
void DreamConn::connect()
589595
{
596+
if (maple_io_connected) {
597+
disconnect();
598+
}
599+
590600
maple_io_connected = false;
591601
expansionDevs = 0;
592602

@@ -607,7 +617,7 @@ void DreamConn::connect()
607617

608618
if (hasVmu() || hasRumble())
609619
{
610-
NOTICE_LOG(INPUT, "Connected to DreamcastController[%d]: Type:%s, VMU:%d, Rumble Pack:%d", bus, dreamcastControllerType == 1 ? "DreamConn+ / DreamcConn S Controller" : "Dreamcast Controller USB", hasVmu(), hasRumble());
620+
NOTICE_LOG(INPUT, "Connected to DreamcastController[%d]: Type:%s, VMU:%d, Rumble Pack:%d", bus, name.c_str(), hasVmu(), hasRumble());
611621
maple_io_connected = true;
612622
}
613623
else
@@ -682,13 +692,13 @@ DreamConnGamepad::DreamConnGamepad(int maple_port, int joystick_idx, SDL_Joystic
682692
// Dreamcast Controller USB VID:1209 PID:2f07
683693
if (memcmp(DreamConnConnection::VID_PID_GUID, guid_str + 8, 16) == 0)
684694
{
685-
dreamcastControllerType = TYPE_DREAMCONN;
686695
_name = "DreamConn+ / DreamConn S Controller";
696+
dreamconn = std::make_shared<DreamConn>(maple_port, TYPE_DREAMCONN, _name);
687697
}
688698
else if (memcmp(DreamcastControllerUsbPicoConnection::VID_PID_GUID, guid_str + 8, 16) == 0)
689699
{
690-
dreamcastControllerType = TYPE_DREAMCASTCONTROLLERUSB;
691700
_name = "Dreamcast Controller USB";
701+
dreamconn = std::make_shared<DreamConn>(maple_port, TYPE_DREAMCASTCONTROLLERUSB, _name);
692702
}
693703

694704
EventManager::listen(Event::Start, handleEvent, this);
@@ -702,16 +712,28 @@ DreamConnGamepad::~DreamConnGamepad() {
702712

703713
void DreamConnGamepad::set_maple_port(int port)
704714
{
705-
if (port < 0 || port >= 4) {
706-
dreamconn.reset();
707-
}
708-
else if (dreamconn == nullptr || dreamconn->getBus() != port) {
709-
dreamconn.reset();
710-
dreamconn = std::make_shared<DreamConn>(port, dreamcastControllerType);
715+
if (dreamconn) {
716+
if (port < 0 || port >= 4) {
717+
dreamconn->disconnect();
718+
}
719+
else if (dreamconn->getBus() != port) {
720+
dreamconn->change_bus(port);
721+
if (is_registered()) {
722+
dreamconn->connect();
723+
}
724+
}
711725
}
712726
SDLGamepad::set_maple_port(port);
713727
}
714728

729+
void DreamConnGamepad::registered()
730+
{
731+
if (dreamconn)
732+
{
733+
dreamconn->connect();
734+
}
735+
}
736+
715737
void DreamConnGamepad::handleEvent(Event event, void *arg)
716738
{
717739
DreamConnGamepad *gamepad = static_cast<DreamConnGamepad*>(arg);
@@ -778,6 +800,8 @@ DreamConnGamepad::~DreamConnGamepad() {
778800
void DreamConnGamepad::set_maple_port(int port) {
779801
SDLGamepad::set_maple_port(port);
780802
}
803+
void DreamConnGamepad::registered() {
804+
}
781805
bool DreamConnGamepad::gamepad_btn_input(u32 code, bool pressed) {
782806
return SDLGamepad::gamepad_btn_input(code, pressed);
783807
}

Diff for: core/sdl/dreamconn.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ static_assert(sizeof(MapleMsg) == 1028);
5050

5151
class DreamConn
5252
{
53-
const int bus;
53+
int bus = -1;
5454
const int dreamcastControllerType;
55+
const std::string name;
5556
#ifdef USE_DREAMCASTCONTROLLER
5657
std::unique_ptr<class DreamcastControllerConnection> dcConnection;
5758
#endif
58-
bool maple_io_connected;
59+
bool maple_io_connected = false;
5960
u8 expansionDevs = 0;
6061

6162
public:
62-
DreamConn(int bus, int dreamcastControllerType);
63+
DreamConn(int bus, int dreamcastControllerType, const std::string& name);
6364

6465
~DreamConn();
6566

@@ -75,7 +76,8 @@ class DreamConn
7576
return expansionDevs & 2;
7677
}
7778

78-
private:
79+
void change_bus(int bus);
80+
7981
void connect();
8082
void disconnect();
8183
};
@@ -87,6 +89,7 @@ class DreamConnGamepad : public SDLGamepad
8789
~DreamConnGamepad();
8890

8991
void set_maple_port(int port) override;
92+
void registered() override;
9093
bool gamepad_btn_input(u32 code, bool pressed) override;
9194
bool gamepad_axis_input(u32 code, int value) override;
9295
static bool isDreamcastController(int deviceIndex);
@@ -99,5 +102,4 @@ class DreamConnGamepad : public SDLGamepad
99102
bool ltrigPressed = false;
100103
bool rtrigPressed = false;
101104
bool startPressed = false;
102-
int dreamcastControllerType;
103105
};

0 commit comments

Comments
 (0)