]> diplodocus.org Git - xorg-xinput/blob - src/buttonmap.c
Make dependency on inputproto >= 1.4 explicit.
[xorg-xinput] / src / buttonmap.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
26 int
27 set_button_map(Display *display,
28 int argc,
29 char *argv[],
30 char *name,
31 char *desc)
32 {
33 XDeviceInfo *info;
34 XDevice *device;
35 XAnyClassPtr ip;
36 int i;
37 int nbuttons;
38
39 if (argc < 2) {
40 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
41 return EXIT_FAILURE;
42 }
43
44 info = find_device_info(display, argv[0], True);
45
46 if (!info) {
47 fprintf(stderr, "unable to find device %s\n", argv[0]);
48 return EXIT_FAILURE;
49 }
50
51 ip = (XAnyClassPtr) info->inputclassinfo;
52 nbuttons = 0;
53
54 /* try to find the number of buttons */
55 for(i=0; i<info->num_classes; i++) {
56 if (ip->class == ButtonClass) {
57 nbuttons = ((XButtonInfoPtr)ip)->num_buttons;
58 break;
59 }
60 ip = (XAnyClassPtr) ((char *) ip + ip->length);
61 }
62 if (nbuttons == 0) {
63 fprintf(stderr, "device has no buttons\n");
64 return EXIT_FAILURE;
65 }
66
67 device = XOpenDevice(display, info->id);
68 if (device) {
69 int idx;
70 unsigned char *map;
71 int min;
72
73 map = (unsigned char *) malloc(sizeof(unsigned char) * nbuttons);
74
75 XGetDeviceButtonMapping(display, device, map, nbuttons);
76
77 min = (argc > nbuttons + 1) ? nbuttons + 1 : argc;
78
79 for(idx=1; idx < min; idx++) {
80 map[idx - 1] = atoi(argv[idx]);
81 }
82 XSetDeviceButtonMapping(display, device, map, nbuttons);
83 XCloseDevice(display, device);
84 return EXIT_SUCCESS;
85 } else {
86 fprintf(stderr, "Unable to open device\n");
87 return EXIT_FAILURE;
88 }
89 }
90
91 /* end of buttonmap.c */