window-basic-settings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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 <util/util.hpp>
  15. #include <util/lexer.h>
  16. #include <sstream>
  17. #include <QLineEdit>
  18. #include <QMessageBox>
  19. #include <QCloseEvent>
  20. #include "obs-app.hpp"
  21. #include "platform.hpp"
  22. #include "qt-wrappers.hpp"
  23. #include "window-basic-settings.hpp"
  24. #include <util/platform.h>
  25. using namespace std;
  26. struct BaseLexer {
  27. lexer lex;
  28. public:
  29. inline BaseLexer() {lexer_init(&lex);}
  30. inline ~BaseLexer() {lexer_free(&lex);}
  31. operator lexer*() {return &lex;}
  32. };
  33. /* parses "[width]x[height]", string, i.e. 1024x768 */
  34. static bool ConvertResText(const char *res, uint32_t &cx, uint32_t &cy)
  35. {
  36. BaseLexer lex;
  37. base_token token;
  38. lexer_start(lex, res);
  39. /* parse width */
  40. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  41. return false;
  42. if (token.type != BASETOKEN_DIGIT)
  43. return false;
  44. cx = std::stoul(token.text.array);
  45. /* parse 'x' */
  46. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  47. return false;
  48. if (strref_cmpi(&token.text, "x") != 0)
  49. return false;
  50. /* parse height */
  51. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  52. return false;
  53. if (token.type != BASETOKEN_DIGIT)
  54. return false;
  55. cy = std::stoul(token.text.array);
  56. /* shouldn't be any more tokens after this */
  57. if (lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  58. return false;
  59. return true;
  60. }
  61. OBSBasicSettings::OBSBasicSettings(QWidget *parent)
  62. : QDialog (parent),
  63. ui (new Ui::OBSBasicSettings),
  64. generalChanged (false),
  65. outputsChanged (false),
  66. audioChanged (false),
  67. videoChanged (false),
  68. pageIndex (0),
  69. loading (true)
  70. {
  71. string path;
  72. ui->setupUi(this);
  73. if (!GetDataFilePath("locale/locale.ini", path))
  74. throw "Could not find locale/locale.ini path";
  75. if (localeIni.Open(path.c_str(), CONFIG_OPEN_EXISTING) != 0)
  76. throw "Could not open locale.ini";
  77. LoadSettings(false);
  78. }
  79. void OBSBasicSettings::LoadLanguageList()
  80. {
  81. const char *currentLang = config_get_string(GetGlobalConfig(),
  82. "General", "Language");
  83. ui->language->clear();
  84. size_t numSections = config_num_sections(localeIni);
  85. for (size_t i = 0; i < numSections; i++) {
  86. const char *tag = config_get_section(localeIni, i);
  87. const char *name = config_get_string(localeIni, tag, "Name");
  88. int idx = ui->language->count();
  89. ui->language->addItem(QT_UTF8(name), QT_UTF8(tag));
  90. if (strcmp(tag, currentLang) == 0)
  91. ui->language->setCurrentIndex(idx);
  92. }
  93. ui->language->model()->sort(0);
  94. }
  95. void OBSBasicSettings::LoadGeneralSettings()
  96. {
  97. loading = true;
  98. LoadLanguageList();
  99. loading = false;
  100. }
  101. void OBSBasicSettings::LoadRendererList()
  102. {
  103. const char *renderer = config_get_string(GetGlobalConfig(), "Video",
  104. "Renderer");
  105. #ifdef _WIN32
  106. ui->renderer->addItem(QT_UTF8("Direct3D 11"));
  107. #endif
  108. ui->renderer->addItem(QT_UTF8("OpenGL"));
  109. int idx = ui->renderer->findText(QT_UTF8(renderer));
  110. if (idx == -1)
  111. idx = 0;
  112. ui->renderer->setCurrentIndex(idx);
  113. }
  114. Q_DECLARE_METATYPE(MonitorInfo);
  115. static string ResString(uint32_t cx, uint32_t cy)
  116. {
  117. stringstream res;
  118. res << cx << "x" << cy;
  119. return res.str();
  120. }
  121. /* some nice default output resolution vals */
  122. static const double vals[] =
  123. {
  124. 1.0,
  125. 1.25,
  126. (1.0/0.75),
  127. 1.5,
  128. (1.0/0.6),
  129. 1.75,
  130. 2.0,
  131. 2.25,
  132. 2.5,
  133. 2.75,
  134. 3.0
  135. };
  136. static const size_t numVals = sizeof(vals)/sizeof(double);
  137. void OBSBasicSettings::ResetDownscales(uint32_t cx, uint32_t cy)
  138. {
  139. for (size_t idx = 0; idx < numVals; idx++) {
  140. uint32_t downscaleCX = uint32_t(double(cx) / vals[idx]);
  141. uint32_t downscaleCY = uint32_t(double(cy) / vals[idx]);
  142. string res = ResString(downscaleCX, downscaleCY);
  143. ui->outputResolution->addItem(res.c_str());
  144. }
  145. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  146. }
  147. void OBSBasicSettings::LoadResolutionLists()
  148. {
  149. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  150. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  151. vector<MonitorInfo> monitors;
  152. ui->baseResolution->clear();
  153. ui->outputResolution->clear();
  154. GetMonitors(monitors);
  155. for (MonitorInfo &monitor : monitors) {
  156. string res = ResString(monitor.cx, monitor.cy);
  157. ui->baseResolution->addItem(res.c_str());
  158. }
  159. ResetDownscales(cx, cy);
  160. cx = config_get_uint(GetGlobalConfig(), "Video", "OutputCX");
  161. cy = config_get_uint(GetGlobalConfig(), "Video", "OutputCY");
  162. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  163. }
  164. static inline void LoadFPSCommon(Ui::OBSBasicSettings *ui)
  165. {
  166. const char *val = config_get_string(GetGlobalConfig(), "Video",
  167. "FPSCommon");
  168. int idx = ui->fpsCommon->findText(val);
  169. if (idx == -1) idx = 3;
  170. ui->fpsCommon->setCurrentIndex(idx);
  171. }
  172. static inline void LoadFPSInteger(Ui::OBSBasicSettings *ui)
  173. {
  174. uint32_t val = config_get_uint(GetGlobalConfig(), "Video", "FPSInt");
  175. ui->fpsInteger->setValue(val);
  176. }
  177. static inline void LoadFPSFraction(Ui::OBSBasicSettings *ui)
  178. {
  179. uint32_t num = config_get_uint(GetGlobalConfig(), "Video", "FPSNum");
  180. uint32_t den = config_get_uint(GetGlobalConfig(), "Video", "FPSDen");
  181. ui->fpsNumerator->setValue(num);
  182. ui->fpsDenominator->setValue(den);
  183. }
  184. void OBSBasicSettings::LoadFPSData()
  185. {
  186. LoadFPSCommon(ui.get());
  187. LoadFPSInteger(ui.get());
  188. LoadFPSFraction(ui.get());
  189. uint32_t fpsType = config_get_uint(GetGlobalConfig(), "Video",
  190. "FPSType");
  191. if (fpsType > 2) fpsType = 0;
  192. ui->fpsType->setCurrentIndex(fpsType);
  193. ui->fpsTypes->setCurrentIndex(fpsType);
  194. }
  195. void OBSBasicSettings::LoadVideoSettings()
  196. {
  197. loading = true;
  198. LoadRendererList();
  199. LoadResolutionLists();
  200. LoadFPSData();
  201. loading = false;
  202. }
  203. void OBSBasicSettings::LoadSettings(bool changedOnly)
  204. {
  205. if (!changedOnly || generalChanged)
  206. LoadGeneralSettings();
  207. //if (!changedOnly || outputChanged)
  208. // LoadOutputSettings();
  209. //if (!changedOnly || audioChanged)
  210. // LoadOutputSettings();
  211. if (!changedOnly || videoChanged)
  212. LoadVideoSettings();
  213. }
  214. void OBSBasicSettings::SaveGeneralSettings()
  215. {
  216. int languageIndex = ui->language->currentIndex();
  217. QVariant langData = ui->language->itemData(languageIndex);
  218. string language = langData.toString().toStdString();
  219. config_set_string(GetGlobalConfig(), "General", "Language",
  220. language.c_str());
  221. }
  222. void OBSBasicSettings::SaveVideoSettings()
  223. {
  224. QString renderer = ui->renderer->currentText();
  225. QString baseResolution = ui->baseResolution->currentText();
  226. QString outputResolution = ui->outputResolution->currentText();
  227. int fpsType = ui->fpsType->currentIndex();
  228. QString fpsCommon = ui->fpsCommon->currentText();
  229. int fpsInteger = ui->fpsInteger->value();
  230. int fpsNumerator = ui->fpsNumerator->value();
  231. int fpsDenominator = ui->fpsDenominator->value();
  232. uint32_t cx, cy;
  233. /* ------------------- */
  234. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  235. QT_TO_UTF8(renderer));
  236. if (ConvertResText(QT_TO_UTF8(baseResolution), cx, cy)) {
  237. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  238. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  239. }
  240. if (ConvertResText(QT_TO_UTF8(outputResolution), cx, cy)) {
  241. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  242. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  243. }
  244. config_set_uint(GetGlobalConfig(), "Video", "FPSType", fpsType);
  245. config_set_string(GetGlobalConfig(), "Video", "FPSCommon",
  246. QT_TO_UTF8(fpsCommon));
  247. config_set_uint(GetGlobalConfig(), "Video", "FPSInt", fpsInteger);
  248. config_set_uint(GetGlobalConfig(), "Video", "FPSNum", fpsNumerator);
  249. config_set_uint(GetGlobalConfig(), "Video", "FPSDen", fpsDenominator);
  250. }
  251. void OBSBasicSettings::SaveSettings()
  252. {
  253. if (generalChanged)
  254. SaveGeneralSettings();
  255. //if (outputChanged)
  256. // SaveOutputSettings();
  257. //if (audioChanged)
  258. // SaveAudioSettings();
  259. if (videoChanged)
  260. SaveVideoSettings();
  261. config_save(GetGlobalConfig());
  262. }
  263. bool OBSBasicSettings::QueryChanges()
  264. {
  265. QMessageBox::StandardButton button;
  266. button = QMessageBox::question(this,
  267. QTStr("Settings.ConfirmTitle"),
  268. QTStr("Settings.Confirm"),
  269. QMessageBox::Yes | QMessageBox::No |
  270. QMessageBox::Cancel);
  271. if (button == QMessageBox::Cancel)
  272. return false;
  273. else if (button == QMessageBox::Yes)
  274. SaveSettings();
  275. else
  276. LoadSettings(true);
  277. ClearChanged();
  278. return true;
  279. }
  280. void OBSBasicSettings::closeEvent(QCloseEvent *event)
  281. {
  282. if (Changed() && !QueryChanges())
  283. event->ignore();
  284. }
  285. void OBSBasicSettings::on_listWidget_itemSelectionChanged()
  286. {
  287. int row = ui->listWidget->currentRow();
  288. if (loading || row == pageIndex)
  289. return;
  290. if (Changed() && !QueryChanges()) {
  291. ui->listWidget->setCurrentRow(pageIndex);
  292. return;
  293. }
  294. pageIndex = row;
  295. }
  296. void OBSBasicSettings::on_buttonBox_clicked(QAbstractButton *button)
  297. {
  298. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  299. if (val == QDialogButtonBox::ApplyRole ||
  300. val == QDialogButtonBox::AcceptRole) {
  301. SaveSettings();
  302. ClearChanged();
  303. }
  304. if (val == QDialogButtonBox::AcceptRole ||
  305. val == QDialogButtonBox::RejectRole) {
  306. ClearChanged();
  307. close();
  308. }
  309. }
  310. static bool ValidResolutions(Ui::OBSBasicSettings *ui)
  311. {
  312. QString baseRes = ui->baseResolution->lineEdit()->text();
  313. QString outputRes = ui->outputResolution->lineEdit()->text();
  314. uint32_t cx, cy;
  315. if (!ConvertResText(QT_TO_UTF8(baseRes), cx, cy) ||
  316. !ConvertResText(QT_TO_UTF8(outputRes), cx, cy)) {
  317. ui->errorText->setText(
  318. QTStr("Settings.Video.InvalidResolution"));
  319. return false;
  320. }
  321. ui->errorText->setText("");
  322. return true;
  323. }
  324. void OBSBasicSettings::on_language_currentIndexChanged(int index)
  325. {
  326. if (!loading)
  327. generalChanged = true;
  328. UNUSED_PARAMETER(index);
  329. }
  330. void OBSBasicSettings::on_renderer_currentIndexChanged(int index)
  331. {
  332. if (!loading) {
  333. videoChanged = true;
  334. ui->errorText->setText(
  335. QTStr("Settings.ProgramRestart"));
  336. }
  337. UNUSED_PARAMETER(index);
  338. }
  339. void OBSBasicSettings::on_fpsType_currentIndexChanged(int index)
  340. {
  341. if (!loading)
  342. videoChanged = true;
  343. UNUSED_PARAMETER(index);
  344. }
  345. void OBSBasicSettings::on_baseResolution_editTextChanged(const QString &text)
  346. {
  347. if (!loading && ValidResolutions(ui.get()))
  348. videoChanged = true;
  349. UNUSED_PARAMETER(text);
  350. }
  351. void OBSBasicSettings::on_outputResolution_editTextChanged(const QString &text)
  352. {
  353. if (!loading && ValidResolutions(ui.get()))
  354. videoChanged = true;
  355. UNUSED_PARAMETER(text);
  356. }