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