]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
c8cc4f917618fda3d908329e100b7045ec8bd4b7
[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 { "create-master",
85 "<id> [sendCore (dflt:1)] [enable (dflt:1)]",
86 create_master
87 },
88 { "remove-master",
89 "<id> [returnMode (dflt:Floating)] [returnPointer] [returnKeyboard]",
90 remove_master
91 },
92 { "reattach",
93 "<id> <master>",
94 change_attachment
95 },
96 { "float",
97 "<id>",
98 float_device
99 },
100 { "set-cp",
101 "<window> <device>",
102 set_clientpointer
103 },
104 {0, 0, 0
105 }
106 };
107
108 static Bool
109 is_xinput_present(Display *display)
110 {
111 XExtensionVersion *version;
112 Bool present;
113
114 version = XQueryInputVersion(display, XI_2_Major, XI_2_Minor);
115
116 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
117 present = version->present;
118 XFree(version);
119 return present;
120 } else {
121 return False;
122 }
123 }
124
125 XDeviceInfo*
126 find_device_info(Display *display,
127 char *name,
128 Bool only_extended)
129 {
130 XDeviceInfo *devices;
131 XDeviceInfo *found = NULL;
132 int loop;
133 int num_devices;
134 int len = strlen(name);
135 Bool is_id = True;
136 XID id;
137
138 for(loop=0; loop<len; loop++) {
139 if (!isdigit(name[loop])) {
140 is_id = False;
141 break;
142 }
143 }
144
145 if (is_id) {
146 id = atoi(name);
147 }
148
149 devices = XListInputDevices(display, &num_devices);
150
151 for(loop=0; loop<num_devices; loop++) {
152 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
153 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
154 (is_id && devices[loop].id == id))) {
155 if (found) {
156 fprintf(stderr,
157 "Warning: There are multiple devices named \"%s\".\n"
158 "To ensure the correct one is selected, please use "
159 "the device ID instead.\n\n", name);
160 } else {
161 found = &devices[loop];
162 }
163 }
164 }
165 return found;
166 }
167
168 static void
169 usage(void)
170 {
171 entry *pdriver = drivers;
172
173 fprintf(stderr, "usage :\n");
174
175 while(pdriver->func_name) {
176 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
177 pdriver->arg_desc);
178 pdriver++;
179 }
180 }
181
182 int
183 main(int argc, char * argv[])
184 {
185 Display *display;
186 entry *driver = drivers;
187 char *func;
188
189 if (argc < 2) {
190 usage();
191 return EXIT_FAILURE;
192 }
193
194 display = XOpenDisplay(NULL);
195
196 if (display == NULL) {
197 fprintf(stderr, "Unable to connect to X server\n");
198 return EXIT_FAILURE;
199 }
200
201 func = argv[1];
202 while((*func) == '-') func++;
203
204 if (!is_xinput_present(display)) {
205 fprintf(stderr, "%s extension not available\n", INAME);
206 return EXIT_FAILURE;
207 }
208
209 while(driver->func_name) {
210 if (strcmp(driver->func_name, func) == 0) {
211 int r = (*driver->func)(display, argc-2, argv+2,
212 driver->func_name, driver->arg_desc);
213 XFlush(display);
214 return r;
215 }
216 driver++;
217 }
218
219 usage();
220
221 return EXIT_FAILURE;
222 }
223
224 /* end of xinput.c */