bellctrl.c revision 1.10.8.1 1 1.10.8.1 yamt /* $NetBSD: bellctrl.c,v 1.10.8.1 2006/08/11 15:43:15 yamt Exp $ */
2 1.2 thorpej
3 1.1 oki /*
4 1.1 oki * bellctrl - OPM bell controller (for NetBSD/X680x0)
5 1.1 oki * Copyright (c)1995 ussy.
6 1.1 oki */
7 1.8 lukem
8 1.8 lukem #include <sys/cdefs.h>
9 1.10.8.1 yamt __RCSID("$NetBSD: bellctrl.c,v 1.10.8.1 2006/08/11 15:43:15 yamt Exp $");
10 1.1 oki
11 1.6 isaki #include <err.h>
12 1.1 oki #include <stdio.h>
13 1.1 oki #include <stdlib.h>
14 1.1 oki #include <ctype.h>
15 1.10.8.1 yamt #include <string.h>
16 1.1 oki #include <sys/file.h>
17 1.1 oki #include <sys/ioctl.h>
18 1.9 minoura #include <machine/opmbellio.h>
19 1.1 oki
20 1.1 oki #define DEFAULT -1
21 1.1 oki
22 1.1 oki #define nextarg(i, argv) \
23 1.1 oki argv[i]; \
24 1.1 oki if (i >= argc) \
25 1.1 oki break; \
26 1.1 oki
27 1.1 oki int bell_setting;
28 1.1 oki char *progName;
29 1.1 oki struct opm_voice voice;
30 1.1 oki
31 1.9 minoura static struct opm_voice bell_voice = DEFAULT_BELL_VOICE;
32 1.9 minoura
33 1.1 oki static struct bell_info values = {
34 1.5 isaki DEFAULT, DEFAULT, DEFAULT
35 1.1 oki };
36 1.1 oki
37 1.1 oki /* function prototype */
38 1.7 isaki int is_number(char *, int);
39 1.7 isaki void set_bell_vol(int);
40 1.7 isaki void set_bell_pitch(int);
41 1.7 isaki void set_bell_dur(int);
42 1.7 isaki void set_voice_param(char *, int);
43 1.7 isaki void set_bell_param(void);
44 1.7 isaki int usage(char *, char *);
45 1.1 oki
46 1.1 oki int
47 1.5 isaki main(int argc, char **argv)
48 1.5 isaki {
49 1.5 isaki register char *arg;
50 1.5 isaki int percent;
51 1.5 isaki int i;
52 1.5 isaki
53 1.5 isaki progName = argv[0];
54 1.5 isaki bell_setting = 0;
55 1.5 isaki
56 1.5 isaki if (argc < 2)
57 1.5 isaki usage(NULL, NULL);
58 1.5 isaki
59 1.5 isaki for (i = 1; i < argc; ) {
60 1.5 isaki arg = argv[i++];
61 1.5 isaki if (strcmp(arg, "-b") == 0) {
62 1.5 isaki /* turn off bell */
63 1.5 isaki set_bell_vol(0);
64 1.5 isaki } else if (strcmp(arg, "b") == 0) {
65 1.5 isaki /* set bell to default */
66 1.5 isaki percent = DEFAULT;
67 1.5 isaki
68 1.5 isaki if (i >= argc) {
69 1.5 isaki /* set bell to default */
70 1.5 isaki set_bell_vol(percent);
71 1.5 isaki /* set pitch to default */
72 1.5 isaki set_bell_pitch(percent);
73 1.5 isaki /* set duration to default */
74 1.5 isaki set_bell_dur(percent);
75 1.5 isaki break;
76 1.5 isaki }
77 1.5 isaki arg = nextarg(i, argv);
78 1.5 isaki if (strcmp(arg, "on") == 0) {
79 1.5 isaki /*
80 1.5 isaki * let it stay that way
81 1.5 isaki */
82 1.5 isaki /* set bell on */
83 1.5 isaki set_bell_vol(BELL_VOLUME);
84 1.5 isaki /* set pitch to default */
85 1.5 isaki set_bell_pitch(BELL_PITCH);
86 1.5 isaki /* set duration to default */
87 1.5 isaki set_bell_dur(BELL_DURATION);
88 1.5 isaki i++;
89 1.5 isaki } else if (strcmp(arg, "off") == 0) {
90 1.5 isaki /* turn the bell off */
91 1.5 isaki percent = 0;
92 1.5 isaki set_bell_vol(percent);
93 1.5 isaki i++;
94 1.5 isaki } else if (is_number(arg, MAXBVOLUME)) {
95 1.5 isaki /*
96 1.5 isaki * If volume is given
97 1.5 isaki */
98 1.5 isaki /* set bell appropriately */
99 1.5 isaki percent = atoi(arg);
100 1.5 isaki
101 1.5 isaki set_bell_vol(percent);
102 1.5 isaki i++;
103 1.5 isaki
104 1.5 isaki arg = nextarg(i, argv);
105 1.5 isaki
106 1.5 isaki /* if pitch is given */
107 1.5 isaki if (is_number(arg, MAXBPITCH)) {
108 1.5 isaki /* set the bell */
109 1.5 isaki set_bell_pitch(atoi(arg));
110 1.5 isaki i++;
111 1.5 isaki
112 1.5 isaki arg = nextarg(i, argv);
113 1.5 isaki /* If duration is given */
114 1.5 isaki if (is_number(arg, MAXBTIME)) {
115 1.5 isaki /* set the bell */
116 1.5 isaki set_bell_dur(atoi(arg));
117 1.5 isaki i++;
118 1.5 isaki }
119 1.5 isaki }
120 1.5 isaki } else {
121 1.5 isaki /* set bell to default */
122 1.5 isaki set_bell_vol(BELL_VOLUME);
123 1.5 isaki }
124 1.5 isaki } else if (strcmp(arg, "v") == 0) {
125 1.5 isaki /*
126 1.5 isaki * set voice parameter
127 1.5 isaki */
128 1.5 isaki if (i >= argc) {
129 1.5 isaki arg = "default";
130 1.5 isaki } else {
131 1.5 isaki arg = nextarg(i, argv);
132 1.5 isaki }
133 1.5 isaki set_voice_param(arg, 1);
134 1.5 isaki i++;
135 1.5 isaki } else if (strcmp(arg, "-v") == 0) {
136 1.5 isaki /*
137 1.5 isaki * set voice parameter
138 1.5 isaki */
139 1.7 isaki if (i >= argc)
140 1.5 isaki usage("missing -v argument", NULL);
141 1.5 isaki arg = nextarg(i, argv);
142 1.5 isaki set_voice_param(arg, 0);
143 1.1 oki i++;
144 1.5 isaki } else {
145 1.5 isaki usage("unknown option %s", arg);
146 1.1 oki }
147 1.1 oki }
148 1.1 oki
149 1.5 isaki if (bell_setting)
150 1.7 isaki set_bell_param();
151 1.1 oki
152 1.5 isaki exit(0);
153 1.1 oki }
154 1.1 oki
155 1.1 oki int
156 1.5 isaki is_number(char *arg, int maximum)
157 1.1 oki {
158 1.5 isaki register char *p;
159 1.5 isaki
160 1.5 isaki if (arg[0] == '-' && arg[1] == '1' && arg[2] == '\0')
161 1.5 isaki return 1;
162 1.5 isaki for (p = arg; isdigit((unsigned char)*p); p++)
163 1.5 isaki ;
164 1.5 isaki if (*p || atoi(arg) > maximum)
165 1.5 isaki return 0;
166 1.1 oki
167 1.1 oki return 1;
168 1.1 oki }
169 1.1 oki
170 1.1 oki void
171 1.5 isaki set_bell_vol(int percent)
172 1.1 oki {
173 1.5 isaki values.volume = percent;
174 1.5 isaki bell_setting++;
175 1.1 oki }
176 1.1 oki
177 1.1 oki void
178 1.5 isaki set_bell_pitch(int pitch)
179 1.1 oki {
180 1.5 isaki values.pitch = pitch;
181 1.5 isaki bell_setting++;
182 1.1 oki }
183 1.1 oki
184 1.1 oki void
185 1.5 isaki set_bell_dur(int duration)
186 1.1 oki {
187 1.5 isaki values.msec = duration;
188 1.5 isaki bell_setting++;
189 1.1 oki }
190 1.1 oki
191 1.1 oki void
192 1.5 isaki set_voice_param(char *path, int flag)
193 1.5 isaki {
194 1.5 isaki int fd;
195 1.5 isaki
196 1.5 isaki if (flag) {
197 1.5 isaki memcpy(&voice, &bell_voice, sizeof(bell_voice));
198 1.1 oki } else {
199 1.5 isaki if ((fd = open(path, 0)) >= 0) {
200 1.5 isaki if (read(fd, &voice, sizeof(voice)) != sizeof(voice))
201 1.6 isaki err(1, "cannot read voice parameter");
202 1.5 isaki close(fd);
203 1.5 isaki } else {
204 1.6 isaki err(1, "cannot open voice parameter");
205 1.5 isaki }
206 1.1 oki }
207 1.1 oki
208 1.5 isaki if ((fd = open("/dev/bell", O_RDWR)) < 0)
209 1.6 isaki err(1, "cannot open /dev/bell");
210 1.5 isaki if (ioctl(fd, BELLIOCSVOICE, &voice))
211 1.6 isaki err(1, "ioctl BELLIOCSVOICE failed");
212 1.5 isaki
213 1.5 isaki close(fd);
214 1.1 oki }
215 1.1 oki
216 1.1 oki void
217 1.1 oki set_bell_param(void)
218 1.1 oki {
219 1.5 isaki int fd;
220 1.5 isaki struct bell_info param;
221 1.1 oki
222 1.5 isaki if ((fd = open("/dev/bell", O_RDWR)) < 0)
223 1.6 isaki err(1, "cannot open /dev/bell");
224 1.5 isaki if (ioctl(fd, BELLIOCGPARAM, ¶m))
225 1.6 isaki err(1, "ioctl BELLIOCGPARAM failed");
226 1.5 isaki
227 1.5 isaki if (values.volume == DEFAULT)
228 1.5 isaki values.volume = param.volume;
229 1.5 isaki if (values.pitch == DEFAULT)
230 1.5 isaki values.pitch = param.pitch;
231 1.5 isaki if (values.msec == DEFAULT)
232 1.5 isaki values.msec = param.msec;
233 1.1 oki
234 1.5 isaki if (ioctl(fd, BELLIOCSPARAM, &values))
235 1.6 isaki err(1, "ioctl BELLIOCSPARAM failed");
236 1.5 isaki
237 1.5 isaki close(fd);
238 1.1 oki }
239 1.1 oki
240 1.1 oki int
241 1.5 isaki usage(char *fmt, char *arg)
242 1.5 isaki {
243 1.5 isaki if (fmt) {
244 1.7 isaki fprintf(stderr, "%s: ", progName);
245 1.7 isaki fprintf(stderr, fmt, arg);
246 1.7 isaki fprintf(stderr, "\n\n");
247 1.5 isaki }
248 1.5 isaki
249 1.5 isaki fprintf(stderr, "usage: %s option ...\n", progName);
250 1.5 isaki fprintf(stderr, " To turn bell off:\n");
251 1.5 isaki fprintf(stderr, "\t-b b off"
252 1.5 isaki " b 0\n");
253 1.5 isaki fprintf(stderr, " To set bell volume, pitch and duration:\n");
254 1.5 isaki fprintf(stderr, "\t b [vol [pitch [dur]]] b on\n");
255 1.5 isaki fprintf(stderr, " To restore default voice parameter:\n");
256 1.5 isaki fprintf(stderr, "\t v default\n");
257 1.5 isaki fprintf(stderr, " To set voice parameter:\n");
258 1.5 isaki fprintf(stderr, "\t-v voicefile\n");
259 1.5 isaki exit(0);
260 1.1 oki }
261