]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Make the master peak display clickable, like all the other peak labels.
[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         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
20                 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
21         }
22
23         ui->setupUi(this);
24         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
25         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
26
27         fill_ui_from_mapping(mapping);
28         connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
29         connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
30         connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
31         connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
32         connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
33         connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
34         connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
35         connect(ui->save_button, &QPushButton::clicked, this, &InputMappingDialog::save_clicked);
36         connect(ui->load_button, &QPushButton::clicked, this, &InputMappingDialog::load_clicked);
37
38         update_button_state();
39         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
40
41         saved_callback = global_audio_mixer->get_state_changed_callback();
42         global_audio_mixer->set_state_changed_callback([this]{
43                 post_to_main_thread([this]{
44                         devices = global_audio_mixer->get_devices();
45                         for (unsigned row = 0; row < mapping.buses.size(); ++row) {
46                                 fill_row_from_bus(row, mapping.buses[row]);
47                         }
48                 });
49         });
50 }
51
52 InputMappingDialog::~InputMappingDialog()
53 {
54         global_audio_mixer->set_state_changed_callback(saved_callback);
55 }
56
57 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
58 {
59         ui->table->verticalHeader()->hide();
60         ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
61         ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
62         ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
63         ui->table->horizontalHeader()->setSectionsClickable(false);
64
65         ui->table->setRowCount(mapping.buses.size());
66         for (unsigned row = 0; row < mapping.buses.size(); ++row) {
67                 fill_row_from_bus(row, mapping.buses[row]);
68         }
69 }
70
71 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
72 {
73         QString name(QString::fromStdString(bus.name));
74         ui->table->setItem(row, 0, new QTableWidgetItem(name));
75
76         // Card choices. If there's already a combobox here, we try to modify
77         // the elements in-place, so that the UI doesn't go away under the user's feet
78         // if they are in the process of choosing an item.
79         QComboBox *card_combo = static_cast<QComboBox *>(ui->table->cellWidget(row, 1));
80         if (card_combo == nullptr) {
81                 card_combo = new QComboBox;
82         }
83         unsigned current_index = 0;
84         if (card_combo->count() == 0) {
85                 card_combo->addItem(QString("(none)   "));
86         }
87         for (const auto &spec_and_info : devices) {
88                 QString label(QString::fromStdString(spec_and_info.second.display_name));
89                 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
90                         ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
91                         if (state == ALSAPool::Device::State::EMPTY) {
92                                 continue;
93                         } else if (state == ALSAPool::Device::State::STARTING) {
94                                 label += " (busy)";
95                         } else if (state == ALSAPool::Device::State::DEAD) {
96                                 label += " (dead)";
97                         }
98                 }
99                 ++current_index;
100                 if (unsigned(card_combo->count()) > current_index) {
101                         card_combo->setItemText(current_index, label + "   ");
102                         card_combo->setItemData(current_index, qulonglong(DeviceSpec_to_key(spec_and_info.first)));
103                 } else {
104                         card_combo->addItem(
105                                 label + "   ",
106                                 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
107                 }
108                 if (bus.device == spec_and_info.first) {
109                         card_combo->setCurrentIndex(current_index);
110                 }
111         }
112         // Remove any excess items from earlier. (This is only for paranoia;
113         // they should be held, so it shouldn't matter.)
114         while (unsigned(card_combo->count()) > current_index + 1) {
115                 card_combo->removeItem(current_index + 1);
116         }
117         connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
118                 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
119         ui->table->setCellWidget(row, 1, card_combo);
120
121         setup_channel_choices_from_bus(row, bus);
122 }
123
124 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
125 {
126         // Left and right channel.
127         // TODO: If there's already a widget here, modify it instead of creating a new one,
128         // as we do with card choices.
129         for (unsigned channel = 0; channel < 2; ++channel) {
130                 QComboBox *channel_combo = new QComboBox;
131                 channel_combo->addItem(QString("(none)"));
132                 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
133                     bus.device.type == InputSourceType::ALSA_INPUT) {
134                         auto device_it = devices.find(bus.device);
135                         assert(device_it != devices.end());
136                         unsigned num_device_channels = device_it->second.num_channels;
137                         for (unsigned source = 0; source < num_device_channels; ++source) {
138                                 char buf[256];
139                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
140                                 channel_combo->addItem(QString(buf));
141                         }
142                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
143                 } else {
144                         channel_combo->setCurrentIndex(0);
145                 }
146                 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
147                         bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
148                 ui->table->setCellWidget(row, 2 + channel, channel_combo);
149         }
150 }
151
152 void InputMappingDialog::ok_clicked()
153 {
154         global_audio_mixer->set_state_changed_callback(saved_callback);
155         global_audio_mixer->set_input_mapping(mapping);
156         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
157                 global_audio_mixer->set_bus_settings(bus_index, bus_settings[bus_index]);
158                 global_audio_mixer->reset_peak(bus_index);
159         }
160         accept();
161 }
162
163 void InputMappingDialog::cancel_clicked()
164 {
165         global_audio_mixer->set_state_changed_callback(saved_callback);
166         global_audio_mixer->set_input_mapping(old_mapping);
167         reject();
168 }
169
170 void InputMappingDialog::cell_changed(int row, int column)
171 {
172         if (column != 0) {
173                 // Spurious; only really the name column should fire these.
174                 return;
175         }
176         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
177 }
178
179 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
180 {
181         uint64_t key = card_combo->itemData(index).toULongLong();
182         mapping.buses[row].device = key_to_DeviceSpec(key);
183         setup_channel_choices_from_bus(row, mapping.buses[row]);
184 }
185
186 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
187 {
188         mapping.buses[row].source_channel[channel] = index - 1;
189 }
190
191 void InputMappingDialog::add_clicked()
192 {
193         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
194         ui->table->setRangeSelected(all, false);
195
196         InputMapping::Bus new_bus;
197         new_bus.name = "New input";
198         new_bus.device.type = InputSourceType::SILENCE;
199         mapping.buses.push_back(new_bus);
200         bus_settings.push_back(AudioMixer::get_default_bus_settings());
201         ui->table->setRowCount(mapping.buses.size());
202
203         unsigned row = mapping.buses.size() - 1;
204         fill_row_from_bus(row, new_bus);
205         ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
206         update_button_state();
207 }
208
209 void InputMappingDialog::remove_clicked()
210 {
211         assert(ui->table->rowCount() != 0);
212
213         set<int, greater<int>> rows_to_delete;  // Need to remove in reverse order.
214         for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
215                 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
216                         rows_to_delete.insert(row);
217                 }
218         }
219         if (rows_to_delete.empty()) {
220                 rows_to_delete.insert(ui->table->rowCount() - 1);
221         }
222
223         for (int row : rows_to_delete) {
224                 ui->table->removeRow(row);
225                 mapping.buses.erase(mapping.buses.begin() + row);
226                 bus_settings.erase(bus_settings.begin() + row);
227         }
228         update_button_state();
229 }
230
231 void InputMappingDialog::updown_clicked(int direction)
232 {
233         assert(ui->table->selectedRanges().size() == 1);
234         const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
235         int a_row = range.bottomRow();
236         int b_row = range.bottomRow() + direction;
237
238         swap(mapping.buses[a_row], mapping.buses[b_row]);
239         swap(bus_settings[a_row], bus_settings[b_row]);
240         fill_row_from_bus(a_row, mapping.buses[a_row]);
241         fill_row_from_bus(b_row, mapping.buses[b_row]);
242
243         QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
244         QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
245         ui->table->setRangeSelected(a_sel, false);
246         ui->table->setRangeSelected(b_sel, true);
247 }
248
249 void InputMappingDialog::save_clicked()
250 {
251         QString filename = QFileDialog::getSaveFileName(this,
252                 "Save input mapping", QString(), tr("Mapping files (*.mapping)"));
253         if (!filename.endsWith(".mapping")) {
254                 filename += ".mapping";
255         }
256         if (!save_input_mapping_to_file(devices, mapping, filename.toStdString())) {
257                 QMessageBox box;
258                 box.setText("Could not save mapping to '" + filename + "'. Check that you have the right permissions and try again.");
259                 box.exec();
260         }
261 }
262
263 void InputMappingDialog::load_clicked()
264 {
265         QString filename = QFileDialog::getOpenFileName(this,
266                 "Load input mapping", QString(), tr("Mapping files (*.mapping)"));
267         InputMapping new_mapping;
268         if (!load_input_mapping_from_file(devices, filename.toStdString(), &new_mapping)) {
269                 QMessageBox box;
270                 box.setText("Could not load mapping from '" + filename + "'. Check that the file exists, has the right permissions and is valid.");
271                 box.exec();
272                 return;
273         }
274
275         mapping = new_mapping;
276         bus_settings.clear();
277         for (unsigned bus_index = 0; bus_index < mapping.buses.size(); ++bus_index) {
278                 bus_settings.push_back(global_audio_mixer->get_bus_settings(bus_index));
279         }
280         devices = global_audio_mixer->get_devices();  // New dead cards may have been made.
281         fill_ui_from_mapping(mapping);
282 }
283
284 void InputMappingDialog::update_button_state()
285 {
286         ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
287         ui->remove_button->setDisabled(mapping.buses.size() == 0);
288         ui->up_button->setDisabled(
289                 ui->table->selectedRanges().empty() ||
290                 ui->table->selectedRanges()[0].bottomRow() == 0);
291         ui->down_button->setDisabled(
292                 ui->table->selectedRanges().empty() ||
293                 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);
294 }