1 #include "input_mapping_dialog.h"
3 #include "post_to_main_thread.h"
4 #include "ui_input_mapping.h"
9 using namespace std::placeholders;
11 InputMappingDialog::InputMappingDialog()
12 : ui(new Ui::InputMappingDialog),
13 mapping(global_audio_mixer->get_input_mapping()),
15 devices(global_audio_mixer->get_devices())
18 ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
19 ui->table->setSelectionMode(QAbstractItemView::SingleSelection); // Makes implementing moving easier for now.
21 fill_ui_from_mapping(mapping);
22 connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
23 connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
24 connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
25 connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
26 connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
27 connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
28 connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
30 update_button_state();
31 connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
33 saved_callback = global_audio_mixer->get_state_changed_callback();
34 global_audio_mixer->set_state_changed_callback([this]{
35 post_to_main_thread([this]{
36 devices = global_audio_mixer->get_devices();
37 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
38 fill_row_from_bus(row, mapping.buses[row]);
44 InputMappingDialog::~InputMappingDialog()
46 global_audio_mixer->set_state_changed_callback(saved_callback);
49 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
51 ui->table->verticalHeader()->hide();
52 ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
53 ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
54 ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
55 ui->table->horizontalHeader()->setSectionsClickable(false);
57 ui->table->setRowCount(mapping.buses.size());
58 for (unsigned row = 0; row < mapping.buses.size(); ++row) {
59 fill_row_from_bus(row, mapping.buses[row]);
63 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
65 QString name(QString::fromStdString(bus.name));
66 ui->table->setItem(row, 0, new QTableWidgetItem(name));
68 // Card choices. If there's already a combobox here, we try to modify
69 // the elements in-place, so that the UI doesn't go away under the user's feet
70 // if they are in the process of choosing an item.
71 QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
72 if (card_combo == nullptr) {
73 card_combo = new QComboBox;
75 unsigned current_index = 0;
76 if (card_combo->count() == 0) {
77 card_combo->addItem(QString("(none) "));
79 for (const auto &spec_and_info : devices) {
80 QString label(QString::fromStdString(spec_and_info.second.name));
81 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
82 ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
83 if (state == ALSAPool::Device::State::EMPTY) {
85 } else if (state == ALSAPool::Device::State::STARTING) {
87 } else if (state == ALSAPool::Device::State::DEAD) {
92 if (unsigned(card_combo->count()) > current_index) {
93 card_combo->setItemText(current_index, label + " ");
94 card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
98 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
100 if (bus.device == spec_and_info.first) {
101 card_combo->setCurrentIndex(current_index);
104 // Remove any excess items from earlier. (This is only for paranoia;
105 // they should be held, so it shouldn't matter.)
106 while (unsigned(card_combo->count()) > current_index + 1) {
107 card_combo->removeItem(current_index + 1);
109 connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
110 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
111 ui->table->setCellWidget(row, 1, card_combo);
113 setup_channel_choices_from_bus(row, bus);
116 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
118 // Left and right channel.
119 // TODO: If there's already a widget here, modify it instead of creating a new one,
120 // as we do with card choices.
121 for (unsigned channel = 0; channel < 2; ++channel) {
122 QComboBox *channel_combo = new QComboBox;
123 channel_combo->addItem(QString("(none)"));
124 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
125 bus.device.type == InputSourceType::ALSA_INPUT) {
126 auto device_it = devices.find(bus.device);
127 assert(device_it != devices.end());
128 unsigned num_device_channels = device_it->second.num_channels;
129 for (unsigned source = 0; source < num_device_channels; ++source) {
131 snprintf(buf, sizeof(buf), "Channel %u ", source + 1);
132 channel_combo->addItem(QString(buf));
134 channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
136 channel_combo->setCurrentIndex(0);
138 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
139 bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
140 ui->table->setCellWidget(row, 2 + channel, channel_combo);
144 void InputMappingDialog::ok_clicked()
146 global_audio_mixer->set_state_changed_callback(saved_callback);
147 global_audio_mixer->set_input_mapping(mapping);
151 void InputMappingDialog::cancel_clicked()
153 global_audio_mixer->set_state_changed_callback(saved_callback);
154 global_audio_mixer->set_input_mapping(old_mapping);
158 void InputMappingDialog::cell_changed(int row, int column)
161 // Spurious; only really the name column should fire these.
164 mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
167 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
169 uint64_t key = card_combo->itemData(index).toULongLong();
170 mapping.buses[row].device = key_to_DeviceSpec(key);
171 setup_channel_choices_from_bus(row, mapping.buses[row]);
174 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
176 mapping.buses[row].source_channel[channel] = index - 1;
179 void InputMappingDialog::add_clicked()
181 QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
182 ui->table->setRangeSelected(all, false);
184 InputMapping::Bus new_bus;
185 new_bus.name = "New input";
186 new_bus.device.type = InputSourceType::SILENCE;
187 mapping.buses.push_back(new_bus);
188 ui->table->setRowCount(mapping.buses.size());
190 unsigned row = mapping.buses.size() - 1;
191 fill_row_from_bus(row, new_bus);
192 ui->table->editItem(ui->table->item(row, 0)); // Start editing the name.
193 update_button_state();
196 void InputMappingDialog::remove_clicked()
198 assert(ui->table->rowCount() != 0);
200 set<int, greater<int>> rows_to_delete; // Need to remove in reverse order.
201 for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
202 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
203 rows_to_delete.insert(row);
206 if (rows_to_delete.empty()) {
207 rows_to_delete.insert(ui->table->rowCount() - 1);
210 for (int row : rows_to_delete) {
211 ui->table->removeRow(row);
212 mapping.buses.erase(mapping.buses.begin() + row);
214 update_button_state();
217 void InputMappingDialog::updown_clicked(int direction)
219 assert(ui->table->selectedRanges().size() == 1);
220 const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
221 int a_row = range.bottomRow();
222 int b_row = range.bottomRow() + direction;
224 swap(mapping.buses[a_row], mapping.buses[b_row]);
225 fill_row_from_bus(a_row, mapping.buses[a_row]);
226 fill_row_from_bus(b_row, mapping.buses[b_row]);
228 QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
229 QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
230 ui->table->setRangeSelected(a_sel, false);
231 ui->table->setRangeSelected(b_sel, true);
234 void InputMappingDialog::update_button_state()
236 ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
237 ui->remove_button->setDisabled(mapping.buses.size() == 0);
238 ui->up_button->setDisabled(
239 ui->table->selectedRanges().empty() ||
240 ui->table->selectedRanges()[0].bottomRow() == 0);
241 ui->down_button->setDisabled(
242 ui->table->selectedRanges().empty() ||
243 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);