[edid-decode 6/7] Add a small framework to decode fields generically

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Damien Lespiau <damien.lespiau@xxxxxxxxx>
---
 edid-decode.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 67 insertions(+), 2 deletions(-)

diff --git a/edid-decode.c b/edid-decode.c
index 5061228..7aed3c6 100644
--- a/edid-decode.c
+++ b/edid-decode.c
@@ -32,8 +32,6 @@
 #include <time.h>
 #include <ctype.h>
 
-#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
-
 static int claims_one_point_oh = 0;
 static int claims_one_point_two = 0;
 static int claims_one_point_three = 0;
@@ -65,6 +63,73 @@ static int warning_zero_preferred_refresh = 0;
 
 static int conformant = 1;
 
+struct value {
+    int value;
+    const char *description;
+};
+
+struct field {
+    const char *name;
+    int start, end;
+    struct value *values;
+    int n_values;
+};
+
+#define DEFINE_FIELD(n, var, s, e, ...)				\
+    static struct value var##_values[] =  {			\
+        __VA_ARGS__						\
+    };								\
+    static struct field var = {					\
+        .name = n,						\
+        .start = s,		        			\
+        .end = e,						\
+        .values = var##_values,	        			\
+        .n_values = ARRAY_SIZE(var##_values),			\
+    }
+
+static void
+decode_value(struct field *field, int val, const char *prefix)
+{
+    struct value *v;
+    int i;
+
+    for (i = 0; i < field->n_values; i++) {
+        v = &field->values[i];
+
+        if (v->value == val)
+           break;
+    }
+
+    if (i == field->n_values) {
+       printf("%s%s: %d\n", prefix, field->name, val);
+       return;
+    }
+
+    printf("%s%s: %s (%d)\n", prefix, field->name, v->description, val);
+}
+
+static void
+_decode(struct field **fields, int n_fields, int data, const char *prefix)
+{
+    int i;
+
+    for (i = 0; i < n_fields; i++) {
+	struct field *f = fields[i];
+	int field_length = f->end - f->start + 1;
+	int val;
+
+        if (field_length == 32)
+            val = data;
+        else
+            val = (data >> f->start) & ((1 << field_length) - 1);
+
+        decode_value(f, val, prefix);
+    }
+}
+
+#define decode(fields, data, prefix)    \
+    _decode(fields, ARRAY_SIZE(fields), data, prefix)
+
 static char *manufacturer_name(unsigned char *x)
 {
     static char name[4];
-- 
1.8.3.1

_______________________________________________
dri-devel mailing list
dri-devel@xxxxxxxxxxxxxxxxxxxxx
http://lists.freedesktop.org/mailman/listinfo/dri-devel




[Index of Archives]     [Linux DRI Users]     [Linux Intel Graphics]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux