Skip to content

Commit

Permalink
Update controller mappings when new controller is added
Browse files Browse the repository at this point in the history
  • Loading branch information
streetpea committed Sep 29, 2024
1 parent 945df95 commit 6028156
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions gui/include/controllermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Controller : public QObject
#endif
QString GetName();
QString GetType();
QString GetGUIDString();
ChiakiControllerState GetState();
void SetRumble(uint8_t left, uint8_t right);
void SetTriggerEffects(uint8_t type_left, const uint8_t *data_left, uint8_t type_right, const uint8_t *data_right);
Expand Down
1 change: 1 addition & 0 deletions gui/include/qmlbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class QmlBackend : public QObject
bool controller_mapping_default_mapping = false;
bool controller_mapping_altered = false;
Controller *controller_mapping_controller = {};
QList<QString> current_controller_guids = {};
int controller_mapping_id = -1;
QString controller_mapping_controller_guid = "";
QString controller_mapping_controller_type = "";
Expand Down
1 change: 1 addition & 0 deletions gui/include/qmlcontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class QmlController : public QObject
bool isHandheld() const;
bool isSteamVirtual() const;
bool isDualSenseEdge() const;
QString GetGUID() const;

private:
void sendKey(Qt::Key key, Qt::KeyboardModifiers modifiers = Qt::NoModifier);
Expand Down
15 changes: 15 additions & 0 deletions gui/src/controllermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,21 @@ QString Controller::GetType()
#endif
}

QString Controller::GetGUIDString()
{
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
if(!controller)
return QString();
SDL_Joystick *js = SDL_GameControllerGetJoystick(controller);
char guid_str[256];
SDL_JoystickGUID guid = SDL_JoystickGetGUID(js);
SDL_JoystickGetGUIDString(guid, guid_str, sizeof(guid_str));
return QString("%1").arg(guid_str);
#else
return QString();
#endif
}

QString Controller::GetName()
{
#ifdef CHIAKI_GUI_ENABLE_SDL_GAMECONTROLLER
Expand Down
25 changes: 17 additions & 8 deletions gui/src/qmlbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ QmlBackend::QmlBackend(Settings *settings, QmlMainWindow *window)
updateControllers();
updateControllerMappings();
connect(settings, &Settings::ControllerMappingsUpdated, this, &QmlBackend::updateControllerMappings);
connect(this, &QmlBackend::controllersChanged, this, &QmlBackend::updateControllerMappings);
auto_connect_mac = settings->GetAutoConnectHost().GetServerMAC();
auto_connect_nickname = settings->GetAutoConnectHost().GetServerNickname();
psn_auto_connect_timer = new QTimer(this);
Expand Down Expand Up @@ -1126,6 +1127,9 @@ void QmlBackend::updateControllers()
controller_mapping_controller = nullptr;
controllerMappingQuit();
}
QString guid = it.value()->GetGUID();
if(current_controller_guids.contains(guid))
current_controller_guids.removeOne(guid);
it.value()->deleteLater();
it = controllers.erase(it);
changed = true;
Expand All @@ -1137,6 +1141,9 @@ void QmlBackend::updateControllers()
if (!controller)
continue;
controllers[id] = new QmlController(controller, window, this);
QString guid = controller->GetGUIDString();
if(!current_controller_guids.contains(guid))
current_controller_guids.append(guid);
connect(controller, &Controller::UpdatingControllerMapping, this, &QmlBackend::controllerMappingUpdate);
connect(controller, &Controller::NewButtonMapping, this, &QmlBackend::controllerMappingChangeButton);
changed = true;
Expand Down Expand Up @@ -1234,15 +1241,17 @@ void QmlBackend::updateControllerMappings()
for(int i=0; i<mapping_guids.length(); i++)
{
QString guid = mapping_guids.at(i);
if(!current_controller_guids.contains(guid))
continue;
if(!controller_mapping_original_controller_mappings.contains(guid))
{
SDL_JoystickGUID real_guid = SDL_JoystickGetGUIDFromString(guid.toUtf8().constData());
char *mapping = SDL_GameControllerMappingForGUID(real_guid);
const SDL_JoystickGUID real_guid = SDL_JoystickGetGUIDFromString(guid.toUtf8().constData());
const char *mapping = SDL_GameControllerMappingForGUID(real_guid);
QString original_controller_mapping(mapping);
SDL_free(mapping);
SDL_free((char *)mapping);
if(original_controller_mapping.isEmpty())
{
qCWarning(chiakiGui) << "Error retrieving controller mapping " << SDL_GetError();
qCWarning(chiakiGui) << "Error retrieving controller mapping of GUID " << guid << "with error: " << SDL_GetError();
return;
}
controller_mapping_original_controller_mappings.insert(guid, original_controller_mapping);
Expand All @@ -1254,7 +1263,7 @@ void QmlBackend::updateControllerMappings()
qCWarning(chiakiGui) << "Error setting controller mapping for guid: " << guid << " with error: " << SDL_GetError();
break;
case 0:
qCInfo(chiakiGui) << "Updated controller mapping updated for guid: " << guid;
qCInfo(chiakiGui) << "Updated controller mapping for guid: " << guid;
break;
case 1:
qCInfo(chiakiGui) << "Added controller mapping for guid: " << guid;
Expand Down Expand Up @@ -1377,10 +1386,10 @@ void QmlBackend::controllerMappingUpdate(Controller *controller)
return;
}
controller_mapping_id = controller->GetDeviceID();
char *mapping = SDL_GameControllerMapping(controller->GetController());
const char *mapping = SDL_GameControllerMapping(controller->GetController());
QString original_controller_mapping(mapping);
qCInfo(chiakiGui) << "Original controller mapping: " << original_controller_mapping;
SDL_free(mapping);
SDL_free((char *)mapping);
if(original_controller_mapping.isEmpty())
{
qCWarning(chiakiGui) << "Error retrieving controller mapping " << SDL_GetError();
Expand Down Expand Up @@ -1467,7 +1476,7 @@ void QmlBackend::controllerMappingReset()
qCWarning(chiakiGui) << "Error setting controller mapping for guid: " << controller_mapping_controller_guid << " with error: " << SDL_GetError();
break;
case 0:
qCInfo(chiakiGui) << "Updated controller mapping updated for guid: " << controller_mapping_controller_guid;
qCInfo(chiakiGui) << "Updated controller mapping for guid: " << controller_mapping_controller_guid;
break;
case 1:
qCInfo(chiakiGui) << "Added controller mapping for guid: " << controller_mapping_controller_guid;
Expand Down
5 changes: 5 additions & 0 deletions gui/src/qmlcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ bool QmlController::isDualSenseEdge() const
return controller->IsDualSenseEdge();
}

QString QmlController::GetGUID() const
{
return controller->GetGUIDString();
}

void QmlController::sendKey(Qt::Key key, Qt::KeyboardModifiers modifiers)
{
QKeyEvent press(QEvent::KeyPress, key, modifiers);
Expand Down

0 comments on commit 6028156

Please sign in to comment.