2 * Copyright 2007 Peter Hutterer
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
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
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.
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
32 #include <X11/Xatom.h>
33 #include <X11/extensions/XIproto.h>
38 print_property(Display
*dpy
, XDevice
* dev
, Atom property
)
43 unsigned long nitems
, bytes_after
;
44 unsigned char *data
, *ptr
;
47 name
= XGetAtomName(dpy
, property
);
48 printf("\t%s (%ld):\t", name
, property
);
50 if (XGetDeviceProperty(dpy
, dev
, property
, 0, 1000, False
,
51 AnyPropertyType
, &act_type
, &act_format
,
52 &nitems
, &bytes_after
, &data
) == Success
)
54 int float_atom
= XInternAtom(dpy
, "FLOAT", False
);
58 for (j
= 0; j
< nitems
; j
++)
66 printf("%d", *((int8_t*)ptr
));
69 printf("%d", *((int16_t*)ptr
));
72 printf("%ld", *((long*)ptr
));
77 printf("\"%s\"", ptr
);
81 printf("\"%s\"", XGetAtomName(dpy
, *(Atom
*)ptr
));
84 if (float_atom
!= None
&& act_type
== float_atom
)
86 printf("%f", *((float*)ptr
));
90 printf("\t... of unknown type %s\n",
91 XGetAtomName(dpy
, act_type
));
106 printf("\tFetch failure\n");
110 int list_props(Display
*dpy
, int argc
, char** argv
, char* name
, char *desc
)
120 fprintf(stderr
, "Usage: xinput %s %s\n", name
, desc
);
124 for (i
= 0; i
< argc
; i
++)
126 info
= find_device_info(dpy
, argv
[i
], False
);
129 fprintf(stderr
, "unable to find device %s\n", argv
[i
]);
133 dev
= XOpenDevice(dpy
, info
->id
);
136 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
140 props
= XListDeviceProperties(dpy
, dev
, &nprops
);
143 printf("Device '%s' does not report any properties.\n", info
->name
);
147 printf("Device '%s':\n", info
->name
);
150 print_property(dpy
, dev
, props
[nprops
]);
154 XCloseDevice(dpy
, dev
);
160 set_int_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
169 int format
, nelements
= 0;
173 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
177 info
= find_device_info(dpy
, argv
[0], False
);
180 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
184 dev
= XOpenDevice(dpy
, info
->id
);
187 fprintf(stderr
, "unable to open device %s\n", argv
[0]);
193 for(i
= 0; i
< strlen(name
); i
++) {
194 if (!isdigit(name
[i
])) {
201 prop
= XInternAtom(dpy
, name
, False
);
205 nelements
= argc
- 3;
206 format
= atoi(argv
[2]);
207 if (format
!= 8 && format
!= 16 && format
!= 32)
209 fprintf(stderr
, "Invalid format %d\n", format
);
213 data
= calloc(nelements
, format
/8);
214 for (i
= 0; i
< nelements
; i
++)
219 *(((int8_t*)data
) + i
) = atoi(argv
[3 + i
]);
222 *(((int16_t*)data
) + i
) = atoi(argv
[3 + i
]);
225 *(((long*)data
) + i
) = atoi(argv
[3 + i
]);
230 XChangeDeviceProperty(dpy
, dev
, prop
, XA_INTEGER
, format
, PropModeReplace
,
231 (unsigned char*)data
, nelements
);
234 XCloseDevice(dpy
, dev
);
239 set_float_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
243 Atom prop
, float_atom
;
253 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
257 info
= find_device_info(dpy
, argv
[0], False
);
260 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
264 dev
= XOpenDevice(dpy
, info
->id
);
267 fprintf(stderr
, "unable to open device %s\n", argv
[0]);
273 for(i
= 0; i
< strlen(name
); i
++) {
274 if (!isdigit(name
[i
])) {
281 prop
= XInternAtom(dpy
, name
, False
);
285 nelements
= argc
- 2;
287 float_atom
= XInternAtom(dpy
, "FLOAT", False
);
289 if (float_atom
== (Atom
)0)
291 fprintf(stderr
, "no FLOAT atom present in server\n");
295 if (sizeof(float) != 4)
297 fprintf(stderr
, "sane FP required\n");
301 data
= calloc(nelements
, 4);
302 for (i
= 0; i
< nelements
; i
++)
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
]);
311 XChangeDeviceProperty(dpy
, dev
, prop
, float_atom
, 32, PropModeReplace
,
312 (unsigned char*)data
, nelements
);
315 XCloseDevice(dpy
, dev
);
320 int watch_props(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
325 XDevicePropertyNotifyEvent
*dpev
;
328 XEventClass cls_prop
;
330 if (list_props(dpy
, argc
, argv
, n
, desc
) != EXIT_SUCCESS
)
333 info
= find_device_info(dpy
, argv
[0], False
);
336 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
340 dev
= XOpenDevice(dpy
, info
->id
);
343 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
347 DevicePropertyNotify(dev
, type_prop
, cls_prop
);
348 XSelectExtensionEvent(dpy
, DefaultRootWindow(dpy
), &cls_prop
, 1);
352 XNextEvent(dpy
, &ev
);
354 dpev
= (XDevicePropertyNotifyEvent
*)&ev
;
355 if (dpev
->type
!= type_prop
)
358 name
= XGetAtomName(dpy
, dpev
->atom
);
359 printf("Property '%s' changed.\n", name
);
360 print_property(dpy
, dev
, dpev
->atom
);
363 XCloseDevice(dpy
, dev
);
366 int delete_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
375 info
= find_device_info(dpy
, argv
[0], False
);
378 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
382 dev
= XOpenDevice(dpy
, info
->id
);
385 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
391 for(i
= 0; i
< strlen(name
); i
++) {
392 if (!isdigit(name
[i
])) {
399 prop
= XInternAtom(dpy
, name
, False
);
403 XDeleteDeviceProperty(dpy
, dev
, prop
);
405 XCloseDevice(dpy
, dev
);
410 set_atom_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
423 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
427 info
= find_device_info(dpy
, argv
[0], False
);
430 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
434 dev
= XOpenDevice(dpy
, info
->id
);
437 fprintf(stderr
, "unable to open device %s\n", argv
[0]);
443 for(i
= 0; i
< strlen(name
); i
++) {
444 if (!isdigit(name
[i
])) {
451 prop
= XInternAtom(dpy
, name
, False
);
455 nelements
= argc
- 2;
456 data
= calloc(nelements
, sizeof(Atom
));
457 for (i
= 0; i
< nelements
; i
++)
461 for(j
= 0; j
< strlen(name
); j
++) {
462 if (!isdigit(name
[j
])) {
469 data
[i
] = XInternAtom(dpy
, name
, False
);
472 data
[i
] = atoi(name
);
473 XFree(XGetAtomName(dpy
, data
[i
]));
477 XChangeDeviceProperty(dpy
, dev
, prop
, XA_ATOM
, 32, PropModeReplace
,
478 (unsigned char*)data
, nelements
);
481 XCloseDevice(dpy
, dev
);