]> diplodocus.org Git - xorg-xinput/blob - src/property.c
Require inputproto 1.9.99.5
[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;
46
47 name = XGetAtomName(dpy, property);
48 printf("\t%s (%d):\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 ptr = data;
55 printf("\t");
56
57 for (j = 0; j < nitems; j++)
58 {
59 switch(act_type)
60 {
61 case XA_INTEGER:
62 switch(act_format)
63 {
64 case 8:
65 printf("%d", *((int8_t*)ptr));
66 break;
67 case 16:
68 printf("%d", *((int16_t*)ptr));
69 break;
70 case 32:
71 printf("%d", *((int32_t*)ptr));
72 break;
73 }
74 break;
75 case XA_STRING:
76 printf("\t%s\n", ptr);
77 break;
78 case XA_ATOM:
79 printf("\t%s\n", XGetAtomName(dpy, (Atom)(*ptr)));
80 break;
81 default:
82 printf("\t\t... of unknown type %s\n",
83 XGetAtomName(dpy, act_type));
84 break;
85 }
86
87 ptr += act_format/8;
88
89 if (j < nitems - 1)
90 printf(", ");
91 if (act_type == XA_STRING)
92 break;
93 }
94 printf("\n");
95 XFree(data);
96 } else
97 printf("\tFetch failure\n");
98
99 }
100
101 int list_props(Display *dpy, int argc, char** argv, char* name, char *desc)
102 {
103 XDeviceInfo *info;
104 XDevice *dev;
105 int i;
106 int nprops;
107 Atom *props;
108
109 if (argc == 0)
110 {
111 fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
112 return EXIT_FAILURE;
113 }
114
115 for (i = 0; i < argc; i++)
116 {
117 info = find_device_info(dpy, argv[i], False);
118 if (!info)
119 {
120 fprintf(stderr, "unable to find device %s\n", argv[i]);
121 continue;
122 }
123
124 dev = XOpenDevice(dpy, info->id);
125 if (!dev)
126 {
127 fprintf(stderr, "unable to open device '%s'\n", info->name);
128 continue;
129 }
130
131 props = XListDeviceProperties(dpy, dev, &nprops);
132 if (!nprops)
133 {
134 printf("Device '%s' does not report any properties.\n", info->name);
135 continue;
136 }
137
138 printf("Device '%s':\n", info->name);
139 while(nprops--)
140 {
141 print_property(dpy, dev, props[nprops]);
142 }
143
144 XFree(props);
145 XCloseDevice(dpy, dev);
146 }
147 return EXIT_SUCCESS;
148 }
149
150 int
151 set_int_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
152 {
153 XDeviceInfo *info;
154 XDevice *dev;
155 Atom prop;
156 char *name;
157 int i;
158 Bool is_atom = True;
159 char *data;
160 int format, nelements = 0;
161
162 if (argc < 3)
163 {
164 fprintf(stderr, "Usage: xinput %s %s\n", n, desc);
165 return EXIT_FAILURE;
166 }
167
168 info = find_device_info(dpy, argv[0], False);
169 if (!info)
170 {
171 fprintf(stderr, "unable to find device %s\n", argv[0]);
172 return EXIT_FAILURE;
173 }
174
175 dev = XOpenDevice(dpy, info->id);
176 if (!dev)
177 {
178 fprintf(stderr, "unable to open device %s\n", argv[0]);
179 return EXIT_FAILURE;
180 }
181
182 name = argv[1];
183
184 for(i = 0; i < strlen(name); i++) {
185 if (!isdigit(name[i])) {
186 is_atom = False;
187 break;
188 }
189 }
190
191 if (!is_atom)
192 prop = XInternAtom(dpy, name, False);
193 else
194 prop = atoi(name);
195
196 nelements = argc - 3;
197 format = atoi(argv[2]);
198 if (format != 8 && format != 16 && format != 32)
199 {
200 fprintf(stderr, "Invalid format %d\n", format);
201 return EXIT_FAILURE;
202 }
203
204 data = calloc(nelements, format/8);
205 for (i = 0; i < nelements; i++)
206 {
207 switch(format)
208 {
209 case 8:
210 *(((int8_t*)data) + i) = atoi(argv[3 + i]);
211 break;
212 case 16:
213 *(((int16_t*)data) + i) = atoi(argv[3 + i]);
214 break;
215 case 32:
216 *(((int32_t*)data) + i) = atoi(argv[3 + i]);
217 break;
218 }
219 }
220
221 XChangeDeviceProperty(dpy, dev, prop, XA_INTEGER, format, PropModeReplace,
222 (unsigned char*)data, nelements);
223
224 free(data);
225 XCloseDevice(dpy, dev);
226 return EXIT_SUCCESS;
227 }
228
229
230 int watch_props(Display *dpy, int argc, char** argv, char* n, char *desc)
231 {
232 XDevice *dev;
233 XDeviceInfo *info;
234 XEvent ev;
235 XDevicePropertyNotifyEvent *dpev;
236 char *name;
237 int type_prop;
238 XEventClass cls_prop;
239
240 if (list_props(dpy, argc, argv, n, desc) != EXIT_SUCCESS)
241 return EXIT_FAILURE;
242
243 info = find_device_info(dpy, argv[0], False);
244 if (!info)
245 {
246 fprintf(stderr, "unable to find device %s\n", argv[0]);
247 return EXIT_FAILURE;
248 }
249
250 dev = XOpenDevice(dpy, info->id);
251 if (!dev)
252 {
253 fprintf(stderr, "unable to open device '%s'\n", info->name);
254 return EXIT_FAILURE;
255 }
256
257 DevicePropertyNotify(dev, type_prop, cls_prop);
258 XSelectExtensionEvent(dpy, DefaultRootWindow(dpy), &cls_prop, 1);
259
260 while(1)
261 {
262 XNextEvent(dpy, &ev);
263
264 dpev = (XDevicePropertyNotifyEvent*)&ev;
265 if (dpev->type != type_prop)
266 continue;
267
268 name = XGetAtomName(dpy, dpev->atom);
269 printf("Property '%s' changed.\n", name);
270 print_property(dpy, dev, dpev->atom);
271 }
272
273 XCloseDevice(dpy, dev);
274 }
275
276 int delete_prop(Display *dpy, int argc, char** argv, char* n, char *desc)
277 {
278 XDevice *dev;
279 XDeviceInfo *info;
280 char *name;
281 int i;
282 Bool is_atom = True;
283 Atom prop;
284
285 info = find_device_info(dpy, argv[0], False);
286 if (!info)
287 {
288 fprintf(stderr, "unable to find device %s\n", argv[0]);
289 return EXIT_FAILURE;
290 }
291
292 dev = XOpenDevice(dpy, info->id);
293 if (!dev)
294 {
295 fprintf(stderr, "unable to open device '%s'\n", info->name);
296 return EXIT_FAILURE;
297 }
298
299 name = argv[1];
300
301 for(i = 0; i < strlen(name); i++) {
302 if (!isdigit(name[i])) {
303 is_atom = False;
304 break;
305 }
306 }
307
308 if (!is_atom)
309 prop = XInternAtom(dpy, name, False);
310 else
311 prop = atoi(name);
312
313 XDeleteDeviceProperty(dpy, dev, prop);
314
315 XCloseDevice(dpy, dev);
316 return EXIT_SUCCESS;
317 }