/* mkcursors: build the NBCUR1 cursor bundle (include/cursor_abi.h) from an * Xcursor theme directory. * * ./mkcursors * * For every shape in CURSOR_LIST it reads /, takes the * image chunks of nominal size , and writes them in shape order. * Animated cursors (Adwaita's wait/progress: 60 frames x 16 ms) keep every * Nth frame so that no frame is shorter than MIN_DELAY_MS, with the delay * scaled to keep the animation speed; the kernel only advances frames from * the poll loop, so 16 ms frames would just be skipped anyway. */ #include #include #include #include #include "cursor_abi.h" #define XCUR_IMAGE 0xFFFD0002u #define MIN_DELAY_MS 60 #define MAX_FRAMES 64 struct xframe { uint32_t w, h, xhot, yhot, delay; const uint32_t *px; }; static uint32_t rd32(const unsigned char *p) { return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); } static unsigned char *read_file(const char *path, size_t *len) { FILE *f = fopen(path, "rb"); unsigned char *buf; long n; if (!f) return NULL; fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET); buf = malloc(n > 0 ? (size_t)n : 1); if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) { fclose(f); free(buf); return NULL; } fclose(f); *len = (size_t)n; return buf; } /* Collect the image chunks of `size`; returns the frame count or -1. */ static int parse_xcursor(const unsigned char *d, size_t len, unsigned int size, struct xframe *out, int max) { uint32_t hdr, ntoc, i; int n = 0; if (len < 16 || memcmp(d, "Xcur", 4) != 0) return -1; hdr = rd32(d + 4); ntoc = rd32(d + 12); if ((size_t)hdr + (size_t)ntoc * 12 > len) return -1; for (i = 0; i < ntoc; i++) { const unsigned char *t = d + hdr + i * 12; uint32_t pos = rd32(t + 8); const unsigned char *c; struct xframe fr; if (rd32(t) != XCUR_IMAGE || rd32(t + 4) != size) continue; if ((size_t)pos + 36 > len) return -1; c = d + pos; fr.w = rd32(c + 16); fr.h = rd32(c + 20); fr.xhot = rd32(c + 24); fr.yhot = rd32(c + 28); fr.delay = rd32(c + 32); if (!fr.w || !fr.h || fr.w > CURSOR_MAX_DIM || fr.h > CURSOR_MAX_DIM || fr.xhot >= fr.w || fr.yhot >= fr.h) return -1; if ((size_t)pos + rd32(c) + (size_t)fr.w * fr.h * 4 > len) return -1; fr.px = (const uint32_t *)(c + rd32(c)); if (n >= max) return -1; out[n++] = fr; } return n ? n : -1; } #define CURSOR_FILE_(e, css, file) file, static const char *const theme_files[CURSOR_COUNT] = { CURSOR_LIST(CURSOR_FILE_) }; #undef CURSOR_FILE_ int main(int argc, char **argv) { struct cursor_file_hdr hdr; struct cursor_file_shape shapes[CURSOR_COUNT]; unsigned char *files[CURSOR_COUNT]; struct xframe frames[CURSOR_COUNT][MAX_FRAMES]; int nframes[CURSOR_COUNT], step[CURSOR_COUNT]; unsigned int size, off; FILE *out; int i, j; if (argc != 4) { fprintf(stderr, "usage: %s \n", argv[0]); return 2; } size = (unsigned int)atoi(argv[2]); off = sizeof(hdr) + sizeof(shapes); for (i = 0; i < CURSOR_COUNT; i++) { char path[1024]; size_t len; int n, kept; snprintf(path, sizeof(path), "%s/%s", argv[1], theme_files[i]); files[i] = read_file(path, &len); if (!files[i]) { fprintf(stderr, "mkcursors: cannot read %s\n", path); return 1; } n = parse_xcursor(files[i], len, size, frames[i], MAX_FRAMES); if (n < 0) { fprintf(stderr, "mkcursors: %s: no valid %u px image\n", path, size); return 1; } step[i] = 1; if (n > 1 && frames[i][0].delay && frames[i][0].delay < MIN_DELAY_MS) step[i] = (MIN_DELAY_MS + frames[i][0].delay - 1) / frames[i][0].delay; kept = (n + step[i] - 1) / step[i]; nframes[i] = n; shapes[i].w = (unsigned short)frames[i][0].w; shapes[i].h = (unsigned short)frames[i][0].h; shapes[i].hot_x = (unsigned short)frames[i][0].xhot; shapes[i].hot_y = (unsigned short)frames[i][0].yhot; shapes[i].frames = (unsigned short)kept; shapes[i].delay_ms = (unsigned short)(n > 1 ? frames[i][0].delay * (unsigned int)step[i] : 0); shapes[i].offset = off; for (j = 0; j < n; j += step[i]) if (frames[i][j].w != frames[i][0].w || frames[i][j].h != frames[i][0].h) { fprintf(stderr, "mkcursors: %s: frame sizes differ\n", path); return 1; } off += (unsigned int)kept * shapes[i].w * shapes[i].h * 4; } memcpy(hdr.magic, CURSOR_FILE_MAGIC, 8); hdr.count = CURSOR_COUNT; hdr.nominal = size; out = fopen(argv[3], "wb"); if (!out) { perror(argv[3]); return 1; } fwrite(&hdr, sizeof(hdr), 1, out); fwrite(shapes, sizeof(shapes), 1, out); for (i = 0; i < CURSOR_COUNT; i++) for (j = 0; j < nframes[i]; j += step[i]) fwrite(frames[i][j].px, 4, (size_t)shapes[i].w * shapes[i].h, out); if (fclose(out) != 0) { perror(argv[3]); return 1; } for (i = 0; i < CURSOR_COUNT; i++) free(files[i]); return 0; }