Home | History | Annotate | Line # | Download | only in bellctrl
bellctrl.c revision 1.7
      1  1.7    isaki /*	$NetBSD: bellctrl.c,v 1.7 2003/05/17 10:11:19 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.6    isaki #include <err.h>
      9  1.1      oki #include <stdio.h>
     10  1.1      oki #include <stdlib.h>
     11  1.1      oki #include <ctype.h>
     12  1.1      oki #include <sys/file.h>
     13  1.1      oki #include <sys/ioctl.h>
     14  1.1      oki #include "../../include/opmbellio.h"
     15  1.1      oki #include "../../dev/opmbellvar.h"
     16  1.1      oki #include "../../dev/opmreg.h"
     17  1.1      oki 
     18  1.1      oki #define DEFAULT -1
     19  1.1      oki 
     20  1.1      oki #define nextarg(i, argv) \
     21  1.1      oki 	argv[i]; \
     22  1.1      oki 	if (i >= argc) \
     23  1.1      oki 		break; \
     24  1.1      oki 
     25  1.1      oki int bell_setting;
     26  1.1      oki char *progName;
     27  1.1      oki struct opm_voice voice;
     28  1.1      oki 
     29  1.1      oki static struct bell_info values = {
     30  1.5    isaki 	DEFAULT, DEFAULT, DEFAULT
     31  1.1      oki };
     32  1.1      oki 
     33  1.1      oki /* function prototype */
     34  1.7    isaki int is_number(char *, int);
     35  1.7    isaki void set_bell_vol(int);
     36  1.7    isaki void set_bell_pitch(int);
     37  1.7    isaki void set_bell_dur(int);
     38  1.7    isaki void set_voice_param(char *, int);
     39  1.7    isaki void set_bell_param(void);
     40  1.7    isaki int usage(char *, char *);
     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.7    isaki 			if (i >= argc)
    136  1.5    isaki 				usage("missing -v argument", NULL);
    137  1.5    isaki 			arg = nextarg(i, argv);
    138  1.5    isaki 			set_voice_param(arg, 0);
    139  1.1      oki 			i++;
    140  1.5    isaki 		} else {
    141  1.5    isaki 			usage("unknown option %s", arg);
    142  1.1      oki 		}
    143  1.1      oki 	}
    144  1.1      oki 
    145  1.5    isaki 	if (bell_setting)
    146  1.7    isaki 		set_bell_param();
    147  1.1      oki 
    148  1.5    isaki 	exit(0);
    149  1.1      oki }
    150  1.1      oki 
    151  1.1      oki int
    152  1.5    isaki is_number(char *arg, int maximum)
    153  1.1      oki {
    154  1.5    isaki 	register char *p;
    155  1.5    isaki 
    156  1.5    isaki 	if (arg[0] == '-' && arg[1] == '1' && arg[2] == '\0')
    157  1.5    isaki 		return 1;
    158  1.5    isaki 	for (p = arg; isdigit((unsigned char)*p); p++)
    159  1.5    isaki 		;
    160  1.5    isaki 	if (*p || atoi(arg) > maximum)
    161  1.5    isaki 		return 0;
    162  1.1      oki 
    163  1.1      oki 	return 1;
    164  1.1      oki }
    165  1.1      oki 
    166  1.1      oki void
    167  1.5    isaki set_bell_vol(int percent)
    168  1.1      oki {
    169  1.5    isaki 	values.volume = percent;
    170  1.5    isaki 	bell_setting++;
    171  1.1      oki }
    172  1.1      oki 
    173  1.1      oki void
    174  1.5    isaki set_bell_pitch(int pitch)
    175  1.1      oki {
    176  1.5    isaki 	values.pitch = pitch;
    177  1.5    isaki 	bell_setting++;
    178  1.1      oki }
    179  1.1      oki 
    180  1.1      oki void
    181  1.5    isaki set_bell_dur(int duration)
    182  1.1      oki {
    183  1.5    isaki 	values.msec = duration;
    184  1.5    isaki 	bell_setting++;
    185  1.1      oki }
    186  1.1      oki 
    187  1.1      oki void
    188  1.5    isaki set_voice_param(char *path, int flag)
    189  1.5    isaki {
    190  1.5    isaki 	int fd;
    191  1.5    isaki 
    192  1.5    isaki 	if (flag) {
    193  1.5    isaki 		memcpy(&voice, &bell_voice, sizeof(bell_voice));
    194  1.1      oki 	} else {
    195  1.5    isaki 		if ((fd = open(path, 0)) >= 0) {
    196  1.5    isaki 			if (read(fd, &voice, sizeof(voice)) != sizeof(voice))
    197  1.6    isaki 				err(1, "cannot read voice parameter");
    198  1.5    isaki 			close(fd);
    199  1.5    isaki 		} else {
    200  1.6    isaki 			err(1, "cannot open voice parameter");
    201  1.5    isaki 		}
    202  1.1      oki 	}
    203  1.1      oki 
    204  1.5    isaki 	if ((fd = open("/dev/bell", O_RDWR)) < 0)
    205  1.6    isaki 		err(1, "cannot open /dev/bell");
    206  1.5    isaki 	if (ioctl(fd, BELLIOCSVOICE, &voice))
    207  1.6    isaki 		err(1, "ioctl BELLIOCSVOICE failed");
    208  1.5    isaki 
    209  1.5    isaki 	close(fd);
    210  1.1      oki }
    211  1.1      oki 
    212  1.1      oki void
    213  1.1      oki set_bell_param(void)
    214  1.1      oki {
    215  1.5    isaki 	int fd;
    216  1.5    isaki 	struct bell_info param;
    217  1.1      oki 
    218  1.5    isaki 	if ((fd = open("/dev/bell", O_RDWR)) < 0)
    219  1.6    isaki 		err(1, "cannot open /dev/bell");
    220  1.5    isaki 	if (ioctl(fd, BELLIOCGPARAM, &param))
    221  1.6    isaki 		err(1, "ioctl BELLIOCGPARAM failed");
    222  1.5    isaki 
    223  1.5    isaki 	if (values.volume == DEFAULT)
    224  1.5    isaki 		values.volume = param.volume;
    225  1.5    isaki 	if (values.pitch == DEFAULT)
    226  1.5    isaki 		values.pitch = param.pitch;
    227  1.5    isaki 	if (values.msec == DEFAULT)
    228  1.5    isaki 		values.msec = param.msec;
    229  1.1      oki 
    230  1.5    isaki 	if (ioctl(fd, BELLIOCSPARAM, &values))
    231  1.6    isaki 		err(1, "ioctl BELLIOCSPARAM failed");
    232  1.5    isaki 
    233  1.5    isaki 	close(fd);
    234  1.1      oki }
    235  1.1      oki 
    236  1.1      oki int
    237  1.5    isaki usage(char *fmt, char *arg)
    238  1.5    isaki {
    239  1.5    isaki 	if (fmt) {
    240  1.7    isaki 		fprintf(stderr, "%s:  ", progName);
    241  1.7    isaki 		fprintf(stderr, fmt, arg);
    242  1.7    isaki 		fprintf(stderr, "\n\n");
    243  1.5    isaki 	}
    244  1.5    isaki 
    245  1.5    isaki 	fprintf(stderr, "usage:  %s option ...\n", progName);
    246  1.5    isaki 	fprintf(stderr, "	To turn bell off:\n");
    247  1.5    isaki 	fprintf(stderr, "\t-b				b off"
    248  1.5    isaki 	                "			   b 0\n");
    249  1.5    isaki 	fprintf(stderr, "	To set bell volume, pitch and duration:\n");
    250  1.5    isaki 	fprintf(stderr, "\t b [vol [pitch [dur]]]		  b on\n");
    251  1.5    isaki 	fprintf(stderr, "	To restore default voice parameter:\n");
    252  1.5    isaki 	fprintf(stderr, "\t v default\n");
    253  1.5    isaki 	fprintf(stderr, "	To set voice parameter:\n");
    254  1.5    isaki 	fprintf(stderr, "\t-v voicefile\n");
    255  1.5    isaki 	exit(0);
    256  1.1      oki }
    257