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.

430 lines
13KB

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