1 #include "input_mapping_dialog.h"
3 #include "post_to_main_thread.h"
4 #include "ui_input_mapping.h"
11 using namespace std::placeholders;
13 InputMappingDialog::InputMappingDialog()
14 : ui(new Ui::InputMappingDialog),
15 mapping(global_audio_mixer->get_input_mapping()),
17 devices(global_audio_mixer->get_devices())
19 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
20 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
24 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
25 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
27 fill_ui_from_mapping(mapping);
28 connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
29 connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
30 connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
31 connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
32 connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
33 connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
34 connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
35 connect(ui->save_button, &QPushButton::clicked, this, &InputMappingDialog::save_clicked);
36 connect(ui->load_button, &QPushButton::clicked, this, &InputMappingDialog::load_clicked);
38 update_button_state();
39 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
41 saved_callback = global_audio_mixer->get_state_changed_callback();
42 global_audio_mixer->set_state_changed_callback([this]{
43 post_to_main_thread([this]{
44 devices = global_audio_mixer->get_devices();
45 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
46 fill_row_from_bus(row, mapping.buses[row]);
52 InputMappingDialog::~InputMappingDialog()
54 global_audio_mixer->set_state_changed_callback(saved_callback);
57 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
59 ui->table->verticalHeader()->hide();
60 ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
61 ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
62 ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
63 ui->table->horizontalHeader()->setSectionsClickable(false);
65 ui->table->setRowCount(mapping.buses.size());
66 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
67 fill_row_from_bus(row, mapping.buses[row]);
71 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
73 QString name(QString::fromStdString(bus.name));
74 ui->table->setItem(row, 0, new QTableWidgetItem(name));
76 // Card choices. If there's already a combobox here, we try to modify
77 // the elements in-place, so that the UI doesn't go away under the user's feet
78 // if they are in the process of choosing an item.
79 QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
80 if (card_combo == nullptr) {
81 card_combo = new QComboBox;
83 unsigned current_index = 0;
84 if (card_combo->count() == 0) {
85 card_combo->addItem(QString("(none) "));
87 for (const auto &spec_and_info : devices) {
88 QString label(QString::fromStdString(spec_and_info.second.display_name));
89 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
90 ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
91 if (state == ALSAPool::Device::State::EMPTY) {
93 } else if (state == ALSAPool::Device::State::STARTING) {
95 } else if (state == ALSAPool::Device::State::DEAD) {
100 if (unsigned(card_combo->count()) > current_index) {
101 card_combo->setItemText(current_index, label + " ");
102 card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
106 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
108 if (bus.device == spec_and_info.first) {
109 card_combo->setCurrentIndex(current_index);
112 // Remove any excess items from earlier. (This is only for paranoia;
113 // they should be held, so it shouldn't matter.)
114 while (unsigned(card_combo->count()) > current_index + 1) {
115 card_combo->removeItem(current_index + 1);
117 connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
118 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
119 ui->table->setCellWidget(row, 1, card_combo);
121 setup_channel_choices_from_bus(row, bus);
124 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
126 // Left and right channel.
127 // TODO: If there's already a widget here, modify it instead of creating a new one,
128 // as we do with card choices.
129 for (unsigned channel = 0; channel < 2; ++channel) {
130 QComboBox *channel_combo = new QComboBox;
131 channel_combo->addItem(QString("(none)"));
132 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
133 bus.device.type == InputSourceType::ALSA_INPUT) {
134 auto device_it = devices.find(bus.device);
135 assert(device_it != devices.end());
136 unsigned num_device_channels = device_it->second.num_channels;
137 for (unsigned source = 0; source < num_device_channels; ++source) {
139 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
140 channel_combo->addItem(QString(buf));
142 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
144 channel_combo->setCurrentIndex(0);
146 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
147 bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
148 ui->table->setCellWidget(row, 2 + channel, channel_combo);
152 void InputMappingDialog::ok_clicked()
154 global_audio_mixer->set_state_changed_callback(saved_callback);
155 global_audio_mixer->set_input_mapping(mapping);
156 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
157 global_audio_mixer->set_bus_settings(bus_index, bus_settings[bus_index]);
158 global_audio_mixer->reset_peak(bus_index);
163 void InputMappingDialog::cancel_clicked()
165 global_audio_mixer->set_state_changed_callback(saved_callback);
166 global_audio_mixer->set_input_mapping(old_mapping);
170 void InputMappingDialog::cell_changed(int row, int column)
173 // Spurious; only really the name column should fire these.
176 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
179 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
181 uint64_t key = card_combo->itemData(index).toULongLong();
182 mapping.buses[row].device = key_to_DeviceSpec(key);
183 setup_channel_choices_from_bus(row, mapping.buses[row]);
186 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
188 mapping.buses[row].source_channel[channel] = index - 1;
191 void InputMappingDialog::add_clicked()
193 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
194 ui->table->setRangeSelected(all, false);
196 InputMapping::Bus new_bus;
197 new_bus.name = "New input";
198 new_bus.device.type = InputSourceType::SILENCE;
199 mapping.buses.push_back(new_bus);
200 bus_settings.push_back(AudioMixer::get_default_bus_settings());
201 ui->table->setRowCount(mapping.buses.size());
203 unsigned row = mapping.buses.size() - 1;
204 fill_row_from_bus(row, new_bus);
205 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.
206 update_button_state();
209 void InputMappingDialog::remove_clicked()
211 assert(ui->table->rowCount() != 0);
213 set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
214 for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
215 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
216 rows_to_delete.insert(row);
219 if (rows_to_delete.empty()) {
220 rows_to_delete.insert(ui->table->rowCount() - 1);
223 for (int row : rows_to_delete) {
224 ui->table->removeRow(row);
225 mapping.buses.erase(mapping.buses.begin() + row);
226 bus_settings.erase(bus_settings.begin() + row);
228 update_button_state();
231 void InputMappingDialog::updown_clicked(int direction)
233 assert(ui->table->selectedRanges().size() == 1);
234 const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
235 int a_row = range.bottomRow();
236 int b_row = range.bottomRow() + direction;
238 swap(mapping.buses[a_row], mapping.buses[b_row]);
239 swap(bus_settings[a_row], bus_settings[b_row]);
240 fill_row_from_bus(a_row, mapping.buses[a_row]);
241 fill_row_from_bus(b_row, mapping.buses[b_row]);
243 QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
244 QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
245 ui->table->setRangeSelected(a_sel, false);
246 ui->table->setRangeSelected(b_sel, true);
249 void InputMappingDialog::save_clicked()
251 QString filename = QFileDialog::getSaveFileName(this,
252 "Save input mapping", QString(), tr("Mapping files (*.mapping)"));
253 if (!filename.endsWith(".mapping")) {
254 filename += ".mapping";
256 if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
258 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
263 void InputMappingDialog::load_clicked()
265 QString filename = QFileDialog::getOpenFileName(this,
266 "Load input mapping", QString(), tr("Mapping files (*.mapping)"));
267 InputMapping new_mapping;
268 if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
270 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
275 mapping = new_mapping;
276 bus_settings.clear();
277 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
278 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
280 devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
281 fill_ui_from_mapping(mapping);
284 void InputMappingDialog::update_button_state()
286 ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
287 ui->remove_button->setDisabled(mapping.buses.size() == 0);
288 ui->up_button->setDisabled(
289 ui->table->selectedRanges().empty() ||
290 ui->table->selectedRanges()[0].bottomRow() == 0);
291 ui->down_button->setDisabled(
292 ui->table->selectedRanges().empty() ||
293 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);