window-basic-settings.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. for (size_t idx = 0; idx < numVals; idx++) {
  142. uint32_t downscaleCX = uint32_t(double(cx) / vals[idx]);
  143. uint32_t downscaleCY = uint32_t(double(cy) / vals[idx]);
  144. string res = ResString(downscaleCX, downscaleCY);
  145. ui->outputResolution->addItem(res.c_str());
  146. }
  147. ui->outputResolution->lineEdit()->setText(ResString(cx, cy).c_str());
  148. }
  149. void OBSBasicSettings::LoadResolutionLists()
  150. {
  151. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  152. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  153. vector<MonitorInfo> monitors;
  154. ui->baseResolution->clear();
  155. ui->outputResolution->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::LoadSettings(bool changedOnly)
  211. {
  212. if (!changedOnly || generalChanged)
  213. LoadGeneralSettings();
  214. //if (!changedOnly || outputChanged)
  215. // LoadOutputSettings();
  216. //if (!changedOnly || audioChanged)
  217. // LoadOutputSettings();
  218. if (!changedOnly || videoChanged)
  219. LoadVideoSettings();
  220. }
  221. void OBSBasicSettings::SaveGeneralSettings()
  222. {
  223. int languageIndex = ui->language->currentIndex();
  224. QVariant langData = ui->language->itemData(languageIndex);
  225. string language = langData.toString().toStdString();
  226. config_set_string(GetGlobalConfig(), "General", "Language",
  227. language.c_str());
  228. }
  229. void OBSBasicSettings::SaveVideoSettings()
  230. {
  231. QString renderer = ui->renderer->currentText();
  232. QString baseResolution = ui->baseResolution->currentText();
  233. QString outputResolution = ui->outputResolution->currentText();
  234. int fpsType = ui->fpsType->currentIndex();
  235. QString fpsCommon = ui->fpsCommon->currentText();
  236. int fpsInteger = ui->fpsInteger->value();
  237. int fpsNumerator = ui->fpsNumerator->value();
  238. int fpsDenominator = ui->fpsDenominator->value();
  239. uint32_t cx, cy;
  240. /* ------------------- */
  241. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  242. QT_TO_UTF8(renderer));
  243. if (ConvertResText(QT_TO_UTF8(baseResolution), cx, cy)) {
  244. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  245. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  246. }
  247. if (ConvertResText(QT_TO_UTF8(outputResolution), cx, cy)) {
  248. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  249. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  250. }
  251. config_set_uint(GetGlobalConfig(), "Video", "FPSType", fpsType);
  252. config_set_string(GetGlobalConfig(), "Video", "FPSCommon",
  253. QT_TO_UTF8(fpsCommon));
  254. config_set_uint(GetGlobalConfig(), "Video", "FPSInt", fpsInteger);
  255. config_set_uint(GetGlobalConfig(), "Video", "FPSNum", fpsNumerator);
  256. config_set_uint(GetGlobalConfig(), "Video", "FPSDen", fpsDenominator);
  257. OBSBasic *window = qobject_cast<OBSBasic*>(parent());
  258. if (window)
  259. window->ResetVideo();
  260. }
  261. void OBSBasicSettings::SaveSettings()
  262. {
  263. if (generalChanged)
  264. SaveGeneralSettings();
  265. //if (outputChanged)
  266. // SaveOutputSettings();
  267. //if (audioChanged)
  268. // SaveAudioSettings();
  269. if (videoChanged)
  270. SaveVideoSettings();
  271. config_save(GetGlobalConfig());
  272. }
  273. bool OBSBasicSettings::QueryChanges()
  274. {
  275. QMessageBox::StandardButton button;
  276. button = QMessageBox::question(this,
  277. QTStr("Settings.ConfirmTitle"),
  278. QTStr("Settings.Confirm"),
  279. QMessageBox::Yes | QMessageBox::No |
  280. QMessageBox::Cancel);
  281. if (button == QMessageBox::Cancel)
  282. return false;
  283. else if (button == QMessageBox::Yes)
  284. SaveSettings();
  285. else
  286. LoadSettings(true);
  287. ClearChanged();
  288. return true;
  289. }
  290. void OBSBasicSettings::closeEvent(QCloseEvent *event)
  291. {
  292. if (Changed() && !QueryChanges())
  293. event->ignore();
  294. }
  295. void OBSBasicSettings::on_listWidget_itemSelectionChanged()
  296. {
  297. int row = ui->listWidget->currentRow();
  298. if (loading || row == pageIndex)
  299. return;
  300. if (Changed() && !QueryChanges()) {
  301. ui->listWidget->setCurrentRow(pageIndex);
  302. return;
  303. }
  304. pageIndex = row;
  305. }
  306. void OBSBasicSettings::on_buttonBox_clicked(QAbstractButton *button)
  307. {
  308. QDialogButtonBox::ButtonRole val = ui->buttonBox->buttonRole(button);
  309. if (val == QDialogButtonBox::ApplyRole ||
  310. val == QDialogButtonBox::AcceptRole) {
  311. SaveSettings();
  312. ClearChanged();
  313. }
  314. if (val == QDialogButtonBox::AcceptRole ||
  315. val == QDialogButtonBox::RejectRole) {
  316. ClearChanged();
  317. close();
  318. }
  319. }
  320. static bool ValidResolutions(Ui::OBSBasicSettings *ui)
  321. {
  322. QString baseRes = ui->baseResolution->lineEdit()->text();
  323. QString outputRes = ui->outputResolution->lineEdit()->text();
  324. uint32_t cx, cy;
  325. if (!ConvertResText(QT_TO_UTF8(baseRes), cx, cy) ||
  326. !ConvertResText(QT_TO_UTF8(outputRes), cx, cy)) {
  327. ui->videoMsg->setText(
  328. QTStr("Settings.Video.InvalidResolution"));
  329. return false;
  330. }
  331. ui->videoMsg->setText("");
  332. return true;
  333. }
  334. void OBSBasicSettings::on_language_currentIndexChanged(int index)
  335. {
  336. if (!loading)
  337. generalChanged = true;
  338. UNUSED_PARAMETER(index);
  339. }
  340. void OBSBasicSettings::on_renderer_currentIndexChanged(int index)
  341. {
  342. if (!loading) {
  343. videoChanged = true;
  344. ui->videoMsg->setText(QTStr("Settings.ProgramRestart"));
  345. }
  346. UNUSED_PARAMETER(index);
  347. }
  348. void OBSBasicSettings::on_fpsType_currentIndexChanged(int index)
  349. {
  350. if (!loading)
  351. videoChanged = true;
  352. UNUSED_PARAMETER(index);
  353. }
  354. void OBSBasicSettings::on_baseResolution_editTextChanged(const QString &text)
  355. {
  356. if (!loading && ValidResolutions(ui.get()))
  357. videoChanged = true;
  358. UNUSED_PARAMETER(text);
  359. }
  360. void OBSBasicSettings::on_outputResolution_editTextChanged(const QString &text)
  361. {
  362. if (!loading && ValidResolutions(ui.get()))
  363. videoChanged = true;
  364. UNUSED_PARAMETER(text);
  365. }