The open source OpenXR runtime
0
fork

Configure Feed

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

st/prober: Refactor JSON config loading so that we print less errors when we don't need the config

authored by

Jakob Bornecrantz and committed by
Jakob Bornecrantz
e620fa90 1744715a

+63 -40
+37 -14
src/xrt/state_trackers/prober/p_json.c
··· 43 43 return buffer; 44 44 } 45 45 46 - cJSON * 47 - p_json_open_or_create_main_file(void) 46 + void 47 + p_json_open_or_create_main_file(struct prober *p) 48 48 { 49 + char tmp[1024]; 50 + ssize_t ret = 51 + u_file_get_path_in_config_dir("config_v0.json", tmp, sizeof(tmp)); 52 + if (ret <= 0) { 53 + fprintf(stderr, 54 + "ERROR:Could not load or create config file no $HOME " 55 + "or $XDG_CONFIG_HOME env variables defined\n"); 56 + return; 57 + } 58 + 49 59 FILE *file = u_file_open_file_in_config_dir("config_v0.json", "r"); 50 60 if (file == NULL) { 51 - fprintf(stderr, "Could not open the file!\n"); 52 - return NULL; 61 + return; 53 62 } 63 + 64 + p->json.file_loaded = true; 54 65 55 66 char *str = read_content(file); 56 67 fclose(file); 57 68 if (str == NULL) { 58 - fprintf(stderr, "Could not read the contents of the file!\n"); 59 - return NULL; 69 + fprintf(stderr, "ERROR: Could not read the contents of '%s'!\n", 70 + tmp); 71 + return; 72 + } 73 + 74 + // No config created, ignore. 75 + if (strlen(str) == 0) { 76 + free(str); 77 + return; 60 78 } 61 79 62 - cJSON *ret = cJSON_Parse(str); 63 - if (ret == NULL) { 64 - fprintf(stderr, "Failed to parse JSON:\n%s\n#######\n", str); 80 + p->json.root = cJSON_Parse(str); 81 + if (p->json.root == NULL) { 82 + fprintf(stderr, "Failed to parse JSON in '%s':\n%s\n#######\n", 83 + tmp, str); 65 84 fprintf(stderr, "'%s'\n", cJSON_GetErrorPtr()); 66 85 } 67 86 68 87 free(str); 69 - return ret; 70 88 } 71 89 72 90 static cJSON * ··· 128 146 } 129 147 130 148 bool 131 - p_json_get_tracking_settings(cJSON *root, struct xrt_settings_tracking *s) 149 + p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s) 132 150 { 133 - if (root == NULL) { 151 + if (p->json.root == NULL) { 152 + if (p->json.file_loaded) { 153 + fprintf(stderr, "JSON not parsed!\n"); 154 + } else { 155 + fprintf(stderr, "No config file!\n"); 156 + } 134 157 return false; 135 158 } 136 159 137 - cJSON *t = cJSON_GetObjectItemCaseSensitive(root, "tracking"); 160 + cJSON *t = cJSON_GetObjectItemCaseSensitive(p->json.root, "tracking"); 138 161 if (t == NULL) { 139 - fprintf(stderr, "No tracking node!\n"); 162 + fprintf(stderr, "No tracking node\n"); 140 163 return false; 141 164 } 142 165
+1 -1
src/xrt/state_trackers/prober/p_prober.c
··· 328 328 329 329 int ret; 330 330 331 - p->json.root = p_json_open_or_create_main_file(); 331 + p_json_open_or_create_main_file(p); 332 332 333 333 ret = collect_entries(p); 334 334 if (ret != 0) {
+6 -3
src/xrt/state_trackers/prober/p_prober.h
··· 135 135 136 136 struct 137 137 { 138 + //! For error reporting, was it loaded but not parsed? 139 + bool file_loaded; 140 + 138 141 cJSON *root; 139 142 } json; 140 143 ··· 178 181 /*! 179 182 * Load the JSON config file. 180 183 */ 181 - cJSON * 182 - p_json_open_or_create_main_file(void); 184 + void 185 + p_json_open_or_create_main_file(struct prober *p); 183 186 184 187 /*! 185 188 * Extract tracking settings from the JSON. 186 189 */ 187 190 bool 188 - p_json_get_tracking_settings(cJSON *root, struct xrt_settings_tracking *s); 191 + p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s); 189 192 190 193 /*! 191 194 * Dump the given device to stdout.
+19 -22
src/xrt/state_trackers/prober/p_tracking.c
··· 40 40 // Owning prober. 41 41 struct prober *p; 42 42 43 - // Has the settings be loaded. 44 - bool setting_ok; 43 + // Have we tried to load the settings. 44 + bool tried_settings; 45 45 46 46 // Settings for this tracking system. 47 47 struct xrt_settings_tracking settings; ··· 112 112 static void 113 113 p_factory_ensure_frameserver(struct p_factory *fact) 114 114 { 115 - // No settings loaded. 116 - if (!fact->setting_ok) { 115 + // Already created. 116 + if (fact->xfs != NULL) { 117 + return; 118 + } 119 + 120 + // We have already tried to load the settings. 121 + if (fact->tried_settings) { 117 122 return; 118 123 } 119 124 120 - // Already created. 121 - if (fact->xfs != NULL) { 125 + // We have no tried the settings. 126 + fact->tried_settings = true; 127 + 128 + if (!p_json_get_tracking_settings(fact->p, &fact->settings)) { 129 + fprintf(stderr, 130 + "ERROR: Could not setup PSVR and/or PSMV tracking, see " 131 + "above.\n"); 122 132 return; 123 133 } 124 134 ··· 217 227 { 218 228 struct p_factory *fact = p_factory(xfact); 219 229 220 - if (!fact->setting_ok) { 221 - return -1; 222 - } 223 - 224 230 #ifdef XRT_HAVE_OPENCV 225 231 struct xrt_tracked_psmv *xtmv = NULL; 226 232 227 233 p_factory_ensure_frameserver(fact); 228 234 229 235 if (fact->num_xtmv < ARRAY_SIZE(fact->xtmv)) { 230 - xtmv = fact->xtmv[fact->num_xtmv++]; 236 + xtmv = fact->xtmv[fact->num_xtmv]; 231 237 } 232 238 233 239 if (xtmv == NULL) { 234 240 return -1; 235 241 } 236 242 243 + fact->num_xtmv++; 244 + 237 245 t_psmv_start(xtmv); 238 246 *out_xtmv = xtmv; 239 247 ··· 249 257 struct xrt_tracked_psvr **out_xtvr) 250 258 { 251 259 struct p_factory *fact = p_factory(xfact); 252 - 253 - if (!fact->setting_ok) { 254 - return -1; 255 - } 256 260 257 261 #ifdef XRT_HAVE_OPENCV 258 262 struct xrt_tracked_psvr *xtvr = NULL; ··· 305 309 306 310 // Finally set us as the tracking factory. 307 311 p->base.tracking = &fact->base; 308 - 309 - fact->setting_ok = 310 - p_json_get_tracking_settings(p->json.root, &fact->settings); 311 - 312 - if (!fact->setting_ok) { 313 - fprintf(stderr, "Failed to load settings!\n"); 314 - } 315 312 316 313 return 0; 317 314 }