]> diplodocus.org Git - xorg-xinput/blob - src/property.c
Require inputproto 1.9.99.4
[xorg-xinput] / src / property.c
1 /*
2 * Copyright 2007 Peter Hutterer
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation.
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 * OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of the author shall
22 * not be used in advertising or otherwise to promote the sale, use or
23 * other dealings in this Software without prior written authorization
24 * from the author.
25 *
26 */
27
28 #include <ctype.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <X11/Xatom.h>
33 #include <X11/extensions/XIproto.h>
34
35 #include "xinput.h"
36
37 static void
38 print_property(Display *dpy, XDevice* dev, Atom property)
39 {
40 XIPropertyInfo *propinfo;
41 Atom act_type;
42 char *name;
43 int act_format;
44 unsigned long nitems, bytes_after;
45 unsigned char *data, *ptr;
46 int j;
47
48 propinfo = XQueryDeviceProperty(dpy, dev, property);
49 if (!propinfo)
50 return;
51
52 name = XGetAtomName(dpy, property);
53 printf("\t%s (%d):\t", name, property);
54
55 if (XGetDeviceProperty(dpy, dev, property, 0, 1000, False, False,
56 AnyPropertyType, &act_type, &act_format,
57 &nitems, &bytes_after, &data) == Success)
58 {
59 ptr = data;
60 printf("\t");
61
62 for (j = 0; j < nitems; j++)
63 {
64 switch(act_type)
65 {
66 case XA_INTEGER:
67 switch(act_format)
68 {
69 case 8:
70 printf("%d", *((int8_t*)ptr));
71 break;
72 case 16:
73 printf("%d", *((int16_t*)ptr));
74 break;
75 case 32:
76 printf("%d", *((int32_t*)ptr));
77 break;
78 }
79 break;
80 case XA_STRING:
81 printf("\t%s\n", ptr);
82 break;
83 case XA_ATOM:
84 printf("\t%s\n", XGetAtomName(dpy, (Atom)(*ptr)));
85 break;
86 default:
87 printf("\t\t... of unknown type %s\n",
88 XGetAtomName(dpy, act_type));
89 break;
90 }
91
92 ptr += act_format/8;
93
94 if (j < nitems - 1)
95 printf(", ");
96 if (act_type == XA_STRING)
97 break;
98 }
99 printf("\n");
100 XFree(data);
101 } else
102 printf("\tFetch failure\n");
103
104 if (propinfo->pending || propinfo->range || propinfo->immutable || propinfo->fromClient)
105 {
106 printf("\t\t%s%s%s%s", ((propinfo->pending) ? "[pending]" : ""),
107 ((propinfo->range) ? "[range]" : ""),
108 ((propinfo->immutable) ? "[immutable]" : ""),
109 ((propinfo->fromClient) ? "[client]" : ""));
110 printf("\n");
111 }
112
113 if (propinfo->num_values)
114 {
115 long *values = propinfo->values;
116 printf("\t\tvalid values: ");
117 while(values && propinfo->num_values--)
118 printf("%ld ", *values++);
119 printf("\n");
120 }
121
122 XFree(propinfo);
123
124 }
125
126 int list_props(Display *dpy, int argc, char** argv, char* name, char *desc)
127 {
128 XDeviceInfo *info;
129 XDevice *dev;
130 int i;
131 int nprops;
132 Atom *props;
133
134 if (argc == 0)
135 {
136 fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
137 return EXIT_FAILURE;
138 }
139
140 for (i = 0; i < argc; i++)
141 {
142 info = find_device_info(dpy, argv[i], False);
143 if (!info)
144 {
145 fprintf(stderr, "unable to find device %s\n", argv[i]);
146 continue;
147 }
148
149 dev = XOpenDevice(dpy, info->id);
150 if (!dev)
151 {
152 fprintf(stderr, "unable to open device '%s'\n", info->name);
153 continue;
154 }
155
156 props = XListDeviceProperties(dpy, dev, &nprops);
157 if (!nprops)
158 {
159 printf("Device '%s' does not report any properties.\n", info->name);
160 continue;
161 }
162
163 printf("Device '%s':\n", info->name);
164 while(nprops--)
165 {
166 print_property(dpy, dev, props[nprops]);
167 }
168
169 XFree(props);
170 XCloseDevice(dpy, dev);
171 }
172 return EXIT_SUCCESS;
173 }
174
175 int
176 set_int_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
177 {
178 XDeviceInfo *info;
179 XDevice *dev;
180 Atom prop;
181 char *name;
182 int i;
183 Bool is_atom = True;
184 char *data;
185 int format, nelements = 0;
186
187 if (argc < 3)
188 {
189 fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
190 return EXIT_FAILURE;
191 }
192
193 info = find_device_info(dpy, argv[0], False);
194 if (!info)
195 {
196 fprintf(stderr, "unable to find device %s\n", argv[0]);
197 return EXIT_FAILURE;
198 }
199
200 dev = XOpenDevice(dpy, info->id);
201 if (!dev)
202 {
203 fprintf(stderr, "unable to open device %s\n", argv[0]);
204 return EXIT_FAILURE;
205 }
206
207 name = argv[1];
208
209 for(i = 0; i < strlen(name); i++) {
210 if (!isdigit(name[i])) {
211 is_atom = False;
212 break;
213 }
214 }
215
216 if (!is_atom)
217 prop = XInternAtom(dpy, name, False);
218 else
219 prop = atoi(name);
220
221 nelements = argc - 3;
222 format = atoi(argv[2]);
223 if (format != 8 && format != 16 && format != 32)
224 {
225 fprintf(stderr, "Invalid format %d\n", format);
226 return EXIT_FAILURE;
227 }
228
229 data = calloc(nelements, format/8);
230 for (i = 0; i < nelements; i++)
231 {
232 switch(format)
233 {
234 case 8:
235 *(((int8_t*)data) + i) = atoi(argv[3 + i]);
236 break;
237 case 16:
238 *(((int16_t*)data) + i) = atoi(argv[3 + i]);
239 break;
240 case 32:
241 *(((int32_t*)data) + i) = atoi(argv[3 + i]);
242 break;
243 }
244 }
245
246 XChangeDeviceProperty(dpy, dev, prop, XA_INTEGER, format, PropModeReplace,
247 (unsigned char*)data, nelements);
248
249 free(data);
250 XCloseDevice(dpy, dev);
251 return EXIT_SUCCESS;
252 }
253
254
255 int watch_props(Display *dpy, int argc, char** argv, char* n, char *desc)
256 {
257 XDevice *dev;
258 XDeviceInfo *info;
259 XEvent ev;
260 XDevicePropertyNotifyEvent *dpev;
261 char *name;
262 int type_prop;
263 XEventClass cls_prop;
264
265 if (list_props(dpy, argc, argv, n, desc) != EXIT_SUCCESS)
266 return EXIT_FAILURE;
267
268 info = find_device_info(dpy, argv[0], False);
269 if (!info)
270 {
271 fprintf(stderr, "unable to find device %s\n", argv[0]);
272 return EXIT_FAILURE;
273 }
274
275 dev = XOpenDevice(dpy, info->id);
276 if (!dev)
277 {
278 fprintf(stderr, "unable to open device '%s'\n", info->name);
279 return EXIT_FAILURE;
280 }
281
282 DevicePropertyNotify(dev, type_prop, cls_prop);
283 XSelectExtensionEvent(dpy, DefaultRootWindow(dpy), &cls_prop, 1);
284
285 while(1)
286 {
287 XNextEvent(dpy, &ev);
288
289 dpev = (XDevicePropertyNotifyEvent*)&ev;
290 if (dpev->type != type_prop)
291 continue;
292
293 name = XGetAtomName(dpy, dpev->atom);
294 printf("Property '%s' changed.\n", name);
295 print_property(dpy, dev, dpev->atom);
296 }
297
298 XCloseDevice(dpy, dev);
299 }