2 * Flashlight applet (widget) for Maemo.
3 * Copyright (C) 2009 Roman Moravcik
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/ioctl.h>
31 #include <asm/types.h>
32 #include <linux/videodev2.h>
34 #include "flashlight_lib.h"
41 struct buffer *buffers = NULL;
42 static unsigned int n_buffers = 0;
44 int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
46 struct v4l2_control ctrl;
48 printf ("flashlight_get_status()\n");
50 if (flashlight == NULL) {
51 printf ("flashlight_get_status: flashlight context is not valid\n");
55 if (flashlight->fd == -1) {
56 printf ("flashlight_get_status: device not openned\n");
62 /* check short circuit fault */
63 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
64 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
65 printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
70 *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
72 *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
74 /* check overtemperature fault */
75 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
76 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
77 printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
82 *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
84 *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
86 /* check timeout fault */
87 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
88 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
89 printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
94 *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
96 *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
98 /* check overtemperature fault */
99 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
100 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
101 printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
106 *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
108 *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
113 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
115 struct v4l2_control ctrl;
116 enum v4l2_buf_type type;
119 printf ("flashlight_set_intensity(%d)\n", intensity);
121 if (flashlight == NULL) {
122 printf ("flashlight_set_intensity: flashlight context is not valid\n");
126 if (flashlight->fd == -1) {
127 printf ("flashlight_set_intensity: device not openned\n");
131 if (intensity > flashlight->max_intensity)
132 intensity = flashlight->max_intensity;
134 ctrl.id = V4L2_CID_TORCH_INTENSITY;
135 ctrl.value = intensity;
137 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
138 printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
143 WORKAROUND: start/stop i/o streaming to block camera application
146 for (i = 0; i < n_buffers; ++i) {
147 struct v4l2_buffer buf;
149 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
150 buf.memory = V4L2_MEMORY_MMAP;
152 if (ioctl (flashlight->fd, VIDIOC_QBUF, &buf) == -1) {
153 printf ("flashlight_set_intensity: unable to exchange a buffer %d with driver (%s)\n", i, strerror (errno));
158 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
159 if (ioctl (flashlight->fd, VIDIOC_STREAMON, &type)) {
160 printf ("flashlight_set_intensity: unable to start i/o streaming (%s)\n", strerror (errno));
164 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
165 if (ioctl (flashlight->fd, VIDIOC_STREAMOFF, &type) == -1) {
166 printf ("flashlight_set_intensity: unable to stop i/o streaming (%s)\n", strerror (errno));
174 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
176 struct v4l2_control ctrl;
178 printf ("flashlight_get_intensity()\n");
180 if (flashlight == NULL) {
181 printf ("flashlight_get_intensity: flashlight context is not valid\n");
185 if (flashlight->fd == -1) {
186 printf ("flashlight_get_intensity: device not openned\n");
190 ctrl.id = V4L2_CID_TORCH_INTENSITY;
192 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
193 printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
197 *intensity = ctrl.value;
201 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
203 struct v4l2_queryctrl ctrl;
204 struct v4l2_cropcap cropcap;
205 struct v4l2_crop crop;
206 struct v4l2_format fmt;
207 struct v4l2_requestbuffers req;
210 printf ("flashlight_open(%s)\n", device_name);
212 if (flashlight == NULL) {
213 printf ("flashlight_open: flashlight context is not valid\n");
217 if (device_name == NULL) {
218 printf ("flashlight_open: device name not specified\n");
222 memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
224 if (stat (flashlight->device_name, &st) == -1) {
225 printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
229 /* check it device_name is real device */
230 if (!S_ISCHR (st.st_mode)) {
231 printf ("flashlight_open: %s is no device\n", flashlight->device_name);
235 flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
237 if (flashlight->fd == -1) {
238 printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
242 /* query from driver minimal and maximal flashlight intensity */
243 ctrl.id = V4L2_CID_TORCH_INTENSITY;
244 if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
245 printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
249 flashlight->min_intensity = ctrl.minimum;
250 flashlight->max_intensity = ctrl.maximum;
253 WORKAROUND: Initialization of camera extracted from v4l2_example.
254 http://v4l2spec.bytesex.org/spec/capture-example.html
256 We need to initialize camera in other to block camera application.
257 (bug 4949: Applet breaks the camera application)
259 /* get crop capabilities */
260 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
261 if (ioctl (flashlight->fd, VIDIOC_CROPCAP, &cropcap) == -1) {
262 printf ("flashlight_open: unable to get crop capabilities (%s)\n", strerror (errno));
266 /* set crop capabilities */
267 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
268 crop.c = cropcap.defrect; /* reset to default */
269 if (ioctl (flashlight->fd, VIDIOC_S_CROP, &crop) == -1) {
270 printf ("flashlight_open: unable to set cropping rectangle (%s)\n", strerror (errno));
274 /* set data format */
275 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
276 fmt.fmt.pix.width = 640;
277 fmt.fmt.pix.height = 480;
278 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
279 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
280 if (ioctl (flashlight->fd, VIDIOC_S_FMT, &fmt) == -1) {
281 printf ("flashlight_open: unable to set data format (%s)\n", strerror (errno));
286 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287 req.memory = V4L2_MEMORY_MMAP;
288 if (ioctl (flashlight->fd, VIDIOC_REQBUFS, &req) == -1) {
289 printf ("flashlight_open: unable to initiate memory mapping (%s)\n", strerror (errno));
294 printf ("flashlight_open: insufficient buffer memory on %s\n", device_name);
298 buffers = calloc (req.count, sizeof (*buffers));
300 printf ("flashlight_open: unable to allocate memory\n");
304 for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
305 struct v4l2_buffer buf;
307 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
308 buf.memory = V4L2_MEMORY_MMAP;
309 buf.index = n_buffers;
311 if (ioctl (flashlight->fd, VIDIOC_QUERYBUF, &buf) == -1) {
312 printf ("flashlight_open: unable to query the status of a buffer %d (%s)\n",
313 n_buffers, strerror (errno));
317 buffers[n_buffers].length = buf.length;
318 buffers[n_buffers].start = mmap (NULL /* start anywhere */,
320 PROT_READ | PROT_WRITE /* required */,
321 MAP_SHARED /* recommended */,
325 if (buffers[n_buffers].start == MAP_FAILED) {
326 printf ("flashlight_open: unable to map memory (%s)\n", strerror (errno));
334 int flashlight_close (FlashlightContext_t *flashlight)
338 printf ("flashlight_close()\n");
340 if (flashlight == NULL) {
341 printf ("flashlight_close: flashlight context is not valid\n");
345 /* unmap memory mapped buffers */
346 for (i = 0; i < n_buffers; ++i) {
347 if (munmap (buffers[i].start, buffers[i].length) == -1) {
348 printf ("flashlight_close: unable to unmap memory (%s)\n", strerror (errno));
354 if (flashlight->fd != -1) {
355 if (close (flashlight->fd) == -1) {
356 printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
365 int flashlight_init (FlashlightContext_t **pRefContext)
367 FlashlightContext_t *flashlight = NULL;
369 printf ("flashlight_init()\n");
371 if (*pRefContext != NULL) {
372 printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
376 /* allocate memory for context structure */
377 flashlight = malloc (sizeof (FlashlightContext_t));
378 if (flashlight == NULL) {
379 printf ("flashlight_init: unable to allocate memory for context\n");
383 *pRefContext = flashlight;
385 /* initialize default values */
386 memset (flashlight, 0x00, sizeof (FlashlightContext_t));
390 flashlight->min_intensity = 0;
391 flashlight->max_intensity = 11;
396 int flashlight_deinit (FlashlightContext_t *flashlight)
400 printf ("flashlight_deinit()\n");
402 if (flashlight == NULL) {
403 printf ("flashlight_deinit: flashlight context is not valid\n");
407 if (flashlight->fd != -1) {
408 /* check if flashlight isn't enabled before closing device */
409 if (flashlight_get_intensity (flashlight, &intensity) == -1)
413 if (flashlight_set_intensity (flashlight, 0) == -1)
417 if (flashlight_close(flashlight))
421 /* free allocated memory */