duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

controllerglobalsettingswidget.cpp (6810B)


      1 // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
      2 // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
      3 
      4 #include "controllerglobalsettingswidget.h"
      5 #include "controllersettingswindow.h"
      6 #include "controllersettingwidgetbinder.h"
      7 #include "qtutils.h"
      8 #include "settingwidgetbinder.h"
      9 
     10 #include "util/sdl_input_source.h"
     11 
     12 ControllerGlobalSettingsWidget::ControllerGlobalSettingsWidget(QWidget* parent, ControllerSettingsWindow* dialog)
     13   : QWidget(parent), m_dialog(dialog)
     14 {
     15   m_ui.setupUi(this);
     16 
     17   SettingsInterface* sif = dialog->getEditingSettingsInterface();
     18 
     19   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLSource, "InputSources", "SDL", true);
     20   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLEnhancedMode, "InputSources",
     21                                                "SDLControllerEnhancedMode", false);
     22   connect(m_ui.enableSDLSource, &QCheckBox::checkStateChanged, this,
     23           &ControllerGlobalSettingsWidget::updateSDLOptionsEnabled);
     24   connect(m_ui.ledSettings, &QToolButton::clicked, this, &ControllerGlobalSettingsWidget::ledSettingsClicked);
     25 
     26 #ifdef __APPLE__
     27   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLIOKitDriver, "InputSources", "SDLIOKitDriver", true);
     28   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLMFIDriver, "InputSources", "SDLMFIDriver", true);
     29 #else
     30   m_ui.sdlGridLayout->removeWidget(m_ui.enableSDLIOKitDriver);
     31   delete m_ui.enableSDLIOKitDriver;
     32   m_ui.enableSDLIOKitDriver = nullptr;
     33   m_ui.sdlGridLayout->removeWidget(m_ui.enableSDLMFIDriver);
     34   delete m_ui.enableSDLMFIDriver;
     35   m_ui.enableSDLMFIDriver = nullptr;
     36 #endif
     37 
     38 #ifdef _WIN32
     39   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableDInputSource, "InputSources", "DInput", false);
     40   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableXInputSource, "InputSources", "XInput", false);
     41   SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableRawInput, "InputSources", "RawInput", false);
     42 #else
     43   m_ui.mainLayout->removeWidget(m_ui.xinputGroup);
     44   delete m_ui.xinputGroup;
     45   m_ui.xinputGroup = nullptr;
     46   m_ui.mainLayout->removeWidget(m_ui.dinputGroup);
     47   delete m_ui.dinputGroup;
     48   m_ui.dinputGroup = nullptr;
     49 #endif
     50 
     51   ControllerSettingWidgetBinder::BindWidgetToInputProfileBool(sif, m_ui.enableMouseMapping, "UI", "EnableMouseMapping",
     52                                                               false);
     53   SettingWidgetBinder::BindWidgetToEnumSetting(sif, m_ui.multitapMode, "ControllerPorts", "MultitapMode",
     54                                                &Settings::ParseMultitapModeName, &Settings::GetMultitapModeName,
     55                                                Settings::DEFAULT_MULTITAP_MODE);
     56   ControllerSettingWidgetBinder::BindWidgetToInputProfileFloat(sif, m_ui.pointerXScale, "ControllerPorts",
     57                                                                "PointerXScale", 8.0f);
     58   ControllerSettingWidgetBinder::BindWidgetToInputProfileFloat(sif, m_ui.pointerYScale, "ControllerPorts",
     59                                                                "PointerYScale", 8.0f);
     60 
     61   if (dialog->isEditingProfile())
     62   {
     63     m_ui.useProfileHotkeyBindings->setChecked(
     64       m_dialog->getBoolValue("ControllerPorts", "UseProfileHotkeyBindings", false));
     65     connect(m_ui.useProfileHotkeyBindings, &QCheckBox::checkStateChanged, this, [this](int new_state) {
     66       m_dialog->setBoolValue("ControllerPorts", "UseProfileHotkeyBindings", (new_state == Qt::Checked));
     67       emit bindingSetupChanged();
     68     });
     69   }
     70   else
     71   {
     72     // remove profile options from the UI.
     73     m_ui.mainLayout->removeWidget(m_ui.profileSettings);
     74     delete m_ui.profileSettings;
     75     m_ui.profileSettings = nullptr;
     76   }
     77 
     78   if (dialog->isEditingGameSettings())
     79     m_ui.deviceListGroup->setEnabled(false);
     80 
     81   connect(m_ui.multitapMode, &QComboBox::currentIndexChanged, this, [this]() { emit bindingSetupChanged(); });
     82 
     83   connect(m_ui.pointerXScale, &QSlider::valueChanged, this,
     84           [this](int value) { m_ui.pointerXScaleLabel->setText(QStringLiteral("%1").arg(value)); });
     85   connect(m_ui.pointerYScale, &QSlider::valueChanged, this,
     86           [this](int value) { m_ui.pointerYScaleLabel->setText(QStringLiteral("%1").arg(value)); });
     87   m_ui.pointerXScaleLabel->setText(QStringLiteral("%1").arg(m_ui.pointerXScale->value()));
     88   m_ui.pointerYScaleLabel->setText(QStringLiteral("%1").arg(m_ui.pointerYScale->value()));
     89 
     90   updateSDLOptionsEnabled();
     91 }
     92 
     93 ControllerGlobalSettingsWidget::~ControllerGlobalSettingsWidget() = default;
     94 
     95 void ControllerGlobalSettingsWidget::addDeviceToList(const QString& identifier, const QString& name)
     96 {
     97   QListWidgetItem* item = new QListWidgetItem();
     98   item->setText(QStringLiteral("%1: %2").arg(identifier).arg(name));
     99   item->setData(Qt::UserRole, identifier);
    100   m_ui.deviceList->addItem(item);
    101 }
    102 
    103 void ControllerGlobalSettingsWidget::removeDeviceFromList(const QString& identifier)
    104 {
    105   const int count = m_ui.deviceList->count();
    106   for (int i = 0; i < count; i++)
    107   {
    108     QListWidgetItem* item = m_ui.deviceList->item(i);
    109     if (item->data(Qt::UserRole) != identifier)
    110       continue;
    111 
    112     delete m_ui.deviceList->takeItem(i);
    113     break;
    114   }
    115 }
    116 
    117 void ControllerGlobalSettingsWidget::ledSettingsClicked()
    118 {
    119   ControllerLEDSettingsDialog dialog(this, m_dialog);
    120   dialog.exec();
    121 }
    122 
    123 void ControllerGlobalSettingsWidget::updateSDLOptionsEnabled()
    124 {
    125   const bool enabled = m_ui.enableSDLSource->isChecked();
    126   m_ui.enableSDLEnhancedMode->setEnabled(enabled);
    127   m_ui.ledSettings->setEnabled(enabled);
    128 }
    129 
    130 ControllerLEDSettingsDialog::ControllerLEDSettingsDialog(QWidget* parent, ControllerSettingsWindow* dialog)
    131   : QDialog(parent), m_dialog(dialog)
    132 {
    133   m_ui.setupUi(this);
    134 
    135   linkButton(m_ui.SDL0LED, 0);
    136   linkButton(m_ui.SDL1LED, 1);
    137   linkButton(m_ui.SDL2LED, 2);
    138   linkButton(m_ui.SDL3LED, 3);
    139 
    140   SettingsInterface* sif = dialog->getEditingSettingsInterface();
    141 
    142   ControllerSettingWidgetBinder::BindWidgetToInputProfileBool(sif, m_ui.enableSDLPS5PlayerLED, "InputSources",
    143                                                               "SDLPS5PlayerLED", false);
    144   connect(m_ui.buttonBox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &QDialog::accept);
    145 }
    146 
    147 ControllerLEDSettingsDialog::~ControllerLEDSettingsDialog() = default;
    148 
    149 void ControllerLEDSettingsDialog::linkButton(ColorPickerButton* button, u32 player_id)
    150 {
    151   std::string key(fmt::format("Player{}LED", player_id));
    152   const u32 current_value =
    153     SDLInputSource::ParseRGBForPlayerId(m_dialog->getStringValue("SDLExtra", key.c_str(), ""), player_id);
    154   button->setColor(current_value);
    155 
    156   connect(button, &ColorPickerButton::colorChanged, this, [this, key = std::move(key)](u32 new_rgb) {
    157     m_dialog->setStringValue("SDLExtra", key.c_str(), fmt::format("{:06X}", new_rgb).c_str());
    158   });
    159 }