+ /*
+ WORKAROUND: Initialization of camera extracted from v4l2_example.
+ http://v4l2spec.bytesex.org/spec/capture-example.html
+
+ We need to initialize camera in other to block camera application.
+ (bug 4949: Applet breaks the camera application)
+ */
+ /* get crop capabilities */
+ cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ if (ioctl (flashlight->fd, VIDIOC_CROPCAP, &cropcap) == -1) {
+ printf ("flashlight_open: unable to get crop capabilities (%s)\n", strerror (errno));
+ return EGENERROR;
+ }
+
+ /* set crop capabilities */
+ crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ crop.c = cropcap.defrect; /* reset to default */
+ if (ioctl (flashlight->fd, VIDIOC_S_CROP, &crop) == -1) {
+ printf ("flashlight_open: unable to set cropping rectangle (%s)\n", strerror (errno));
+ return EGENERROR;
+ }
+
+ /* set data format */
+ fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ fmt.fmt.pix.width = 640;
+ fmt.fmt.pix.height = 480;
+ fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
+ fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
+ if (ioctl (flashlight->fd, VIDIOC_S_FMT, &fmt) == -1) {
+ printf ("flashlight_open: unable to set data format (%s)\n", strerror (errno));
+ return EGENERROR;
+ }
+
+ req.count = 4;
+ req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ req.memory = V4L2_MEMORY_MMAP;
+ if (ioctl (flashlight->fd, VIDIOC_REQBUFS, &req) == -1) {
+ printf ("flashlight_open: unable to initiate memory mapping (%s)\n", strerror (errno));
+ return EGENERROR;
+ }
+
+ if (req.count < 2) {
+ printf ("flashlight_open: insufficient buffer memory on %s\n", device_name);
+ return EGENERROR;
+ }
+
+ buffers = calloc (req.count, sizeof (*buffers));
+ if (!buffers) {
+ printf ("flashlight_open: unable to allocate memory\n");
+ return EGENERROR;
+ }
+
+ for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
+ struct v4l2_buffer buf;
+
+ buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+ buf.memory = V4L2_MEMORY_MMAP;
+ buf.index = n_buffers;
+
+ if (ioctl (flashlight->fd, VIDIOC_QUERYBUF, &buf) == -1) {
+ printf ("flashlight_open: unable to query the status of a buffer %d (%s)\n",
+ n_buffers, strerror (errno));
+ return EGENERROR;
+ }
+
+ buffers[n_buffers].length = buf.length;
+ buffers[n_buffers].start = mmap (NULL /* start anywhere */,
+ buf.length,
+ PROT_READ | PROT_WRITE /* required */,
+ MAP_SHARED /* recommended */,
+ flashlight->fd,
+ buf.m.offset);
+
+ if (buffers[n_buffers].start == MAP_FAILED) {
+ printf ("flashlight_open: unable to map memory (%s)\n", strerror (errno));
+ return EGENERROR;
+ }
+ }
+