hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <xf86drm.h>
#include <sys/mman.h>
 
#include <xf86drm.h>
#include <xf86drmMode.h>
 
#define CURSOR_WIDTH 64
#define CURSOR_HEIGHT 64
 
int main(int argc, const char **argv)
{
  int fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
  int width = CURSOR_WIDTH;
  int height = CURSOR_HEIGHT;
  int crtc_id = 0;
 
  struct drm_mode_create_dumb create_arg = {
    .width = width,
    .height = height,
    .bpp = 32,
  };
 
  drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
  int handle = create_arg.handle;
  int size = create_arg.size;
 
  struct drm_mode_map_dumb map_arg = {
    .handle = handle,
  };
 
  drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
 
  int *ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED,
                  fd, map_arg.offset);
  for (int i = 0; i < width * height; i++)
        ptr[i] = 0x4F000000 | (i % width) * 2 << 16 | (i / height) << 8;
 
  if (argc > 1)
    crtc_id = atoi(argv[1]);
 
  if (argc > 2)
    setenv("DRM_CURSOR_PREFER_PLANE", argv[2], 1);
 
  drmModeSetCursor(fd, crtc_id, handle, width, height);
 
  for (int i = 0; i < 100000; i++) {
    drmModeMoveCursor(fd, crtc_id, i % 1024, i % 1024);
    usleep(100000);
  }
 
  return 0;
}