]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
da54b480a7ccc14c4d28feaf0c576bf34386f0d1
[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 {"get-button-map",
57 "<device name>",
58 get_button_map
59 },
60 {"set-button-map",
61 "<device name> <map button 1> [<map button 2> [...]]",
62 set_button_map
63 },
64 {"set-pointer",
65 "<device name> [<x index> <y index>]",
66 set_pointer
67 },
68 {"set-mode",
69 "<device name> ABSOLUTE|RELATIVE",
70 set_mode
71 },
72 {"list",
73 "[--loop || --short || <device name>...]",
74 list
75 },
76 {"query-state",
77 "<device name>",
78 query_state
79 },
80 {"test",
81 "[-proximity] <device name>",
82 test
83 },
84 {"version",
85 "",
86 version
87 },
88 #if HAVE_XI2
89 { "create-master",
90 "<id> [sendCore (dflt:1)] [enable (dflt:1)]",
91 create_master
92 },
93 { "remove-master",
94 "<id> [returnMode (dflt:Floating)] [returnPointer] [returnKeyboard]",
95 remove_master
96 },
97 { "reattach",
98 "<id> <master>",
99 change_attachment
100 },
101 { "float",
102 "<id>",
103 float_device
104 },
105 { "set-cp",
106 "<window> <device>",
107 set_clientpointer
108 },
109 { "test-xi2",
110 "<device>",
111 test_xi2,
112 },
113 #endif
114 { "list-props",
115 "<device> [<device> ...]",
116 list_props
117 },
118 { "set-int-prop",
119 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
120 set_int_prop
121 },
122 { "set-float-prop",
123 "<device> <property> <val> [<val> ...]",
124 set_float_prop
125 },
126 { "set-atom-prop",
127 "<device> <property> <val> [<val> ...]",
128 set_atom_prop
129 },
130 { "watch-props",
131 "<device>",
132 watch_props
133 },
134 { "delete-prop",
135 "<device> <property>",
136 delete_prop
137 },
138 { "set-prop",
139 "<device> <property> <val> [<val> ...]",
140 set_prop
141 },
142 {NULL, NULL, NULL
143 }
144 };
145
146 int
147 xinput_version(Display *display)
148 {
149 XExtensionVersion *version;
150 static int vers = -1;
151
152 if (vers != -1)
153 return vers;
154
155 version = XGetExtensionVersion(display, INAME);
156
157 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
158 vers = version->major_version;
159 XFree(version);
160 }
161
162 return vers;
163 }
164
165 XDeviceInfo*
166 find_device_info(Display *display,
167 char *name,
168 Bool only_extended)
169 {
170 XDeviceInfo *devices;
171 XDeviceInfo *found = NULL;
172 int loop;
173 int num_devices;
174 int len = strlen(name);
175 Bool is_id = True;
176 XID id = (XID)-1;
177
178 for(loop=0; loop<len; loop++) {
179 if (!isdigit(name[loop])) {
180 is_id = False;
181 break;
182 }
183 }
184
185 if (is_id) {
186 id = atoi(name);
187 }
188
189 devices = XListInputDevices(display, &num_devices);
190
191 for(loop=0; loop<num_devices; loop++) {
192 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
193 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
194 (is_id && devices[loop].id == id))) {
195 if (found) {
196 fprintf(stderr,
197 "Warning: There are multiple devices named \"%s\".\n"
198 "To ensure the correct one is selected, please use "
199 "the device ID instead.\n\n", name);
200 return NULL;
201 } else {
202 found = &devices[loop];
203 }
204 }
205 }
206 return found;
207 }
208
209 #ifdef HAVE_XI2
210 int
211 xi2_find_device_id(Display *display, char *name)
212 {
213 XIDeviceInfo *info;
214 int ndevices;
215 Bool is_id = True;
216 int i, id = -1;
217
218 for(i = 0; i < strlen(name); i++) {
219 if (!isdigit(name[i])) {
220 is_id = False;
221 break;
222 }
223 }
224
225 if (is_id) {
226 id = atoi(name);
227 } else
228 {
229 info = XIQueryDevice(display, AllDevices, &ndevices);
230 for(i = 0; i < ndevices; i++)
231 {
232 if ((is_id && info[i].deviceid == id) ||
233 (!is_id && strcmp(info[i].name, name) == 0))
234 {
235 id = info[i].deviceid;
236 break;
237 }
238 }
239
240 XIFreeDeviceInfo(info);
241 }
242 return id;;
243 }
244 #endif
245
246 static void
247 usage(void)
248 {
249 entry *pdriver = drivers;
250
251 fprintf(stderr, "usage :\n");
252
253 while(pdriver->func_name) {
254 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
255 pdriver->arg_desc);
256 pdriver++;
257 }
258 }
259
260 int
261 main(int argc, char * argv[])
262 {
263 Display *display;
264 entry *driver = drivers;
265 char *func;
266
267 if (argc < 2) {
268 usage();
269 return EXIT_FAILURE;
270 }
271
272 display = XOpenDisplay(NULL);
273
274 if (display == NULL) {
275 fprintf(stderr, "Unable to connect to X server\n");
276 return EXIT_FAILURE;
277 }
278
279 func = argv[1];
280 while((*func) == '-') func++;
281
282 if (!xinput_version(display)) {
283 fprintf(stderr, "%s extension not available\n", INAME);
284 return EXIT_FAILURE;
285 }
286
287 while(driver->func_name) {
288 if (strcmp(driver->func_name, func) == 0) {
289 int r = (*driver->func)(display, argc-2, argv+2,
290 driver->func_name, driver->arg_desc);
291 XSync(display, False);
292 return r;
293 }
294 driver++;
295 }
296
297 usage();
298
299 return EXIT_FAILURE;
300 }
301
302 /* end of xinput.c */