Просмотр исходного кода

Use the Adwaita cursor set instead of the single arrow

NebOS had one 16x16 two-colour arrow.  It now has the 35 CSS cursor
shapes from GNOME's Adwaita theme (adwaita-cursors 51, LGPL-3 /
CC-BY-SA-3.0): the arrow, hand, I-beam, the resize arrows, move, help,
not-allowed, zoom, the animated wait/progress spinners and the rest.

This system ships no SVG cursor theme, only Xcursor bitmaps, so the
assets are Adwaita's Xcursor files cut down to their 24 px images
(assets/cursors/adwaita/, 424 KB instead of 12 MB, by
tools/xcursor_trim.py).  The host tool mkcursors turns them into one
NBCUR1 bundle (include/cursor_abi.h: size, hotspot, frames, premultiplied
ARGB), packed as cursors/adwaita24.cur in the full pack only (~18 KB
gzip; the floppy has no room and keeps the built-in arrow).  The
spinners keep every fourth of their 60 x 16 ms frames.

kernel/cursor.c copies the bundle out of the ramfs at boot and after a
runtime pack replacement.  mouse.c treats the position as the hotspot,
clips the image at the screen edges and blends it over the saved
background (up to 32x32); Intel Ironlake and Radeon load the ARGB image
into their 64x64 planes, Cirrus reduces it to its two colours.

SYS_WIN_SET_CURSOR (117) sets a window's content cursor and
SYS_WM_SET_CURSOR (118) is the WM's override.  The window server
resolves the shape on every WM poll: the override, else the window
holding the pointer capture, else the window under the pointer, else
the arrow.  The WM uses resize arrows on the grip, the move cross while
dragging a title bar and a hand over launchers and the status area.
ngui asks for the I-beam over text fields, the terminal uses it for its
cells, the browser honours CSS `cursor` (a hand over links, the I-beam
over text fields and the progress cursor while loading for `auto`), and
the gallery shows every shape.  browser-host maps them to SDL system
cursors.

make test-cursors checks the bundle.  Verified in QEMU with the
software and Cirrus cursors; the Intel/Radeon paths are untested.
Johannes Zellner 3 дней назад
Родитель
Сommit
c5f21a2ab9
80 измененных файлов с 1754 добавлено и 177 удалено
  1. 3 0
      .gitignore
  2. 75 14
      AGENTS.md
  3. 30 7
      Makefile
  4. 10 0
      assets/cursors/adwaita/COPYING
  5. 7 0
      assets/cursors/adwaita/COPYING_CCBYSA3
  6. 165 0
      assets/cursors/adwaita/COPYING_LGPL
  7. 19 0
      assets/cursors/adwaita/README
  8. BIN
      assets/cursors/adwaita/alias
  9. BIN
      assets/cursors/adwaita/all-resize
  10. BIN
      assets/cursors/adwaita/all-scroll
  11. BIN
      assets/cursors/adwaita/cell
  12. BIN
      assets/cursors/adwaita/col-resize
  13. BIN
      assets/cursors/adwaita/context-menu
  14. BIN
      assets/cursors/adwaita/copy
  15. BIN
      assets/cursors/adwaita/crosshair
  16. BIN
      assets/cursors/adwaita/default
  17. BIN
      assets/cursors/adwaita/e-resize
  18. BIN
      assets/cursors/adwaita/ew-resize
  19. BIN
      assets/cursors/adwaita/grab
  20. BIN
      assets/cursors/adwaita/grabbing
  21. BIN
      assets/cursors/adwaita/help
  22. BIN
      assets/cursors/adwaita/n-resize
  23. BIN
      assets/cursors/adwaita/ne-resize
  24. BIN
      assets/cursors/adwaita/nesw-resize
  25. BIN
      assets/cursors/adwaita/no-drop
  26. BIN
      assets/cursors/adwaita/not-allowed
  27. BIN
      assets/cursors/adwaita/ns-resize
  28. BIN
      assets/cursors/adwaita/nw-resize
  29. BIN
      assets/cursors/adwaita/nwse-resize
  30. BIN
      assets/cursors/adwaita/pointer
  31. BIN
      assets/cursors/adwaita/progress
  32. BIN
      assets/cursors/adwaita/row-resize
  33. BIN
      assets/cursors/adwaita/s-resize
  34. BIN
      assets/cursors/adwaita/se-resize
  35. BIN
      assets/cursors/adwaita/sw-resize
  36. BIN
      assets/cursors/adwaita/text
  37. BIN
      assets/cursors/adwaita/vertical-text
  38. BIN
      assets/cursors/adwaita/w-resize
  39. BIN
      assets/cursors/adwaita/wait
  40. BIN
      assets/cursors/adwaita/zoom-in
  41. BIN
      assets/cursors/adwaita/zoom-out
  42. 1 0
      docs/development.html
  43. 29 0
      docs/graphics.html
  44. 1 1
      docs/index.html
  45. 1 0
      docs/userland.html
  46. 4 0
      host/syscall.c
  47. 9 0
      host/window.c
  48. 3 0
      host/window.h
  49. 47 0
      host/window_sdl3.c
  50. 87 0
      include/cursor_abi.h
  51. 4 0
      include/syscall.h
  52. 44 28
      kernel/cirrus.c
  53. 5 2
      kernel/cirrus.h
  54. 145 0
      kernel/cursor.c
  55. 29 0
      kernel/cursor.h
  56. 48 27
      kernel/intel.c
  57. 11 5
      kernel/intel.h
  58. 2 0
      kernel/kernel.c
  59. 219 50
      kernel/mouse.c
  60. 8 0
      kernel/mouse.h
  61. 49 35
      kernel/radeon.c
  62. 6 3
      kernel/radeon.h
  63. 3 0
      kernel/ramfs.c
  64. 10 0
      kernel/sys_wm.c
  65. 2 0
      kernel/sys_wm.h
  66. 4 0
      kernel/syscall.c
  67. 61 0
      kernel/wm.c
  68. 5 0
      kernel/wm.h
  69. 104 0
      tools/cursor_test.c
  70. 184 0
      tools/mkcursors.c
  71. 67 0
      tools/xcursor_trim.py
  72. 39 0
      user/apps/browser/browser.c
  73. 45 1
      user/apps/browser/browser_html.c
  74. 6 0
      user/apps/browser/browser_html.h
  75. 53 4
      user/apps/gallery/gallery.c
  76. 2 0
      user/apps/term/ucmd_term.c
  77. 54 0
      user/apps/wm/ucmd_wm.c
  78. 26 0
      user/lib/ngui/ngui.c
  79. 13 0
      user/lib/ngui/ngui.h
  80. 15 0
      user/ucmd_lib.h

+ 3 - 0
.gitignore

@@ -36,5 +36,8 @@ host/build/
 browser-host
 browser-host
 host-curl
 host-curl
 firmware/
 firmware/
+/mkcursors
+/cursors/
 /mkicons
 /mkicons
 /icons/
 /icons/
+/cursor_test

+ 75 - 14
AGENTS.md

@@ -16,8 +16,8 @@ Targets i586 (first-gen Pentium); runs under QEMU or on real hardware.
 | `user/` | `ucmd_lib.h` (the ring-3 syscall wrappers) and `ucmd.ld` |
 | `user/` | `ucmd_lib.h` (the ring-3 syscall wrappers) and `ucmd.ld` |
 | `user/apps/<name>/` | One ring-3 program per directory: entry file `ucmd_<name>.c` plus private modules (`browser/` holds `browser.c`, `browser_html.c`, `js_*.c`, `svg_raster.c`; `chat/` holds `chat_login.c`; `gallery/` holds the real `gallery.c` behind the `ucmd_gallery.c` launcher; `hello/` has its own `hello.ld`) |
 | `user/apps/<name>/` | One ring-3 program per directory: entry file `ucmd_<name>.c` plus private modules (`browser/` holds `browser.c`, `browser_html.c`, `js_*.c`, `svg_raster.c`; `chat/` holds `chat_login.c`; `gallery/` holds the real `gallery.c` behind the `ucmd_gallery.c` launcher; `hello/` has its own `hello.ld`) |
 | `user/lib/<lib>/` | Ring-3 libraries: `libc`, `ngui`, `ufont`, `img`, `tls` (crypto incl. `wpa_crypto.c`), `uhttp`, `ujson`, `uwm` (`uwm_lib.c`), `wifi` (`wifi_store.c`, `wpa.c`); host tests live next to their library |
 | `user/lib/<lib>/` | Ring-3 libraries: `libc`, `ngui`, `ufont`, `img`, `tls` (crypto incl. `wpa_crypto.c`), `uhttp`, `ujson`, `uwm` (`uwm_lib.c`), `wifi` (`wifi_store.c`, `wpa.c`); host tests live next to their library |
-| `tools/` | Host tools (`mkpack.c`, `mkrootfs.c`, `mkrtwtbl.c`, `mkicons.c`, `kexec_send.c`, `netkexec_send.c`, `netmonitor.c`, `flash.sh`) and the QMP test scripts |
-| `assets/` | Imported assets: `assets/icons/adwaita/` (the Adwaita symbolic SVGs used as UI icons, LGPL-3 / CC-BY-SA-3.0, with README and licences) |
+| `tools/` | Host tools (`mkpack.c`, `mkrootfs.c`, `mkrtwtbl.c`, `mkcursors.c`, `mkicons.c`, `kexec_send.c`, `netkexec_send.c`, `netmonitor.c`, `flash.sh`, `xcursor_trim.py`), the QMP test scripts and `cursor_test.c` |
+| `assets/` | Imported assets: `assets/cursors/adwaita/` (the Adwaita 24 px Xcursor files) and `assets/icons/adwaita/` (the Adwaita symbolic SVGs used as UI icons), both LGPL-3 / CC-BY-SA-3.0, with README and licences |
 | `host/` | Linux SDL3 browser harness |
 | `host/` | Linux SDL3 browser harness |
 
 
 The top-level `Makefile` builds everything.  Objects go to `build/`
 The top-level `Makefile` builds everything.  Objects go to `build/`
@@ -117,6 +117,7 @@ make test-font  # host TrueType parser/rasterizer specimen test
 make test-font-asan  # ASan/UBSan variant
 make test-font-asan  # ASan/UBSan variant
 make test-svg   # host tests for the inline-SVG rasterizer (ASan/UBSan)
 make test-svg   # host tests for the inline-SVG rasterizer (ASan/UBSan)
 make test-touchpad  # host tests for the contact gesture engine
 make test-touchpad  # host tests for the contact gesture engine
+make test-cursors   # host test for the NBCUR1 cursor bundle (mkcursors output)
 make test-wpa   # host tests for the WPA2-PSK supplicant (SHA-1/PBKDF2/PRF/AES, 4-way)
 make test-wpa   # host tests for the WPA2-PSK supplicant (SHA-1/PBKDF2/PRF/AES, 4-way)
 make test-wifi-store  # host tests for the saved-network list file
 make test-wifi-store  # host tests for the saved-network list file
 make test-chat  # host tests: JSON parser, cookie jar, chat login parsers (ASan)
 make test-chat  # host tests: JSON parser, cookie jar, chat login parsers (ASan)
@@ -146,10 +147,10 @@ make clean
   to `cmds_pack_gz.bin`, wrapped by `mkrootfs` with a one-sector `NBROOT1`
   to `cmds_pack_gz.bin`, wrapped by `mkrootfs` with a one-sector `NBROOT1`
   header into `cmds.img` (the 8 MB USB partition image) and `BOOTX64.EFI`.
   header into `cmds.img` (the 8 MB USB partition image) and `BOOTX64.EFI`.
   The floppy images append `rootfs.bin`, built the same way from
   The floppy images append `rootfs.bin`, built the same way from
-  `cmds-lite.pack` (everything but `firmware/*`, `install/kernel.bin` and
-  `bin/browser.app`), because the ~1 MB WiFi firmware and the ~500 KB gzip
-  browser do not fit a 1.44 MB floppy; a floppy boot runs the browser from
-  `BROWSER` on `root.img`.  The kernel itself no longer
+  `cmds-lite.pack` (everything but `firmware/*`, the cursor bundle,
+  `install/kernel.bin` and `bin/browser.app`), because the ~1 MB WiFi
+  firmware and the ~500 KB gzip browser do not fit a 1.44 MB floppy; a
+  floppy boot runs the browser from `BROWSER` on `root.img`.  The kernel itself no longer
   carries the pack; the boot path loads it into RAM and `ramfs_init` takes a
   carries the pack; the boot path loads it into RAM and `ramfs_init` takes a
   private copy (`bin/<name>` mounted at `/ramfs`).
   private copy (`bin/<name>` mounted at `/ramfs`).
 - The WiFi firmware `firmware/iwlwifi-7265D-29.ucode` (Chromebook) and
 - The WiFi firmware `firmware/iwlwifi-7265D-29.ucode` (Chromebook) and
@@ -478,6 +479,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   input.  `SYS_WIN_SCROLL` returns scroll accumulated for the canvas window
   input.  `SYS_WIN_SCROLL` returns scroll accumulated for the canvas window
   under the pointer, in pixels (`+dy` = later content), so every client sees
   under the pointer, in pixels (`+dy` = later content), so every client sees
   one unit independent of whether it came from the touchpad or a wheel.
   one unit independent of whether it came from the touchpad or a wheel.
+  `SYS_WIN_SET_CURSOR` (117) sets the caller's content cursor and
+  `SYS_WM_SET_CURSOR` (118) the WM's override (see the Mouse cursors bullet).
   `SYS_WIN_RESIZE_SELF` (115) lets a canvas window grow or shrink itself and
   `SYS_WIN_RESIZE_SELF` (115) lets a canvas window grow or shrink itself and
   `SYS_SCREEN_INFO` (116) returns the framebuffer dimensions; the panel uses
   `SYS_SCREEN_INFO` (116) returns the framebuffer dimensions; the panel uses
   them to collapse to the pills or grow over an expanded section.  The kernel
   them to collapse to the pills or grow over an expanded section.  The kernel
@@ -1010,6 +1013,38 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `wifi.c` reloads on the next use.  Tested by QEMU `usb-host` passthrough
   `wifi.c` reloads on the next use.  Tested by QEMU `usb-host` passthrough
   on xHCI (C-cut chip, firmware 42.4): 2.4 + 5 GHz scan, WPA2-PSK connect
   on xHCI (C-cut chip, firmware 42.4): 2.4 + 5 GHz scan, WPA2-PSK connect
   and DHCP through the control panel.
   and DHCP through the control panel.
+- **Mouse cursors**: the set is GNOME's Adwaita theme (adwaita-cursors 51,
+  LGPL-3 or CC-BY-SA-3.0).  `assets/cursors/adwaita/` holds its Xcursor
+  files trimmed to the 24 px images (`tools/xcursor_trim.py`; the X11 alias
+  symlinks and `X_cursor` are not copied); `tools/mkcursors.c` converts them
+  into one `NBCUR1` bundle (`include/cursor_abi.h`: header, per-shape
+  w/h/hotspot/frames/delay/offset, premultiplied ARGB8888 frames), packed as
+  `cursors/adwaita24.cur` in the **full pack only** (~146 KB, ~18 KB gzip;
+  the floppy lite pack had ~2 KB of room left, so a floppy boot keeps the
+  built-in 16x16 arrow of `cursor_shape.h`).  `CURSOR_LIST` (an X-macro in
+  `cursor_abi.h`) is the shape id order, the bundle order and the CSS name
+  table: 35 CSS cursor names (`move` maps to Adwaita's `all-resize`, since
+  its `move` is a symlink to the arrow).  `wait`/`progress` keep every
+  fourth of Adwaita's 60 x 16 ms frames (15 x 64 ms).  `cursor.c/h` copies
+  the bundle out of the ramfs into its own pages after `ramfs_init()` and in
+  `ramfs_replace()` (which frees the old pack), calls `mouse_cursor_reload()`
+  and only then frees the previous copy.  `wm.c` resolves the shape in every
+  `SYS_WM_POLL`: the WM override (`SYS_WM_SET_CURSOR`), else the cursor of
+  the window holding the pointer capture, else the content cursor
+  (`SYS_WIN_SET_CURSOR`, `struct window.cursor`) of the window under the
+  pointer (not on a normal window's 16 px grip), else `CURSOR_DEFAULT`; a WM
+  restart clears the override.  The ring-3 WM overrides with resize arrows
+  on the grip and while resizing, the move cross during a title-bar drag
+  (once it moved), and a hand over launchers and the status area.  ngui
+  resets `ui->cursor` to the arrow for every redrawn frame (begin returned
+  1), `ngui_text_input` asks for the I-beam (not-allowed when disabled),
+  apps use `ngui_cursor_rect()`/`ngui_set_cursor()`, and `ngui_end_ex()`
+  sends it when it changed.  The terminal sets the I-beam once; the browser
+  parses the inherited CSS `cursor` (the keyword after the last comma;
+  `br_style.cursor` is shape + 1, 0 = auto) and for `auto` uses a hand over
+  links, the I-beam over text fields and `progress` while loading; the
+  gallery's Cursors section shows every shape; `browser-host` maps the
+  shapes to SDL system cursors.
 - **UI icons**: ngui's icons are GNOME Adwaita symbolic icons
 - **UI icons**: ngui's icons are GNOME Adwaita symbolic icons
   (adwaita-icon-theme 51, LGPL-3 or CC-BY-SA-3.0).  `assets/icons/adwaita/`
   (adwaita-icon-theme 51, LGPL-3 or CC-BY-SA-3.0).  `assets/icons/adwaita/`
   holds the unchanged `*-symbolic.svg` files; `ICON_LIST` (an X-macro in
   holds the unchanged `*-symbolic.svg` files; `ICON_LIST` (an X-macro in
@@ -1033,8 +1068,20 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   `browser-host` symlinks the repository's `icons/` into its `/ramfs` like
   `browser-host` symlinks the repository's `icons/` into its `/ramfs` like
   `fonts/`.
   `fonts/`.
 - **Mouse**: `mouse.c/h` — cursor move/draw shared by the PS/2 path
 - **Mouse**: `mouse.c/h` — cursor move/draw shared by the PS/2 path
-  (IntelliMouse including the 4th-byte wheel, Cirrus/Radeon hardware
-  cursor) and the USB backend.  Scroll from every backend accumulates in
+  (IntelliMouse including the 4th-byte wheel, Cirrus/Radeon/Intel hardware
+  cursor) and the USB backend.  `cur_x/cur_y` are the hotspot; the image is
+  drawn with its top-left at hotspot minus the image hotspot, so it can be
+  partly off the top/left edge.  The software cursor saves up to 32x32
+  pixels (`CURSOR_MAX_DIM`) and blends the premultiplied ARGB over them
+  (decoding RGB565 or BGR from the saved background); a shape change
+  presents the union of the old and new rectangles at once.  Intel
+  Ironlake and Radeon copy the image into their 64x64 ARGB planes (Intel's
+  CURPOS takes sign-magnitude negatives; Radeon clamps the position at 0
+  and skips the hidden part with `HORZ_VERT_OFF` and a `CUR_OFFSET` shifted
+  by 256 bytes per row, like Linux's legacy path); Cirrus thresholds alpha
+  at 50% into its AND/XOR planes (white/black by luminance) and clamps the
+  position at 0.  An image upload never runs from IRQ12, which only moves.
+  Animated shapes advance from `mouse_poll_update()`.  Scroll from every backend accumulates in
   `mouse_scroll_y` and `mouse_scroll_take()` hands it to the WM in pixels;
   `mouse_scroll_y` and `mouse_scroll_take()` hands it to the WM in pixels;
   one PS/2 wheel notch is 48 px.  `usbhid.c/h` — USB HID boot devices: find
   one PS/2 wheel notch is 48 px.  `usbhid.c/h` — USB HID boot devices: find
   + configure a mouse and/or keyboard, arm their interrupt endpoints, decode
   + configure a mouse and/or keyboard, arm their interrupt endpoints, decode
@@ -1946,11 +1993,11 @@ inflates it into page-allocated memory and mounts it read-only at `/ramfs`.
 The kernel image no longer contains it, so the pack no longer moves `_end`;
 The kernel image no longer contains it, so the pack no longer moves `_end`;
 the high-memory legacy path supports up to 2 MB (known issue 8).  The UI
 the high-memory legacy path supports up to 2 MB (known issue 8).  The UI
 font subsets live in the pack under `fonts/`, the WiFi firmware under
 font subsets live in the pack under `fonts/`, the WiFi firmware under
-`firmware/`, the UI icons at `icons/adwaita16.icn` (both packs) and the
-flat kernel at `install/kernel.bin` (full pack only;
+`firmware/`, the mouse cursors at `cursors/adwaita24.cur` (full pack only),
+the UI icons at `icons/adwaita16.icn` (both packs) and the flat kernel at `install/kernel.bin` (full pack only;
 `install self` reads it).  The floppy images use `cmds-lite.pack` (no
 `install self` reads it).  The floppy images use `cmds-lite.pack` (no
-firmware, `install/kernel.bin` or `bin/browser.app`, to stay inside
-1.44 MB; the browser launcher falls back to `BROWSER` on `root.img`).
+firmware, cursor bundle, `install/kernel.bin` or `bin/browser.app`, to stay
+inside 1.44 MB; the browser launcher falls back to `BROWSER` on `root.img`).
 
 
 `rootfs.bin` is the floppy append (header + lite pack); `rootfs-full.bin`
 `rootfs.bin` is the floppy append (header + lite pack); `rootfs-full.bin`
 is the header + full gzip pack used by `make net-install`; `cmds.img` is the
 is the header + full gzip pack used by `make net-install`; `cmds.img` is the
@@ -2492,9 +2539,9 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
     (8 slots x 1 KB), and the FAT listing cache another 5 KB.  The command
     pack is out of the kernel now; after the WiFi/netmenu/install code and the
     pack is out of the kernel now; after the WiFi/netmenu/install code and the
     per-connection TCP queues moved to the frame allocator, `_end` is
     per-connection TCP queues moved to the frame allocator, `_end` is
-    ~0x8a440 (with the 6200/6300 DVM layer, the RTL8821AU driver, the
+    ~0x8eb20 (with the 6200/6300 DVM layer, the RTL8821AU driver, the
     8×16 boot-console font, the IOAPIC/AMD-thermal additions and the safe
     8×16 boot-console font, the IOAPIC/AMD-thermal additions and the safe
-    AMD MTRR path), ~23 KB below the `0x90000` wall, and the remaining limit
+    AMD MTRR path and the 32x32 cursor save buffer), ~5 KB below the `0x90000` wall, and the remaining limit
     is kernel code/buffers only.
     is kernel code/buffers only.
 
 
 22. Browser limits: JavaScript runs inline and external classic scripts on
 22. Browser limits: JavaScript runs inline and external classic scripts on
@@ -2884,3 +2931,17 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     for a few seconds on the first `wifi` command.  A plugged-in stick
     for a few seconds on the first `wifi` command.  A plugged-in stick
     always wins over the built-in Intel card; there is no command to pick
     always wins over the built-in Intel card; there is no command to pick
     the Intel card while it is attached.
     the Intel card while it is attached.
+
+39. Mouse cursor limits: one fixed 24 px theme (no HiDPI size selection or
+    user theme switch), no CSS `url()` cursors (the fallback keyword after
+    the last comma is used) and no `none`/hidden cursor (it maps to the
+    arrow).  Floppy boots have only the built-in 16x16 arrow, because the
+    lite pack has no room for the ~18 KB gzip bundle.  Cirrus is
+    two-colour, and its position cannot go negative, so near the top/left
+    edge the hotspot is off by up to the image's hotspot offset.  The
+    Intel/Radeon image upload (16 KB, plus `wbinvd` on Intel) also runs
+    for every spinner frame; both paths were written from the Linux
+    drivers and only Cirrus and the software cursor are tested in QEMU.
+    The cursor shape follows the WM poll, so it can lag the pointer by
+    one WM loop.  The 4 KB save buffer and the cursor code (~8 KB in all) leave `_end` at
+    ~0x8eb20, ~5 KB below the `0x90000` wall.

+ 30 - 7
Makefile

@@ -71,7 +71,7 @@ EFI_LDFLAGS = -nostdlib -Wl,--subsystem,10 -Wl,-e,efi_main \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--image-base,0x0 -Wl,--dynamicbase \
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
               -Wl,--enable-reloc-section -Wl,--no-insert-timestamp
 
 
-.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-rndis run-e1000 run-ahci run-amd test-amd test-usb test-install run-serial floppy usb uefi run-uefi test-uefi flash dev net-dev net-install cmds-dev net-cmds browser-dev gallery-dev compat-serve netmon monitor test-crypto test-wpa test-ccmp test-images test-images-asan test-libc test-js test-font test-font-asan test-svg test-touchpad test-chat test-chat-e2e test-wifi-store fonts kernel-dev kernel-net-dev rootfs nowm
+.PHONY: all clean run run-smp run-floppy run-usb run-uhci run-ehci run-ehci-asix run-xhci run-xhci-asix run-net run-rndis run-e1000 run-ahci run-amd test-amd test-usb test-install run-serial floppy usb uefi run-uefi test-uefi flash dev net-dev net-install cmds-dev net-cmds browser-dev gallery-dev compat-serve netmon monitor test-crypto test-wpa test-ccmp test-images test-images-asan test-libc test-js test-font test-font-asan test-svg test-touchpad test-chat test-chat-e2e test-wifi-store test-cursors fonts kernel-dev kernel-net-dev rootfs nowm
 
 
 all: floppy.img usb.img final.bin root.img image.bin
 all: floppy.img usb.img final.bin root.img image.bin
 
 
@@ -113,7 +113,7 @@ KERNEL_LINK_ORDER = vesa_stage entry kernel timer switch sched process mm \
 	terminal wm reboot poweroff disk fat serial serial_ring_buf serial_loop \
 	terminal wm reboot poweroff disk fat serial serial_ring_buf serial_loop \
 	vga blit cpu thermal it87 tpec ec cros_ec power_src power vga_copy \
 	vga blit cpu thermal it87 tpec ec cros_ec power_src power vga_copy \
 	gfxbench gpu backlight ati cirrus intel nvidia nv50 radeon mtrr \
 	gfxbench gpu backlight ati cirrus intel nvidia nv50 radeon mtrr \
-	font_8x8 font_8x16 fbtext gfx display mouse i2c touchpad \
+	font_8x8 font_8x16 fbtext gfx display cursor mouse i2c touchpad \
 	touchpad_gesture uhci ohci ehci xhci asix rndis iwl_trans iwl_mvm \
 	touchpad_gesture uhci ohci ehci xhci asix rndis iwl_trans iwl_mvm \
 	iwl_dvm wifi_fw rtl8821au wifi ccmp usb usbmsc usbhid lsusb panic \
 	iwl_dvm wifi_fw rtl8821au wifi ccmp usb usbmsc usbhid lsusb panic \
 	bootcon blkdev sdhci ata ahci fatdetect fatformat format install vfs \
 	bootcon blkdev sdhci ata ahci fatdetect fatformat format install vfs \
@@ -758,6 +758,12 @@ netmonitor: tools/netmonitor.c
 mkicons: tools/mkicons.c include/icon_abi.h user/apps/browser/svg_raster.c user/apps/browser/svg_raster.h
 mkicons: tools/mkicons.c include/icon_abi.h user/apps/browser/svg_raster.c user/apps/browser/svg_raster.h
 	gcc -O2 -Wall -Wextra -Iinclude -o $@ tools/mkicons.c user/apps/browser/svg_raster.c -lm
 	gcc -O2 -Wall -Wextra -Iinclude -o $@ tools/mkicons.c user/apps/browser/svg_raster.c -lm
 
 
+mkcursors: tools/mkcursors.c include/cursor_abi.h
+	gcc -O2 -Wall -Wextra -Iinclude -o $@ $<
+
+cursor_test: tools/cursor_test.c include/cursor_abi.h
+	gcc -O1 -g -Wall -Wextra -fsanitize=address,undefined -Iinclude -o $@ $<
+
 # ---- Command pack (the system rootfs) ---------------------------------------
 # ---- Command pack (the system rootfs) ---------------------------------------
 #
 #
 # The ring-3 command ELFs plus hello and the browser/gallery apps, packed
 # The ring-3 command ELFs plus hello and the browser/gallery apps, packed
@@ -765,6 +771,21 @@ mkicons: tools/mkicons.c include/icon_abi.h user/apps/browser/svg_raster.c user/
 FONT_FILES = fonts/sans.ttf fonts/sans-b.ttf fonts/sans-i.ttf \
 FONT_FILES = fonts/sans.ttf fonts/sans-b.ttf fonts/sans-i.ttf \
              fonts/sans-bi.ttf fonts/mono.ttf fonts/emoji.ttf
              fonts/sans-bi.ttf fonts/mono.ttf fonts/emoji.ttf
 
 
+# Mouse cursors: the Adwaita theme's 24 px images (assets/cursors/adwaita/,
+# LGPL-3 / CC-BY-SA-3.0) converted to one NBCUR1 bundle (include/cursor_abi.h)
+# that kernel/cursor.c loads from /ramfs/cursors/adwaita24.cur.  Full pack
+# only (~18 KB gzip): the floppy lite pack has no room, and its kernel keeps
+# the built-in 16x16 arrow.
+CURSOR_THEME = assets/cursors/adwaita
+CURSOR_FILE = cursors/adwaita24.cur
+$(CURSOR_FILE): mkcursors $(filter-out %/README %/COPYING%,$(wildcard $(CURSOR_THEME)/*))
+	@mkdir -p cursors
+	./mkcursors $(CURSOR_THEME) 24 $@
+
+# Defined after CURSOR_FILE: make expands a rule's prerequisites at once.
+test-cursors: cursor_test $(CURSOR_FILE)
+	./cursor_test $(CURSOR_FILE)
+
 # UI icons: the Adwaita symbolic SVGs (assets/icons/adwaita/, LGPL-3 /
 # UI icons: the Adwaita symbolic SVGs (assets/icons/adwaita/, LGPL-3 /
 # CC-BY-SA-3.0) rasterized by mkicons (with the browser's svg_raster.c) into
 # CC-BY-SA-3.0) rasterized by mkicons (with the browser's svg_raster.c) into
 # one NBICO1 bundle of 16 px coverage masks (include/icon_abi.h) that ngui
 # one NBICO1 bundle of 16 px coverage masks (include/icon_abi.h) that ngui
@@ -784,6 +805,7 @@ PACK_FILES = $(foreach a,$(UCMDS),bin/$(a)=$(call app,$(a)).elf) \
              $(ICON_FILE)=$(ICON_FILE)
              $(ICON_FILE)=$(ICON_FILE)
 PACK_DEPS = $(UCMD_ELFS) $(HELLO_ELF) browser.elf gallery.elf $(FONT_FILES) \
 PACK_DEPS = $(UCMD_ELFS) $(HELLO_ELF) browser.elf gallery.elf $(FONT_FILES) \
             $(ICON_FILE) mkpack
             $(ICON_FILE) mkpack
+PACK_CURSOR_FILES = $(CURSOR_FILE)=$(CURSOR_FILE)
 # The browser (~500 KB gzip) is in the full pack only: a floppy boot runs it
 # The browser (~500 KB gzip) is in the full pack only: a floppy boot runs it
 # from BROWSER on root.img, which the bin/browser launcher already tries.
 # from BROWSER on root.img, which the bin/browser launcher already tries.
 PACK_BROWSER_FILES = bin/browser.app=browser.elf
 PACK_BROWSER_FILES = bin/browser.app=browser.elf
@@ -837,12 +859,13 @@ firmware/rtl8821a.tbl: mkrtwtbl
 	@mkdir -p firmware
 	@mkdir -p firmware
 	./mkrtwtbl $@
 	./mkrtwtbl $@
 
 
-cmds.pack: $(PACK_DEPS) $(WIFI_FW_FILE) kernel.bin
-	./mkpack $@ $(PACK_FILES) $(PACK_BROWSER_FILES) $(PACK_INSTALL_FILES) \
+cmds.pack: $(PACK_DEPS) $(WIFI_FW_FILE) $(CURSOR_FILE) kernel.bin
+	./mkpack $@ $(PACK_FILES) $(PACK_BROWSER_FILES) $(PACK_CURSOR_FILES) \
+	    $(PACK_INSTALL_FILES) \
 	    $(PACK_FW_FILES)
 	    $(PACK_FW_FILES)
 
 
 # The floppy images cannot hold the ~1 MB firmware: they get the same pack
 # The floppy images cannot hold the ~1 MB firmware: they get the same pack
-# without firmware/* (and without bin/browser.app).
+# without firmware/* (and without the cursor bundle or bin/browser.app).
 cmds-lite.pack: $(PACK_DEPS)
 cmds-lite.pack: $(PACK_DEPS)
 	./mkpack $@ $(PACK_FILES)
 	./mkpack $@ $(PACK_FILES)
 
 
@@ -1204,8 +1227,8 @@ clean:
 	      BOOTX64.EFI browser.elf gallery.elf *.boot.tmp \
 	      BOOTX64.EFI browser.elf gallery.elf *.boot.tmp \
 	      cmds.pack cmds-lite.pack cmds_pack_gz.bin cmds_lite_gz.bin \
 	      cmds.pack cmds-lite.pack cmds_pack_gz.bin cmds_lite_gz.bin \
 	      rootfs.bin rootfs-full.bin cmds.img \
 	      rootfs.bin rootfs-full.bin cmds.img \
-	      firmware/*.ucode firmware/*.tbl firmware/*.bin icons/*.icn \
-	      mkpack mkrootfs mkrtwtbl mkicons kexec_send netkexec_send netmonitor \
+	      firmware/*.ucode firmware/*.tbl firmware/*.bin cursors/*.cur icons/*.icn \
+	      mkpack mkrootfs mkrtwtbl mkcursors mkicons cursor_test kexec_send netkexec_send netmonitor \
 	      browser-host host-curl \
 	      browser-host host-curl \
 	      ufont_test ufont_test_asan svg_test touchpad_gesture_test \
 	      ufont_test ufont_test_asan svg_test touchpad_gesture_test \
 	      ujson_test uhttp_cookie_test chat_login_test tls_test wpa_test \
 	      ujson_test uhttp_cookie_test chat_login_test tls_test wpa_test \

+ 10 - 0
assets/cursors/adwaita/COPYING

@@ -0,0 +1,10 @@
+This work is licenced under the terms of either the GNU LGPL v3 or
+Creative Commons Attribution-Share Alike 3.0 United States License.
+
+To view a copy of the CC-BY-SA licence, visit
+http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative
+Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
+
+When attributing the artwork, using "GNOME Project" is enough.
+Please link to http://www.gnome.org where available.
+

+ 7 - 0
assets/cursors/adwaita/COPYING_CCBYSA3

@@ -0,0 +1,7 @@
+This work is licenced under the Creative Commons Attribution-Share Alike 3.0
+United States License. To view a copy of this licence, visit
+http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative
+Commons, 171 Second Street, Suite 300, San Francisco, California 94105, USA.
+
+When attributing the artwork, using "GNOME Project" is enough. 
+Please link to http://www.gnome.org where available.

+ 165 - 0
assets/cursors/adwaita/COPYING_LGPL

@@ -0,0 +1,165 @@
+                   GNU LESSER GENERAL PUBLIC LICENSE
+                       Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+
+  This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
+
+  0. Additional Definitions.
+
+  As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
+
+  "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+  An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+  A "Combined Work" is a work produced by combining or linking an
+Application with the Library.  The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+  The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+  The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+  1. Exception to Section 3 of the GNU GPL.
+
+  You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+  2. Conveying Modified Versions.
+
+  If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+   a) under this License, provided that you make a good faith effort to
+   ensure that, in the event an Application does not supply the
+   function or data, the facility still operates, and performs
+   whatever part of its purpose remains meaningful, or
+
+   b) under the GNU GPL, with none of the additional permissions of
+   this License applicable to that copy.
+
+  3. Object Code Incorporating Material from Library Header Files.
+
+  The object code form of an Application may incorporate material from
+a header file that is part of the Library.  You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+   a) Give prominent notice with each copy of the object code that the
+   Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the object code with a copy of the GNU GPL and this license
+   document.
+
+  4. Combined Works.
+
+  You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+   a) Give prominent notice with each copy of the Combined Work that
+   the Library is used in it and that the Library and its use are
+   covered by this License.
+
+   b) Accompany the Combined Work with a copy of the GNU GPL and this license
+   document.
+
+   c) For a Combined Work that displays copyright notices during
+   execution, include the copyright notice for the Library among
+   these notices, as well as a reference directing the user to the
+   copies of the GNU GPL and this license document.
+
+   d) Do one of the following:
+
+       0) Convey the Minimal Corresponding Source under the terms of this
+       License, and the Corresponding Application Code in a form
+       suitable for, and under terms that permit, the user to
+       recombine or relink the Application with a modified version of
+       the Linked Version to produce a modified Combined Work, in the
+       manner specified by section 6 of the GNU GPL for conveying
+       Corresponding Source.
+
+       1) Use a suitable shared library mechanism for linking with the
+       Library.  A suitable mechanism is one that (a) uses at run time
+       a copy of the Library already present on the user's computer
+       system, and (b) will operate properly with a modified version
+       of the Library that is interface-compatible with the Linked
+       Version.
+
+   e) Provide Installation Information, but only if you would otherwise
+   be required to provide such information under section 6 of the
+   GNU GPL, and only to the extent that such information is
+   necessary to install and execute a modified version of the
+   Combined Work produced by recombining or relinking the
+   Application with a modified version of the Linked Version. (If
+   you use option 4d0, the Installation Information must accompany
+   the Minimal Corresponding Source and Corresponding Application
+   Code. If you use option 4d1, you must provide the Installation
+   Information in the manner specified by section 6 of the GNU GPL
+   for conveying Corresponding Source.)
+
+  5. Combined Libraries.
+
+  You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+   a) Accompany the combined library with a copy of the same work based
+   on the Library, uncombined with any other library facilities,
+   conveyed under the terms of this License.
+
+   b) Give prominent notice with the combined library that part of it
+   is a work based on the Library, and explaining where to find the
+   accompanying uncombined form of the same work.
+
+  6. Revised Versions of the GNU Lesser General Public License.
+
+  The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+  Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+  If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.

+ 19 - 0
assets/cursors/adwaita/README

@@ -0,0 +1,19 @@
+Adwaita cursor theme (24 px images only)
+
+Source:  the Arch Linux package adwaita-cursors 51.0
+         (/usr/share/icons/Adwaita/cursors), upstream
+         https://gitlab.gnome.org/GNOME/adwaita-icon-theme
+Licence: GNU LGPL v3 or Creative Commons Attribution-Share Alike 3.0
+         (see COPYING, COPYING_LGPL, COPYING_CCBYSA3).  Attribution:
+         "GNOME Project", https://www.gnome.org
+
+Each file is a valid Xcursor file reduced to its 24 px image chunks by
+tools/xcursor_trim.py (the upstream files also carry 30..96 px images):
+
+    python3 tools/xcursor_trim.py /usr/share/icons/Adwaita/cursors \
+        assets/cursors/adwaita 24
+
+The legacy X11 aliases (left_ptr, xterm, hand2, ...) are symlinks upstream
+and were not copied; X_cursor was dropped.  tools/mkcursors.c converts these
+files into the NBCUR1 bundle (include/cursor_abi.h) that is packed as
+/ramfs/cursors/adwaita24.cur.

BIN
assets/cursors/adwaita/alias


BIN
assets/cursors/adwaita/all-resize


BIN
assets/cursors/adwaita/all-scroll


BIN
assets/cursors/adwaita/cell


BIN
assets/cursors/adwaita/col-resize


BIN
assets/cursors/adwaita/context-menu


BIN
assets/cursors/adwaita/copy


BIN
assets/cursors/adwaita/crosshair


BIN
assets/cursors/adwaita/default


BIN
assets/cursors/adwaita/e-resize


BIN
assets/cursors/adwaita/ew-resize


BIN
assets/cursors/adwaita/grab


BIN
assets/cursors/adwaita/grabbing


BIN
assets/cursors/adwaita/help


BIN
assets/cursors/adwaita/n-resize


BIN
assets/cursors/adwaita/ne-resize


BIN
assets/cursors/adwaita/nesw-resize


BIN
assets/cursors/adwaita/no-drop


BIN
assets/cursors/adwaita/not-allowed


BIN
assets/cursors/adwaita/ns-resize


BIN
assets/cursors/adwaita/nw-resize


BIN
assets/cursors/adwaita/nwse-resize


BIN
assets/cursors/adwaita/pointer


BIN
assets/cursors/adwaita/progress


BIN
assets/cursors/adwaita/row-resize


BIN
assets/cursors/adwaita/s-resize


BIN
assets/cursors/adwaita/se-resize


BIN
assets/cursors/adwaita/sw-resize


BIN
assets/cursors/adwaita/text


BIN
assets/cursors/adwaita/vertical-text


BIN
assets/cursors/adwaita/w-resize


BIN
assets/cursors/adwaita/wait


BIN
assets/cursors/adwaita/zoom-in


BIN
assets/cursors/adwaita/zoom-out


+ 1 - 0
docs/development.html

@@ -234,6 +234,7 @@
 					<tr><td><code>make test-js</code></td><td>Host tests (ASan/UBSan) for the vendored QuickJS engine through the <code>js_mu</code> stack API: modern syntax (classes, arrows, templates, destructuring, spread, async/await, BigInt, named regex groups), Promise jobs, C functions and <code>new</code>, thrown TypeErrors, userdata hooks and expandos, registry refs and the wall-clock interrupt.</td></tr>
 					<tr><td><code>make test-js</code></td><td>Host tests (ASan/UBSan) for the vendored QuickJS engine through the <code>js_mu</code> stack API: modern syntax (classes, arrows, templates, destructuring, spread, async/await, BigInt, named regex groups), Promise jobs, C functions and <code>new</code>, thrown TypeErrors, userdata hooks and expandos, registry refs and the wall-clock interrupt.</td></tr>
 					<tr><td><code>make test-font</code> / <code>test-font-asan</code></td><td>Host specimen test for the fixed-point TrueType parser/rasterizer and glyph cache, with an AddressSanitizer/UBSan variant.</td></tr>
 					<tr><td><code>make test-font</code> / <code>test-font-asan</code></td><td>Host specimen test for the fixed-point TrueType parser/rasterizer and glyph cache, with an AddressSanitizer/UBSan variant.</td></tr>
 					<tr><td><code>make test-svg</code></td><td>Host tests (ASan/UBSan) for the inline-SVG rasterizer: exact rect coverage, antialiased edges, relative commands, nonzero vs even-odd, circle area, curves and arcs, transforms, number grammar and strokes; <code>./svg_test out.ppm</code> writes a specimen.</td></tr>
 					<tr><td><code>make test-svg</code></td><td>Host tests (ASan/UBSan) for the inline-SVG rasterizer: exact rect coverage, antialiased edges, relative commands, nonzero vs even-odd, circle area, curves and arcs, transforms, number grammar and strokes; <code>./svg_test out.ppm</code> writes a specimen.</td></tr>
+					<tr><td><code>make test-cursors</code></td><td>Host test (ASan/UBSan) for the NBCUR1 cursor bundle built by <code>mkcursors</code>: header and shape count, sizes and hotspots against the Adwaita files, premultiplied pixels, no empty frame, and the 15-frame <code>wait</code>/<code>progress</code> spinners.</td></tr>
 					<tr><td><code>make test-touchpad</code></td><td>Host tests for the device-independent contact gesture engine: taps, right-click zones, physical-button latch, two-finger scroll, direction and pointer re-anchoring.</td></tr>
 					<tr><td><code>make test-touchpad</code></td><td>Host tests for the device-independent contact gesture engine: taps, right-click zones, physical-button latch, two-finger scroll, direction and pointer re-anchoring.</td></tr>
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat</code></td><td>Host tests (ASan/UBSan) for the chat client's JSON parser, cookie jar and the OIDC login page/reply/config parsers.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>
 					<tr><td><code>make test-chat-e2e</code></td><td>Boots QEMU with an NE2000 on slirp, starts <code>tools/mock_chat.py</code> and drives <code>chat</code> through QMP: native login, send, live poll, read marker, older paging, browse/join, new DM, saved-session resume and sign-out.  <code>CHAT_E2E=--totp|--consent|--passkey|--wrong</code> runs a login variant.</td></tr>

+ 29 - 0
docs/graphics.html

@@ -61,6 +61,7 @@
 			<li><a href="#apps">Applications</a></li>
 			<li><a href="#apps">Applications</a></li>
 			<li><a href="#images">Image library</a></li>
 			<li><a href="#images">Image library</a></li>
 			<li><a href="#icons">UI icons</a></li>
 			<li><a href="#icons">UI icons</a></li>
+			<li><a href="#cursors">Mouse cursors</a></li>
 			<li><a href="#gpu">GPU quirks</a></li>
 			<li><a href="#gpu">GPU quirks</a></li>
 			<li><a href="#backlight">Panel backlight</a></li>
 			<li><a href="#backlight">Panel backlight</a></li>
 			<li><a href="#sources">Source map</a></li>
 			<li><a href="#sources">Source map</a></li>
@@ -467,6 +468,32 @@
 		</ul>
 		</ul>
 	</section>
 	</section>
 
 
+	<section id="cursors">
+		<h2>Mouse cursors</h2>
+		<p>
+			The cursor set is the GNOME Adwaita theme (adwaita-cursors 51,
+			LGPL-3 or CC-BY-SA-3.0).  Its 24&nbsp;px Xcursor images are copied
+			into <span class="file">assets/cursors/adwaita/</span> (trimmed by
+			<span class="file">tools/xcursor_trim.py</span>), and the host tool
+			<span class="file">tools/mkcursors.c</span> turns them into one
+			<code>NBCUR1</code> bundle, <code>/ramfs/cursors/adwaita24.cur</code>
+			(layout in <span class="file">include/cursor_abi.h</span>):
+			35 shapes named after the CSS <code>cursor</code> keywords, each
+			with its size, hotspot, frame count and premultiplied ARGB8888
+			frames.  The animated <code>wait</code>/<code>progress</code>
+			spinners keep every fourth of Adwaita's 60 frames (15 frames at
+			64&nbsp;ms, the same speed).  The bundle is ~146&nbsp;KB raw,
+			~18&nbsp;KB gzip, and only in the full pack: the floppy lite pack
+			has no room, so a floppy boot keeps the built-in 16&times;16 arrow.
+		</p>
+		<ul>
+			<li><strong>Loading.</strong> <span class="file">cursor.c</span> copies the bundle out of the ramfs into its own pages after <code>ramfs_init()</code> and after every <code>ramfs_replace()</code> (the old pack is freed), then tells <span class="file">mouse.c</span> to re-fetch its image.</li>
+			<li><strong>Selection.</strong> On every <code>SYS_WM_POLL</code> the window server resolves the shape: the WM's override (<code>SYS_WM_SET_CURSOR</code>: resize arrows on the grip and while resizing, the move cross during a title-bar drag, a hand over launchers and the status area), else the cursor of the window holding the pointer capture, else the content cursor (<code>SYS_WIN_SET_CURSOR</code>) of the window under the pointer, else the arrow.</li>
+			<li><strong>Clients.</strong> ngui resets the cursor to the arrow for each redrawn frame, a hovered text field asks for the I-beam, <code>ngui_cursor_rect()</code>/<code>ngui_set_cursor()</code> serve apps, and <code>ngui_end_ex()</code> sends it when it changed.  The terminal uses the I-beam; the browser maps the inherited CSS <code>cursor</code> property and, for <code>auto</code>, a hand over links, an I-beam over text fields and the progress arrow while a page loads.  The gallery's Cursors section shows every shape.</li>
+			<li><strong>Drawing.</strong> <code>cur_x</code>/<code>cur_y</code> stay the hotspot; the image's top-left is the hotspot minus the image's hotspot and may be off-screen.  The software cursor saves up to 32&times;32 pixels under it and blends the premultiplied pixels over them.  Intel Ironlake and Radeon copy the ARGB image into their 64&times;64 planes (Intel uses CURPOS sign bits for negative positions, Radeon <code>HORZ_VERT_OFF</code> plus a shifted <code>CUR_OFFSET</code>); Cirrus reduces it to its two colours and clamps the position at 0.  Animated frames advance from <code>mouse_poll_update()</code>.</li>
+		</ul>
+	</section>
+
 	<section id="gpu">
 	<section id="gpu">
 		<h2>GPU quirks</h2>
 		<h2>GPU quirks</h2>
 		<ul>
 		<ul>
@@ -518,7 +545,9 @@
 			<li><code>user/lib/img/img*.c</code> — image codecs</li>
 			<li><code>user/lib/img/img*.c</code> — image codecs</li>
 			<li><code>kernel/ati.c</code>, <code>kernel/nvidia.c</code>, <code>kernel/intel.c</code>, <code>kernel/nv50.c</code></li>
 			<li><code>kernel/ati.c</code>, <code>kernel/nvidia.c</code>, <code>kernel/intel.c</code>, <code>kernel/nv50.c</code></li>
 			<li><code>kernel/backlight.c</code>, <code>kernel/backlight.h</code> — Cherryview/VLV panel PWM</li>
 			<li><code>kernel/backlight.c</code>, <code>kernel/backlight.h</code> — Cherryview/VLV panel PWM</li>
+			<li><code>kernel/cursor.c</code>, <code>kernel/cursor.h</code>, <code>kernel/mouse.c</code>, <code>kernel/cursor_shape.h</code> — cursor bundle loader, drawing and the built-in fallback arrow; <code>kernel/cirrus.c</code>, <code>kernel/radeon.c</code> — hardware cursors</li>
 			<li><code>include/icon_abi.h</code>, <code>tools/mkicons.c</code>, <code>assets/icons/adwaita/</code> — UI icon list, the NBICO1 bundle and the Adwaita symbolic SVGs</li>
 			<li><code>include/icon_abi.h</code>, <code>tools/mkicons.c</code>, <code>assets/icons/adwaita/</code> — UI icon list, the NBICO1 bundle and the Adwaita symbolic SVGs</li>
+			<li><code>include/cursor_abi.h</code>, <code>tools/mkcursors.c</code>, <code>tools/xcursor_trim.py</code>, <code>tools/cursor_test.c</code>, <code>assets/cursors/adwaita/</code> — cursor shapes, the NBCUR1 bundle and the Adwaita source images</li>
 		</ul>
 		</ul>
 	</section>
 	</section>
 </main>
 </main>

+ 1 - 1
docs/index.html

@@ -212,7 +212,7 @@
 			</a>
 			</a>
 			<a class="card" href="graphics.html">
 			<a class="card" href="graphics.html">
 				<h3>Graphics &amp; UI</h3>
 				<h3>Graphics &amp; UI</h3>
-				<p>VBE framebuffer, shadow buffer, damage rectangles, compositor, canvas ABI, ngui, the Adwaita UI icons, the fixed-point TrueType text engine and image codecs.</p>
+				<p>VBE framebuffer, shadow buffer, damage rectangles, compositor, canvas ABI, ngui, the Adwaita UI icons and mouse cursors, the fixed-point TrueType text engine and image codecs.</p>
 				<span class="card-tag">graphics.html</span>
 				<span class="card-tag">graphics.html</span>
 			</a>
 			</a>
 			<a class="card" href="network.html">
 			<a class="card" href="network.html">

+ 1 - 0
docs/userland.html

@@ -230,6 +230,7 @@
 					<tr><td>NIC menu</td><td>111–112</td><td><code>SYS_NET_DEVLIST</code> (present drivers with kind/active/link) and <code>SYS_NET_SELECT</code> (make a named netdev active)</td></tr>
 					<tr><td>NIC menu</td><td>111–112</td><td><code>SYS_NET_DEVLIST</code> (present drivers with kind/active/link) and <code>SYS_NET_SELECT</code> (make a named netdev active)</td></tr>
 					<tr><td>Hardware settings</td><td>113–114</td><td><code>SYS_BACKLIGHT(op, value)</code> get/set/step the panel PWM, <code>SYS_CPUFREQ(mode)</code> get/set the P-state mode (control panel sliders/tiles)</td></tr>
 					<tr><td>Hardware settings</td><td>113–114</td><td><code>SYS_BACKLIGHT(op, value)</code> get/set/step the panel PWM, <code>SYS_CPUFREQ(mode)</code> get/set the P-state mode (control panel sliders/tiles)</td></tr>
 					<tr><td>Popover layout</td><td>115–116</td><td><code>SYS_WIN_RESIZE_SELF(w, h)</code> resizes the caller's canvas, <code>SYS_SCREEN_INFO(out)</code> returns the framebuffer dimensions (the control panel collapses/grows)</td></tr>
 					<tr><td>Popover layout</td><td>115–116</td><td><code>SYS_WIN_RESIZE_SELF(w, h)</code> resizes the caller's canvas, <code>SYS_SCREEN_INFO(out)</code> returns the framebuffer dimensions (the control panel collapses/grows)</td></tr>
+					<tr><td>Mouse cursor</td><td>117–118</td><td><code>SYS_WIN_SET_CURSOR(shape)</code> sets the caller's content cursor, <code>SYS_WM_SET_CURSOR(shape | -1)</code> is the WM's override for its chrome (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
 				</tbody>
 				</tbody>
 			</table>
 			</table>
 		</div>
 		</div>

+ 4 - 0
host/syscall.c

@@ -212,6 +212,10 @@ int u_syscall3(int num, int a1, int a2, int a3)
 		return host_window_scroll((struct win_scroll *)argp(a1));
 		return host_window_scroll((struct win_scroll *)argp(a1));
 	case SYS_WIN_RESIZE_SELF:
 	case SYS_WIN_RESIZE_SELF:
 		return -1;
 		return -1;
+	case SYS_WIN_SET_CURSOR:
+		return host_window_set_cursor((int)a1);
+	case SYS_WM_SET_CURSOR:
+		return -1;
 	case SYS_SCREEN_INFO: {
 	case SYS_SCREEN_INFO: {
 		struct sys_screen *out = (struct sys_screen *)argp(a1);
 		struct sys_screen *out = (struct sys_screen *)argp(a1);
 
 

+ 9 - 0
host/window.c

@@ -119,6 +119,15 @@ int host_window_screen(unsigned int *w, unsigned int *h)
 	return headless ? host_headless_screen(w, h) : host_sdl_screen(w, h);
 	return headless ? host_headless_screen(w, h) : host_sdl_screen(w, h);
 }
 }
 
 
+int host_window_set_cursor(int shape)
+{
+	if (!open)
+		return -1;
+	if (!headless)
+		host_sdl_set_cursor(shape);
+	return 0;
+}
+
 int host_window_key_wait(unsigned int timeout_ms)
 int host_window_key_wait(unsigned int timeout_ms)
 {
 {
 	if (!open) {
 	if (!open) {

+ 3 - 0
host/window.h

@@ -19,6 +19,8 @@ int host_window_key(void);
 int host_window_screen(unsigned int *w, unsigned int *h);
 int host_window_screen(unsigned int *w, unsigned int *h);
 int host_window_key_wait(unsigned int timeout_ms);
 int host_window_key_wait(unsigned int timeout_ms);
 const char *host_window_backend(void);
 const char *host_window_backend(void);
+/* SYS_WIN_SET_CURSOR: enum cursor_shape (cursor_abi.h). */
+int host_window_set_cursor(int shape);
 
 
 /* Active backend (window.c selects by host_opts.headless). */
 /* Active backend (window.c selects by host_opts.headless). */
 void host_sdl_close(void);
 void host_sdl_close(void);
@@ -35,6 +37,7 @@ int host_sdl_key(void);
 int host_sdl_screen(unsigned int *w, unsigned int *h);
 int host_sdl_screen(unsigned int *w, unsigned int *h);
 int host_sdl_key_wait(unsigned int timeout_ms);
 int host_sdl_key_wait(unsigned int timeout_ms);
 const char *host_sdl_backend(void);
 const char *host_sdl_backend(void);
+void host_sdl_set_cursor(int shape);
 
 
 void host_headless_close(void);
 void host_headless_close(void);
 int host_headless_open(unsigned int w, unsigned int h, const char *title);
 int host_headless_open(unsigned int w, unsigned int h, const char *title);

+ 47 - 0
host/window_sdl3.c

@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <unistd.h>
 
 
 #include "window.h"
 #include "window.h"
+#include "cursor_abi.h"
 
 
 #define KEYQ_MAX 256
 #define KEYQ_MAX 256
 #define EVQ_MAX  64
 #define EVQ_MAX  64
@@ -431,6 +432,52 @@ int host_sdl_key(void)
 	return pop_key();
 	return pop_key();
 }
 }
 
 
+/* NebOS cursor shapes on the desktop's own cursor theme.  SDL has no
+ * help/zoom/copy/... system cursors: those fall back to the arrow. */
+void host_sdl_set_cursor(int shape)
+{
+	static SDL_Cursor *cache[CURSOR_COUNT];
+	static int current = -1;
+	SDL_SystemCursor sc = SDL_SYSTEM_CURSOR_DEFAULT;
+
+	if (shape < 0 || shape >= CURSOR_COUNT || shape == current)
+		return;
+	switch (shape) {
+	case CURSOR_POINTER:     sc = SDL_SYSTEM_CURSOR_POINTER; break;
+	case CURSOR_TEXT:
+	case CURSOR_VERTICAL_TEXT: sc = SDL_SYSTEM_CURSOR_TEXT; break;
+	case CURSOR_WAIT:        sc = SDL_SYSTEM_CURSOR_WAIT; break;
+	case CURSOR_PROGRESS:    sc = SDL_SYSTEM_CURSOR_PROGRESS; break;
+	case CURSOR_CROSSHAIR:
+	case CURSOR_CELL:        sc = SDL_SYSTEM_CURSOR_CROSSHAIR; break;
+	case CURSOR_MOVE:
+	case CURSOR_ALL_SCROLL:
+	case CURSOR_ALL_RESIZE:  sc = SDL_SYSTEM_CURSOR_MOVE; break;
+	case CURSOR_NOT_ALLOWED:
+	case CURSOR_NO_DROP:     sc = SDL_SYSTEM_CURSOR_NOT_ALLOWED; break;
+	case CURSOR_N_RESIZE:    sc = SDL_SYSTEM_CURSOR_N_RESIZE; break;
+	case CURSOR_E_RESIZE:    sc = SDL_SYSTEM_CURSOR_E_RESIZE; break;
+	case CURSOR_S_RESIZE:    sc = SDL_SYSTEM_CURSOR_S_RESIZE; break;
+	case CURSOR_W_RESIZE:    sc = SDL_SYSTEM_CURSOR_W_RESIZE; break;
+	case CURSOR_NE_RESIZE:   sc = SDL_SYSTEM_CURSOR_NE_RESIZE; break;
+	case CURSOR_NW_RESIZE:   sc = SDL_SYSTEM_CURSOR_NW_RESIZE; break;
+	case CURSOR_SE_RESIZE:   sc = SDL_SYSTEM_CURSOR_SE_RESIZE; break;
+	case CURSOR_SW_RESIZE:   sc = SDL_SYSTEM_CURSOR_SW_RESIZE; break;
+	case CURSOR_EW_RESIZE:
+	case CURSOR_COL_RESIZE:  sc = SDL_SYSTEM_CURSOR_EW_RESIZE; break;
+	case CURSOR_NS_RESIZE:
+	case CURSOR_ROW_RESIZE:  sc = SDL_SYSTEM_CURSOR_NS_RESIZE; break;
+	case CURSOR_NESW_RESIZE: sc = SDL_SYSTEM_CURSOR_NESW_RESIZE; break;
+	case CURSOR_NWSE_RESIZE: sc = SDL_SYSTEM_CURSOR_NWSE_RESIZE; break;
+	default: break;
+	}
+	if (!cache[shape])
+		cache[shape] = SDL_CreateSystemCursor(sc);
+	if (cache[shape])
+		SDL_SetCursor(cache[shape]);
+	current = shape;
+}
+
 int host_sdl_screen(unsigned int *w, unsigned int *h)
 int host_sdl_screen(unsigned int *w, unsigned int *h)
 {
 {
 	SDL_Rect r;
 	SDL_Rect r;

+ 87 - 0
include/cursor_abi.h

@@ -0,0 +1,87 @@
+/* Mouse-cursor shapes and the NBCUR1 cursor bundle.
+ *
+ * Shared by the kernel (kernel/cursor.c loads the bundle and mouse.c draws
+ * it), ring 3 (SYS_WIN_SET_CURSOR / SYS_WM_SET_CURSOR take a shape id, the
+ * browser maps CSS `cursor` keywords) and the host tool tools/mkcursors.c,
+ * which builds /ramfs/cursors/adwaita24.cur from assets/cursors/adwaita/.
+ *
+ * The shape names are the CSS / freedesktop cursor names.  CURSOR_LIST is an
+ * X-macro: X(ENUM_SUFFIX, "css-name", "theme-file"); the order is the shape
+ * id and the order of the shapes in the bundle, so append only. */
+#ifndef CURSOR_ABI_H
+#define CURSOR_ABI_H
+
+#define CURSOR_LIST(X) \
+	X(DEFAULT,       "default",       "default")       \
+	X(POINTER,       "pointer",       "pointer")       \
+	X(TEXT,          "text",          "text")          \
+	X(VERTICAL_TEXT, "vertical-text", "vertical-text") \
+	X(WAIT,          "wait",          "wait")          \
+	X(PROGRESS,      "progress",      "progress")      \
+	X(HELP,          "help",          "help")          \
+	X(CONTEXT_MENU,  "context-menu",  "context-menu")  \
+	X(CELL,          "cell",          "cell")          \
+	X(CROSSHAIR,     "crosshair",     "crosshair")     \
+	X(MOVE,          "move",          "all-resize")    \
+	X(ALL_SCROLL,    "all-scroll",    "all-scroll")    \
+	X(GRAB,          "grab",          "grab")          \
+	X(GRABBING,      "grabbing",      "grabbing")      \
+	X(NOT_ALLOWED,   "not-allowed",   "not-allowed")   \
+	X(NO_DROP,       "no-drop",       "no-drop")       \
+	X(COPY,          "copy",          "copy")          \
+	X(ALIAS,         "alias",         "alias")         \
+	X(ZOOM_IN,       "zoom-in",       "zoom-in")       \
+	X(ZOOM_OUT,      "zoom-out",      "zoom-out")      \
+	X(COL_RESIZE,    "col-resize",    "col-resize")    \
+	X(ROW_RESIZE,    "row-resize",    "row-resize")    \
+	X(N_RESIZE,      "n-resize",      "n-resize")      \
+	X(E_RESIZE,      "e-resize",      "e-resize")      \
+	X(S_RESIZE,      "s-resize",      "s-resize")      \
+	X(W_RESIZE,      "w-resize",      "w-resize")      \
+	X(NE_RESIZE,     "ne-resize",     "ne-resize")     \
+	X(NW_RESIZE,     "nw-resize",     "nw-resize")     \
+	X(SE_RESIZE,     "se-resize",     "se-resize")     \
+	X(SW_RESIZE,     "sw-resize",     "sw-resize")     \
+	X(EW_RESIZE,     "ew-resize",     "ew-resize")     \
+	X(NS_RESIZE,     "ns-resize",     "ns-resize")     \
+	X(NESW_RESIZE,   "nesw-resize",   "nesw-resize")   \
+	X(NWSE_RESIZE,   "nwse-resize",   "nwse-resize")   \
+	X(ALL_RESIZE,    "all-resize",    "all-resize")
+
+#define CURSOR_ENUM_(e, css, file) CURSOR_##e,
+enum cursor_shape {
+	CURSOR_LIST(CURSOR_ENUM_)
+	CURSOR_COUNT
+};
+#undef CURSOR_ENUM_
+
+/* SYS_WM_SET_CURSOR: no WM override, use the window's own cursor. */
+#define CURSOR_NO_OVERRIDE (-1)
+
+/* Largest cursor image the kernel accepts (the software cursor's save
+ * buffer and the hardware planes are sized for it). */
+#define CURSOR_MAX_DIM 32
+
+/* ---- NBCUR1 bundle ------------------------------------------------------
+ * [struct cursor_file_hdr][count x struct cursor_file_shape][pixels]
+ * Pixels are premultiplied ARGB8888 (0xAARRGGBB little-endian dwords, as in
+ * Xcursor), w*h per frame, frames stored back to back starting at
+ * `offset` (bytes from the start of the file).  Every field is
+ * little-endian. */
+#define CURSOR_FILE_MAGIC "NBCUR1\0\0"
+
+struct cursor_file_hdr {
+	char magic[8];
+	unsigned int count;      /* number of shapes (CURSOR_COUNT when built) */
+	unsigned int nominal;    /* theme size in px, e.g. 24 */
+};
+
+struct cursor_file_shape {
+	unsigned short w, h;
+	unsigned short hot_x, hot_y;
+	unsigned short frames;   /* >= 1; > 1 animates */
+	unsigned short delay_ms; /* per frame when animated */
+	unsigned int offset;     /* first frame's pixels */
+};
+
+#endif

+ 4 - 0
include/syscall.h

@@ -117,6 +117,10 @@
 #define SYS_WIN_SCROLL     109 /* (struct win_scroll *out) -> 1 delta, 0 none, -1 */
 #define SYS_WIN_SCROLL     109 /* (struct win_scroll *out) -> 1 delta, 0 none, -1 */
 #define SYS_WIN_RESIZE_SELF 115 /* (int w, int h) -> 0/-1; resize the caller's canvas */
 #define SYS_WIN_RESIZE_SELF 115 /* (int w, int h) -> 0/-1; resize the caller's canvas */
 #define SYS_SCREEN_INFO   116 /* (struct sys_screen *out) -> 0/-1 */
 #define SYS_SCREEN_INFO   116 /* (struct sys_screen *out) -> 0/-1 */
+#define SYS_WIN_SET_CURSOR 117 /* (int shape) -> 0/-1; the caller's content
+				  cursor (enum cursor_shape, cursor_abi.h) */
+#define SYS_WM_SET_CURSOR 118 /* (int shape | CURSOR_NO_OVERRIDE) -> 0/-1;
+				  WM only: cursor for its chrome/drags */
 #define SYS_BACKLIGHT     113 /* (int op, int value) -> percent / -1.
 #define SYS_BACKLIGHT     113 /* (int op, int value) -> percent / -1.
 				  op 0 = get, 1 = set (5..100), 2 = step */
 				  op 0 = get, 1 = set (5..100), 2 = step */
 #define SYS_BACKLIGHT_GET  0
 #define SYS_BACKLIGHT_GET  0

+ 44 - 28
kernel/cirrus.c

@@ -2,7 +2,7 @@
    Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
    Reference: QEMU hw/display/cirrus_vga.c (register offsets, ROP codes). */
 
 
 #include "cirrus.h"
 #include "cirrus.h"
-#include "cursor_shape.h"
+#include "cursor.h"
 #include "pci.h"
 #include "pci.h"
 #include "vga.h"
 #include "vga.h"
 
 
@@ -195,37 +195,47 @@ void cirrus_fill(unsigned int x, unsigned int y,
 		;
 		;
 }
 }
 
 
-void cirrus_cursor_setup(void)
+/* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
+ * 4 bytes per row, the image in the top-left of the box:
+ *   AND=0 XOR=0 -> transparent
+ *   AND=0 XOR=1 -> color0 (white)
+ *   AND=1 XOR=1 -> color1 (black)
+ * The hidden DAC entries 0 and 15 are programmed white and black.  The ARGB
+ * image is reduced to that: alpha >= 50% is opaque, and the (premultiplied)
+ * luminance picks white or black. */
+void cirrus_cursor_load(const struct cursor_image *img)
 {
 {
-	unsigned char bitmap[256];
-	unsigned int r, i;
 	unsigned int vram = vga_fb_vram_kb() * 1024;
 	unsigned int vram = vga_fb_vram_kb() * 1024;
 	unsigned char *dst;
 	unsigned char *dst;
+	unsigned int r, c, i;
 
 
-	/* 32x32 two-plane cursor: AND plane (offset 0..127), XOR plane (128..255),
-	   4 bytes per row.  The shared arrow is placed in the top-left of the
-	   box (the cursor position is the top-left of the 32x32 box):
-	     AND=0 XOR=0 -> transparent
-	     AND=0 XOR=1 -> color0 (white fill)
-	     AND=1 XOR=1 -> color1 (black outline)
-	   The hidden DAC entries 0 and 15 are programmed white and black. */
+	dst = (unsigned char *)(vga_fb_lfb_addr() + vram - 16384);
 	for (i = 0; i < 256; i++)
 	for (i = 0; i < 256; i++)
-		bitmap[i] = 0;
-	for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
-		unsigned short fill = cursor_shape_fill[r];
-		unsigned short outl = cursor_shape_outline[r];
-		unsigned short xor = fill | outl;
-		bitmap[r * 4 + 0] = (unsigned char)((outl >> 8) & 0xFF);        /* AND x=0..7  */
-		bitmap[r * 4 + 1] = (unsigned char)(outl & 0xFF);               /* AND x=8..15 */
-		bitmap[128 + r * 4 + 0] = (unsigned char)((xor >> 8) & 0xFF);   /* XOR x=0..7  */
-		bitmap[128 + r * 4 + 1] = (unsigned char)(xor & 0xFF);          /* XOR x=8..15 */
-	}
+		dst[i] = 0;
+	if (!img || !img->px)
+		return;
+	for (r = 0; r < img->h && r < 32; r++)
+		for (c = 0; c < img->w && c < 32; c++) {
+			unsigned int p = img->px[r * img->w + c];
+			unsigned int a = p >> 24;
+			unsigned int lum = (((p >> 16) & 0xFF) * 3 +
+					    ((p >> 8) & 0xFF) * 6 +
+					    (p & 0xFF)) / 10;
+			unsigned char bit = (unsigned char)(0x80u >> (c & 7));
+
+			if (a < 128)
+				continue;
+			dst[128 + r * 4 + c / 8] |= bit;    /* XOR: opaque */
+			if (lum * 2 < a)
+				dst[r * 4 + c / 8] |= bit;  /* AND: black */
+		}
+}
 
 
-	/* Upload to the top of VRAM (SR[0x13] = 0 selects the first 256-byte block). */
+void cirrus_cursor_setup(const struct cursor_image *img)
+{
+	/* SR[0x13] = 0 selects the first 256-byte block at the top of VRAM. */
 	sr_write(SR_CURSOR_PAT, 0);
 	sr_write(SR_CURSOR_PAT, 0);
-	dst = (unsigned char *)(vga_fb_lfb_addr() + vram - 16384);
-	for (i = 0; i < 256; i++)
-		dst[i] = bitmap[i];
+	cirrus_cursor_load(img);
 
 
 	/* Cursor colors via the hidden DAC palette: entry 0 = white, 15 = black. */
 	/* Cursor colors via the hidden DAC palette: entry 0 = white, 15 = black. */
 	sr_write(SR_CURSOR_ATTR, CURSOR_HIDDENPEL);
 	sr_write(SR_CURSOR_ATTR, CURSOR_HIDDENPEL);
@@ -236,11 +246,17 @@ void cirrus_cursor_setup(void)
 	sr_write(SR_CURSOR_ATTR, CURSOR_SHOW);
 	sr_write(SR_CURSOR_ATTR, CURSOR_SHOW);
 }
 }
 
 
-void cirrus_cursor_move(unsigned int x, unsigned int y)
+/* (x,y) is the image's top-left.  The Cirrus position cannot go negative,
+ * so near the top/left edge the image is clamped (the hotspot is then off
+ * by up to its offset). */
+void cirrus_cursor_move(int x, int y)
 {
 {
+	unsigned int ux = x < 0 ? 0u : (unsigned int)x;
+	unsigned int uy = y < 0 ? 0u : (unsigned int)y;
+
 	/* Position is 11 bits; low 3 bits select the index alias, high 8 are data. */
 	/* Position is 11 bits; low 3 bits select the index alias, high 8 are data. */
-	sr_write(SR_CURSOR_X | ((x & 0x7) << 5), (unsigned char)(x >> 3));
-	sr_write(SR_CURSOR_Y | ((y & 0x7) << 5), (unsigned char)(y >> 3));
+	sr_write(SR_CURSOR_X | ((ux & 0x7) << 5), (unsigned char)(ux >> 3));
+	sr_write(SR_CURSOR_Y | ((uy & 0x7) << 5), (unsigned char)(uy >> 3));
 }
 }
 
 
 void cirrus_cursor_show(int show)
 void cirrus_cursor_show(int show)

+ 5 - 2
kernel/cirrus.h

@@ -16,8 +16,11 @@ void cirrus_fill(unsigned int x, unsigned int y,
 		 unsigned int r, unsigned int g, unsigned int b);
 		 unsigned int r, unsigned int g, unsigned int b);
 
 
 /* Hardware cursor (32x32). */
 /* Hardware cursor (32x32). */
-void cirrus_cursor_setup(void);
-void cirrus_cursor_move(unsigned int x, unsigned int y);
+struct cursor_image;
+void cirrus_cursor_setup(const struct cursor_image *img);
+void cirrus_cursor_load(const struct cursor_image *img);
+/* (x,y) is the image's top-left; negative values are clamped to 0. */
+void cirrus_cursor_move(int x, int y);
 void cirrus_cursor_show(int show);
 void cirrus_cursor_show(int show);
 
 
 #endif
 #endif

+ 145 - 0
kernel/cursor.c

@@ -0,0 +1,145 @@
+/* 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;
+}

+ 29 - 0
kernel/cursor.h

@@ -0,0 +1,29 @@
+/* Mouse-cursor images: the NBCUR1 bundle from /ramfs/cursors (see
+ * include/cursor_abi.h), with a built-in 16x16 arrow as the fallback. */
+#ifndef CURSOR_H
+#define CURSOR_H
+
+#include "cursor_abi.h"
+
+struct cursor_image {
+	const unsigned int *px;     /* premultiplied ARGB8888, w*h */
+	unsigned int w, h;
+	unsigned int hot_x, hot_y;
+	unsigned int frames;        /* >= 1 */
+	unsigned int delay_ms;      /* per frame when frames > 1 */
+};
+
+/* (Re)load the bundle from the ramfs into page-allocated memory.  Called
+ * after ramfs_init() and from ramfs_replace(); the mouse is told to pick up
+ * the new images.  A missing or invalid bundle keeps the previous one (or
+ * the built-in arrow). */
+void cursor_load(void);
+
+/* Fill *out with frame `frame` (mod the frame count) of `shape`.  An
+ * unknown shape, or one the bundle lacks, gives the default arrow. */
+void cursor_get(int shape, unsigned int frame, struct cursor_image *out);
+
+/* 1 when the bundle is loaded (0 = built-in arrow only). */
+int cursor_bundle_loaded(void);
+
+#endif

+ 48 - 27
kernel/intel.c

@@ -9,7 +9,9 @@
  *
  *
  * Register semantics follow the Linux i915 Ironlake path:
  * Register semantics follow the Linux i915 Ironlake path:
  *  - CURACNTR/CURABASE/CURAPOS are per-pipe (A at 0x70080, B at 0x700c0).
  *  - CURACNTR/CURABASE/CURAPOS are per-pipe (A at 0x70080, B at 0x700c0).
- *  - 64x64 ARGB8888 cursor, MSB A:R:G:B, hotspot at (0,0).
+ *  - 64x64 ARGB8888 cursor, MSB A:R:G:B, hotspot at (0,0); the mouse code
+ *    positions the image's top-left, so CURPOS takes negative values in
+ *    sign-magnitude form (bit 15 = X sign, bit 31 = Y sign).
  *  - CURCNTR bit 31 enables, mode 0x27 is 64x64 ARGB, bits 29:28 hold the
  *  - CURCNTR bit 31 enables, mode 0x27 is 64x64 ARGB, bits 29:28 hold the
  *    pipe select.  CURBASE is written after CURPOS to arm the update.
  *    pipe select.  CURBASE is written after CURPOS to arm the update.
  *  - The GTT PTE is the physical page, its bits 35:32 in PTE bits 7:4, the
  *  - The GTT PTE is the physical page, its bits 35:32 in PTE bits 7:4, the
@@ -17,7 +19,7 @@
  */
  */
 
 
 #include "intel.h"
 #include "intel.h"
-#include "cursor_shape.h"
+#include "cursor.h"
 #include "mm.h"
 #include "mm.h"
 #include "paging.h"
 #include "paging.h"
 #include "pci.h"
 #include "pci.h"
@@ -176,28 +178,19 @@ static int intel_alloc_cursor(void)
 	return 0;
 	return 0;
 }
 }
 
 
-static void intel_fill_cursor(unsigned int phys)
+/* Copy a cursor image (premultiplied ARGB, as the plane expects) into the
+ * top-left of the 64x64 plane; NULL clears it. */
+static void intel_fill_cursor(unsigned int phys, const struct cursor_image *ci)
 {
 {
 	unsigned int *img = (unsigned int *)phys;
 	unsigned int *img = (unsigned int *)phys;
 	unsigned int i, r, c;
 	unsigned int i, r, c;
 
 
 	for (i = 0; i < CUR_SIZE * CUR_SIZE; i++)
 	for (i = 0; i < CUR_SIZE * CUR_SIZE; i++)
 		img[i] = 0;
 		img[i] = 0;
-
-	for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
-		unsigned short fill = cursor_shape_fill[r];
-		unsigned short outline = cursor_shape_outline[r];
-
-		for (c = 0; c < CURSOR_SHAPE_WIDTH; c++) {
-			unsigned short bit = (unsigned short)(0x8000u >> c);
-			unsigned int *px = &img[r * CUR_SIZE + c];
-
-			if (fill & bit)
-				*px = 0xFFFFFFFFu;      /* opaque white */
-			else if (outline & bit)
-				*px = 0xFF000000u;      /* opaque black */
-		}
-	}
+	if (ci && ci->px)
+		for (r = 0; r < ci->h && r < CUR_SIZE; r++)
+			for (c = 0; c < ci->w && c < CUR_SIZE; c++)
+				img[r * CUR_SIZE + c] = ci->px[r * ci->w + c];
 
 
 	/* The GTT mapping is uncached, so push the WB image out to DRAM. */
 	/* The GTT mapping is uncached, so push the WB image out to DRAM. */
 	__asm__ volatile ("wbinvd" ::: "memory");
 	__asm__ volatile ("wbinvd" ::: "memory");
@@ -325,7 +318,7 @@ void intel_init(unsigned int bus, unsigned int dev, unsigned int device_id)
 		return;
 		return;
 	}
 	}
 	intel_cursor_gtt = slot * 4096;
 	intel_cursor_gtt = slot * 4096;
-	intel_fill_cursor(intel_cursor_phys);
+	intel_fill_cursor(intel_cursor_phys, 0);
 	if (intel_map_cursor(slot) != 0) {
 	if (intel_map_cursor(slot) != 0) {
 		serial_puts(SERIAL_COM1, "intel: GTT write did not stick\n");
 		serial_puts(SERIAL_COM1, "intel: GTT write did not stick\n");
 		return;
 		return;
@@ -349,19 +342,33 @@ int intel_available(void)
 	return intel_probe_ok;
 	return intel_probe_ok;
 }
 }
 
 
-static void intel_cursor_program(unsigned int pipe, unsigned int x,
-				 unsigned int y)
+static unsigned int intel_cursor_pos(int x, int y)
+{
+	unsigned int pos = 0;
+
+	if (x < 0) {
+		pos |= 0x8000u;
+		x = -x;
+	}
+	if (y < 0) {
+		pos |= 0x80000000u;
+		y = -y;
+	}
+	return pos | ((unsigned int)x & 0x7FFFu) |
+	       (((unsigned int)y & 0x7FFFu) << 16);
+}
+
+static void intel_cursor_program(unsigned int pipe, int x, int y)
 {
 {
 	unsigned int base = CURACNTR + (pipe ? CUR_PIPE_B : 0);
 	unsigned int base = CURACNTR + (pipe ? CUR_PIPE_B : 0);
-	unsigned int pos = (x & 0x7FFFu) | ((y & 0x7FFFu) << 16);
 
 
 	mmio_write(base, cursor_ctrl(pipe));
 	mmio_write(base, cursor_ctrl(pipe));
-	mmio_write(base + 8, pos);                  /* CURAPOS */
-	mmio_write(base + 4, intel_cursor_gtt);     /* CURABASE */
+	mmio_write(base + 8, intel_cursor_pos(x, y)); /* CURAPOS */
+	mmio_write(base + 4, intel_cursor_gtt);       /* CURABASE */
 	(void)mmio_read(base + 4);
 	(void)mmio_read(base + 4);
 }
 }
 
 
-int intel_cursor_setup(unsigned int x, unsigned int y)
+int intel_cursor_setup(int x, int y)
 {
 {
 	if (!intel_probe_ok || !intel_cursor_gtt)
 	if (!intel_probe_ok || !intel_cursor_gtt)
 		return -1;
 		return -1;
@@ -378,13 +385,27 @@ int intel_cursor_setup(unsigned int x, unsigned int y)
 	return 0;
 	return 0;
 }
 }
 
 
-void intel_cursor_move(unsigned int x, unsigned int y)
+void intel_cursor_load(const struct cursor_image *img)
+{
+	if (!intel_probe_ok || !intel_cursor_phys)
+		return;
+	intel_fill_cursor(intel_cursor_phys, img);
+	/* Re-arm CURBASE so the plane fetches the new image. */
+	if (intel_cursor_ok) {
+		if (intel_pipe_mask & 1)
+			mmio_write(CURABASE, intel_cursor_gtt);
+		if (intel_pipe_mask & 2)
+			mmio_write(CURABASE + CUR_PIPE_B, intel_cursor_gtt);
+	}
+}
+
+void intel_cursor_move(int x, int y)
 {
 {
 	unsigned int pos;
 	unsigned int pos;
 
 
 	if (!intel_cursor_ok)
 	if (!intel_cursor_ok)
 		return;
 		return;
-	pos = (x & 0x7FFFu) | ((y & 0x7FFFu) << 16);
+	pos = intel_cursor_pos(x, y);
 	if (intel_pipe_mask & 1) {
 	if (intel_pipe_mask & 1) {
 		mmio_write(CURAPOS, pos);
 		mmio_write(CURAPOS, pos);
 		mmio_write(CURABASE, intel_cursor_gtt);
 		mmio_write(CURABASE, intel_cursor_gtt);

+ 11 - 5
kernel/intel.h

@@ -9,11 +9,17 @@ void intel_init(unsigned int bus, unsigned int dev, unsigned int device_id);
 /* 1 when the hardware cursor is fully set up and usable. */
 /* 1 when the hardware cursor is fully set up and usable. */
 int intel_available(void);
 int intel_available(void);
 
 
-/* Enable the cursor at (x,y); returns 0 on success and -1 when the cursor
- * image/GTT was not prepared (the caller then keeps the software cursor). */
-int intel_cursor_setup(unsigned int x, unsigned int y);
+struct cursor_image;
 
 
-/* Move the hardware cursor. */
-void intel_cursor_move(unsigned int x, unsigned int y);
+/* Enable the cursor with its image's top-left at (x,y) (may be negative);
+ * returns 0 on success and -1 when the cursor image/GTT was not prepared
+ * (the caller then keeps the software cursor). */
+int intel_cursor_setup(int x, int y);
+
+/* Copy a cursor image into the 64x64 plane (top-left aligned). */
+void intel_cursor_load(const struct cursor_image *img);
+
+/* Move the hardware cursor: (x,y) is the image's top-left. */
+void intel_cursor_move(int x, int y);
 
 
 #endif
 #endif

+ 2 - 0
kernel/kernel.c

@@ -22,6 +22,7 @@
 #include "power.h"
 #include "power.h"
 #include "process.h"
 #include "process.h"
 #include "ramfs.h"
 #include "ramfs.h"
+#include "cursor.h"
 #include "rndis.h"
 #include "rndis.h"
 #include "rtl8821au.h"
 #include "rtl8821au.h"
 #include "sched.h"
 #include "sched.h"
@@ -547,6 +548,7 @@ void kernel_main(void)
 				serial_puts(SERIAL_COM1, " files, ");
 				serial_puts(SERIAL_COM1, " files, ");
 				serial_putdec(SERIAL_COM1, ramfs_pack_size());
 				serial_putdec(SERIAL_COM1, ramfs_pack_size());
 				serial_puts(SERIAL_COM1, " bytes\n");
 				serial_puts(SERIAL_COM1, " bytes\n");
+				cursor_load();
 				if (!gz) {
 				if (!gz) {
 					/* ramfs has its own copy now; keep that
 					/* ramfs has its own copy now; keep that
 					 * as the self-update image. */
 					 * as the self-update image. */

+ 219 - 50
kernel/mouse.c

@@ -1,7 +1,7 @@
 #include "mouse.h"
 #include "mouse.h"
 #include "bootinfo.h"
 #include "bootinfo.h"
 #include "cirrus.h"
 #include "cirrus.h"
-#include "cursor_shape.h"
+#include "cursor.h"
 #include "intel.h"
 #include "intel.h"
 #include "radeon.h"
 #include "radeon.h"
 #include "timer.h"
 #include "timer.h"
@@ -20,9 +20,6 @@
 #define MOUSE_ACK     0xFA
 #define MOUSE_ACK     0xFA
 #define MOUSE_RESEND  0xFE
 #define MOUSE_RESEND  0xFE
 
 
-#define MOUSE_CUR_W CURSOR_SHAPE_WIDTH
-#define MOUSE_CUR_H CURSOR_SHAPE_HEIGHT
-
 /* One PS/2 wheel notch in scroll pixels; touchpads emit pixels directly. */
 /* One PS/2 wheel notch in scroll pixels; touchpads emit pixels directly. */
 #define MOUSE_WHEEL_PX 48
 #define MOUSE_WHEEL_PX 48
 #define MOUSE_SCROLL_MAX 100000
 #define MOUSE_SCROLL_MAX 100000
@@ -42,7 +39,11 @@ static int mouse_aux_irq_ok;        /* controller config accepted AUX IRQ */
 static unsigned int fbw, fbh;
 static unsigned int fbw, fbh;
 static unsigned int cur_x, cur_y;
 static unsigned int cur_x, cur_y;
 static unsigned int saved_x, saved_y, saved_w, saved_h;
 static unsigned int saved_x, saved_y, saved_w, saved_h;
-static unsigned char cur_saved[MOUSE_CUR_W * MOUSE_CUR_H * 4];
+static int saved_ox, saved_oy;       /* unclipped image origin of the save */
+static unsigned char cur_saved[CURSOR_MAX_DIM * CURSOR_MAX_DIM * 4];
+static struct cursor_image cur_img;  /* the frame on screen */
+static int cur_shape = CURSOR_DEFAULT;
+static unsigned int cur_frame, cur_frame_ms;
 static unsigned char pkt[4];
 static unsigned char pkt[4];
 static unsigned int pkt_idx;
 static unsigned int pkt_idx;
 
 
@@ -167,21 +168,34 @@ static int mouse_send_cmd_ack(unsigned char cmd)
 	return r;
 	return r;
 }
 }
 
 
-enum {
-	MOUSE_PIX_TRANSPARENT = 0,
-	MOUSE_PIX_OUTLINE,
-	MOUSE_PIX_FILL
-};
-
-static int mouse_pixel_at(int r, int c)
-{
-	if (r < 0 || c < 0 || r >= (int)MOUSE_CUR_H || c >= (int)MOUSE_CUR_W)
-		return MOUSE_PIX_TRANSPARENT;
-	if (cursor_shape_fill[(unsigned int)r] & (0x8000u >> c))
-		return MOUSE_PIX_FILL;
-	if (cursor_shape_outline[(unsigned int)r] & (0x8000u >> c))
-		return MOUSE_PIX_OUTLINE;
-	return MOUSE_PIX_TRANSPARENT;
+/* ---- Cursor image ---------------------------------------------------------
+ *
+ * cur_x/cur_y are the hotspot.  The image (kernel/cursor.c: an Adwaita
+ * shape or the built-in arrow) is drawn with its top-left at
+ * (cur_x - hot_x, cur_y - hot_y), so near the top/left edge part of it is
+ * off-screen.  The software cursor blends the premultiplied ARGB over the
+ * saved background; the hardware backends take the same image. */
+
+static void cursor_calc_rect(unsigned int *rx, unsigned int *ry,
+			     unsigned int *rw, unsigned int *rh)
+{
+	int ox = (int)cur_x - (int)cur_img.hot_x;
+	int oy = (int)cur_y - (int)cur_img.hot_y;
+	int x0 = ox, y0 = oy;
+	int x1 = ox + (int)cur_img.w, y1 = oy + (int)cur_img.h;
+
+	if (x0 < 0) x0 = 0;
+	if (y0 < 0) y0 = 0;
+	if (x1 > (int)fbw) x1 = (int)fbw;
+	if (y1 > (int)fbh) y1 = (int)fbh;
+	if (x1 <= x0 || y1 <= y0) {
+		*rx = *ry = *rw = *rh = 0;
+		return;
+	}
+	*rx = (unsigned int)x0;
+	*ry = (unsigned int)y0;
+	*rw = (unsigned int)(x1 - x0);
+	*rh = (unsigned int)(y1 - y0);
 }
 }
 
 
 static void cursor_save_under(void)
 static void cursor_save_under(void)
@@ -192,10 +206,9 @@ static void cursor_save_under(void)
 		saved_w = saved_h = 0;
 		saved_w = saved_h = 0;
 		return;
 		return;
 	}
 	}
-	saved_x = cur_x;
-	saved_y = cur_y;
-	saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
-	saved_h = (cur_y + MOUSE_CUR_H <= fbh) ? MOUSE_CUR_H : fbh - cur_y;
+	cursor_calc_rect(&saved_x, &saved_y, &saved_w, &saved_h);
+	saved_ox = (int)cur_x - (int)cur_img.hot_x;
+	saved_oy = (int)cur_y - (int)cur_img.hot_y;
 	if (saved_w && saved_h)
 	if (saved_w && saved_h)
 		vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
 		vga_fb_read_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
 }
 }
@@ -215,22 +228,172 @@ static void cursor_present(void)
 		vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
 		vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
 }
 }
 
 
-static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
+/* Blend the current image over the background saved by cursor_save_under()
+ * (which must have run for the same position and image). */
+static void mouse_draw_cursor(void)
 {
 {
+	unsigned int pb = vga_fb_bpp() == 16 ? 2u : vga_fb_bpp() == 24 ? 3u : 4u;
 	unsigned int r, c;
 	unsigned int r, c;
 
 
-	/* White arrow body with a black outline around it. */
-	for (r = 0; r < MOUSE_CUR_H; r++) {
-		for (c = 0; c < MOUSE_CUR_W; c++) {
-			int px = mouse_pixel_at((int)r, (int)c);
-			if (px == MOUSE_PIX_FILL)
-				vga_fb_put_pixel(x0 + c, y0 + r, 255, 255, 255);
-			else if (px == MOUSE_PIX_OUTLINE)
-				vga_fb_put_pixel(x0 + c, y0 + r, 0, 0, 0);
+	if (!cur_img.px)
+		return;
+	for (r = 0; r < saved_h; r++) {
+		unsigned int iy = saved_y + r - (unsigned int)saved_oy;
+		const unsigned int *src = cur_img.px + iy * cur_img.w +
+			(saved_x - (unsigned int)saved_ox);
+
+		for (c = 0; c < saved_w; c++) {
+			unsigned int s = src[c];
+			unsigned int a = s >> 24;
+			unsigned int sr = (s >> 16) & 0xFF, sg = (s >> 8) & 0xFF;
+			unsigned int sb = s & 0xFF;
+
+			if (a == 0)
+				continue;
+			if (a != 255) {
+				const unsigned char *d =
+					cur_saved + (r * saved_w + c) * pb;
+				unsigned int dr, dg, db, inv = 255 - a;
+
+				if (pb == 2) {
+					unsigned int v = d[0] | (d[1] << 8);
+
+					dr = ((v >> 11) & 0x1F) * 255 / 31;
+					dg = ((v >> 5) & 0x3F) * 255 / 63;
+					db = (v & 0x1F) * 255 / 31;
+				} else {
+					db = d[0];
+					dg = d[1];
+					dr = d[2];
+				}
+				/* Premultiplied source over the background. */
+				sr += (dr * inv + 127) / 255;
+				sg += (dg * inv + 127) / 255;
+				sb += (db * inv + 127) / 255;
+				if (sr > 255) sr = 255;
+				if (sg > 255) sg = 255;
+				if (sb > 255) sb = 255;
+			}
+			vga_fb_put_pixel(saved_x + c, saved_y + r, sr, sg, sb);
 		}
 		}
 	}
 	}
 }
 }
 
 
+/* Hardware cursor position: the image's top-left, may be negative. */
+static void cursor_hw_move(void)
+{
+	int ox = (int)cur_x - (int)cur_img.hot_x;
+	int oy = (int)cur_y - (int)cur_img.hot_y;
+
+	if (mouse_hw_cursor == 3)
+		intel_cursor_move(ox, oy);
+	else if (mouse_hw_cursor == 2)
+		radeon_cursor_move(ox, oy);
+	else if (mouse_hw_cursor == 1)
+		cirrus_cursor_move(ox, oy);
+}
+
+static void cursor_hw_load(void)
+{
+	if (mouse_hw_cursor == 3)
+		intel_cursor_load(&cur_img);
+	else if (mouse_hw_cursor == 2)
+		radeon_cursor_load(&cur_img);
+	else if (mouse_hw_cursor == 1)
+		cirrus_cursor_load(&cur_img);
+}
+
+/* Switch to `img`: re-upload the hardware image, or erase and redraw the
+ * software cursor (presenting the union of the old and new rectangles in
+ * one go, so a shape change does not blink). */
+static void cursor_set_image(const struct cursor_image *img)
+{
+	unsigned int ox, oy, ow, oh;
+
+	if (!mouse_cursor_flag) {
+		cur_img = *img;
+		return;
+	}
+	if (mouse_hw_cursor) {
+		unsigned int flags = mouse_irq_save();
+
+		cur_img = *img;
+		cursor_hw_load();
+		cursor_hw_move();
+		mouse_irq_restore(flags);
+		return;
+	}
+	if (!mouse_auto_draw) {
+		/* A WM frame owns the shadow; PRESENT redraws the cursor. */
+		cur_img = *img;
+		return;
+	}
+	cursor_restore_under();
+	ox = saved_x; oy = saved_y; ow = saved_w; oh = saved_h;
+	cur_img = *img;
+	cursor_save_under();
+	mouse_draw_cursor();
+	if (ow && oh && saved_w && saved_h) {
+		unsigned int x0 = ox < saved_x ? ox : saved_x;
+		unsigned int y0 = oy < saved_y ? oy : saved_y;
+		unsigned int x1 = ox + ow > saved_x + saved_w ?
+				  ox + ow : saved_x + saved_w;
+		unsigned int y1 = oy + oh > saved_y + saved_h ?
+				  oy + oh : saved_y + saved_h;
+
+		vga_fb_present_rect(x0, y0, x1 - x0, y1 - y0);
+	} else {
+		if (ow && oh)
+			vga_fb_present_rect(ox, oy, ow, oh);
+		cursor_present();
+	}
+}
+
+static void cursor_select(int shape, unsigned int frame)
+{
+	struct cursor_image img;
+
+	cursor_get(shape, frame, &img);
+	cursor_set_image(&img);
+}
+
+void mouse_set_shape(int shape)
+{
+	if (shape < 0 || shape >= CURSOR_COUNT)
+		shape = CURSOR_DEFAULT;
+	if (shape == cur_shape)
+		return;
+	cur_shape = shape;
+	cur_frame = 0;
+	cur_frame_ms = get_ticks_ms();
+	cursor_select(cur_shape, 0);
+}
+
+int mouse_get_shape(void)
+{
+	return cur_shape;
+}
+
+void mouse_cursor_reload(void)
+{
+	cursor_select(cur_shape, cur_frame);
+}
+
+/* Advance an animated cursor (wait/progress) from the poll loop. */
+static void mouse_cursor_tick(void)
+{
+	unsigned int now;
+
+	if (!mouse_cursor_flag || cur_img.frames <= 1)
+		return;
+	now = get_ticks_ms();
+	if (now - cur_frame_ms < cur_img.delay_ms)
+		return;
+	cur_frame_ms = now;
+	cur_frame = (cur_frame + 1) % cur_img.frames;
+	cursor_select(cur_shape, cur_frame);
+}
+
 static void mouse_clamp_ptr(long *nx, long *ny)
 static void mouse_clamp_ptr(long *nx, long *ny)
 {
 {
 	long maxx = (long)fbw - 1;
 	long maxx = (long)fbw - 1;
@@ -257,8 +420,10 @@ static void mouse_cursor_setup(void)
 		return;
 		return;
 	fbw = vga_fb_width();
 	fbw = vga_fb_width();
 	fbh = vga_fb_height();
 	fbh = vga_fb_height();
-	if (fbw < MOUSE_CUR_W || fbh < MOUSE_CUR_H)
+	if (fbw < CURSOR_MAX_DIM || fbh < CURSOR_MAX_DIM)
 		return;
 		return;
+	cursor_get(cur_shape, cur_frame, &cur_img);
+	cur_frame_ms = get_ticks_ms();
 
 
 	/* Cirrus hardware cursor: the card overlays it; no save/restore.
 	/* Cirrus hardware cursor: the card overlays it; no save/restore.
 	 * UEFI GOP modes are not programmed through the legacy VGA sequencer,
 	 * UEFI GOP modes are not programmed through the legacy VGA sequencer,
@@ -267,8 +432,8 @@ static void mouse_cursor_setup(void)
 		mouse_hw_cursor = 1;
 		mouse_hw_cursor = 1;
 		cur_x = 100;
 		cur_x = 100;
 		cur_y = 100;
 		cur_y = 100;
-		cirrus_cursor_setup();
-		cirrus_cursor_move(cur_x, cur_y);
+		cirrus_cursor_setup(&cur_img);
+		cursor_hw_move();
 		mouse_cursor_flag = 1;
 		mouse_cursor_flag = 1;
 		return;
 		return;
 	}
 	}
@@ -278,8 +443,8 @@ static void mouse_cursor_setup(void)
 		mouse_hw_cursor = 2;
 		mouse_hw_cursor = 2;
 		cur_x = 100;
 		cur_x = 100;
 		cur_y = 100;
 		cur_y = 100;
-		radeon_cursor_setup();
-		radeon_cursor_move(cur_x, cur_y);
+		radeon_cursor_setup(&cur_img);
+		cursor_hw_move();
 		mouse_cursor_flag = 1;
 		mouse_cursor_flag = 1;
 		return;
 		return;
 	}
 	}
@@ -294,7 +459,9 @@ static void mouse_cursor_setup(void)
 		mouse_clamp_ptr(&nx, &ny);
 		mouse_clamp_ptr(&nx, &ny);
 		cur_x = (unsigned int)nx;
 		cur_x = (unsigned int)nx;
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
-		if (intel_cursor_setup(cur_x, cur_y) == 0) {
+		intel_cursor_load(&cur_img);
+		if (intel_cursor_setup((int)cur_x - (int)cur_img.hot_x,
+				       (int)cur_y - (int)cur_img.hot_y) == 0) {
 			mouse_hw_cursor = 3;
 			mouse_hw_cursor = 3;
 			mouse_cursor_flag = 1;
 			mouse_cursor_flag = 1;
 			return;
 			return;
@@ -316,7 +483,7 @@ static void mouse_cursor_setup(void)
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
 	}
 	}
 	cursor_save_under();
 	cursor_save_under();
-	mouse_draw_cursor(cur_x, cur_y);
+	mouse_draw_cursor();
 	cursor_present();
 	cursor_present();
 	mouse_cursor_flag = 1;
 	mouse_cursor_flag = 1;
 }
 }
@@ -410,12 +577,7 @@ static void mouse_move(int dx, int dy)
 
 
 		cur_x = (unsigned int)nx;
 		cur_x = (unsigned int)nx;
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
-		if (mouse_hw_cursor == 3)
-			intel_cursor_move(cur_x, cur_y);
-		else if (mouse_hw_cursor == 2)
-			radeon_cursor_move(cur_x, cur_y);
-		else
-			cirrus_cursor_move(cur_x, cur_y);
+		cursor_hw_move();
 		mouse_irq_restore(flags);
 		mouse_irq_restore(flags);
 	} else if (!mouse_auto_draw) {
 	} else if (!mouse_auto_draw) {
 		/* A WM frame is between BEGIN and PRESENT: it erased the
 		/* A WM frame is between BEGIN and PRESENT: it erased the
@@ -428,7 +590,7 @@ static void mouse_move(int dx, int dy)
 		cur_x = (unsigned int)nx;
 		cur_x = (unsigned int)nx;
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
 		cursor_save_under();
 		cursor_save_under();
-		mouse_draw_cursor(cur_x, cur_y);
+		mouse_draw_cursor();
 		cursor_present();          /* draw the new position in VRAM */
 		cursor_present();          /* draw the new position in VRAM */
 	}
 	}
 }
 }
@@ -508,6 +670,7 @@ void mouse_poll_update(void)
 	if ((mouse_present_flag || usbhid_mouse_attached() ||
 	if ((mouse_present_flag || usbhid_mouse_attached() ||
 	     touchpad_present()) && !mouse_cursor_flag)
 	     touchpad_present()) && !mouse_cursor_flag)
 		mouse_cursor_setup();
 		mouse_cursor_setup();
+	mouse_cursor_tick();
 
 
 	/* USB HID mouse: reports arrive through the armed interrupt
 	/* USB HID mouse: reports arrive through the armed interrupt
 	   endpoint; the backend also handles hotplug/detach. */
 	   endpoint; the backend also handles hotplug/detach. */
@@ -632,10 +795,16 @@ void mouse_redraw_cursor(void)
 	 * rectangle is still the old position and has been restored to the
 	 * rectangle is still the old position and has been restored to the
 	 * background there.  Erase it in VRAM before drawing the new one, so
 	 * background there.  Erase it in VRAM before drawing the new one, so
 	 * a moving cursor does not leave a trail behind a slow frame. */
 	 * a moving cursor does not leave a trail behind a slow frame. */
-	if ((saved_x != cur_x || saved_y != cur_y) && saved_w && saved_h)
-		vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
+	if (saved_w && saved_h) {
+		unsigned int nx, ny, nw, nh;
+
+		cursor_calc_rect(&nx, &ny, &nw, &nh);
+		if (nx != saved_x || ny != saved_y || nw != saved_w ||
+		    nh != saved_h)
+			vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
+	}
 	cursor_save_under();
 	cursor_save_under();
-	mouse_draw_cursor(cur_x, cur_y);
+	mouse_draw_cursor();
 	cursor_present();
 	cursor_present();
 }
 }
 
 

+ 8 - 0
kernel/mouse.h

@@ -39,4 +39,12 @@ void mouse_redraw_cursor(void);
  * WM erases/redraws the cursor itself at BEGIN/PRESENT. */
  * WM erases/redraws the cursor itself at BEGIN/PRESENT. */
 void mouse_set_auto_draw(int on);
 void mouse_set_auto_draw(int on);
 
 
+/* Cursor shape (enum cursor_shape, include/cursor_abi.h).  The WM server
+ * resolves the shape under the pointer on every poll; an unknown value
+ * selects CURSOR_DEFAULT.  Animated shapes advance from mouse_poll_update. */
+void mouse_set_shape(int shape);
+int mouse_get_shape(void);
+/* Re-fetch the current image after kernel/cursor.c (re)loaded the bundle. */
+void mouse_cursor_reload(void);
+
 #endif
 #endif

+ 49 - 35
kernel/radeon.c

@@ -22,7 +22,7 @@
  */
  */
 
 
 #include "radeon.h"
 #include "radeon.h"
-#include "cursor_shape.h"
+#include "cursor.h"
 #include "pci.h"
 #include "pci.h"
 #include "paging.h"
 #include "paging.h"
 #include "serial.h"
 #include "serial.h"
@@ -120,11 +120,30 @@ int radeon_available(void)
 	return radeon_present;
 	return radeon_present;
 }
 }
 
 
-void radeon_cursor_setup(void)
+#define RADEON_CUR_LOCK              (1u << 31)
+
+/* Copy a cursor image into the top-left of the 64x64 ARGB plane (the image
+ * is premultiplied ARGB8888, which the R200 cursor blends as-is). */
+void radeon_cursor_load(const struct cursor_image *img)
+{
+	unsigned int *dst;
+	unsigned int i, r, c;
+
+	if (!radeon_present)
+		return;
+	dst = (unsigned int *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
+	for (i = 0; i < RADEON_CUR_SIZE * RADEON_CUR_SIZE; i++)
+		dst[i] = 0;
+	if (!img || !img->px)
+		return;
+	for (r = 0; r < img->h && r < RADEON_CUR_SIZE; r++)
+		for (c = 0; c < img->w && c < RADEON_CUR_SIZE; c++)
+			dst[r * RADEON_CUR_SIZE + c] = img->px[r * img->w + c];
+}
+
+void radeon_cursor_setup(const struct cursor_image *img)
 {
 {
-	unsigned char *dst;
-	unsigned int vram;
-	unsigned int i, r, c, g;
+	unsigned int vram, g;
 
 
 	if (!radeon_present)
 	if (!radeon_present)
 		return;
 		return;
@@ -135,35 +154,9 @@ void radeon_cursor_setup(void)
 		return;
 		return;
 	}
 	}
 
 
-	dst = (unsigned char *)(vga_fb_lfb_addr() + RADEON_CUR_VRAM_OFS);
-
-	/* Transparent background (alpha 0), then the opaque white fill and
-	   black outline of the shared arrow. */
-	for (i = 0; i < RADEON_CUR_BYTES; i++)
-		dst[i] = 0;
-	for (r = 0; r < CURSOR_SHAPE_HEIGHT; r++) {
-		unsigned short fill = cursor_shape_fill[r];
-		unsigned short outl = cursor_shape_outline[r];
-		for (c = 0; c < CURSOR_SHAPE_WIDTH; c++) {
-			unsigned short bit = (unsigned short)(0x8000u >> c);
-			unsigned int off = (r * RADEON_CUR_SIZE + c) * 4;
-			if (fill & bit) {
-				dst[off + 0] = 0xFF;  /* B */
-				dst[off + 1] = 0xFF;  /* G */
-				dst[off + 2] = 0xFF;  /* R */
-			} else if (outl & bit) {
-				dst[off + 0] = 0x00;  /* B */
-				dst[off + 1] = 0x00;  /* G */
-				dst[off + 2] = 0x00;  /* R */
-			} else {
-				continue;
-			}
-			dst[off + 3] = 0xFF;          /* A */
-		}
-	}
-
+	radeon_cursor_load(img);
 	mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS);
 	mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS);
-	mmio_write(RADEON_CUR_HORZ_VERT_OFF, 0);    /* hotspot at (0,0) */
+	mmio_write(RADEON_CUR_HORZ_VERT_OFF, 0);
 	mmio_write(RADEON_CUR_HORZ_VERT_POSN, 0);   /* position set on move */
 	mmio_write(RADEON_CUR_HORZ_VERT_POSN, 0);   /* position set on move */
 
 
 	/* Enable the 24 bpp (ARGB8888) hardware cursor. */
 	/* Enable the 24 bpp (ARGB8888) hardware cursor. */
@@ -173,11 +166,32 @@ void radeon_cursor_setup(void)
 	mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
 	mmio_mm_write(RADEON_CRTC_GEN_CNTL, g);
 }
 }
 
 
-void radeon_cursor_move(unsigned int x, unsigned int y)
+/* (x,y) is the image's top-left.  The position register cannot go
+ * negative: like Linux's legacy radeon_cursor_move_locked(), clamp it at 0
+ * and skip the hidden part through HORZ_VERT_OFF (x) and a CUR_OFFSET that
+ * starts yorigin rows (256 bytes each) into the image. */
+void radeon_cursor_move(int x, int y)
 {
 {
+	unsigned int xo = 0, yo = 0;
+
 	if (!radeon_present)
 	if (!radeon_present)
 		return;
 		return;
-	mmio_write(RADEON_CUR_HORZ_VERT_POSN, (x << 16) | y);
+	if (x < 0) {
+		xo = (unsigned int)-x;
+		if (xo > RADEON_CUR_SIZE - 1)
+			xo = RADEON_CUR_SIZE - 1;
+		x = 0;
+	}
+	if (y < 0) {
+		yo = (unsigned int)-y;
+		if (yo > RADEON_CUR_SIZE - 1)
+			yo = RADEON_CUR_SIZE - 1;
+		y = 0;
+	}
+	mmio_write(RADEON_CUR_HORZ_VERT_OFF, RADEON_CUR_LOCK | (xo << 16) | yo);
+	mmio_write(RADEON_CUR_HORZ_VERT_POSN, RADEON_CUR_LOCK |
+		   ((unsigned int)x << 16) | (unsigned int)y);
+	mmio_write(RADEON_CUR_OFFSET, RADEON_CUR_VRAM_OFS + yo * 256);
 }
 }
 
 
 void radeon_cursor_show(int show)
 void radeon_cursor_show(int show)

+ 6 - 3
kernel/radeon.h

@@ -6,9 +6,12 @@
 void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id);
 void radeon_init(unsigned int bus, unsigned int dev, unsigned int device_id);
 int  radeon_available(void);
 int  radeon_available(void);
 
 
-/* Hardware cursor (64x64, 2 bpp). */
-void radeon_cursor_setup(void);
-void radeon_cursor_move(unsigned int x, unsigned int y);
+/* Hardware cursor (64x64 ARGB8888). */
+struct cursor_image;
+void radeon_cursor_setup(const struct cursor_image *img);
+void radeon_cursor_load(const struct cursor_image *img);
+/* (x,y) is the image's top-left and may be negative. */
+void radeon_cursor_move(int x, int y);
 void radeon_cursor_show(int show);
 void radeon_cursor_show(int show);
 
 
 #endif
 #endif

+ 3 - 0
kernel/ramfs.c

@@ -9,6 +9,7 @@
 #include "inflate.h"
 #include "inflate.h"
 #include "mm.h"
 #include "mm.h"
 #include "serial.h"
 #include "serial.h"
+#include "cursor.h"
 
 
 #define RAMFS_MAX_DECOMPRESSED (8u * 1024u * 1024u)
 #define RAMFS_MAX_DECOMPRESSED (8u * 1024u * 1024u)
 
 
@@ -279,6 +280,8 @@ int ramfs_replace(const void *pack, unsigned int size)
 	for (i = 0; i < old_pages; i++)
 	for (i = 0; i < old_pages; i++)
 		mm_free_page((unsigned int)old_base + i * 0x1000);
 		mm_free_page((unsigned int)old_base + i * 0x1000);
 	(void)old_size;
 	(void)old_size;
+	/* The new pack may carry different cursors. */
+	cursor_load();
 	return 0;
 	return 0;
 }
 }
 
 

+ 10 - 0
kernel/sys_wm.c

@@ -285,3 +285,13 @@ int sys_win_resize_self(unsigned int w, unsigned int h)
 {
 {
 	return wm_server_win_resize_self(w, h);
 	return wm_server_win_resize_self(w, h);
 }
 }
+
+int sys_win_set_cursor(int shape)
+{
+	return wm_server_win_set_cursor(shape);
+}
+
+int sys_wm_set_cursor(int shape)
+{
+	return wm_server_set_cursor(shape);
+}

+ 2 - 0
kernel/sys_wm.h

@@ -29,6 +29,8 @@ int sys_win_scroll(struct win_scroll *out);
 int sys_wm_resize(unsigned int index, const struct wm_size *in);
 int sys_wm_resize(unsigned int index, const struct wm_size *in);
 int sys_win_key_wait(unsigned int timeout_ms);
 int sys_win_key_wait(unsigned int timeout_ms);
 int sys_win_resize_self(unsigned int w, unsigned int h);
 int sys_win_resize_self(unsigned int w, unsigned int h);
+int sys_win_set_cursor(int shape);
+int sys_wm_set_cursor(int shape);
 
 
 /* Set once the first application window (the boot terminal) has presented
 /* Set once the first application window (the boot terminal) has presented
  * a frame; kernel_main waits for it before the USB bring-up. */
  * a frame; kernel_main waits for it before the USB bring-up. */

+ 4 - 0
kernel/syscall.c

@@ -350,6 +350,10 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_win_key_wait((unsigned int)a1);
 		return sys_win_key_wait((unsigned int)a1);
 	case SYS_WIN_RESIZE_SELF:
 	case SYS_WIN_RESIZE_SELF:
 		return sys_win_resize_self((unsigned int)a1, (unsigned int)a2);
 		return sys_win_resize_self((unsigned int)a1, (unsigned int)a2);
+	case SYS_WIN_SET_CURSOR:
+		return sys_win_set_cursor((int)a1);
+	case SYS_WM_SET_CURSOR:
+		return sys_wm_set_cursor((int)a1);
 	case SYS_SCREEN_INFO: {
 	case SYS_SCREEN_INFO: {
 		struct sys_screen *out = (struct sys_screen *)a1;
 		struct sys_screen *out = (struct sys_screen *)a1;
 
 

+ 61 - 0
kernel/wm.c

@@ -2,6 +2,7 @@
  * mapping/presentation and input routing.  There is no in-kernel WM. */
  * mapping/presentation and input routing.  There is no in-kernel WM. */
 
 
 #include "wm.h"
 #include "wm.h"
+#include "cursor_abi.h"
 #include "backlight.h"
 #include "backlight.h"
 #include "bootcon.h"
 #include "bootcon.h"
 #include "io.h"
 #include "io.h"
@@ -33,6 +34,9 @@ static unsigned int wm_next_content_gen(void)
  * called by the ring-3 WM every loop, so it is the cheapest place to turn the
  * called by the ring-3 WM every loop, so it is the cheapest place to turn the
  * sampled mouse state into press/release edges. */
  * sampled mouse state into press/release edges. */
 static unsigned int wm_ptr_buttons;
 static unsigned int wm_ptr_buttons;
+/* SYS_WM_SET_CURSOR: the WM's shape for its own chrome (resize grip,
+ * title-bar drag, launchers), or CURSOR_NO_OVERRIDE. */
+static int wm_cursor_override = CURSOR_NO_OVERRIDE;
 static int wm_ptr_capture = -1;
 static int wm_ptr_capture = -1;
 
 
 /* Ring-3 WM lifecycle.  The kernel only admits the process it spawned; a
 /* Ring-3 WM lifecycle.  The kernel only admits the process it spawned; a
@@ -272,6 +276,7 @@ int wm_server_win_create(const struct win_create *in, struct win_info *out)
 	w->pending_pages = 0;
 	w->pending_pages = 0;
 	w->resize_pending = 0;
 	w->resize_pending = 0;
 	w->scroll_x = w->scroll_y = 0;
 	w->scroll_x = w->scroll_y = 0;
+	w->cursor = CURSOR_DEFAULT;
 	w->canvas_dmg_x = w->canvas_dmg_y = 0;
 	w->canvas_dmg_x = w->canvas_dmg_y = 0;
 	w->canvas_dmg_w = w->canvas_dmg_h = 0;
 	w->canvas_dmg_w = w->canvas_dmg_h = 0;
 	w->pw = cw + pad * 2;
 	w->pw = cw + pad * 2;
@@ -592,6 +597,59 @@ int wm_server_win_resize_self(unsigned int cw, unsigned int ch)
 	return wm_resize_window((unsigned int)idx, cw, ch);
 	return wm_resize_window((unsigned int)idx, cw, ch);
 }
 }
 
 
+int wm_server_win_set_cursor(int shape)
+{
+	int idx = wm_window_for_pid(current_proc()->pid);
+
+	if (idx < 0 || shape < 0 || shape >= CURSOR_COUNT)
+		return -1;
+	windows[idx].cursor = shape;
+	return 0;
+}
+
+int wm_server_set_cursor(int shape)
+{
+	if (!wm_server_is_wm())
+		return -1;
+	if (shape != CURSOR_NO_OVERRIDE && (shape < 0 || shape >= CURSOR_COUNT))
+		return -1;
+	wm_cursor_override = shape;
+	return 0;
+}
+
+/* The shape for the pointer at (mx,my): the WM's override, else the
+ * content cursor of the window that holds the pointer capture (a drag that
+ * started in the content keeps its cursor), else of the window whose
+ * content is under the pointer (not on a normal window's resize grip),
+ * else the default arrow (title bars, the desktop, the top bar). */
+static int wm_cursor_at(unsigned int mx, unsigned int my)
+{
+	int idx;
+	struct window *w;
+	int cx, cy;
+
+	if (wm_cursor_override != CURSOR_NO_OVERRIDE)
+		return wm_cursor_override;
+	if (wm_ptr_capture >= 0 && windows[wm_ptr_capture].used &&
+	    windows[wm_ptr_capture].canvas_window)
+		return windows[wm_ptr_capture].cursor;
+	idx = wm_window_at((int)mx, (int)my);
+	if (idx < 0)
+		return CURSOR_DEFAULT;
+	w = &windows[idx];
+	if (!w->canvas_window)
+		return CURSOR_DEFAULT;
+	cx = (int)mx - win_content_x(w);
+	cy = (int)my - win_content_y(w);
+	if (cx < 0 || cy < 0 || cx >= (int)w->canvas.w ||
+	    cy >= (int)w->canvas.h)
+		return CURSOR_DEFAULT;
+	if (!w->popover && (int)mx >= w->x + (int)w->pw - 16 &&
+	    (int)my >= w->y + (int)w->ph - 16)
+		return CURSOR_DEFAULT;
+	return w->cursor;
+}
+
 int wm_server_key_poll(void)
 int wm_server_key_poll(void)
 {
 {
 	int idx = wm_window_for_pid(current_proc()->pid);
 	int idx = wm_window_for_pid(current_proc()->pid);
@@ -982,6 +1040,7 @@ int wm_server_poll(struct wm_status *out)
 	out->mouse.buttons = mouse_buttons_state();
 	out->mouse.buttons = mouse_buttons_state();
 	wm_route_pointer(out->mouse.x, out->mouse.y, out->mouse.buttons);
 	wm_route_pointer(out->mouse.x, out->mouse.y, out->mouse.buttons);
 	wm_route_scroll(out->mouse.x, out->mouse.y);
 	wm_route_scroll(out->mouse.x, out->mouse.y);
+	mouse_set_shape(wm_cursor_at(out->mouse.x, out->mouse.y));
 	out->full_redraw = wm_full_redraw ? 1 : 0;
 	out->full_redraw = wm_full_redraw ? 1 : 0;
 	out->boot_ready = wm_boot_ready ? 1 : 0;
 	out->boot_ready = wm_boot_ready ? 1 : 0;
 	out->kexec_busy = kexec_busy ? 1 : 0;
 	out->kexec_busy = kexec_busy ? 1 : 0;
@@ -1074,6 +1133,8 @@ static void wm_server_wm_died(void)
 	wm_cr3 = 0;
 	wm_cr3 = 0;
 	wm_ptr_buttons = 0;
 	wm_ptr_buttons = 0;
 	wm_ptr_capture = -1;
 	wm_ptr_capture = -1;
+	wm_cursor_override = CURSOR_NO_OVERRIDE;
+	mouse_set_shape(CURSOR_DEFAULT);
 	for (i = 0; i < MAX_WINDOWS; i++) {
 	for (i = 0; i < MAX_WINDOWS; i++) {
 		windows[i].mapped_kind = WM_KIND_NONE;
 		windows[i].mapped_kind = WM_KIND_NONE;
 		windows[i].mapped_gen = 0;
 		windows[i].mapped_gen = 0;

+ 5 - 0
kernel/wm.h

@@ -39,6 +39,9 @@ struct window {
 	unsigned int ptr_head, ptr_tail;
 	unsigned int ptr_head, ptr_tail;
 	/* Scroll accumulated while the pointer is over the content. */
 	/* Scroll accumulated while the pointer is over the content. */
 	int scroll_x, scroll_y;
 	int scroll_x, scroll_y;
+	/* Cursor shape over the content (enum cursor_shape), set by the app
+	 * with SYS_WIN_SET_CURSOR. */
+	int cursor;
 	/* Content-relative damage union since the WM last ACKed a present. */
 	/* Content-relative damage union since the WM last ACKed a present. */
 	unsigned int canvas_dmg_x, canvas_dmg_y;
 	unsigned int canvas_dmg_x, canvas_dmg_y;
 	unsigned int canvas_dmg_w, canvas_dmg_h;
 	unsigned int canvas_dmg_w, canvas_dmg_h;
@@ -87,6 +90,8 @@ int  wm_server_win_pointer(struct win_pointer *out);
 int  wm_server_win_event(struct win_event *out);
 int  wm_server_win_event(struct win_event *out);
 int  wm_server_win_scroll(struct win_scroll *out);
 int  wm_server_win_scroll(struct win_scroll *out);
 int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
 int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
+int  wm_server_win_set_cursor(int shape);
+int  wm_server_set_cursor(int shape);
 void wm_server_boot_ready(void);
 void wm_server_boot_ready(void);
 struct window *wm_focused_window(void);
 struct window *wm_focused_window(void);
 int  wm_is_focused(const struct window *w);
 int  wm_is_focused(const struct window *w);

+ 104 - 0
tools/cursor_test.c

@@ -0,0 +1,104 @@
+/* Host test for the NBCUR1 cursor bundle built by mkcursors
+ * (make test-cursors): structure, a few Adwaita hotspots, the subsampled
+ * spinner, and that every frame has visible pixels. */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "cursor_abi.h"
+
+static int fails;
+
+#define CHECK(c, ...) do { if (!(c)) { fails++; \
+	fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \
+	fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0)
+
+int main(int argc, char **argv)
+{
+	const char *path = argc > 1 ? argv[1] : "cursors/adwaita24.cur";
+	FILE *f = fopen(path, "rb");
+	unsigned char *buf;
+	long size;
+	const struct cursor_file_hdr *h;
+	const struct cursor_file_shape *s;
+	unsigned int i, fr;
+
+	if (!f) {
+		perror(path);
+		return 1;
+	}
+	fseek(f, 0, SEEK_END);
+	size = ftell(f);
+	fseek(f, 0, SEEK_SET);
+	buf = malloc((size_t)size);
+	if (!buf || fread(buf, 1, (size_t)size, f) != (size_t)size)
+		return 1;
+	fclose(f);
+
+	h = (const struct cursor_file_hdr *)buf;
+	s = (const struct cursor_file_shape *)(buf + sizeof(*h));
+	CHECK(memcmp(h->magic, CURSOR_FILE_MAGIC, 8) == 0, "magic");
+	CHECK(h->count == CURSOR_COUNT, "count %u", h->count);
+	CHECK(h->nominal == 24, "nominal %u", h->nominal);
+	if (fails)
+		return 1;
+
+	for (i = 0; i < h->count; i++) {
+		unsigned int px = (unsigned int)s[i].w * s[i].h;
+
+		CHECK(s[i].w == 24 && s[i].h == 24, "shape %u size %ux%u",
+		      i, s[i].w, s[i].h);
+		CHECK(s[i].hot_x < s[i].w && s[i].hot_y < s[i].h,
+		      "shape %u hotspot", i);
+		CHECK(s[i].frames >= 1, "shape %u frames", i);
+		CHECK((s[i].offset & 3) == 0 &&
+		      s[i].offset + px * 4u * s[i].frames <= (unsigned long)size,
+		      "shape %u range", i);
+		for (fr = 0; fr < s[i].frames; fr++) {
+			const unsigned int *p = (const unsigned int *)
+				(buf + s[i].offset) + fr * px;
+			unsigned int k, opaque = 0, bad = 0;
+
+			for (k = 0; k < px; k++) {
+				unsigned int a = p[k] >> 24;
+
+				if (a)
+					opaque++;
+				/* Premultiplied: no channel above alpha. */
+				if (((p[k] >> 16) & 0xFF) > a ||
+				    ((p[k] >> 8) & 0xFF) > a || (p[k] & 0xFF) > a)
+					bad++;
+			}
+			CHECK(opaque > 10, "shape %u frame %u is empty", i, fr);
+			CHECK(bad == 0, "shape %u frame %u not premultiplied", i,
+			      fr);
+		}
+	}
+	/* Hotspots from the Adwaita 51 Xcursor files. */
+	CHECK(s[CURSOR_DEFAULT].hot_x == 3 && s[CURSOR_DEFAULT].hot_y == 1,
+	      "default hotspot");
+	CHECK(s[CURSOR_TEXT].hot_x == 11 && s[CURSOR_TEXT].hot_y == 12,
+	      "text hotspot");
+	CHECK(s[CURSOR_POINTER].hot_x == 7 && s[CURSOR_POINTER].hot_y == 5,
+	      "pointer hotspot");
+	/* move is Adwaita's all-resize (its own `move` is a symlink to the
+	 * arrow). */
+	CHECK(s[CURSOR_MOVE].hot_x == 12 && s[CURSOR_MOVE].hot_y == 11,
+	      "move hotspot");
+	/* 60 frames x 16 ms, every 4th kept: 15 frames x 64 ms. */
+	CHECK(s[CURSOR_WAIT].frames == 15 && s[CURSOR_WAIT].delay_ms == 64,
+	      "wait %u frames x %u ms", s[CURSOR_WAIT].frames,
+	      s[CURSOR_WAIT].delay_ms);
+	CHECK(s[CURSOR_PROGRESS].frames == 15, "progress frames");
+	CHECK(s[CURSOR_DEFAULT].frames == 1 && s[CURSOR_DEFAULT].delay_ms == 0,
+	      "default is static");
+
+	i = h->count;
+	free(buf);
+	if (fails) {
+		fprintf(stderr, "cursor_test: %d failure(s)\n", fails);
+		return 1;
+	}
+	printf("cursor_test: %u shapes OK (%ld bytes)\n", i, size);
+	return 0;
+}

+ 184 - 0
tools/mkcursors.c

@@ -0,0 +1,184 @@
+/* mkcursors: build the NBCUR1 cursor bundle (include/cursor_abi.h) from an
+ * Xcursor theme directory.
+ *
+ *   ./mkcursors <theme-dir> <size> <out.cur>
+ *
+ * For every shape in CURSOR_LIST it reads <theme-dir>/<file>, takes the
+ * image chunks of nominal size <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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#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 <theme-dir> <size> <out.cur>\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;
+}

+ 67 - 0
tools/xcursor_trim.py

@@ -0,0 +1,67 @@
+#!/usr/bin/env python3
+"""Copy an Xcursor theme, keeping only the images of one nominal size.
+
+Usage: tools/xcursor_trim.py SRC_DIR DST_DIR [SIZE]
+
+Used once to import assets/cursors/adwaita/ from the host's adwaita-cursors
+package (/usr/share/icons/Adwaita/cursors).  Symlinks (legacy X11 aliases)
+are skipped; every regular file is rewritten as a valid Xcursor file that
+holds only the SIZE (default 24) image chunks, so the tree does not carry
+the 30..96 px images (12 MB for the whole theme, mostly the animated
+wait/progress spinners).  Comment chunks are dropped.
+"""
+import os
+import struct
+import sys
+
+XCUR_IMAGE = 0xFFFD0002
+
+
+def trim(data, size):
+    magic, hdr, version, ntoc = struct.unpack_from('<4sIII', data, 0)
+    if magic != b'Xcur':
+        raise ValueError('not an Xcursor file')
+    chunks = []
+    for i in range(ntoc):
+        typ, sub, pos = struct.unpack_from('<III', data, hdr + i * 12)
+        if typ != XCUR_IMAGE or sub != size:
+            continue
+        chdr, ctyp, csub, cver, w, h, xh, yh, delay = \
+            struct.unpack_from('<IIIIIIIII', data, pos)
+        end = pos + chdr + w * h * 4
+        chunks.append((typ, sub, data[pos:end]))
+    if not chunks:
+        raise ValueError('no %d px images' % size)
+    out = bytearray(struct.pack('<4sIII', b'Xcur', 16, version, len(chunks)))
+    pos = 16 + 12 * len(chunks)
+    toc = bytearray()
+    body = bytearray()
+    for typ, sub, raw in chunks:
+        toc += struct.pack('<III', typ, sub, pos + len(body))
+        body += raw
+    return bytes(out + toc + body)
+
+
+def main():
+    if len(sys.argv) < 3:
+        sys.exit(__doc__)
+    src, dst = sys.argv[1], sys.argv[2]
+    size = int(sys.argv[3]) if len(sys.argv) > 3 else 24
+    os.makedirs(dst, exist_ok=True)
+    for name in sorted(os.listdir(src)):
+        path = os.path.join(src, name)
+        if os.path.islink(path) or not os.path.isfile(path):
+            continue
+        with open(path, 'rb') as f:
+            data = f.read()
+        try:
+            out = trim(data, size)
+        except ValueError as e:
+            print('skip %s: %s' % (name, e))
+            continue
+        with open(os.path.join(dst, name), 'wb') as f:
+            f.write(out)
+
+
+if __name__ == '__main__':
+    main()

+ 39 - 0
user/apps/browser/browser.c

@@ -11857,6 +11857,44 @@ static void draw_status(struct ngui_rect r)
 	}
 	}
 }
 }
 
 
+/* Mouse cursor over the page: the hovered node's CSS `cursor` (inherited);
+ * for `auto`, a hand over links, the I-beam over text fields, and the
+ * progress arrow anywhere on the page while it is still loading.  The
+ * toolbar's URL field sets its own I-beam through ngui. */
+static void page_cursor(void)
+{
+	int hover = page_hover_node();
+	struct br_style st;
+
+	if (!hover) {
+		if (page_loading && ui.inside &&
+		    ngui_rect_contains(&page_rect, ui.mouse_x, ui.mouse_y))
+			ngui_set_cursor(&ui, CURSOR_PROGRESS);
+		return;
+	}
+	style_for_node((unsigned int)hover, &st);
+	if (st.cursor != BR_CURSOR_AUTO) {
+		ngui_set_cursor(&ui, (int)st.cursor - 1);
+		return;
+	}
+	if (br_href(&doc, (unsigned int)hover)) {
+		ngui_set_cursor(&ui, CURSOR_POINTER);
+		return;
+	}
+	if (form_is_control((unsigned int)hover)) {
+		int k = form_kind_of((unsigned int)hover);
+
+		if (form_disabled((unsigned int)hover))
+			ngui_set_cursor(&ui, CURSOR_DEFAULT);
+		else if (k == FORM_TEXT || k == FORM_PASSWORD ||
+			 k == FORM_TEXTAREA)
+			ngui_set_cursor(&ui, CURSOR_TEXT);
+		return;
+	}
+	if (page_loading)
+		ngui_set_cursor(&ui, CURSOR_PROGRESS);
+}
+
 static void draw(unsigned int *action)
 static void draw(unsigned int *action)
 {
 {
 	struct ngui_rect r = {0, 0, ui.w, ui.h};
 	struct ngui_rect r = {0, 0, ui.w, ui.h};
@@ -11870,6 +11908,7 @@ static void draw(unsigned int *action)
 	draw_toolbar(toolbar, action);
 	draw_toolbar(toolbar, action);
 	draw_page(page);
 	draw_page(page);
 	draw_status(status);
 	draw_status(status);
+	page_cursor();
 	if (sug_visible())
 	if (sug_visible())
 		draw_suggest(page);
 		draw_suggest(page);
 	else
 	else

+ 45 - 1
user/apps/browser/browser_html.c

@@ -308,6 +308,42 @@ static int ci_eq_n(const char *a, const char *b, unsigned int n)
 	return 1;
 	return 1;
 }
 }
 
 
+#define CURSOR_CSS_(e, css, file) css,
+static const char *const css_cursor_names[CURSOR_COUNT] = {
+	CURSOR_LIST(CURSOR_CSS_)
+};
+#undef CURSOR_CSS_
+
+/* CSS `cursor`: the keyword after the last comma (url() images before it
+ * are not supported, so the fallback keyword is what counts).  Returns
+ * BR_CURSOR_AUTO for auto, the shape + 1 for a known keyword, and -1 for
+ * anything else (the declaration is then ignored). */
+static int parse_cursor(const char *v, unsigned int n)
+{
+	unsigned int a = 0, b, i;
+
+	for (i = 0; i < n; i++)
+		if (v[i] == ',')
+			a = i + 1;
+	while (a < n && (v[a] == ' ' || v[a] == '\t' || v[a] == '\n'))
+		a++;
+	b = a;
+	while (b < n && ((v[b] >= 'a' && v[b] <= 'z') ||
+			 (v[b] >= 'A' && v[b] <= 'Z') || v[b] == '-'))
+		b++;
+	if (b == a)
+		return -1;
+	if (b - a == 4 && ci_eq_n(v + a, "auto", 4))
+		return BR_CURSOR_AUTO;
+	if (b - a == 4 && ci_eq_n(v + a, "none", 4))
+		return CURSOR_DEFAULT + 1;
+	for (i = 0; i < CURSOR_COUNT; i++)
+		if (strlen(css_cursor_names[i]) == b - a &&
+		    ci_eq_n(v + a, css_cursor_names[i], b - a))
+			return (int)i + 1;
+	return -1;
+}
+
 static int find_ci(const char *hay, unsigned int len, const char *needle,
 static int find_ci(const char *hay, unsigned int len, const char *needle,
 		   unsigned int nlen, unsigned int from)
 		   unsigned int nlen, unsigned int from)
 {
 {
@@ -2418,7 +2454,7 @@ enum {
 	PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
 	PROP_VIS_HIDDEN, PROP_OPACITY_HIDDEN, PROP_OVERFLOW_HIDDEN,
 	PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
 	PROP_FILL_MODE, PROP_FILL, PROP_STROKE_MODE, PROP_STROKE,
 	PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
 	PROP_STROKE_W, PROP_FILL_RULE, PROP_CONTENT,
-	PROP_ASPECT_W, PROP_ASPECT_H,
+	PROP_ASPECT_W, PROP_ASPECT_H, PROP_CURSOR,
 	PROP_COUNT
 	PROP_COUNT
 };
 };
 
 
@@ -4929,6 +4965,11 @@ static void parse_decls(struct br_doc *d, const char *s,
 			decl_add(out, nout, PROP_UNDERLINE, u, important);
 			decl_add(out, nout, PROP_UNDERLINE, u, important);
 			decl_add(out, nout, PROP_STRIKE, stv, important);
 			decl_add(out, nout, PROP_STRIKE, stv, important);
 			decl_add(out, nout, PROP_OVERLINE, ov, important);
 			decl_add(out, nout, PROP_OVERLINE, ov, important);
+		} else if (str_eq(prop, "cursor")) {
+			int c = parse_cursor(s + vstart, vend - vstart);
+
+			if (c >= 0)
+				decl_add(out, nout, PROP_CURSOR, c, important);
 		} else if (str_eq(prop, "text-transform")) {
 		} else if (str_eq(prop, "text-transform")) {
 			int u = (vend - vstart >= 9 &&
 			int u = (vend - vstart >= 9 &&
 				 ci_eq_n(s + vstart, "uppercase", 9));
 				 ci_eq_n(s + vstart, "uppercase", 9));
@@ -7886,6 +7927,9 @@ static void apply_decl(struct br_style *st, const struct br_decl *dcl)
 	case PROP_UPPER:
 	case PROP_UPPER:
 		st->uppercase = (unsigned char)(dcl->val != 0);
 		st->uppercase = (unsigned char)(dcl->val != 0);
 		break;
 		break;
+	case PROP_CURSOR:
+		st->cursor = (unsigned char)dcl->val;
+		break;
 	case PROP_FLOAT:
 	case PROP_FLOAT:
 		st->float_left = (unsigned char)dcl->val;
 		st->float_left = (unsigned char)dcl->val;
 		break;
 		break;

+ 6 - 0
user/apps/browser/browser_html.h

@@ -6,6 +6,10 @@
 #ifndef BROWSER_HTML_H
 #ifndef BROWSER_HTML_H
 #define BROWSER_HTML_H
 #define BROWSER_HTML_H
 
 
+#include "cursor_abi.h"
+
+#define BR_CURSOR_AUTO 0
+
 #define BR_MAX_NODES     16384
 #define BR_MAX_NODES     16384
 #define BR_TEXT_SIZE     524288
 #define BR_TEXT_SIZE     524288
 #define BR_ATTR_SIZE     4194304
 #define BR_ATTR_SIZE     4194304
@@ -165,6 +169,8 @@ struct br_style {
 	unsigned char list_item;    /* li marker is drawn */
 	unsigned char list_item;    /* li marker is drawn */
 	unsigned char nowrap;       /* white-space: nowrap (inherited) */
 	unsigned char nowrap;       /* white-space: nowrap (inherited) */
 	unsigned char uppercase;    /* text-transform: uppercase (inherited) */
 	unsigned char uppercase;    /* text-transform: uppercase (inherited) */
+	unsigned char cursor;       /* CSS cursor (inherited): BR_CURSOR_AUTO or
+				       enum cursor_shape + 1 */
 	unsigned char border_w[4];  /* top, right, bottom, left */
 	unsigned char border_w[4];  /* top, right, bottom, left */
 	unsigned char border_s[4];  /* BR_BORDER_* per side */
 	unsigned char border_s[4];  /* BR_BORDER_* per side */
 	unsigned char border_t[4];  /* 1 = fully transparent border side */
 	unsigned char border_t[4];  /* 1 = fully transparent border side */

+ 53 - 4
user/apps/gallery/gallery.c

@@ -30,6 +30,7 @@
 #define INPUTS_H        250
 #define INPUTS_H        250
 #define SCROLL_H        220
 #define SCROLL_H        220
 #define LAYOUT_H        190
 #define LAYOUT_H        190
+#define CURSORS_H       230
 
 
 #define LIST_ROWS       12
 #define LIST_ROWS       12
 #define LIST_ROW_H      24
 #define LIST_ROW_H      24
@@ -43,6 +44,7 @@ enum section {
 	SEC_INPUTS,
 	SEC_INPUTS,
 	SEC_SCROLL,
 	SEC_SCROLL,
 	SEC_LAYOUT,
 	SEC_LAYOUT,
+	SEC_CURSORS,
 	SEC_COUNT
 	SEC_COUNT
 };
 };
 
 
@@ -70,12 +72,12 @@ enum {
 
 
 static const int sec_card_h[SEC_COUNT] = {
 static const int sec_card_h[SEC_COUNT] = {
 	TYPO_H, SHAPES_H, THEME_H, ICONS_H,
 	TYPO_H, SHAPES_H, THEME_H, ICONS_H,
-	BUTTONS_H, INPUTS_H, SCROLL_H, LAYOUT_H
+	BUTTONS_H, INPUTS_H, SCROLL_H, LAYOUT_H, CURSORS_H
 };
 };
 
 
 static const char *const sec_titles[SEC_COUNT] = {
 static const char *const sec_titles[SEC_COUNT] = {
 	"Text", "Shapes", "Theme", "Icons",
 	"Text", "Shapes", "Theme", "Icons",
-	"Buttons", "Inputs", "Scroll", "Layout"
+	"Buttons", "Inputs", "Scroll", "Layout", "Cursors"
 };
 };
 
 
 static const char *const sec_descs[SEC_COUNT] = {
 static const char *const sec_descs[SEC_COUNT] = {
@@ -86,7 +88,8 @@ static const char *const sec_descs[SEC_COUNT] = {
 	"buttons and icon buttons",
 	"buttons and icon buttons",
 	"single-line text fields",
 	"single-line text fields",
 	"a nested draggable scroll pane",
 	"a nested draggable scroll pane",
-	"ngui_cut_* layout helpers"
+	"ngui_cut_* layout helpers",
+	"every mouse cursor shape"
 };
 };
 
 
 static const char *const color_names[15] = {
 static const char *const color_names[15] = {
@@ -216,7 +219,7 @@ static int active_section(void)
 	int i, active = 0;
 	int i, active = 0;
 
 
 	if (max > 0 && page.offset >= max - 1)
 	if (max > 0 && page.offset >= max - 1)
-		return SEC_LAYOUT;
+		return SEC_COUNT - 1;
 	for (i = 0; i < SEC_COUNT; i++)
 	for (i = 0; i < SEC_COUNT; i++)
 		if (sec_y[i] <= probe)
 		if (sec_y[i] <= probe)
 			active = i;
 			active = i;
@@ -1074,6 +1077,49 @@ static void draw_layout(struct ngui *g, struct ngui_rect card)
 		  g->theme.border);
 		  g->theme.border);
 }
 }
 
 
+#define CURSOR_NAME_(e, css, file) css,
+static const char *const cursor_names[CURSOR_COUNT] = {
+	CURSOR_LIST(CURSOR_NAME_)
+};
+#undef CURSOR_NAME_
+
+/* One cell per cursor shape: hovering a cell shows that cursor (the
+ * Adwaita set from /ramfs/cursors, see include/cursor_abi.h). */
+static void draw_cursors(struct ngui *g, struct ngui_rect card)
+{
+	int inner_w = card.w - 2 * CARD_PAD;
+	int cols = inner_w / 120;
+	int rows, cell_w, cell_h, i;
+	int y0 = card.y + 32;
+
+	if (cols < 3)
+		cols = 3;
+	rows = (CURSOR_COUNT + cols - 1) / cols;
+	cell_w = inner_w / cols;
+	cell_h = (card.y + card.h - 10 - y0) / rows;
+	if (cell_h > 30)
+		cell_h = 30;
+	if (cell_w < 20 || cell_h < 12)
+		return;
+	ngui_text(g, card.x + CARD_PAD, card.y + 10,
+		  "Hover a cell to see the cursor (CSS cursor names).",
+		  g->theme.text_dim);
+	for (i = 0; i < CURSOR_COUNT; i++) {
+		struct ngui_rect c = rect_make(
+			card.x + CARD_PAD + (i % cols) * cell_w + 2,
+			y0 + (i / cols) * cell_h + 2, cell_w - 4, cell_h - 4);
+		int hot = ngui_cursor_rect(g, c, i);
+
+		ngui_fill_rect(g, c, hot ? g->theme.button_hot :
+					   g->theme.panel_alt);
+		ngui_frame(g, c, hot ? g->theme.accent : g->theme.border);
+		ngui_text_ellipsis(g, rect_make(c.x + 6, c.y, c.w - 12, c.h),
+				   c.x + 6, c.y + (c.h - 16) / 2,
+				   cursor_names[i],
+				   hot ? g->theme.text : g->theme.text_dim);
+	}
+}
+
 static void draw_body(struct ngui *g, struct ngui_rect body)
 static void draw_body(struct ngui *g, struct ngui_rect body)
 {
 {
 	int x = body.x + PAGE_PAD;
 	int x = body.x + PAGE_PAD;
@@ -1124,6 +1170,9 @@ static void draw_body(struct ngui *g, struct ngui_rect body)
 		case SEC_LAYOUT:
 		case SEC_LAYOUT:
 			draw_layout(g, card);
 			draw_layout(g, card);
 			break;
 			break;
+		case SEC_CURSORS:
+			draw_cursors(g, card);
+			break;
 		default:
 		default:
 			break;
 			break;
 		}
 		}

+ 2 - 0
user/apps/term/ucmd_term.c

@@ -1423,6 +1423,8 @@ void _start(const char *arg)
 		u_exit(1);
 		u_exit(1);
 	if (u_win_create(512, 512, "term", &wi) != 0)
 	if (u_win_create(512, 512, "term", &wi) != 0)
 		u_exit(1);
 		u_exit(1);
+	/* Text cells: the I-beam over the whole content area. */
+	u_win_set_cursor(CURSOR_TEXT);
 	usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
 	usurf_init(&surf, wi.fb, wi.width, wi.height, wi.width * 2, 16);
 	term_alloc_rows();
 	term_alloc_rows();
 	cols = (wi.width > SB_W) ? (wi.width - SB_W) / 8 : 1;
 	cols = (wi.width > SB_W) ? (wi.width - SB_W) / 8 : 1;

+ 54 - 0
user/apps/wm/ucmd_wm.c

@@ -1724,6 +1724,59 @@ static void dismiss_popover_outside(unsigned int mx, unsigned int my)
 	}
 	}
 }
 }
 
 
+/* Top-bar launcher under (mx, my)?  Same rectangles as the click chain in
+ * handle_mouse(); every launcher shares WM_BTN_Y/WM_BTN_H. */
+static int bar_launcher_hit(unsigned int mx, unsigned int my)
+{
+	static const int x0[] = {
+		WM_BTN_X, WM_BTN_CLOCK_X, WM_BTN_COLORS_X, WM_BTN_FILES_X,
+		WM_BTN_MINES_X, WM_BTN_WEB_X, WM_BTN_CARDS_X, WM_BTN_GALLERY_X,
+		WM_BTN_CHAT_X
+	};
+	static const int w[] = {
+		WM_BTN_W, WM_BTN_CLOCK_W, WM_BTN_COLORS_W, WM_BTN_FILES_W,
+		WM_BTN_MINES_W, WM_BTN_WEB_W, WM_BTN_CARDS_W, WM_BTN_GALLERY_W,
+		WM_BTN_CHAT_W
+	};
+	unsigned int i;
+
+	if ((int)my < WM_BTN_Y || (int)my >= WM_BTN_Y + WM_BTN_H)
+		return 0;
+	for (i = 0; i < sizeof(x0) / sizeof(x0[0]); i++)
+		if ((int)mx >= x0[i] && (int)mx < x0[i] + w[i])
+			return 1;
+	return 0;
+}
+
+/* The WM's cursor override (SYS_WM_SET_CURSOR): resize arrows on the grip
+ * and while resizing, the move cross while a title-bar drag is under way,
+ * a hand over the launchers and the status area.  Everywhere else the
+ * kernel picks the window's own content cursor (or the arrow). */
+static void update_cursor(unsigned int mx, unsigned int my)
+{
+	static int current = CURSOR_NO_OVERRIDE;
+	int want = CURSOR_NO_OVERRIDE;
+
+	if (resize_id >= 0) {
+		want = CURSOR_NWSE_RESIZE;
+	} else if (drag_id >= 0) {
+		if (drag_moved)
+			want = CURSOR_MOVE;
+	} else if (my < WM_BAR_H) {
+		if (bar_launcher_hit(mx, my) || bar_status_hit(mx, my))
+			want = CURSOR_POINTER;
+	} else {
+		int idx = window_at((int)mx, (int)my);
+
+		if (idx >= 0 && !wins[idx].info.popover &&
+		    (int)mx >= wins[idx].info.x + (int)wins[idx].info.w - 16 &&
+		    (int)my >= wins[idx].info.y + (int)wins[idx].info.h - 16)
+			want = CURSOR_NWSE_RESIZE;
+	}
+	if (want != current && u_wm_set_cursor(want) == 0)
+		current = want;
+}
+
 static void handle_mouse(struct wm_status *st)
 static void handle_mouse(struct wm_status *st)
 {
 {
 	unsigned int buttons = st->mouse.buttons;
 	unsigned int buttons = st->mouse.buttons;
@@ -1957,6 +2010,7 @@ static void handle_mouse(struct wm_status *st)
 		resize_id = -1;
 		resize_id = -1;
 	}
 	}
 	prev_buttons = buttons;
 	prev_buttons = buttons;
+	update_cursor(mx, my);
 }
 }
 
 
 static char *put_str(char *p, const char *s)
 static char *put_str(char *p, const char *s)

+ 26 - 0
user/lib/ngui/ngui.c

@@ -327,13 +327,38 @@ int ngui_begin(struct ngui *ui)
 		want = 1;
 		want = 1;
 	if (want)
 	if (want)
 		ui->motion_pending = 0;
 		ui->motion_pending = 0;
+	ui->cursor_frame = want;
+	if (want)
+		ui->cursor = CURSOR_DEFAULT;
 	return want;
 	return want;
 }
 }
 
 
+void ngui_set_cursor(struct ngui *ui, int shape)
+{
+	ui->cursor = shape;
+}
+
+int ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape)
+{
+	if (!ui->inside || !ngui_rect_contains(&r, ui->mouse_x, ui->mouse_y))
+		return 0;
+	if (ui->nclip > 0 &&
+	    !ngui_rect_contains(&ui->clip[ui->nclip - 1], ui->mouse_x,
+				ui->mouse_y))
+		return 0;
+	ui->cursor = shape;
+	return 1;
+}
+
 void ngui_end_ex(struct ngui *ui, int present)
 void ngui_end_ex(struct ngui *ui, int present)
 {
 {
 	if (present && u_win_present() != 0)
 	if (present && u_win_present() != 0)
 		ui->dirty = 1;
 		ui->dirty = 1;
+	/* Only a redrawn frame has evaluated every widget's hover state. */
+	if (ui->cursor_frame && ui->cursor != ui->cursor_sent &&
+	    u_win_set_cursor(ui->cursor) == 0)
+		ui->cursor_sent = ui->cursor;
+	ui->cursor_frame = 0;
 
 
 	if (ui->clicked && !ui->focus_claimed)
 	if (ui->clicked && !ui->focus_claimed)
 		ui->focus = 0;
 		ui->focus = 0;
@@ -2026,6 +2051,7 @@ int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 	avail = inner.w;
 	avail = inner.w;
 	if (in->secret)
 	if (in->secret)
 		secret_mask(in);
 		secret_mask(in);
+	ngui_cursor_rect(ui, r, in->disabled ? CURSOR_NOT_ALLOWED : CURSOR_TEXT);
 
 
 	if (in->disabled) {
 	if (in->disabled) {
 		/* A field that became disabled while focused gives the focus
 		/* A field that became disabled while focused gives the focus

+ 13 - 0
user/lib/ngui/ngui.h

@@ -183,6 +183,14 @@ struct ngui {
 	 * once 33 ms have passed, so the final hover state still appears. */
 	 * once 33 ms have passed, so the final hover state still appears. */
 	unsigned int motion_ms;
 	unsigned int motion_ms;
 	int motion_pending;
 	int motion_pending;
+
+	/* Mouse cursor (enum cursor_shape): widgets set `cursor` while they
+	 * draw a frame (a text field under the pointer asks for the I-beam);
+	 * ngui_begin() resets it to the arrow for every redrawn frame and
+	 * ngui_end_ex() sends it to the kernel when it changed. */
+	int cursor;
+	int cursor_sent;
+	int cursor_frame;      /* this frame was redrawn (begin returned 1) */
 };
 };
 
 
 void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
 void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
@@ -299,6 +307,11 @@ int  ngui_slider(struct ngui *ui, unsigned int id, struct ngui_slider *s,
 		 struct ngui_rect r, int *value, int min, int max);
 		 struct ngui_rect r, int *value, int min, int max);
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 int ngui_text_input(struct ngui *ui, unsigned int id, struct ngui_rect r,
 		    struct ngui_input *in);
 		    struct ngui_input *in);
+/* Ask for `shape` (enum cursor_shape) while the pointer is in `r` (and in
+ * the current clip); returns 1 when it is.  ngui_set_cursor() sets it
+ * unconditionally for the current frame. */
+int  ngui_cursor_rect(struct ngui *ui, struct ngui_rect r, int shape);
+void ngui_set_cursor(struct ngui *ui, int shape);
 void ngui_input_set(struct ngui_input *in, const char *s);
 void ngui_input_set(struct ngui_input *in, const char *s);
 /* Copy a field's real value out (a secret field's '*' mask is bypassed). */
 /* Copy a field's real value out (a secret field's '*' mask is bypassed). */
 void ngui_input_value(const struct ngui_input *in, char *out,
 void ngui_input_value(const struct ngui_input *in, char *out,

+ 15 - 0
user/ucmd_lib.h

@@ -4,6 +4,7 @@
 #define UCMD_LIB_H
 #define UCMD_LIB_H
 
 
 #include "syscall.h"
 #include "syscall.h"
+#include "cursor_abi.h"
 #include "wm_abi.h"
 #include "wm_abi.h"
 
 
 #ifdef UCMD_HOST
 #ifdef UCMD_HOST
@@ -350,6 +351,20 @@ static inline int u_win_resize_self(unsigned int w, unsigned int h)
 	return u_syscall2(SYS_WIN_RESIZE_SELF, (int)w, (int)h);
 	return u_syscall2(SYS_WIN_RESIZE_SELF, (int)w, (int)h);
 }
 }
 
 
+/* Cursor shown while the pointer is over this window's content
+ * (enum cursor_shape in cursor_abi.h). */
+static inline int u_win_set_cursor(int shape)
+{
+	return u_syscall1(SYS_WIN_SET_CURSOR, shape);
+}
+
+/* WM only: cursor for the WM's own chrome and drags; CURSOR_NO_OVERRIDE
+ * hands the pointer back to the windows. */
+static inline int u_wm_set_cursor(int shape)
+{
+	return u_syscall1(SYS_WM_SET_CURSOR, shape);
+}
+
 static inline int u_screen_size(unsigned int *w, unsigned int *h)
 static inline int u_screen_size(unsigned int *w, unsigned int *h)
 {
 {
 	struct sys_screen s;
 	struct sys_screen s;