]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Make it possible to load/save input mappings.
[nageru] / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include "post_to_main_thread.h"
4 #include "ui_input_mapping.h"
5
6 #include <QComboBox>
7 #include <QFileDialog>
8 #include <QMessageBox>
9
10 using namespace std;
11 using namespace std::placeholders;
12
13 InputMappingDialog::InputMappingDialog()
14         : ui(new Ui::InputMappingDialog),
15           mapping(global_audio_mixer->get_input_mapping()),
16           old_mapping(mapping),
17           devices(global_audio_mixer->get_devices())
18 {
19         ui->setupUi(this);
20         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
21         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
22
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);
33
34         update_button_state();
35         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
36
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]);
43                         }
44                 });
45         });
46 }
47
48 InputMappingDialog::~InputMappingDialog()
49 {
50         global_audio_mixer->set_state_changed_callback(saved_callback);
51 }
52
53 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
54 {
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);
60
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]);
64         }
65 }
66
67 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
68 {
69         QString name(QString::fromStdString(bus.name));
70         ui->table->setItem(row, 0, new QTableWidgetItem(name));
71
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;
78         }
79         unsigned current_index = 0;
80         if (card_combo->count() == 0) {
81                 card_combo->addItem(QString("(none)   "));
82         }
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) {
88                                 continue;
89                         } else if (state == ALSAPool::Device::State::STARTING) {
90                                 label += " (busy)";
91                         } else if (state == ALSAPool::Device::State::DEAD) {
92                                 label += " (dead)";
93                         }
94                 }
95                 ++current_index;
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)));
99                 } else {
100                         card_combo->addItem(
101                                 label + "   ",
102                                 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
103                 }
104                 if (bus.device == spec_and_info.first) {
105                         card_combo->setCurrentIndex(current_index);
106                 }
107         }
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);
112         }
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);
116
117         setup_channel_choices_from_bus(row, bus);
118 }
119
120 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
121 {
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) {
134                                 char buf[256];
135                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
136                                 channel_combo->addItem(QString(buf));
137                         }
138                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
139                 } else {
140                         channel_combo->setCurrentIndex(0);
141                 }
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);
145         }
146 }
147
148 void InputMappingDialog::ok_clicked()
149 {
150         global_audio_mixer->set_state_changed_callback(saved_callback);
151         global_audio_mixer->set_input_mapping(mapping);
152         accept();
153 }
154
155 void InputMappingDialog::cancel_clicked()
156 {
157         global_audio_mixer->set_state_changed_callback(saved_callback);
158         global_audio_mixer->set_input_mapping(old_mapping);
159         reject();
160 }
161
162 void InputMappingDialog::cell_changed(int row, int column)
163 {
164         if (column != 0) {
165                 // Spurious; only really the name column should fire these.
166                 return;
167         }
168         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
169 }
170
171 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
172 {
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]);
176 }
177
178 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
179 {
180         mapping.buses[row].source_channel[channel] = index - 1;
181 }
182
183 void InputMappingDialog::add_clicked()
184 {
185         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
186         ui->table->setRangeSelected(all, false);
187
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());
193
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();
198 }
199
200 void InputMappingDialog::remove_clicked()
201 {
202         assert(ui->table->rowCount() != 0);
203
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);
208                 }
209         }
210         if (rows_to_delete.empty()) {
211                 rows_to_delete.insert(ui->table->rowCount() - 1);
212         }
213
214         for (int row : rows_to_delete) {
215                 ui->table->removeRow(row);
216                 mapping.buses.erase(mapping.buses.begin() + row);
217         }
218         update_button_state();
219 }
220
221 void InputMappingDialog::updown_clicked(int direction)
222 {
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;
227
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]);
231
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);
236 }
237
238 void InputMappingDialog::save_clicked()
239 {
240         QString filename = QFileDialog::getSaveFileName(this,
241                 "Save input mapping", QString(), tr("Mapping files (*.mapping)"));
242         if (!filename.endsWith(".mapping")) {
243                 filename += ".mapping";
244         }
245         if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
246                 QMessageBox box;
247                 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
248                 box.exec();
249         }
250 }
251
252 void InputMappingDialog::load_clicked()
253 {
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)) {
258                 QMessageBox box;
259                 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
260                 box.exec();
261                 return;
262         }
263
264         mapping = new_mapping;
265         devices = global_audio_mixer->get_devices();  // New dead cards may have been made.
266         fill_ui_from_mapping(mapping);
267 }
268
269 void InputMappingDialog::update_button_state()
270 {
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);
279 }