1 #include "input_mapping_dialog.h"
7 #include <QAbstractItemView>
9 #include <QDialogButtonBox>
10 #include <QFileDialog>
11 #include <QHeaderView>
13 #include <QMessageBox>
14 #include <QPushButton>
16 #include <QTableWidget>
24 #include "alsa_pool.h"
26 #include "shared/post_to_main_thread.h"
27 #include "ui_input_mapping.h"
30 using namespace std::placeholders;
32 InputMappingDialog::InputMappingDialog()
33 : ui(new Ui::InputMappingDialog),
34 mapping(global_audio_mixer->get_input_mapping()),
36 devices(global_audio_mixer->get_devices())
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));
43 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
44 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
45 ui->table->setColumnWidth(4, 200);
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);
58 update_button_state();
59 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
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();
65 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
66 fill_row_from_bus(row, mapping.buses[row], mapping);
72 InputMappingDialog::~InputMappingDialog()
74 global_audio_mixer->set_state_changed_callback(saved_callback);
77 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
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);
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);
91 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus, const InputMapping &mapping)
93 QString name(QString::fromStdString(bus.name));
94 ui->table->setItem(row, 0, new QTableWidgetItem(name));
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;
103 unsigned current_index = 0;
104 if (card_combo->count() == 0) {
105 card_combo->addItem(QString("(none) "));
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) {
113 } else if (state == ALSAPool::Device::State::STARTING) {
115 } else if (state == ALSAPool::Device::State::DEAD) {
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)));
126 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
128 if (bus.device == spec_and_info.first) {
129 card_combo->setCurrentIndex(current_index);
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);
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);
141 fill_delay_from_bus(row, bus, mapping);
142 setup_channel_choices_from_bus(row, bus);
145 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
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) {
161 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
162 channel_combo->addItem(QString(buf));
164 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
166 assert(bus.device.type == InputSourceType::SILENCE);
167 channel_combo->setCurrentIndex(0);
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);
175 void InputMappingDialog::fill_delay_from_bus(unsigned row, const InputMapping::Bus &bus, const InputMapping &mapping)
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));
186 delay_spinner->blockSignals(true);
187 if (bus.device.type == InputSourceType::SILENCE) {
188 delay_spinner->setEnabled(false);
189 delay_spinner->setValue(0.0);
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);
196 delay_spinner->setValue(0.0);
199 delay_spinner->blockSignals(false);
200 ui->table->setCellWidget(row, 4, delay_spinner);
203 void InputMappingDialog::ok_clicked()
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);
214 void InputMappingDialog::cancel_clicked()
216 global_audio_mixer->set_state_changed_callback(saved_callback);
217 global_audio_mixer->set_input_mapping(old_mapping);
221 void InputMappingDialog::cell_changed(int row, int column)
224 // Spurious; only really the name column should fire these.
227 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
230 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
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]);
238 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
240 mapping.buses[row].source_channel[channel] = index - 1;
243 void InputMappingDialog::delay_changed(unsigned row, double value)
245 mapping.extra_delay_ms[mapping.buses[row].device] = value;
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);
254 void InputMappingDialog::add_clicked()
256 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
257 ui->table->setRangeSelected(all, false);
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());
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();
272 void InputMappingDialog::remove_clicked()
274 assert(ui->table->rowCount() != 0);
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);
282 if (rows_to_delete.empty()) {
283 rows_to_delete.insert(ui->table->rowCount() - 1);
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);
291 update_button_state();
294 void InputMappingDialog::updown_clicked(int direction)
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;
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);
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);
312 void InputMappingDialog::save_clicked()
315 // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
316 QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
318 QFileDialog::Option options(QFileDialog::Option(0));
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";
325 if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
327 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
332 void InputMappingDialog::load_clicked()
335 // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
336 QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
338 QFileDialog::Option options(QFileDialog::Option(0));
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)) {
345 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
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));
355 devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
356 fill_ui_from_mapping(mapping);
359 void InputMappingDialog::update_button_state()
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);