/*
 * X10 controller plugin for the Video Disk Recorder       
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id: deviceListMenu.c,v 1.9 2005/03/17 13:06:34 toni Exp $                                           
 */

#include "deviceListMenu.h"
#include "X10Device.h"
#include "X10Setup.h"
#include "i18n.h"
#include <vdr/interface.h>
#include <vdr/menuitems.h>
#include <vdr/osdbase.h>

cDeviceListMenu::cDeviceListMenu() : cOsdMenu(tr("Devices"), 13, 12) {
  for (cX10Device *device = devices.First(); device; device = devices.Next(device)) {
      char *buf=0;
      asprintf(&buf,"%s %s", device->getXid(),device->getName());
      Add(new cOsdItem(buf, osUser1));
      dsyslog("httpd: Diudi");
      free(buf);
  }
  SetHelp(tr("On"),tr("Bright"),tr("Dim"), tr("Off"));
  Display();
}

cDeviceListMenu::~cDeviceListMenu() {
}

cX10Device* cDeviceListMenu::getCurrentDevice() {
  int i = Current();
  return devices.Get(i);
}

eOSState cDeviceListMenu::DeviceOn(void) {
  cX10Device* device = getCurrentDevice();
  if (HasSubMenu() || !device)
    return osContinue;
  char *command= 0;
  asprintf(&command,"/bin/heyu turn %s on &", device->getXid());
  dsyslog("X10: executing command: %s", command);
  system(command);
  free(command);
  return osContinue;
}

eOSState cDeviceListMenu::DeviceBright(void) {
  cX10Device* ev = getCurrentDevice();
  if (HasSubMenu() || !ev)
    return osContinue;
  char *command= 0;
  asprintf(&command,"/bin/heyu bright %s %d &", ev->getXid(),globalSetup.step);
  dsyslog("X10: executing command: %s", command);
  system(command);
  free(command);
  return osContinue;
}

eOSState cDeviceListMenu::DeviceDim(void) {
  cX10Device* ev = getCurrentDevice();
  if (HasSubMenu() || !ev)
    return osContinue;
  char *command= 0;
  asprintf(&command,"/bin/heyu dim %s %d &", ev->getXid(),globalSetup.step);
  dsyslog("X10: executing command: %s", command);
  system(command);
  free(command);
  return osContinue;
}

eOSState cDeviceListMenu::DeviceOff(void) {
  cX10Device* ev = getCurrentDevice();
  if (HasSubMenu() || !ev)
    return osContinue;
  char *command= 0;
  asprintf(&command,"/bin/heyu turn %s off &", ev->getXid());
  dsyslog("X10: executing command: %s", command);
  system(command);
  free(command);
  return osContinue;
}

eOSState cDeviceListMenu::ProcessKey(eKeys key) {
  eOSState state = cOsdMenu::ProcessKey(key);
  if (state == osUnknown)
    switch (key)
    {
       case kOk:     state = DeviceOn();  break;
       case kRed:    state = DeviceOn();  break;
       case kGreen:  state = DeviceBright();  break;
       case kYellow: state = DeviceDim();  break;
       case kBlue:   state = DeviceOff(); break;
       default:      break;
    }
  return state;
}

