瀏覽代碼

Merge pull request #178 from OpenHMD/get-api-version

Get api version
TheOnlyJoey 6 年之前
父節點
當前提交
a4dc15f740
共有 4 個文件被更改,包括 64 次插入0 次删除
  1. 7 0
      examples/simple/simple.c
  2. 20 0
      include/openhmd.h
  3. 33 0
      src/openhmd.c
  4. 4 0
      src/openhmdi.h

+ 7 - 0
examples/simple/simple.c

@@ -42,6 +42,13 @@ int main(int argc, char** argv)
 	if(argc > 1)
 		device_idx = atoi(argv[1]);
 
+	ohmd_require_version(0, 3, 0);
+
+	int major, minor, patch;
+	ohmd_get_version(&major, &minor, &patch);
+
+	printf("OpenHMD version: %d.%d.%d\n", major, minor, patch);
+
 	ohmd_context* ctx = ohmd_ctx_create();
 
 	// Probe for devices

+ 20 - 0
include/openhmd.h

@@ -452,6 +452,26 @@ OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_seti(ohmd_device* device, ohmd_in
  **/
 OHMD_APIENTRYDLL int OHMD_APIENTRY ohmd_device_set_data(ohmd_device* device, ohmd_data_value type, const void* in);
 
+/**
+ * Get the library version.
+ *
+ * @param major Major version.
+ * @param minor Minor version.
+ * @param patch Patch version.
+ **/
+OHMD_APIENTRYDLL void OHMD_APIENTRY ohmd_get_version(int* out_major, int* out_minor, int* out_patch);
+
+/**
+ * Set an void* data value for a device.
+ *
+ * @param major Required major version.
+ * @param minor Required minor version.
+ * @param patch Required patch version.
+ * @return OMHD_S_OK if the version is compatible or OHMD_S_UNSUPPORTED if it's not.
+ **/
+OHMD_APIENTRYDLL ohmd_status OHMD_APIENTRY ohmd_require_version(int major, int minor, int patch);
+
+
 #ifdef __cplusplus
 }
 #endif

+ 33 - 0
src/openhmd.c

@@ -610,3 +610,36 @@ uint64_t ohmd_monotonic_conv(uint64_t ticks, uint64_t srcTicksPerSecond, uint64_
 	return ticks / srcTicksPerSecond * dstTicksPerSecond +
 		ticks % srcTicksPerSecond * dstTicksPerSecond / srcTicksPerSecond;
 }
+
+void ohmd_get_version(int* out_major, int* out_minor, int* out_patch)
+{
+	*out_major = OHMD_VERSION_MAJOR;
+	*out_minor = OHMD_VERSION_MINOR;
+	*out_patch = OHMD_VERSION_PATCH;
+}
+
+ohmd_status ohmd_require_version(int major, int minor, int patch)
+{
+	int curr_major, curr_minor, curr_patch;
+	ohmd_get_version(&curr_major, &curr_minor, &curr_patch);
+
+	if(curr_major != major){
+		// require same major version
+		return OHMD_S_UNSUPPORTED;
+	}
+
+	if(curr_minor == minor){
+		// check patch version if the required minor version matches current
+		if(curr_patch < patch){
+			// fail is patch is too low
+			return OHMD_S_UNSUPPORTED;
+		}
+	}
+	else if(curr_minor < minor)
+	{
+		// fail if minor is too low
+		return OHMD_S_UNSUPPORTED;
+	}
+
+	return OHMD_S_OK;
+}

+ 4 - 0
src/openhmdi.h

@@ -27,6 +27,10 @@
 
 #define OHMD_STRINGIFY(_what) #_what
 
+#define OHMD_VERSION_MAJOR 0
+#define OHMD_VERSION_MINOR 3
+#define OHMD_VERSION_PATCH 0
+
 typedef struct ohmd_driver ohmd_driver;
 
 typedef struct {