#include #include #include #include #include #include #include #include #include "config.h" #include "engine.h" #include "line.h" void usage_quit(void); int *sepVol(char *newValue); void modifyVol(const char *nameofDevice, char *newValue); void writevol(const char *nameofDevice, char *newValue); void readvol(const char *nameofDevice); void printProfiles(void); void printAllsets(void); void saveConf(const char *filename); void readConf(const char *filename); void deleteConf(const char *filename); void rec(const int opt, const char *devicename); const char *labels[] = SOUND_DEVICE_LABELS; const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES; int linemain(char *argv[]) { if(!strncmp(argv[1], "-h", strlen("-h"))) usage_quit(); else if(!strncmp(argv[1], "-v", strlen("-v"))) { fprintf(stdout, "Ermixer 0.8 by NerdSoft (C) 2002\n"); exit(0); } initMixer(); initMask(); if(!strncmp(argv[1], "-a", strlen("-a"))) printAllsets(); else if(!strncmp(argv[1], "-s", strlen("-s"))) saveConf(argv[2]); else if(!strncmp(argv[1], "-c", strlen("-c"))) readConf(argv[2]); else if(!strncmp(argv[1], "-d", strlen("-d"))) deleteConf(argv[2]); else if(!strncmp(argv[1], "+rec", strlen("+rec")) || !strncmp(argv[1], "-rec", strlen("-rec"))) { if(!argv[2]) usage_quit(); if(argv[1][0] == '-') rec(MUTE, argv[2]); else rec(REC, argv[2]); } else if(!strncmp(argv[1], "-l", strlen("-l"))) printProfiles(); else { if(argv[2]) { if(argv[2][0] == '+' || argv[2][0] == '-') modifyVol(argv[1], argv[2]); else writevol(argv[1], argv[2]); } else readvol(argv[1]); } close(mixer_fd); exit(0); } int *sepVol(char *newValue) { char *c = NULL; int *result = malloc(sizeof(int) * 2); if(!result) err_quit("No memory Available\n"); c = strtok(newValue, ":"); if(!c) usage_quit(); result[0] = atoi(c); c = strtok(NULL, "\n"); if(!c) result[1] = result[0]; else result[1] = atoi(c); return result; } void readvol(const char *nameofDevice) { int device = 0, *result; device = searchDevice(nameofDevice); if(device == ERROR) err_quit("This device was not recognised, run ermixer with -a flag for a list of supported devices and -h for a list of the options\n"); result = readFromDevice(device); if(!result) err_quit("Unable to read from device"); printf("%s is set to %d:%d\n", names[device], result[0], result[1]); free(result); } void modifyVol(const char *nameofDevice, char *newValue) { int device = 0, *result1 = NULL, *result2 = NULL, left = 0, right = 0; device = searchDevice(nameofDevice); if(device == ERROR) err_quit("This device was not recognised, run ermixer with -a flag for a list of supported devices and -h for a list of the options"); result1 = readFromDevice(device); if(!result1) err_quit("Unable to read from device"); result2 = sepVol(newValue); if(result2 == NULL) usage_quit(); left = result1[0] + result2[0]; right = result1[1] + result2[1]; if(left > 100) left = 100; else if(left < 0) left = 0; if(right > 100) right = 100; else if(right < 0) right = 0; free(result1); free(result2); if((writeToDevice(device, left, right)) == ERROR) { fprintf(stderr, "Unable to set %s to %d:%d\n", labels[device], left, right); exit(1); } fprintf(stdout, "Now %s is set to %d:%d\n", nameofDevice, left, right); } void writevol(const char *nameofDevice, char *newValue) { int device = 0; int *result = NULL; device = searchDevice(nameofDevice); if(device == ERROR) err_quit("This device was not recognised, run ermixer with -a flag for a list of supported devices and -h for a list of the options"); result = sepVol(newValue); if((writeToDevice(device, result[0], result[1])) == ERROR) { fprintf(stderr, "Unable to set %s to %d:%d\n", labels[device], result[0], result[1]); exit(1); } fprintf(stdout, "Now %s is set to %d:%d\n", nameofDevice, result[0], result[1]); free(result); } void printProfiles(void) { int x = 0; if(getProfiles() == ERROR) err_quit("Unable to open ~/.ermixer"); if(!**ret) err_quit("No profiles available\n"); while(*ret[x]) { if(x == 10) exit(0); printf("%-2d -> %s\n", x, ret[x++]); } } void printAllsets(void) { int x = 0; getChannels(); while((devs[x].chan) < SOUND_MIXER_NRDEVICES) { if(devs[x].chan == -1) break; printf("%-8s is set to -> [%d", names[devs[x].chan], devs[x].left); switch(devs[x].type) { case MONO: printf("]"); break; case STEREO: printf(":%d]", devs[x].right); } switch(devs[x].state) { case REC: printf(" -> RECORDING\n"); break; case PREC: printf(" -> RECORDABLE\n"); break; case MUTE: printf(" -> MUTE\n"); break; default: printf(" -> PLAY\n"); } x++; } } void saveConf(const char *filename) { int readerror = 0; if(!filename) usage_quit(); printf("Saving settings...\n"); getChannels(); if((readerror = writeFile(filename)) != SUCCESS) { if(readerror == NOSPACE) { fprintf(stderr, "You've reached the maximum profiles's number(10)\n\ run ermixer -d for delete a profile\n"); exit(EXIT_FAILURE); } else err_quit("An error was occured\n"); } printf("Done\n"); } void readConf(const char *filename) { if(!filename) usage_quit(); printf("Restoring settings...\n"); if((parseFile(filename)) == ERROR) err_quit(""); printf("Done!\n"); } void deleteConf(const char *filename) { if(!filename) usage_quit(); printf("Deleting %s.profile\n", filename); if((deleteFile(filename)) == ERROR) err_quit("Failed"); printf("Done\n"); printProfiles(); } void rec(const int opt, const char *devicename) { int device = 0; device = searchDevice(devicename); if(device == ERROR) err_quit("No device found\n"); if((modifySrc(device, opt)) == ERROR) err_quit("mmm, it don't works. It's a recordable device? run ermixer -d for a list\n"); if(opt == REC) printf("Now %s is active\n", devicename); else printf("%s disabled\n", devicename); } void usage_quit() { fprintf(stderr, "[ermixer] usage:\n"); fprintf(stderr, " -> set the channel's value\n"); fprintf(stderr, " -> read the channel's value\n"); fprintf(stderr, "-v -> print the version number of ermixer and exit\n"); fprintf(stderr, "-a -> list all channel\n"); fprintf(stderr, "-s -> save current settings\n"); fprintf(stderr, "-c -> restore previous saved settings\n"); fprintf(stderr, "-d -> delete a profile\n"); fprintf(stderr, "<+,->rec -> set or unset a new recordable device\n"); fprintf(stderr, "If you want other info please read the fantastic manual! man ermixer\n"); exit(EXIT_FAILURE); }