1 #include "input_mapping_dialog.h"
7 #include <QAbstractItemView>
9 #include <QDialogButtonBox>
10 #include <QFileDialog>
11 #include <QHeaderView>
13 #include <QMessageBox>
14 #include <QPushButton>
15 #include <QTableWidget>
22 #include "alsa_pool.h"
23 #include "audio_mixer.h"
25 #include "input_mapping.h"
26 #include "shared/post_to_main_thread.h"
27 #include "ui_input_mapping.h"
30 using namespace std::placeholders;
34 bool uses_device(const InputMapping &mapping, DeviceSpec device)
36 for (const InputMapping::Bus &bus : mapping.buses) {
37 if (bus.device == device) {
46 InputMappingDialog::InputMappingDialog()
47 : ui(new Ui::InputMappingDialog),
48 mapping(global_audio_mixer->get_input_mapping()),
50 devices(global_audio_mixer->get_devices())
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));
57 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
58 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
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);
71 update_button_state();
72 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
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]);
85 InputMappingDialog::~InputMappingDialog()
87 global_audio_mixer->set_state_changed_callback(saved_callback);
90 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
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);
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]);
104 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
106 QString name(QString::fromStdString(bus.name));
107 ui->table->setItem(row, 0, new QTableWidgetItem(name));
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;
116 unsigned current_index = 0;
117 if (card_combo->count() == 0) {
118 card_combo->addItem(QString("(none) "));
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) {
126 } else if (state == ALSAPool::Device::State::STARTING) {
128 } else if (state == ALSAPool::Device::State::DEAD) {
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)) {
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)));
148 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
150 if (bus.device == spec_and_info.first) {
151 card_combo->setCurrentIndex(current_index);
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);
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);
163 setup_channel_choices_from_bus(row, bus);
166 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
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) {
181 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
182 channel_combo->addItem(QString(buf));
184 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
186 assert(bus.device.type == InputSourceType::SILENCE);
187 channel_combo->setCurrentIndex(0);
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);
195 void InputMappingDialog::ok_clicked()
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);
206 void InputMappingDialog::cancel_clicked()
208 global_audio_mixer->set_state_changed_callback(saved_callback);
209 global_audio_mixer->set_input_mapping(old_mapping);
213 void InputMappingDialog::cell_changed(int row, int column)
216 // Spurious; only really the name column should fire these.
219 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
222 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
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]);
229 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
231 mapping.buses[row].source_channel[channel] = index - 1;
234 void InputMappingDialog::add_clicked()
236 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
237 ui->table->setRangeSelected(all, false);
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());
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();
252 void InputMappingDialog::remove_clicked()
254 assert(ui->table->rowCount() != 0);
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);
262 if (rows_to_delete.empty()) {
263 rows_to_delete.insert(ui->table->rowCount() - 1);
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);
271 update_button_state();
274 void InputMappingDialog::updown_clicked(int direction)
276 assert(ui->table->selectedRanges().size() == 1);
277 QTableWidgetSelectionRange range = ui->table->selectedRanges()[0];
278 int a_row = range.bottomRow();
279 int b_row = range.bottomRow() + direction;
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]);
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);
292 void InputMappingDialog::save_clicked()
295 // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
296 QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
298 QFileDialog::Option options(QFileDialog::Option(0));
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";
305 if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
307 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
312 void InputMappingDialog::load_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::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)) {
325 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
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));
335 devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
336 fill_ui_from_mapping(mapping);
339 void InputMappingDialog::update_button_state()
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);