2 * Flashlight applet (widget) for Maemo.
3 * Copyright (C) 2009, 2010 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"
37 int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
39 struct v4l2_control ctrl;
41 printf ("flashlight_get_status()\n");
43 if (flashlight == NULL) {
44 printf ("flashlight_get_status: flashlight context is not valid\n");
48 if (flashlight->fd == -1) {
49 printf ("flashlight_get_status: device not openned\n");
55 /* check short circuit fault */
56 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
57 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
58 printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
63 *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
65 *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
67 /* check overtemperature fault */
68 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
69 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
70 printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
75 *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
77 *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
79 /* check timeout fault */
80 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
81 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
82 printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
87 *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
89 *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
91 /* check overtemperature fault */
92 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
93 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
94 printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
99 *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
101 *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
106 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
108 struct v4l2_control ctrl;
109 enum v4l2_buf_type type;
111 printf ("flashlight_set_intensity(%d)\n", intensity);
113 if (flashlight == NULL) {
114 printf ("flashlight_set_intensity: flashlight context is not valid\n");
118 if (flashlight->fd == -1) {
119 printf ("flashlight_set_intensity: device not openned\n");
123 if (intensity > flashlight->max_intensity)
124 intensity = flashlight->max_intensity;
126 ctrl.id = V4L2_CID_TORCH_INTENSITY;
127 ctrl.value = intensity;
129 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
130 printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
135 WORKAROUND: start/stop i/o streaming to block camera application
138 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
139 if (ioctl (flashlight->fd, VIDIOC_STREAMON, &type)) {
140 printf ("flashlight_set_intensity: unable to start i/o streaming (%s)\n", strerror (errno));
144 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
145 if (ioctl (flashlight->fd, VIDIOC_STREAMOFF, &type) == -1) {
146 printf ("flashlight_set_intensity: unable to stop i/o streaming (%s)\n", strerror (errno));
154 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
156 struct v4l2_control ctrl;
158 printf ("flashlight_get_intensity()\n");
160 if (flashlight == NULL) {
161 printf ("flashlight_get_intensity: flashlight context is not valid\n");
165 if (flashlight->fd == -1) {
166 printf ("flashlight_get_intensity: device not openned\n");
170 ctrl.id = V4L2_CID_TORCH_INTENSITY;
172 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
173 printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
177 *intensity = ctrl.value;
181 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
183 struct v4l2_queryctrl ctrl;
184 struct v4l2_cropcap cropcap;
185 struct v4l2_crop crop;
186 struct v4l2_format fmt;
187 struct v4l2_requestbuffers req;
190 printf ("flashlight_open(%s)\n", device_name);
192 if (flashlight == NULL) {
193 printf ("flashlight_open: flashlight context is not valid\n");
197 if (device_name == NULL) {
198 printf ("flashlight_open: device name not specified\n");
202 memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
204 if (stat (flashlight->device_name, &st) == -1) {
205 printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
209 /* check it device_name is real device */
210 if (!S_ISCHR (st.st_mode)) {
211 printf ("flashlight_open: %s is no device\n", flashlight->device_name);
215 flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
217 if (flashlight->fd == -1) {
218 printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
222 /* query from driver minimal and maximal flashlight intensity */
223 ctrl.id = V4L2_CID_TORCH_INTENSITY;
224 if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
225 printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
229 flashlight->min_intensity = ctrl.minimum;
230 flashlight->max_intensity = ctrl.maximum;
233 WORKAROUND: Initialization of camera extracted from v4l2_example.
234 http://v4l2spec.bytesex.org/spec/capture-example.html
236 We need to initialize camera in other to block camera application.
237 (bug 4949: Applet breaks the camera application)
239 /* get crop capabilities */
240 cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
241 if (ioctl (flashlight->fd, VIDIOC_CROPCAP, &cropcap) == -1) {
242 printf ("flashlight_open: unable to get crop capabilities (%s)\n", strerror (errno));
246 /* set crop capabilities */
247 crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248 crop.c = cropcap.defrect; /* reset to default */
249 if (ioctl (flashlight->fd, VIDIOC_S_CROP, &crop) == -1) {
250 printf ("flashlight_open: unable to set cropping rectangle (%s)\n", strerror (errno));
254 /* set data format */
255 fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
256 fmt.fmt.pix.width = 640;
257 fmt.fmt.pix.height = 480;
258 fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
259 fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
260 if (ioctl (flashlight->fd, VIDIOC_S_FMT, &fmt) == -1) {
261 printf ("flashlight_open: unable to set data format (%s)\n", strerror (errno));
266 req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
267 req.memory = V4L2_MEMORY_MMAP;
268 if (ioctl (flashlight->fd, VIDIOC_REQBUFS, &req) == -1) {
269 printf ("flashlight_open: unable to initiate memory mapping (%s)\n", strerror (errno));
274 printf ("flashlight_open: insufficient buffer memory on %s\n", device_name);
278 flashlight->buffers = calloc (req.count, sizeof (*flashlight->buffers));
279 if (!flashlight->buffers) {
280 printf ("flashlight_open: unable to allocate memory\n");
284 for (flashlight->n_buffers = 0; flashlight->n_buffers < req.count; ++flashlight->n_buffers) {
285 struct v4l2_buffer buf;
287 buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288 buf.memory = V4L2_MEMORY_MMAP;
289 buf.index = flashlight->n_buffers;
291 if (ioctl (flashlight->fd, VIDIOC_QUERYBUF, &buf) == -1) {
292 printf ("flashlight_open: unable to query the status of a buffer %d (%s)\n",
293 flashlight->n_buffers, strerror (errno));
297 flashlight->buffers[flashlight->n_buffers].length = buf.length;
298 flashlight->buffers[flashlight->n_buffers].start = mmap (NULL /* start anywhere */,
300 PROT_READ | PROT_WRITE /* required */,
301 MAP_SHARED /* recommended */,
305 if (flashlight->buffers[flashlight->n_buffers].start == MAP_FAILED) {
306 printf ("flashlight_open: unable to map memory (%s)\n", strerror (errno));
314 int flashlight_close (FlashlightContext_t *flashlight)
318 printf ("flashlight_close()\n");
320 if (flashlight == NULL) {
321 printf ("flashlight_close: flashlight context is not valid\n");
325 /* unmap memory mapped buffers */
326 for (i = 0; i < flashlight->n_buffers; ++i) {
327 if (flashlight->buffers[flashlight->n_buffers].start != MAP_FAILED) {
328 if (munmap (flashlight->buffers[i].start, flashlight->buffers[i].length) == -1) {
329 printf ("flashlight_close: unable to unmap memory (%s)\n", strerror (errno));
334 flashlight->n_buffers = 0;
337 if (flashlight->buffers)
338 free (flashlight->buffers);
339 flashlight->buffers = NULL;
341 /* close camera device */
342 if (flashlight->fd != -1) {
343 if (close (flashlight->fd) == -1) {
344 printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
353 int flashlight_init (FlashlightContext_t **pRefContext)
355 FlashlightContext_t *flashlight = NULL;
357 printf ("flashlight_init()\n");
359 if (*pRefContext != NULL) {
360 printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
364 /* allocate memory for context structure */
365 flashlight = malloc (sizeof (FlashlightContext_t));
366 if (flashlight == NULL) {
367 printf ("flashlight_init: unable to allocate memory for context\n");
371 *pRefContext = flashlight;
373 /* initialize default values */
374 memset (flashlight, 0x00, sizeof (FlashlightContext_t));
377 flashlight->n_buffers = 0;
378 flashlight->buffers = NULL;
381 flashlight->min_intensity = 0;
382 flashlight->max_intensity = 11;
387 int flashlight_deinit (FlashlightContext_t *flashlight)
391 printf ("flashlight_deinit()\n");
393 if (flashlight == NULL) {
394 printf ("flashlight_deinit: flashlight context is not valid\n");
398 if (flashlight->fd != -1) {
399 /* check if flashlight isn't enabled before closing device */
400 if (flashlight_get_intensity (flashlight, &intensity) == -1)
404 if (flashlight_set_intensity (flashlight, 0) == -1)
408 if (flashlight_close (flashlight))
412 /* free allocated memory */