]> diplodocus.org Git - xorg-xinput/blob - src/xinput.c
c25a4de9a045a448d4f061333fde1f6eec64a509
[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
27 static const char rcs_id[] = "$Id: xinput.c,v 1.5 1997/06/09 16:06:31 fred Exp $";
28
29 typedef int (*prog)(
30 #if NeedFunctionPrototypes
31 Display* display, int argc, char *argv[],
32 char *prog_name, char *prog_desc
33 #endif
34 );
35
36 typedef struct
37 {
38 char *func_name;
39 char *arg_desc;
40 prog func;
41 } entry;
42
43 static entry drivers[] =
44 {
45 {"get-feedbacks",
46 "<device name>",
47 get_feedbacks
48 },
49 {"set-ptr-feedback",
50 "<device name> <threshold> <num> <denom>",
51 set_ptr_feedback
52 },
53 {"set-integer-feedback",
54 "<device name> <feedback id> <value>",
55 set_integer_feedback
56 },
57 {"set-button-map",
58 "<device name> <map button 1> [<map button 2> [...]]",
59 set_button_map
60 },
61 {"set-pointer",
62 "<device name> [<x index> <y index>]",
63 set_pointer
64 },
65 {"set-mode",
66 "<device name> ABSOLUTE|RELATIVE",
67 set_mode
68 },
69 {"list",
70 "[<device name>...]",
71 list
72 },
73 {"query-state",
74 "<device name>",
75 query_state
76 },
77 {"test",
78 "[-proximity] <device name>",
79 test
80 },
81 {"version",
82 "",
83 version
84 },
85 {0, 0, 0
86 }
87 };
88
89 static Bool
90 is_xinput_present(Display *display)
91 {
92 XExtensionVersion *version;
93 Bool present;
94
95 version = XGetExtensionVersion(display, INAME);
96
97 if (version && (version != (XExtensionVersion*) NoSuchExtension)) {
98 present = version->present;
99 XFree(version);
100 return present;
101 } else {
102 return False;
103 }
104 }
105
106 XDeviceInfo*
107 find_device_info(Display *display,
108 char *name,
109 Bool only_extended)
110 {
111 XDeviceInfo *devices;
112 int loop;
113 int num_devices;
114 int len = strlen(name);
115 Bool is_id = True;
116 XID id;
117
118 for(loop=0; loop<len; loop++) {
119 if (!isdigit(name[loop])) {
120 is_id = False;
121 break;
122 }
123 }
124
125 if (is_id) {
126 id = atoi(name);
127 }
128
129 devices = XListInputDevices(display, &num_devices);
130
131 for(loop=0; loop<num_devices; loop++) {
132 if ((!only_extended || (devices[loop].use == IsXExtensionDevice)) &&
133 ((!is_id && strcmp(devices[loop].name, name) == 0) ||
134 (is_id && devices[loop].id == id))) {
135 return &devices[loop];
136 }
137 }
138 return NULL;
139 }
140
141 static void
142 usage()
143 {
144 entry *pdriver = drivers;
145
146 fprintf(stderr, "usage :\n");
147
148 while(pdriver->func_name) {
149 fprintf(stderr, "\txinput %s %s\n", pdriver->func_name,
150 pdriver->arg_desc);
151 pdriver++;
152 }
153 }
154
155 int
156 main(int argc, char * argv[])
157 {
158 Display *display;
159 entry *driver = drivers;
160
161 if (argc < 2) {
162 usage();
163 return EXIT_FAILURE;
164 }
165
166 display = XOpenDisplay(NULL);
167
168 if (display == NULL) {
169 fprintf(stderr, "Unable to connect to X server\n");
170 return EXIT_FAILURE;
171 }
172
173 if (!is_xinput_present(display)) {
174 fprintf(stderr, "%s extension not available\n", INAME);
175 return EXIT_FAILURE;
176 }
177
178 while(driver->func_name) {
179 if (strcmp(driver->func_name, argv[1]) == 0) {
180 int r = (*driver->func)(display, argc-2, argv+2,
181 driver->func_name, driver->arg_desc);
182 XFlush(display);
183 return r;
184 }
185 driver++;
186 }
187
188 usage();
189
190 return EXIT_FAILURE;
191 }
192
193 /* end of xinput.c
194 */