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>
37 static Atom
parse_atom(Display
*dpy
, char *name
) {
41 for (i
= 0; name
[i
] != '\0'; i
++) {
42 if (!isdigit(name
[i
])) {
51 return XInternAtom(dpy
, name
, False
);
55 print_property(Display
*dpy
, XDevice
* dev
, Atom property
)
60 unsigned long nitems
, bytes_after
;
61 unsigned char *data
, *ptr
;
62 int j
, done
= False
, size
;
64 name
= XGetAtomName(dpy
, property
);
65 printf("\t%s (%ld):\t", name
, property
);
67 if (XGetDeviceProperty(dpy
, dev
, property
, 0, 1000, False
,
68 AnyPropertyType
, &act_type
, &act_format
,
69 &nitems
, &bytes_after
, &data
) == Success
)
71 Atom float_atom
= XInternAtom(dpy
, "FLOAT", True
);
80 case 8: size
= sizeof(char); break;
81 case 16: size
= sizeof(short); break;
82 case 32: size
= sizeof(long); break;
85 for (j
= 0; j
< nitems
; j
++)
93 printf("%d", *((char*)ptr
));
96 printf("%d", *((short*)ptr
));
99 printf("%ld", *((long*)ptr
));
106 printf("Unknown string format.\n");
110 printf("\"%s\"", ptr
);
111 j
+= strlen((char*)ptr
); /* The loop's j++ jumps over the
113 ptr
+= strlen((char*)ptr
); /* ptr += size below jumps over
118 Atom a
= *(Atom
*)ptr
;
119 printf("\"%s\" (%d)",
120 (a
) ? XGetAtomName(dpy
, a
) : "None",
125 if (float_atom
!= None
&& act_type
== float_atom
)
127 printf("%f", *((float*)ptr
));
131 printf("\t... of unknown type %s\n",
132 XGetAtomName(dpy
, act_type
));
147 printf("\tFetch failure\n");
152 list_props_xi1(Display
*dpy
, int argc
, char** argv
, char* name
, char *desc
)
162 fprintf(stderr
, "Usage: xinput %s %s\n", name
, desc
);
166 for (i
= 0; i
< argc
; i
++)
168 info
= find_device_info(dpy
, argv
[i
], False
);
171 fprintf(stderr
, "unable to find device %s\n", argv
[i
]);
175 dev
= XOpenDevice(dpy
, info
->id
);
178 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
182 props
= XListDeviceProperties(dpy
, dev
, &nprops
);
185 printf("Device '%s' does not report any properties.\n", info
->name
);
189 printf("Device '%s':\n", info
->name
);
192 print_property(dpy
, dev
, props
[nprops
]);
196 XCloseDevice(dpy
, dev
);
202 int watch_props(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
207 XDevicePropertyNotifyEvent
*dpev
;
210 XEventClass cls_prop
;
212 if (list_props(dpy
, argc
, argv
, n
, desc
) != EXIT_SUCCESS
)
215 info
= find_device_info(dpy
, argv
[0], False
);
218 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
222 dev
= XOpenDevice(dpy
, info
->id
);
225 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
229 DevicePropertyNotify(dev
, type_prop
, cls_prop
);
230 XSelectExtensionEvent(dpy
, DefaultRootWindow(dpy
), &cls_prop
, 1);
234 XNextEvent(dpy
, &ev
);
236 dpev
= (XDevicePropertyNotifyEvent
*)&ev
;
237 if (dpev
->type
!= type_prop
)
240 name
= XGetAtomName(dpy
, dpev
->atom
);
241 printf("Property '%s' changed.\n", name
);
242 print_property(dpy
, dev
, dpev
->atom
);
245 XCloseDevice(dpy
, dev
);
249 delete_prop_xi1(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
256 info
= find_device_info(dpy
, argv
[0], False
);
259 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
263 dev
= XOpenDevice(dpy
, info
->id
);
266 fprintf(stderr
, "unable to open device '%s'\n", info
->name
);
272 prop
= parse_atom(dpy
, name
);
274 XDeleteDeviceProperty(dpy
, dev
, prop
);
276 XCloseDevice(dpy
, dev
);
281 do_set_prop_xi1(Display
*dpy
, Atom type
, int format
, int argc
, char **argv
, char *n
, char *desc
)
290 int old_format
, nelements
= 0;
291 unsigned long act_nitems
, bytes_after
;
302 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
306 info
= find_device_info(dpy
, argv
[0], False
);
309 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
313 dev
= XOpenDevice(dpy
, info
->id
);
316 fprintf(stderr
, "unable to open device %s\n", argv
[0]);
322 prop
= parse_atom(dpy
, name
);
325 fprintf(stderr
, "invalid property %s\n", name
);
329 float_atom
= XInternAtom(dpy
, "FLOAT", False
);
331 nelements
= argc
- 2;
332 if (type
== None
|| format
== 0) {
333 if (XGetDeviceProperty(dpy
, dev
, prop
, 0, 0, False
, AnyPropertyType
,
334 &old_type
, &old_format
, &act_nitems
,
335 &bytes_after
, &data
.c
) != Success
) {
336 fprintf(stderr
, "failed to get property type and format for %s\n",
350 fprintf(stderr
, "property %s doesn't exist, you need to specify "
351 "its type and format\n", name
);
355 data
.c
= calloc(nelements
, sizeof(long));
357 for (i
= 0; i
< nelements
; i
++)
359 if (type
== XA_INTEGER
) {
363 data
.c
[i
] = atoi(argv
[2 + i
]);
366 data
.s
[i
] = atoi(argv
[2 + i
]);
369 data
.l
[i
] = atoi(argv
[2 + i
]);
372 fprintf(stderr
, "unexpected size for property %s", name
);
375 } else if (type
== float_atom
) {
377 fprintf(stderr
, "unexpected format %d for property %s\n",
381 *(float *)(data
.l
+ i
) = strtod(argv
[2 + i
], &endptr
);
382 if (endptr
== argv
[2 + i
]) {
383 fprintf(stderr
, "argument %s could not be parsed\n", argv
[2 + i
]);
386 } else if (type
== XA_ATOM
) {
388 fprintf(stderr
, "unexpected format %d for property %s\n",
392 data
.a
[i
] = parse_atom(dpy
, argv
[2 + i
]);
394 fprintf(stderr
, "unexpected type for property %s\n", name
);
399 XChangeDeviceProperty(dpy
, dev
, prop
, type
, format
, PropModeReplace
,
402 XCloseDevice(dpy
, dev
);
408 print_property_xi2(Display
*dpy
, int deviceid
, Atom property
)
413 unsigned long nitems
, bytes_after
;
414 unsigned char *data
, *ptr
;
417 name
= XGetAtomName(dpy
, property
);
418 printf("\t%s (%ld):\t", name
, property
);
420 if (XIGetProperty(dpy
, deviceid
, property
, 0, 1000, False
,
421 AnyPropertyType
, &act_type
, &act_format
,
422 &nitems
, &bytes_after
, &data
) == Success
)
424 Atom float_atom
= XInternAtom(dpy
, "FLOAT", True
);
429 printf("<no items>");
431 for (j
= 0; j
< nitems
; j
++)
439 printf("%d", *((int8_t*)ptr
));
442 printf("%d", *((int16_t*)ptr
));
445 printf("%d", *((int32_t*)ptr
));
452 printf("Unknown string format.\n");
456 printf("\"%s\"", ptr
);
457 j
+= strlen((char*)ptr
); /* The loop's j++ jumps over the
459 ptr
+= strlen((char*)ptr
); /* ptr += size below jumps over
464 Atom a
= *(Atom
*)ptr
;
465 printf("\"%s\" (%d)",
466 (a
) ? XGetAtomName(dpy
, a
) : "None",
472 if (float_atom
!= None
&& act_type
== float_atom
)
474 printf("%f", *((float*)ptr
));
478 printf("\t... of unknown type %s\n",
479 XGetAtomName(dpy
, act_type
));
494 printf("\tFetch failure\n");
499 list_props_xi2(Display
*dpy
, int argc
, char** argv
, char* name
, char *desc
)
508 fprintf(stderr
, "Usage: xinput %s %s\n", name
, desc
);
512 for (i
= 0; i
< argc
; i
++)
514 info
= xi2_find_device_info(dpy
, argv
[i
]);
517 fprintf(stderr
, "unable to find device %s\n", argv
[i
]);
521 props
= XIListProperties(dpy
, info
->deviceid
, &nprops
);
524 printf("Device '%s' does not report any properties.\n", info
->name
);
528 printf("Device '%s':\n", info
->name
);
531 print_property_xi2(dpy
, info
->deviceid
, props
[nprops
]);
540 delete_prop_xi2(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
546 info
= xi2_find_device_info(dpy
, argv
[0]);
549 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
555 prop
= parse_atom(dpy
, name
);
557 XIDeleteProperty(dpy
, info
->deviceid
, prop
);
563 do_set_prop_xi2(Display
*dpy
, Atom type
, int format
, int argc
, char **argv
, char *n
, char *desc
)
571 int old_format
, nelements
= 0;
572 unsigned long act_nitems
, bytes_after
;
582 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
586 info
= xi2_find_device_info(dpy
, argv
[0]);
589 fprintf(stderr
, "unable to find device %s\n", argv
[0]);
595 prop
= parse_atom(dpy
, name
);
598 fprintf(stderr
, "invalid property %s\n", name
);
602 float_atom
= XInternAtom(dpy
, "FLOAT", False
);
604 nelements
= argc
- 2;
605 if (type
== None
|| format
== 0) {
606 if (XIGetProperty(dpy
, info
->deviceid
, prop
, 0, 0, False
,
607 AnyPropertyType
, &old_type
, &old_format
, &act_nitems
,
608 &bytes_after
, &data
.c
) != Success
) {
609 fprintf(stderr
, "failed to get property type and format for %s\n",
623 fprintf(stderr
, "property %s doesn't exist, you need to specify "
624 "its type and format\n", name
);
628 data
.c
= calloc(nelements
, sizeof(int32_t));
630 for (i
= 0; i
< nelements
; i
++)
632 if (type
== XA_INTEGER
) {
636 data
.c
[i
] = atoi(argv
[2 + i
]);
639 data
.s
[i
] = atoi(argv
[2 + i
]);
642 data
.l
[i
] = atoi(argv
[2 + i
]);
645 fprintf(stderr
, "unexpected size for property %s", name
);
648 } else if (type
== float_atom
) {
650 fprintf(stderr
, "unexpected format %d for property %s\n",
654 *(float *)(data
.l
+ i
) = strtod(argv
[2 + i
], &endptr
);
655 if (endptr
== argv
[2 + i
]) {
656 fprintf(stderr
, "argument %s could not be parsed\n", argv
[2 + i
]);
659 } else if (type
== XA_ATOM
) {
661 fprintf(stderr
, "unexpected format %d for property %s\n",
665 data
.l
[i
] = parse_atom(dpy
, argv
[2 + i
]);
667 fprintf(stderr
, "unexpected type for property %s\n", name
);
672 XIChangeProperty(dpy
, info
->deviceid
, prop
, type
, format
, PropModeReplace
,
679 int list_props(Display
*display
, int argc
, char *argv
[], char *name
,
683 if (xinput_version(display
) == XI_2_Major
)
684 return list_props_xi2(display
, argc
, argv
, name
, desc
);
686 return list_props_xi1(display
, argc
, argv
, name
, desc
);
690 int delete_prop(Display
*display
, int argc
, char *argv
[], char *name
,
694 if (xinput_version(display
) == XI_2_Major
)
695 return delete_prop_xi2(display
, argc
, argv
, name
, desc
);
697 return delete_prop_xi1(display
, argc
, argv
, name
, desc
);
702 do_set_prop(Display
*display
, Atom type
, int format
, int argc
, char *argv
[], char *name
, char *desc
)
705 if (xinput_version(display
) == XI_2_Major
)
706 return do_set_prop_xi2(display
, type
, format
, argc
, argv
, name
, desc
);
708 return do_set_prop_xi1(display
, type
, format
, argc
, argv
, name
, desc
);
712 set_atom_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
714 return do_set_prop(dpy
, XA_ATOM
, 32, argc
, argv
, n
, desc
);
718 set_int_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
725 fprintf(stderr
, "Usage: xinput %s %s\n", n
, desc
);
729 format
= atoi(argv
[2]);
730 if (format
!= 8 && format
!= 16 && format
!= 32)
732 fprintf(stderr
, "Invalid format %d\n", format
);
736 for (i
= 3; i
< argc
; i
++)
737 argv
[i
- 1] = argv
[i
];
739 return do_set_prop(dpy
, XA_INTEGER
, format
, argc
- 1, argv
, n
, desc
);
743 set_float_prop(Display
*dpy
, int argc
, char** argv
, char* n
, char *desc
)
745 Atom float_atom
= XInternAtom(dpy
, "FLOAT", False
);
747 if (sizeof(float) != 4)
749 fprintf(stderr
, "sane FP required\n");
753 return do_set_prop(dpy
, float_atom
, 32, argc
, argv
, n
, desc
);
756 int set_prop(Display
*display
, int argc
, char *argv
[], char *name
,
764 char *option
= strchr(argv
[i
], '=');
765 /* skip non-option arguments */
766 if (strncmp(argv
[i
], "--", 2) || !option
) {
771 if (!strncmp(argv
[i
], "--type=", strlen("--type="))) {
772 if (!strcmp(option
+ 1, "int")) {
774 } else if (!strcmp(option
+ 1, "float")) {
775 type
= XInternAtom(display
, "FLOAT", False
);
777 } else if (!strcmp(option
+ 1, "atom")) {
781 fprintf(stderr
, "unknown property type %s\n", option
+ 1);
784 } else if (!strncmp(argv
[i
], "--format=", strlen("--format="))) {
785 format
= atoi(option
+ 1);
786 if (format
!= 8 && format
!= 16 && format
!= 32) {
787 fprintf(stderr
, "invalid property format %s\n", option
+ 1);
791 fprintf(stderr
, "invalid option %s\n", argv
[i
]);
795 for (j
= i
; j
+ 1 < argc
; j
++)
796 argv
[j
] = argv
[j
+ 1];
800 return do_set_prop(display
, type
, format
, argc
, argv
, name
, desc
);