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