#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <inttypes.h>
#include "bvdd.h"

int main(int argc, char **argv)
{
    int fd;
    uint32_t *mapped;
    bvdd_vram_plane_t vp[3];

    fd = open("/dev/bvdd", O_RDWR);
    if (fd < 0) {
	perror("open");
	return 1;
    }

    vp[0].area_type = BVDD_VRAM_AREA_BVDD;
    vp[0].size = 320 * 240;
    if (ioctl(fd, BVDD_IOCTL_ALLOC_PLANE, &vp[0]) < 0) {
	perror("ioctl(BVDD_IOCTL_ALLOC_PLANE)");
	close(fd);
	return 1;
    }

    vp[1].area_type = BVDD_VRAM_AREA_BVDD;
    vp[1].size = 320 * 240 / 2;
    if (ioctl(fd, BVDD_IOCTL_ALLOC_PLANE, &vp[1]) < 0) {
	perror("ioctl(BVDD_IOCTL_ALLOC_PLANE)");
	close(fd);
	return 1;
    }

    vp[2].area_type = BVDD_VRAM_AREA_BVDD;
    vp[2].size = 320 * 240 / 2;
    if (ioctl(fd, BVDD_IOCTL_ALLOC_PLANE, &vp[2]) < 0) {
	perror("ioctl(BVDD_IOCTL_ALLOC_PLANE)");
	close(fd);
	return 1;
    }
    printf("vp[0].offset = %p\n", vp[0].addr.phys);

    mapped = mmap(0, vp[0].size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, vp[0].addr.phys);
    if (mapped == MAP_FAILED) {
	perror("mmap");
	close(fd);
	return 1;
    }

    {
	bvdd_frame_t frame;
	frame.format = BVDD_FORMAT_YUV420;
	frame.width = 240;
	frame.height = 320;
	frame.x = 0;
	frame.y = 0;
	frame.plane_id = vp[0].id;
	frame.plane_id_u = vp[1].id;
	frame.plane_id_v = vp[2].id;
	ioctl(fd, BVDD_IOCTL_SETUP_OVERLAY2_FRAME, &frame);
    }

    sleep(1);

    ioctl(fd, BVDD_IOCTL_DEBUG);

    close(fd);
    return 0;
}
