6b868c8
From 669f6810af9a89187d6149841925fe765f3988ff Mon Sep 17 00:00:00 2001
6b868c8
From: Peter Hutterer <peter.hutterer@redhat.com>
6b868c8
Date: Wed, 19 Nov 2008 15:50:57 +1000
6b868c8
Subject: [PATCH] Xi: add XIPropToInt() auxiliary function.
6b868c8
6b868c8
Converts an XIPropertyValuePtr to an integer, provided that type and format is
6b868c8
right.
6b868c8
6b868c8
Code originally written by Simon Thum.
6b868c8
6b868c8
Signed-off-by: Peter Hutterer <peter.hutterer@redhat.com>
6b868c8
---
6b868c8
 Xi/xiproperty.c    |   68 ++++++++++++++++++++++++++++++++++++++++++++++++++++
6b868c8
 include/exevents.h |    6 ++++
6b868c8
 2 files changed, 74 insertions(+), 0 deletions(-)
6b868c8
6b868c8
diff --git a/Xi/xiproperty.c b/Xi/xiproperty.c
6b868c8
index e79a2ed..cd9805a 100644
6b868c8
--- a/Xi/xiproperty.c
6b868c8
+++ b/Xi/xiproperty.c
6b868c8
@@ -32,6 +32,7 @@
6b868c8
 #include "dix.h"
6b868c8
 #include "inputstr.h"
6b868c8
 #include <X11/extensions/XI.h>
6b868c8
+#include <X11/Xatom.h>
6b868c8
 #include <X11/extensions/XIproto.h>
6b868c8
 #include "exglobals.h"
6b868c8
 #include "exevents.h"
6b868c8
@@ -71,6 +72,73 @@ XIGetKnownProperty(char *name)
6b868c8
 }
6b868c8
 
6b868c8
 /**
6b868c8
+ * Convert the given property's value(s) into @nelem_return integer values and
6b868c8
+ * store them in @buf_return. If @nelem_return is larger than the number of
6b868c8
+ * values in the property, @nelem_return is set to the number of values in the
6b868c8
+ * property.
6b868c8
+ *
6b868c8
+ * If *@buf_return is NULL and @nelem_return is 0, memory is allocated
6b868c8
+ * automatically and must be freed by the caller.
6b868c8
+ *
6b868c8
+ * Possible return codes.
6b868c8
+ * Success ... No error.
6b868c8
+ * BadMatch ... Wrong atom type, atom is not XA_INTEGER
6b868c8
+ * BadAlloc ... NULL passed as buffer and allocation failed.
6b868c8
+ * BadLength ... @buff is NULL but @nelem_return is non-zero.
6b868c8
+ *
6b868c8
+ * @param val The property value
6b868c8
+ * @param nelem_return The maximum number of elements to return.
6b868c8
+ * @param buf_return Pointer to an array of at least @nelem_return values.
6b868c8
+ * @return Success or the error code if an error occured.
6b868c8
+ */
6b868c8
+_X_EXPORT int
6b868c8
+XIPropToInt(XIPropertyValuePtr val, int *nelem_return, int **buf_return)
6b868c8
+{
6b868c8
+    int i;
6b868c8
+    int *buf;
6b868c8
+
6b868c8
+    if (val->type != XA_INTEGER)
6b868c8
+        return BadMatch;
6b868c8
+    if (!*buf_return && *nelem_return)
6b868c8
+        return BadLength;
6b868c8
+
6b868c8
+    switch(val->format)
6b868c8
+    {
6b868c8
+        case 8:
6b868c8
+        case 16:
6b868c8
+        case 32:
6b868c8
+            break;
6b868c8
+        default:
6b868c8
+            return BadValue;
6b868c8
+    }
6b868c8
+
6b868c8
+    buf = *buf_return;
6b868c8
+
6b868c8
+    if (!buf && !(*nelem_return))
6b868c8
+    {
6b868c8
+        buf = xcalloc(val->size, sizeof(int));
6b868c8
+        if (!buf)
6b868c8
+            return BadAlloc;
6b868c8
+        *buf_return = buf;
6b868c8
+        *nelem_return = val->size;
6b868c8
+    } else if (val->size < *nelem_return)
6b868c8
+        *nelem_return = val->size;
6b868c8
+
6b868c8
+    for (i = 0; i < val->size && i < *nelem_return; i++)
6b868c8
+    {
6b868c8
+        switch(val->format)
6b868c8
+        {
6b868c8
+            case 8:  buf[i] = ((CARD8*)val->data)[i]; break;
6b868c8
+            case 16: buf[i] = ((CARD16*)val->data)[i]; break;
6b868c8
+            case 32: buf[i] = ((CARD32*)val->data)[i]; break;
6b868c8
+        }
6b868c8
+    }
6b868c8
+
6b868c8
+    return Success;
6b868c8
+}
6b868c8
+
6b868c8
+
6b868c8
+/**
6b868c8
  * Init those properties that are allocated by the server and most likely used
6b868c8
  * by the DIX or the DDX.
6b868c8
  */
6b868c8
diff --git a/include/exevents.h b/include/exevents.h
6b868c8
index 2a7ec97..485347b 100644
6b868c8
--- a/include/exevents.h
6b868c8
+++ b/include/exevents.h
6b868c8
@@ -251,4 +251,10 @@ extern _X_EXPORT Atom XIGetKnownProperty(
6b868c8
 
6b868c8
 extern DeviceIntPtr XIGetDevice(xEvent *ev);
6b868c8
 
6b868c8
+extern _X_EXPORT int XIPropToInt(
6b868c8
+        XIPropertyValuePtr val,
6b868c8
+        int *nelem_return,
6b868c8
+        int **buf_return
6b868c8
+);
6b868c8
+
6b868c8
 #endif /* EXEVENTS_H */
6b868c8
-- 
6b868c8
1.6.0.6
6b868c8