Updated copyright years.
[flashlight-appl] / src / flashlight_lib.c
index d7ce455..31a3939 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Flashlight applet (widget) for Maemo.
- *  Copyright (C) 2009 Roman Moravcik
+ *  Copyright (C) 2009, 2010 Roman Moravcik
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
 
 #include "flashlight_lib.h"
 
-struct buffer {
-       void *start;
-       size_t length;
-};
-
-struct buffer *buffers = NULL;
-static unsigned int n_buffers = 0;
 
 int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
 {
@@ -114,7 +107,6 @@ int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
 {
        struct v4l2_control ctrl;
        enum v4l2_buf_type type;
-       unsigned int i;
 
        printf ("flashlight_set_intensity(%d)\n", intensity);
 
@@ -143,18 +135,6 @@ int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
           WORKAROUND: start/stop i/o streaming to block camera application
         */
        if (intensity > 0) {
-               for (i = 0; i < n_buffers; ++i) {
-                       struct v4l2_buffer buf;
-
-                       buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
-                       buf.memory      = V4L2_MEMORY_MMAP;
-                       buf.index       = i;
-                       if (ioctl (flashlight->fd, VIDIOC_QBUF, &buf) == -1) {
-                               printf ("flashlight_set_intensity: unable to exchange a buffer %d with driver (%s)\n", i, strerror (errno));
-                               return EGENERROR;
-                       }
-               }
-
                type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                if (ioctl (flashlight->fd, VIDIOC_STREAMON, &type)) {
                        printf ("flashlight_set_intensity: unable to start i/o streaming (%s)\n", strerror (errno));
@@ -295,34 +275,34 @@ int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
                return EGENERROR;
        }
 
-       buffers = calloc (req.count, sizeof (*buffers));
-       if (!buffers) {
+       flashlight->buffers = calloc (req.count, sizeof (*flashlight->buffers));
+       if (!flashlight->buffers) {
                printf ("flashlight_open: unable to allocate memory\n");
                return EGENERROR;
        }
 
-       for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
+       for (flashlight->n_buffers = 0; flashlight->n_buffers < req.count; ++flashlight->n_buffers) {
                struct v4l2_buffer buf;
 
                buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
                buf.memory      = V4L2_MEMORY_MMAP;
-               buf.index       = n_buffers;
+               buf.index       = flashlight->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));
+                               flashlight->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);
+               flashlight->buffers[flashlight->n_buffers].length = buf.length;
+               flashlight->buffers[flashlight->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) {
+               if (flashlight->buffers[flashlight->n_buffers].start == MAP_FAILED) {
                        printf ("flashlight_open: unable to map memory (%s)\n", strerror (errno));
                        return EGENERROR;
                }
@@ -343,22 +323,30 @@ int flashlight_close (FlashlightContext_t *flashlight)
        }
 
        /* unmap memory mapped buffers */
-       for (i = 0; i < n_buffers; ++i) {
-               if (munmap (buffers[i].start, buffers[i].length) == -1) {
-                       printf ("flashlight_close: unable to unmap memory (%s)\n", strerror (errno));
-                       return EGENERROR;
+       for (i = 0; i < flashlight->n_buffers; ++i) {
+               if (flashlight->buffers[flashlight->n_buffers].start != MAP_FAILED) {
+                       if (munmap (flashlight->buffers[i].start, flashlight->buffers[i].length) == -1) {
+                               printf ("flashlight_close: unable to unmap memory (%s)\n", strerror (errno));
+                               return EGENERROR;
+                       }
                }
        }
-       free (buffers);
+       flashlight->n_buffers = 0;
+
+       /* free buffers */
+       if (flashlight->buffers)
+               free (flashlight->buffers);
+       flashlight->buffers = NULL;
 
+       /* close camera device */
        if (flashlight->fd != -1) {
                if (close (flashlight->fd) == -1) {
                        printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
                        return ENODEVICE;
                }
        }
-
        flashlight->fd = -1;
+
        return ENOERROR;
 }
 
@@ -386,6 +374,9 @@ int flashlight_init (FlashlightContext_t **pRefContext)
        memset (flashlight, 0x00, sizeof (FlashlightContext_t));
        flashlight->fd = -1;
 
+       flashlight->n_buffers = 0;
+       flashlight->buffers = NULL;
+
        /* from adp1653.c */
        flashlight->min_intensity = 0;
        flashlight->max_intensity = 11;
@@ -414,7 +405,7 @@ int flashlight_deinit (FlashlightContext_t *flashlight)
                                return EGENERROR;
                }
 
-               if (flashlight_close(flashlight))
+               if (flashlight_close (flashlight))
                        return EGENERROR;
        }