#include "common.h"

void log(int level, const char * fmt, ...) {
  char tmpstr[BUFSIZ];
  char timestr[16];
  va_list ap;
  time_t now;
  struct tm local;

  va_start(ap, fmt);

  if (PvrSetup.LogLevel >= level) {
    time(&now);
    localtime_r(&now, &local);
    vsnprintf(tmpstr, sizeof(tmpstr), fmt, ap);
    strftime(timestr, sizeof(timestr), "%H:%M:%S", &local);
    printf("pvrinput: %s %s\n", timestr, tmpstr);
    }
  va_end(ap);
}

/*
function IOCTL
retries the ioctl given six times before giving up,
improves stability if device/driver is actually busy
*/
int IOCTL(int fd, int cmd, void *data) {
  if (fd<=0) return (-1); 
  for (int retry = 5; retry >= 0; ) {
    if (ioctl(fd, cmd, data) != 0) {
      if (retry){
        usleep(20000); /* 20msec */
        retry--;
        continue;
        }
      return -1;
      }
    else
      return 0;  /* all went okay :) */
    }
  return 0;  /* should never reach this */
}

/*
This function takes a ivtv V4L2 integer value
and returns it as rounded 0..100% integer value
-wirbel-
*/

int IntVal2Percent(int NumVal, int MinVal, int MaxVal){
 double t=0;
 log(3, "IntVal2Percent(NumVal=%d, MinVal=%d, MaxVal=%d)\n",
    NumVal, MinVal, MaxVal);    
 t=(double) (MaxVal - MinVal);
 t=(NumVal-MinVal)/t;    
 t=100.0*t + 0.5;    /* rounding to nearest int) */
 return (int) t;
}

/*
This function takes a 0..100% integer value
and returns it as rounded ivtv V4L2 integer value
-wirbel-
*/

int Percent2IntVal(int Percent, int MinVal, int MaxVal){
 double t=0;
 log(3, "Percent2IntVal(Percent=%d, MinVal=%d, MaxVal=%d)\n",
    Percent, MinVal, MaxVal);
 t=Percent/100.0;
 t=0.5 + t*(MaxVal-MinVal);  /* +0.5 is rounding to int */
 t=MinVal+t;
 return (int) t;
}

