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