window-basic-settings.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /******************************************************************************
  2. Copyright (C) 2013-2014 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs.hpp>
  15. #include <util/util.hpp>
  16. #include <util/lexer.h>
  17. #include <sstream>
  18. #include <QLineEdit>
  19. #include <QMessageBox>
  20. #include <QCloseEvent>
  21. #include <QFileDialog>
  22. #include "obs-app.hpp"
  23. #include "platform.hpp"
  24. #include "properties-view.hpp"
  25. #include "qt-wrappers.hpp"
  26. #include "window-basic-main.hpp"
  27. #include "window-basic-settings.hpp"
  28. #include <util/platform.h>
  29. using namespace std;
  30. /* parses "[width]x[height]", string, i.e. 1024x768 */
  31. static bool ConvertResText(const char *res, uint32_t &cx, uint32_t &cy)
  32. {
  33. BaseLexer lex;
  34. base_token token;
  35. lexer_start(lex, res);
  36. /* parse width */
  37. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  38. return false;
  39. if (token.type != BASETOKEN_DIGIT)
  40. return false;
  41. cx = std::stoul(token.text.array);
  42. /* parse 'x' */
  43. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  44. return false;
  45. if (strref_cmpi(&token.text, "x") != 0)
  46. return false;
  47. /* parse height */
  48. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  49. return false;
  50. if (token.type != BASETOKEN_DIGIT)
  51. return false;
  52. cy = std::stoul(token.text.array);
  53. /* shouldn't be any more tokens after this */
  54. if (lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  55. return false;
  56. return true;
  57. }
  58. static inline bool WidgetChanged(QWidget *widget)
  59. {
  60. return widget->property("changed").toBool();
  61. }
  62. static inline void SetComboByName(QComboBox *combo, const char *name)
  63. {
  64. int idx = combo->findText(QT_UTF8(name));
  65. if (idx != -1)
  66. combo->setCurrentIndex(idx);
  67. }
  68. static inline void SetComboByValue(QComboBox *combo, const char *name)
  69. {
  70. int idx = combo->findData(QT_UTF8(name));
  71. if (idx != -1)
  72. combo->setCurrentIndex(idx);
  73. }
  74. static inline QString GetComboData(QComboBox *combo)
  75. {
  76. int idx = combo->currentIndex();
  77. if (idx == -1)
  78. return QString();
  79. return combo->itemData(idx).toString();
  80. }
  81. void OBSBasicSettings::HookWidget(QWidget *widget, const char *signal,
  82. const char *slot)
  83. {
  84. QObject::connect(widget, signal, this, slot);
  85. widget->setProperty("changed", QVariant(false));
  86. }
  87. #define COMBO_CHANGED SIGNAL(currentIndexChanged(int))
  88. #define EDIT_CHANGED SIGNAL(textChanged(const QString &))
  89. #define CBEDIT_CHANGED SIGNAL(editTextChanged(const QString &))
  90. #define SCROLL_CHANGED SIGNAL(valueChanged(int))
  91. #define GENERAL_CHANGED SLOT(GeneralChanged())
  92. #define OUTPUTS_CHANGED SLOT(OutputsChanged())
  93. #define AUDIO_RESTART SLOT(AudioChangedRestart())
  94. #define AUDIO_CHANGED SLOT(AudioChanged())
  95. #define VIDEO_RESTART SLOT(VideoChangedRestart())
  96. #define VIDEO_RES SLOT(VideoChangedResolution())
  97. #define VIDEO_CHANGED SLOT(VideoChanged())
  98. OBSBasicSettings::OBSBasicSettings(QWidget *parent)
  99. : QDialog (parent),
  100. main (qobject_cast<OBSBasic*>(parent)),
  101. ui (new Ui::OBSBasicSettings),
  102. generalChanged (false),
  103. outputsChanged (false),
  104. audioChanged (false),
  105. videoChanged (false),
  106. pageIndex (0),
  107. loading (true),
  108. streamProperties (nullptr)
  109. {
  110. string path;
  111. ui->setupUi(this);
  112. if (!GetDataFilePath("locale/locale.ini", path))
  113. throw "Could not find locale/locale.ini path";
  114. if (localeIni.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  115. throw "Could not open locale.ini";
  116. HookWidget(ui->language, COMBO_CHANGED, GENERAL_CHANGED);
  117. HookWidget(ui->outputMode, COMBO_CHANGED, OUTPUTS_CHANGED);
  118. HookWidget(ui->simpleOutputPath, EDIT_CHANGED, OUTPUTS_CHANGED);
  119. HookWidget(ui->simpleOutputVBitrate, SCROLL_CHANGED, OUTPUTS_CHANGED);
  120. HookWidget(ui->simpleOutputABitrate, COMBO_CHANGED, OUTPUTS_CHANGED);
  121. HookWidget(ui->channelSetup, COMBO_CHANGED, AUDIO_RESTART);
  122. HookWidget(ui->sampleRate, COMBO_CHANGED, AUDIO_RESTART);
  123. HookWidget(ui->desktopAudioDevice1, COMBO_CHANGED, AUDIO_CHANGED);
  124. HookWidget(ui->desktopAudioDevice2, COMBO_CHANGED, AUDIO_CHANGED);
  125. HookWidget(ui->auxAudioDevice1, COMBO_CHANGED, AUDIO_CHANGED);
  126. HookWidget(ui->auxAudioDevice2, COMBO_CHANGED, AUDIO_CHANGED);
  127. HookWidget(ui->auxAudioDevice3, COMBO_CHANGED, AUDIO_CHANGED);
  128. HookWidget(ui->renderer, COMBO_CHANGED, VIDEO_RESTART);
  129. HookWidget(ui->adapter, COMBO_CHANGED, VIDEO_RESTART);
  130. HookWidget(ui->baseResolution, CBEDIT_CHANGED, VIDEO_RES);
  131. HookWidget(ui->outputResolution, CBEDIT_CHANGED, VIDEO_RES);
  132. HookWidget(ui->downscaleFilter, COMBO_CHANGED, VIDEO_CHANGED);
  133. HookWidget(ui->fpsType, COMBO_CHANGED, VIDEO_CHANGED);
  134. HookWidget(ui->fpsCommon, COMBO_CHANGED, VIDEO_CHANGED);
  135. HookWidget(ui->fpsInteger, SCROLL_CHANGED, VIDEO_CHANGED);
  136. HookWidget(ui->fpsInteger, SCROLL_CHANGED, VIDEO_CHANGED);
  137. HookWidget(ui->fpsNumerator, SCROLL_CHANGED, VIDEO_CHANGED);
  138. HookWidget(ui->fpsDenominator, SCROLL_CHANGED, VIDEO_CHANGED);
  139. //Apply button disabled until change.
  140. EnableApplyButton(false);
  141. LoadServiceTypes();
  142. LoadServiceInfo();
  143. LoadSettings(false);
  144. }
  145. void OBSBasicSettings::SaveCombo(QComboBox *widget, const char *section,
  146. const char *value)
  147. {
  148. if (WidgetChanged(widget))
  149. config_set_string(main->Config(), section, value,
  150. QT_TO_UTF8(widget->currentText()));
  151. }
  152. void OBSBasicSettings::SaveComboData(QComboBox *widget, const char *section,
  153. const char *value)
  154. {
  155. if (WidgetChanged(widget)) {
  156. QString str = GetComboData(widget);
  157. config_set_string(main->Config(), section, value,
  158. QT_TO_UTF8(str));
  159. }
  160. }
  161. void OBSBasicSettings::SaveEdit(QLineEdit *widget, const char *section,
  162. const char *value)
  163. {
  164. if (WidgetChanged(widget))
  165. config_set_string(main->Config(), section, value,
  166. QT_TO_UTF8(widget->text()));
  167. }
  168. void OBSBasicSettings::SaveSpinBox(QSpinBox *widget, const char *section,
  169. const char *value)
  170. {
  171. if (WidgetChanged(widget))
  172. config_set_int(main->Config(), section, value, widget->value());
  173. }
  174. void OBSBasicSettings::LoadServiceTypes()
  175. {
  176. const char *type;
  177. size_t idx = 0;
  178. while (obs_enum_service_types(idx++, &type)) {
  179. const char *name = obs_service_getdisplayname(type);
  180. QString qName = QT_UTF8(name);
  181. QString qType = QT_UTF8(type);
  182. ui->streamType->addItem(qName, qType);
  183. }
  184. type = obs_service_gettype(main->GetService());
  185. SetComboByValue(ui->streamType, type);
  186. }
  187. void OBSBasicSettings::LoadServiceInfo()
  188. {
  189. QLayout *layout = ui->streamContainer->layout();
  190. obs_service_t service = main->GetService();
  191. obs_data_t settings = obs_service_get_settings(service);
  192. obs_properties_t properties = obs_service_properties(service);
  193. delete streamProperties;
  194. streamProperties = new OBSPropertiesView(
  195. settings,
  196. properties,
  197. service,
  198. (PropertiesUpdateCallback)obs_service_update,
  199. 170);
  200. layout->addWidget(streamProperties);
  201. obs_data_release(settings);
  202. }
  203. void OBSBasicSettings::LoadLanguageList()
  204. {
  205. const char *currentLang = config_get_string(GetGlobalConfig(),
  206. "General", "Language");
  207. ui->language->clear();
  208. size_t numSections = config_num_sections(localeIni);
  209. for (size_t i = 0; i < numSections; i++) {
  210. const char *tag = config_get_section(localeIni, i);
  211. const char *name = config_get_string(localeIni, tag, "Name");
  212. int idx = ui->language->count();
  213. ui->language->addItem(QT_UTF8(name), QT_UTF8(tag));
  214. if (strcmp(tag, currentLang) == 0)
  215. ui->language->setCurrentIndex(idx);
  216. }
  217. ui->language->model()->sort(0);
  218. }
  219. void OBSBasicSettings::LoadGeneralSettings()
  220. {
  221. loading = true;
  222. LoadLanguageList();
  223. loading = false;
  224. }
  225. void OBSBasicSettings::LoadRendererList()
  226. {
  227. const char *renderer = config_get_string(GetGlobalConfig(), "Video",
  228. "Renderer");
  229. #ifdef _WIN32
  230. ui->renderer->addItem(QT_UTF8("Direct3D 11"));
  231. #endif
  232. ui->renderer->addItem(QT_UTF8("OpenGL"));
  233. int idx = ui->renderer->findText(QT_UTF8(renderer));
  234. if (idx == -1)
  235. idx = 0;
  236. ui->renderer->setCurrentIndex(idx);
  237. }
  238. Q_DECLARE_METATYPE(MonitorInfo);
  239. static string ResString(uint32_t cx, uint32_t cy)
  240. {
  241. stringstream res;
  242. res << cx << "x" << cy;
  243. return res.str();
  244. }
  245. /* some nice default output resolution vals */
  246. static const double vals[] =
  247. {
  248. 1.0,
  249. 1.25,
  250. (1.0/0.75),
  251. 1.5,
  252. (1.0/0.6),
  253. 1.75,
  254. 2.0,
  255. 2.25,
  256. 2.5,
  257. 2.75,
  258. 3.0
  259. };
  260. static const size_t numVals = sizeof(vals)/sizeof(double);
  261. void OBSBasicSettings::ResetDownscales(uint32_t cx, uint32_t cy)
  262. {
  263. ui->outputResolution->clear();
  264. for (size_t idx = 0; idx < numVals; idx++) {
  265. uint32_t downscaleCX = uint32_t(double(cx) / vals[idx]);
  266. uint32_t downscaleCY = uint32_t(double(cy) / vals[idx]);
  267. string res = ResString(downscaleCX, downscaleCY);
  268. ui->outputResolution->addItem(res.c_str());
  269. }
  270. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  271. }
  272. void OBSBasicSettings::LoadResolutionLists()
  273. {
  274. uint32_t cx = config_get_uint(main->Config(), "Video", "BaseCX");
  275. uint32_t cy = config_get_uint(main->Config(), "Video", "BaseCY");
  276. vector<MonitorInfo> monitors;
  277. ui->baseResolution->clear();
  278. GetMonitors(monitors);
  279. for (MonitorInfo &monitor : monitors) {
  280. string res = ResString(monitor.cx, monitor.cy);
  281. ui->baseResolution->addItem(res.c_str());
  282. }
  283. ResetDownscales(cx, cy);
  284. ui->baseResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  285. cx = config_get_uint(main->Config(), "Video", "OutputCX");
  286. cy = config_get_uint(main->Config(), "Video", "OutputCY");
  287. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  288. }
  289. static inline void LoadFPSCommon(OBSBasic *main, Ui::OBSBasicSettings *ui)
  290. {
  291. const char *val = config_get_string(main->Config(), "Video",
  292. "FPSCommon");
  293. int idx = ui->fpsCommon->findText(val);
  294. if (idx == -1) idx = 3;
  295. ui->fpsCommon->setCurrentIndex(idx);
  296. }
  297. static inline void LoadFPSInteger(OBSBasic *main, Ui::OBSBasicSettings *ui)
  298. {
  299. uint32_t val = config_get_uint(main->Config(), "Video", "FPSInt");
  300. ui->fpsInteger->setValue(val);
  301. }
  302. static inline void LoadFPSFraction(OBSBasic *main, Ui::OBSBasicSettings *ui)
  303. {
  304. uint32_t num = config_get_uint(main->Config(), "Video", "FPSNum");
  305. uint32_t den = config_get_uint(main->Config(), "Video", "FPSDen");
  306. ui->fpsNumerator->setValue(num);
  307. ui->fpsDenominator->setValue(den);
  308. }
  309. void OBSBasicSettings::LoadFPSData()
  310. {
  311. LoadFPSCommon(main, ui.get());
  312. LoadFPSInteger(main, ui.get());
  313. LoadFPSFraction(main, ui.get());
  314. uint32_t fpsType = config_get_uint(main->Config(), "Video",
  315. "FPSType");
  316. if (fpsType > 2) fpsType = 0;
  317. ui->fpsType->setCurrentIndex(fpsType);
  318. ui->fpsTypes->setCurrentIndex(fpsType);
  319. }
  320. void OBSBasicSettings::LoadVideoSettings()
  321. {
  322. loading = true;
  323. if (video_output_active(obs_video())) {
  324. ui->videoPage->setEnabled(false);
  325. ui->videoMsg->setText(
  326. QTStr("Basic.Settings.Video.CurrentlyActive"));
  327. }
  328. LoadRendererList();
  329. LoadResolutionLists();
  330. LoadFPSData();
  331. loading = false;
  332. }
  333. void OBSBasicSettings::LoadSimpleOutputSettings()
  334. {
  335. const char *path = config_get_string(main->Config(), "SimpleOutput",
  336. "FilePath");
  337. int videoBitrate = config_get_uint(main->Config(), "SimpleOutput",
  338. "VBitrate");
  339. int audioBitrate = config_get_uint(main->Config(), "SimpleOutput",
  340. "ABitrate");
  341. ui->simpleOutputPath->setText(path);
  342. ui->simpleOutputVBitrate->setValue(videoBitrate);
  343. SetComboByName(ui->simpleOutputABitrate,
  344. std::to_string(audioBitrate).c_str());
  345. }
  346. void OBSBasicSettings::LoadOutputSettings()
  347. {
  348. loading = true;
  349. LoadSimpleOutputSettings();
  350. loading = false;
  351. }
  352. static inline void LoadListValue(QComboBox *widget, const char *text,
  353. const char *val)
  354. {
  355. widget->addItem(QT_UTF8(text), QT_UTF8(val));
  356. }
  357. void OBSBasicSettings::LoadListValues(QComboBox *widget, obs_property_t prop,
  358. const char *configName)
  359. {
  360. size_t count = obs_property_list_item_count(prop);
  361. const char *deviceId = config_get_string(main->Config(), "Audio",
  362. configName);
  363. widget->addItem(QTStr("Disabled"), "disabled");
  364. for (size_t i = 0; i < count; i++) {
  365. const char *name = obs_property_list_item_name(prop, i);
  366. const char *val = obs_property_list_item_string(prop, i);
  367. LoadListValue(widget, name, val);
  368. }
  369. int idx = widget->findData(QVariant(QT_UTF8(deviceId)));
  370. if (idx == -1) {
  371. deviceId = config_get_default_string(main->Config(), "Audio",
  372. configName);
  373. idx = widget->findData(QVariant(QT_UTF8(deviceId)));
  374. }
  375. if (idx != -1)
  376. widget->setCurrentIndex(idx);
  377. }
  378. void OBSBasicSettings::LoadAudioDevices()
  379. {
  380. const char *input_id = App()->InputAudioSource();
  381. const char *output_id = App()->OutputAudioSource();
  382. obs_properties_t input_props = obs_get_source_properties(
  383. OBS_SOURCE_TYPE_INPUT, input_id);
  384. obs_properties_t output_props = obs_get_source_properties(
  385. OBS_SOURCE_TYPE_INPUT, output_id);
  386. if (input_props) {
  387. obs_property_t inputs = obs_properties_get(input_props,
  388. "device_id");
  389. LoadListValues(ui->auxAudioDevice1, inputs, "AuxDevice1");
  390. LoadListValues(ui->auxAudioDevice2, inputs, "AuxDevice2");
  391. LoadListValues(ui->auxAudioDevice3, inputs, "AuxDevice3");
  392. obs_properties_destroy(input_props);
  393. }
  394. if (output_props) {
  395. obs_property_t outputs = obs_properties_get(output_props,
  396. "device_id");
  397. LoadListValues(ui->desktopAudioDevice1, outputs,
  398. "DesktopDevice1");
  399. LoadListValues(ui->desktopAudioDevice2, outputs,
  400. "DesktopDevice2");
  401. obs_properties_destroy(output_props);
  402. }
  403. }
  404. void OBSBasicSettings::LoadAudioSettings()
  405. {
  406. uint32_t sampleRate = config_get_uint(main->Config(), "Audio",
  407. "SampleRate");
  408. const char *speakers = config_get_string(main->Config(), "Audio",
  409. "ChannelSetup");
  410. loading = true;
  411. const char *str;
  412. if (sampleRate == 22050)
  413. str = "22.05khz";
  414. else if (sampleRate == 48000)
  415. str = "48khz";
  416. else
  417. str = "44.1khz";
  418. int sampleRateIdx = ui->sampleRate->findText(str);
  419. if (sampleRateIdx != -1)
  420. ui->sampleRate->setCurrentIndex(sampleRateIdx);
  421. if (strcmp(speakers, "Mono") == 0)
  422. ui->channelSetup->setCurrentIndex(0);
  423. else
  424. ui->channelSetup->setCurrentIndex(1);
  425. LoadAudioDevices();
  426. loading = false;
  427. }
  428. void OBSBasicSettings::LoadSettings(bool changedOnly)
  429. {
  430. if (!changedOnly || generalChanged)
  431. LoadGeneralSettings();
  432. if (!changedOnly || outputsChanged)
  433. LoadOutputSettings();
  434. if (!changedOnly || audioChanged)
  435. LoadAudioSettings();
  436. if (!changedOnly || videoChanged)
  437. LoadVideoSettings();
  438. }
  439. void OBSBasicSettings::SaveGeneralSettings()
  440. {
  441. int languageIndex = ui->language->currentIndex();
  442. QVariant langData = ui->language->itemData(languageIndex);
  443. string language = langData.toString().toStdString();
  444. if (WidgetChanged(ui->language))
  445. config_set_string(GetGlobalConfig(), "General", "Language",
  446. language.c_str());
  447. }
  448. void OBSBasicSettings::SaveVideoSettings()
  449. {
  450. QString baseResolution = ui->baseResolution->currentText();
  451. QString outputResolution = ui->outputResolution->currentText();
  452. int fpsType = ui->fpsType->currentIndex();
  453. uint32_t cx, cy;
  454. /* ------------------- */
  455. SaveCombo(ui->renderer, "Video", "Renderer");
  456. if (WidgetChanged(ui->baseResolution) &&
  457. ConvertResText(QT_TO_UTF8(baseResolution), cx, cy)) {
  458. config_set_uint(main->Config(), "Video", "BaseCX", cx);
  459. config_set_uint(main->Config(), "Video", "BaseCY", cy);
  460. }
  461. if (WidgetChanged(ui->outputResolution) &&
  462. ConvertResText(QT_TO_UTF8(outputResolution), cx, cy)) {
  463. config_set_uint(main->Config(), "Video", "OutputCX", cx);
  464. config_set_uint(main->Config(), "Video", "OutputCY", cy);
  465. }
  466. if (WidgetChanged(ui->fpsType))
  467. config_set_uint(main->Config(), "Video", "FPSType", fpsType);
  468. SaveCombo(ui->fpsCommon, "Video", "FPSCommon");
  469. SaveSpinBox(ui->fpsInteger, "Video", "FPSInt");
  470. SaveSpinBox(ui->fpsNumerator, "Video", "FPSNum");
  471. SaveSpinBox(ui->fpsDenominator, "Video", "FPSDen");
  472. main->ResetVideo();
  473. }
  474. /* TODO: Temporary! */
  475. void OBSBasicSettings::SaveOutputSettings()
  476. {
  477. SaveSpinBox(ui->simpleOutputVBitrate, "SimpleOutput", "VBitrate");
  478. SaveCombo(ui->simpleOutputABitrate, "SimpleOutput", "ABitrate");
  479. SaveEdit(ui->simpleOutputPath, "SimpleOutput", "FilePath");
  480. }
  481. void OBSBasicSettings::SaveAudioSettings()
  482. {
  483. QString sampleRateStr = ui->sampleRate->currentText();
  484. int channelSetupIdx = ui->channelSetup->currentIndex();
  485. const char *channelSetup = (channelSetupIdx == 0) ? "Mono" : "Stereo";
  486. int sampleRate = 44100;
  487. if (sampleRateStr == "22.05khz")
  488. sampleRate = 22050;
  489. else if (sampleRateStr == "48khz")
  490. sampleRate = 48000;
  491. if (WidgetChanged(ui->sampleRate))
  492. config_set_uint(main->Config(), "Audio", "SampleRate",
  493. sampleRate);
  494. if (WidgetChanged(ui->channelSetup))
  495. config_set_string(main->Config(), "Audio", "ChannelSetup",
  496. channelSetup);
  497. SaveComboData(ui->desktopAudioDevice1, "Audio", "DesktopDevice1");
  498. SaveComboData(ui->desktopAudioDevice2, "Audio", "DesktopDevice2");
  499. SaveComboData(ui->auxAudioDevice1, "Audio", "AuxDevice1");
  500. SaveComboData(ui->auxAudioDevice2, "Audio", "AuxDevice2");
  501. SaveComboData(ui->auxAudioDevice3, "Audio", "AuxDevice3");
  502. main->ResetAudioDevices();
  503. }
  504. void OBSBasicSettings::SaveSettings()
  505. {
  506. if (generalChanged)
  507. SaveGeneralSettings();
  508. if (outputsChanged)
  509. SaveOutputSettings();
  510. if (audioChanged)
  511. SaveAudioSettings();
  512. if (videoChanged)
  513. SaveVideoSettings();
  514. config_save(main->Config());
  515. config_save(GetGlobalConfig());
  516. }
  517. bool OBSBasicSettings::QueryChanges()
  518. {
  519. QMessageBox::StandardButton button;
  520. button = QMessageBox::question(this,
  521. QTStr("Basic.Settings.ConfirmTitle"),
  522. QTStr("Basic.Settings.Confirm"),
  523. QMessageBox::Yes | QMessageBox::No |
  524. QMessageBox::Cancel);
  525. if (button == QMessageBox::Cancel)
  526. return false;
  527. else if (button == QMessageBox::Yes)
  528. SaveSettings();
  529. else
  530. LoadSettings(true);
  531. ClearChanged();
  532. return true;
  533. }
  534. void OBSBasicSettings::closeEvent(QCloseEvent *event)
  535. {
  536. if (Changed() && !QueryChanges())
  537. event->ignore();
  538. }
  539. void OBSBasicSettings::on_listWidget_itemSelectionChanged()
  540. {
  541. int row = ui->listWidget->currentRow();
  542. if (loading || row == pageIndex)
  543. return;
  544. pageIndex = row;
  545. }
  546. void OBSBasicSettings::on_buttonBox_clicked(QAbstractButton *button)
  547. {
  548. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  549. if (val == QDialogButtonBox::ApplyRole ||
  550. val == QDialogButtonBox::AcceptRole) {
  551. SaveSettings();
  552. ClearChanged();
  553. }
  554. if (val == QDialogButtonBox::AcceptRole ||
  555. val == QDialogButtonBox::RejectRole) {
  556. ClearChanged();
  557. close();
  558. }
  559. }
  560. void OBSBasicSettings::on_streamType_currentIndexChanged(int idx)
  561. {
  562. QString val = ui->streamType->itemData(idx).toString();
  563. obs_service_t newService;
  564. if (loading)
  565. return;
  566. delete streamProperties;
  567. streamProperties = nullptr;
  568. newService = obs_service_create(QT_TO_UTF8(val), nullptr, nullptr);
  569. if (newService)
  570. main->SetService(newService);
  571. LoadServiceInfo();
  572. }
  573. void OBSBasicSettings::on_simpleOutputBrowse_clicked()
  574. {
  575. QString dir = QFileDialog::getExistingDirectory(this,
  576. QTStr("OpenDirectory"),
  577. ui->simpleOutputPath->text(),
  578. QFileDialog::ShowDirsOnly |
  579. QFileDialog::DontResolveSymlinks);
  580. if (dir.isEmpty())
  581. return;
  582. ui->simpleOutputPath->setText(dir);
  583. }
  584. static inline bool StreamExists(const char *name)
  585. {
  586. return obs_get_service_by_name(name) != nullptr;
  587. }
  588. #define INVALID_RES_STR "Basic.Settings.Video.InvalidResolution"
  589. static bool ValidResolutions(Ui::OBSBasicSettings *ui)
  590. {
  591. QString baseRes = ui->baseResolution->lineEdit()->text();
  592. QString outputRes = ui->outputResolution->lineEdit()->text();
  593. uint32_t cx, cy;
  594. if (!ConvertResText(QT_TO_UTF8(baseRes), cx, cy) ||
  595. !ConvertResText(QT_TO_UTF8(outputRes), cx, cy)) {
  596. ui->videoMsg->setText(QTStr(INVALID_RES_STR));
  597. return false;
  598. }
  599. ui->videoMsg->setText("");
  600. return true;
  601. }
  602. void OBSBasicSettings::on_baseResolution_editTextChanged(const QString &text)
  603. {
  604. if (!loading && ValidResolutions(ui.get())) {
  605. QString baseResolution = text;
  606. uint32_t cx, cy;
  607. ConvertResText(QT_TO_UTF8(baseResolution), cx, cy);
  608. ResetDownscales(cx, cy);
  609. }
  610. }
  611. void OBSBasicSettings::GeneralChanged()
  612. {
  613. if (!loading) {
  614. generalChanged = true;
  615. sender()->setProperty("changed", QVariant(true));
  616. EnableApplyButton(true);
  617. }
  618. }
  619. void OBSBasicSettings::OutputsChanged()
  620. {
  621. if (!loading) {
  622. outputsChanged = true;
  623. sender()->setProperty("changed", QVariant(true));
  624. EnableApplyButton(true);
  625. }
  626. }
  627. void OBSBasicSettings::AudioChanged()
  628. {
  629. if (!loading) {
  630. audioChanged = true;
  631. sender()->setProperty("changed", QVariant(true));
  632. EnableApplyButton(true);
  633. }
  634. }
  635. void OBSBasicSettings::AudioChangedRestart()
  636. {
  637. if (!loading) {
  638. audioChanged = true;
  639. ui->audioMsg->setText(QTStr("Basic.Settings.ProgramRestart"));
  640. sender()->setProperty("changed", QVariant(true));
  641. EnableApplyButton(true);
  642. }
  643. }
  644. void OBSBasicSettings::VideoChangedRestart()
  645. {
  646. if (!loading) {
  647. videoChanged = true;
  648. ui->videoMsg->setText(QTStr("Basic.Settings.ProgramRestart"));
  649. sender()->setProperty("changed", QVariant(true));
  650. EnableApplyButton(true);
  651. }
  652. }
  653. void OBSBasicSettings::VideoChangedResolution()
  654. {
  655. if (!loading && ValidResolutions(ui.get())) {
  656. videoChanged = true;
  657. sender()->setProperty("changed", QVariant(true));
  658. EnableApplyButton(true);
  659. }
  660. }
  661. void OBSBasicSettings::VideoChanged()
  662. {
  663. if (!loading) {
  664. videoChanged = true;
  665. sender()->setProperty("changed", QVariant(true));
  666. EnableApplyButton(true);
  667. }
  668. }