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