]> git.sesse.net Git - nageru/blob - nageru/input_mapping_dialog.cpp
Fix a dangling reference (found by GCC 14).
[nageru] / nageru / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include <assert.h>
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <QAbstractItemView>
8 #include <QComboBox>
9 #include <QDialogButtonBox>
10 #include <QFileDialog>
11 #include <QHeaderView>
12 #include <QList>
13 #include <QMessageBox>
14 #include <QPushButton>
15 #include <QTableWidget>
16 #include <QVariant>
17 #include <functional>
18 #include <set>
19 #include <string>
20 #include <utility>
21
22 #include "alsa_pool.h"
23 #include "audio_mixer.h"
24 #include "defs.h"
25 #include "input_mapping.h"
26 #include "shared/post_to_main_thread.h"
27 #include "ui_input_mapping.h"
28
29 using namespace std;
30 using namespace std::placeholders;
31
32 namespace {
33
34 bool uses_device(const InputMapping &mapping, DeviceSpec device)
35 {
36         for (const InputMapping::Bus &bus : mapping.buses) {
37                 if (bus.device == device) {
38                         return true;
39                 }
40         }
41         return false;
42 }
43
44 }  // namespace
45
46 InputMappingDialog::InputMappingDialog()
47         : ui(new Ui::InputMappingDialog),
48           mapping(global_audio_mixer->get_input_mapping()),
49           old_mapping(mapping),
50           devices(global_audio_mixer->get_devices())
51 {
52         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
53                 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
54         }
55
56         ui->setupUi(this);
57         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
58         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
59
60         fill_ui_from_mapping(mapping);
61         connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
62         connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
63         connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
64         connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
65         connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
66         connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
67         connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
68         connect(ui->save_button, &QPushButton::clicked, this, &InputMappingDialog::save_clicked);
69         connect(ui->load_button, &QPushButton::clicked, this, &InputMappingDialog::load_clicked);
70
71         update_button_state();
72         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
73
74         saved_callback = global_audio_mixer->get_state_changed_callback();
75         global_audio_mixer->set_state_changed_callback([this]{
76                 post_to_main_thread([this]{
77                         devices = global_audio_mixer->get_devices();
78                         for (unsigned row = 0; row < mapping.buses.size(); ++row) {
79                                 fill_row_from_bus(row, mapping.buses[row]);
80                         }
81                 });
82         });
83 }
84
85 InputMappingDialog::~InputMappingDialog()
86 {
87         global_audio_mixer->set_state_changed_callback(saved_callback);
88 }
89
90 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
91 {
92         ui->table->verticalHeader()->hide();
93         ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
94         ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
95         ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
96         ui->table->horizontalHeader()->setSectionsClickable(false);
97
98         ui->table->setRowCount(mapping.buses.size());
99         for (unsigned row = 0; row < mapping.buses.size(); ++row) {
100                 fill_row_from_bus(row, mapping.buses[row]);
101         }
102 }
103
104 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
105 {
106         QString name(QString::fromStdString(bus.name));
107         ui->table->setItem(row, 0, new QTableWidgetItem(name));
108
109         // Card choices. If there's already a combobox here, we try to modify
110         // the elements in-place, so that the UI doesn't go away under the user's feet
111         // if they are in the process of choosing an item.
112         QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
113         if (card_combo == nullptr) {
114                 card_combo = new QComboBox;
115         }
116         unsigned current_index = 0;
117         if (card_combo->count() == 0) {
118                 card_combo->addItem(QString("(none)   "));
119         }
120         for (const auto &spec_and_info : devices) {
121                 QString label(QString::fromStdString(spec_and_info.second.display_name));
122                 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
123                         ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
124                         if (state == ALSAPool::Device::State::EMPTY) {
125                                 continue;
126                         } else if (state == ALSAPool::Device::State::STARTING) {
127                                 label += " (busy)";
128                         } else if (state == ALSAPool::Device::State::DEAD) {
129                                 label += " (dead)";
130                         }
131                 } else if (!global_audio_mixer->get_active(spec_and_info.first)) {
132                         // Should nominally be skipped, but if we used it before it went away,
133                         // we'll need to allow the user to still see it.
134                         if (uses_device(mapping, spec_and_info.first) ||
135                             uses_device(old_mapping, spec_and_info.first)) {
136                                 label += " (dead)";
137                         } else {
138                                 continue;
139                         }
140                 }
141                 ++current_index;
142                 if (unsigned(card_combo->count()) > current_index) {
143                         card_combo->setItemText(current_index, label + "   ");
144                         card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
145                 } else {
146                         card_combo->addItem(
147                                 label + "   ",
148                                 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
149                 }
150                 if (bus.device == spec_and_info.first) {
151                         card_combo->setCurrentIndex(current_index);
152                 }
153         }
154         // Remove any excess items from earlier. (This is only for paranoia;
155         // they should be held, so it shouldn't matter.)
156         while (unsigned(card_combo->count()) > current_index + 1) {
157                 card_combo->removeItem(current_index + 1);
158         }
159         connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
160                 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
161         ui->table->setCellWidget(row, 1, card_combo);
162
163         setup_channel_choices_from_bus(row, bus);
164 }
165
166 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
167 {
168         // Left and right channel.
169         // TODO: If there's already a widget here, modify it instead of creating a new one,
170         // as we do with card choices.
171         for (unsigned channel = 0; channel < 2; ++channel) {
172                 QComboBox *channel_combo = new QComboBox;
173                 channel_combo->addItem(QString("(none)"));
174                 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
175                     bus.device.type == InputSourceType::ALSA_INPUT) {
176                         auto device_it = devices.find(bus.device);
177                         assert(device_it != devices.end());
178                         unsigned num_device_channels = device_it->second.num_channels;
179                         for (unsigned source = 0; source < num_device_channels; ++source) {
180                                 char buf[256];
181                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
182                                 channel_combo->addItem(QString(buf));
183                         }
184                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
185                 } else {
186                         assert(bus.device.type == InputSourceType::SILENCE);
187                         channel_combo->setCurrentIndex(0);
188                 }
189                 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
190                         bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
191                 ui->table->setCellWidget(row, 2 + channel, channel_combo);
192         }
193 }
194
195 void InputMappingDialog::ok_clicked()
196 {
197         global_audio_mixer->set_state_changed_callback(saved_callback);
198         global_audio_mixer->set_input_mapping(mapping);
199         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
200                 global_audio_mixer->set_bus_settings(bus_index, bus_settings[bus_index]);
201                 global_audio_mixer->reset_peak(bus_index);
202         }
203         accept();
204 }
205
206 void InputMappingDialog::cancel_clicked()
207 {
208         global_audio_mixer->set_state_changed_callback(saved_callback);
209         global_audio_mixer->set_input_mapping(old_mapping);
210         reject();
211 }
212
213 void InputMappingDialog::cell_changed(int row, int column)
214 {
215         if (column != 0) {
216                 // Spurious; only really the name column should fire these.
217                 return;
218         }
219         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
220 }
221
222 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
223 {
224         uint64_t key = card_combo->itemData(index).toULongLong();
225         mapping.buses[row].device = key_to_DeviceSpec(key);
226         setup_channel_choices_from_bus(row, mapping.buses[row]);
227 }
228
229 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
230 {
231         mapping.buses[row].source_channel[channel] = index - 1;
232 }
233
234 void InputMappingDialog::add_clicked()
235 {
236         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
237         ui->table->setRangeSelected(all, false);
238
239         InputMapping::Bus new_bus;
240         new_bus.name = "New input";
241         new_bus.device.type = InputSourceType::SILENCE;
242         mapping.buses.push_back(new_bus);
243         bus_settings.push_back(AudioMixer::get_default_bus_settings());
244         ui->table->setRowCount(mapping.buses.size());
245
246         unsigned row = mapping.buses.size() - 1;
247         fill_row_from_bus(row, new_bus);
248         ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
249         update_button_state();
250 }
251
252 void InputMappingDialog::remove_clicked()
253 {
254         assert(ui->table->rowCount() != 0);
255
256         set<int, greater<int>> rows_to_delete;  // Need to remove in reverse order.
257         for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
258                 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
259                         rows_to_delete.insert(row);
260                 }
261         }
262         if (rows_to_delete.empty()) {
263                 rows_to_delete.insert(ui->table->rowCount() - 1);
264         }
265
266         for (int row : rows_to_delete) {
267                 ui->table->removeRow(row);
268                 mapping.buses.erase(mapping.buses.begin() + row);
269                 bus_settings.erase(bus_settings.begin() + row);
270         }
271         update_button_state();
272 }
273
274 void InputMappingDialog::updown_clicked(int direction)
275 {
276         assert(ui->table->selectedRanges().size() == 1);
277         const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
278         int a_row = range.bottomRow();
279         int b_row = range.bottomRow() + direction;
280
281         swap(mapping.buses[a_row], mapping.buses[b_row]);
282         swap(bus_settings[a_row], bus_settings[b_row]);
283         fill_row_from_bus(a_row, mapping.buses[a_row]);
284         fill_row_from_bus(b_row, mapping.buses[b_row]);
285
286         QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
287         QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
288         ui->table->setRangeSelected(a_sel, false);
289         ui->table->setRangeSelected(b_sel, true);
290 }
291
292 void InputMappingDialog::save_clicked()
293 {
294 #if HAVE_CEF
295         // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
296         QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
297 #else
298         QFileDialog::Option options(QFileDialog::Option(0));
299 #endif
300         QString filename = QFileDialog::getSaveFileName(this,
301                 "Save input mapping", QString(), tr("Mapping files (*.mapping)"), /*selectedFilter=*/nullptr, options);
302         if (!filename.endsWith(".mapping")) {
303                 filename += ".mapping";
304         }
305         if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
306                 QMessageBox box;
307                 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
308                 box.exec();
309         }
310 }
311
312 void InputMappingDialog::load_clicked()
313 {
314 #if HAVE_CEF
315         // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
316         QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
317 #else
318         QFileDialog::Option options(QFileDialog::Option(0));
319 #endif
320         QString filename = QFileDialog::getOpenFileName(this,
321                 "Load input mapping", QString(), tr("Mapping files (*.mapping)"), /*selectedFilter=*/nullptr, options);
322         InputMapping new_mapping;
323         if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
324                 QMessageBox box;
325                 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
326                 box.exec();
327                 return;
328         }
329
330         mapping = new_mapping;
331         bus_settings.clear();
332         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
333                 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
334         }
335         devices = global_audio_mixer->get_devices();  // New dead cards may have been made.
336         fill_ui_from_mapping(mapping);
337 }
338
339 void InputMappingDialog::update_button_state()
340 {
341         ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
342         ui->remove_button->setDisabled(mapping.buses.size() == 0);
343         ui->up_button->setDisabled(
344                 ui->table->selectedRanges().empty() ||
345                 ui->table->selectedRanges()[0].bottomRow() == 0);
346         ui->down_button->setDisabled(
347                 ui->table->selectedRanges().empty() ||
348                 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);
349 }