]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
64b48874e2c82ff4d3df308c06d63425d721c843
[xorg-xinput] / src / xinput.c
1 /*
2 * Copyright 1996 by Frederic Lepied, France. <Frederic.Lepied@sugix.frmug.org>
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, and that the name of the authors not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. The authors make no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
13 *
14 * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
21 *
22 */
23
24 #include "xinput.h"
25 #include <ctype.h>
26 #include <string.h>
27
28 int xi_opcode;
29
30 typedef int (*prog)(Display* display, int argc, char *argv[],
31 char *prog_name, char *prog_desc);
32
33 typedef struct
34 {
35 char *func_name;
36 char *arg_desc;
37 prog func;
38 } entry;
39
40 static entry drivers[] =
41 {
42 {"get-feedbacks",
43 "<device name>",
44 get_feedbacks
45 },
46 {"set-ptr-feedback",
47 "<device name> <threshold> <num> <denom>",
48 set_ptr_feedback
49 },
50 {"set-integer-feedback",
51 "<device name> <feedback id> <value>",
52 set_integer_feedback
53 },
54 {"get-button-map",
55 "<device name>",
56 get_button_map
57 },
58 {"set-button-map",
59 "<device name> <map button 1> [<map button 2> [...]]",
60 set_button_map
61 },
62 {"set-pointer",
63 "<device name> [<x index> <y index>]",
64 set_pointer
65 },
66 {"set-mode",
67 "<device name> ABSOLUTE|RELATIVE",
68 set_mode
69 },
70 {"list",
71 "[--short || --long || --name-only || --id-only] [<device name>...]",
72 list
73 },
74 {"query-state",
75 "<device name>",
76 query_state
77 },
78 {"test",
79 "[-proximity] <device name>",
80 test
81 },
82 #if HAVE_XI2
83 { "create-master",
84 "<id> [<sendCore (dflt:1)>] [<enable (dflt:1)>]",
85 create_master
86 },
87 { "remove-master",
88 "<id> [Floating|AttachToMaster (dflt:Floating)] [<returnPointer>] [<returnKeyboard>]",
89 remove_master
90 },
91 { "reattach",
92 "<id> <master>",
93 change_attachment
94 },
95 { "float",
96 "<id>",
97 float_device
98 },
99 { "set-cp",
100 "<window> <device>",
101 set_clientpointer
102 },
103 { "test-xi2",
104 "<device>",
105 test_xi2,
106 },
107 #endif
108 { "list-props",
109 "<device> [<device> ...]",
110 list_props
111 },
112 { "set-int-prop",
113 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
114 set_int_prop
115 },
116 { "set-float-prop",
117 "<device> <property> <val> [<val> ...]",
118 set_float_prop
119 },
120 { "set-atom-prop",
121 "<device> <property> <val> [<val> ...]",
122 set_atom_prop
123 },
124 { "watch-props",
125 "<device>",
126 watch_props
127 },
128 { "delete-prop",
129 "<device> <property>",
130 delete_prop
131 },
132 { "set-prop",
133 "<device> [--type=atom|float|int] [--format=8|16|32] <property> <val> [<val> ...]",
134 set_prop
135 },
136 {NULL, NULL, NULL
137 }
138 };
139
140 static const char version_id[] = VERSION;
141
142 int
143 print_version()
144 {
145 XExtensionVersion *version;
146 Display *display;
147
148 printf("xinput version %s\n", version_id);
149
150 display = XOpenDisplay(NULL);
151
152 printf("XI version on server: ");
153
154 if (display == NULL)
155 printf("Failed to open display.\n");
156 else {
157 version = XGetExtensionVersion(display, INAME);
158 if (!version || (version == (XExtensionVersion*) NoSuchExtension))
159 printf(" Extension not supported.\n");
160 else {
161 printf("%d.%d\n", version->major_version,
162 version->minor_version);
163 XFree(version);
164 return 0;
165 }
166 }
167
168 return 1;
169 }
170
171 int
172 xinput_version(Display *display)
173 {
174 XExtensionVersion *version;
175 static int vers = -1;
176
177 if (vers != -1)
178 return vers;
179
180 version = XGetExtensionVersion(display, INAME);
181
182 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
183 vers = version->major_version;
184 XFree(version);
185 }
186
187 #if HAVE_XI2
188 /* Announce our supported version so the server treats us correctly. */
189 if (vers >= XI_2_Major)
190 {
191 int maj = 2,
192 min = 0;
193 XIQueryVersion(display, &maj, &min);
194 }
195 #endif
196
197 return vers;
198 }
199
200 XDeviceInfo*
201 find_device_info(Display *display,
202 char *name,
203 Bool only_extended)
204 {
205 XDeviceInfo *devices;
206 XDeviceInfo *found = NULL;
207 int loop;
208 int num_devices;
209 int len = strlen(name);
210 Bool is_id = True;
211 XID id = (XID)-1;
212
213 for(loop=0; loop<len; loop++) {
214 if (!isdigit(name[loop])) {
215 is_id = False;
216 break;
217 }
218 }
219
220 if (is_id) {
221 id = atoi(name);
222 }
223
224 devices = XListInputDevices(display, &num_devices);
225
226 for(loop=0; loop<num_devices; loop++) {
227 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
228 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
229 (is_id && devices[loop].id == id))) {
230 if (found) {
231 fprintf(stderr,
232 "Warning: There are multiple devices named \"%s\".\n"
233 "To ensure the correct one is selected, please use "
234 "the device ID instead.\n\n", name);
235 return NULL;
236 } else {
237 found = &devices[loop];
238 }
239 }
240 }
241 return found;
242 }
243
244 #ifdef HAVE_XI2
245 Bool is_pointer(int use)
246 {
247 return use == XIMasterPointer || use == XISlavePointer;
248 }
249
250 Bool is_keyboard(int use)
251 {
252 return use == XIMasterKeyboard || use == XISlaveKeyboard;
253 }
254
255 Bool device_matches(XIDeviceInfo *info, char *name)
256 {
257 if (strcmp(info->name, name) == 0) {
258 return True;
259 }
260
261 if (strncmp(name, "pointer:", strlen("pointer:")) == 0 &&
262 strcmp(info->name, name + strlen("pointer:")) == 0 &&
263 is_pointer(info->use)) {
264 return True;
265 }
266
267 if (strncmp(name, "keyboard:", strlen("keyboard:")) == 0 &&
268 strcmp(info->name, name + strlen("keyboard:")) == 0 &&
269 is_keyboard(info->use)) {
270 return True;
271 }
272
273 return False;
274 }
275
276 XIDeviceInfo*
277 xi2_find_device_info(Display *display, char *name)
278 {
279 XIDeviceInfo *info;
280 XIDeviceInfo *found = NULL;
281 int ndevices;
282 Bool is_id = True;
283 int i, id = -1;
284
285 for(i = 0; i < strlen(name); i++) {
286 if (!isdigit(name[i])) {
287 is_id = False;
288 break;
289 }
290 }
291
292 if (is_id) {
293 id = atoi(name);
294 }
295
296 info = XIQueryDevice(display, XIAllDevices, &ndevices);
297 for(i = 0; i < ndevices; i++)
298 {
299 if (is_id ? info[i].deviceid == id : device_matches (&info[i], name)) {
300 if (found) {
301 fprintf(stderr,
302 "Warning: There are multiple devices matching '%s'.\n"
303 "To ensure the correct one is selected, please use "
304 "the device ID, or prefix the\ndevice name with "
305 "'pointer:' or 'keyboard:' as appropriate.\n\n", name);
306 XIFreeDeviceInfo(info);
307 return NULL;
308 } else {
309 found = &info[i];
310 }
311 }
312 }
313
314 return found;
315 }
316 #endif
317
318 static void
319 usage(void)
320 {
321 entry *pdriver = drivers;
322
323 fprintf(stderr, "usage :\n");
324
325 while(pdriver->func_name) {
326 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
327 pdriver->arg_desc);
328 pdriver++;
329 }
330 }
331
332 int
333 main(int argc, char * argv[])
334 {
335 Display *display;
336 entry *driver = drivers;
337 char *func;
338 int event, error;
339
340 if (argc < 2) {
341 usage();
342 return EXIT_FAILURE;
343 }
344
345 func = argv[1];
346 while((*func) == '-') func++;
347
348 if (strcmp("version", func) == 0) {
349 return print_version(argv[0]);
350 }
351
352 display = XOpenDisplay(NULL);
353
354 if (display == NULL) {
355 fprintf(stderr, "Unable to connect to X server\n");
356 return EXIT_FAILURE;
357 }
358
359 if (!XQueryExtension(display, "XInputExtension", &xi_opcode, &event, &error)) {
360 printf("X Input extension not available.\n");
361 return EXIT_FAILURE;
362 }
363
364 if (!xinput_version(display)) {
365 fprintf(stderr, "%s extension not available\n", INAME);
366 return EXIT_FAILURE;
367 }
368
369 while(driver->func_name) {
370 if (strcmp(driver->func_name, func) == 0) {
371 int r = (*driver->func)(display, argc-2, argv+2,
372 driver->func_name, driver->arg_desc);
373 XSync(display, False);
374 XCloseDisplay(display);
375 return r;
376 }
377 driver++;
378 }
379
380 usage();
381
382 return EXIT_FAILURE;
383 }
384
385 /* end of xinput.c */