window-basic-settings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. int val = config_get_int(GetGlobalConfig(), "Video", "FPSInt");
  175. ui->fpsInteger->setValue(val);
  176. }
  177. static inline void LoadFPSFraction(Ui::OBSBasicSettings *ui)
  178. {
  179. int num = config_get_int(GetGlobalConfig(), "Video", "FPSNum");
  180. int den = config_get_int(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. int fpsType = config_get_int(GetGlobalConfig(), "Video", "FPSType");
  190. if (fpsType < 0 || fpsType > 2) fpsType = 0;
  191. ui->fpsType->setCurrentIndex(fpsType);
  192. ui->fpsTypes->setCurrentIndex(fpsType);
  193. }
  194. void OBSBasicSettings::LoadVideoSettings()
  195. {
  196. loading = true;
  197. LoadRendererList();
  198. LoadResolutionLists();
  199. LoadFPSData();
  200. loading = false;
  201. }
  202. void OBSBasicSettings::LoadSettings(bool changedOnly)
  203. {
  204. if (!changedOnly || generalChanged)
  205. LoadGeneralSettings();
  206. //if (!changedOnly || outputChanged)
  207. // LoadOutputSettings();
  208. //if (!changedOnly || audioChanged)
  209. // LoadOutputSettings();
  210. if (!changedOnly || videoChanged)
  211. LoadVideoSettings();
  212. }
  213. void OBSBasicSettings::SaveGeneralSettings()
  214. {
  215. int languageIndex = ui->language->currentIndex();
  216. QVariant langData = ui->language->itemData(languageIndex);
  217. string language = langData.toString().toStdString();
  218. config_set_string(GetGlobalConfig(), "General", "Language",
  219. language.c_str());
  220. }
  221. void OBSBasicSettings::SaveVideoSettings()
  222. {
  223. QString renderer = ui->renderer->currentText();
  224. QString baseResolution = ui->baseResolution->currentText();
  225. QString outputResolution = ui->outputResolution->currentText();
  226. int fpsType = ui->fpsType->currentIndex();
  227. QString fpsCommon = ui->fpsCommon->currentText();
  228. int fpsInteger = ui->fpsInteger->value();
  229. int fpsNumerator = ui->fpsNumerator->value();
  230. int fpsDenominator = ui->fpsDenominator->value();
  231. uint32_t cx, cy;
  232. /* ------------------- */
  233. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  234. QT_TO_UTF8(renderer));
  235. if (ConvertResText(QT_TO_UTF8(baseResolution), cx, cy)) {
  236. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  237. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  238. }
  239. if (ConvertResText(QT_TO_UTF8(outputResolution), cx, cy)) {
  240. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  241. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  242. }
  243. config_set_uint(GetGlobalConfig(), "Video", "FPSType", fpsType);
  244. config_set_string(GetGlobalConfig(), "Video", "FPSCommon",
  245. QT_TO_UTF8(fpsCommon));
  246. config_set_uint(GetGlobalConfig(), "Video", "FPSInt", fpsInteger);
  247. config_set_uint(GetGlobalConfig(), "Video", "FPSNum", fpsNumerator);
  248. config_set_uint(GetGlobalConfig(), "Video", "FPSDen", fpsDenominator);
  249. }
  250. void OBSBasicSettings::SaveSettings()
  251. {
  252. if (generalChanged)
  253. SaveGeneralSettings();
  254. //if (outputChanged)
  255. // SaveOutputSettings();
  256. //if (audioChanged)
  257. // SaveAudioSettings();
  258. if (videoChanged)
  259. SaveVideoSettings();
  260. config_save(GetGlobalConfig());
  261. }
  262. bool OBSBasicSettings::QueryChanges()
  263. {
  264. QMessageBox::StandardButton button;
  265. button = QMessageBox::question(this,
  266. QTStr("Settings.ConfirmTitle"),
  267. QTStr("Settings.Confirm"),
  268. QMessageBox::Yes | QMessageBox::No |
  269. QMessageBox::Cancel);
  270. if (button == QMessageBox::Cancel)
  271. return false;
  272. else if (button == QMessageBox::Yes)
  273. SaveSettings();
  274. else
  275. LoadSettings(true);
  276. ClearChanged();
  277. return true;
  278. }
  279. void OBSBasicSettings::closeEvent(QCloseEvent *event)
  280. {
  281. if (Changed() && !QueryChanges())
  282. event->ignore();
  283. }
  284. void OBSBasicSettings::on_listWidget_itemSelectionChanged()
  285. {
  286. int row = ui->listWidget->currentRow();
  287. if (loading || row == pageIndex)
  288. return;
  289. if (Changed() && !QueryChanges()) {
  290. ui->listWidget->setCurrentRow(pageIndex);
  291. return;
  292. }
  293. pageIndex = row;
  294. }
  295. void OBSBasicSettings::on_buttonBox_clicked(QAbstractButton *button)
  296. {
  297. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  298. if (val == QDialogButtonBox::ApplyRole ||
  299. val == QDialogButtonBox::AcceptRole) {
  300. SaveSettings();
  301. ClearChanged();
  302. }
  303. if (val == QDialogButtonBox::AcceptRole ||
  304. val == QDialogButtonBox::RejectRole) {
  305. ClearChanged();
  306. close();
  307. }
  308. }
  309. static bool ValidResolutions(Ui::OBSBasicSettings *ui)
  310. {
  311. QString baseRes = ui->baseResolution->lineEdit()->text();
  312. QString outputRes = ui->outputResolution->lineEdit()->text();
  313. uint32_t cx, cy;
  314. if (!ConvertResText(QT_TO_UTF8(baseRes), cx, cy) ||
  315. !ConvertResText(QT_TO_UTF8(outputRes), cx, cy)) {
  316. ui->errorText->setText(
  317. QTStr("Settings.Video.InvalidResolution"));
  318. return false;
  319. }
  320. ui->errorText->setText("");
  321. return true;
  322. }
  323. void OBSBasicSettings::on_language_currentIndexChanged(int index)
  324. {
  325. if (!loading)
  326. generalChanged = true;
  327. }
  328. void OBSBasicSettings::on_renderer_currentIndexChanged(int index)
  329. {
  330. if (!loading) {
  331. videoChanged = true;
  332. ui->errorText->setText(
  333. QTStr("Settings.ProgramRestart"));
  334. }
  335. }
  336. void OBSBasicSettings::on_fpsType_currentIndexChanged(int index)
  337. {
  338. if (!loading)
  339. videoChanged = true;
  340. }
  341. void OBSBasicSettings::on_baseResolution_editTextChanged(const QString &text)
  342. {
  343. if (!loading && ValidResolutions(ui.get()))
  344. videoChanged = true;
  345. }
  346. void OBSBasicSettings::on_outputResolution_editTextChanged(const QString &text)
  347. {
  348. if (!loading && ValidResolutions(ui.get()))
  349. videoChanged = true;
  350. }