]> git.sesse.net Git - nageru/blob - input_mapping_dialog.cpp
Support limited ALSA hotplug.
[nageru] / input_mapping_dialog.cpp
1 #include "input_mapping_dialog.h"
2
3 #include "ui_input_mapping.h"
4
5 #include <QComboBox>
6
7 using namespace std;
8 using namespace std::placeholders;
9
10 InputMappingDialog::InputMappingDialog()
11         : ui(new Ui::InputMappingDialog),
12           mapping(global_audio_mixer->get_input_mapping()),
13           old_mapping(mapping),
14           devices(global_audio_mixer->get_devices())
15 {
16         ui->setupUi(this);
17         ui->table->setSelectionBehavior(QAbstractItemView::SelectRows);
18         ui->table->setSelectionMode(QAbstractItemView::SingleSelection);  // Makes implementing moving easier for now.
19
20         fill_ui_from_mapping(mapping);
21         connect(ui->table, &QTableWidget::cellChanged, this, &InputMappingDialog::cell_changed);
22         connect(ui->ok_cancel_buttons, &QDialogButtonBox::accepted, this, &InputMappingDialog::ok_clicked);
23         connect(ui->ok_cancel_buttons, &QDialogButtonBox::rejected, this, &InputMappingDialog::cancel_clicked);
24         connect(ui->add_button, &QPushButton::clicked, this, &InputMappingDialog::add_clicked);
25         connect(ui->remove_button, &QPushButton::clicked, this, &InputMappingDialog::remove_clicked);
26         connect(ui->up_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, -1));
27         connect(ui->down_button, &QPushButton::clicked, bind(&InputMappingDialog::updown_clicked, this, 1));
28
29         update_button_state();
30         connect(ui->table, &QTableWidget::itemSelectionChanged, this, &InputMappingDialog::update_button_state);
31 }
32
33 void InputMappingDialog::fill_ui_from_mapping(const InputMapping &mapping)
34 {
35         ui->table->verticalHeader()->hide();
36         ui->table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
37         ui->table->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
38         ui->table->horizontalHeader()->setSectionResizeMode(3, QHeaderView::ResizeToContents);
39         ui->table->horizontalHeader()->setSectionsClickable(false);
40
41         ui->table->setRowCount(mapping.buses.size());
42         for (unsigned row = 0; row < mapping.buses.size(); ++row) {
43                 fill_row_from_bus(row, mapping.buses[row]);
44         }
45 }
46
47 void InputMappingDialog::fill_row_from_bus(unsigned row, const InputMapping::Bus &bus)
48 {
49         QString name(QString::fromStdString(bus.name));
50         ui->table->setItem(row, 0, new QTableWidgetItem(name));
51
52         // Card choices.
53         QComboBox *card_combo = new QComboBox;
54         unsigned current_index = 0;
55         card_combo->addItem(QString("(none)   "));
56         for (const auto &spec_and_info : devices) {
57                 QString label(QString::fromStdString(spec_and_info.second.name));
58                 if (spec_and_info.first.type == InputSourceType::ALSA_INPUT) {
59                         ALSAPool::Device::State state = global_audio_mixer->get_alsa_card_state(spec_and_info.first.index);
60                         if (state == ALSAPool::Device::State::EMPTY) {
61                                 continue;
62                         } else if (state == ALSAPool::Device::State::STARTING) {
63                                 label += " (busy)";
64                         } else if (state == ALSAPool::Device::State::DEAD) {
65                                 label += " (dead)";
66                         }
67                 }
68                 ++current_index;
69                 card_combo->addItem(
70                         label + "   ",
71                         qulonglong(DeviceSpec_to_key(spec_and_info.first)));
72                 if (bus.device == spec_and_info.first) {
73                         card_combo->setCurrentIndex(current_index);
74                 }
75         }
76         connect(card_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
77                 bind(&InputMappingDialog::card_selected, this, card_combo, row, _1));
78         ui->table->setCellWidget(row, 1, card_combo);
79
80         setup_channel_choices_from_bus(row, bus);
81 }
82
83 void InputMappingDialog::setup_channel_choices_from_bus(unsigned row, const InputMapping::Bus &bus)
84 {
85         // Left and right channel.
86         for (unsigned channel = 0; channel < 2; ++channel) {
87                 QComboBox *channel_combo = new QComboBox;
88                 channel_combo->addItem(QString("(none)"));
89                 if (bus.device.type == InputSourceType::CAPTURE_CARD ||
90                     bus.device.type == InputSourceType::ALSA_INPUT) {
91                         auto device_it = devices.find(bus.device);
92                         assert(device_it != devices.end());
93                         unsigned num_device_channels = device_it->second.num_channels;
94                         for (unsigned source = 0; source < num_device_channels; ++source) {
95                                 char buf[256];
96                                 snprintf(buf, sizeof(buf), "Channel %u   ", source + 1);
97                                 channel_combo->addItem(QString(buf));
98                         }
99                         channel_combo->setCurrentIndex(bus.source_channel[channel] + 1);
100                 } else {
101                         channel_combo->setCurrentIndex(0);
102                 }
103                 connect(channel_combo, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
104                         bind(&InputMappingDialog::channel_selected, this, row, channel, _1));
105                 ui->table->setCellWidget(row, 2 + channel, channel_combo);
106         }
107 }
108
109 void InputMappingDialog::ok_clicked()
110 {
111         global_audio_mixer->set_input_mapping(mapping);
112         accept();
113 }
114
115 void InputMappingDialog::cancel_clicked()
116 {
117         global_audio_mixer->set_input_mapping(old_mapping);
118         reject();
119 }
120
121 void InputMappingDialog::cell_changed(int row, int column)
122 {
123         if (column != 0) {
124                 // Spurious; only really the name column should fire these.
125                 return;
126         }
127         mapping.buses[row].name = ui->table->item(row, column)->text().toStdString();
128 }
129
130 void InputMappingDialog::card_selected(QComboBox *card_combo, unsigned row, int index)
131 {
132         uint64_t key = card_combo->itemData(index).toULongLong();
133         mapping.buses[row].device = key_to_DeviceSpec(key);
134         setup_channel_choices_from_bus(row, mapping.buses[row]);
135 }
136
137 void InputMappingDialog::channel_selected(unsigned row, unsigned channel, int index)
138 {
139         mapping.buses[row].source_channel[channel] = index - 1;
140 }
141
142 void InputMappingDialog::add_clicked()
143 {
144         QTableWidgetSelectionRange all(0, 0, ui->table->rowCount() - 1, ui->table->columnCount() - 1);
145         ui->table->setRangeSelected(all, false);
146
147         InputMapping::Bus new_bus;
148         new_bus.name = "New input";
149         new_bus.device.type = InputSourceType::SILENCE;
150         mapping.buses.push_back(new_bus);
151         ui->table->setRowCount(mapping.buses.size());
152
153         unsigned row = mapping.buses.size() - 1;
154         fill_row_from_bus(row, new_bus);
155         ui->table->editItem(ui->table->item(row, 0));  // Start editing the name.
156         update_button_state();
157 }
158
159 void InputMappingDialog::remove_clicked()
160 {
161         assert(ui->table->rowCount() != 0);
162
163         set<int, greater<int>> rows_to_delete;  // Need to remove in reverse order.
164         for (const QTableWidgetSelectionRange &range : ui->table->selectedRanges()) {
165                 for (int row = range.topRow(); row <= range.bottomRow(); ++row) {
166                         rows_to_delete.insert(row);
167                 }
168         }
169         if (rows_to_delete.empty()) {
170                 rows_to_delete.insert(ui->table->rowCount() - 1);
171         }
172
173         for (int row : rows_to_delete) {
174                 ui->table->removeRow(row);
175                 mapping.buses.erase(mapping.buses.begin() + row);
176         }
177         update_button_state();
178 }
179
180 void InputMappingDialog::updown_clicked(int direction)
181 {
182         assert(ui->table->selectedRanges().size() == 1);
183         const QTableWidgetSelectionRange &range = ui->table->selectedRanges()[0];
184         int a_row = range.bottomRow();
185         int b_row = range.bottomRow() + direction;
186
187         swap(mapping.buses[a_row], mapping.buses[b_row]);
188         fill_row_from_bus(a_row, mapping.buses[a_row]);
189         fill_row_from_bus(b_row, mapping.buses[b_row]);
190
191         QTableWidgetSelectionRange a_sel(a_row, 0, a_row, ui->table->columnCount() - 1);
192         QTableWidgetSelectionRange b_sel(b_row, 0, b_row, ui->table->columnCount() - 1);
193         ui->table->setRangeSelected(a_sel, false);
194         ui->table->setRangeSelected(b_sel, true);
195 }
196
197 void InputMappingDialog::update_button_state()
198 {
199         ui->add_button->setDisabled(mapping.buses.size() >= MAX_BUSES);
200         ui->remove_button->setDisabled(mapping.buses.size() == 0);
201         ui->up_button->setDisabled(
202                 ui->table->selectedRanges().empty() ||
203                 ui->table->selectedRanges()[0].bottomRow() == 0);
204         ui->down_button->setDisabled(
205                 ui->table->selectedRanges().empty() ||
206                 ui->table->selectedRanges()[0].bottomRow() == ui->table->rowCount() - 1);
207 }