File: rexima.c

package info (click to toggle)
rexima 1.4-8
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid, stretch
  • size: 172 kB
  • ctags: 82
  • sloc: ansic: 845; makefile: 50; sh: 23
file content (578 lines) | stat: -rw-r--r-- 13,180 bytes parent folder | download | duplicates (2)
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* rexima 1.4 - a curses-based (and command-line) mixer for Linux.
 * Copyright (C) 1996-2003 Russell Marks.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <curses.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>


#define REXIMA_VER	"1.4"


#define DEV_Y_START	3
#define DEV_X_START	4
#define DEV_X_DEVEND	(DEV_X_START+9)
#define DEV_X_BAR	(DEV_X_DEVEND+3)
#define DEV_X_PCNT	(DEV_X_BAR+55)
#define DEV_X_REC	(DEV_X_PCNT+5)


char dev_labels[SOUND_MIXER_NRDEVICES][80]=SOUND_DEVICE_LABELS;
char dev_names [SOUND_MIXER_NRDEVICES][80]=SOUND_DEVICE_NAMES;
int dev_line[SOUND_MIXER_NRDEVICES];


char *mixerdev="/dev/mixer";


/* equivalents of optopt, opterr, optind, and optarg */
int optnopt=0,optnerr=0,optnind=1;
char *optnarg=NULL;

/* holds offset in current argv[] value */
static int optnpos=1;


/* This routine assumes that the caller is pretty sane and doesn't
 * try passing an invalid 'optstring' or varying argc/argv.
 */
int getoptn(int argc,char *argv[],char *optstring)
{
char *ptr;

/* check for end of arg list */
if(optnind==argc || *(argv[optnind])!='-' || strlen(argv[optnind])<=1)
  return(-1);

if((ptr=strchr(optstring,argv[optnind][optnpos]))==NULL)
  return('?');		/* error: unknown option */
else
  {
  optnopt=*ptr;
  if(ptr[1]==':')
    {
    if(optnind==argc-1) return(':');	/* error: missing option */
    optnarg=argv[optnind+1];
    optnpos=1;
    optnind+=2;
    return(optnopt);	/* return early, avoiding the normal increment */
    }
  }

/* now increment position ready for next time.
 * no checking is done for the end of args yet - this is done on
 * the next call.
 */
optnpos++;
if(optnpos>strlen(argv[optnind]))
  {
  optnpos=1;
  optnind++;
  }

return(optnopt);	/* return the found option */
}



void die(char *str)
{
fprintf(stderr,"rexima: couldn't %s.\n",str);
exit(1);
}


void init(int *mixfd,int *existmask,int *canrecmask,int *isrecmask,
          int *stereomask)
{
if(((*mixfd)=open(mixerdev,O_RDWR))<0) die("open mixer device");
if(ioctl(*mixfd,SOUND_MIXER_READ_DEVMASK,existmask)==-1) die("ioctl");
if(ioctl(*mixfd,SOUND_MIXER_READ_RECMASK,canrecmask)==-1) die("ioctl");
if(ioctl(*mixfd,SOUND_MIXER_READ_RECSRC,isrecmask)==-1) die("ioctl");
/* this looks like a `recent' addition [in 1996 maybe ;-)] so be lenient */
if(ioctl(*mixfd,SOUND_MIXER_READ_STEREODEVS,stereomask)==-1) *stereomask=0;
}


void init_term()
{
initscr(); cbreak(); noecho();
keypad(stdscr,TRUE);
}


void uninit(int mixfd)
{
clear(); refresh();
echo(); nocbreak(); endwin();
putchar('\n');
close(mixfd);
}


void drawsel(int new,int old)
{
if(new!=old)
  {
  if(old>=0)
    {
    mvaddstr(DEV_Y_START+dev_line[old],DEV_X_START-3,"  ");
    mvaddstr(DEV_Y_START+dev_line[old],DEV_X_DEVEND,"  ");
    }
  
  if(new>=0)
    {
    mvaddstr(DEV_Y_START+dev_line[new],DEV_X_START-3,"->");
    mvaddstr(DEV_Y_START+dev_line[new],DEV_X_DEVEND,"<-");
    }
  }

/* this'll be the last thing before a refresh, so... */
move(DEV_Y_START+dev_line[new],0);
}


int mixer_getlevel_stereo(int mixfd,int dev)
{
int level=0;

ioctl(mixfd,MIXER_READ(dev),&level);
return(level);
}


int mixer_getlevel(int mixfd,int dev)
{
return(mixer_getlevel_stereo(mixfd,dev)&255);
}


void mixer_setlevel_stereo(int mixfd,int dev,int left,int right)
{
left+=256*right;
ioctl(mixfd,MIXER_WRITE(dev),&left);
}


void mixer_setlevel(int mixfd,int dev,int level)
{
mixer_setlevel_stereo(mixfd,dev,level,level);
}


void mixer_change(int mixfd,int dev,int add)
{
int level=mixer_getlevel(mixfd,dev)+add;

if(level<0) level=0;
if(level>100) level=100;
mixer_setlevel(mixfd,dev,level);
}


void mixer_rectoggle(int mixfd,int dev,int *isrecmask)
{
(*isrecmask)^=(1<<dev);
ioctl(mixfd,SOUND_MIXER_WRITE_RECSRC,isrecmask);
}


void mixer_recset(int mixfd,int dev,int *isrecmask,int on)
{
if(on)
  (*isrecmask)|= (1<<dev);
else
  (*isrecmask)&=~(1<<dev);
ioctl(mixfd,SOUND_MIXER_WRITE_RECSRC,isrecmask);
}


void drawlevel(int dev,int level)
{
char buf[60];
int f;

/* sanity check */
if(level<0) level=0;
if(level>100) level=100;

memset(buf,'=',51);
buf[51]=0;
buf[level/2]='|';
for(f=level/2+1;f<51;f++)
  buf[f]='-';

mvaddstr(DEV_Y_START+dev_line[dev],DEV_X_BAR+1,buf);

sprintf(buf,"%3d%%",level);
mvaddstr(DEV_Y_START+dev_line[dev],DEV_X_PCNT,buf);
}


void drawrec(int dev,int on)
{
mvaddch(DEV_Y_START+dev_line[dev],DEV_X_REC+1,on?'R':' ');
}


void setupframe(int existmask,int canrecmask,int *firstdevp,int *lastdevp)
{
int f;
int offset=0;

mvaddstr(0,36,"rexima");
mvaddstr(DEV_Y_START-1,DEV_X_BAR+1,
         "min  .    .    .    .    :    .    .    .    .  max");

/* we know existmask is non-zero by now (see main()) */
for(f=0;f<SOUND_MIXER_NRDEVICES;f++)
  {
  if(existmask&(1<<f))
    {
    if(offset==0) *firstdevp=f;
    *lastdevp=f;
    dev_line[f]=offset++;
    mvaddstr(DEV_Y_START+dev_line[f],DEV_X_START,dev_labels[f]);
    mvaddch(DEV_Y_START+dev_line[f],DEV_X_BAR,'[');
    mvaddch(DEV_Y_START+dev_line[f],DEV_X_BAR+52,']');
    if(canrecmask&(1<<f))
      mvaddstr(DEV_Y_START+dev_line[f],DEV_X_REC,"[ ]");
    }
  }

mvaddstr(LINES-1,2,
         "< move/alter with hjkl/cursors; space to toggle rec src; "
         "Esc/q/x to quit >");
}



void usage_help(int existmask)
{
int f,count;

puts(
"rexima " REXIMA_VER " - copyright (c) 1996-2003 Russell Marks.\n"
"\n"
"usage: rexima [-hv] [-d mixer_device_file]\n"
"\t\t    [device <level | offset | left,right | <rec | norec>>\n"
"\t\t     [device ...]]\n"
"\n"
"	-d	specify mixer device file to use (ordinarily /dev/mixer).\n"
"	-h	give this usage help.\n"
"	-v	show current mixer settings.\n");
printf(
"	device	a device to set the level of. %s\n",mixerdev);
printf("\t\twill allow the levels of these devices to be set:\n\n\t\t");

count=0;
for(f=0;f<SOUND_MIXER_NRDEVICES;f++)
  {
  if(existmask&(1<<f))
    printf("%s ",dev_names[f]),count++;
  if(count>=7) printf("\n\t\t"),count=0;
  }

puts(
"\n\n"
"        level	level to set specified device to.\n"
"       offset	amount to change level by (e.g. `-3', `+12').\n"
"   left,right	set (stereo) device's level with independent left/right values.\n"
"  rec | norec	`rec' makes device a recording source, `norec' makes it, well,\n"
"		not a recording source. :-)\n"
"\n"
"If invoked without any args (with the exception of `-d'), rexima runs\n"
"interactively.");
}


/* on entry, we know argc>=2 */
void cmdline_main(int argc,char *argv[])
{
int mixfd;
int existmask,canrecmask,isrecmask,stereomask;
int f,tmp,l,r;
int found;
char *ptr;
int done=0,want_usage=0,want_levels=0;

do
  switch(getoptn(argc,argv,"d:hvV"))
    {
    case 'd':	/* mixer device */
      if((mixerdev=malloc(strlen(optnarg)+1))==NULL)
        die("allocate memory");
      strcpy(mixerdev,optnarg);
      break;
    case 'h':
      want_usage=1;
      break;
    case 'v': case 'V':	/* show levels */
      want_levels=1;
      break;
    case '?':
      switch(optnopt)
        {
        case 'd':
          fprintf(stderr,"rexima: "
                  "the `-d' option needs a mixer device to be specified.\n");
          break;
        default:
          fprintf(stderr,"rexima: option `%c' not recognised.\n",optnopt);
        }
      exit(1);
    case -1:
      done=1;
    }
while(!done);


if(want_usage)
  {
  init(&mixfd,&existmask,&canrecmask,&isrecmask,&stereomask);
  
  usage_help(existmask);
  
  exit(0);
  }

if(want_levels)
  {
  init(&mixfd,&existmask,&canrecmask,&isrecmask,&stereomask);
  
  for(f=0;f<SOUND_MIXER_NRDEVICES;f++)
    if(existmask&(1<<f))
      {
      tmp=mixer_getlevel_stereo(mixfd,f);
      l=(tmp&255); r=((tmp>>8)&255);
      printf("%s\t%3d",dev_names[f],l);
      if(stereomask&(1<<f)) printf(",%3d",r);
      if(canrecmask&(1<<f))
        printf("\t[%c]",(isrecmask&(1<<f))?'R':' ');
      putchar('\n');
      }
  
  exit(0);
  }

/* if there aren't any "device, setting" pairs, return (without init). */
if(optnind>=argc)
  return;

/* otherwise, init - we have one or more pairs to deal with. */

init(&mixfd,&existmask,&canrecmask,&isrecmask,&stereomask);

while(optnind<argc)
  {
  /* lookup argv[1] in dev_names[] */
  found=0;
  for(f=0;f<SOUND_MIXER_NRDEVICES;f++)
    if(existmask&(1<<f) && strcmp(argv[optnind],dev_names[f])==0)
      {
      found=1;
      break;
      }
  
  if(!found)
    {
    fprintf(stderr,"rexima: unavailable or unknown device `%s'.\n",argv[optnind]);
    exit(1);
    }
  
  if(argc-optnind<2)
    {
    fprintf(stderr,"rexima: no setting specified for device `%s'.\n",argv[optnind]);
    exit(1);
    }
  
  optnind++;
  
  tmp=f;	/* save dev no. */
  
  /* setting can be mono, stereo, or rec/norec.
   * first see if it's rec or norec:
   */
  if(strcmp(argv[optnind],"rec")==0 || strcmp(argv[optnind],"norec")==0)
    {
    if(!(canrecmask&(1<<f)))
      {
      fprintf(stderr,"rexima: can't set rec/norec on device `%s'.\n",
              argv[optnind-1]);
      exit(1);
      }
    
    mixer_recset(mixfd,tmp,&isrecmask,(argv[optnind][0]=='r'));
    }
  else
    {
    int offset_sign=0;
    
    /* see if it's an offset */
    if(argv[optnind][0]=='+')
      offset_sign=1;
    if(argv[optnind][0]=='-')
      offset_sign=-1;

    /* be sure to puke if level contains funny chars */
    for(f=(offset_sign?1:0);f<strlen(argv[optnind]);f++)
      if(strchr("0123456789, \t",argv[optnind][f])==NULL)
        {
        fprintf(stderr,"rexima: invalid level for device `%s'.\n",
                argv[optnind-1]);
        exit(1);
        }
    
    /* now check for stereo */
    if((ptr=strchr(argv[optnind],','))!=NULL)
      {
      if(offset_sign)
        {
        fprintf(stderr,"rexima: level offset must be mono, not stereo.\n");
        exit(1);
        }
      
      l=atoi(argv[optnind]);
      r=atoi(ptr+1);
      mixer_setlevel_stereo(mixfd,tmp,l,r);
      }
    else
      {
      /* must be mono; default to 2 for an offset with no size specified. */
      if(offset_sign)
        mixer_change(mixfd,tmp,
                     (strlen(argv[optnind])<2)?
                     2*offset_sign:atoi(argv[optnind]));
      else
        mixer_setlevel(mixfd,tmp,atoi(argv[optnind]));
      }
    }
    
  optnind++;
  }

exit(0);
}



int main(int argc,char *argv[])
{
int quit=0;
int cursel;	/* currently selected device */
int oldsel;
int mixfd;
int key;
int existmask,canrecmask,isrecmask,stereomask;
int firstdev,lastdev;
int f;

if(argc>1)
  cmdline_main(argc,argv);  /* returns (without init) if we're interactive */

init(&mixfd,&existmask,&canrecmask,&isrecmask,&stereomask);

/* later things assume existmask is non-zero, so... */
if(!existmask)
  fprintf(stderr,"rexima: mixer has no devices!\n"),exit(1);

init_term();
setupframe(existmask,canrecmask,&firstdev,&lastdev);
cursel=firstdev;

for(f=0;f<SOUND_MIXER_NRDEVICES;f++)
  if(existmask&(1<<f))
    {
    drawlevel(f,mixer_getlevel(mixfd,f));
    if(canrecmask&(1<<f))
      drawrec(f,isrecmask&(1<<f));
    }

drawsel(cursel,-1);
refresh();

while(!quit)
  {
  oldsel=cursel;
  
  key=getch();
  
  switch(key)
    {
    case 'q': case 'x': case 27:
      quit=1; break;
    case 'k': case KEY_UP:
      do
        cursel--;
      while(cursel>=firstdev && !(existmask&(1<<cursel)));
      if(cursel<firstdev) cursel=lastdev;
      break;
    case 'j': case KEY_DOWN:
      do
        cursel++;
      while(cursel<=lastdev && !(existmask&(1<<cursel)));
      if(cursel>lastdev) cursel=firstdev;
      break;
    case 'h': case 'H': case KEY_LEFT: case '-':
      if(existmask&(1<<cursel))
        {
        mixer_change(mixfd,cursel,(key=='H')?-1:-2);
        drawlevel(cursel,mixer_getlevel(mixfd,cursel));
        }
      break;
    case 'l': case 'L': case KEY_RIGHT: case '+': case '=':
      if(existmask&(1<<cursel))
        {
        mixer_change(mixfd,cursel,(key=='L')?1:2);
        drawlevel(cursel,mixer_getlevel(mixfd,cursel));
        }
      break;
    case ' ':
      if(canrecmask&(1<<cursel))
        {
        mixer_rectoggle(mixfd,cursel,&isrecmask);
        drawrec(cursel,isrecmask&(1<<cursel));
        }
      break;
    case 'R'-0x40: case 'L'-0x40:	/* ^R and ^L */
      clearok(curscr,TRUE);
      break;
    }

  /* tmancill@debian.org - add 1-9 keys */
  if (key >= 0x31 && key <= 0x39) {
    if(existmask&(1<<cursel))
      {
        mixer_setlevel(mixfd,cursel,(key - 0x30)*10);
        drawlevel(cursel,mixer_getlevel(mixfd,cursel));
      }
    }

  drawsel(cursel,oldsel);
  
  refresh();
  }

uninit(mixfd);
exit(0);
}