You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

357 lines
11KB

  1. #include <jansson.h>
  2. #include <settings.hpp>
  3. #include <window.hpp>
  4. #include <plugin.hpp>
  5. #include <app/Scene.hpp>
  6. #include <app/ModuleBrowser.hpp>
  7. #include <engine/Engine.hpp>
  8. #include <context.hpp>
  9. #include <patch.hpp>
  10. namespace rack {
  11. namespace settings {
  12. bool devMode = false;
  13. bool headless = false;
  14. std::string token;
  15. math::Vec windowSize;
  16. math::Vec windowPos;
  17. float zoom = 0.25;
  18. bool invertZoom = false;
  19. float cableOpacity = 0.5;
  20. float cableTension = 0.5;
  21. bool allowCursorLock = true;
  22. KnobMode knobMode = KNOB_MODE_LINEAR;
  23. float knobLinearSensitivity = 0.001f;
  24. float sampleRate = 44100.0;
  25. int threadCount = 1;
  26. bool tooltips = true;
  27. bool cpuMeter = false;
  28. bool lockModules = false;
  29. #if defined ARCH_MAC
  30. // Most Mac GPUs can't handle rendering the screen every frame, so use ~30 Hz by default.
  31. int frameSwapInterval = 2;
  32. #else
  33. int frameSwapInterval = 1;
  34. #endif
  35. float autosaveInterval = 15.0;
  36. bool skipLoadOnLaunch = false;
  37. std::string patchPath;
  38. std::list<std::string> recentPatchPaths;
  39. std::vector<NVGcolor> cableColors = {
  40. color::fromHexString("#fc2d5aff"), // red
  41. color::fromHexString("#f9b130ff"), // orange
  42. // color::fromHexString("#f7da31ff"), // yellow
  43. color::fromHexString("#67c12dff"), // green
  44. color::fromHexString("#0f8df4ff"), // blue
  45. color::fromHexString("#8c1889ff"), // purple
  46. };
  47. bool autoCheckUpdates = true;
  48. bool showTipsOnLaunch = true;
  49. int tipIndex = -1;
  50. ModuleBrowserSort moduleBrowserSort = MODULE_BROWSER_SORT_UPDATED;
  51. float moduleBrowserZoom = -2.f;
  52. std::map<std::string, std::set<std::string>> moduleWhitelist = {};
  53. std::map<std::string, std::map<std::string, ModuleUsage>> moduleUsages = {};
  54. json_t* toJson() {
  55. json_t* rootJ = json_object();
  56. json_object_set_new(rootJ, "token", json_string(token.c_str()));
  57. json_t* windowSizeJ = json_pack("[f, f]", windowSize.x, windowSize.y);
  58. json_object_set_new(rootJ, "windowSize", windowSizeJ);
  59. json_t* windowPosJ = json_pack("[f, f]", windowPos.x, windowPos.y);
  60. json_object_set_new(rootJ, "windowPos", windowPosJ);
  61. json_object_set_new(rootJ, "zoom", json_real(zoom));
  62. json_object_set_new(rootJ, "invertZoom", json_boolean(invertZoom));
  63. json_object_set_new(rootJ, "cableOpacity", json_real(cableOpacity));
  64. json_object_set_new(rootJ, "cableTension", json_real(cableTension));
  65. json_object_set_new(rootJ, "allowCursorLock", json_boolean(allowCursorLock));
  66. json_object_set_new(rootJ, "knobMode", json_integer((int) knobMode));
  67. json_object_set_new(rootJ, "knobLinearSensitivity", json_real(knobLinearSensitivity));
  68. json_object_set_new(rootJ, "sampleRate", json_real(sampleRate));
  69. json_object_set_new(rootJ, "threadCount", json_integer(threadCount));
  70. json_object_set_new(rootJ, "tooltips", json_boolean(tooltips));
  71. json_object_set_new(rootJ, "cpuMeter", json_boolean(cpuMeter));
  72. json_object_set_new(rootJ, "lockModules", json_boolean(lockModules));
  73. json_object_set_new(rootJ, "frameSwapInterval", json_integer(frameSwapInterval));
  74. json_object_set_new(rootJ, "autosaveInterval", json_real(autosaveInterval));
  75. if (skipLoadOnLaunch)
  76. json_object_set_new(rootJ, "skipLoadOnLaunch", json_boolean(true));
  77. json_object_set_new(rootJ, "patchPath", json_string(patchPath.c_str()));
  78. json_t* recentPatchPathsJ = json_array();
  79. for (const std::string& path : recentPatchPaths) {
  80. json_array_append_new(recentPatchPathsJ, json_string(path.c_str()));
  81. }
  82. json_object_set_new(rootJ, "recentPatchPaths", recentPatchPathsJ);
  83. json_t* cableColorsJ = json_array();
  84. for (NVGcolor cableColor : cableColors) {
  85. std::string colorStr = color::toHexString(cableColor);
  86. json_array_append_new(cableColorsJ, json_string(colorStr.c_str()));
  87. }
  88. json_object_set_new(rootJ, "cableColors", cableColorsJ);
  89. json_object_set_new(rootJ, "autoCheckUpdates", json_boolean(autoCheckUpdates));
  90. json_object_set_new(rootJ, "showTipsOnLaunch", json_boolean(showTipsOnLaunch));
  91. json_object_set_new(rootJ, "tipIndex", json_integer(tipIndex));
  92. json_object_set_new(rootJ, "moduleBrowserSort", json_integer((int) moduleBrowserSort));
  93. json_object_set_new(rootJ, "moduleBrowserZoom", json_real(moduleBrowserZoom));
  94. json_t* moduleWhitelistJ = json_object();
  95. for (const auto& pair : moduleWhitelist) {
  96. json_t* moduleSlugsJ = json_array();
  97. for (const std::string& moduleSlug : pair.second) {
  98. json_array_append_new(moduleSlugsJ, json_string(moduleSlug.c_str()));
  99. }
  100. json_object_set_new(moduleWhitelistJ, pair.first.c_str(), moduleSlugsJ);
  101. }
  102. json_object_set_new(rootJ, "moduleWhitelist", moduleWhitelistJ);
  103. json_t* moduleUsagesJ = json_object();
  104. for (const auto& pair : moduleUsages) {
  105. json_t* modulesJ = json_object();
  106. for (const auto& modulePair : pair.second) {
  107. const ModuleUsage& mu = modulePair.second;
  108. if (mu.count <= 0 || !std::isfinite(mu.lastTime))
  109. continue;
  110. json_t* moduleUsagesJ = json_object();
  111. {
  112. json_object_set_new(moduleUsagesJ, "count", json_integer(mu.count));
  113. json_object_set_new(moduleUsagesJ, "lastTime", json_real(mu.lastTime));
  114. }
  115. json_object_set_new(modulesJ, modulePair.first.c_str(), moduleUsagesJ);
  116. }
  117. json_object_set_new(moduleUsagesJ, pair.first.c_str(), modulesJ);
  118. }
  119. json_object_set_new(rootJ, "moduleUsages", moduleUsagesJ);
  120. return rootJ;
  121. }
  122. void fromJson(json_t* rootJ) {
  123. json_t* tokenJ = json_object_get(rootJ, "token");
  124. if (tokenJ)
  125. token = json_string_value(tokenJ);
  126. json_t* windowSizeJ = json_object_get(rootJ, "windowSize");
  127. if (windowSizeJ) {
  128. double x, y;
  129. json_unpack(windowSizeJ, "[F, F]", &x, &y);
  130. windowSize = math::Vec(x, y);
  131. }
  132. json_t* windowPosJ = json_object_get(rootJ, "windowPos");
  133. if (windowPosJ) {
  134. double x, y;
  135. json_unpack(windowPosJ, "[F, F]", &x, &y);
  136. windowPos = math::Vec(x, y);
  137. }
  138. json_t* zoomJ = json_object_get(rootJ, "zoom");
  139. if (zoomJ)
  140. zoom = json_number_value(zoomJ);
  141. json_t* invertZoomJ = json_object_get(rootJ, "invertZoom");
  142. if (invertZoomJ)
  143. invertZoom = json_boolean_value(invertZoomJ);
  144. json_t* cableOpacityJ = json_object_get(rootJ, "cableOpacity");
  145. if (cableOpacityJ)
  146. cableOpacity = json_number_value(cableOpacityJ);
  147. json_t* cableTensionJ = json_object_get(rootJ, "cableTension");
  148. if (cableTensionJ)
  149. cableTension = json_number_value(cableTensionJ);
  150. json_t* allowCursorLockJ = json_object_get(rootJ, "allowCursorLock");
  151. if (allowCursorLockJ)
  152. allowCursorLock = json_boolean_value(allowCursorLockJ);
  153. json_t* knobModeJ = json_object_get(rootJ, "knobMode");
  154. if (knobModeJ)
  155. knobMode = (KnobMode) json_integer_value(knobModeJ);
  156. json_t* knobLinearSensitivityJ = json_object_get(rootJ, "knobLinearSensitivity");
  157. if (knobLinearSensitivityJ)
  158. knobLinearSensitivity = json_number_value(knobLinearSensitivityJ);
  159. json_t* sampleRateJ = json_object_get(rootJ, "sampleRate");
  160. if (sampleRateJ)
  161. sampleRate = json_number_value(sampleRateJ);
  162. json_t* threadCountJ = json_object_get(rootJ, "threadCount");
  163. if (threadCountJ)
  164. threadCount = json_integer_value(threadCountJ);
  165. json_t* tooltipsJ = json_object_get(rootJ, "tooltips");
  166. if (tooltipsJ)
  167. tooltips = json_boolean_value(tooltipsJ);
  168. json_t* cpuMeterJ = json_object_get(rootJ, "cpuMeter");
  169. if (cpuMeterJ)
  170. cpuMeter = json_boolean_value(cpuMeterJ);
  171. json_t* lockModulesJ = json_object_get(rootJ, "lockModules");
  172. if (lockModulesJ)
  173. lockModules = json_boolean_value(lockModulesJ);
  174. json_t* frameSwapIntervalJ = json_object_get(rootJ, "frameSwapInterval");
  175. if (frameSwapIntervalJ)
  176. frameSwapInterval = json_integer_value(frameSwapIntervalJ);
  177. json_t* autosaveIntervalJ = json_object_get(rootJ, "autosaveInterval");
  178. if (autosaveIntervalJ)
  179. autosaveInterval = json_number_value(autosaveIntervalJ);
  180. json_t* skipLoadOnLaunchJ = json_object_get(rootJ, "skipLoadOnLaunch");
  181. if (skipLoadOnLaunchJ)
  182. skipLoadOnLaunch = json_boolean_value(skipLoadOnLaunchJ);
  183. json_t* patchPathJ = json_object_get(rootJ, "patchPath");
  184. if (patchPathJ)
  185. patchPath = json_string_value(patchPathJ);
  186. recentPatchPaths.clear();
  187. json_t* recentPatchPathsJ = json_object_get(rootJ, "recentPatchPaths");
  188. if (recentPatchPathsJ) {
  189. size_t i;
  190. json_t* pathJ;
  191. json_array_foreach(recentPatchPathsJ, i, pathJ) {
  192. std::string path = json_string_value(pathJ);
  193. recentPatchPaths.push_back(path);
  194. }
  195. }
  196. cableColors.clear();
  197. json_t* cableColorsJ = json_object_get(rootJ, "cableColors");
  198. if (cableColorsJ) {
  199. size_t i;
  200. json_t* cableColorJ;
  201. json_array_foreach(cableColorsJ, i, cableColorJ) {
  202. std::string colorStr = json_string_value(cableColorJ);
  203. cableColors.push_back(color::fromHexString(colorStr));
  204. }
  205. }
  206. json_t* autoCheckUpdatesJ = json_object_get(rootJ, "autoCheckUpdates");
  207. if (autoCheckUpdatesJ)
  208. autoCheckUpdates = json_boolean_value(autoCheckUpdatesJ);
  209. json_t* showTipsOnLaunchJ = json_object_get(rootJ, "showTipsOnLaunch");
  210. if (showTipsOnLaunchJ)
  211. showTipsOnLaunch = json_boolean_value(showTipsOnLaunchJ);
  212. json_t* tipIndexJ = json_object_get(rootJ, "tipIndex");
  213. if (tipIndexJ)
  214. tipIndex = json_integer_value(tipIndexJ);
  215. json_t* moduleBrowserSortJ = json_object_get(rootJ, "moduleBrowserSort");
  216. if (moduleBrowserSortJ)
  217. moduleBrowserSort = (ModuleBrowserSort) json_integer_value(moduleBrowserSortJ);
  218. json_t* moduleBrowserZoomJ = json_object_get(rootJ, "moduleBrowserZoom");
  219. if (moduleBrowserZoomJ)
  220. moduleBrowserZoom = json_number_value(moduleBrowserZoomJ);
  221. moduleWhitelist.clear();
  222. json_t* moduleWhitelistJ = json_object_get(rootJ, "moduleWhitelist");
  223. if (moduleWhitelistJ) {
  224. const char* pluginSlug;
  225. json_t* moduleSlugsJ;
  226. json_object_foreach(moduleWhitelistJ, pluginSlug, moduleSlugsJ) {
  227. auto& moduleSlugs = moduleWhitelist[pluginSlug];
  228. size_t i;
  229. json_t* moduleSlugJ;
  230. json_array_foreach(moduleSlugsJ, i, moduleSlugJ) {
  231. std::string moduleSlug = json_string_value(moduleSlugJ);
  232. moduleSlugs.insert(moduleSlug);
  233. }
  234. }
  235. }
  236. moduleUsages.clear();
  237. json_t* moduleUsagesJ = json_object_get(rootJ, "moduleUsages");
  238. if (moduleUsagesJ) {
  239. const char* pluginSlug;
  240. json_t* modulesJ;
  241. json_object_foreach(moduleUsagesJ, pluginSlug, modulesJ) {
  242. const char* moduleSlug;
  243. json_t* moduleJ;
  244. json_object_foreach(modulesJ, moduleSlug, moduleJ) {
  245. ModuleUsage mu;
  246. json_t* countJ = json_object_get(moduleJ, "count");
  247. if (countJ)
  248. mu.count = json_integer_value(countJ);
  249. json_t* lastTimeJ = json_object_get(moduleJ, "lastTime");
  250. if (lastTimeJ)
  251. mu.lastTime = json_number_value(lastTimeJ);
  252. moduleUsages[pluginSlug][moduleSlug] = mu;
  253. }
  254. }
  255. }
  256. }
  257. void save(const std::string& path) {
  258. INFO("Saving settings %s", path.c_str());
  259. json_t* rootJ = toJson();
  260. if (!rootJ)
  261. return;
  262. FILE* file = std::fopen(path.c_str(), "w");
  263. if (!file)
  264. return;
  265. DEFER({std::fclose(file);});
  266. // Because settings include doubles, it should use 17 decimal digits of precision instead of just 9 for float32.
  267. json_dumpf(rootJ, file, JSON_INDENT(2) | JSON_REAL_PRECISION(17));
  268. json_decref(rootJ);
  269. }
  270. void load(const std::string& path) {
  271. INFO("Loading settings %s", path.c_str());
  272. FILE* file = std::fopen(path.c_str(), "r");
  273. if (!file)
  274. return;
  275. DEFER({std::fclose(file);});
  276. json_error_t error;
  277. json_t* rootJ = json_loadf(file, 0, &error);
  278. if (!rootJ)
  279. throw Exception(string::f("Settings file has invalid JSON at %d:%d %s", error.line, error.column, error.text));
  280. fromJson(rootJ);
  281. json_decref(rootJ);
  282. }
  283. } // namespace settings
  284. } // namespace rack