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