-
Notifications
You must be signed in to change notification settings - Fork 14
/
SettingsDlg.cpp
428 lines (371 loc) · 12 KB
/
SettingsDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
* Copyright (c) 2019-2021 by Thomas A. Early N7TAE
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <alsa/asoundlib.h>
#include "AudioManager.h"
#include "SettingsDlg.h"
#include "MainWindow.h"
#define _(STRING) gettext(STRING)
static const char *notfoundstr = _(" not found");
CSettingsDlg::CSettingsDlg() : pMainWindow(nullptr), pDlg(nullptr)
{
}
CSettingsDlg::~CSettingsDlg()
{
if (pDlg)
delete pDlg;
}
void CSettingsDlg::Show()
{
CConfigure cfg;
pMainWindow->cfg.CopyTo(data); // get the saved config data (MainWindow has alread read it)
SetWidgetStates(data);
pDlg->show();
pTabs->value(pStationGroup);
AudioRescanButton(); // re-read the audio PCM devices
}
void CSettingsDlg::UpdateButtonCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->UpdateButton();
}
void CSettingsDlg::UpdateButton()
{
pDlg->hide();
CFGDATA newstate; // the user clicked okay, time to look at what's changed
SaveWidgetStates(newstate); // newstate is now the current contents of the Settings Dialog
#ifndef NO_DHT
if (newstate.sBootstrap.compare(pMainWindow->cfg.GetData()->sBootstrap))
{
fl_message("Please restart to use new bootstrap");
}
#endif
pMainWindow->cfg.CopyFrom(newstate); // and it is now in the global cfg object
pMainWindow->cfg.WriteData(); // and it's saved in the config dir
// reconfigure current environment if anything changed
pMainWindow->cfg.CopyTo(data);
pMainWindow->NewSettings(&newstate);
}
void CSettingsDlg::SaveWidgetStates(CFGDATA &d)
{
// M17
d.sM17SourceCallsign.assign(pSourceCallsignInput->value());
d.bVoiceOnlyEnable = 1u == pVoiceOnlyRadioButton->value();
// station
d.cModule = data.cModule;
// Internet
if (pIPv4RadioButton->value())
d.eNetType = EInternetType::ipv4only;
else if (pIPv6RadioButton->value())
d.eNetType = EInternetType::ipv6only;
else
d.eNetType = EInternetType::dualstack;
// audio
const std::string in(pAudioInputChoice->text());
auto itin = AudioInMap.find(in);
if (AudioInMap.end() != itin)
{
d.sAudioIn.assign(itin->second.first);
}
const std::string out(pAudioOutputChoice->text());
auto itout = AudioOutMap.find(out);
if (AudioOutMap.end() != itout)
{
d.sAudioOut.assign(itout->second.first);
}
#ifndef NO_DHT
d.sBootstrap.assign(pBootstrapInput->value());
#endif
}
void CSettingsDlg::SetWidgetStates(const CFGDATA &d)
{
// M17
if (d.bVoiceOnlyEnable)
pVoiceOnlyRadioButton->setonly();
else
pVoiceDataRadioButton->setonly();
pSourceCallsignInput->value(d.sM17SourceCallsign.c_str());
SourceCallsignInput();
pModuleChoice->value(d.cModule - 'A');
#ifndef NO_DHT
pBootstrapInput->value(d.sBootstrap.c_str());
#endif
// internet
switch (d.eNetType) {
case EInternetType::ipv6only:
pIPv6RadioButton->setonly();
break;
case EInternetType::dualstack:
pDualStackRadioButton->setonly();
break;
default:
pIPv4RadioButton->setonly();
break;
}
}
bool CSettingsDlg::Init(CMainWindow *pMain)
{
pMainWindow = pMain;
pDlg = new Fl_Double_Window(450, 350, _("Settings"));
pTabs = new Fl_Tabs(10, 10, 430, 240);
pTabs->labelsize(16);
//////////////////////////////////////////////////////////////////////////
pStationGroup = new Fl_Group(20, 30, 410, 210, _("Station"));
pStationGroup->labelsize(16);
pSourceCallsignInput = new Fl_Input(218, 45, 127, 30, _("My Callsign:"));
pSourceCallsignInput->tooltip(_("Input your callsign, up to 8 characters"));
pSourceCallsignInput->color(FL_RED);
pSourceCallsignInput->labelsize(16);
pSourceCallsignInput->textsize(16);
pSourceCallsignInput->when(FL_WHEN_CHANGED);
pSourceCallsignInput->callback(&CSettingsDlg::SourceCallsignInputCB, this);
pModuleChoice = new Fl_Choice(218, 85, 50, 30, _("Using Module:"));
pModuleChoice->down_box(FL_BORDER_BOX);
pModuleChoice->tooltip(_("Assign the transceiver module"));
pModuleChoice->labelsize(16);
pModuleChoice->textsize(16);
pModuleChoice->callback(&CSettingsDlg::ModuleChoiceCB, this);
for (char c='A'; c<='Z'; c++)
{
const std::string l(1, c);
pModuleChoice->add(l.c_str());
}
pCodecGroup = new Fl_Group(110, 138, 245, 105, "Codec:");
pCodecGroup->box(FL_THIN_UP_BOX);
pCodecGroup->labelsize(16);
pVoiceOnlyRadioButton = new Fl_Radio_Round_Button(160, 155, 150, 30, _("Voice-only"));
pVoiceOnlyRadioButton->tooltip(_("This is the higher quality, 3200 bits/s codec"));
pVoiceOnlyRadioButton->labelsize(16);
pVoiceDataRadioButton = new Fl_Radio_Round_Button(160, 195, 150, 30, _("Voice+Data"));
pVoiceDataRadioButton->tooltip(_("This is the 1600 bits/s codec"));
pVoiceDataRadioButton->labelsize(16);
pCodecGroup->end();
pStationGroup->end();
pTabs->add(pStationGroup);
//////////////////////////////////////////////////////////////////////////
pInternetGroup = new Fl_Group(20, 30, 410, 210, _("Network"));
pInternetGroup->labelsize(16);
pIPv4RadioButton = new Fl_Radio_Round_Button(145, 60, 200, 30, _("IPv4 Only"));
pIPv4RadioButton->labelsize(16);
pIPv6RadioButton = new Fl_Radio_Round_Button(145, 110, 200, 30, _("IPv6 Only"));
pIPv6RadioButton->labelsize(16);
pDualStackRadioButton = new Fl_Radio_Round_Button(145, 160, 200, 30, _("IPv4 && IPv6"));
pDualStackRadioButton->labelsize(16);
pInternetGroup->end();
pTabs->add(pInternetGroup);
///////////////////////////////////////////////////////////////////////////
#ifndef NO_DHT
pDHTGroup = new Fl_Group(20, 30, 410, 210, _("DHT"));
pBootstrapInput = new Fl_Input(170, 120, 227, 30, _("DHT Bootstrap:"));
pBootstrapInput->tooltip(_("An existing node on the DHT Network"));
pDHTGroup->end();
pTabs->add(pDHTGroup);
#endif
//////////////////////////////////////////////////////////////////////////
pAudioGroup = new Fl_Group(20, 30, 410, 210, _("Audio"));
pAudioGroup->tooltip(_("Select the audio Input and Output devices"));
pAudioGroup->labelsize(16);
pAudioInputChoice = new Fl_Choice(135, 50, 260, 24, _("Input:"));
pAudioInputChoice->tooltip(_("Select your audio input device, usually \"default\""));
pAudioInputChoice->down_box(FL_BORDER_BOX);
pAudioInputChoice->labelsize(16);
pAudioInputChoice->textsize(16);
pAudioInputChoice->callback(&CSettingsDlg::AudioInputChoiceCB, this);
pAudioInputDescBox = new Fl_Box(30, 80, 390, 25, "input description");
pAudioInputDescBox->labelsize(12);
pAudioOutputChoice = new Fl_Choice(135, 120, 260, 24, _("Output:"));
pAudioOutputChoice->tooltip(_("Select the audio output device, usually \"default\""));
pAudioOutputChoice->down_box(FL_BORDER_BOX);
pAudioOutputChoice->labelsize(16);
pAudioOutputChoice->textsize(16);
pAudioOutputChoice->callback(&CSettingsDlg::AudioOutputChoiceCB, this);
pAudioOutputDescBox = new Fl_Box(30, 150, 390, 25, "output description");
pAudioOutputDescBox->labelsize(12);
pAudioRescanButton = new Fl_Button(192, 186, 90, 40, _("Rescan"));
pAudioRescanButton->tooltip(_("Rescan for new audio devices"));
pAudioRescanButton->labelsize(16);
pAudioGroup->end();
pTabs->add(pAudioGroup);
pTabs->end();
pOkayButton = new Fl_Return_Button(310, 280, 120, 44, _("Update"));
pOkayButton->labelsize(16);
pOkayButton->callback(&CSettingsDlg::UpdateButtonCB, this);
pDlg->end();
pDlg->set_modal();
return false;
}
void CSettingsDlg::ModuleChoiceCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->ModuleChoice();
}
void CSettingsDlg::ModuleChoice()
{
const std::string selected(pModuleChoice->text());
data.cModule = selected.at(0);
}
void CSettingsDlg::SourceCallsignInputCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->SourceCallsignInput();
}
void CSettingsDlg::SourceCallsignInput()
{
auto pos = pSourceCallsignInput->position();
std::string s(pSourceCallsignInput->value());
if (pMainWindow->ToUpper(s))
{
pSourceCallsignInput->value(s.c_str());
pSourceCallsignInput->position(pos);
}
bM17Source = std::regex_match(s.c_str(), pMainWindow->M17CallRegEx);
if (bM17Source)
{
pSourceCallsignInput->color(FL_GREEN);
pOkayButton->activate();
}
else
{
pSourceCallsignInput->color(FL_RED);
pOkayButton->deactivate();
}
pSourceCallsignInput->damage(FL_DAMAGE_ALL);
}
void CSettingsDlg::AudioInputChoiceCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->AudioInputChoice();
}
void CSettingsDlg::AudioInputChoice()
{
const std::string selected(pAudioInputChoice->text());
auto it = AudioInMap.find(selected);
if (AudioInMap.end() == it)
{
data.sAudioIn.assign("ERROR");
pAudioInputDescBox->label(std::string(selected+notfoundstr).c_str());
}
else
{
data.sAudioIn.assign(it->second.first);
pAudioInputDescBox->label(it->second.second.c_str());
}
}
void CSettingsDlg::AudioOutputChoiceCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->AudioOutputChoice();
}
void CSettingsDlg::AudioOutputChoice()
{
const std::string selected(pAudioOutputChoice->text());
auto it = AudioOutMap.find(selected);
if (AudioOutMap.end() == it)
{
data.sAudioOut.assign(_("ERROR"));
pAudioOutputDescBox->label(std::string(selected+notfoundstr).c_str());
}
else
{
data.sAudioOut.assign(it->second.first);
pAudioOutputDescBox->label(it->second.second.c_str());
}
}
void CSettingsDlg::AudioRescanButtonCB(Fl_Widget *, void *This)
{
((CSettingsDlg *)This)->AudioRescanButton();
}
void CSettingsDlg::AudioRescanButton()
{
auto inchoice = pAudioInputChoice->value();
if (inchoice < 0)
inchoice = 0;
auto outchoice = pAudioOutputChoice->value();
if (outchoice < 0)
outchoice = 0;
void **hints;
if (snd_device_name_hint(-1, "pcm", &hints) < 0)
return;
void **n = hints;
pAudioInputChoice->clear();
pAudioOutputChoice->clear();
AudioInMap.clear();
AudioOutMap.clear();
while (*n != NULL) {
char *name = snd_device_name_get_hint(*n, "NAME");
if (NULL == name) {
n++;
continue;
}
char *desc = snd_device_name_get_hint(*n, "DESC");
if (NULL == desc) {
free(name);
n++;
continue;
}
if ((0==strcmp(name, "default") || strstr(name, "plughw")) && NULL==strstr(desc, "without any conversions")) {
char *io = snd_device_name_get_hint(*n, "IOID");
bool is_input = true, is_output = true;
if (io) { // io == NULL means it's for both input and output
if (0 == strcasecmp(io, "Input")) {
is_output = false;
} else if (0 == strcasecmp(io, "Output")) {
is_input = false;
} else {
std::cerr << _("ERROR: unexpected IOID=") << io << std::endl;
}
free(io);
}
std::string short_name(name);
auto pos = short_name.find("plughw:CARD=");
if (short_name.npos != pos) {
short_name = short_name.replace(pos, 12, "");
pos = short_name.find(",DEV=0");
if (short_name.npos != pos)
short_name = short_name.replace(pos, 6, "");
if (0 == short_name.size())
short_name.assign(name);
}
if (is_input) {
snd_pcm_t *handle;
if (snd_pcm_open(&handle, name, SND_PCM_STREAM_CAPTURE, 0) == 0) {
AudioInMap[short_name] = std::pair<std::string, std::string>(name, desc);
snd_pcm_close(handle);
pAudioInputChoice->add(short_name.c_str());
}
}
if (is_output) {
snd_pcm_t *handle;
if (snd_pcm_open(&handle, name, SND_PCM_STREAM_PLAYBACK, 0) == 0) {
AudioOutMap[short_name] = std::pair<std::string, std::string>(name, desc);
snd_pcm_close(handle);
pAudioOutputChoice->add(short_name.c_str());
}
}
}
if (name) {
free(name);
}
if (desc) {
free(desc);
}
n++;
}
snd_device_name_free_hint(hints);
pAudioInputChoice->value(inchoice);
AudioInputChoice();
pAudioOutputChoice->value(outchoice);
AudioOutputChoice();
}