| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- /* Mouse-cursor images.
- *
- * The cursor set is the Adwaita theme (assets/cursors/adwaita/), converted
- * by tools/mkcursors.c into one NBCUR1 bundle that the rootfs pack carries
- * as cursors/adwaita24.cur. The kernel cannot keep a pointer into the pack
- * (ramfs_replace() frees the old one), so cursor_load() copies the bundle
- * into its own pages. Until then -- and on a pack without the bundle --
- * the built-in 16x16 arrow from cursor_shape.h is used. */
- #include "cursor.h"
- #include "cursor_shape.h"
- #include "mm.h"
- #include "mouse.h"
- #include "ramfs.h"
- #include "serial.h"
- #define CURSOR_PACK_PATH "cursors/adwaita24.cur"
- static const unsigned char *bundle;
- static unsigned int bundle_pages;
- static unsigned int fallback_px[CURSOR_SHAPE_WIDTH * CURSOR_SHAPE_HEIGHT];
- static int fallback_ready;
- static void fallback_build(void)
- {
- unsigned int r, c;
- for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++)
- for (c = 0; c < CURSOR_SHAPE_WIDTH; c++) {
- unsigned short bit = (unsigned short)(0x8000u >> c);
- unsigned int v = 0;
- if (cursor_shape_fill[r] & bit)
- v = 0xFFFFFFFFu;
- else if (cursor_shape_outline[r] & bit)
- v = 0xFF000000u;
- fallback_px[r * CURSOR_SHAPE_WIDTH + c] = v;
- }
- fallback_ready = 1;
- }
- /* Bounds-check the whole bundle once, so cursor_get() can trust it. */
- static int bundle_valid(const unsigned char *p, unsigned int size)
- {
- const struct cursor_file_hdr *h = (const struct cursor_file_hdr *)p;
- const struct cursor_file_shape *s;
- const char *magic = CURSOR_FILE_MAGIC;
- unsigned int i;
- if (size < sizeof(*h))
- return 0;
- for (i = 0; i < 8; i++)
- if (h->magic[i] != magic[i])
- return 0;
- if (h->count == 0 || h->count > 256 ||
- sizeof(*h) + h->count * sizeof(*s) > size)
- return 0;
- s = (const struct cursor_file_shape *)(p + sizeof(*h));
- for (i = 0; i < h->count; i++) {
- unsigned int bytes;
- if (!s[i].w || !s[i].h || s[i].w > CURSOR_MAX_DIM ||
- s[i].h > CURSOR_MAX_DIM || s[i].hot_x >= s[i].w ||
- s[i].hot_y >= s[i].h || !s[i].frames ||
- s[i].frames > 256 || (s[i].offset & 3))
- return 0;
- bytes = (unsigned int)s[i].w * s[i].h * 4u * s[i].frames;
- if (s[i].offset > size || bytes > size - s[i].offset)
- return 0;
- }
- return 1;
- }
- void cursor_load(void)
- {
- unsigned int size = 0, pages, i;
- const unsigned char *src =
- (const unsigned char *)ramfs_lookup(CURSOR_PACK_PATH, &size);
- unsigned char *copy;
- const unsigned char *old = bundle;
- unsigned int old_pages = bundle_pages;
- if (!src || !bundle_valid(src, size)) {
- serial_puts(SERIAL_COM1, "cursor: no valid " CURSOR_PACK_PATH
- ", keeping the current cursors\n");
- return;
- }
- pages = (size + 0xFFFu) >> 12;
- copy = (unsigned char *)mm_alloc_pages(pages);
- if (!copy) {
- serial_puts(SERIAL_COM1, "cursor: out of memory\n");
- return;
- }
- for (i = 0; i < size; i++)
- copy[i] = src[i];
- bundle = copy;
- bundle_pages = pages;
- /* The mouse may still point into the old copy: let it switch before
- * the pages go back to the allocator. */
- mouse_cursor_reload();
- for (i = 0; i < old_pages; i++)
- mm_free_page((unsigned int)old + i * 0x1000u);
- serial_puts(SERIAL_COM1, "cursor: loaded ");
- serial_putdec(SERIAL_COM1,
- ((const struct cursor_file_hdr *)bundle)->count);
- serial_puts(SERIAL_COM1, " shapes\n");
- }
- int cursor_bundle_loaded(void)
- {
- return bundle != 0;
- }
- void cursor_get(int shape, unsigned int frame, struct cursor_image *out)
- {
- if (bundle) {
- const struct cursor_file_hdr *h =
- (const struct cursor_file_hdr *)bundle;
- const struct cursor_file_shape *s =
- (const struct cursor_file_shape *)(bundle + sizeof(*h));
- if (shape < 0 || (unsigned int)shape >= h->count)
- shape = CURSOR_DEFAULT;
- s += shape;
- frame %= s->frames;
- out->w = s->w;
- out->h = s->h;
- out->hot_x = s->hot_x;
- out->hot_y = s->hot_y;
- out->frames = s->frames;
- out->delay_ms = s->delay_ms ? s->delay_ms : 100;
- out->px = (const unsigned int *)(bundle + s->offset) +
- frame * s->w * s->h;
- return;
- }
- if (!fallback_ready)
- fallback_build();
- out->px = fallback_px;
- out->w = CURSOR_SHAPE_WIDTH;
- out->h = CURSOR_SHAPE_HEIGHT;
- out->hot_x = 0;
- out->hot_y = 0;
- out->frames = 1;
- out->delay_ms = 0;
- }
|