]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
Require inputproto 1.9.99.4
[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 { "list-props",
106 "<device> [<device> ...]",
107 list_props
108 },
109 { "set-int-prop",
110 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
111 set_int_prop
112 },
113 { "watch-props",
114 "<device>",
115 watch_props
116 },
117 #endif
118 {0, 0, 0
119 }
120 };
121
122 static Bool
123 is_xinput_present(Display *display)
124 {
125 XExtensionVersion *version;
126 Bool present;
127
128 #if HAVE_XI2
129 version = XQueryInputVersion(display, XI_2_Major, XI_2_Minor);
130 #else
131 version = XGetExtensionVersion(display, INAME);
132 #endif
133
134 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
135 present = version->present;
136 XFree(version);
137 return present;
138 } else {
139 return False;
140 }
141 }
142
143 XDeviceInfo*
144 find_device_info(Display *display,
145 char *name,
146 Bool only_extended)
147 {
148 XDeviceInfo *devices;
149 XDeviceInfo *found = NULL;
150 int loop;
151 int num_devices;
152 int len = strlen(name);
153 Bool is_id = True;
154 XID id;
155
156 for(loop=0; loop<len; loop++) {
157 if (!isdigit(name[loop])) {
158 is_id = False;
159 break;
160 }
161 }
162
163 if (is_id) {
164 id = atoi(name);
165 }
166
167 devices = XListInputDevices(display, &num_devices);
168
169 for(loop=0; loop<num_devices; loop++) {
170 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
171 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
172 (is_id && devices[loop].id == id))) {
173 if (found) {
174 fprintf(stderr,
175 "Warning: There are multiple devices named \"%s\".\n"
176 "To ensure the correct one is selected, please use "
177 "the device ID instead.\n\n", name);
178 } else {
179 found = &devices[loop];
180 }
181 }
182 }
183 return found;
184 }
185
186 static void
187 usage(void)
188 {
189 entry *pdriver = drivers;
190
191 fprintf(stderr, "usage :\n");
192
193 while(pdriver->func_name) {
194 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
195 pdriver->arg_desc);
196 pdriver++;
197 }
198 }
199
200 int
201 main(int argc, char * argv[])
202 {
203 Display *display;
204 entry *driver = drivers;
205 char *func;
206
207 if (argc < 2) {
208 usage();
209 return EXIT_FAILURE;
210 }
211
212 display = XOpenDisplay(NULL);
213
214 if (display == NULL) {
215 fprintf(stderr, "Unable to connect to X server\n");
216 return EXIT_FAILURE;
217 }
218
219 func = argv[1];
220 while((*func) == '-') func++;
221
222 if (!is_xinput_present(display)) {
223 fprintf(stderr, "%s extension not available\n", INAME);
224 return EXIT_FAILURE;
225 }
226
227 while(driver->func_name) {
228 if (strcmp(driver->func_name, func) == 0) {
229 int r = (*driver->func)(display, argc-2, argv+2,
230 driver->func_name, driver->arg_desc);
231 XFlush(display);
232 return r;
233 }
234 driver++;
235 }
236
237 usage();
238
239 return EXIT_FAILURE;
240 }
241
242 /* end of xinput.c */