/*
 * analogradio - A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 */

#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <typeinfo>
#include <sys/ioctl.h>
#include <vdr/plugin.h>

static const char *VERSION        = "0.1.3a";
static const char *DESCRIPTION    = "Source device for analog radio tuner devices";

static char *DEV_RADIO      = "/dev/radio";
static char *DEV_DSP        = "/dev/dsp";
static char *DEV_SND        = "/dev/snd";

#include "device.h"
#include "analogradio.h"

//-----cMenuSetupAnalogRadio---------------------------------------------------

cMenuSetupAnalogRadio::cMenuSetupAnalogRadio(analogRadioSetup * arsSettings) {
  settings = arsSettings;
  Add(new cMenuEditIntItem (tr("Tuner Volume (0-100)"),   &settings->initVolume, 0, 100));
  Add(new cMenuEditBoolItem(tr("Mute tuner if not used"), &settings->muteTuner));
}

void cMenuSetupAnalogRadio::Store(void) {
  SetupStore("initVolume", settings->initVolume);
  SetupStore("muteTuner", settings->muteTuner);
}

//-----cPluginAnalogRadio------------------------------------------------------

cPluginAnalogRadio::cPluginAnalogRadio(void) {
  analogRadioDevice = NULL;
  after = NULL;
  before = NULL;
  device = NULL;
  audio_device = NULL;
  audio_mode = AUDIO_MODE_OSS;

  settings = new analogRadioSetup;
  settings->initVolume = 90;
  settings->muteTuner = 1;
}

cPluginAnalogRadio::~cPluginAnalogRadio() {
}

bool cPluginAnalogRadio::Initialize(void) {
  if (!device)
    device = DEV_RADIO;

  if (!audio_device) {
    if (audio_mode == AUDIO_MODE_ALSA)
      audio_device = DEV_SND;
    else
      audio_device = DEV_DSP;
  }

  analogRadioDevice = new cAnalogRadioDevice(device, audio_device, audio_mode, after, before, settings);
  return true;
}

bool cPluginAnalogRadio::Start(void) {
  return true;
}

const char *cPluginAnalogRadio::CommandLineHelp(void) {
  return "  -a cmd    --after=cmd    execute command after muting the radio device\n"
         "  -b cmd    --before=cmd   execute command before unmuting the radio device\n"
         "  -m name   --mode=name    Use one of following audio modes:\n"
         "                           OSS (default)\n"
         "                           RAW\n"
         "                           ALSA (currently not supported)\n"
         "  -r dev    --devradio=dev set radio device (default: /dev/radio)\n"
         "  -s dev    --devaudio=dev set audio device (default: /dev/snd or /dev/dsp)\n";
}

bool cPluginAnalogRadio::ProcessArgs(int argc, char *argv[]) {
  static struct option long_options[] = {
    { "after",       required_argument, NULL, 'a' },
    { "before",      required_argument, NULL, 'b' },
    { "mode",        required_argument, NULL, 'm' },
    { "devradio",    required_argument, NULL, 'r' },
    { "devaudio",    required_argument, NULL, 's' },
    { NULL }
  };

  int c;
while ((c = getopt_long(argc, argv, "a:b:m:r:s:", long_options, NULL)) != -1) {
    switch (c) {
      case 'a': after = optarg;
                break;
      case 'b': before = optarg;
                break;
      case 'm': if (strcasecmp(optarg, "OSS") == 0)
                  audio_mode = AUDIO_MODE_OSS;
                else if (strcasecmp(optarg, "RAW") == 0)
                  audio_mode = AUDIO_MODE_RAW;
                else if (strcasecmp(optarg, "ALSA") == 0)
                  audio_mode = AUDIO_MODE_ALSA;
                break;
      case 'r': device = optarg;
                break;
      case 's': audio_device = optarg;
                break;
    }
  }

  return true;
}

cMenuSetupPage *cPluginAnalogRadio::SetupMenu(void) {
  return new cMenuSetupAnalogRadio(settings);
}

bool cPluginAnalogRadio::SetupParse(const char *Name, const char *Value) {
  if      (!strcasecmp(Name, "initVolume")) settings->initVolume = atoi(Value);
  else if (!strcasecmp(Name, "muteTuner"))  settings->muteTuner = atoi(Value);
  else
     return false;
  return true;
}

VDRPLUGINCREATOR(cPluginAnalogRadio); // Don't touch this!
