mkcursors.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* mkcursors: build the NBCUR1 cursor bundle (include/cursor_abi.h) from an
  2. * Xcursor theme directory.
  3. *
  4. * ./mkcursors <theme-dir> <size> <out.cur>
  5. *
  6. * For every shape in CURSOR_LIST it reads <theme-dir>/<file>, takes the
  7. * image chunks of nominal size <size>, and writes them in shape order.
  8. * Animated cursors (Adwaita's wait/progress: 60 frames x 16 ms) keep every
  9. * Nth frame so that no frame is shorter than MIN_DELAY_MS, with the delay
  10. * scaled to keep the animation speed; the kernel only advances frames from
  11. * the poll loop, so 16 ms frames would just be skipped anyway. */
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include "cursor_abi.h"
  17. #define XCUR_IMAGE 0xFFFD0002u
  18. #define MIN_DELAY_MS 60
  19. #define MAX_FRAMES 64
  20. struct xframe {
  21. uint32_t w, h, xhot, yhot, delay;
  22. const uint32_t *px;
  23. };
  24. static uint32_t rd32(const unsigned char *p)
  25. {
  26. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) |
  27. ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
  28. }
  29. static unsigned char *read_file(const char *path, size_t *len)
  30. {
  31. FILE *f = fopen(path, "rb");
  32. unsigned char *buf;
  33. long n;
  34. if (!f)
  35. return NULL;
  36. fseek(f, 0, SEEK_END);
  37. n = ftell(f);
  38. fseek(f, 0, SEEK_SET);
  39. buf = malloc(n > 0 ? (size_t)n : 1);
  40. if (!buf || fread(buf, 1, (size_t)n, f) != (size_t)n) {
  41. fclose(f);
  42. free(buf);
  43. return NULL;
  44. }
  45. fclose(f);
  46. *len = (size_t)n;
  47. return buf;
  48. }
  49. /* Collect the image chunks of `size`; returns the frame count or -1. */
  50. static int parse_xcursor(const unsigned char *d, size_t len, unsigned int size,
  51. struct xframe *out, int max)
  52. {
  53. uint32_t hdr, ntoc, i;
  54. int n = 0;
  55. if (len < 16 || memcmp(d, "Xcur", 4) != 0)
  56. return -1;
  57. hdr = rd32(d + 4);
  58. ntoc = rd32(d + 12);
  59. if ((size_t)hdr + (size_t)ntoc * 12 > len)
  60. return -1;
  61. for (i = 0; i < ntoc; i++) {
  62. const unsigned char *t = d + hdr + i * 12;
  63. uint32_t pos = rd32(t + 8);
  64. const unsigned char *c;
  65. struct xframe fr;
  66. if (rd32(t) != XCUR_IMAGE || rd32(t + 4) != size)
  67. continue;
  68. if ((size_t)pos + 36 > len)
  69. return -1;
  70. c = d + pos;
  71. fr.w = rd32(c + 16);
  72. fr.h = rd32(c + 20);
  73. fr.xhot = rd32(c + 24);
  74. fr.yhot = rd32(c + 28);
  75. fr.delay = rd32(c + 32);
  76. if (!fr.w || !fr.h || fr.w > CURSOR_MAX_DIM ||
  77. fr.h > CURSOR_MAX_DIM || fr.xhot >= fr.w || fr.yhot >= fr.h)
  78. return -1;
  79. if ((size_t)pos + rd32(c) + (size_t)fr.w * fr.h * 4 > len)
  80. return -1;
  81. fr.px = (const uint32_t *)(c + rd32(c));
  82. if (n >= max)
  83. return -1;
  84. out[n++] = fr;
  85. }
  86. return n ? n : -1;
  87. }
  88. #define CURSOR_FILE_(e, css, file) file,
  89. static const char *const theme_files[CURSOR_COUNT] = {
  90. CURSOR_LIST(CURSOR_FILE_)
  91. };
  92. #undef CURSOR_FILE_
  93. int main(int argc, char **argv)
  94. {
  95. struct cursor_file_hdr hdr;
  96. struct cursor_file_shape shapes[CURSOR_COUNT];
  97. unsigned char *files[CURSOR_COUNT];
  98. struct xframe frames[CURSOR_COUNT][MAX_FRAMES];
  99. int nframes[CURSOR_COUNT], step[CURSOR_COUNT];
  100. unsigned int size, off;
  101. FILE *out;
  102. int i, j;
  103. if (argc != 4) {
  104. fprintf(stderr, "usage: %s <theme-dir> <size> <out.cur>\n",
  105. argv[0]);
  106. return 2;
  107. }
  108. size = (unsigned int)atoi(argv[2]);
  109. off = sizeof(hdr) + sizeof(shapes);
  110. for (i = 0; i < CURSOR_COUNT; i++) {
  111. char path[1024];
  112. size_t len;
  113. int n, kept;
  114. snprintf(path, sizeof(path), "%s/%s", argv[1], theme_files[i]);
  115. files[i] = read_file(path, &len);
  116. if (!files[i]) {
  117. fprintf(stderr, "mkcursors: cannot read %s\n", path);
  118. return 1;
  119. }
  120. n = parse_xcursor(files[i], len, size, frames[i], MAX_FRAMES);
  121. if (n < 0) {
  122. fprintf(stderr, "mkcursors: %s: no valid %u px image\n",
  123. path, size);
  124. return 1;
  125. }
  126. step[i] = 1;
  127. if (n > 1 && frames[i][0].delay && frames[i][0].delay < MIN_DELAY_MS)
  128. step[i] = (MIN_DELAY_MS + frames[i][0].delay - 1) /
  129. frames[i][0].delay;
  130. kept = (n + step[i] - 1) / step[i];
  131. nframes[i] = n;
  132. shapes[i].w = (unsigned short)frames[i][0].w;
  133. shapes[i].h = (unsigned short)frames[i][0].h;
  134. shapes[i].hot_x = (unsigned short)frames[i][0].xhot;
  135. shapes[i].hot_y = (unsigned short)frames[i][0].yhot;
  136. shapes[i].frames = (unsigned short)kept;
  137. shapes[i].delay_ms = (unsigned short)(n > 1 ?
  138. frames[i][0].delay * (unsigned int)step[i] : 0);
  139. shapes[i].offset = off;
  140. for (j = 0; j < n; j += step[i])
  141. if (frames[i][j].w != frames[i][0].w ||
  142. frames[i][j].h != frames[i][0].h) {
  143. fprintf(stderr, "mkcursors: %s: frame sizes differ\n",
  144. path);
  145. return 1;
  146. }
  147. off += (unsigned int)kept * shapes[i].w * shapes[i].h * 4;
  148. }
  149. memcpy(hdr.magic, CURSOR_FILE_MAGIC, 8);
  150. hdr.count = CURSOR_COUNT;
  151. hdr.nominal = size;
  152. out = fopen(argv[3], "wb");
  153. if (!out) {
  154. perror(argv[3]);
  155. return 1;
  156. }
  157. fwrite(&hdr, sizeof(hdr), 1, out);
  158. fwrite(shapes, sizeof(shapes), 1, out);
  159. for (i = 0; i < CURSOR_COUNT; i++)
  160. for (j = 0; j < nframes[i]; j += step[i])
  161. fwrite(frames[i][j].px, 4,
  162. (size_t)shapes[i].w * shapes[i].h, out);
  163. if (fclose(out) != 0) {
  164. perror(argv[3]);
  165. return 1;
  166. }
  167. for (i = 0; i < CURSOR_COUNT; i++)
  168. free(files[i]);
  169. return 0;
  170. }