From 27abc58d519a4fdd0f381d5b398097288e025c09 Mon Sep 17 00:00:00 2001
From: Jakob Bornecrantz <jakob@collabora.com>
Date: Thu, 15 Oct 2020 17:58:52 +0100
Subject: [PATCH] st/prober: Add the concept of active config to config file

---
 src/xrt/state_trackers/prober/p_json.c   | 55 ++++++++++++++++++++++++
 src/xrt/state_trackers/prober/p_prober.c | 14 +++++-
 src/xrt/state_trackers/prober/p_prober.h | 17 ++++++++
 3 files changed, 84 insertions(+), 2 deletions(-)

diff --git a/src/xrt/state_trackers/prober/p_json.c b/src/xrt/state_trackers/prober/p_json.c
index 7a3609084..3cf4d1911 100644
--- a/src/xrt/state_trackers/prober/p_json.c
+++ b/src/xrt/state_trackers/prober/p_json.c
@@ -10,6 +10,7 @@
 #include "util/u_file.h"
 #include "util/u_json.h"
 #include "util/u_debug.h"
+
 #include "p_prober.h"
 
 #include <stdio.h>
@@ -17,6 +18,8 @@
 #include <string.h>
 #include <sys/stat.h>
 
+DEBUG_GET_ONCE_OPTION(active_config, "P_OVERRIDE_ACTIVE_CONFIG", NULL)
+
 
 char *
 read_content(FILE *file)
@@ -150,6 +153,58 @@ get_obj_str(cJSON *json, const char *name, char *array, size_t array_size)
 	return true;
 }
 
+static bool
+is_json_ok(struct prober *p)
+{
+	if (p->json.root == NULL) {
+		if (p->json.file_loaded) {
+			fprintf(stderr, "JSON not parsed!\n");
+		} else {
+			fprintf(stderr, "No config file!\n");
+		}
+		return false;
+	}
+
+	return true;
+}
+
+static bool
+parse_active(const char *str,
+             const char *from,
+             enum p_active_config *out_active)
+{
+	if (strcmp(str, "none") == 0) {
+		*out_active = P_ACTIVE_CONFIG_NONE;
+	} else if (strcmp(str, "tracking") == 0) {
+		*out_active = P_ACTIVE_CONFIG_TRACKING;
+	} else {
+		fprintf(stderr, "Unknown active config '%s' from %s.\n", str,
+		        from);
+		*out_active = P_ACTIVE_CONFIG_NONE;
+		return false;
+	}
+
+	return true;
+}
+
+void
+p_json_get_active(struct prober *p, enum p_active_config *out_active)
+{
+	const char *str = debug_get_option_active_config();
+	if (str != NULL && parse_active(str, "environment", out_active)) {
+		return;
+	}
+
+	char tmp[256];
+	if (!is_json_ok(p) ||
+	    !get_obj_str(p->json.root, "active", tmp, sizeof(tmp))) {
+		*out_active = P_ACTIVE_CONFIG_NONE;
+		return;
+	}
+
+	parse_active(tmp, "json", out_active);
+}
+
 bool
 p_json_get_tracking_settings(struct prober *p, struct xrt_settings_tracking *s)
 {
diff --git a/src/xrt/state_trackers/prober/p_prober.c b/src/xrt/state_trackers/prober/p_prober.c
index d8f3cd063..7012e7d85 100644
--- a/src/xrt/state_trackers/prober/p_prober.c
+++ b/src/xrt/state_trackers/prober/p_prober.c
@@ -20,6 +20,7 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <assert.h>
 
 
 /*
@@ -656,10 +657,19 @@ select_device(struct xrt_prober *xp,
               size_t num_xdevs)
 {
 	struct prober *p = (struct prober *)xp;
+	enum p_active_config active;
 	bool have_hmd = false;
 
-	add_from_devices(p, xdevs, num_xdevs, &have_hmd);
-	add_from_auto_probers(p, xdevs, num_xdevs, &have_hmd);
+	p_json_get_active(p, &active);
+
+	switch (active) {
+	case P_ACTIVE_CONFIG_NONE:
+	case P_ACTIVE_CONFIG_TRACKING:
+		add_from_devices(p, xdevs, num_xdevs, &have_hmd);
+		add_from_auto_probers(p, xdevs, num_xdevs, &have_hmd);
+		break;
+	default: assert(false);
+	}
 
 	// It's easier if we just put the first hmd first,
 	// but keep other internal ordering of devices.
diff --git a/src/xrt/state_trackers/prober/p_prober.h b/src/xrt/state_trackers/prober/p_prober.h
index fbc165c2e..36106d13c 100644
--- a/src/xrt/state_trackers/prober/p_prober.h
+++ b/src/xrt/state_trackers/prober/p_prober.h
@@ -60,6 +60,14 @@
 
 #define MAX_AUTO_PROBERS 8
 
+/*!
+ * What config is currently active in the config file.
+ */
+enum p_active_config
+{
+	P_ACTIVE_CONFIG_NONE = 0,
+	P_ACTIVE_CONFIG_TRACKING = 1,
+};
 
 #ifdef XRT_OS_LINUX
 /*!
@@ -194,6 +202,15 @@ struct prober
 void
 p_json_open_or_create_main_file(struct prober *p);
 
+/*!
+ * Read from the JSON loaded json config file and returns the active config,
+ * can be overridden by `P_OVERRIDE_ACTIVE_CONFIG` envirmental variable.
+ *
+ * @public @memberof prober
+ */
+void
+p_json_get_active(struct prober *p, enum p_active_config *out_active);
+
 /*!
  * Extract tracking settings from the JSON.
  *