1 /* $NetBSD: bellctrl.c,v 1.10 2005/12/11 12:19:45 christos Exp $ */
4 * bellctrl - OPM bell controller (for NetBSD/X680x0)
5 * Copyright (c)1995 ussy.
9 __RCSID("$NetBSD: bellctrl.c,v 1.10 2005/12/11 12:19:45 christos Exp $");
17 #include <sys/ioctl.h>
18 #include <machine/opmbellio.h>
22 #define nextarg(i, argv) \
29 struct opm_voice voice
;
31 static struct opm_voice bell_voice
= DEFAULT_BELL_VOICE
;
33 static struct bell_info values
= {
34 DEFAULT
, DEFAULT
, DEFAULT
37 /* function prototype */
38 int is_number(char *, int);
39 void set_bell_vol(int);
40 void set_bell_pitch(int);
41 void set_bell_dur(int);
42 void set_voice_param(char *, int);
43 void set_bell_param(void);
44 int usage(char *, char *);
47 main(int argc
, char **argv
)
59 for (i
= 1; i
< argc
; ) {
61 if (strcmp(arg
, "-b") == 0) {
64 } else if (strcmp(arg
, "b") == 0) {
65 /* set bell to default */
69 /* set bell to default */
70 set_bell_vol(percent
);
71 /* set pitch to default */
72 set_bell_pitch(percent
);
73 /* set duration to default */
74 set_bell_dur(percent
);
77 arg
= nextarg(i
, argv
);
78 if (strcmp(arg
, "on") == 0) {
80 * let it stay that way
83 set_bell_vol(BELL_VOLUME
);
84 /* set pitch to default */
85 set_bell_pitch(BELL_PITCH
);
86 /* set duration to default */
87 set_bell_dur(BELL_DURATION
);
89 } else if (strcmp(arg
, "off") == 0) {
90 /* turn the bell off */
92 set_bell_vol(percent
);
94 } else if (is_number(arg
, MAXBVOLUME
)) {
98 /* set bell appropriately */
101 set_bell_vol(percent
);
104 arg
= nextarg(i
, argv
);
106 /* if pitch is given */
107 if (is_number(arg
, MAXBPITCH
)) {
109 set_bell_pitch(atoi(arg
));
112 arg
= nextarg(i
, argv
);
113 /* If duration is given */
114 if (is_number(arg
, MAXBTIME
)) {
116 set_bell_dur(atoi(arg
));
121 /* set bell to default */
122 set_bell_vol(BELL_VOLUME
);
124 } else if (strcmp(arg
, "v") == 0) {
126 * set voice parameter
131 arg
= nextarg(i
, argv
);
133 set_voice_param(arg
, 1);
135 } else if (strcmp(arg
, "-v") == 0) {
137 * set voice parameter
140 usage("missing -v argument", NULL
);
141 arg
= nextarg(i
, argv
);
142 set_voice_param(arg
, 0);
145 usage("unknown option %s", arg
);
156 is_number(char *arg
, int maximum
)
160 if (arg
[0] == '-' && arg
[1] == '1' && arg
[2] == '\0')
162 for (p
= arg
; isdigit((unsigned char)*p
); p
++)
164 if (*p
|| atoi(arg
) > maximum
)
171 set_bell_vol(int percent
)
173 values
.volume
= percent
;
178 set_bell_pitch(int pitch
)
180 values
.pitch
= pitch
;
185 set_bell_dur(int duration
)
187 values
.msec
= duration
;
192 set_voice_param(char *path
, int flag
)
197 memcpy(&voice
, &bell_voice
, sizeof(bell_voice
));
199 if ((fd
= open(path
, 0)) >= 0) {
200 if (read(fd
, &voice
, sizeof(voice
)) != sizeof(voice
))
201 err(1, "cannot read voice parameter");
204 err(1, "cannot open voice parameter");
208 if ((fd
= open("/dev/bell", O_RDWR
)) < 0)
209 err(1, "cannot open /dev/bell");
210 if (ioctl(fd
, BELLIOCSVOICE
, &voice
))
211 err(1, "ioctl BELLIOCSVOICE failed");
220 struct bell_info param
;
222 if ((fd
= open("/dev/bell", O_RDWR
)) < 0)
223 err(1, "cannot open /dev/bell");
224 if (ioctl(fd
, BELLIOCGPARAM
, ¶m
))
225 err(1, "ioctl BELLIOCGPARAM failed");
227 if (values
.volume
== DEFAULT
)
228 values
.volume
= param
.volume
;
229 if (values
.pitch
== DEFAULT
)
230 values
.pitch
= param
.pitch
;
231 if (values
.msec
== DEFAULT
)
232 values
.msec
= param
.msec
;
234 if (ioctl(fd
, BELLIOCSPARAM
, &values
))
235 err(1, "ioctl BELLIOCSPARAM failed");
241 usage(char *fmt
, char *arg
)
244 fprintf(stderr
, "%s: ", progName
);
245 fprintf(stderr
, fmt
, arg
);
246 fprintf(stderr
, "\n\n");
249 fprintf(stderr
, "usage: %s option ...\n", progName
);
250 fprintf(stderr
, " To turn bell off:\n");
251 fprintf(stderr
, "\t-b b off"
253 fprintf(stderr
, " To set bell volume, pitch and duration:\n");
254 fprintf(stderr
, "\t b [vol [pitch [dur]]] b on\n");
255 fprintf(stderr
, " To restore default voice parameter:\n");
256 fprintf(stderr
, "\t v default\n");
257 fprintf(stderr
, " To set voice parameter:\n");
258 fprintf(stderr
, "\t-v voicefile\n");