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