- Remove main proto
[dragonfly.git] / usr.sbin / pcvt / mcon / mcon.c
1 /*
2  * Copyright (c) 1994 Joerg Wunsch
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Joerg Wunsch
17  * 4. The name authors may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  *
32  * @(#)mcon.c, 3.20, Last Edit-Date: [Tue Dec 20 14:53:15 1994]
33  */
34
35 /*---------------------------------------------------------------------------*
36  *
37  *      history:
38  *
39  *      -jw     initial version; includes a basic mapping between PeeCee
40  *              scan codes and key names
41  *      -hm     changed sys/pcvt_ioctl.h -> machine/pcvt_ioctl.h
42  *
43  *---------------------------------------------------------------------------*/
44
45 /*
46  * Utility program to wire the mouse emulator control ioctl to the
47  * user level. Allows setting of any configurable parameter, or
48  * display the current configuration.
49  */
50
51 #include <machine/pcvt_ioctl.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <string.h>
56 #include <sys/fcntl.h>
57
58 static const char *keynames[] = {
59         "", "esc", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
60         "-", "+", "bksp", "tab", "q", "w", "e", "r", "t", "y", "u",
61         "i", "o", "p", "[", "]", "enter", "ctrl", "a", "s", "d", "f",
62         "g", "h", "j", "k", "l", ";", "\"", "`", "lshift", "\\",
63         "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "rshift",
64         "prtscr", "alt", "space", "caps", "f1", "f2", "f3", "f4",
65         "f5", "f6", "f7", "f8", "f9", "f10", "numlock", "scrolllock",
66         "kp7", "kp8", "kp9", "kp-", "kp4", "kp5", "kp6", "kp+",
67         "kp1", "kp2", "kp3", "kp0", "kp."
68 };
69
70
71 const char *scantoname(int scan) {
72         if(scan >= sizeof keynames / sizeof(const char *))
73                 return "???";
74         else
75                 return keynames[scan];
76 }
77
78 int nametoscan(const char *name) {
79         int i;
80         for(i = 0; i < sizeof keynames / sizeof(const char *); i++)
81                 if(strcmp(keynames[i], name) == 0)
82                         return i;
83         return -1;
84 }
85
86
87 int main(int argc, char **argv) {
88         int c, errs = 0, fd, do_set = 0;
89         int left = 0, mid = 0, right = 0, accel = 0, sticky = -1;
90         struct mousedefs mdef;
91
92         while((c = getopt(argc, argv, "l:m:r:a:s:")) != -1)
93                 switch(c) {
94                 case 'l':
95                         left = nametoscan(optarg);
96                         do_set = 1;
97                         if(left == -1) goto keynameerr;
98                         break;
99
100                 case 'm':
101                         mid = nametoscan(optarg);
102                         do_set = 1;
103                         if(mid == -1) goto keynameerr;
104                         break;
105
106                 case 'r':
107                         right = nametoscan(optarg);
108                         do_set = 1;
109                         if(right == -1) goto keynameerr;
110                         break;
111
112                 keynameerr:
113                 {
114                         fprintf(stderr, "unknown key name: %s\n",
115                                 optarg);
116                         errs++;
117                 }
118                         break;
119
120                 case 'a':
121                         accel = 1000 * strtol(optarg, 0, 10);
122                         do_set = 1;
123                         break;
124
125                 case 's':
126                         if(strcmp(optarg, "0") == 0
127                            || strcmp(optarg, "false") == 0
128                            || strcmp(optarg, "no") == 0)
129                                 sticky = 0;
130                         else if(strcmp(optarg, "1") == 0
131                                 || strcmp(optarg, "true") == 0
132                                 || strcmp(optarg, "yes") == 0)
133                                 sticky = 1;
134                         else {
135                                 fprintf(stderr, "invalid argument to -s: %s\n",
136                                         optarg);
137                                 errs++;
138                         }
139                         do_set = 1;
140                         break;
141
142                 default:
143                         errs++;
144                 }
145
146         argc -= optind;
147         argv += optind;
148
149         if(errs || argc != 1) {
150                 fprintf(stderr, "usage: "
151                         "mouse [-l key][-m key][-r key][-a acctime][-s 0|1] "
152                         "mousedev\n");
153                 return 2;
154         }
155
156         if((fd = open(argv[0], O_RDONLY)) < 0) {
157                 perror("open(mousedev)");
158                 return 2;
159         }
160         if(ioctl(fd, KBDMOUSEGET, &mdef) < 0) {
161                 perror("ioctl(KBDMOUSEGET)");
162                 return 1;
163         }
164
165         if(!do_set) {
166                 printf("Current mouse emulator definitions:\n"
167                        "left button: %s\n"
168                        "middle button: %s\n"
169                        "right button: %s\n"
170                        "acceleration limit: %d msec\n"
171                        "sticky buttons: %s\n",
172                        scantoname(mdef.leftbutton),
173                        scantoname(mdef.middlebutton),
174                        scantoname(mdef.rightbutton),
175                        mdef.acceltime / 1000,
176                        mdef.stickybuttons? "yes": "no");
177                 return 0;
178         }
179
180         if(left) mdef.leftbutton = left & 0x7f;
181         if(mid) mdef.middlebutton = mid & 0x7f;
182         if(right) mdef.rightbutton = right & 0x7f;
183
184         if(accel) mdef.acceltime = accel;
185         if(sticky != -1) mdef.stickybuttons = sticky;
186
187         if(ioctl(fd, KBDMOUSESET, &mdef) < 0) {
188                 perror("ioctl(KBDMOUSESET)");
189                 return 1;
190         }
191
192         return 0;
193 }