#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/mman.h>
#include <sys/fcntl.h>
#include <sys/signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/shm.h>
#include <math.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/extensions/XShm.h>
#include <X11/keysym.h>
#include <sys/time.h>
#include <unistd.h>
#include <fstream.h>
#include <sys/ioctl.h>
extern "C"
{
#include "ioctl_meteor.h"
}
#include "grabber.h"
#include "grabparent.h"

extern int errno;

/* device stuff */
static int   meteor;
static char *DevName;
static char *mmbuf;
static struct meteor_geomet geo;

/* geometry stuff */
static int columns = NUM_COLS,
           rows = NUM_ROWS,
	   window_size_x,
           window_size_y,
	   pixelsize = PIXELSIZE,
           noframes = NOFRAMES,
	   bufsize = PIXELSIZE*NUM_COLS*NUM_ROWS,
           fieldmode = 0x0,
           sigmode = 0x0,
	   fps = 15;
struct meteor_frame_offset off;

static int usePacked;

int checkChips(void) 
{
    FILE *file;
    char buffer[256];
    int count;
    
    file = fopen("/proc/pci", "r");
    if (file==NULL) {
	fprintf(stderr, "failed to open /proc/pci.  "
		"Assuming this is not Natoma.\n");
	return(0);
    }

    while ((count=fscanf(file, "%255s\n", buffer))>0) {
	if (!strncasecmp("Natoma", buffer, 255)) {
	    fclose(file);
	    fprintf(stderr, "Looks like a Natoma chipset.  Using YUV_PACKED.\n");
	    return(1);
	}
    }
    fprintf(stderr, "Doesn't look like a Natoma chipset.  Using YUV_PLANAR.\n");
    fclose(file);
    return(0);
}


int grabber(int grabbernum, int source, char brightness, char contrast)
{

  char Name[12];
  u_long  size;
  u_short s;
  int c, i, inputdev;
  
  switch (source)
    {
    case 0:
      inputdev = METEOR_INPUT_DEV0;
      break;
    case 1:
      inputdev = METEOR_INPUT_DEV1;
      break;
    case 2:
      inputdev = METEOR_INPUT_DEV2;
      break;
    }
  sprintf(Name, "/dev/bt848-%d", grabbernum-1);
  DevName = Name;
    
  if ((meteor = open(DevName, O_RDWR)) < 0) {
    perror("open failed");
    printf("device %s\n", DevName);
    exit(1);
  }

  printf("Digitizer %s is online, capturing from source %d.\n", Name,source);
  
  pixelsize = 4;
  bufsize = 4*rows*columns;
  geo.oformat = METEOR_GEO_RGB24;
  
  geo.rows = rows;
  geo.columns = columns;
  geo.frames = noframes;
  geo.oformat |= fieldmode;
  
    if (ioctl(meteor, METEORSETGEO, &geo) < 0) {
      perror("ioctl SetGeometry failed");
      exit(1);
    }
    
    /* if (ioctl(meteor, METEORGFROFF, &off) < 0) {
       perror("ioctl FrameOffset failed");
       exit(1);
       }*/
    
    if ((mmbuf=(char *) mmap((caddr_t)0, bufsize,
			     PROT_READ|PROT_WRITE, 
			     MAP_FILE|MAP_SHARED, 
			     meteor, (off_t)0)) == (char *)(-1)) {
      perror("mmap failed");
      exit(1);
    }

    c = DEFAULT_FORMAT;
    if (ioctl(meteor, METEORSFMT, &c) < 0) {
	perror("ioctl SetFormat failed");
	exit(1);
    }

    if (ioctl(meteor, METEORSINPUT, &inputdev) < 0) {
	perror("ioctl Setinput failed");
	exit(1);
    }

    if (inputdev==METEOR_INPUT_DEV_RGB && geo.oformat==METEOR_GEO_RGB16) {
	/* set RGB section for 15 bit colour */
	u_short s = (0x4 << 4);
	if (ioctl(meteor, METEORSBT254, &s)) {
	    perror("ioctl SetBt254 failed");
	    exit(1);
	}
    }

    /* adjust the brightness */
    if (ioctl (meteor, METEORSBRIG, &brightness))
    {
      perror ("ioctl set brightness failed");
      exit (1);
    }

    /* adjust the contrast */
    if (ioctl (meteor, METEORSCONT, &contrast))
    {
      perror ("ioctl set contrast failed");
    }


    if (noframes < 2) {
	/* continuous frame capture */
	c = METEOR_CAP_CONTINOUS ;
	if (ioctl(meteor, METEORCAPTUR, &c)) {
	    perror("ioctl CaptContinuous failed");
	    exit(1);
	}
    } else {
	struct meteor_capframe frame;

	frame.command = METEOR_CAP_N_FRAMES;
	frame.lowat = 0;
	frame.hiwat = 0;
	if (ioctl(meteor, METEORCAPFRM, &frame)) {
	    perror("ioctl CaptFrm failed");
	    exit(1);
	}
    }

    /* process events */
    for (;;) {
      pause();
    }
}

