/*
 * cc -I/usr/include/drm -ldrm trim.c -o trim
 *
 * SYNTAX: trim delta duration scanline
 * sample: trim 24 1 82
 */

#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_syncf_t syncf;

    if ((fd = drmOpen("radeon", 0)) < 0) {
	printf("drmOpen: %s\n", strerror(errno)); exit(-1);
    }
    syncf.trim = 0;
    if (ioctl(fd, DRM_IOCTL_RADEON_SYNCF, &syncf)) {
	printf("DRM_IOCTL_RADEON_SYNCF: %s\n", strerror(errno)); exit(-1);
    }
    printf("0x%08x", syncf.trim);
    if (B(1) && B(2) && B(3)) {
	syncf.trim = VSF_SET_TRIM | atoi(B(3)) << 16 | atoi(B(2)) << 8 | (atoi(B(1)) & 0xff) << 0;
	if (ioctl(fd, DRM_IOCTL_RADEON_SYNCF, &syncf)) {
	    printf("DRM_IOCTL_RADEON_SYNCF: %s\n", strerror(errno)); exit(-1);
	}
	printf(" --> 0x%08x\n", syncf.trim);
    } else {
	printf("\n");
    }
}

