summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Hutterer <peter.hutterer@redhat.com>2008-11-19 15:50:57 +1000
committerPeter Hutterer <peter.hutterer@who-t.net>2009-01-12 12:55:27 +1000
commit669f6810af9a89187d6149841925fe765f3988ff (patch)
treef5467b1bbec5382a36c5b9f7696d5c62664f681e
parent56efbc0986e782da45addb05ece9f456d41d7a90 (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>
-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 e79a2edc6..cd9805aa1 100644
--- a/Xi/xiproperty.c
+++ b/Xi/xiproperty.c
@@ -32,6 +32,7 @@
32#include "dix.h" 32#include "dix.h"
33#include "inputstr.h" 33#include "inputstr.h"
34#include <X11/extensions/XI.h> 34#include <X11/extensions/XI.h>
35#include <X11/Xatom.h>
35#include <X11/extensions/XIproto.h> 36#include <X11/extensions/XIproto.h>
36#include "exglobals.h" 37#include "exglobals.h"
37#include "exevents.h" 38#include "exevents.h"
@@ -71,6 +72,73 @@ XIGetKnownProperty(char *name)
71} 72}
72 73
73/** 74/**
75 * Convert the given property's value(s) into @nelem_return integer values and
76 * store them in @buf_return. If @nelem_return is larger than the number of
77 * values in the property, @nelem_return is set to the number of values in the
78 * property.
79 *
80 * If *@buf_return is NULL and @nelem_return is 0, memory is allocated
81 * automatically and must be freed by the caller.
82 *
83 * Possible return codes.
84 * Success ... No error.
85 * BadMatch ... Wrong atom type, atom is not XA_INTEGER
86 * BadAlloc ... NULL passed as buffer and allocation failed.
87 * BadLength ... @buff is NULL but @nelem_return is non-zero.
88 *
89 * @param val The property value
90 * @param nelem_return The maximum number of elements to return.
91 * @param buf_return Pointer to an array of at least @nelem_return values.
92 * @return Success or the error code if an error occured.
93 */
94_X_EXPORT int
95XIPropToInt(XIPropertyValuePtr val, int *nelem_return, int **buf_return)
96{
97 int i;
98 int *buf;
99
100 if (val->type != XA_INTEGER)
101 return BadMatch;
102 if (!*buf_return && *nelem_return)
103 return BadLength;
104
105 switch(val->format)
106 {
107 case 8:
108 case 16:
109 case 32:
110 break;
111 default:
112 return BadValue;
113 }
114
115 buf = *buf_return;
116
117 if (!buf && !(*nelem_return))
118 {
119 buf = xcalloc(val->size, sizeof(int));
120 if (!buf)
121 return BadAlloc;
122 *buf_return = buf;
123 *nelem_return = val->size;
124 } else if (val->size < *nelem_return)
125 *nelem_return = val->size;
126
127 for (i = 0; i < val->size && i < *nelem_return; i++)
128 {
129 switch(val->format)
130 {
131 case 8: buf[i] = ((CARD8*)val->data)[i]; break;
132 case 16: buf[i] = ((CARD16*)val->data)[i]; break;
133 case 32: buf[i] = ((CARD32*)val->data)[i]; break;
134 }
135 }
136
137 return Success;
138}
139
140
141/**
74 * Init those properties that are allocated by the server and most likely used 142 * Init those properties that are allocated by the server and most likely used
75 * by the DIX or the DDX. 143 * by the DIX or the DDX.
76 */ 144 */
diff --git a/include/exevents.h b/include/exevents.h
index 2a7ec97a2..485347be2 100644
--- a/include/exevents.h
+++ b/include/exevents.h
@@ -251,4 +251,10 @@ extern _X_EXPORT Atom XIGetKnownProperty(
251 251
252extern _X_EXPORT DeviceIntPtr XIGetDevice(xEvent *ev); 252extern _X_EXPORT DeviceIntPtr XIGetDevice(xEvent *ev);
253 253
254extern _X_EXPORT int XIPropToInt(
255 XIPropertyValuePtr val,
256 int *nelem_return,
257 int **buf_return
258);
259
254#endif /* EXEVENTS_H */ 260#endif /* EXEVENTS_H */