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>
23 #include "alsa_pool.h"
25 #include "shared/post_to_main_thread.h"
26 #include "ui_input_mapping.h"
29 using namespace std::placeholders;
31 InputMappingDialog::InputMappingDialog()
32 : ui(new Ui::InputMappingDialog),
33 mapping(global_audio_mixer->get_input_mapping()),
35 devices(global_audio_mixer->get_devices())
37 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
38 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
42 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
43 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
45 fill_ui_from_mapping(mapping);
46 connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
47 connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
48 connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
49 connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
50 connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
51 connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
52 connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
53 connect(ui->save_button, &QPushButton::clicked, this, &InputMappingDialog::save_clicked);
54 connect(ui->load_button, &QPushButton::clicked, this, &InputMappingDialog::load_clicked);
56 update_button_state();
57 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
59 saved_callback = global_audio_mixer->get_state_changed_callback();
60 global_audio_mixer->set_state_changed_callback([this]{
61 post_to_main_thread([this]{
62 devices = global_audio_mixer->get_devices();
63 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
64 fill_row_from_bus(row, mapping.buses[row]);
70 InputMappingDialog::~InputMappingDialog()
72 global_audio_mixer->set_state_changed_callback(saved_callback);
75 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
77 ui->table->verticalHeader()->hide();
78 ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
79 ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
80 ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
81 ui->table->horizontalHeader()->setSectionsClickable(false);
83 ui->table->setRowCount(mapping.buses.size());
84 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
85 fill_row_from_bus(row, mapping.buses[row]);
89 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
91 QString name(QString::fromStdString(bus.name));
92 ui->table->setItem(row, 0, new QTableWidgetItem(name));
94 // Card choices. If there's already a combobox here, we try to modify
95 // the elements in-place, so that the UI doesn't go away under the user's feet
96 // if they are in the process of choosing an item.
97 QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
98 if (card_combo == nullptr) {
99 card_combo = new QComboBox;
101 unsigned current_index = 0;
102 if (card_combo->count() == 0) {
103 card_combo->addItem(QString("(none) "));
105 for (const auto &spec_and_info : devices) {
106 QString label(QString::fromStdString(spec_and_info.second.display_name));
107 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
108 ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
109 if (state == ALSAPool::Device::State::EMPTY) {
111 } else if (state == ALSAPool::Device::State::STARTING) {
113 } else if (state == ALSAPool::Device::State::DEAD) {
118 if (unsigned(card_combo->count()) > current_index) {
119 card_combo->setItemText(current_index, label + " ");
120 card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
124 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
126 if (bus.device == spec_and_info.first) {
127 card_combo->setCurrentIndex(current_index);
130 // Remove any excess items from earlier. (This is only for paranoia;
131 // they should be held, so it shouldn't matter.)
132 while (unsigned(card_combo->count()) > current_index + 1) {
133 card_combo->removeItem(current_index + 1);
135 connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
136 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
137 ui->table->setCellWidget(row, 1, card_combo);
139 setup_channel_choices_from_bus(row, bus);
142 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
144 // Left and right channel.
145 // TODO: If there's already a widget here, modify it instead of creating a new one,
146 // as we do with card choices.
147 for (unsigned channel = 0; channel < 2; ++channel) {
148 QComboBox *channel_combo = new QComboBox;
149 channel_combo->addItem(QString("(none)"));
150 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
151 bus.device.type == InputSourceType::ALSA_INPUT ||
152 bus.device.type == InputSourceType::FFMPEG_VIDEO_INPUT) {
153 auto device_it = devices.find(bus.device);
154 assert(device_it != devices.end());
155 unsigned num_device_channels = device_it->second.num_channels;
156 for (unsigned source = 0; source < num_device_channels; ++source) {
158 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
159 channel_combo->addItem(QString(buf));
161 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
163 assert(bus.device.type == InputSourceType::SILENCE);
164 channel_combo->setCurrentIndex(0);
166 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
167 bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
168 ui->table->setCellWidget(row, 2 + channel, channel_combo);
172 void InputMappingDialog::ok_clicked()
174 global_audio_mixer->set_state_changed_callback(saved_callback);
175 global_audio_mixer->set_input_mapping(mapping);
176 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
177 global_audio_mixer->set_bus_settings(bus_index, bus_settings[bus_index]);
178 global_audio_mixer->reset_peak(bus_index);
183 void InputMappingDialog::cancel_clicked()
185 global_audio_mixer->set_state_changed_callback(saved_callback);
186 global_audio_mixer->set_input_mapping(old_mapping);
190 void InputMappingDialog::cell_changed(int row, int column)
193 // Spurious; only really the name column should fire these.
196 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
199 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
201 uint64_t key = card_combo->itemData(index).toULongLong();
202 mapping.buses[row].device = key_to_DeviceSpec(key);
203 setup_channel_choices_from_bus(row, mapping.buses[row]);
206 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
208 mapping.buses[row].source_channel[channel] = index - 1;
211 void InputMappingDialog::add_clicked()
213 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
214 ui->table->setRangeSelected(all, false);
216 InputMapping::Bus new_bus;
217 new_bus.name = "New input";
218 new_bus.device.type = InputSourceType::SILENCE;
219 mapping.buses.push_back(new_bus);
220 bus_settings.push_back(AudioMixer::get_default_bus_settings());
221 ui->table->setRowCount(mapping.buses.size());
223 unsigned row = mapping.buses.size() - 1;
224 fill_row_from_bus(row, new_bus);
225 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.
226 update_button_state();
229 void InputMappingDialog::remove_clicked()
231 assert(ui->table->rowCount() != 0);
233 set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
234 for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
235 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
236 rows_to_delete.insert(row);
239 if (rows_to_delete.empty()) {
240 rows_to_delete.insert(ui->table->rowCount() - 1);
243 for (int row : rows_to_delete) {
244 ui->table->removeRow(row);
245 mapping.buses.erase(mapping.buses.begin() + row);
246 bus_settings.erase(bus_settings.begin() + row);
248 update_button_state();
251 void InputMappingDialog::updown_clicked(int direction)
253 assert(ui->table->selectedRanges().size() == 1);
254 const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
255 int a_row = range.bottomRow();
256 int b_row = range.bottomRow() + direction;
258 swap(mapping.buses[a_row], mapping.buses[b_row]);
259 swap(bus_settings[a_row], bus_settings[b_row]);
260 fill_row_from_bus(a_row, mapping.buses[a_row]);
261 fill_row_from_bus(b_row, mapping.buses[b_row]);
263 QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
264 QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
265 ui->table->setRangeSelected(a_sel, false);
266 ui->table->setRangeSelected(b_sel, true);
269 void InputMappingDialog::save_clicked()
272 // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
273 QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
275 QFileDialog::Option options(QFileDialog::Option(0));
277 QString filename = QFileDialog::getSaveFileName(this,
278 "Save input mapping", QString(), tr("Mapping files (*.mapping)"), /*selectedFilter=*/nullptr, options);
279 if (!filename.endsWith(".mapping")) {
280 filename += ".mapping";
282 if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
284 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
289 void InputMappingDialog::load_clicked()
292 // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
293 QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
295 QFileDialog::Option options(QFileDialog::Option(0));
297 QString filename = QFileDialog::getOpenFileName(this,
298 "Load input mapping", QString(), tr("Mapping files (*.mapping)"), /*selectedFilter=*/nullptr, options);
299 InputMapping new_mapping;
300 if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
302 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
307 mapping = new_mapping;
308 bus_settings.clear();
309 for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
310 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
312 devices = global_audio_mixer->get_devices(); // New dead cards may have been made.
313 fill_ui_from_mapping(mapping);
316 void InputMappingDialog::update_button_state()
318 ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
319 ui->remove_button->setDisabled(mapping.buses.size() == 0);
320 ui->up_button->setDisabled(
321 ui->table->selectedRanges().empty() ||
322 ui->table->selectedRanges()[0].bottomRow() == 0);
323 ui->down_button->setDisabled(
324 ui->table->selectedRanges().empty() ||
325 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);