#include "QCamV4L.hpp"
#include "QCamVesta.hpp"

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/videodev.h>

QCam * QCamV4L::openBestDevice(const char * devpath) {
   int cam_fd;
   if (-1 == (cam_fd=open(devpath,O_RDONLY))) {
      perror(devpath);
      return NULL;
   } else {
      struct video_capability vcap;
      int type;
      if (ioctl(cam_fd, VIDIOCGCAP,&vcap ) < 0) {
         perror(devpath);
         return NULL;
      }
      close(cam_fd);
      if (sscanf(vcap.name, "Philips %d webcam",&type ) == 1) {
         printf("Philips webcam type %d detected.\n", type);
         return new QCamVesta(devpath);
      }
      if (strncmp(vcap.name,"OV511",5)==0) {
         printf("webcam %s detected.\n",
                vcap.name);
         return new QCamV4L(devpath, false, 0 /*VIDEO_PALETTE_RGB24*/);
      } 
      printf("unknow %s camera detected.\n"
             "Using generic V4L driver.\n",
             vcap.name);
      return new QCamV4L(devpath,false);
   }
   printf("no camera detected.\n");
   return NULL;
}

