/*
 * cc -I/usr/include/drm -ldrm trim.c -o trim
 */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <xf86drm.h>
#include <radeon_drm.h>

#define B(a) (*(argv + (a)) ? *(argv + (a)) : 0)

main(argc, argv)
    char **argv;
{
    int fd;
    drm_radeon_vsync_t vsync;

    if ((fd = drmOpen("radeon", 0)) < 0) {
	printf("drmOpen: %s\n", strerror(errno)); exit(-1);
    }
    if (B(1) && B(2) && B(3) && B(4)) {
	vsync.trim = VBL_SET_TRIM | (*B(4) == '-' ? VBL_INC_RATE : 0) | atoi(B(3)) << 16 | atoi(B(2)) << 8 | atoi(B(1)) << 0;
	printf("===> 0x%08x\n", vsync.trim);

	if (ioctl(fd, DRM_IOCTL_RADEON_VSYNC, &vsync)) {
	    printf("DRM_IOCTL_RADEON_VSYNC: %s\n", strerror(errno)); exit(-1);
	}
    } else {
	printf("%s delta duration initial_wait polarity\n", *argv);
	printf("sample: %s 24 85 200 -\n", *argv);
    }
}

