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