Debian Codegraph @

Source/Core/DolphinQt/Config/SDLHints/SDLHintsWindow.cpp

raw symbols SCIP
1 // Copyright 2026 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/SDLHints/SDLHintsWindow.h"
5
6 #include "Common/Config/Config.h"
7
8 #include "Core/Config/MainSettings.h"
9
10 #include "DolphinQt/Config/ToolTipControls/ToolTipCheckBox.h"
11 #include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
12 #include "DolphinQt/QtUtils/QtUtils.h"
13 #include "DolphinQt/QtUtils/SignalBlocking.h"
14
15 #include <QDialogButtonBox>
16 #include <QFrame>
17 #include <QHeaderView>
18 #include <QLabel>
19 #include <QPushButton>
20 #include <QResizeEvent>
21 #include <QTabWidget>
22 #include <QTableWidget>
23 #include <QVBoxLayout>
24
25 SDLHintsWindow::SDLHintsWindow(QWidget* parent) : QDialog(parent)
26 {
27 CreateMainLayout();
28
29 setWindowTitle(tr("SDL Controller Settings"));
30 }
31
32 QSize SDLHintsWindow::sizeHint() const
33 {
34 return {450, 0};
35 }
36
37 void SDLHintsWindow::CreateMainLayout()
38 {
39 setMinimumWidth(300);
40 setMinimumHeight(270);
41
42 m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
43 connect(m_button_box, &QDialogButtonBox::rejected, this, &SDLHintsWindow::OnClose);
44
45 // Create hints table
46 m_hints_table = new QTableWidget(0, 2);
47 QHeaderView* const hints_table_header = m_hints_table->horizontalHeader();
48 m_hints_table->setHorizontalHeaderLabels({tr("Name"), tr("Value")});
49 m_hints_table->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
50 m_hints_table->setSelectionMode(QTableWidget::SingleSelection);
51 hints_table_header->setSectionResizeMode(0, QHeaderView::Interactive);
52 hints_table_header->setSectionResizeMode(1, QHeaderView::Fixed);
53 hints_table_header->setMinimumSectionSize(60);
54 m_hints_table->verticalHeader()->setVisible(false);
55 hints_table_header->installEventFilter(this);
56 QObject::connect(hints_table_header, &QHeaderView::sectionResized, this,
57 &SDLHintsWindow::SectionResized);
58
59 PopulateTable();
60
61 // Create table buttons
62 auto* const add_row_btn = new NonDefaultQPushButton(tr("Add"));
63 connect(add_row_btn, &QPushButton::pressed, this, &SDLHintsWindow::AddRow);
64
65 m_rem_row_btn = new NonDefaultQPushButton(tr("Remove"));
66 m_rem_row_btn->setEnabled(false);
67 connect(m_rem_row_btn, &QPushButton::pressed, this, &SDLHintsWindow::RemoveRow);
68 connect(m_hints_table, &QTableWidget::itemSelectionChanged, this,
69 &SDLHintsWindow::SelectionChanged);
70
71 auto* const btns_layout = new QDialogButtonBox;
72 btns_layout->setContentsMargins(0, 0, 5, 5);
73 btns_layout->addButton(add_row_btn, QDialogButtonBox::ActionRole);
74 btns_layout->addButton(m_rem_row_btn, QDialogButtonBox::ActionRole);
75
76 // Create advanced tab
77 auto* advanced_layout = new QVBoxLayout();
78 advanced_layout->addWidget(m_hints_table);
79 advanced_layout->addWidget(btns_layout);
80
81 auto* advanced_frame = new QFrame();
82 advanced_frame->setLayout(advanced_layout);
83
84 // Create default tab
85 m_directinput_detection = new ToolTipCheckBox(tr("Enable DirectInput Detection"));
86 m_directinput_detection->SetDescription(
87 tr("Controls whether SDL should use DirectInput for detecting controllers. Enabling this "
88 "fixes hotplug detection issues with DualSense controllers but causes Dolphin to hang up "
89 "on shutdown when using certain 8BitDo controllers.<br><br><dolphin_emphasis>If unsure, "
90 "leave this checked.</dolphin_emphasis>"));
91 connect(m_directinput_detection, &ToolTipCheckBox::toggled, [](bool checked) {
92 Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_DIRECTINPUT, checked ? "1" : "0");
93 });
94
95 m_combine_joy_cons = new ToolTipCheckBox(tr("Use Joy-Con Pairs as a Single Controller"));
96 m_combine_joy_cons->SetDescription(
97 tr("Controls whether SDL should treat a pair of Joy-Con as a single controller or as two "
98 "separate controllers.<br><br><dolphin_emphasis>If unsure, leave this "
99 "checked.</dolphin_emphasis>"));
100 connect(m_combine_joy_cons, &ToolTipCheckBox::toggled, [](bool checked) {
101 Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS, checked ? "1" : "0");
102 });
103
104 m_horizontal_joy_cons = new ToolTipCheckBox(tr("Sideways Joy-Con"));
105 m_horizontal_joy_cons->SetDescription(
106 tr("Defines the default orientation for individual Joy-Con. This setting has no effect when "
107 "Use Joy-Con Pairs as a Single Controller is "
108 "enabled.<br><br><dolphin_emphasis>If unsure, "
109 "leave this checked.</dolphin_emphasis>"));
110 connect(m_horizontal_joy_cons, &ToolTipCheckBox::toggled, [](bool checked) {
111 Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS, checked ? "0" : "1");
112 });
113
114 m_dualsense_player_led = new ToolTipCheckBox(tr("Enable DualSense Player LEDs"));
115 m_dualsense_player_led->SetDescription(
116 tr("Controls whether the player LEDs should be lit to indicate which player is associated "
117 "with a DualSense controller.<br><br><dolphin_emphasis>If unsure, leave this "
118 "unchecked.</dolphin_emphasis>"));
119 connect(m_dualsense_player_led, &ToolTipCheckBox::toggled, [](bool checked) {
120 Config::SetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED, checked ? "1" : "0");
121 });
122
123 auto* const default_layout = new QVBoxLayout();
124 default_layout->setContentsMargins(10, 10, 10, 10);
125 default_layout->addWidget(m_directinput_detection);
126 default_layout->addWidget(m_combine_joy_cons);
127 default_layout->addWidget(m_horizontal_joy_cons);
128 default_layout->addWidget(m_dualsense_player_led);
129 default_layout->addStretch(1);
130
131 auto* const default_frame = new QFrame();
132 default_frame->setLayout(default_layout);
133
134 PopulateChecklist();
135
136 // Create the tab widget
137 m_tab_widget = new QTabWidget();
138 m_tab_widget->addTab(default_frame, tr("Main"));
139 m_tab_widget->addTab(advanced_frame, tr("Advanced"));
140
141 m_current_tab_index = 0;
142 m_tab_widget->setCurrentIndex(m_current_tab_index);
143
144 connect(m_tab_widget, &QTabWidget::currentChanged, this, &SDLHintsWindow::TabChanged);
145
146 auto* const warning_text =
147 new QLabel(tr("Dolphin must be restarted for these changes to take effect."));
148 warning_text->setWordWrap(true);
149
150 // Create main layout
151 auto* const main_layout = new QVBoxLayout();
152 main_layout->addWidget(
153 QtUtils::CreateIconWarning(this, QStyle::SP_MessageBoxWarning, warning_text), 0);
154 main_layout->addWidget(m_tab_widget, 1);
155 main_layout->addWidget(m_button_box, 0, Qt::AlignBottom | Qt::AlignRight);
156 setLayout(main_layout);
157 }
158
159 void SDLHintsWindow::PopulateTable()
160 {
161 m_hints_table->setRowCount(0);
162
163 // Loop through all the values in the SDL_Hints settings section and load them into the table
164 std::shared_ptr<Config::Layer> layer = Config::GetLayer(Config::LayerType::Base);
165 const Config::Section& section = layer->GetSection(Config::System::Main, "SDL_Hints");
166 for (auto& row_data : section)
167 {
168 const Config::Location& location = row_data.first;
169 const std::optional<std::string>& value = row_data.second;
170
171 if (value)
172 {
173 m_hints_table->insertRow(m_hints_table->rowCount());
174 m_hints_table->setItem(m_hints_table->rowCount() - 1, 0,
175 new QTableWidgetItem(QString::fromStdString(location.key)));
176 m_hints_table->setItem(m_hints_table->rowCount() - 1, 1,
177 new QTableWidgetItem(QString::fromStdString(*value)));
178 }
179 }
180 }
181
182 void SDLHintsWindow::SaveTable()
183 {
184 // Clear all the old values from the SDL_Hints section
185 std::shared_ptr<Config::Layer> layer = Config::GetLayer(Config::LayerType::Base);
186 Config::Section section = layer->GetSection(Config::System::Main, "SDL_Hints");
187
188 for (auto& row_data : section)
189 row_data.second.reset();
190
191 // Add each item still in the table to the config file
192 for (int row = 0; row < m_hints_table->rowCount(); ++row)
193 {
194 QTableWidgetItem* hint_name_item = m_hints_table->item(row, 0);
195 QTableWidgetItem* hint_value_item = m_hints_table->item(row, 1);
196
197 if (hint_name_item != nullptr && hint_value_item != nullptr)
198 {
199 const QString& hint_name = hint_name_item->text().trimmed();
200 const QString& hint_value = hint_value_item->text().trimmed();
201
202 if (!hint_name.isEmpty() && !hint_value.isEmpty())
203 {
204 const Config::Info<std::string> setting{
205 {Config::System::Main, "SDL_Hints", hint_name.toStdString()}, ""};
206 Config::SetBase(setting, hint_value.toStdString());
207 }
208 }
209 }
210 }
211
212 void SDLHintsWindow::PopulateChecklist()
213 {
214 // Populate the checklist and default to the SDL default for an invalid value
215
216 // Default to checked if incorrectly set
217 SignalBlocking(m_directinput_detection)
218 ->setChecked(Config::GetBase(Config::MAIN_SDL_HINT_JOYSTICK_DIRECTINPUT) != "0");
219
220 // Default to checked if incorrectly set
221 SignalBlocking(m_combine_joy_cons)
222 ->setChecked(Config::GetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_COMBINE_JOY_CONS) != "0");
223
224 // Default to checked if incorrectly set
225 SignalBlocking(m_horizontal_joy_cons)
226 ->setChecked(Config::GetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_VERTICAL_JOY_CONS) != "1");
227
228 // Default to checked if incorrectly set
229 SignalBlocking(m_dualsense_player_led)
230 ->setChecked(Config::GetBase(Config::MAIN_SDL_HINT_JOYSTICK_HIDAPI_PS5_PLAYER_LED) != "0");
231 }
232
233 void SDLHintsWindow::AddRow()
234 {
235 m_hints_table->insertRow(m_hints_table->rowCount());
236 m_hints_table->scrollToBottom();
237 }
238
239 void SDLHintsWindow::RemoveRow()
240 {
241 QModelIndex index = m_hints_table->selectionModel()->currentIndex();
242 m_hints_table->removeRow(index.row());
243 }
244
245 void SDLHintsWindow::SelectionChanged()
246 {
247 m_rem_row_btn->setEnabled(m_hints_table->selectionModel()->hasSelection());
248 }
249
250 void SDLHintsWindow::OnClose()
251 {
252 TabChanged(-1); // Pass -1 to indicate exit
253
254 reject();
255 }
256
257 void SDLHintsWindow::TabChanged(int new_index)
258 {
259 // Check which tab we're coming from, cur_tab_idx has not been updated yet
260 switch (m_current_tab_index)
261 {
262 case 1: // Coming from the advanced tab
263 SaveTable();
264 break;
265
266 default:
267 break;
268 }
269
270 // Check which tab we're going to
271 switch (new_index)
272 {
273 case 0: // Going to the main tab
274 PopulateChecklist();
275 break;
276
277 case 1: // Going to the advanced tab
278 PopulateTable();
279 break;
280
281 default:
282 break;
283 }
284
285 m_current_tab_index = new_index;
286 }
287
288 void SDLHintsWindow::SectionResized(int logical_index, int old_size, int new_size)
289 {
290 if (logical_index == 0 && old_size != new_size)
291 {
292 QHeaderView* const header = m_hints_table->horizontalHeader();
293 header->setMaximumSectionSize(header->size().width() - header->minimumSectionSize());
294 header->resizeSection(1, header->size().width() - new_size);
295 }
296 }
297
298 bool SDLHintsWindow::eventFilter(QObject* obj, QEvent* event)
299 {
300 auto* const table_widget = qobject_cast<QHeaderView*>(obj);
301 if (table_widget)
302 {
303 if (event->type() == QEvent::Resize)
304 {
305 auto* const resize_event = static_cast<QResizeEvent*>(event);
306 QHeaderView* header = m_hints_table->horizontalHeader();
307 header->setMaximumSectionSize(resize_event->size().width() - header->minimumSectionSize());
308 header->resizeSection(0, resize_event->size().width() - header->sectionSize(1));
309 }
310 }
311
312 return QDialog::eventFilter(obj, event);
313 }