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