"Fossies" - the Fresh Open Source Software Archive

Member "alsa-oss-1.1.8/test/lmixer.cc" (7 Jan 2019, 7313 Bytes) of package /linux/misc/alsa-oss-1.1.8.tar.bz2:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 //  lmixer
    2 //  
    3 //  Written by Brandon Zehm
    4 //  caspian@linuxfreak.com
    5 //
    6 //  This software is released under the GNU GPL license.
    7 //  For details see http://www.gnu.org/copyleft/gpl.html
    8 //
    9 //  If you have patches, suggestions, comments, or anything
   10 //  else to tell me, please feel free to email me.
   11 //
   12 
   13 #define MIXERVERSION "1.0.7"
   14 #define MIXERDEV    "/dev/mixer"
   15 
   16 #include <stdio.h>
   17 #include <stdlib.h>
   18 #include <string.h>
   19 #include <unistd.h>
   20 #include "mixctl.h"
   21 
   22 
   23 // Initialize functions
   24 void help();
   25 void version();
   26 void scanArgs(int argc, char **argv);
   27 void ShowChannelInfo();
   28 int VerifyChannelName();
   29 int open_mixer();
   30 
   31 
   32 // Define Variables
   33 char mixdev[256]=MIXERDEV;    // The var with the mixer device
   34 int volume=255;               // Set volume to a value out of range
   35 int channel=255;              // Set channel to a value out of range
   36 char channel_name[32]="BLANK";  // 
   37 
   38 // Initialize interface to mixer.h
   39 MixCtl *mixctl;
   40 
   41 
   42 
   43 
   44 
   45 
   46 ////////////////////////////////////////////////////////
   47 // Sub:         main()
   48 // Input:       argc and **argv
   49 ////////////////////////////////////////////////////////
   50 int main(int argc, char **argv)
   51 {
   52    // Get command line parameters.
   53    scanArgs(argc, argv);
   54    
   55    // If neither the volume or the channel got set print help.
   56    if ((volume == 255) && (channel == 255)) {
   57      help();
   58      exit(1);
   59    }
   60    
   61    // Check to see if the volume got set
   62    if (volume == 255) {
   63      printf ("You must set a volume!\n");
   64      help();
   65      exit(1);
   66    }
   67    
   68    // Check to see if the volume got set
   69    if(strcmp(channel_name, "BLANK")==0) {
   70      printf ("You must set a mixer channel!\n");
   71      help();
   72      exit(1);
   73    }
   74    
   75    
   76    // If we get here we assume we are doing the defualt operation
   77    // which is: 
   78    //            1. open mixer 
   79    //            2. write volume to a channel
   80    //            3. quit
   81    
   82    
   83    // Open the mixer or die
   84    if (open_mixer() != 1) {
   85      exit(1);
   86    } 
   87    
   88    // Verify that the incoming channel_name is valid
   89    // and set 'channel' to the corresponding channel number.
   90    if (VerifyChannelName() == 0) {
   91      printf("\'%s\' is not a valid channel name!\n", channel_name);
   92      exit(1);
   93    }
   94    
   95    // Write the volume
   96    mixctl->setBoth(channel,volume,volume);
   97    mixctl->writeVol(channel);
   98    
   99    
  100    // Exit
  101    delete mixctl;
  102    return 0;
  103 }
  104 
  105 
  106 
  107 
  108 
  109 
  110 
  111 
  112 
  113 
  114 ////////////////////////////////////////////////////////
  115 // Sub:         scanArgs()
  116 // Input:       argc and *argv
  117 // Output:      Sets initial variables
  118 // Description: Receives incoming data and directs 
  119 //              the flow of the program.
  120 ////////////////////////////////////////////////////////
  121 void scanArgs(int argc, char *argv[]){
  122    for(int i=1;i<argc;i++){
  123       // Help
  124       if(strcmp(argv[i], "-h")==0 || strcmp(argv[i], "--help")==0){
  125         help();
  126         exit(0);
  127       }
  128       // Set Mixer Device
  129       if(strcmp(argv[i], "-d")==0 || strcmp(argv[i], "--device")==0){
  130         if(i<argc-1){
  131           i++;
  132           sprintf(mixdev, "%s", argv[i]);
  133         }
  134         continue;
  135       }
  136       // Set new volume
  137       if(strcmp(argv[i], "-v")==0 || strcmp(argv[i], "--volume")==0){
  138         if(i<argc-1){
  139           i++;
  140           volume = atoi(argv[i]);
  141         }
  142         continue;
  143       }
  144       // Set new mixer channel
  145       if(strcmp(argv[i], "-c")==0 || strcmp(argv[i], "--channel")==0){
  146         if(i<argc-1){
  147           i++;
  148           sprintf(channel_name, "%s", argv[i]);
  149         }
  150         continue;
  151       }
  152       // Show mixer information
  153       if(strcmp(argv[i], "-i")==0 || strcmp(argv[i], "--info")==0){
  154         if (open_mixer() != 1) {
  155           exit(1);
  156         }
  157         ShowChannelInfo();
  158         exit(0);
  159       }
  160       // Show version information
  161       if(strcmp(argv[i], "-V")==0 || strcmp(argv[i], "--version")==0){
  162         version();
  163         exit(0);
  164       }
  165    }
  166 }
  167 
  168 
  169 
  170 
  171 
  172 
  173 //////////////////////////////////////////////////////////
  174 // Sub:         Help()
  175 // Input:       void
  176 // Output:      
  177 // Description: Prints the help message.
  178 /////////////////////////////////////////////////////////
  179 void help() {
  180   printf("\n");
  181   printf("lmixer v%s\n", MIXERVERSION);
  182   printf("                                                                       \n");
  183   printf("Usage:   lmixer [options]                                              \n");
  184   printf("                                                                       \n");
  185   printf(" -c,--channel   <channel name>   Mixer channel to adjust               \n");
  186   printf(" -v,--volume    <volume>         Volume (1-100)                        \n");
  187   printf(" -d,--device    <mixer device>   Use specified mixer device            \n");
  188   printf(" -i,--info                       Shows the volume of each mixer device \n");
  189   printf(" -V,--version                    Display version information           \n");
  190   printf(" -h,--help                       Display this help screen              \n");
  191   printf("                                                                       \n");
  192   printf("Typical useage:  'lmixer -c bass -v 85'                                \n");
  193   printf("\n");
  194 }
  195 
  196 
  197 
  198 //////////////////////////////////////////////////////////
  199 // Sub:         version()
  200 // Input:       void
  201 // Description: Prints version informaion
  202 /////////////////////////////////////////////////////////
  203 void version() {
  204   printf("lmixer version %s\n", MIXERVERSION);
  205 }
  206  
  207 
  208 
  209 
  210 ////////////////////////////////////////////////////////
  211 // Sub:         open_mixer()
  212 // Input:       int
  213 // Output:      Returns 1 on success 0 on failure.
  214 // Description: Opens the mixer device or dies.
  215 ////////////////////////////////////////////////////////
  216 int open_mixer () {
  217    // Open the mixer, and verify it worked.
  218    mixctl=new MixCtl(mixdev);
  219    if(!mixctl->openOK()) {
  220       fprintf(stdout,"Unable to open mixer device: %s\n", mixdev);
  221       return(0);
  222    }
  223    return(1);
  224 }
  225 
  226 
  227 
  228 
  229 
  230 
  231 ////////////////////////////////////////////////////////
  232 // Sub:         ShowChannelInfo()
  233 // Input:       void
  234 // Output:      STDOUT
  235 // Description: Shows a list of channels and their 
  236 //              current volumes.
  237 ////////////////////////////////////////////////////////
  238 void ShowChannelInfo () {
  239   for (int i = 0; i < mixctl->getNrDevices(); i++) {            // For every device,
  240     if (mixctl->getSupport(i)) {                                // if the device exists,
  241       volume = mixctl->readVol(i,1);                            // Get the volume
  242       printf ("%s\t%d\n", mixctl->getName(i), volume);          // and print a line.
  243     }
  244   }
  245 }
  246 
  247 
  248 
  249 ////////////////////////////////////////////////////////
  250 // Sub:         VerifyChannelName()
  251 // Input:       Reads global var channel_name
  252 // Output:      returns a 1 on success 0 on failure
  253 // Description: Checks to see if 'channel_name' is
  254 //              a valid mixer device, and if it
  255 //              is it sets 'channel' to the number of
  256 //              that device.
  257 ////////////////////////////////////////////////////////
  258 int VerifyChannelName () {
  259   for (int i = 0; i < mixctl->getNrDevices(); i++) {            // For every device,
  260     if (mixctl->getSupport(i)) {                                // if the device exists,
  261       if(strcmp(channel_name, mixctl->getName(i))==0) {
  262         channel = i;
  263         return 1;
  264       }
  265     }
  266   }
  267   return 0;
  268 }
  269 
  270 
  271 
  272 
  273 
  274 
  275 
  276 
  277 
  278