Tizen 2.0 Release
[framework/uifw/xorg/util/x11-xserver-utils.git] / 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  the authors  not  be  used  in
9  * advertising or publicity pertaining to distribution of the software without
10  * specific,  written      prior  permission.     The authors  make  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  * THE AUTHORS DISCLAIM ALL   WARRANTIES WITH REGARD  TO  THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED   WARRANTIES OF MERCHANTABILITY  AND   FITNESS, IN NO
16  * EVENT  SHALL THE AUTHORS  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
27 int
28 get_button_map(Display  *display,
29                int      argc,
30                char     *argv[],
31                char     *name,
32                char     *desc)
33 {
34     XDeviceInfo         *info;
35     XDevice             *device;
36     XAnyClassPtr        ip;
37     int                 i;
38     int                 nbuttons;
39
40     if (argc != 1) {
41         fprintf(stderr, "usage: xinput %s %s\n", name, desc);
42         return EXIT_FAILURE;
43     }
44
45     info = find_device_info(display, argv[0], False);
46
47     if (!info) {
48         fprintf(stderr, "unable to find device '%s'\n", argv[0]);
49         return EXIT_FAILURE;
50     }
51
52     ip = (XAnyClassPtr) info->inputclassinfo;
53     nbuttons = 0;
54
55     /* try to find the number of buttons */
56     for(i=0; i<info->num_classes; i++) {
57         if (ip->class == ButtonClass) {
58             nbuttons = ((XButtonInfoPtr)ip)->num_buttons;
59             break;
60         }
61         ip = (XAnyClassPtr) ((char *) ip + ip->length);
62     }
63     if (nbuttons == 0) {
64         fprintf(stderr, "device has no buttons\n");
65         return EXIT_FAILURE;
66     }
67
68     device = XOpenDevice(display, info->id);
69     if (device) {
70         int             idx;
71         unsigned char   *map;
72
73         map = (unsigned char *) malloc(sizeof(unsigned char) * nbuttons);
74
75         XGetDeviceButtonMapping(display, device, map, nbuttons);
76
77         for(idx=1; idx < nbuttons + 1; idx++) {
78             printf("%d ", map[idx - 1]);
79         }
80         printf("\n");
81         XCloseDevice(display, device);
82         return EXIT_SUCCESS;
83     } else {
84         fprintf(stderr, "Unable to open device\n");
85         return EXIT_FAILURE;
86     }
87 }
88
89
90
91 int
92 set_button_map(Display  *display,
93                int      argc,
94                char     *argv[],
95                char     *name,
96                char     *desc)
97 {
98     XDeviceInfo         *info;
99     XDevice             *device;
100     XAnyClassPtr        ip;
101     int                 i;
102     int                 nbuttons;
103
104     if (argc < 2) {
105         fprintf(stderr, "usage: xinput %s %s\n", name, desc);
106         return EXIT_FAILURE;
107     }
108
109     info = find_device_info(display, argv[0], False);
110
111     if (!info) {
112         fprintf(stderr, "unable to find device '%s'\n", argv[0]);
113         return EXIT_FAILURE;
114     }
115
116     ip = (XAnyClassPtr) info->inputclassinfo;
117     nbuttons = 0;
118
119     /* try to find the number of buttons */
120     for(i=0; i<info->num_classes; i++) {
121         if (ip->class == ButtonClass) {
122             nbuttons = ((XButtonInfoPtr)ip)->num_buttons;
123             break;
124         }
125         ip = (XAnyClassPtr) ((char *) ip + ip->length);
126     }
127     if (nbuttons == 0) {
128         fprintf(stderr, "device has no buttons\n");
129         return EXIT_FAILURE;
130     }
131
132     device = XOpenDevice(display, info->id);
133     if (device) {
134         int             idx;
135         unsigned char   *map;
136         int             min;
137
138         map = (unsigned char *) malloc(sizeof(unsigned char) * nbuttons);
139
140         XGetDeviceButtonMapping(display, device, map, nbuttons);
141
142         min = (argc > nbuttons + 1) ? nbuttons + 1 : argc;
143
144         for(idx=1; idx < min; idx++) {
145             map[idx - 1] = atoi(argv[idx]);
146         }
147         XSetDeviceButtonMapping(display, device, map, nbuttons);
148         XCloseDevice(display, device);
149         return EXIT_SUCCESS;
150     } else {
151         fprintf(stderr, "Unable to open device\n");
152         return EXIT_FAILURE;
153     }
154 }
155
156 /* end of buttonmap.c */