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