home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / C / VOLUME.ZIP / VOLUME.C
Encoding:
C/C++ Source or Header  |  1997-01-09  |  2.8 KB  |  103 lines

  1. /* volume.c   version 1.1   Sat Mar 30 18:44:45 EST 1996
  2.  * Author:  Andy Isaacson <adisaacs@mtu.edu>
  3.  *
  4.  * sets the audio volume on SPARCstations running 4.*
  5.  *
  6.  * Copyright 1996 Andrew Isaacson
  7.  * this software comes with no warranty, either express or implied.
  8.  * This software may be redistributed so long as this copyright notice 
  9.  * is retained on all redistributions.
  10.  *
  11.  * Changes for version 1.1:
  12.  *   prints the range when printing the volume
  13.  *   more informative output
  14.  *   accepts -q option for quieter operation
  15.  */
  16.  
  17. #include <stdio.h>
  18. #include <sys/ioctl.h>
  19. #include <sun/audioio.h>
  20. #include <fcntl.h>
  21. #include <errno.h>
  22.  
  23. void printvol(int);
  24. void setvol(int,int);
  25. void printusage(char *);
  26.  
  27. audio_info_t info;
  28. /*int audiofd = 0;*/
  29. main(int argc, char* argv[]) {
  30.  
  31.    int err, vol;
  32.    char opt;
  33.    extern char *optarg;
  34.    extern int optind, opterr;
  35.    opt = getopt(argc, argv, "q");
  36.    if (opt == -1 && argv[1])
  37.       setvol(atoi(argv[1]), 1);
  38.    else if ( opt == '?')
  39.       printusage(argv[0]);
  40.    else if (opt == 'q') {
  41.       if (argv[2]) {
  42.          vol = atoi (argv[2]);
  43.          setvol(vol,0);
  44.       } else
  45.          printvol(0);
  46.    } else
  47.       printvol(1);
  48. }
  49.  
  50. void printvol(int format)
  51. {
  52.    int err,audiofd = open ("/dev/audioctl", O_RDONLY);
  53.    if (audiofd < 1) {
  54.       perror("volume: /dev/audioctl");
  55.       exit (-1);
  56.    };
  57.    err = ioctl(audiofd, AUDIO_GETINFO, &info);
  58.    if (err) {
  59.       printf("Error %i occurred while getting the volume.\n",err);
  60.       perror ("volume");
  61.       exit(-1);
  62.    }
  63.    if (format)
  64.       printf ("The current audio volume is %i [%i to %i]\n",info.play.gain,
  65.               AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
  66.    else
  67.       printf ("%i\n",info.play.gain);
  68. }
  69.  
  70. void setvol(int newvol, int format)
  71. {
  72.    int err, audiofd = open ("/dev/audioctl", O_RDONLY);
  73.    if (audiofd < 1) {
  74.       perror("volume: /dev/audioctl");
  75.       exit (-1);
  76.    }
  77.    if ((newvol < AUDIO_MIN_GAIN)||(newvol > AUDIO_MAX_GAIN)) {
  78.       printf("Invalid volume: %i. Should be in the range %i to %i.\n",newvol,
  79.              AUDIO_MIN_GAIN, AUDIO_MAX_GAIN);
  80.       exit(-1);
  81.    }
  82.    AUDIO_INITINFO(&info);
  83.    info.play.gain = newvol;
  84.     err = ioctl(audiofd, AUDIO_SETINFO, &info);
  85.    if (err) {
  86.       printf("Error %i occurred while setting the volume.\n",err);
  87.       perror ("volume");
  88.       exit(-1);
  89.    }
  90.    printvol(format);
  91. }
  92.  
  93. void printusage(char *progname)
  94. {
  95.    printf("%s [-q] [vol]: change or view audio volume\n", progname);
  96.    puts(  "volume 1.1    Andrew Isaacson");
  97.    puts("\nOptions:");
  98.    puts("\n     -q     quiet; print just integer value of volume");
  99.    puts(  "     -q vol quiet; set volume and exit");
  100.    puts(  "     vol    set volume and print new volume");
  101.    puts("\n     default: print volume verbosely");
  102. }
  103.