]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
f584459293538b2ce69cdc9296ef5fa88d5541ce
[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 #endif
110 { "list-props",
111 "<device> [<device> ...]",
112 list_props
113 },
114 { "set-int-prop",
115 "<device> <property> <format (8, 16, 32)> <val> [<val> ...]",
116 set_int_prop
117 },
118 { "set-float-prop",
119 "<device> <property> <val> [<val> ...]",
120 set_float_prop
121 },
122 { "set-atom-prop",
123 "<device> <property> <val> [<val> ...]",
124 set_atom_prop
125 },
126 { "watch-props",
127 "<device>",
128 watch_props
129 },
130 { "delete-prop",
131 "<device> <property>",
132 delete_prop
133 },
134 { "set-prop",
135 "<device> <property> <val> [<val> ...]",
136 set_prop
137 },
138 {NULL, NULL, NULL
139 }
140 };
141
142 static Bool
143 is_xinput_present(Display *display)
144 {
145 XExtensionVersion *version;
146 Bool present;
147
148 #if HAVE_XI2
149 version = XQueryInputVersion(display, XI_2_Major, XI_2_Minor);
150 #else
151 version = XGetExtensionVersion(display, INAME);
152 #endif
153
154 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
155 present = version->present;
156 XFree(version);
157 return present;
158 } else {
159 return False;
160 }
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 static void
208 usage(void)
209 {
210 entry *pdriver = drivers;
211
212 fprintf(stderr, "usage :\n");
213
214 while(pdriver->func_name) {
215 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
216 pdriver->arg_desc);
217 pdriver++;
218 }
219 }
220
221 int
222 main(int argc, char * argv[])
223 {
224 Display *display;
225 entry *driver = drivers;
226 char *func;
227
228 if (argc < 2) {
229 usage();
230 return EXIT_FAILURE;
231 }
232
233 display = XOpenDisplay(NULL);
234
235 if (display == NULL) {
236 fprintf(stderr, "Unable to connect to X server\n");
237 return EXIT_FAILURE;
238 }
239
240 func = argv[1];
241 while((*func) == '-') func++;
242
243 if (!is_xinput_present(display)) {
244 fprintf(stderr, "%s extension not available\n", INAME);
245 return EXIT_FAILURE;
246 }
247
248 while(driver->func_name) {
249 if (strcmp(driver->func_name, func) == 0) {
250 int r = (*driver->func)(display, argc-2, argv+2,
251 driver->func_name, driver->arg_desc);
252 XSync(display, False);
253 return r;
254 }
255 driver++;
256 }
257
258 usage();
259
260 return EXIT_FAILURE;
261 }
262
263 /* end of xinput.c */