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.

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