settings-basic-video.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/lexer.h>
  15. #include "obs-app.hpp"
  16. #include "settings-basic.hpp"
  17. #include "window-basic-settings.hpp"
  18. #include "wx-wrappers.hpp"
  19. #include "platform.hpp"
  20. #include <sstream>
  21. #include <unordered_set>
  22. using namespace std;
  23. class BasicVideoData : public BasicSettingsData {
  24. ConnectorList connections;
  25. int AddRes(uint32_t cx, uint32_t cy);
  26. void LoadOther();
  27. void LoadResolutionData();
  28. void LoadFPSData();
  29. void LoadFPSCommon();
  30. void LoadFPSInteger();
  31. void LoadFPSFraction();
  32. void LoadFPSNanoseconds();
  33. void ResetScaleList(uint32_t cx, uint32_t cy);
  34. void RendererChanged(wxCommandEvent &event);
  35. void BaseResChanged(wxCommandEvent &event);
  36. void OutputResChanged(wxCommandEvent &event);
  37. void FPSChanged(wxCommandEvent &event);
  38. void SaveOther();
  39. void SaveFPSData();
  40. void SaveFPSCommon();
  41. void SaveFPSInteger();
  42. void SaveFPSFraction();
  43. void SaveFPSNanoseconds();
  44. virtual void SetChanged()
  45. {
  46. BasicSettingsData::SetChanged();
  47. window->videoChangedText->Show();
  48. }
  49. public:
  50. BasicVideoData(OBSBasicSettings *window);
  51. virtual void Apply();
  52. };
  53. struct BaseLexer {
  54. lexer lex;
  55. public:
  56. inline BaseLexer() {lexer_init(&lex);}
  57. inline ~BaseLexer() {lexer_free(&lex);}
  58. operator lexer*() {return &lex;}
  59. };
  60. /* parses "[width]x[height]", string, i.e. 1024x768 */
  61. static bool ConvertTextRes(wxComboBox *combo, uint32_t &cx, uint32_t &cy)
  62. {
  63. string str = combo->GetValue().ToStdString();
  64. BaseLexer lex;
  65. lexer_start(lex, str.c_str());
  66. base_token token;
  67. /* parse width */
  68. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  69. return false;
  70. if (token.type != BASETOKEN_DIGIT)
  71. return false;
  72. cx = std::stoul(token.text.array);
  73. /* parse 'x' */
  74. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  75. return false;
  76. if (strref_cmpi(&token.text, "x") != 0)
  77. return false;
  78. /* parse height */
  79. if (!lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  80. return false;
  81. if (token.type != BASETOKEN_DIGIT)
  82. return false;
  83. cy = std::stoul(token.text.array);
  84. /* shouldn't be any more tokens after this */
  85. if (lexer_getbasetoken(lex, &token, IGNORE_WHITESPACE))
  86. return false;
  87. return true;
  88. }
  89. int BasicVideoData::AddRes(uint32_t cx, uint32_t cy)
  90. {
  91. stringstream res;
  92. res << cx << "x" << cy;
  93. return window->baseResList->Append(res.str());
  94. }
  95. void BasicVideoData::LoadOther()
  96. {
  97. const char *renderer = config_get_string(GetGlobalConfig(), "Video",
  98. "Renderer");
  99. window->rendererList->Clear();
  100. window->rendererList->Append("OpenGL");
  101. #ifdef _WIN32
  102. window->rendererList->Append("Direct3D 11");
  103. #endif
  104. int sel = window->rendererList->FindString(renderer);
  105. if (sel == wxNOT_FOUND)
  106. sel = 0;
  107. window->rendererList->SetSelection(sel);
  108. }
  109. static uint64_t append_uint32_t(uint64_t first, uint64_t second)
  110. {
  111. return (first << 32) | second;
  112. }
  113. void BasicVideoData::LoadResolutionData()
  114. {
  115. window->baseResList->Clear();
  116. uint32_t cx = config_get_uint(GetGlobalConfig(), "Video", "BaseCX");
  117. uint32_t cy = config_get_uint(GetGlobalConfig(), "Video", "BaseCY");
  118. vector<MonitorInfo> monitors;
  119. GetMonitors(monitors);
  120. unordered_set<uint64_t> resolutions;
  121. for (size_t i = 0; i < monitors.size(); i++) {
  122. uint64_t res = append_uint32_t(monitors[i].cx, monitors[i].cy);
  123. if(resolutions.emplace(res).second)
  124. AddRes(monitors[i].cx, monitors[i].cy);
  125. }
  126. stringstream res;
  127. res << cx << "x" << cy;
  128. window->baseResList->SetValue(res.str());
  129. ResetScaleList(cx, cy);
  130. cx = config_get_uint(GetGlobalConfig(), "Video", "OutputCX");
  131. cy = config_get_uint(GetGlobalConfig(), "Video", "OutputCY");
  132. res.str(string());
  133. res.clear();
  134. res << cx << "x" << cy;
  135. window->outputResList->SetValue(res.str());
  136. }
  137. void BasicVideoData::LoadFPSData()
  138. {
  139. const char *fpsType = config_get_string(GetGlobalConfig(), "Video",
  140. "FPSType");
  141. LoadFPSCommon();
  142. LoadFPSInteger();
  143. LoadFPSFraction();
  144. LoadFPSNanoseconds();
  145. if (!fpsType)
  146. return;
  147. else if (strcmp(fpsType, "Common") == 0)
  148. window->fpsTypeList->SetSelection(0);
  149. else if (strcmp(fpsType, "Integer") == 0)
  150. window->fpsTypeList->SetSelection(1);
  151. else if (strcmp(fpsType, "Fraction") == 0)
  152. window->fpsTypeList->SetSelection(2);
  153. else if (strcmp(fpsType, "Nanoseconds") == 0)
  154. window->fpsTypeList->SetSelection(3);
  155. }
  156. void BasicVideoData::LoadFPSCommon()
  157. {
  158. const char *val = config_get_string(GetGlobalConfig(), "Video",
  159. "FPSCommon");
  160. int sel = window->fpsCommonList->FindString(val);
  161. if (sel == wxNOT_FOUND)
  162. sel = window->fpsCommonList->FindString("30");
  163. window->fpsCommonList->SetSelection(sel);
  164. }
  165. void BasicVideoData::LoadFPSInteger()
  166. {
  167. window->fpsTypeList->SetSelection(1);
  168. int val = config_get_int(GetGlobalConfig(), "Video", "FPSInt");
  169. window->fpsIntegerScroller->SetValue(val);
  170. }
  171. void BasicVideoData::LoadFPSFraction()
  172. {
  173. window->fpsTypeList->SetSelection(2);
  174. int num = config_get_int(GetGlobalConfig(), "Video", "FPSNum");
  175. int den = config_get_int(GetGlobalConfig(), "Video", "FPSDen");
  176. window->fpsNumeratorScroller->SetValue(num);
  177. window->fpsDenominatorScroller->SetValue(den);
  178. }
  179. void BasicVideoData::LoadFPSNanoseconds()
  180. {
  181. window->fpsTypeList->SetSelection(3);
  182. int val = config_get_int(GetGlobalConfig(), "Video", "FPSNS");
  183. window->fpsNanosecondsScroller->SetValue(val);
  184. }
  185. /* some nice default output resolution vals */
  186. static const double vals[] =
  187. {
  188. 1.0,
  189. 1.25,
  190. (1.0/0.75),
  191. 1.5,
  192. (1.0/0.6),
  193. 1.75,
  194. 2.0,
  195. 2.25,
  196. 2.5,
  197. 2.75,
  198. 3.0
  199. };
  200. static const size_t numVals = sizeof(vals)/sizeof(double);
  201. void BasicVideoData::ResetScaleList(uint32_t cx, uint32_t cy)
  202. {
  203. window->outputResList->Clear();
  204. for (size_t i = 0; i < numVals; i++) {
  205. stringstream res;
  206. res << uint32_t(double(cx) / vals[i]);
  207. res << "x";
  208. res << uint32_t(double(cy) / vals[i]);
  209. window->outputResList->Append(res.str());
  210. }
  211. if (numVals) {
  212. stringstream str;
  213. str << cx << "x" << cy;
  214. window->outputResList->SetValue(str.str());
  215. }
  216. }
  217. #define ADD_CONNECT(control, event, func) \
  218. connections.Add(window->control, event, \
  219. wxCommandEventHandler(BasicVideoData::func), \
  220. NULL, this)
  221. BasicVideoData::BasicVideoData(OBSBasicSettings *window)
  222. : BasicSettingsData(window)
  223. {
  224. LoadResolutionData();
  225. LoadFPSData();
  226. LoadOther();
  227. /* load connectors after loading data to prevent them from triggering */
  228. ADD_CONNECT(baseResList, wxEVT_TEXT, BaseResChanged);
  229. ADD_CONNECT(outputResList, wxEVT_TEXT, OutputResChanged);
  230. ADD_CONNECT(rendererList, wxEVT_COMBOBOX, RendererChanged);
  231. ADD_CONNECT(fpsCommonList, wxEVT_COMBOBOX, FPSChanged);
  232. ADD_CONNECT(fpsIntegerScroller, wxEVT_SPINCTRL, FPSChanged);
  233. ADD_CONNECT(fpsNumeratorScroller, wxEVT_SPINCTRL, FPSChanged);
  234. ADD_CONNECT(fpsDenominatorScroller, wxEVT_SPINCTRL, FPSChanged);
  235. ADD_CONNECT(fpsNanosecondsScroller, wxEVT_SPINCTRL, FPSChanged);
  236. ADD_CONNECT(fpsIntegerScroller, wxEVT_TEXT, FPSChanged);
  237. ADD_CONNECT(fpsNumeratorScroller, wxEVT_TEXT, FPSChanged);
  238. ADD_CONNECT(fpsDenominatorScroller, wxEVT_TEXT, FPSChanged);
  239. ADD_CONNECT(fpsNanosecondsScroller, wxEVT_TEXT, FPSChanged);
  240. ADD_CONNECT(fpsTypeList, wxEVT_CHOICEBOOK_PAGE_CHANGED,
  241. FPSChanged);
  242. window->videoChangedText->Hide();
  243. }
  244. void BasicVideoData::RendererChanged(wxCommandEvent &event)
  245. {
  246. SetChanged();
  247. window->videoChangedText->SetLabel(WXStr("Settings.ProgramRestart"));
  248. window->videoChangedText->Show();
  249. }
  250. void BasicVideoData::FPSChanged(wxCommandEvent &event)
  251. {
  252. SetChanged();
  253. }
  254. void BasicVideoData::BaseResChanged(wxCommandEvent &event)
  255. {
  256. uint32_t cx, cy;
  257. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  258. window->videoChangedText->SetLabel(
  259. WXStr("Settings.Video.InvalidResolution"));
  260. window->videoChangedText->Show();
  261. return;
  262. }
  263. SetChanged();
  264. ResetScaleList(cx, cy);
  265. }
  266. void BasicVideoData::OutputResChanged(wxCommandEvent &event)
  267. {
  268. uint32_t cx, cy;
  269. if (!ConvertTextRes(window->outputResList, cx, cy)) {
  270. window->videoChangedText->SetLabel(
  271. WXStr("Settings.Video.InvalidResolution"));
  272. window->videoChangedText->Show();
  273. return;
  274. }
  275. SetChanged();
  276. }
  277. void BasicVideoData::SaveOther()
  278. {
  279. int sel = window->rendererList->GetSelection();
  280. if (sel == wxNOT_FOUND)
  281. return;
  282. wxString renderer = window->rendererList->GetString(sel);
  283. config_set_string(GetGlobalConfig(), "Video", "Renderer",
  284. renderer.c_str());
  285. }
  286. void BasicVideoData::SaveFPSData()
  287. {
  288. int id = window->fpsTypeList->GetCurrentPage()->GetId();
  289. const char *type;
  290. switch (id) {
  291. case ID_FPSPANEL_COMMON: type = "Common"; break;
  292. case ID_FPSPANEL_INTEGER: type = "Integer"; break;
  293. case ID_FPSPANEL_FRACTION: type = "Fraction"; break;
  294. case ID_FPSPANEL_NANOSECONDS: type = "Nanoseconds"; break;
  295. }
  296. config_set_string(GetGlobalConfig(), "Video", "FPSType", type);
  297. SaveFPSCommon();
  298. SaveFPSInteger();
  299. SaveFPSFraction();
  300. SaveFPSNanoseconds();
  301. }
  302. void BasicVideoData::SaveFPSCommon()
  303. {
  304. int sel = window->fpsCommonList->GetSelection();
  305. wxString str = window->fpsCommonList->GetString(sel);
  306. config_set_string(GetGlobalConfig(), "Video", "FPSCommon", str.c_str());
  307. }
  308. void BasicVideoData::SaveFPSInteger()
  309. {
  310. int val = window->fpsIntegerScroller->GetValue();
  311. config_set_int(GetGlobalConfig(), "Video", "FPSInt", val);
  312. }
  313. void BasicVideoData::SaveFPSFraction()
  314. {
  315. int num = window->fpsNumeratorScroller->GetValue();
  316. int den = window->fpsDenominatorScroller->GetValue();
  317. config_set_int(GetGlobalConfig(), "Video", "FPSNum", num);
  318. config_set_int(GetGlobalConfig(), "Video", "FPSDen", den);
  319. }
  320. void BasicVideoData::SaveFPSNanoseconds()
  321. {
  322. int val = window->fpsNanosecondsScroller->GetValue();
  323. config_set_int(GetGlobalConfig(), "Video", "FPSNS", val);
  324. }
  325. void BasicVideoData::Apply()
  326. {
  327. uint32_t cx, cy;
  328. if (!ConvertTextRes(window->baseResList, cx, cy)) {
  329. config_set_uint(GetGlobalConfig(), "Video", "BaseCX", cx);
  330. config_set_uint(GetGlobalConfig(), "Video", "BaseCY", cy);
  331. }
  332. if (ConvertTextRes(window->outputResList, cx, cy)) {
  333. config_set_uint(GetGlobalConfig(), "Video", "OutputCX", cx);
  334. config_set_uint(GetGlobalConfig(), "Video", "OutputCY", cy);
  335. }
  336. SaveOther();
  337. SaveFPSData();
  338. config_save(GetGlobalConfig());
  339. /* TODO: If resolutiosn/fps were chaned, reset video */
  340. SetSaved();
  341. }
  342. BasicSettingsData *CreateBasicVideoSettings(OBSBasicSettings *window)
  343. {
  344. return new BasicVideoData(window);
  345. }