]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
3c8b23ca17413152a474547dd50d1e4ab284afb7
[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] [<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 {"version",
83 "",
84 version
85 },
86 #if HAVE_XI2
87 { "create-master",
88 "<id> [<sendCore (dflt:1)>] [<enable (dflt:1)>]",
89 create_master
90 },
91 { "remove-master",
92 "<id> [Floating|AttachToMaster (dflt:Floating)] [<returnPointer>] [<returnKeyboard>]",
93 remove_master
94 },
95 { "reattach",
96 "<id> <master>",
97 change_attachment
98 },
99 { "float",
100 "<id>",
101 float_device
102 },
103 { "set-cp",
104 "<window> <device>",
105 set_clientpointer
106 },
107 { "test-xi2",
108 "<device>",
109 test_xi2,
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 int
145 xinput_version(Display *display)
146 {
147 XExtensionVersion *version;
148 static int vers = -1;
149
150 if (vers != -1)
151 return vers;
152
153 version = XGetExtensionVersion(display, INAME);
154
155 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
156 vers = version->major_version;
157 XFree(version);
158 }
159
160 return vers;
161 }
162
163 XDeviceInfo*
164 find_device_info(Display *display,
165 char *name,
166 Bool only_extended)
167 {
168 XDeviceInfo *devices;
169 XDeviceInfo *found = NULL;
170 int loop;
171 int num_devices;
172 int len = strlen(name);
173 Bool is_id = True;
174 XID id = (XID)-1;
175
176 for(loop=0; loop<len; loop++) {
177 if (!isdigit(name[loop])) {
178 is_id = False;
179 break;
180 }
181 }
182
183 if (is_id) {
184 id = atoi(name);
185 }
186
187 devices = XListInputDevices(display, &num_devices);
188
189 for(loop=0; loop<num_devices; loop++) {
190 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
191 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
192 (is_id && devices[loop].id == id))) {
193 if (found) {
194 fprintf(stderr,
195 "Warning: There are multiple devices named \"%s\".\n"
196 "To ensure the correct one is selected, please use "
197 "the device ID instead.\n\n", name);
198 return NULL;
199 } else {
200 found = &devices[loop];
201 }
202 }
203 }
204 return found;
205 }
206
207 #ifdef HAVE_XI2
208 XIDeviceInfo*
209 xi2_find_device_info(Display *display, char *name)
210 {
211 XIDeviceInfo *info;
212 int ndevices;
213 Bool is_id = True;
214 int i, id = -1;
215
216 for(i = 0; i < strlen(name); i++) {
217 if (!isdigit(name[i])) {
218 is_id = False;
219 break;
220 }
221 }
222
223 if (is_id) {
224 id = atoi(name);
225 }
226
227 info = XIQueryDevice(display, XIAllDevices, &ndevices);
228 for(i = 0; i < ndevices; i++)
229 {
230 if ((is_id && info[i].deviceid == id) ||
231 (!is_id && strcmp(info[i].name, name) == 0))
232 {
233 return &info[i];
234 }
235 }
236
237 XIFreeDeviceInfo(info);
238 return NULL;
239 }
240 #endif
241
242 static void
243 usage(void)
244 {
245 entry *pdriver = drivers;
246
247 fprintf(stderr, "usage :\n");
248
249 while(pdriver->func_name) {
250 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
251 pdriver->arg_desc);
252 pdriver++;
253 }
254 }
255
256 int
257 main(int argc, char * argv[])
258 {
259 Display *display;
260 entry *driver = drivers;
261 char *func;
262 int event, error;
263
264 if (argc < 2) {
265 usage();
266 return EXIT_FAILURE;
267 }
268
269 display = XOpenDisplay(NULL);
270
271 if (display == NULL) {
272 fprintf(stderr, "Unable to connect to X server\n");
273 return EXIT_FAILURE;
274 }
275
276 if (!XQueryExtension(display, "XInputExtension", &xi_opcode, &event, &error)) {
277 printf("X Input extension not available.\n");
278 return EXIT_FAILURE;
279 }
280
281 func = argv[1];
282 while((*func) == '-') func++;
283
284 if (!xinput_version(display)) {
285 fprintf(stderr, "%s extension not available\n", INAME);
286 return EXIT_FAILURE;
287 }
288
289 while(driver->func_name) {
290 if (strcmp(driver->func_name, func) == 0) {
291 int r = (*driver->func)(display, argc-2, argv+2,
292 driver->func_name, driver->arg_desc);
293 XSync(display, False);
294 XCloseDisplay(display);
295 return r;
296 }
297 driver++;
298 }
299
300 usage();
301
302 return EXIT_FAILURE;
303 }
304
305 /* end of xinput.c */