summaryrefslogtreecommitdiff
path: root/common.c
blob: 1ff6b65cfc384cf9825c7945ec8fb75deabf2680 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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;

  if (PvrSetup.LogLevel >= level) {
    va_start(ap, fmt);
    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);
    switch (level) {
      case pvrERROR:
         esyslog("%s",tmpstr);
         break;
      case pvrINFO:
         isyslog("%s",tmpstr);
         break;
      case pvrDEBUG1:
      case pvrDEBUG2:
      default:
         dsyslog("%s",tmpstr);
         break;
      }
    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) {
    log(pvrERROR, "Error IOCTL %s: %s is not open", cmd, fd);
    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(pvrDEBUG1, "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(pvrDEBUG1, "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;
}