Skip to content

Commit

Permalink
Fix controller mappings in stream session
Browse files Browse the repository at this point in the history
  • Loading branch information
streetpea committed Oct 3, 2024
1 parent 799f441 commit 1df3cc4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gui/include/qmlbackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class QmlBackend : public QObject
bool controller_mapping_altered = false;
bool disable_zero_copy = false;
Controller *controller_mapping_controller = {};
QList<QString> current_controller_guids = {};
QList<QString> controller_guids_to_update = {};
int controller_mapping_id = -1;
QString controller_mapping_controller_guid = "";
QString controller_mapping_controller_type = "";
Expand Down
5 changes: 4 additions & 1 deletion gui/include/streamsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct StreamSessionConnectInfo
RumbleHapticsIntensity rumble_haptics_intensity;
bool buttons_by_pos;
bool start_mic_unmuted;
QMap<QString, QString> controller_mappings;
#if CHIAKI_GUI_ENABLE_STEAMDECK_NATIVE
bool vertical_sdeck;
bool enable_steamdeck_haptics;
Expand Down Expand Up @@ -153,7 +154,8 @@ class StreamSession : public QObject
QList<double> packet_loss_history;
bool cant_display = false;
int haptics_handheld;

QList<QString> controller_guids_to_update;
QMap<QString, QString> controller_mappings;
QHash<int, Controller *> controllers;
#if CHIAKI_GUI_ENABLE_SETSU
Setsu *setsu;
Expand Down Expand Up @@ -222,6 +224,7 @@ class StreamSession : public QObject
void PushHapticsFrame(uint8_t *buf, size_t buf_size);
void CantDisplayMessage(bool cant_display);
ChiakiErrorCode InitiatePsnConnection(QString psn_token);
void updateControllerMappings();
#ifdef Q_OS_MACOS
void SetMicAuthorization(Authorization authorization);
#endif
Expand Down
18 changes: 11 additions & 7 deletions gui/src/qmlbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,8 @@ void QmlBackend::updateControllers()
controllerMappingQuit();
}
QString guid = it.value()->GetGUID();
if(current_controller_guids.contains(guid))
current_controller_guids.removeOne(guid);
if(controller_guids_to_update.contains(guid))
controller_guids_to_update.removeOne(guid);
it.value()->deleteLater();
it = controllers.erase(it);
changed = true;
Expand All @@ -1142,8 +1142,7 @@ void QmlBackend::updateControllers()
continue;
controllers[id] = new QmlController(controller, window, this);
QString guid = controller->GetGUIDString();
if(!current_controller_guids.contains(guid))
current_controller_guids.append(guid);
controller_guids_to_update.append(guid);
connect(controller, &Controller::UpdatingControllerMapping, this, &QmlBackend::controllerMappingUpdate);
connect(controller, &Controller::NewButtonMapping, this, &QmlBackend::controllerMappingChangeButton);
changed = true;
Expand Down Expand Up @@ -1241,7 +1240,7 @@ void QmlBackend::updateControllerMappings()
for(int i=0; i<mapping_guids.length(); i++)
{
QString guid = mapping_guids.at(i);
if(!current_controller_guids.contains(guid))
if(!controller_guids_to_update.contains(guid))
continue;
if(!controller_mapping_original_controller_mappings.contains(guid))
{
Expand Down Expand Up @@ -1270,7 +1269,9 @@ void QmlBackend::updateControllerMappings()
break;
default:
qCInfo(chiakiGui) << "Unidentified problem mapping for guid: " << guid;
break;
}
controller_guids_to_update.removeOne(guid);
}
}

Expand Down Expand Up @@ -1364,7 +1365,10 @@ void QmlBackend::updateButton(int chiaki_button, QString physical_button, int ne
controller_mapping_physical_button_mappings.insert(new_mapping_buttons.at(new_index), QString());
new_mapping_buttons.remove(new_index);
}
new_mapping_buttons.append(physical_button);
if(new_index == 0)
new_mapping_buttons.prepend(physical_button);
else
new_mapping_buttons.append(physical_button);
controller_mapping_controller_mappings.insert(button, new_mapping_buttons);
controller_mapping_physical_button_mappings.insert(physical_button, button);
if(controller_mapping_controller_mappings == controller_mapping_applied_controller_mappings)
Expand Down Expand Up @@ -1425,7 +1429,7 @@ void QmlBackend::controllerMappingUpdate(Controller *controller)
if(controller_mapping_controller_mappings.contains(key))
{
auto update_list = controller_mapping_controller_mappings.value(key);
individual_mapping_list += update_list;
individual_mapping_list = update_list + individual_mapping_list;
}
controller_mapping_controller_mappings.insert(key, individual_mapping_list);
for(int j = 0; j < individual_mapping_list.length(); j++)
Expand Down
38 changes: 38 additions & 0 deletions gui/src/streamsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ StreamSessionConnectInfo::StreamSessionConnectInfo(
this->buttons_by_pos = settings->GetButtonsByPosition();
this->start_mic_unmuted = settings->GetStartMicUnmuted();
this->packet_loss_max = settings->GetPacketLossMax();
this->controller_mappings = settings->GetControllerMappings();
#if CHIAKI_GUI_ENABLE_STEAMDECK_NATIVE
this->enable_steamdeck_haptics = settings->GetSteamDeckHapticsEnabled();
this->vertical_sdeck = settings->GetVerticalDeckEnabled();
Expand Down Expand Up @@ -224,6 +225,7 @@ StreamSession::StreamSession(const StreamSessionConnectInfo &connect_info, QObje

host = connect_info.host;
QByteArray host_str = connect_info.host.toUtf8();
controller_mappings = connect_info.controller_mappings;

ChiakiConnectInfo chiaki_connect_info = {};
chiaki_connect_info.ps5 = chiaki_target_is_ps5(connect_info.target);
Expand Down Expand Up @@ -738,6 +740,9 @@ void StreamSession::UpdateGamepads()
if(!controller->IsConnected())
{
CHIAKI_LOGI(log.GetChiakiLog(), "Controller %d disconnected", controller->GetDeviceID());
QString guid = controller->GetGUIDString();
if(controller_guids_to_update.contains(guid))
controller_guids_to_update.removeOne(guid);
controllers.remove(controller_id);
if (controller->IsDualSense() || controller->IsDualSenseEdge())
{
Expand All @@ -757,15 +762,18 @@ void StreamSession::UpdateGamepads()
if(!controllers.contains(controller_id))
{
auto controller = ControllerManager::GetInstance()->OpenController(controller_id);
QString guid = controller->GetGUIDString();
if(!controller)
{
CHIAKI_LOGE(log.GetChiakiLog(), "Failed to open controller %d", controller_id);
continue;
}
controller_guids_to_update.append(guid);
CHIAKI_LOGI(log.GetChiakiLog(), "Controller %d opened: \"%s\"", controller_id, controller->GetName().toLocal8Bit().constData());
connect(controller, &Controller::StateChanged, this, &StreamSession::SendFeedbackState);
connect(controller, &Controller::MicButtonPush, this, &StreamSession::ToggleMute);
controllers[controller_id] = controller;
updateControllerMappings();
if(controller->IsHandheld())
{
#if CHIAKI_GUI_ENABLE_STEAMDECK_NATIVE
Expand Down Expand Up @@ -803,6 +811,36 @@ void StreamSession::UpdateGamepads()
#endif
}

void StreamSession::updateControllerMappings()
{
if(SDL_WasInit(SDL_INIT_GAMECONTROLLER)==0)
return;
QStringList mapping_guids = controller_mappings.keys();
for(int i=0; i<mapping_guids.length(); i++)
{
QString guid = mapping_guids.at(i);
if(!controller_guids_to_update.contains(guid))
continue;
int result = SDL_GameControllerAddMapping(controller_mappings.value(guid).toUtf8().constData());
switch(result)
{
case -1:
CHIAKI_LOGE(GetChiakiLog(), "Error setting controller mapping for guid: %s with error: %s", guid.toUtf8().constData(), SDL_GetError());
break;
case 0:
CHIAKI_LOGI(GetChiakiLog(), "Updated controller mapping for guid: %s", guid.toUtf8().constData());
break;
case 1:
CHIAKI_LOGI(GetChiakiLog(), "Added controller mapping for guid: %s", guid.toUtf8().constData());
break;
default:
CHIAKI_LOGW(GetChiakiLog(), "Unidentified problem mapping for guid: %s", guid.toUtf8().constData());
break;
}
controller_guids_to_update.removeOne(guid);
}
}

void StreamSession::SendFeedbackState()
{
ChiakiControllerState state;
Expand Down

0 comments on commit 1df3cc4

Please sign in to comment.