/*
  NOTE!
  This driver is specific to VS1005g
*/
#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <string.h>
#include <mutex.h>
#include <kernel.h>
#include <aucommon.h>
#include <stdlib.h>
#include "../ftMono.h"


DLLENTRY(main)
ioresult main(char *parameters) {
  int nParam = RunLibraryFunction("ParamSpl",ENTRY_MAIN,(u_int16)parameters);
  char *p = parameters;
  int i, ftMonoFlags = 0, currentMonoFlags = 0, anyFlagsSet = 0;
  FILE *fp = stdaudioout;

  for (i=0; i<nParam; i++) {
    if (!strcmp(p, "-o")) {
      fp = stdaudioout;
    } else if (!strcmp(p, "-i")) {
      fp = stdaudioin;
    } else if (!strcmp(p, "-h")) {
      printf("Usage: SetMono [-o] [-i] [lr|ll|rr|rl|mono] [il] [ir] [-h]\n"
	     "-i\tSet stdaudioin\n"
	     "-o\tSet stdaudioout (default)\n"
	     "lr\tNormal LR stereo\n"
	     "ll\tCopy Left to output\n"
	     "rr\tCopy Right to output\n"
	     "rl\tSwap Left and Right\n"
	     "mono\tOutput mean of Left and Right\n"
	     "il\tInvert Left output\n"
	     "ir\tInvert Right output\n"	     
	     "-h\tShow this help\n"
	     "\nWith no parameters set, current flags are shown\n");
      return S_OK;
    } else if (!strcmp(p, "lr")) {
      ftMonoFlags = (ftMonoFlags & ~MONO_MODE_MASK) | MONO_MODE_LR;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "ll")) {
      ftMonoFlags = (ftMonoFlags & ~MONO_MODE_MASK) | MONO_MODE_LL;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "rr")) {
      ftMonoFlags = (ftMonoFlags & ~MONO_MODE_MASK) | MONO_MODE_RR;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "rl")) {
      ftMonoFlags = (ftMonoFlags & ~MONO_MODE_MASK) | MONO_MODE_RL;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "mono")) {
      ftMonoFlags = (ftMonoFlags & ~MONO_MODE_MASK) | MONO_MODE_MONO;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "il")) {
      ftMonoFlags |= MONO_INV_L;
      anyFlagsSet = 1;
    } else if (!strcmp(p, "ir")) {
      ftMonoFlags |= MONO_INV_R;
      anyFlagsSet = 1;
    } else {
      printf("E: Unknown parameter \"%s\"\n", p);
      return S_ERROR;
    }
    p += strlen(p)+1;
  }

  if ((currentMonoFlags = ioctl(fp, IOCTL_AUDIO_GET_MONO, NULL)) < 0) {
    printf("E: Could not find MONO driver in audio path\n");
    return S_ERROR;
  }

  if (anyFlagsSet) {
    ioctl(fp, IOCTL_AUDIO_SET_MONO, (void *)ftMonoFlags);
  } else {
    printf("Mono flags: ");
    switch (currentMonoFlags & MONO_MODE_MASK) {
    case MONO_MODE_LR:
      printf("lr");
      break;
    case MONO_MODE_LL:
      printf("ll");
      break;
    case MONO_MODE_RR:
      printf("rr");
      break;
    case MONO_MODE_RL:
      printf("rl");
      break;
    case MONO_MODE_MONO:
      printf("mono");
      break;
    default:
      printf("<unknown>");
    }
    if (currentMonoFlags & MONO_INV_L) printf(" il");
    if (currentMonoFlags & MONO_INV_R) printf(" ir");
    printf("\n");
  }

#if 0
  {
    static s_int16 puf[] =
      {
       20000, 20000,
       20001, 20000,
       20001, 20001,
       20000, 20001,
       20001, 20001,
       -20000, -20000,
       -20001, -20000,
       -20001, -20001,
       -20000, -20001,
       -20001, -20001,
       11000, 20500,
       12000, 21000,
       13000, 21500,
       14000, 22000,
       32767, -32768,
       -32768, 32767,
       32767, 32767,
       -32768, -32768,
      };
    
    fwrite(puf, 1, sizeof(puf), stdaudioout);
    {
      int i;
      for (i=0; i<sizeof(puf); i+=2) {
	printf("%6d %6d\n", puf[i], puf[i+1]);
      }
    }
  }
#endif


  
  return S_OK;
}
