[PATCH 3/6] Xi: add XIPropToInt() auxiliary function.

Peter Hutterer peter.hutterer at who-t.net
Thu Dec 4 22:42:35 PST 2008


From: Peter Hutterer <peter.hutterer at redhat.com>

Converts an XIPropertyValuePtr to an integer, provided that type and format is
right.

Code originally written by Simon Thum.

Signed-off-by: Peter Hutterer <peter.hutterer at redhat.com>
---
 Xi/xiproperty.c    |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 include/exevents.h |    5 ++++
 2 files changed, 62 insertions(+), 0 deletions(-)

diff --git a/Xi/xiproperty.c b/Xi/xiproperty.c
index e79a2ed..5f435ff 100644
--- a/Xi/xiproperty.c
+++ b/Xi/xiproperty.c
@@ -32,6 +32,7 @@
 #include "dix.h"
 #include "inputstr.h"
 #include <X11/extensions/XI.h>
+#include <X11/Xatom.h>
 #include <X11/extensions/XIproto.h>
 #include "exglobals.h"
 #include "exevents.h"
@@ -71,6 +72,62 @@ XIGetKnownProperty(char *name)
 }
 
 /**
+ * Convert the given property's value(s) into integer values and store them in
+ * @buf. If @buf is NULL, memory is allocated automatically and must be freed
+ * by the caller.
+ *
+ * Possible return codes.
+ * Success ... No error.
+ * BadMatch ... Wrong atom type, atom is not XA_INTEGER
+ * BadValue ... Wrong format, format is not 32
+ * BadAlloc ... NULL passed as buffer and allocation failed.
+ *
+ * @return Success or the error code if an error occured.
+ */
+_X_EXPORT int
+XIPropToInt(XIPropertyValuePtr val, int **buf_return)
+{
+    int i;
+    int *buf;
+
+    if (val->type != XA_INTEGER)
+        return BadMatch;
+
+    switch(val->format)
+    {
+        case 8:
+        case 16:
+        case 32:
+            break;
+        default:
+            return BadValue;
+    }
+
+    buf = *buf_return;
+
+    if (!buf)
+    {
+        buf = xcalloc(val->size, sizeof(int));
+        if (!buf)
+            return BadAlloc;
+        *buf_return = buf;
+    }
+
+    for (i = 0; i < val->size; i++)
+    {
+        switch(val->format)
+        {
+            case 8:  buf[i] = *(CARD8*)val->data; break;
+            case 16: buf[i] = *(CARD16*)val->data; break;
+            case 32: buf[i] = *(CARD32*)val->data; break;
+        }
+    }
+
+    return Success;
+}
+
+
+/**
  * Init those properties that are allocated by the server and most likely used
  * by the DIX or the DDX.
  */
diff --git a/include/exevents.h b/include/exevents.h
index 2a7ec97..d5c8c01 100644
--- a/include/exevents.h
+++ b/include/exevents.h
@@ -251,4 +251,9 @@ extern _X_EXPORT Atom XIGetKnownProperty(
 
 extern _X_EXPORT DeviceIntPtr XIGetDevice(xEvent *ev);
 
+extern _X_EXPORT int XIPropToInt(
+        XIPropertyValuePtr val,
+        int **buf_return
+);
+
 #endif /* EXEVENTS_H */
-- 
1.6.0.4




More information about the xorg mailing list