]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Rename “name” to “display name”.
[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
8 using namespace std;
9 using namespace std::placeholders;
10
11 InputMappingDialog::InputMappingDialog()
12         : ui(new Ui::InputMappingDialog),
13           mapping(global_audio_mixer->get_input_mapping()),
14           old_mapping(mapping),
15           devices(global_audio_mixer->get_devices())
16 {
17         ui->setupUi(this);
18         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
19         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
20
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));
29
30         update_button_state();
31         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
32
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]);
39                         }
40                 });
41         });
42 }
43
44 InputMappingDialog::~InputMappingDialog()
45 {
46         global_audio_mixer->set_state_changed_callback(saved_callback);
47 }
48
49 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
50 {
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);
56
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]);
60         }
61 }
62
63 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
64 {
65         QString name(QString::fromStdString(bus.name));
66         ui->table->setItem(row, 0, new QTableWidgetItem(name));
67
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;
74         }
75         unsigned current_index = 0;
76         if (card_combo->count() == 0) {
77                 card_combo->addItem(QString("(none)   "));
78         }
79         for (const auto &spec_and_info : devices) {
80                 QString label(QString::fromStdString(spec_and_info.second.display_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) {
84                                 continue;
85                         } else if (state == ALSAPool::Device::State::STARTING) {
86                                 label += " (busy)";
87                         } else if (state == ALSAPool::Device::State::DEAD) {
88                                 label += " (dead)";
89                         }
90                 }
91                 ++current_index;
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)));
95                 } else {
96                         card_combo->addItem(
97                                 label + "   ",
98                                 qulonglong(DeviceSpec_to_key(spec_and_info.first)));
99                 }
100                 if (bus.device == spec_and_info.first) {
101                         card_combo->setCurrentIndex(current_index);
102                 }
103         }
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);
108         }
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);
112
113         setup_channel_choices_from_bus(row, bus);
114 }
115
116 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
117 {
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) {
130                                 char buf[256];
131                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
132                                 channel_combo->addItem(QString(buf));
133                         }
134                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
135                 } else {
136                         channel_combo->setCurrentIndex(0);
137                 }
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);
141         }
142 }
143
144 void InputMappingDialog::ok_clicked()
145 {
146         global_audio_mixer->set_state_changed_callback(saved_callback);
147         global_audio_mixer->set_input_mapping(mapping);
148         accept();
149 }
150
151 void InputMappingDialog::cancel_clicked()
152 {
153         global_audio_mixer->set_state_changed_callback(saved_callback);
154         global_audio_mixer->set_input_mapping(old_mapping);
155         reject();
156 }
157
158 void InputMappingDialog::cell_changed(int row, int column)
159 {
160         if (column != 0) {
161                 // Spurious; only really the name column should fire these.
162                 return;
163         }
164         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
165 }
166
167 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
168 {
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]);
172 }
173
174 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
175 {
176         mapping.buses[row].source_channel[channel] = index - 1;
177 }
178
179 void InputMappingDialog::add_clicked()
180 {
181         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
182         ui->table->setRangeSelected(all, false);
183
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());
189
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();
194 }
195
196 void InputMappingDialog::remove_clicked()
197 {
198         assert(ui->table->rowCount() != 0);
199
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);
204                 }
205         }
206         if (rows_to_delete.empty()) {
207                 rows_to_delete.insert(ui->table->rowCount() - 1);
208         }
209
210         for (int row : rows_to_delete) {
211                 ui->table->removeRow(row);
212                 mapping.buses.erase(mapping.buses.begin() + row);
213         }
214         update_button_state();
215 }
216
217 void InputMappingDialog::updown_clicked(int direction)
218 {
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;
223
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]);
227
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);
232 }
233
234 void InputMappingDialog::update_button_state()
235 {
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);
244 }