summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@redhat.com>2008-11-19 15:50:57 +1000
committerAdam Jackson <ajax@redhat.com>2009-03-27 15:25:07 -0400
commit6ad6b6f900e9c3a55e4ee509bdc9ff0ddec9a59a (patch)
treef613d9e561e707d66e26ae6ced43d0735d134f2a
parent667865b9eb98ed73461028b3d38be19619712533 (diff)
Xi: add XIPropToInt() auxiliary function.
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@redhat.com> (cherry picked from commit 669f6810af9a89187d6149841925fe765f3988ff)
-rw-r--r--Xi/xiproperty.c68
-rw-r--r--include/exevents.h6
2 files changed, 74 insertions, 0 deletions
diff --git a/Xi/xiproperty.c b/Xi/xiproperty.c
index 01db856bf..0c2475b63 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,73 @@ XIGetKnownProperty(char *name)
}
/**
+ * Convert the given property's value(s) into @nelem_return integer values and
+ * store them in @buf_return. If @nelem_return is larger than the number of
+ * values in the property, @nelem_return is set to the number of values in the
+ * property.
+ *
+ * If *@buf_return is NULL and @nelem_return is 0, 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
+ * BadAlloc ... NULL passed as buffer and allocation failed.
+ * BadLength ... @buff is NULL but @nelem_return is non-zero.
+ *
+ * @param val The property value
+ * @param nelem_return The maximum number of elements to return.
+ * @param buf_return Pointer to an array of at least @nelem_return values.
+ * @return Success or the error code if an error occured.
+ */
+_X_EXPORT int
+XIPropToInt(XIPropertyValuePtr val, int *nelem_return, int **buf_return)
+{
+ int i;
+ int *buf;
+
+ if (val->type != XA_INTEGER)
+ return BadMatch;
+ if (!*buf_return && *nelem_return)
+ return BadLength;
+
+ switch(val->format)
+ {
+ case 8:
+ case 16:
+ case 32:
+ break;
+ default:
+ return BadValue;
+ }
+
+ buf = *buf_return;
+
+ if (!buf && !(*nelem_return))
+ {
+ buf = xcalloc(val->size, sizeof(int));
+ if (!buf)
+ return BadAlloc;
+ *buf_return = buf;
+ *nelem_return = val->size;
+ } else if (val->size < *nelem_return)
+ *nelem_return = val->size;
+
+ for (i = 0; i < val->size && i < *nelem_return; i++)
+ {
+ switch(val->format)
+ {
+ case 8: buf[i] = ((CARD8*)val->data)[i]; break;
+ case 16: buf[i] = ((CARD16*)val->data)[i]; break;
+ case 32: buf[i] = ((CARD32*)val->data)[i]; 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 7459b5c24..b13bf270a 100644
--- a/include/exevents.h
+++ b/include/exevents.h
@@ -251,4 +251,10 @@ extern Atom XIGetKnownProperty(
extern DeviceIntPtr XIGetDevice(xEvent *ev);
+extern _X_EXPORT int XIPropToInt(
+ XIPropertyValuePtr val,
+ int *nelem_return,
+ int **buf_return
+);
+
#endif /* EXEVENTS_H */