cursor.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Mouse-cursor images.
  2. *
  3. * The cursor set is the Adwaita theme (assets/cursors/adwaita/), converted
  4. * by tools/mkcursors.c into one NBCUR1 bundle that the rootfs pack carries
  5. * as cursors/adwaita24.cur. The kernel cannot keep a pointer into the pack
  6. * (ramfs_replace() frees the old one), so cursor_load() copies the bundle
  7. * into its own pages. Until then -- and on a pack without the bundle --
  8. * the built-in 16x16 arrow from cursor_shape.h is used. */
  9. #include "cursor.h"
  10. #include "cursor_shape.h"
  11. #include "mm.h"
  12. #include "mouse.h"
  13. #include "ramfs.h"
  14. #include "serial.h"
  15. #define CURSOR_PACK_PATH "cursors/adwaita24.cur"
  16. static const unsigned char *bundle;
  17. static unsigned int bundle_pages;
  18. static unsigned int fallback_px[CURSOR_SHAPE_WIDTH * CURSOR_SHAPE_HEIGHT];
  19. static int fallback_ready;
  20. static void fallback_build(void)
  21. {
  22. unsigned int r, c;
  23. for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++)
  24. for (c = 0; c < CURSOR_SHAPE_WIDTH; c++) {
  25. unsigned short bit = (unsigned short)(0x8000u >> c);
  26. unsigned int v = 0;
  27. if (cursor_shape_fill[r] & bit)
  28. v = 0xFFFFFFFFu;
  29. else if (cursor_shape_outline[r] & bit)
  30. v = 0xFF000000u;
  31. fallback_px[r * CURSOR_SHAPE_WIDTH + c] = v;
  32. }
  33. fallback_ready = 1;
  34. }
  35. /* Bounds-check the whole bundle once, so cursor_get() can trust it. */
  36. static int bundle_valid(const unsigned char *p, unsigned int size)
  37. {
  38. const struct cursor_file_hdr *h = (const struct cursor_file_hdr *)p;
  39. const struct cursor_file_shape *s;
  40. const char *magic = CURSOR_FILE_MAGIC;
  41. unsigned int i;
  42. if (size < sizeof(*h))
  43. return 0;
  44. for (i = 0; i < 8; i++)
  45. if (h->magic[i] != magic[i])
  46. return 0;
  47. if (h->count == 0 || h->count > 256 ||
  48. sizeof(*h) + h->count * sizeof(*s) > size)
  49. return 0;
  50. s = (const struct cursor_file_shape *)(p + sizeof(*h));
  51. for (i = 0; i < h->count; i++) {
  52. unsigned int bytes;
  53. if (!s[i].w || !s[i].h || s[i].w > CURSOR_MAX_DIM ||
  54. s[i].h > CURSOR_MAX_DIM || s[i].hot_x >= s[i].w ||
  55. s[i].hot_y >= s[i].h || !s[i].frames ||
  56. s[i].frames > 256 || (s[i].offset & 3))
  57. return 0;
  58. bytes = (unsigned int)s[i].w * s[i].h * 4u * s[i].frames;
  59. if (s[i].offset > size || bytes > size - s[i].offset)
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. void cursor_load(void)
  65. {
  66. unsigned int size = 0, pages, i;
  67. const unsigned char *src =
  68. (const unsigned char *)ramfs_lookup(CURSOR_PACK_PATH, &size);
  69. unsigned char *copy;
  70. const unsigned char *old = bundle;
  71. unsigned int old_pages = bundle_pages;
  72. if (!src || !bundle_valid(src, size)) {
  73. serial_puts(SERIAL_COM1, "cursor: no valid " CURSOR_PACK_PATH
  74. ", keeping the current cursors\n");
  75. return;
  76. }
  77. pages = (size + 0xFFFu) >> 12;
  78. copy = (unsigned char *)mm_alloc_pages(pages);
  79. if (!copy) {
  80. serial_puts(SERIAL_COM1, "cursor: out of memory\n");
  81. return;
  82. }
  83. for (i = 0; i < size; i++)
  84. copy[i] = src[i];
  85. bundle = copy;
  86. bundle_pages = pages;
  87. /* The mouse may still point into the old copy: let it switch before
  88. * the pages go back to the allocator. */
  89. mouse_cursor_reload();
  90. for (i = 0; i < old_pages; i++)
  91. mm_free_page((unsigned int)old + i * 0x1000u);
  92. serial_puts(SERIAL_COM1, "cursor: loaded ");
  93. serial_putdec(SERIAL_COM1,
  94. ((const struct cursor_file_hdr *)bundle)->count);
  95. serial_puts(SERIAL_COM1, " shapes\n");
  96. }
  97. int cursor_bundle_loaded(void)
  98. {
  99. return bundle != 0;
  100. }
  101. void cursor_get(int shape, unsigned int frame, struct cursor_image *out)
  102. {
  103. if (bundle) {
  104. const struct cursor_file_hdr *h =
  105. (const struct cursor_file_hdr *)bundle;
  106. const struct cursor_file_shape *s =
  107. (const struct cursor_file_shape *)(bundle + sizeof(*h));
  108. if (shape < 0 || (unsigned int)shape >= h->count)
  109. shape = CURSOR_DEFAULT;
  110. s += shape;
  111. frame %= s->frames;
  112. out->w = s->w;
  113. out->h = s->h;
  114. out->hot_x = s->hot_x;
  115. out->hot_y = s->hot_y;
  116. out->frames = s->frames;
  117. out->delay_ms = s->delay_ms ? s->delay_ms : 100;
  118. out->px = (const unsigned int *)(bundle + s->offset) +
  119. frame * s->w * s->h;
  120. return;
  121. }
  122. if (!fallback_ready)
  123. fallback_build();
  124. out->px = fallback_px;
  125. out->w = CURSOR_SHAPE_WIDTH;
  126. out->h = CURSOR_SHAPE_HEIGHT;
  127. out->hot_x = 0;
  128. out->hot_y = 0;
  129. out->frames = 1;
  130. out->delay_ms = 0;
  131. }