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())
20 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
21 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
23 fill_ui_from_mapping(mapping);
24 connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
25 connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
26 connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
27 connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
28 connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
29 connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
30 connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
31 connect(ui->save_button, &QPushButton::clicked, this, &InputMappingDialog::save_clicked);
32 connect(ui->load_button, &QPushButton::clicked, this, &InputMappingDialog::load_clicked);
34 update_button_state();
35 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
37 saved_callback = global_audio_mixer->get_state_changed_callback();
38 global_audio_mixer->set_state_changed_callback([this]{
39 post_to_main_thread([this]{
40 devices = global_audio_mixer->get_devices();
41 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
42 fill_row_from_bus(row, mapping.buses[row]);
48 InputMappingDialog::~InputMappingDialog()
50 global_audio_mixer->set_state_changed_callback(saved_callback);
53 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
55 ui->table->verticalHeader()->hide();
56 ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
57 ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
58 ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
59 ui->table->horizontalHeader()->setSectionsClickable(false);
61 ui->table->setRowCount(mapping.buses.size());
62 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
63 fill_row_from_bus(row, mapping.buses[row]);
67 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
69 QString name(QString::fromStdString(bus.name));
70 ui->table->setItem(row, 0, new QTableWidgetItem(name));
72 // Card choices. If there's already a combobox here, we try to modify
73 // the elements in-place, so that the UI doesn't go away under the user's feet
74 // if they are in the process of choosing an item.
75 QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
76 if (card_combo == nullptr) {
77 card_combo = new QComboBox;
79 unsigned current_index = 0;
80 if (card_combo->count() == 0) {
81 card_combo->addItem(QString("(none) "));
83 for (const auto &spec_and_info : devices) {
84 QString label(QString::fromStdString(spec_and_info.second.display_name));
85 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
86 ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
87 if (state == ALSAPool::Device::State::EMPTY) {
89 } else if (state == ALSAPool::Device::State::STARTING) {
91 } else if (state == ALSAPool::Device::State::DEAD) {
96 if (unsigned(card_combo->count()) > current_index) {
97 card_combo->setItemText(current_index, label + " ");
98 card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
102 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
104 if (bus.device == spec_and_info.first) {
105 card_combo->setCurrentIndex(current_index);
108 // Remove any excess items from earlier. (This is only for paranoia;
109 // they should be held, so it shouldn't matter.)
110 while (unsigned(card_combo->count()) > current_index + 1) {
111 card_combo->removeItem(current_index + 1);
113 connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
114 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
115 ui->table->setCellWidget(row, 1, card_combo);
117 setup_channel_choices_from_bus(row, bus);
120 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
122 // Left and right channel.
123 // TODO: If there's already a widget here, modify it instead of creating a new one,
124 // as we do with card choices.
125 for (unsigned channel = 0; channel < 2; ++channel) {
126 QComboBox *channel_combo = new QComboBox;
127 channel_combo->addItem(QString("(none)"));
128 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
129 bus.device.type == InputSourceType::ALSA_INPUT) {
130 auto device_it = devices.find(bus.device);
131 assert(device_it != devices.end());
132 unsigned num_device_channels = device_it->second.num_channels;
133 for (unsigned source = 0; source < num_device_channels; ++source) {
135 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
136 channel_combo->addItem(QString(buf));
138 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
140 channel_combo->setCurrentIndex(0);
142 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
143 bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
144 ui->table->setCellWidget(row, 2 + channel, channel_combo);
148 void InputMappingDialog::ok_clicked()
150 global_audio_mixer->set_state_changed_callback(saved_callback);
151 global_audio_mixer->set_input_mapping(mapping);
155 void InputMappingDialog::cancel_clicked()
157 global_audio_mixer->set_state_changed_callback(saved_callback);
158 global_audio_mixer->set_input_mapping(old_mapping);
162 void InputMappingDialog::cell_changed(int row, int column)
165 // Spurious; only really the name column should fire these.
168 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
171 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
173 uint64_t key = card_combo->itemData(index).toULongLong();
174 mapping.buses[row].device = key_to_DeviceSpec(key);
175 setup_channel_choices_from_bus(row, mapping.buses[row]);
178 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
180 mapping.buses[row].source_channel[channel] = index - 1;
183 void InputMappingDialog::add_clicked()
185 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
186 ui->table->setRangeSelected(all, false);
188 InputMapping::Bus new_bus;
189 new_bus.name = "New input";
190 new_bus.device.type = InputSourceType::SILENCE;
191 mapping.buses.push_back(new_bus);
192 ui->table->setRowCount(mapping.buses.size());
194 unsigned row = mapping.buses.size() - 1;
195 fill_row_from_bus(row, new_bus);
196 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.
197 update_button_state();
200 void InputMappingDialog::remove_clicked()
202 assert(ui->table->rowCount() != 0);
204 set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
205 for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
206 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
207 rows_to_delete.insert(row);
210 if (rows_to_delete.empty()) {
211 rows_to_delete.insert(ui->table->rowCount() - 1);
214 for (int row : rows_to_delete) {
215 ui->table->removeRow(row);
216 mapping.buses.erase(mapping.buses.begin() + row);
218 update_button_state();
221 void InputMappingDialog::updown_clicked(int direction)
223 assert(ui->table->selectedRanges().size() == 1);
224 const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
225 int a_row = range.bottomRow();
226 int b_row = range.bottomRow() + direction;
228 swap(mapping.buses[a_row], mapping.buses[b_row]);
229 fill_row_from_bus(a_row, mapping.buses[a_row]);
230 fill_row_from_bus(b_row, mapping.buses[b_row]);
232 QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
233 QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
234 ui->table->setRangeSelected(a_sel, false);
235 ui->table->setRangeSelected(b_sel, true);
238 void InputMappingDialog::save_clicked()
240 QString filename = QFileDialog::getSaveFileName(this,
241 "Save input mapping", QString(), tr("Mapping files (*.mapping)"));
242 if (!filename.endsWith(".mapping")) {
243 filename += ".mapping";
245 if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
247 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
252 void InputMappingDialog::load_clicked()
254 QString filename = QFileDialog::getOpenFileName(this,
255 "Load input mapping", QString(), tr("Mapping files (*.mapping)"));
256 InputMapping new_mapping;
257 if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
259 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
264 mapping = new_mapping;
265 devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
266 fill_ui_from_mapping(mapping);
269 void InputMappingDialog::update_button_state()
271 ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
272 ui->remove_button->setDisabled(mapping.buses.size() == 0);
273 ui->up_button->setDisabled(
274 ui->table->selectedRanges().empty() ||
275 ui->table->selectedRanges()[0].bottomRow() == 0);
276 ui->down_button->setDisabled(
277 ui->table->selectedRanges().empty() ||
278 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);