#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <linux/fb.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>

int want_to_exit = 0;

void sighandler(int signum)
{
	want_to_exit = 1;
}

int main(int argc, char *argv[])
{
	int r, fb;
	struct fb_var_screeninfo fbvar;
	struct fb_fix_screeninfo fbfix;
	short *mem;
	int i;
	char y, u, v;

	signal(SIGINT, &sighandler);
	signal(SIGTERM, &sighandler);

	fb = open("/dev/fb1", O_RDWR);
	r = ioctl(fb, FBIOGET_VSCREENINFO, &fbvar);
	if (r) {
		perror("get_vscreeninfo ioctl");
		goto error;
	}
	fbvar.red.offset = 11; fbvar.red.length = 5;
	fbvar.green.offset = 5; fbvar.green.length = 6;
	fbvar.blue.offset = 0; fbvar.green.length = 5;
	fbvar.transp.offset = 0; fbvar.transp.length = 0;
	fbvar.xres_virtual = fbvar.xres = 240;
	fbvar.yres_virtual = fbvar.yres = 320;
	fbvar.bits_per_pixel = 16;
	//fbvar.nonstd = (4 << 20) | (0 << 10) | (0); /* YUV420P, X=0, Y=0 */
	fbvar.nonstd = (0 << 20) | (0 << 10) | (0); /* RGB, X=0, Y=0 */
	r = ioctl(fb, FBIOPUT_VSCREENINFO, &fbvar);
	if (r) {
		perror("put_vscreeninfo ioctl");
		goto error;
	}
	r = ioctl(fb, FBIOGET_FSCREENINFO, &fbfix);
	if (r) {
		perror("get_fscreeninfo ioctl");
		goto error;
	}

	mem = mmap(NULL, fbfix.smem_len, (PROT_READ | PROT_WRITE),
		MAP_SHARED, fb, 0);

	while (!want_to_exit)
	{
		for (i = 0; i < 240 * 320; i++)
		{
			mem[i] = 0x1f;
		}
	}
	munmap(mem, fbfix.smem_len);
	close(fb);

error:
	return 0;
}
