]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
Require inputproto 1.9.99.12
[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)(Display* display, int argc, char *argv[],
29 char *prog_name, char *prog_desc);
30
31 typedef struct
32 {
33 char *func_name;
34 char *arg_desc;
35 prog func;
36 } entry;
37
38 static entry drivers[] =
39 {
40 {"get-feedbacks",
41 "<device name>",
42 get_feedbacks
43 },
44 {"set-ptr-feedback",
45 "<device name> <threshold> <num> <denom>",
46 set_ptr_feedback
47 },
48 {"set-integer-feedback",
49 "<device name> <feedback id> <value>",
50 set_integer_feedback
51 },
52 {"get-button-map",
53 "<device name>",
54 get_button_map
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 { "test-xi2",
106 "<device>",
107 test_xi2,
108 },
109 #endif
110 { "list-props",
111 "<device> [<device> ...]",
112 list_props
113 },
114 { "set-int-prop",
115 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
116 set_int_prop
117 },
118 { "set-float-prop",
119 "<device> <property> <val> [<val> ...]",
120 set_float_prop
121 },
122 { "set-atom-prop",
123 "<device> <property> <val> [<val> ...]",
124 set_atom_prop
125 },
126 { "watch-props",
127 "<device>",
128 watch_props
129 },
130 { "delete-prop",
131 "<device> <property>",
132 delete_prop
133 },
134 { "set-prop",
135 "<device> <property> <val> [<val> ...]",
136 set_prop
137 },
138 {NULL, NULL, NULL
139 }
140 };
141
142 int
143 xinput_version(Display *display)
144 {
145 XExtensionVersion *version;
146 static int vers = -1;
147
148 if (vers != -1)
149 return vers;
150
151 version = XGetExtensionVersion(display, INAME);
152
153 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
154 vers = version->major_version;
155 XFree(version);
156 }
157
158 return vers;
159 }
160
161 XDeviceInfo*
162 find_device_info(Display *display,
163 char *name,
164 Bool only_extended)
165 {
166 XDeviceInfo *devices;
167 XDeviceInfo *found = NULL;
168 int loop;
169 int num_devices;
170 int len = strlen(name);
171 Bool is_id = True;
172 XID id = (XID)-1;
173
174 for(loop=0; loop<len; loop++) {
175 if (!isdigit(name[loop])) {
176 is_id = False;
177 break;
178 }
179 }
180
181 if (is_id) {
182 id = atoi(name);
183 }
184
185 devices = XListInputDevices(display, &num_devices);
186
187 for(loop=0; loop<num_devices; loop++) {
188 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
189 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
190 (is_id && devices[loop].id == id))) {
191 if (found) {
192 fprintf(stderr,
193 "Warning: There are multiple devices named \"%s\".\n"
194 "To ensure the correct one is selected, please use "
195 "the device ID instead.\n\n", name);
196 return NULL;
197 } else {
198 found = &devices[loop];
199 }
200 }
201 }
202 return found;
203 }
204
205 #ifdef HAVE_XI2
206 XIDeviceInfo*
207 xi2_find_device_info(Display *display, char *name)
208 {
209 XIDeviceInfo *info;
210 int ndevices;
211 Bool is_id = True;
212 int i, id = -1;
213
214 for(i = 0; i < strlen(name); i++) {
215 if (!isdigit(name[i])) {
216 is_id = False;
217 break;
218 }
219 }
220
221 if (is_id) {
222 id = atoi(name);
223 }
224
225 info = XIQueryDevice(display, XIAllDevices, &ndevices);
226 for(i = 0; i < ndevices; i++)
227 {
228 if ((is_id && info[i].deviceid == id) ||
229 (!is_id && strcmp(info[i].name, name) == 0))
230 {
231 return &info[i];
232 }
233 }
234
235 XIFreeDeviceInfo(info);
236 return NULL;
237 }
238 #endif
239
240 static void
241 usage(void)
242 {
243 entry *pdriver = drivers;
244
245 fprintf(stderr, "usage :\n");
246
247 while(pdriver->func_name) {
248 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
249 pdriver->arg_desc);
250 pdriver++;
251 }
252 }
253
254 int
255 main(int argc, char * argv[])
256 {
257 Display *display;
258 entry *driver = drivers;
259 char *func;
260
261 if (argc < 2) {
262 usage();
263 return EXIT_FAILURE;
264 }
265
266 display = XOpenDisplay(NULL);
267
268 if (display == NULL) {
269 fprintf(stderr, "Unable to connect to X server\n");
270 return EXIT_FAILURE;
271 }
272
273 func = argv[1];
274 while((*func) == '-') func++;
275
276 if (!xinput_version(display)) {
277 fprintf(stderr, "%s extension not available\n", INAME);
278 return EXIT_FAILURE;
279 }
280
281 while(driver->func_name) {
282 if (strcmp(driver->func_name, func) == 0) {
283 int r = (*driver->func)(display, argc-2, argv+2,
284 driver->func_name, driver->arg_desc);
285 XSync(display, False);
286 XCloseDisplay(display);
287 return r;
288 }
289 driver++;
290 }
291
292 usage();
293
294 return EXIT_FAILURE;
295 }
296
297 /* end of xinput.c */