#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>

#include <nouveau.h>
#include <nouveau_drm.h>

#include "dri2_auth.h"

int main(int argc, char *argv[])
{
	int fd;
	struct nouveau_device *dev = NULL;
	struct nouveau_client *client = NULL;
	struct nouveau_object *chan, *obj;
	struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };

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

	if (dri2_auth(fd))
		return 1;

	if (nouveau_device_wrap(fd, 1, &dev)) {
		perror("nouveau_device_wrap");
		return 1;
	}

	if (nouveau_object_new(&dev->object, 0, NOUVEAU_FIFO_CHANNEL_CLASS,
			&nv04_data, sizeof(nv04_data), &chan)) {
		perror("nouveau_object_new");
		return 1;
	}

	if (nouveau_client_new(dev, &client)) {
		perror("nouveau_client_new");
		return 1;
	}

	while (true) {
		//errors:
		//0x0030 - pgraph
		//0x502d - pgraph
		//0x5039 - pgraph
		//0x50c0 - pgraph
		//0x8297 - pgraph

		//no errors:
		//0x74c1 - crypt
		//0x8274 - mpeg
		//0x506e - sw

		if (nouveau_object_new(chan, 0xbeef0000, 0x8297, NULL, 0, &obj)) {
			perror("nouveau_object_new 2");
			return 1;
		}
		nouveau_object_del(&obj);
	}

	return 0;
}
