window-basic-settings.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /******************************************************************************
  2. Copyright (C) 2013 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 "obs-app.hpp"
  22. #include "platform.hpp"
  23. #include "qt-wrappers.hpp"
  24. #include "window-basic-main.hpp"
  25. #include "window-basic-settings.hpp"
  26. #include <util/platform.h>
  27. using namespace std;
  28. struct BaseLexer {
  29. lexer lex;
  30. public:
  31. inline BaseLexer() {lexer_init(&lex);}
  32. inline ~BaseLexer() {lexer_free(&lex);}
  33. operator lexer*() {return &lex;}
  34. };
  35. /* parses "[width]x[height]", string, i.e. 1024x768 */
  36. static bool ConvertResText(const char *res, uint32_t &cx, uint32_t &cy)
  37. {
  38. BaseLexer lex;
  39. base_token token;
  40. lexer_start(lex, res);
  41. /* parse width */
  42. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  43. return false;
  44. if (token.type != BASETOKEN_DIGIT)
  45. return false;
  46. cx = std::stoul(token.text.array);
  47. /* parse 'x' */
  48. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  49. return false;
  50. if (strref_cmpi(&token.text, "x") != 0)
  51. return false;
  52. /* parse height */
  53. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  54. return false;
  55. if (token.type != BASETOKEN_DIGIT)
  56. return false;
  57. cy = std::stoul(token.text.array);
  58. /* shouldn't be any more tokens after this */
  59. if (lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  60. return false;
  61. return true;
  62. }
  63. OBSBasicSettings::OBSBasicSettings(QWidget *parent)
  64. : QDialog (parent),
  65. ui (new Ui::OBSBasicSettings),
  66. generalChanged (false),
  67. outputsChanged (false),
  68. audioChanged (false),
  69. videoChanged (false),
  70. pageIndex (0),
  71. loading (true)
  72. {
  73. string path;
  74. ui->setupUi(this);
  75. if (!GetDataFilePath("locale/locale.ini", path))
  76. throw "Could not find locale/locale.ini path";
  77. if (localeIni.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  78. throw "Could not open locale.ini";
  79. LoadSettings(false);
  80. }
  81. void OBSBasicSettings::LoadLanguageList()
  82. {
  83. const char *currentLang = config_get_string(GetGlobalConfig(),
  84. "General", "Language");
  85. ui->language->clear();
  86. size_t numSections = config_num_sections(localeIni);
  87. for (size_t i = 0; i < numSections; i++) {
  88. const char *tag = config_get_section(localeIni, i);
  89. const char *name = config_get_string(localeIni, tag, "Name");
  90. int idx = ui->language->count();
  91. ui->language->addItem(QT_UTF8(name), QT_UTF8(tag));
  92. if (strcmp(tag, currentLang) == 0)
  93. ui->language->setCurrentIndex(idx);
  94. }
  95. ui->language->model()->sort(0);
  96. }
  97. void OBSBasicSettings::LoadGeneralSettings()
  98. {
  99. loading = true;
  100. LoadLanguageList();
  101. loading = false;
  102. }
  103. void OBSBasicSettings::LoadRendererList()
  104. {
  105. const char *renderer = config_get_string(GetGlobalConfig(), "Video",
  106. "Renderer");
  107. #ifdef _WIN32
  108. ui->renderer->addItem(QT_UTF8("Direct3D 11"));
  109. #endif
  110. ui->renderer->addItem(QT_UTF8("OpenGL"));
  111. int idx = ui->renderer->findText(QT_UTF8(renderer));
  112. if (idx == -1)
  113. idx = 0;
  114. ui->renderer->setCurrentIndex(idx);
  115. }
  116. Q_DECLARE_METATYPE(MonitorInfo);
  117. static string ResString(uint32_t cx, uint32_t cy)
  118. {
  119. stringstream res;
  120. res << cx << "x" << cy;
  121. return res.str();
  122. }
  123. /* some nice default output resolution vals */
  124. static const double vals[] =
  125. {
  126. 1.0,
  127. 1.25,
  128. (1.0/0.75),
  129. 1.5,
  130. (1.0/0.6),
  131. 1.75,
  132. 2.0,
  133. 2.25,
  134. 2.5,
  135. 2.75,
  136. 3.0
  137. };
  138. static const size_t numVals = sizeof(vals)/sizeof(double);
  139. void OBSBasicSettings::ResetDownscales(uint32_t cx, uint32_t cy)
  140. {
  141. ui->outputResolution->clear();
  142. for (size_t idx = 0; idx < numVals; idx++) {
  143. uint32_t downscaleCX = uint32_t(double(cx) / vals[idx]);
  144. uint32_t downscaleCY = uint32_t(double(cy) / vals[idx]);
  145. string res = ResString(downscaleCX, downscaleCY);
  146. ui->outputResolution->addItem(res.c_str());
  147. }
  148. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  149. }
  150. void OBSBasicSettings::LoadResolutionLists()
  151. {
  152. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  153. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  154. vector<MonitorInfo> monitors;
  155. ui->baseResolution->clear();
  156. GetMonitors(monitors);
  157. for (MonitorInfo &monitor : monitors) {
  158. string res = ResString(monitor.cx, monitor.cy);
  159. ui->baseResolution->addItem(res.c_str());
  160. }
  161. ResetDownscales(cx, cy);
  162. ui->baseResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  163. cx = config_get_uint(GetGlobalConfig(), "Video", "OutputCX");
  164. cy = config_get_uint(GetGlobalConfig(), "Video", "OutputCY");
  165. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  166. }
  167. static inline void LoadFPSCommon(Ui::OBSBasicSettings *ui)
  168. {
  169. const char *val = config_get_string(GetGlobalConfig(), "Video",
  170. "FPSCommon");
  171. int idx = ui->fpsCommon->findText(val);
  172. if (idx == -1) idx = 3;
  173. ui->fpsCommon->setCurrentIndex(idx);
  174. }
  175. static inline void LoadFPSInteger(Ui::OBSBasicSettings *ui)
  176. {
  177. uint32_t val = config_get_uint(GetGlobalConfig(), "Video", "FPSInt");
  178. ui->fpsInteger->setValue(val);
  179. }
  180. static inline void LoadFPSFraction(Ui::OBSBasicSettings *ui)
  181. {
  182. uint32_t num = config_get_uint(GetGlobalConfig(), "Video", "FPSNum");
  183. uint32_t den = config_get_uint(GetGlobalConfig(), "Video", "FPSDen");
  184. ui->fpsNumerator->setValue(num);
  185. ui->fpsDenominator->setValue(den);
  186. }
  187. void OBSBasicSettings::LoadFPSData()
  188. {
  189. LoadFPSCommon(ui.get());
  190. LoadFPSInteger(ui.get());
  191. LoadFPSFraction(ui.get());
  192. uint32_t fpsType = config_get_uint(GetGlobalConfig(), "Video",
  193. "FPSType");
  194. if (fpsType > 2) fpsType = 0;
  195. ui->fpsType->setCurrentIndex(fpsType);
  196. ui->fpsTypes->setCurrentIndex(fpsType);
  197. }
  198. void OBSBasicSettings::LoadVideoSettings()
  199. {
  200. loading = true;
  201. if (video_output_active(obs_video())) {
  202. ui->videoPage->setEnabled(false);
  203. ui->videoMsg->setText(QTStr("Settings.Video.CurrentlyActive"));
  204. }
  205. LoadRendererList();
  206. LoadResolutionLists();
  207. LoadFPSData();
  208. loading = false;
  209. }
  210. void OBSBasicSettings::LoadAudioSettings()
  211. {
  212. uint32_t sampleRate = config_get_uint(GetGlobalConfig(), "Audio",
  213. "SampleRate");
  214. const char *speakers = config_get_string(GetGlobalConfig(), "Audio",
  215. "ChannelSetup");
  216. uint32_t bufferingTime = config_get_uint(GetGlobalConfig(), "Audio",
  217. "BufferingTime");
  218. loading = true;
  219. const char *str;
  220. if (sampleRate == 22050)
  221. str = "22.05khz";
  222. else if (sampleRate == 48000)
  223. str = "48khz";
  224. else
  225. str = "44.1khz";
  226. int sampleRateIdx = ui->sampleRate->findText(str);
  227. if (sampleRateIdx != -1)
  228. ui->sampleRate->setCurrentIndex(sampleRateIdx);
  229. if (strcmp(speakers, "Mono") == 0)
  230. ui->channelSetup->setCurrentIndex(0);
  231. else
  232. ui->channelSetup->setCurrentIndex(1);
  233. ui->audioBufferingTime->setValue(bufferingTime);
  234. loading = false;
  235. }
  236. void OBSBasicSettings::LoadSettings(bool changedOnly)
  237. {
  238. if (!changedOnly || generalChanged)
  239. LoadGeneralSettings();
  240. //if (!changedOnly || outputChanged)
  241. // LoadOutputSettings();
  242. if (!changedOnly || audioChanged)
  243. LoadAudioSettings();
  244. if (!changedOnly || videoChanged)
  245. LoadVideoSettings();
  246. }
  247. void OBSBasicSettings::SaveGeneralSettings()
  248. {
  249. int languageIndex = ui->language->currentIndex();
  250. QVariant langData = ui->language->itemData(languageIndex);
  251. string language = langData.toString().toStdString();
  252. config_set_string(GetGlobalConfig(), "General", "Language",
  253. language.c_str());
  254. }
  255. void OBSBasicSettings::SaveVideoSettings()
  256. {
  257. QString renderer = ui->renderer->currentText();
  258. QString baseResolution = ui->baseResolution->currentText();
  259. QString outputResolution = ui->outputResolution->currentText();
  260. int fpsType = ui->fpsType->currentIndex();
  261. QString fpsCommon = ui->fpsCommon->currentText();
  262. int fpsInteger = ui->fpsInteger->value();
  263. int fpsNumerator = ui->fpsNumerator->value();
  264. int fpsDenominator = ui->fpsDenominator->value();
  265. uint32_t cx, cy;
  266. /* ------------------- */
  267. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  268. QT_TO_UTF8(renderer));
  269. if (ConvertResText(QT_TO_UTF8(baseResolution), cx, cy)) {
  270. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  271. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  272. }
  273. if (ConvertResText(QT_TO_UTF8(outputResolution), cx, cy)) {
  274. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  275. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  276. }
  277. config_set_uint(GetGlobalConfig(), "Video", "FPSType", fpsType);
  278. config_set_string(GetGlobalConfig(), "Video", "FPSCommon",
  279. QT_TO_UTF8(fpsCommon));
  280. config_set_uint(GetGlobalConfig(), "Video", "FPSInt", fpsInteger);
  281. config_set_uint(GetGlobalConfig(), "Video", "FPSNum", fpsNumerator);
  282. config_set_uint(GetGlobalConfig(), "Video", "FPSDen", fpsDenominator);
  283. OBSBasic *window = qobject_cast<OBSBasic*>(parent());
  284. if (window)
  285. window->ResetVideo();
  286. }
  287. void OBSBasicSettings::SaveAudioSettings()
  288. {
  289. QString sampleRateStr = ui->sampleRate->currentText();
  290. int channelSetupIdx = ui->channelSetup->currentIndex();
  291. int bufferingTime = ui->audioBufferingTime->value();
  292. const char *channelSetup;
  293. if (channelSetupIdx == 0)
  294. channelSetup = "Mono";
  295. else
  296. channelSetup = "Stereo";
  297. int sampleRate = 44100;
  298. if (sampleRateStr == "22.05khz")
  299. sampleRate = 22050;
  300. else if (sampleRateStr == "48khz")
  301. sampleRate = 48000;
  302. config_set_uint(GetGlobalConfig(), "Audio", "SampleRate", sampleRate);
  303. config_set_string(GetGlobalConfig(), "Audio", "ChannelSetup",
  304. channelSetup);
  305. config_set_uint(GetGlobalConfig(), "Audio", "BufferingTime",
  306. bufferingTime);
  307. }
  308. void OBSBasicSettings::SaveSettings()
  309. {
  310. if (generalChanged)
  311. SaveGeneralSettings();
  312. //if (outputChanged)
  313. // SaveOutputSettings();
  314. if (audioChanged)
  315. SaveAudioSettings();
  316. if (videoChanged)
  317. SaveVideoSettings();
  318. config_save(GetGlobalConfig());
  319. }
  320. bool OBSBasicSettings::QueryChanges()
  321. {
  322. QMessageBox::StandardButton button;
  323. button = QMessageBox::question(this,
  324. QTStr("Settings.ConfirmTitle"),
  325. QTStr("Settings.Confirm"),
  326. QMessageBox::Yes | QMessageBox::No |
  327. QMessageBox::Cancel);
  328. if (button == QMessageBox::Cancel)
  329. return false;
  330. else if (button == QMessageBox::Yes)
  331. SaveSettings();
  332. else
  333. LoadSettings(true);
  334. ClearChanged();
  335. return true;
  336. }
  337. void OBSBasicSettings::closeEvent(QCloseEvent *event)
  338. {
  339. if (Changed() && !QueryChanges())
  340. event->ignore();
  341. }
  342. void OBSBasicSettings::on_listWidget_itemSelectionChanged()
  343. {
  344. int row = ui->listWidget->currentRow();
  345. if (loading || row == pageIndex)
  346. return;
  347. if (Changed() && !QueryChanges()) {
  348. ui->listWidget->setCurrentRow(pageIndex);
  349. return;
  350. }
  351. pageIndex = row;
  352. }
  353. void OBSBasicSettings::on_buttonBox_clicked(QAbstractButton *button)
  354. {
  355. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  356. if (val == QDialogButtonBox::ApplyRole ||
  357. val == QDialogButtonBox::AcceptRole) {
  358. SaveSettings();
  359. ClearChanged();
  360. }
  361. if (val == QDialogButtonBox::AcceptRole ||
  362. val == QDialogButtonBox::RejectRole) {
  363. ClearChanged();
  364. close();
  365. }
  366. }
  367. static bool ValidResolutions(Ui::OBSBasicSettings *ui)
  368. {
  369. QString baseRes = ui->baseResolution->lineEdit()->text();
  370. QString outputRes = ui->outputResolution->lineEdit()->text();
  371. uint32_t cx, cy;
  372. if (!ConvertResText(QT_TO_UTF8(baseRes), cx, cy) ||
  373. !ConvertResText(QT_TO_UTF8(outputRes), cx, cy)) {
  374. ui->videoMsg->setText(
  375. QTStr("Settings.Video.InvalidResolution"));
  376. return false;
  377. }
  378. ui->videoMsg->setText("");
  379. return true;
  380. }
  381. void OBSBasicSettings::on_language_currentIndexChanged(int index)
  382. {
  383. if (!loading)
  384. generalChanged = true;
  385. UNUSED_PARAMETER(index);
  386. }
  387. void OBSBasicSettings::on_sampleRate_currentIndexChanged(int index)
  388. {
  389. if (!loading) {
  390. audioChanged = true;
  391. ui->audioMsg->setText(QTStr("Settings.ProgramRestart"));
  392. }
  393. UNUSED_PARAMETER(index);
  394. }
  395. void OBSBasicSettings::on_channelSetup_currentIndexChanged(int index)
  396. {
  397. if (!loading) {
  398. audioChanged = true;
  399. ui->audioMsg->setText(QTStr("Settings.ProgramRestart"));
  400. }
  401. UNUSED_PARAMETER(index);
  402. }
  403. void OBSBasicSettings::on_audioBufferingTime_valueChanged(int value)
  404. {
  405. if (!loading) {
  406. audioChanged = true;
  407. ui->audioMsg->setText(QTStr("Settings.ProgramRestart"));
  408. }
  409. UNUSED_PARAMETER(value);
  410. }
  411. void OBSBasicSettings::on_renderer_currentIndexChanged(int index)
  412. {
  413. if (!loading) {
  414. videoChanged = true;
  415. ui->videoMsg->setText(QTStr("Settings.ProgramRestart"));
  416. }
  417. UNUSED_PARAMETER(index);
  418. }
  419. void OBSBasicSettings::on_fpsType_currentIndexChanged(int index)
  420. {
  421. if (!loading)
  422. videoChanged = true;
  423. UNUSED_PARAMETER(index);
  424. }
  425. void OBSBasicSettings::on_baseResolution_editTextChanged(const QString &text)
  426. {
  427. if (!loading && ValidResolutions(ui.get())) {
  428. QString baseResolution = ui->baseResolution->currentText();
  429. uint32_t cx, cy;
  430. ConvertResText(QT_TO_UTF8(baseResolution), cx, cy);
  431. ResetDownscales(cx, cy);
  432. videoChanged = true;
  433. }
  434. UNUSED_PARAMETER(text);
  435. }
  436. void OBSBasicSettings::on_outputResolution_editTextChanged(const QString &text)
  437. {
  438. if (!loading && ValidResolutions(ui.get()))
  439. videoChanged = true;
  440. UNUSED_PARAMETER(text);
  441. }
  442. void OBSBasicSettings::on_fpsCommon_currentIndexChanged(int index)
  443. {
  444. if (!loading)
  445. videoChanged = true;
  446. UNUSED_PARAMETER(index);
  447. }
  448. void OBSBasicSettings::on_fpsInteger_valueChanged(int value)
  449. {
  450. if (!loading)
  451. videoChanged = true;
  452. UNUSED_PARAMETER(value);
  453. }
  454. void OBSBasicSettings::on_fpsNumerator_valueChanged(int value)
  455. {
  456. if (!loading)
  457. videoChanged = true;
  458. UNUSED_PARAMETER(value);
  459. }
  460. void OBSBasicSettings::on_fpsDenominator_valueChanged(int value)
  461. {
  462. if (!loading)
  463. videoChanged = true;
  464. UNUSED_PARAMETER(value);
  465. }