]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
66b967bc1bb3ac76e3eaed02c91bbee48d1ed032
[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 { "map-to-output",
108 "<device> <output name>",
109 map_to_output,
110 },
111 #endif
112 { "list-props",
113 "<device> [<device> ...]",
114 list_props
115 },
116 { "set-int-prop",
117 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
118 set_int_prop
119 },
120 { "set-float-prop",
121 "<device> <property> <val> [<val> ...]",
122 set_float_prop
123 },
124 { "set-atom-prop",
125 "<device> <property> <val> [<val> ...]",
126 set_atom_prop
127 },
128 { "watch-props",
129 "<device>",
130 watch_props
131 },
132 { "delete-prop",
133 "<device> <property>",
134 delete_prop
135 },
136 { "set-prop",
137 "<device> [--type=atom|float|int] [--format=8|16|32] <property> <val> [<val> ...]",
138 set_prop
139 },
140 {NULL, NULL, NULL
141 }
142 };
143
144 static const char version_id[] = VERSION;
145
146 static int
147 print_version(void)
148 {
149 XExtensionVersion *version;
150 Display *display;
151
152 printf("xinput version %s\n", version_id);
153
154 display = XOpenDisplay(NULL);
155
156 printf("XI version on server: ");
157
158 if (display == NULL)
159 printf("Failed to open display.\n");
160 else {
161 version = XGetExtensionVersion(display, INAME);
162 if (!version || (version == (XExtensionVersion*) NoSuchExtension))
163 printf(" Extension not supported.\n");
164 else {
165 printf("%d.%d\n", version->major_version,
166 version->minor_version);
167 XFree(version);
168 return 0;
169 }
170 }
171
172 return 1;
173 }
174
175 int
176 xinput_version(Display *display)
177 {
178 XExtensionVersion *version;
179 static int vers = -1;
180
181 if (vers != -1)
182 return vers;
183
184 version = XGetExtensionVersion(display, INAME);
185
186 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
187 vers = version->major_version;
188 XFree(version);
189 }
190
191 #if HAVE_XI2
192 /* Announce our supported version so the server treats us correctly. */
193 if (vers >= XI_2_Major)
194 {
195 int maj = 2,
196 min = 0;
197
198 #if HAVE_XI21
199 min = 1;
200 #elif HAVE_XI22
201 min = 2;
202 #endif
203
204 XIQueryVersion(display, &maj, &min);
205 }
206 #endif
207
208 return vers;
209 }
210
211 XDeviceInfo*
212 find_device_info(Display *display,
213 char *name,
214 Bool only_extended)
215 {
216 XDeviceInfo *devices;
217 XDeviceInfo *found = NULL;
218 int loop;
219 int num_devices;
220 int len = strlen(name);
221 Bool is_id = True;
222 XID id = (XID)-1;
223
224 for(loop=0; loop<len; loop++) {
225 if (!isdigit(name[loop])) {
226 is_id = False;
227 break;
228 }
229 }
230
231 if (is_id) {
232 id = atoi(name);
233 }
234
235 devices = XListInputDevices(display, &num_devices);
236
237 for(loop=0; loop<num_devices; loop++) {
238 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
239 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
240 (is_id && devices[loop].id == id))) {
241 if (found) {
242 fprintf(stderr,
243 "Warning: There are multiple devices named \"%s\".\n"
244 "To ensure the correct one is selected, please use "
245 "the device ID instead.\n\n", name);
246 return NULL;
247 } else {
248 found = &devices[loop];
249 }
250 }
251 }
252 return found;
253 }
254
255 #ifdef HAVE_XI2
256 Bool is_pointer(int use)
257 {
258 return use == XIMasterPointer || use == XISlavePointer;
259 }
260
261 Bool is_keyboard(int use)
262 {
263 return use == XIMasterKeyboard || use == XISlaveKeyboard;
264 }
265
266 Bool device_matches(XIDeviceInfo *info, char *name)
267 {
268 if (strcmp(info->name, name) == 0) {
269 return True;
270 }
271
272 if (strncmp(name, "pointer:", strlen("pointer:")) == 0 &&
273 strcmp(info->name, name + strlen("pointer:")) == 0 &&
274 is_pointer(info->use)) {
275 return True;
276 }
277
278 if (strncmp(name, "keyboard:", strlen("keyboard:")) == 0 &&
279 strcmp(info->name, name + strlen("keyboard:")) == 0 &&
280 is_keyboard(info->use)) {
281 return True;
282 }
283
284 return False;
285 }
286
287 XIDeviceInfo*
288 xi2_find_device_info(Display *display, char *name)
289 {
290 XIDeviceInfo *info;
291 XIDeviceInfo *found = NULL;
292 int ndevices;
293 Bool is_id = True;
294 int i, id = -1;
295
296 for(i = 0; i < strlen(name); i++) {
297 if (!isdigit(name[i])) {
298 is_id = False;
299 break;
300 }
301 }
302
303 if (is_id) {
304 id = atoi(name);
305 }
306
307 info = XIQueryDevice(display, XIAllDevices, &ndevices);
308 for(i = 0; i < ndevices; i++)
309 {
310 if (is_id ? info[i].deviceid == id : device_matches (&info[i], name)) {
311 if (found) {
312 fprintf(stderr,
313 "Warning: There are multiple devices matching '%s'.\n"
314 "To ensure the correct one is selected, please use "
315 "the device ID, or prefix the\ndevice name with "
316 "'pointer:' or 'keyboard:' as appropriate.\n\n", name);
317 XIFreeDeviceInfo(info);
318 return NULL;
319 } else {
320 found = &info[i];
321 }
322 }
323 }
324
325 return found;
326 }
327 #endif
328
329 static void
330 usage(void)
331 {
332 entry *pdriver = drivers;
333
334 fprintf(stderr, "usage :\n");
335
336 while(pdriver->func_name) {
337 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
338 pdriver->arg_desc);
339 pdriver++;
340 }
341 }
342
343 int
344 main(int argc, char * argv[])
345 {
346 Display *display;
347 entry *driver = drivers;
348 char *func;
349 int event, error;
350
351 if (argc > 1) {
352 func = argv[1];
353 while(func[0] == '-') func++;
354 } else {
355 func = "list";
356 }
357
358 if (strcmp("version", func) == 0) {
359 return print_version();
360 }
361
362 if (strcmp("help", func) == 0) {
363 usage();
364 return 0;
365 }
366
367 display = XOpenDisplay(NULL);
368
369 if (display == NULL) {
370 fprintf(stderr, "Unable to connect to X server\n");
371 return EXIT_FAILURE;
372 }
373
374 if (!XQueryExtension(display, "XInputExtension", &xi_opcode, &event, &error)) {
375 printf("X Input extension not available.\n");
376 return EXIT_FAILURE;
377 }
378
379 if (!xinput_version(display)) {
380 fprintf(stderr, "%s extension not available\n", INAME);
381 return EXIT_FAILURE;
382 }
383
384 while(driver->func_name) {
385 if (strcmp(driver->func_name, func) == 0) {
386 int r = (*driver->func)(display, argc-2, argv+2,
387 driver->func_name, driver->arg_desc);
388 XSync(display, False);
389 XCloseDisplay(display);
390 return r;
391 }
392 driver++;
393 }
394
395 usage();
396
397 return EXIT_FAILURE;
398 }
399
400 /* end of xinput.c */