The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

util/u_config_json: add functions to save/load gui state to file

authored by

Moses Turner and committed by
Jakob Bornecrantz
254c233c dfd576e0

+82 -8
+67 -8
src/xrt/auxiliary/util/u_config_json.c
··· 23 23 #include <sys/stat.h> 24 24 25 25 #include "bindings/b_generated_bindings.h" 26 + #include <assert.h> 26 27 27 28 DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL) 28 29 29 30 #define CONFIG_FILE_NAME "config_v0.json" 31 + #define GUI_STATE_FILE_NAME "gui_state_v0.json" 30 32 31 33 void 32 34 u_config_json_close(struct u_config_json *json) ··· 38 40 json->file_loaded = false; 39 41 } 40 42 41 - void 42 - u_config_json_open_or_create_main_file(struct u_config_json *json) 43 + static void 44 + u_config_json_open_or_create_file(struct u_config_json *json, const char *filename) 43 45 { 44 46 json->file_loaded = false; 45 47 #if defined(XRT_OS_LINUX) && !defined(XRT_OS_ANDROID) 46 48 char tmp[1024]; 47 - ssize_t ret = u_file_get_path_in_config_dir(CONFIG_FILE_NAME, tmp, sizeof(tmp)); 49 + ssize_t ret = u_file_get_path_in_config_dir(filename, tmp, sizeof(tmp)); 48 50 if (ret <= 0) { 49 51 U_LOG_E( 50 52 "Could not load or create config file no $HOME " ··· 52 54 return; 53 55 } 54 56 55 - FILE *file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "r"); 57 + FILE *file = u_file_open_file_in_config_dir(filename, "r"); 56 58 if (file == NULL) { 57 59 return; 58 60 } ··· 83 85 //! @todo implement the underlying u_file_get_path_in_config_dir 84 86 return; 85 87 #endif 88 + } 89 + 90 + void 91 + u_config_json_open_or_create_main_file(struct u_config_json *json) 92 + { 93 + u_config_json_open_or_create_file(json, CONFIG_FILE_NAME); 86 94 } 87 95 88 96 static cJSON * ··· 386 394 } 387 395 388 396 static void 389 - u_config_write(struct u_config_json *json) 397 + u_config_write(struct u_config_json *json, const char *filename) 390 398 { 391 399 char *str = cJSON_Print(json->root); 392 400 U_LOG_D("%s", str); 393 401 394 - FILE *config_file = u_file_open_file_in_config_dir(CONFIG_FILE_NAME, "w"); 402 + FILE *config_file = u_file_open_file_in_config_dir(filename, "w"); 395 403 fprintf(config_file, "%s\n", str); 396 404 fflush(config_file); 397 405 fclose(config_file); ··· 435 443 cJSON_DeleteItemFromObject(t, "calibration_path"); 436 444 cJSON_AddStringToObject(t, "calibration_path", settings->calibration_path); 437 445 438 - u_config_write(json); 446 + u_config_write(json, CONFIG_FILE_NAME); 439 447 } 440 448 441 449 static cJSON * ··· 497 505 cJSON_AddItemToArray(o, entry); 498 506 } 499 507 500 - u_config_write(json); 508 + u_config_write(json, CONFIG_FILE_NAME); 509 + } 510 + 511 + void 512 + u_gui_state_open_file(struct u_config_json *json) 513 + { 514 + u_config_json_open_or_create_file(json, GUI_STATE_FILE_NAME); 515 + } 516 + 517 + static const char * 518 + u_gui_state_scene_to_string(enum u_gui_state_scene scene) 519 + { 520 + switch (scene) { 521 + case GUI_STATE_SCENE_CALIBRATE: return "calibrate"; 522 + default: assert(false); 523 + } 524 + } 525 + 526 + struct cJSON * 527 + u_gui_state_get_scene(struct u_config_json *json, enum u_gui_state_scene scene) 528 + { 529 + if (json->root == NULL) { 530 + return NULL; 531 + } 532 + const char *scene_name = u_gui_state_scene_to_string(scene); 533 + 534 + struct cJSON *c = 535 + cJSON_DetachItemFromObjectCaseSensitive(cJSON_GetObjectItemCaseSensitive(json->root, "scenes"), scene_name); 536 + cJSON_free(json->root); 537 + return c; 538 + } 539 + 540 + void 541 + u_gui_state_save_scene(struct u_config_json *json, enum u_gui_state_scene scene, struct cJSON *new_state) 542 + { 543 + 544 + if (!json->file_loaded) { 545 + u_config_json_make_default_root(json); 546 + } 547 + 548 + cJSON *root = json->root; 549 + 550 + const char *scene_name = u_gui_state_scene_to_string(scene); 551 + 552 + struct cJSON *sc = cJSON_GetObjectItemCaseSensitive(root, "scenes"); 553 + 554 + if (!sc) { 555 + sc = cJSON_AddObjectToObject(root, "scenes"); 556 + } 557 + cJSON_DeleteItemFromObject(sc, scene_name); 558 + cJSON_AddItemToObject(sc, scene_name, new_state); 559 + u_config_write(json, GUI_STATE_FILE_NAME); 501 560 }
+15
src/xrt/auxiliary/util/u_config_json.h
··· 102 102 bool 103 103 u_config_json_get_remote_port(struct u_config_json *json, int *out_port); 104 104 105 + 106 + enum u_gui_state_scene 107 + { 108 + GUI_STATE_SCENE_CALIBRATE 109 + }; 110 + 111 + void 112 + u_gui_state_open_file(struct u_config_json *json); 113 + 114 + struct cJSON * 115 + u_gui_state_get_scene(struct u_config_json *json, enum u_gui_state_scene scene); 116 + 117 + void 118 + u_gui_state_save_scene(struct u_config_json *json, enum u_gui_state_scene scene, struct cJSON *new_state); 119 + 105 120 #ifdef __cplusplus 106 121 } 107 122 #endif