1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/* smix.c
* Simple Linux Mixer controller
* Jim Jackson Fri 16 Jun 1995
*/
/*
smix: (version 1) Nov 1996
based on the dos sb16 mixer......
smix: (version 1) Fri 16 Jun 1995
NAME:
smix - Simple Linux Mixer controller
SYNOPSIS:
smix [-v] [-h] [-o file] [-i file] [command(s)]
DESCRIPTION:
Controls the Mixer settings from the command line parameter(s).
The commands are detailed below, capitals showing the minimum abbreviation
allowed. Upper or lower case can be used on the command line. All Volume
settings are in range 0-100 (0 min, 100 max), but these are scaled to the
mixers actual range, hence set volume may be slightly different.
SHow outputs the settings of the mixer. This is the default
if no command line parameters are given
dev N or L,R sets mixer device 'dev' to volume N, or to seperate
left and right stereo volume L,R
If device doesn't support stereo settings then max of L,R
is used.
INput dev set the DSP input to be 'dev' or 'NOne' to turn inputs off
Verbose makes the program output the settings after doing the
commands
Use '-' as a filename to indicate standard input.
FLAGS:
-state outputs details is a form that can used as the command
line parameters to an smix command. This can be used
to save the settings in a shell variable and then reset them.
-h lists usage summary, which also lists the mixer devices and
the possible input devices.
-v verbose - outputs the results of commands. Same as Verbose above
OPTIONS:
-i file read commands from file
-o file output to file file
-m mixerdev mixer device to use
-C configfile read configfile
NOTES:
BUGS:
Any questions, suggestions or problems, etc. should be sent to
Jim Jackson
Email: jj@franjam.org.uk
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "config.h"
#include "mixer.h"
#define PROGNAME "smix"
#define EFAIL -1
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE !FALSE
#endif
#define nl puts("")
#define option(b,a) ((n>=a)&&(strncasecmp(p,b,n)==0))
#define doshow(F) printvols(devmask,F)
char mixname[130];
int mixer; /* Mixer File Descriptor */
char *outfile;
char *infile;
char *configfile;
char *sys;
int state;
int vflg;
int hflg;
char *strupper(),*reads(),*sindex(),*tab();
FILE *fopen();
unsigned char readmixer();
main(argc,argv)
int argc;
char **argv;
{ int i,j,n,st;
char fname[132],*p;
FILE *fpr,*fpw;
if ((p=strrchr(sys=*argv++,'/'))!=NULL) sys=++p;
argc--;
configfile=DEF_CONF_FILENAME;
infile=outfile=NULL;
mixname[0]=0;
vflg=hflg=FALSE;
state=FALSE;
while (argc && **argv=='-') {
n=strlen(p=(*argv++)+1); argc--;
if (option("output",1)) {
if (argc) {
outfile=*argv++; argc--;
}
}
else if (option("input",1)) {
if (argc) {
infile=*argv++; argc--;
}
}
else if (option("Config",1)) {
if (argc) {
configfile=*argv++; argc--;
}
}
else if (option("mixer",1)) {
if (argc && **argv!='-') {
strncpy(mixname,*argv++,sizeof(mixname)-1); mixname[sizeof(mixname)-1]=0; argc--;
}
}
else if (option("state",1)) {
state=TRUE;
}
else {
for (; *p; p++) {
if (*p=='h') hflg++;
else if (*p=='v') vflg++;
else {
*fname='-'; *(fname+1)=*p; *(fname+2)=0;
exit(err_rpt(EINVAL,fname));
}
}
}
}
/* interrogate config file....... */
init_conf_files(configfile,DEF_CONF_FILENAME,DEF_GLOBAL_CONF_FILE,vflg);
if (vflg==0) {
vflg=atoi(get_conf_value(sys,"verbose","0"));
}
if (mixname[0]==0) {
strncpy(mixname,get_conf_value(sys,"mixerfile",MIXER_FILE),sizeof(mixname)-1);
mixname[sizeof(mixname)-1]=0;
}
if ((mixer=mixinit(mixname)) < 0) {
exit(err_rpt(errno,mixname));
}
if (hflg) exit(help());
if (vflg) printf("Mixer %s\n",mixname);
if (outfile!=NULL)
{ if (strcmp(outfile,"-")==0) fpw=stdout;
else if ((fpw=fopen(outfile,"w"))==NULL)
{ exit(err_rpt(errno,outfile)); }
}
else
{ fpw=stdout; }
if (infile!=NULL)
{ if (strcmp(infile,"-")==0) fpr=stdin;
else if ((fpr=fopen(infile,"r"))==NULL)
{ exit(err_rpt(errno,infile)); }
}
else
{ fpr=stdin; }
if (infile!=NULL)
{ st=docmdfile(infile,fpr,fpw); /* do any command file first */
st+=docmds(argc,argv,fpw);
}
else if (argc)
st+=docmds(argc,argv,fpw);
else
st+=docmdline("show",fpw);
exit(st);
}
docmdfile(fn,f,fo)
char *fn;
FILE *f,*fo;
{
char bf[514],*p;
while ((p=fgets(bf,512,f))!=NULL) { docmdline(bf,fo); }
}
docmdline(s,fo)
char *s;
FILE *fo;
{
int n,v;
char *aa[64],c,*p,*r,*q;
for ( ; *s && isspace(*s); s++) {} /* skip initial whitespace */
if (*s=='#') return(0); /* comment line - ignore */
for (n=0; *s; n++)
{
aa[n]=s++;
for ( ; *s && !isspace(*s); s++ ) {}
if (isspace(*s)) *s++=0;
for ( ; *s && isspace(*s); s++) {}
}
docmds(n,aa,fo);
}
docmds(c,v,fo)
int c;
char **v;
FILE *fo;
{
int i,st,n,d,dopr;
char *p;
for ( ; c; )
{
n=strlen(p=*v++); c--;
if (option("show",2)) doshow(fo);
else if (option("verbose",1)) vflg++;
else if (option("input",2)) {
dopr=0;
if ( c && ((d=isdev(p=*v))>=0 || option("none",2))) {
v++; c--;
if ( (d>=0) && IS_MIX_INPUT_DEV(d)==0 ) {
fprintf(stderr,"%s is not a source device.\n",MIX_DEV_NAME(d));
} else {
if ( SET_MIX_INPUT_DEV(d) < 0) {
fprintf(stderr,"Couldn't set %s as recording source.\n",
(d>=0)?(MIX_DEV_NAME(d)):"no device");
}
if (vflg) dopr=1;
}
} else { dopr=1; }
if (dopr) {
if (sources) printvols(sources,fo);
else fprintf(fo,"No source inputs active.");
}
} else if (option("all",3)) {
if (c && isvol(*v)) {
p=*v++; c--;
doallvols(devmask,p,fo);
} else doshow(fo);
} else if ((d=isdev(p,n))>=0) {
if (c && isvol(*v)) {
p=*v++; c--;
} else p="";
dovol(d,p,fo);
} else {
fprintf(stderr,"Command '%s' not recognised.\n",p);
st=1;
}
}
}
isvol(s)
char *s;
{
return(isdigit(*s) ||
(strncasecmp(s,"off",3)==0) ||
(strncasecmp(s,"full",4)==0));
}
/* dovol(d,s) set mixer device d to volume specified by string s
* s is an "NN" or "LL,RR", i.e. a numeric ascii string
* or two num ascii strings for stereo left and right settings
*/
dovol(d,s,fo)
int d;
char *s;
FILE *fo;
{
int lv,rv,n,dopr;
char buf[130];
dopr=vflg;
if (s!=NULL && *s) {
if (strncasecmp(s,"off",3)==0) { lv=0; s+=3; }
else if (strncasecmp(s,"full",4)==0) { lv=100; s+=4; }
else {
for ( lv=0; isdigit(*s); s++) { lv=lv*10+(*s-'0'); }
}
if (*s == ',' || *s == '/') {
s++;
if (strcasecmp(s,"off")==0) { rv=0; }
else if (strcasecmp(s,"full")==0) { rv=100; }
else {
for ( rv=0; isdigit(*s); s++) { rv=rv*10+(*s-'0'); }
}
} else rv=lv;
if (IS_MIX_DEV_STEREO(d)) n=(rv<<8)+lv;
else n=(lv>rv)?lv:rv;
if (SET_MIX_VALUE(d,n) < 0) {
sprintf(buf,"Problem setting level of mixer channel %s.",
MIX_DEV_NAME(d));
err_rpt(ENODEV,buf);
}
} else { dopr=1; }
if (dopr) printvol(d,fo);
}
printvols(devs,fo)
int devs;
FILE *fo;
{
int i;
for (i=0; i<SOUND_MIXER_NRDEVICES; i++) {
if (devs&(1<<i)) {
printvol(i,fo);
}
}
}
doallvols(devs,s,fo)
int devs,s;
FILE *fo;
{
int i;
for (i=0; i<SOUND_MIXER_NRDEVICES; i++) {
if (devs&(1<<i)) {
dovol(i,s,fo);
}
}
}
printvol(d,fo)
int d;
FILE *fo;
{
int n;
char buf[130];
if (IS_MIX_DEV(d)) {
if (GET_MIX_VALUE(d,n) < 0) {
sprintf(buf,"Problem reading current level of mixer channel %s.",
MIX_DEV_NAME(d));
err_rpt(ENODEV,buf);
} else {
fprintf(fo,"%-6s %3d",MIX_DEV_NAME(d),n&255);
if (IS_MIX_DEV_STEREO(d)) { fprintf(fo,"/%-3d ",(n>>8)&255);
} else { fprintf(fo," "); }
if (state) {
fprintf(fo,"\n");
if (IS_MIX_SOURCE(d)) fprintf(fo,"INPUT %s\n",MIX_DEV_NAME(d));
} else {
fprintf(fo,"%2s ", (IS_MIX_INPUT_DEV(d))? "*":"");
fprintf(fo,"%3s ", (IS_MIX_SOURCE(d))? "<-":"");
fprintf(fo,"\n");
}
}
}
}
help()
{
int i;
printf("\nSet Linux Mixer from the command line - mixer device is %s\n\n",mixname);
printf("Commands are :-\n");
printf(" Verbose or -v verbose mode, outputs results of actions\n");
printf(" DEV N or L,R sets DEV to N or left to L, Right to R\n");
printf(" where DEV can be :\n ");
for (i=0; i<SOUND_MIXER_NRDEVICES; i++) {
if (IS_MIX_DEV(i)) printf("%s ",MIX_DEV_NAME(i));
}
printf("\n INput DEV sets the DSP input to be DEV, where DEV can be :\n ");
for (i=0; i<SOUND_MIXER_NRDEVICES; i++) {
if (IS_MIX_INPUT_DEV(i)) printf("%s ",MIX_DEV_NAME(i));
}
printf("\n\n");
printf("Default Config files are \"%s\", \"$HOME/%s\" and\n\"%s\", searched in that order.\n\n",
configfile, DEF_CONF_FILENAME, DEF_GLOBAL_CONF_FILE);
printf("Usage: %s [-h] [-v] [-i cmdfile] [-o file] [-m mixdev] [command(s)]\n\n",sys);
printf(" -C file use file as local configuration file\n");
printf(" -m mixdev use mixdev as mixer instead of default /dev/mixer\n");
printf(" -o file put output in file instead of to standard output\n");
printf(" -i file take commands from file, after the command line\n");
printf(" -s output settings in form to be used as an input file\n");
printf("\n");
}
/***ERR_RPT***/
/* err_rpt(err,msg)
*/
err_rpt(err,msg)
short int err;
char *msg;
{
extern char *sys;
if (err) fprintf(stderr,"[%s] %s : %s\n",sys,strerror(err),msg);
return(err);
}
/***STRUPPER***
* char *strupper(p) convert all lower case chars to upper in str p
*/
char *strupper(p)
char *p;
{ char *i,d;
d='a'-'A';
for (i=p ; *i ; i++)
if ((*i>='a') && (*i<='z')) *i-=d ;
return(p);
}
/***DELNL***/
/* char *delnl(s) remove trailing nl from end of string s
* return same pointer as given
*/
char *delnl(s)
char *s;
{ char *p;
if (s!=NULL && *s && *(p=s+strlen(s)-1)=='\n') *p=0;
return(s);
}
|