#include "camera.h"

#include <assert.h>
#include <errno.h>
#include <glib.h>
#include <libmegapixels.h>
#include <linux/v4l2-subdev.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/wait.h>
#include <unistd.h>

static void
errno_printerr(const char *s)
{
        g_printerr("MPCamera: %s error %d, %s\n", s, errno, strerror(errno));
}

static int
xioctl(int fd, int request, void *arg)
{
        int r;
        do {
                r = ioctl(fd, request, arg);
        } while (r == -1 && errno == EINTR);
        return r;
}

struct video_buffer {
        uint32_t length;
        uint8_t *data;
        int fd;
};

struct _MPCamera {
        libmegapixels_camera *camera;

        struct video_buffer buffers[MAX_VIDEO_BUFFERS];
        uint32_t num_buffers;

        // keeping track of background task child-PIDs for cleanup code
        int child_bg_pids[MAX_BG_TASKS];

        bool use_mplane;
};

MPCamera *
mp_camera_new(libmegapixels_camera *camera)
{
        libmegapixels_open(camera);
        // Query capabilities
        struct v4l2_capability cap;
        if (xioctl(camera->video_fd, VIDIOC_QUERYCAP, &cap) == -1) {
                return NULL;
        }

        // Check whether this is a video capture device
        bool use_mplane;
        if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
                use_mplane = true;
        } else if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE) {
                use_mplane = false;
        } else {
                return NULL;
        }
        libmegapixels_close(camera);

        MPCamera *cam = malloc(sizeof(MPCamera));
        cam->camera = camera;
        cam->num_buffers = 0;
        cam->use_mplane = use_mplane;
        memset(cam->child_bg_pids, 0, sizeof(cam->child_bg_pids[0]) * MAX_BG_TASKS);
        return cam;
}

void
mp_camera_add_bg_task(MPCamera *camera, pid_t pid)
{
        int status;
        while (true) {
                for (size_t i = 0; i < MAX_BG_TASKS; ++i) {
                        if (camera->child_bg_pids[i] == 0) {
                                camera->child_bg_pids[i] = pid;
                                return;
                        } else {
                                // error == -1, still running == 0
                                if (waitpid(camera->child_bg_pids[i],
                                            &status,
                                            WNOHANG) <= 0)
                                        continue; // consider errored wait still
                                                  // running

                                if (WIFEXITED(status)) {
                                        // replace exited
                                        camera->child_bg_pids[i] = pid;
                                        return;
                                }
                        }
                }

                // wait for any status change on child processes
                pid_t changed = waitpid(-1, &status, 0);
                if (WIFEXITED(status)) {
                        // some child exited
                        for (size_t i = 0; i < MAX_BG_TASKS; ++i) {
                                if (camera->child_bg_pids[i] == changed) {
                                        camera->child_bg_pids[i] = pid;
                                        return;
                                }
                        }
                }

                // no luck, repeat and check if something exited maybe
        }
}

void
mp_camera_wait_bg_tasks(MPCamera *camera)
{
        for (size_t i = 0; i < MAX_BG_TASKS; ++i) {
                if (camera->child_bg_pids[i] != 0) {
                        // ignore errors
                        waitpid(camera->child_bg_pids[i], NULL, 0);
                }
        }
}

bool
mp_camera_check_task_complete(MPCamera *camera, pid_t pid)
{
        // this method is potentially unsafe because pid could already be reused at
        // this point, but extremely unlikely so we won't implement this.
        int status;

        if (pid == 0)
                return true;

        // ignore errors (-1), no exit == 0
		// ECHILD => no such child (probably already reaped in mp_camera_add_bg_task() or mp_camera_wait_bg_tasks()

        int pidchange = waitpid(pid, &status, WNOHANG);

        if (pidchange == -1 ? errno == ECHILD : WIFEXITED(status)) {
                for (size_t i = 0; i < MAX_BG_TASKS; ++i) {
                        if (camera->child_bg_pids[i] == pid) {
                                camera->child_bg_pids[i] = 0;
                                break;
                        }
                }
                return true;
        } else {
                return false;
        }
}

int
mp_camera_get_video_fd(MPCamera *camera)
{
        return camera->camera->video_fd;
}

static enum v4l2_buf_type
get_buf_type(MPCamera *camera)
{
        if (camera->use_mplane) {
                return V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
        }
        return V4L2_BUF_TYPE_VIDEO_CAPTURE;
}

bool
mp_camera_start_capture(MPCamera *camera)
{
        g_return_val_if_fail(camera->num_buffers == 0, false);

        const enum v4l2_buf_type buftype = get_buf_type(camera);

        // Start by requesting buffers
        struct v4l2_requestbuffers req = {};
        req.count = MAX_VIDEO_BUFFERS;
        req.type = buftype;
        req.memory = V4L2_MEMORY_MMAP;

        if (xioctl(camera->camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
                errno_printerr("VIDIOC_REQBUFS");
                return false;
        }

        if (req.count < 2) {
                g_printerr(
                        "Insufficient buffer memory. Only %d buffers available.\n",
                        req.count);
                goto error;
        }

        for (uint32_t i = 0; i < req.count; ++i) {
                // Query each buffer and mmap it
                struct v4l2_buffer buf = {
                        .type = buftype,
                        .memory = V4L2_MEMORY_MMAP,
                        .index = i,
                };

                struct v4l2_plane planes[1];
                if (camera->use_mplane) {
                        buf.m.planes = planes;
                        buf.length = 1;
                }

                if (xioctl(camera->camera->video_fd, VIDIOC_QUERYBUF, &buf) == -1) {
                        errno_printerr("VIDIOC_QUERYBUF");
                        break;
                }

                if (camera->use_mplane) {
                        camera->buffers[i].length = planes[0].length;
                        camera->buffers[i].data = mmap(NULL,
                                                       planes[0].length,
                                                       PROT_READ,
                                                       MAP_SHARED,
                                                       camera->camera->video_fd,
                                                       planes[0].m.mem_offset);
                } else {
                        camera->buffers[i].length = buf.length;
                        camera->buffers[i].data = mmap(NULL,
                                                       buf.length,
                                                       PROT_READ,
                                                       MAP_SHARED,
                                                       camera->camera->video_fd,
                                                       buf.m.offset);
                }

                if (camera->buffers[i].data == MAP_FAILED) {
                        errno_printerr("mmap");
                        break;
                }

                struct v4l2_exportbuffer expbuf = {
                        .type = buftype,
                        .index = i,
                };
                if (xioctl(camera->camera->video_fd, VIDIOC_EXPBUF, &expbuf) == -1) {
                        errno_printerr("VIDIOC_EXPBUF");
                        break;
                }

                camera->buffers[i].fd = expbuf.fd;

                ++camera->num_buffers;
        }

        if (camera->num_buffers != req.count) {
                g_printerr("Unable to map all buffers\n");
                goto error;
        }

        for (uint32_t i = 0; i < camera->num_buffers; ++i) {
                struct v4l2_buffer buf = {
                        .type = buftype,
                        .memory = V4L2_MEMORY_MMAP,
                        .index = i,
                };

                struct v4l2_plane planes[1];
                if (camera->use_mplane) {
                        buf.m.planes = planes;
                        buf.length = 1;
                }

                // Queue the buffer for capture
                if (xioctl(camera->camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
                        errno_printerr("VIDIOC_QBUF");
                        goto error;
                }
        }

        // Start capture
        enum v4l2_buf_type type = buftype;
        if (xioctl(camera->camera->video_fd, VIDIOC_STREAMON, &type) == -1) {
                errno_printerr("VIDIOC_STREAMON");
                goto error;
        }

        return true;

error:
        // Unmap any mapped buffers
        assert(camera->num_buffers <= MAX_VIDEO_BUFFERS);
        for (uint32_t i = 0; i < camera->num_buffers; ++i) {
                if (munmap(camera->buffers[i].data, camera->buffers[i].length) ==
                    -1) {
                        errno_printerr("munmap");
                }

                if (close(camera->buffers[i].fd) == -1) {
                        errno_printerr("close");
                }
        }

        // Reset allocated buffers
        {
                struct v4l2_requestbuffers req = {};
                req.count = 0;
                req.type = buftype;
                req.memory = V4L2_MEMORY_MMAP;

                if (xioctl(camera->camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
                        errno_printerr("VIDIOC_REQBUFS");
                }
        }

        return false;
}

bool
mp_camera_stop_capture(MPCamera *camera)
{
        g_return_val_if_fail(camera->num_buffers > 0, false);

        const enum v4l2_buf_type buftype = get_buf_type(camera);

        enum v4l2_buf_type type = buftype;
        if (xioctl(camera->camera->video_fd, VIDIOC_STREAMOFF, &type) == -1) {
                errno_printerr("VIDIOC_STREAMOFF");
        }

        assert(camera->num_buffers <= MAX_VIDEO_BUFFERS);
        for (int i = 0; i < camera->num_buffers; ++i) {
                if (munmap(camera->buffers[i].data, camera->buffers[i].length) ==
                    -1) {
                        errno_printerr("munmap");
                }

                if (close(camera->buffers[i].fd) == -1) {
                        errno_printerr("close");
                }
        }

        camera->num_buffers = 0;

        struct v4l2_requestbuffers req = {};
        req.count = 0;
        req.type = buftype;
        req.memory = V4L2_MEMORY_MMAP;
        if (xioctl(camera->camera->video_fd, VIDIOC_REQBUFS, &req) == -1) {
                errno_printerr("VIDIOC_REQBUFS");
        }

        return true;
}

bool
mp_camera_is_capturing(MPCamera *camera)
{
        return camera->num_buffers > 0;
}

bool
mp_camera_capture_buffer(MPCamera *camera, MPBuffer *buffer)
{
        const enum v4l2_buf_type buftype = get_buf_type(camera);

        struct v4l2_buffer buf = {};
        buf.type = buftype;
        buf.memory = V4L2_MEMORY_MMAP;

        struct v4l2_plane planes = {};
        if (camera->use_mplane) {
                buf.m.planes = &planes;
                buf.length = 1;
        }

        if (xioctl(camera->camera->video_fd, VIDIOC_DQBUF, &buf) == -1) {
                switch (errno) {
                case EAGAIN:
                        return true;
                case EIO:
                        /* Could ignore EIO, see spec. */
                        /* fallthrough */
                default:
                        errno_printerr("VIDIOC_DQBUF");
                        exit(1);
                        return false;
                }
        }

        int format = camera->camera->current_mode->format;
        assert(format != 0);
        uint32_t width = camera->camera->current_mode->width;
        uint32_t height = camera->camera->current_mode->height;

        uint32_t bytesused;
        if (camera->use_mplane) {
                bytesused = planes.bytesused;
        } else {
                bytesused = buf.bytesused;
        }

        assert(bytesused == (libmegapixels_mode_width_to_bytes(format, width) +
                             libmegapixels_mode_width_to_padding(format, width)) *
                                    height);
        assert(bytesused == camera->buffers[buf.index].length);

        buffer->index = buf.index;
        buffer->data = camera->buffers[buf.index].data;
        buffer->fd = camera->buffers[buf.index].fd;

        return true;
}

bool
mp_camera_release_buffer(MPCamera *camera, uint32_t buffer_index)
{
        const enum v4l2_buf_type buftype = get_buf_type(camera);

        struct v4l2_buffer buf = {};
        buf.type = buftype;
        buf.memory = V4L2_MEMORY_MMAP;
        buf.index = buffer_index;

        struct v4l2_plane planes = {};
        if (camera->use_mplane) {
                buf.m.planes = &planes;
                buf.length = 1;
        }

        if (xioctl(camera->camera->video_fd, VIDIOC_QBUF, &buf) == -1) {
                errno_printerr("VIDIOC_QBUF");
                return false;
        }
        return true;
}

struct int_str_pair {
        uint32_t value;
        const char *str;
};

struct int_str_pair control_id_names[] = {
        { V4L2_CID_BRIGHTNESS, "BRIGHTNESS" },
        { V4L2_CID_CONTRAST, "CONTRAST" },
        { V4L2_CID_SATURATION, "SATURATION" },
        { V4L2_CID_HUE, "HUE" },
        { V4L2_CID_AUDIO_VOLUME, "AUDIO_VOLUME" },
        { V4L2_CID_AUDIO_BALANCE, "AUDIO_BALANCE" },
        { V4L2_CID_AUDIO_BASS, "AUDIO_BASS" },
        { V4L2_CID_AUDIO_TREBLE, "AUDIO_TREBLE" },
        { V4L2_CID_AUDIO_MUTE, "AUDIO_MUTE" },
        { V4L2_CID_AUDIO_LOUDNESS, "AUDIO_LOUDNESS" },
        { V4L2_CID_BLACK_LEVEL, "BLACK_LEVEL" },
        { V4L2_CID_AUTO_WHITE_BALANCE, "AUTO_WHITE_BALANCE" },
        { V4L2_CID_DO_WHITE_BALANCE, "DO_WHITE_BALANCE" },
        { V4L2_CID_RED_BALANCE, "RED_BALANCE" },
        { V4L2_CID_BLUE_BALANCE, "BLUE_BALANCE" },
        { V4L2_CID_GAMMA, "GAMMA" },
        { V4L2_CID_WHITENESS, "WHITENESS" },
        { V4L2_CID_EXPOSURE, "EXPOSURE" },
        { V4L2_CID_AUTOGAIN, "AUTOGAIN" },
        { V4L2_CID_GAIN, "GAIN" },
        { V4L2_CID_HFLIP, "HFLIP" },
        { V4L2_CID_VFLIP, "VFLIP" },
        { V4L2_CID_POWER_LINE_FREQUENCY, "POWER_LINE_FREQUENCY" },
        { V4L2_CID_HUE_AUTO, "HUE_AUTO" },
        { V4L2_CID_WHITE_BALANCE_TEMPERATURE, "WHITE_BALANCE_TEMPERATURE" },
        { V4L2_CID_SHARPNESS, "SHARPNESS" },
        { V4L2_CID_BACKLIGHT_COMPENSATION, "BACKLIGHT_COMPENSATION" },
        { V4L2_CID_CHROMA_AGC, "CHROMA_AGC" },
        { V4L2_CID_COLOR_KILLER, "COLOR_KILLER" },
        { V4L2_CID_COLORFX, "COLORFX" },
        { V4L2_CID_AUTOBRIGHTNESS, "AUTOBRIGHTNESS" },
        { V4L2_CID_BAND_STOP_FILTER, "BAND_STOP_FILTER" },
        { V4L2_CID_ROTATE, "ROTATE" },
        { V4L2_CID_BG_COLOR, "BG_COLOR" },
        { V4L2_CID_CHROMA_GAIN, "CHROMA_GAIN" },
        { V4L2_CID_ILLUMINATORS_1, "ILLUMINATORS_1" },
        { V4L2_CID_ILLUMINATORS_2, "ILLUMINATORS_2" },
        { V4L2_CID_MIN_BUFFERS_FOR_CAPTURE, "MIN_BUFFERS_FOR_CAPTURE" },
        { V4L2_CID_MIN_BUFFERS_FOR_OUTPUT, "MIN_BUFFERS_FOR_OUTPUT" },
        { V4L2_CID_ALPHA_COMPONENT, "ALPHA_COMPONENT" },
        { V4L2_CID_COLORFX_CBCR, "COLORFX_CBCR" },
        { V4L2_CID_LASTP1, "LASTP1" },
        { V4L2_CID_USER_MEYE_BASE, "USER_MEYE_BASE" },
        { V4L2_CID_USER_BTTV_BASE, "USER_BTTV_BASE" },
        { V4L2_CID_USER_S2255_BASE, "USER_S2255_BASE" },
        { V4L2_CID_USER_SI476X_BASE, "USER_SI476X_BASE" },
        { V4L2_CID_USER_TI_VPE_BASE, "USER_TI_VPE_BASE" },
        { V4L2_CID_USER_SAA7134_BASE, "USER_SAA7134_BASE" },
        { V4L2_CID_USER_ADV7180_BASE, "USER_ADV7180_BASE" },
        { V4L2_CID_USER_TC358743_BASE, "USER_TC358743_BASE" },
        { V4L2_CID_USER_MAX217X_BASE, "USER_MAX217X_BASE" },
        { V4L2_CID_USER_IMX_BASE, "USER_IMX_BASE" },
        // { V4L2_CID_USER_ATMEL_ISC_BASE, "USER_ATMEL_ISC_BASE" },
        { V4L2_CID_CAMERA_CLASS_BASE, "CAMERA_CLASS_BASE" },
        { V4L2_CID_CAMERA_CLASS, "CAMERA_CLASS" },
        { V4L2_CID_EXPOSURE_AUTO, "EXPOSURE_AUTO" },
        { V4L2_CID_EXPOSURE_ABSOLUTE, "EXPOSURE_ABSOLUTE" },
        { V4L2_CID_EXPOSURE_AUTO_PRIORITY, "EXPOSURE_AUTO_PRIORITY" },
        { V4L2_CID_PAN_RELATIVE, "PAN_RELATIVE" },
        { V4L2_CID_TILT_RELATIVE, "TILT_RELATIVE" },
        { V4L2_CID_PAN_RESET, "PAN_RESET" },
        { V4L2_CID_TILT_RESET, "TILT_RESET" },
        { V4L2_CID_PAN_ABSOLUTE, "PAN_ABSOLUTE" },
        { V4L2_CID_TILT_ABSOLUTE, "TILT_ABSOLUTE" },
        { V4L2_CID_FOCUS_ABSOLUTE, "FOCUS_ABSOLUTE" },
        { V4L2_CID_FOCUS_RELATIVE, "FOCUS_RELATIVE" },
        { V4L2_CID_FOCUS_AUTO, "FOCUS_AUTO" },
        { V4L2_CID_ZOOM_ABSOLUTE, "ZOOM_ABSOLUTE" },
        { V4L2_CID_ZOOM_RELATIVE, "ZOOM_RELATIVE" },
        { V4L2_CID_ZOOM_CONTINUOUS, "ZOOM_CONTINUOUS" },
        { V4L2_CID_PRIVACY, "PRIVACY" },
        { V4L2_CID_IRIS_ABSOLUTE, "IRIS_ABSOLUTE" },
        { V4L2_CID_IRIS_RELATIVE, "IRIS_RELATIVE" },
        { V4L2_CID_AUTO_EXPOSURE_BIAS, "AUTO_EXPOSURE_BIAS" },
        { V4L2_CID_AUTO_N_PRESET_WHITE_BALANCE, "AUTO_N_PRESET_WHITE_BALANCE" },
        { V4L2_CID_WIDE_DYNAMIC_RANGE, "WIDE_DYNAMIC_RANGE" },
        { V4L2_CID_IMAGE_STABILIZATION, "IMAGE_STABILIZATION" },
        { V4L2_CID_ISO_SENSITIVITY, "ISO_SENSITIVITY" },
        { V4L2_CID_ISO_SENSITIVITY_AUTO, "ISO_SENSITIVITY_AUTO" },
        { V4L2_CID_EXPOSURE_METERING, "EXPOSURE_METERING" },
        { V4L2_CID_SCENE_MODE, "SCENE_MODE" },
        { V4L2_CID_3A_LOCK, "3A_LOCK" },
        { V4L2_CID_AUTO_FOCUS_START, "AUTO_FOCUS_START" },
        { V4L2_CID_AUTO_FOCUS_STOP, "AUTO_FOCUS_STOP" },
        { V4L2_CID_AUTO_FOCUS_STATUS, "AUTO_FOCUS_STATUS" },
        { V4L2_CID_AUTO_FOCUS_RANGE, "AUTO_FOCUS_RANGE" },
        { V4L2_CID_PAN_SPEED, "PAN_SPEED" },
        { V4L2_CID_TILT_SPEED, "TILT_SPEED" },
        // { V4L2_CID_CAMERA_ORIENTATION, "CAMERA_ORIENTATION" },
        // { V4L2_CID_CAMERA_SENSOR_ROTATION, "CAMERA_SENSOR_ROTATION" },
        { V4L2_CID_FLASH_LED_MODE, "FLASH_LED_MODE" },
        { V4L2_CID_FLASH_STROBE_SOURCE, "FLASH_STROBE_SOURCE" },
        { V4L2_CID_FLASH_STROBE, "FLASH_STROBE" },
        { V4L2_CID_FLASH_STROBE_STOP, "FLASH_STROBE_STOP" },
        { V4L2_CID_FLASH_STROBE_STATUS, "FLASH_STROBE_STATUS" },
        { V4L2_CID_FLASH_TIMEOUT, "FLASH_TIMEOUT" },
        { V4L2_CID_FLASH_INTENSITY, "FLASH_INTENSITY" },
        { V4L2_CID_FLASH_TORCH_INTENSITY, "FLASH_TORCH_INTENSITY" },
        { V4L2_CID_FLASH_INDICATOR_INTENSITY, "FLASH_INDICATOR_INTENSITY" },
        { V4L2_CID_FLASH_FAULT, "FLASH_FAULT" },
        { V4L2_CID_FLASH_CHARGE, "FLASH_CHARGE" },
        { V4L2_CID_FLASH_READY, "FLASH_READY" },
};

const char *
mp_control_id_to_str(uint32_t id)
{
        size_t size = sizeof(control_id_names) / sizeof(*control_id_names);

        for (size_t i = 0; i < size; ++i) {
                if (control_id_names[i].value == id) {
                        return control_id_names[i].str;
                }
        }

        return "UNKNOWN";
}

struct int_str_pair control_type_names[] = {
        { V4L2_CTRL_TYPE_INTEGER, "INTEGER" },
        { V4L2_CTRL_TYPE_BOOLEAN, "BOOLEAN" },
        { V4L2_CTRL_TYPE_MENU, "MENU" },
        { V4L2_CTRL_TYPE_INTEGER_MENU, "INTEGER_MENU" },
        { V4L2_CTRL_TYPE_BITMASK, "BITMASK" },
        { V4L2_CTRL_TYPE_BUTTON, "BUTTON" },
        { V4L2_CTRL_TYPE_INTEGER64, "INTEGER64" },
        { V4L2_CTRL_TYPE_STRING, "STRING" },
        { V4L2_CTRL_TYPE_CTRL_CLASS, "CTRL_CLASS" },
        { V4L2_CTRL_TYPE_U8, "U8" },
        { V4L2_CTRL_TYPE_U16, "U16" },
        { V4L2_CTRL_TYPE_U32, "U32" },
        // { V4L2_CTRL_TYPE_MPEG2_SLICE_PARAMS, "MPEG2_SLICE_PARAMS" },
        // { V4L2_CTRL_TYPE_MPEG2_QUANTIZATION, "MPEG2_QUANTIZATION" },
        // { V4L2_CTRL_TYPE_AREA, "AREA" },
        // { V4L2_CTRL_TYPE_H264_SPS, "H264_SPS" },
        // { V4L2_CTRL_TYPE_H264_PPS, "H264_PPS" },
        // { V4L2_CTRL_TYPE_H264_SCALING_MATRIX, "H264_SCALING_MATRIX" },
        // { V4L2_CTRL_TYPE_H264_SLICE_PARAMS, "H264_SLICE_PARAMS" },
        // { V4L2_CTRL_TYPE_H264_DECODE_PARAMS, "H264_DECODE_PARAMS" },
        // { V4L2_CTRL_TYPE_HEVC_SPS, "HEVC_SPS" },
        // { V4L2_CTRL_TYPE_HEVC_PPS, "HEVC_PPS" },
        // { V4L2_CTRL_TYPE_HEVC_SLICE_PARAMS, "HEVC_SLICE_PARAMS" },
};

const char *
mp_control_type_to_str(uint32_t type)
{
        size_t size = sizeof(control_type_names) / sizeof(*control_type_names);

        for (size_t i = 0; i < size; ++i) {
                if (control_type_names[i].value == type) {
                        return control_type_names[i].str;
                }
        }

        return "UNKNOWN";
}

struct _MPControlList {
        MPControl control;
        MPControlList *next;
};

MPControlList *
mp_camera_list_controls(libmegapixels_camera *camera)
{
        MPControlList *item = NULL;

        struct v4l2_query_ext_ctrl ctrl = {};
        ctrl.id = V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
        while (true) {
                if (xioctl(camera->sensor_fd, VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
                        if (errno != EINVAL) {
                                errno_printerr("VIDIOC_QUERY_EXT_CTRL");
                        }
                        break;
                }

                MPControl control = {
                        .id = ctrl.id,
                        .type = ctrl.type,
                        .name = {},
                        .min = ctrl.minimum,
                        .max = ctrl.maximum,
                        .step = ctrl.step,
                        .default_value = ctrl.default_value,
                        .flags = ctrl.flags,
                        .element_size = ctrl.elem_size,
                        .element_count = ctrl.elems,
                        .dimensions_count = ctrl.nr_of_dims,
                        .dimensions = {},
                };

                strcpy(control.name, ctrl.name);
                memcpy(control.dimensions,
                       ctrl.dims,
                       sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);

                MPControlList *new_item = malloc(sizeof(MPControlList));
                new_item->control = control;
                new_item->next = item;
                item = new_item;

                ctrl.id |= V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
        }

        return item;
}

MPControl *
mp_control_list_get(MPControlList *list)
{
        g_return_val_if_fail(list, NULL);
        return &list->control;
}

MPControlList *
mp_control_list_next(MPControlList *list)
{
        g_return_val_if_fail(list, NULL);
        return list->next;
}

void
mp_control_list_free(MPControlList *list)
{
        while (list) {
                MPControlList *tmp = list;
                list = tmp->next;
                free(tmp);
        }
}

bool
mp_camera_query_control(int fd, uint32_t id, MPControl *control)
{
        struct v4l2_query_ext_ctrl ctrl = {};
        ctrl.id = id;
        if (xioctl(fd, VIDIOC_QUERY_EXT_CTRL, &ctrl) == -1) {
                if (errno != EINVAL) {
                        errno_printerr("VIDIOC_QUERY_EXT_CTRL");
                }
                return false;
        }

        if (control) {
                control->fd = fd;
                control->id = ctrl.id;
                control->type = ctrl.type;
                strcpy(control->name, ctrl.name);
                control->min = ctrl.minimum;
                control->max = ctrl.maximum;
                control->step = ctrl.step;
                control->default_value = ctrl.default_value;
                control->flags = ctrl.flags;
                control->element_size = ctrl.elem_size;
                control->element_count = ctrl.elems;
                control->dimensions_count = ctrl.nr_of_dims;
                memcpy(control->dimensions,
                       ctrl.dims,
                       sizeof(uint32_t) * V4L2_CTRL_MAX_DIMS);
        }
        return true;
}

static bool
control_impl_int32(MPControl *control, int request, int32_t *value)
{
        struct v4l2_ext_control ctrl = {};
        ctrl.id = control->id;
        ctrl.value = *value;

        struct v4l2_ext_controls ctrls = {
                .ctrl_class = 0,
                .which = V4L2_CTRL_WHICH_CUR_VAL,
                .count = 1,
                .controls = &ctrl,
        };
        if (xioctl(control->fd, request, &ctrls) == -1) {
                return false;
        }

        *value = ctrl.value;
        return true;
}

pid_t
mp_camera_control_set_int32_bg(MPCamera *camera, MPControl *control, int32_t v)
{
        struct v4l2_ext_control ctrl = {};
        ctrl.id = control->id;
        ctrl.value = v;

        struct v4l2_ext_controls ctrls = {
                .ctrl_class = 0,
                .which = V4L2_CTRL_WHICH_CUR_VAL,
                .count = 1,
                .controls = &ctrl,
        };

        // fork only after all the memory has been read
        pid_t pid = fork();
        if (pid == -1) {
                return 0; // discard errors, nothing to do in parent process
        } else if (pid != 0) {
                // parent process adding pid to wait list (to clear zombie processes)
                mp_camera_add_bg_task(camera, pid);
                return pid;
        }

        // ignore errors
        xioctl(control->fd, VIDIOC_S_EXT_CTRLS, &ctrls);
        // exit without calling exit handlers
        _exit(0);
}

bool
mp_camera_control_try_int32(MPControl *control, int32_t *v)
{
        return control_impl_int32(control, VIDIOC_TRY_EXT_CTRLS, v);
}

bool
mp_camera_control_set_int32(MPControl *control, int32_t v)
{
        return control_impl_int32(control, VIDIOC_S_EXT_CTRLS, &v);
}

int32_t
mp_camera_control_get_int32(MPControl *control)
{
        int32_t v = 0;
        control_impl_int32(control, VIDIOC_G_EXT_CTRLS, &v);
        return v;
}

bool
mp_camera_control_try_boolean(MPControl *control, bool *v)
{
        int32_t value = *v;
        bool s = control_impl_int32(control, VIDIOC_TRY_EXT_CTRLS, &value);
        *v = value;
        return s;
}

bool
mp_camera_control_set_bool(MPControl *control, bool v)
{
        int32_t value = v;
        return control_impl_int32(control, VIDIOC_S_EXT_CTRLS, &value);
}

bool
mp_camera_control_get_bool(MPControl *control)
{
        int32_t v = false;
        control_impl_int32(control, VIDIOC_G_EXT_CTRLS, &v);
        return v;
}

pid_t
mp_camera_control_set_bool_bg(MPCamera *camera, MPControl *control, bool v)
{
        int32_t value = v;
        return mp_camera_control_set_int32_bg(camera, control, value);
}
