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