]> diplodocus.org Git - xorg-xinput/blob - src/property.c
9c5e86b07bd47c8ff5bdd2e4ea21b80675a17abd
[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 Atom act_type;
41 char *name;
42 int act_format;
43 unsigned long nitems, bytes_after;
44 unsigned char *data, *ptr;
45 int j, done = False;
46
47 name = XGetAtomName(dpy, property);
48 printf("\t%s (%ld):\t", name, property);
49
50 if (XGetDeviceProperty(dpy, dev, property, 0, 1000, False,
51 AnyPropertyType, &act_type, &act_format,
52 &nitems, &bytes_after, &data) == Success)
53 {
54 int float_atom = XInternAtom(dpy, "FLOAT", False);
55
56 ptr = data;
57
58 for (j = 0; j < nitems; j++)
59 {
60 switch(act_type)
61 {
62 case XA_INTEGER:
63 switch(act_format)
64 {
65 case 8:
66 printf("%d", *((int8_t*)ptr));
67 break;
68 case 16:
69 printf("%d", *((int16_t*)ptr));
70 break;
71 case 32:
72 printf("%ld", *((long*)ptr));
73 break;
74 }
75 break;
76 case XA_STRING:
77 printf("\"%s\"", ptr);
78 done = True;
79 break;
80 case XA_ATOM:
81 printf("\"%s\"", XGetAtomName(dpy, *(Atom*)ptr));
82 break;
83 default:
84 if (float_atom != None && act_type == float_atom)
85 {
86 printf("%f", *((float*)ptr));
87 break;
88 }
89
90 printf("\t... of unknown type %s\n",
91 XGetAtomName(dpy, act_type));
92 done = True;
93 break;
94 }
95
96 ptr += act_format/8;
97
98 if (j < nitems - 1)
99 printf(", ");
100 if (done == True)
101 break;
102 }
103 printf("\n");
104 XFree(data);
105 } else
106 printf("\tFetch failure\n");
107
108 }
109
110 int list_props(Display *dpy, int argc, char** argv, char* name, char *desc)
111 {
112 XDeviceInfo *info;
113 XDevice *dev;
114 int i;
115 int nprops;
116 Atom *props;
117
118 if (argc == 0)
119 {
120 fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
121 return EXIT_FAILURE;
122 }
123
124 for (i = 0; i < argc; i++)
125 {
126 info = find_device_info(dpy, argv[i], False);
127 if (!info)
128 {
129 fprintf(stderr, "unable to find device %s\n", argv[i]);
130 continue;
131 }
132
133 dev = XOpenDevice(dpy, info->id);
134 if (!dev)
135 {
136 fprintf(stderr, "unable to open device '%s'\n", info->name);
137 continue;
138 }
139
140 props = XListDeviceProperties(dpy, dev, &nprops);
141 if (!nprops)
142 {
143 printf("Device '%s' does not report any properties.\n", info->name);
144 continue;
145 }
146
147 printf("Device '%s':\n", info->name);
148 while(nprops--)
149 {
150 print_property(dpy, dev, props[nprops]);
151 }
152
153 XFree(props);
154 XCloseDevice(dpy, dev);
155 }
156 return EXIT_SUCCESS;
157 }
158
159 int
160 set_int_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
161 {
162 XDeviceInfo *info;
163 XDevice *dev;
164 Atom prop;
165 char *name;
166 int i;
167 Bool is_atom = True;
168 char *data;
169 int format, nelements = 0;
170
171 if (argc < 3)
172 {
173 fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
174 return EXIT_FAILURE;
175 }
176
177 info = find_device_info(dpy, argv[0], False);
178 if (!info)
179 {
180 fprintf(stderr, "unable to find device %s\n", argv[0]);
181 return EXIT_FAILURE;
182 }
183
184 dev = XOpenDevice(dpy, info->id);
185 if (!dev)
186 {
187 fprintf(stderr, "unable to open device %s\n", argv[0]);
188 return EXIT_FAILURE;
189 }
190
191 name = argv[1];
192
193 for(i = 0; i < strlen(name); i++) {
194 if (!isdigit(name[i])) {
195 is_atom = False;
196 break;
197 }
198 }
199
200 if (!is_atom)
201 prop = XInternAtom(dpy, name, False);
202 else
203 prop = atoi(name);
204
205 nelements = argc - 3;
206 format = atoi(argv[2]);
207 if (format != 8 && format != 16 && format != 32)
208 {
209 fprintf(stderr, "Invalid format %d\n", format);
210 return EXIT_FAILURE;
211 }
212
213 data = calloc(nelements, format/8);
214 for (i = 0; i < nelements; i++)
215 {
216 switch(format)
217 {
218 case 8:
219 *(((int8_t*)data) + i) = atoi(argv[3 + i]);
220 break;
221 case 16:
222 *(((int16_t*)data) + i) = atoi(argv[3 + i]);
223 break;
224 case 32:
225 *(((long*)data) + i) = atoi(argv[3 + i]);
226 break;
227 }
228 }
229
230 XChangeDeviceProperty(dpy, dev, prop, XA_INTEGER, format, PropModeReplace,
231 (unsigned char*)data, nelements);
232
233 free(data);
234 XCloseDevice(dpy, dev);
235 return EXIT_SUCCESS;
236 }
237
238 int
239 set_float_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
240 {
241 XDeviceInfo *info;
242 XDevice *dev;
243 Atom prop, float_atom;
244 char *name;
245 int i;
246 Bool is_atom = True;
247 float *data;
248 int nelements = 0;
249 char* endptr;
250
251 if (argc < 2)
252 {
253 fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
254 return EXIT_FAILURE;
255 }
256
257 info = find_device_info(dpy, argv[0], False);
258 if (!info)
259 {
260 fprintf(stderr, "unable to find device %s\n", argv[0]);
261 return EXIT_FAILURE;
262 }
263
264 dev = XOpenDevice(dpy, info->id);
265 if (!dev)
266 {
267 fprintf(stderr, "unable to open device %s\n", argv[0]);
268 return EXIT_FAILURE;
269 }
270
271 name = argv[1];
272
273 for(i = 0; i < strlen(name); i++) {
274 if (!isdigit(name[i])) {
275 is_atom = False;
276 break;
277 }
278 }
279
280 if (!is_atom)
281 prop = XInternAtom(dpy, name, False);
282 else
283 prop = atoi(name);
284
285 nelements = argc - 2;
286
287 float_atom = XInternAtom(dpy, "FLOAT", False);
288
289 if (float_atom == (Atom)0)
290 {
291 fprintf(stderr, "no FLOAT atom present in server\n");
292 return EXIT_FAILURE;
293 }
294
295 if (sizeof(float) != 4)
296 {
297 fprintf(stderr, "sane FP required\n");
298 return EXIT_FAILURE;
299 }
300
301 data = calloc(nelements, 4);
302 for (i = 0; i < nelements; i++)
303 {
304 *(data + i) = strtod(argv[2 + i], &endptr);
305 if(endptr == argv[2 + i]){
306 fprintf(stderr, "argument %s could not be parsed\n", argv[2 + i]);
307 return EXIT_FAILURE;
308 }
309 }
310
311 XChangeDeviceProperty(dpy, dev, prop, float_atom, 32, PropModeReplace,
312 (unsigned char*)data, nelements);
313
314 free(data);
315 XCloseDevice(dpy, dev);
316 return EXIT_SUCCESS;
317 }
318
319
320 int watch_props(Display *dpy, int argc, char** argv, char* n, char *desc)
321 {
322 XDevice *dev;
323 XDeviceInfo *info;
324 XEvent ev;
325 XDevicePropertyNotifyEvent *dpev;
326 char *name;
327 int type_prop;
328 XEventClass cls_prop;
329
330 if (list_props(dpy, argc, argv, n, desc) != EXIT_SUCCESS)
331 return EXIT_FAILURE;
332
333 info = find_device_info(dpy, argv[0], False);
334 if (!info)
335 {
336 fprintf(stderr, "unable to find device %s\n", argv[0]);
337 return EXIT_FAILURE;
338 }
339
340 dev = XOpenDevice(dpy, info->id);
341 if (!dev)
342 {
343 fprintf(stderr, "unable to open device '%s'\n", info->name);
344 return EXIT_FAILURE;
345 }
346
347 DevicePropertyNotify(dev, type_prop, cls_prop);
348 XSelectExtensionEvent(dpy, DefaultRootWindow(dpy), &cls_prop, 1);
349
350 while(1)
351 {
352 XNextEvent(dpy, &ev);
353
354 dpev = (XDevicePropertyNotifyEvent*)&ev;
355 if (dpev->type != type_prop)
356 continue;
357
358 name = XGetAtomName(dpy, dpev->atom);
359 printf("Property '%s' changed.\n", name);
360 print_property(dpy, dev, dpev->atom);
361 }
362
363 XCloseDevice(dpy, dev);
364 }
365
366 int delete_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
367 {
368 XDevice *dev;
369 XDeviceInfo *info;
370 char *name;
371 int i;
372 Bool is_atom = True;
373 Atom prop;
374
375 info = find_device_info(dpy, argv[0], False);
376 if (!info)
377 {
378 fprintf(stderr, "unable to find device %s\n", argv[0]);
379 return EXIT_FAILURE;
380 }
381
382 dev = XOpenDevice(dpy, info->id);
383 if (!dev)
384 {
385 fprintf(stderr, "unable to open device '%s'\n", info->name);
386 return EXIT_FAILURE;
387 }
388
389 name = argv[1];
390
391 for(i = 0; i < strlen(name); i++) {
392 if (!isdigit(name[i])) {
393 is_atom = False;
394 break;
395 }
396 }
397
398 if (!is_atom)
399 prop = XInternAtom(dpy, name, False);
400 else
401 prop = atoi(name);
402
403 XDeleteDeviceProperty(dpy, dev, prop);
404
405 XCloseDevice(dpy, dev);
406 return EXIT_SUCCESS;
407 }
408
409 int
410 set_atom_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
411 {
412 XDeviceInfo *info;
413 XDevice *dev;
414 Atom prop;
415 char *name;
416 int i, j;
417 Bool is_atom = True;
418 Atom *data;
419 int nelements = 0;
420
421 if (argc < 3)
422 {
423 fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
424 return EXIT_FAILURE;
425 }
426
427 info = find_device_info(dpy, argv[0], False);
428 if (!info)
429 {
430 fprintf(stderr, "unable to find device %s\n", argv[0]);
431 return EXIT_FAILURE;
432 }
433
434 dev = XOpenDevice(dpy, info->id);
435 if (!dev)
436 {
437 fprintf(stderr, "unable to open device %s\n", argv[0]);
438 return EXIT_FAILURE;
439 }
440
441 name = argv[1];
442
443 for(i = 0; i < strlen(name); i++) {
444 if (!isdigit(name[i])) {
445 is_atom = False;
446 break;
447 }
448 }
449
450 if (!is_atom)
451 prop = XInternAtom(dpy, name, False);
452 else
453 prop = atoi(name);
454
455 nelements = argc - 2;
456 data = calloc(nelements, sizeof(Atom));
457 for (i = 0; i < nelements; i++)
458 {
459 is_atom = True;
460 name = argv[2 + i];
461 for(j = 0; j < strlen(name); j++) {
462 if (!isdigit(name[j])) {
463 is_atom = False;
464 break;
465 }
466 }
467
468 if (!is_atom)
469 data[i] = XInternAtom(dpy, name, False);
470 else
471 {
472 data[i] = atoi(name);
473 XFree(XGetAtomName(dpy, data[i]));
474 }
475 }
476
477 XChangeDeviceProperty(dpy, dev, prop, XA_ATOM, 32, PropModeReplace,
478 (unsigned char*)data, nelements);
479
480 free(data);
481 XCloseDevice(dpy, dev);
482 return EXIT_SUCCESS;
483 }
484
485