]> git.sesse.net Git - nageru/blob - nageru/input_mapping_dialog.cpp
Support turning off interpolation.
[nageru] / nageru / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include <assert.h>
4 #include <stdbool.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <QAbstractItemView>
8 #include <QComboBox>
9 #include <QDialogButtonBox>
10 #include <QFileDialog>
11 #include <QHeaderView>
12 #include <QList>
13 #include <QMessageBox>
14 #include <QPushButton>
15 #include <QTableWidget>
16 #include <QVariant>
17 #include <functional>
18 #include <memory>
19 #include <set>
20 #include <string>
21 #include <utility>
22
23 #include "alsa_pool.h"
24 #include "defs.h"
25 #include "shared/post_to_main_thread.h"
26 #include "ui_input_mapping.h"
27
28 using namespace std;
29 using namespace std::placeholders;
30
31 InputMappingDialog::InputMappingDialog()
32         : ui(new Ui::InputMappingDialog),
33           mapping(global_audio_mixer->get_input_mapping()),
34           old_mapping(mapping),
35           devices(global_audio_mixer->get_devices())
36 {
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));
39         }
40
41         ui->setupUi(this);
42         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
43         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
44
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);
55
56         update_button_state();
57         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
58
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]);
65                         }
66                 });
67         });
68 }
69
70 InputMappingDialog::~InputMappingDialog()
71 {
72         global_audio_mixer->set_state_changed_callback(saved_callback);
73 }
74
75 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
76 {
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);
82
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]);
86         }
87 }
88
89 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
90 {
91         QString name(QString::fromStdString(bus.name));
92         ui->table->setItem(row, 0, new QTableWidgetItem(name));
93
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;
100         }
101         unsigned current_index = 0;
102         if (card_combo->count() == 0) {
103                 card_combo->addItem(QString("(none)   "));
104         }
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) {
110                                 continue;
111                         } else if (state == ALSAPool::Device::State::STARTING) {
112                                 label += " (busy)";
113                         } else if (state == ALSAPool::Device::State::DEAD) {
114                                 label += " (dead)";
115                         }
116                 }
117                 ++current_index;
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)));
121                 } else {
122                         card_combo->addItem(
123                                 label + "   ",
124                                 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
125                 }
126                 if (bus.device == spec_and_info.first) {
127                         card_combo->setCurrentIndex(current_index);
128                 }
129         }
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);
134         }
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);
138
139         setup_channel_choices_from_bus(row, bus);
140 }
141
142 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
143 {
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) {
157                                 char buf[256];
158                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
159                                 channel_combo->addItem(QString(buf));
160                         }
161                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
162                 } else {
163                         assert(bus.device.type == InputSourceType::SILENCE);
164                         channel_combo->setCurrentIndex(0);
165                 }
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);
169         }
170 }
171
172 void InputMappingDialog::ok_clicked()
173 {
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);
179         }
180         accept();
181 }
182
183 void InputMappingDialog::cancel_clicked()
184 {
185         global_audio_mixer->set_state_changed_callback(saved_callback);
186         global_audio_mixer->set_input_mapping(old_mapping);
187         reject();
188 }
189
190 void InputMappingDialog::cell_changed(int row, int column)
191 {
192         if (column != 0) {
193                 // Spurious; only really the name column should fire these.
194                 return;
195         }
196         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
197 }
198
199 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
200 {
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]);
204 }
205
206 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
207 {
208         mapping.buses[row].source_channel[channel] = index - 1;
209 }
210
211 void InputMappingDialog::add_clicked()
212 {
213         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
214         ui->table->setRangeSelected(all, false);
215
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());
222
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();
227 }
228
229 void InputMappingDialog::remove_clicked()
230 {
231         assert(ui->table->rowCount() != 0);
232
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);
237                 }
238         }
239         if (rows_to_delete.empty()) {
240                 rows_to_delete.insert(ui->table->rowCount() - 1);
241         }
242
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);
247         }
248         update_button_state();
249 }
250
251 void InputMappingDialog::updown_clicked(int direction)
252 {
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;
257
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]);
262
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);
267 }
268
269 void InputMappingDialog::save_clicked()
270 {
271 #if HAVE_CEF
272         // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
273         QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
274 #else
275         QFileDialog::Option options(QFileDialog::Option(0));
276 #endif
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";
281         }
282         if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
283                 QMessageBox box;
284                 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
285                 box.exec();
286         }
287 }
288
289 void InputMappingDialog::load_clicked()
290 {
291 #if HAVE_CEF
292         // The native file dialog uses GTK+, which interferes with CEF's use of the GLib main loop.
293         QFileDialog::Option options(QFileDialog::DontUseNativeDialog);
294 #else
295         QFileDialog::Option options(QFileDialog::Option(0));
296 #endif
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)) {
301                 QMessageBox box;
302                 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
303                 box.exec();
304                 return;
305         }
306
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));
311         }
312         devices = global_audio_mixer->get_devices();  // New dead cards may have been made.
313         fill_ui_from_mapping(mapping);
314 }
315
316 void InputMappingDialog::update_button_state()
317 {
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);
326 }