]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
Clean up the detection of XI2
[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 typedef int (*prog)(
29 #if NeedFunctionPrototypes
30 Display* display, int argc, char *argv[],
31 char *prog_name, char *prog_desc
32 #endif
33 );
34
35 typedef struct
36 {
37 char *func_name;
38 char *arg_desc;
39 prog func;
40 } entry;
41
42 static entry drivers[] =
43 {
44 {"get-feedbacks",
45 "<device name>",
46 get_feedbacks
47 },
48 {"set-ptr-feedback",
49 "<device name> <threshold> <num> <denom>",
50 set_ptr_feedback
51 },
52 {"set-integer-feedback",
53 "<device name> <feedback id> <value>",
54 set_integer_feedback
55 },
56 {"set-button-map",
57 "<device name> <map button 1> [<map button 2> [...]]",
58 set_button_map
59 },
60 {"set-pointer",
61 "<device name> [<x index> <y index>]",
62 set_pointer
63 },
64 {"set-mode",
65 "<device name> ABSOLUTE|RELATIVE",
66 set_mode
67 },
68 {"list",
69 "[--loop || --short || <device name>...]",
70 list
71 },
72 {"query-state",
73 "<device name>",
74 query_state
75 },
76 {"test",
77 "[-proximity] <device name>",
78 test
79 },
80 {"version",
81 "",
82 version
83 },
84 #if HAVE_XI2
85 { "create-master",
86 "<id> [sendCore (dflt:1)] [enable (dflt:1)]",
87 create_master
88 },
89 { "remove-master",
90 "<id> [returnMode (dflt:Floating)] [returnPointer] [returnKeyboard]",
91 remove_master
92 },
93 { "reattach",
94 "<id> <master>",
95 change_attachment
96 },
97 { "float",
98 "<id>",
99 float_device
100 },
101 { "set-cp",
102 "<window> <device>",
103 set_clientpointer
104 },
105 #endif
106 {0, 0, 0
107 }
108 };
109
110 static Bool
111 is_xinput_present(Display *display)
112 {
113 XExtensionVersion *version;
114 Bool present;
115
116 #if HAVE_XI2
117 version = XQueryInputVersion(display, XI_2_Major, XI_2_Minor);
118 #else
119 version = XGetExtensionVersion(display, INAME);
120 #endif
121
122 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
123 present = version->present;
124 XFree(version);
125 return present;
126 } else {
127 return False;
128 }
129 }
130
131 XDeviceInfo*
132 find_device_info(Display *display,
133 char *name,
134 Bool only_extended)
135 {
136 XDeviceInfo *devices;
137 XDeviceInfo *found = NULL;
138 int loop;
139 int num_devices;
140 int len = strlen(name);
141 Bool is_id = True;
142 XID id;
143
144 for(loop=0; loop<len; loop++) {
145 if (!isdigit(name[loop])) {
146 is_id = False;
147 break;
148 }
149 }
150
151 if (is_id) {
152 id = atoi(name);
153 }
154
155 devices = XListInputDevices(display, &num_devices);
156
157 for(loop=0; loop<num_devices; loop++) {
158 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
159 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
160 (is_id && devices[loop].id == id))) {
161 if (found) {
162 fprintf(stderr,
163 "Warning: There are multiple devices named \"%s\".\n"
164 "To ensure the correct one is selected, please use "
165 "the device ID instead.\n\n", name);
166 } else {
167 found = &devices[loop];
168 }
169 }
170 }
171 return found;
172 }
173
174 static void
175 usage(void)
176 {
177 entry *pdriver = drivers;
178
179 fprintf(stderr, "usage :\n");
180
181 while(pdriver->func_name) {
182 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
183 pdriver->arg_desc);
184 pdriver++;
185 }
186 }
187
188 int
189 main(int argc, char * argv[])
190 {
191 Display *display;
192 entry *driver = drivers;
193 char *func;
194
195 if (argc < 2) {
196 usage();
197 return EXIT_FAILURE;
198 }
199
200 display = XOpenDisplay(NULL);
201
202 if (display == NULL) {
203 fprintf(stderr, "Unable to connect to X server\n");
204 return EXIT_FAILURE;
205 }
206
207 func = argv[1];
208 while((*func) == '-') func++;
209
210 if (!is_xinput_present(display)) {
211 fprintf(stderr, "%s extension not available\n", INAME);
212 return EXIT_FAILURE;
213 }
214
215 while(driver->func_name) {
216 if (strcmp(driver->func_name, func) == 0) {
217 int r = (*driver->func)(display, argc-2, argv+2,
218 driver->func_name, driver->arg_desc);
219 XFlush(display);
220 return r;
221 }
222 driver++;
223 }
224
225 usage();
226
227 return EXIT_FAILURE;
228 }
229
230 /* end of xinput.c */