]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
47603d292c5dd38a1e28ee55147ae79ca75990ce
[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 Frederic Lepied not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Frederic Lepied makes 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 * FREDERIC LEPIED DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL FREDERIC LEPIED 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 {0, 0, 0
101 }
102 };
103
104 static Bool
105 is_xinput_present(Display *display)
106 {
107 XExtensionVersion *version;
108 Bool present;
109
110 version = XGetExtensionVersion(display, INAME);
111
112 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
113 present = version->present;
114 XFree(version);
115 return present;
116 } else {
117 return False;
118 }
119 }
120
121 XDeviceInfo*
122 find_device_info(Display *display,
123 char *name,
124 Bool only_extended)
125 {
126 XDeviceInfo *devices;
127 int loop;
128 int num_devices;
129 int len = strlen(name);
130 Bool is_id = True;
131 XID id;
132
133 for(loop=0; loop<len; loop++) {
134 if (!isdigit(name[loop])) {
135 is_id = False;
136 break;
137 }
138 }
139
140 if (is_id) {
141 id = atoi(name);
142 }
143
144 devices = XListInputDevices(display, &num_devices);
145
146 for(loop=0; loop<num_devices; loop++) {
147 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
148 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
149 (is_id && devices[loop].id == id))) {
150 return &devices[loop];
151 }
152 }
153 return NULL;
154 }
155
156 static void
157 usage()
158 {
159 entry *pdriver = drivers;
160
161 fprintf(stderr, "usage :\n");
162
163 while(pdriver->func_name) {
164 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
165 pdriver->arg_desc);
166 pdriver++;
167 }
168 }
169
170 int
171 main(int argc, char * argv[])
172 {
173 Display *display;
174 entry *driver = drivers;
175 char *func;
176
177 if (argc < 2) {
178 usage();
179 return EXIT_FAILURE;
180 }
181
182 display = XOpenDisplay(NULL);
183
184 if (display == NULL) {
185 fprintf(stderr, "Unable to connect to X server\n");
186 return EXIT_FAILURE;
187 }
188
189 func = argv[1];
190 while((*func) == '-') func++;
191
192 if (!is_xinput_present(display)) {
193 fprintf(stderr, "%s extension not available\n", INAME);
194 return EXIT_FAILURE;
195 }
196
197 while(driver->func_name) {
198 if (strcmp(driver->func_name, func) == 0) {
199 int r = (*driver->func)(display, argc-2, argv+2,
200 driver->func_name, driver->arg_desc);
201 XFlush(display);
202 return r;
203 }
204 driver++;
205 }
206
207 usage();
208
209 return EXIT_FAILURE;
210 }
211
212 /* end of xinput.c */