]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
Let autoconf substitute the VERSION.
[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 "[<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 {0, 0, 0
85 }
86 };
87
88 static Bool
89 is_xinput_present(Display *display)
90 {
91 XExtensionVersion *version;
92 Bool present;
93
94 version = XGetExtensionVersion(display, INAME);
95
96 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
97 present = version->present;
98 XFree(version);
99 return present;
100 } else {
101 return False;
102 }
103 }
104
105 XDeviceInfo*
106 find_device_info(Display *display,
107 char *name,
108 Bool only_extended)
109 {
110 XDeviceInfo *devices;
111 int loop;
112 int num_devices;
113 int len = strlen(name);
114 Bool is_id = True;
115 XID id;
116
117 for(loop=0; loop<len; loop++) {
118 if (!isdigit(name[loop])) {
119 is_id = False;
120 break;
121 }
122 }
123
124 if (is_id) {
125 id = atoi(name);
126 }
127
128 devices = XListInputDevices(display, &num_devices);
129
130 for(loop=0; loop<num_devices; loop++) {
131 if ((!only_extended || (devices[loop].use >= IsXExtensionDevice)) &&
132 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
133 (is_id && devices[loop].id == id))) {
134 return &devices[loop];
135 }
136 }
137 return NULL;
138 }
139
140 static void
141 usage()
142 {
143 entry *pdriver = drivers;
144
145 fprintf(stderr, "usage :\n");
146
147 while(pdriver->func_name) {
148 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
149 pdriver->arg_desc);
150 pdriver++;
151 }
152 }
153
154 int
155 main(int argc, char * argv[])
156 {
157 Display *display;
158 entry *driver = drivers;
159
160 if (argc < 2) {
161 usage();
162 return EXIT_FAILURE;
163 }
164
165 display = XOpenDisplay(NULL);
166
167 if (display == NULL) {
168 fprintf(stderr, "Unable to connect to X server\n");
169 return EXIT_FAILURE;
170 }
171
172 if (!is_xinput_present(display)) {
173 fprintf(stderr, "%s extension not available\n", INAME);
174 return EXIT_FAILURE;
175 }
176
177 while(driver->func_name) {
178 if (strcmp(driver->func_name, argv[1]) == 0) {
179 int r = (*driver->func)(display, argc-2, argv+2,
180 driver->func_name, driver->arg_desc);
181 XFlush(display);
182 return r;
183 }
184 driver++;
185 }
186
187 usage();
188
189 return EXIT_FAILURE;
190 }
191
192 /* end of xinput.c */