#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>

/* Manually recreate these defines to avoid depending upon libdrm-dev */
#define DRM_IOCTL_BASE			'd'
#define DRM_IO(nr)			_IO(DRM_IOCTL_BASE,nr)
#define DRM_IOR(nr,type)		_IOR(DRM_IOCTL_BASE,nr,type)
#define DRM_IOW(nr,type)		_IOW(DRM_IOCTL_BASE,nr,type)
#define DRM_IOWR(nr,type)		_IOWR(DRM_IOCTL_BASE,nr,type)
#define DRM_COMMAND_BASE 0x40

struct intel_getparam {
	int param;
	int *value;
};
#define I915_PARAM_HAS_GEM 5

#define I915_GET_PARAM 0x6
#define DRM_IOCTL_I915_GET_PARAM         DRM_IOWR(DRM_COMMAND_BASE + I915_GET_PARAM, struct intel_getparam)

int main(int argc, char **argv)
{
	const char *card = "/dev/dri/card0";
	struct intel_getparam gp;
	int has_gem = 0;
	int fd, ret;

	if (argc > 1)
		card = argv[1];

	fd = open (card, O_RDWR);
	if (fd < 0) {
		perror (card);
		return 1;
	}

	gp.param = I915_PARAM_HAS_GEM;
	gp.value = &has_gem;
	ret = ioctl (fd, DRM_IOCTL_I915_GET_PARAM, &gp);
	if (ret < 0)
		perror("GET_PARAM failed");
	else
		printf ("%s: GEM %sabled\n", card, has_gem ? "en" : "dis");
	close (fd);
	return 0;
}