瀏覽代碼

wm: persisted desktop wallpaper from the image viewer

Johannes Zellner 1 天之前
父節點
當前提交
dac20d37c3
共有 14 個文件被更改,包括 380 次插入 和 8 次删除
  1. 36 3
      AGENTS.md
  2. 5 0
      Makefile
  3. 1 0
      docs/filesystems.html
  4. 7 2
      docs/graphics.html
  5. 2 1
      docs/userland.html
  6. 2 0
      include/syscall.h
  7. 20 0
      include/wm_abi.h
  8. 8 0
      kernel/sys_wm.c
  9. 1 0
      kernel/sys_wm.h
  10. 2 0
      kernel/syscall.c
  11. 9 0
      kernel/wm.c
  12. 1 0
      kernel/wm.h
  13. 111 0
      user/apps/view/ucmd_view.c
  14. 175 2
      user/apps/wm/ucmd_wm.c

+ 36 - 3
AGENTS.md

@@ -128,7 +128,9 @@ make clean
 ```
 ```
 
 
 - Build flags (in `Makefile`): `-m32 -march=i586 -ffreestanding -fno-pie -nostdlib
 - Build flags (in `Makefile`): `-m32 -march=i586 -ffreestanding -fno-pie -nostdlib
-  -fno-stack-protector -mno-sse -mno-sse2 -mno-mmx -O2`.
+  -fno-stack-protector -mno-sse -mno-sse2 -mno-mmx -O2`.  The ring-3 WM and
+  viewer are per-object `-Os` with function sections, like the other size-
+  sensitive GUIs, because the 2 MB gzip rootfs pack is close to its cap.
 - `make browser-host` is a development-only harness, not part of the images: it
 - `make browser-host` is a development-only harness, not part of the images: it
   builds `browser.c`, ngui, the image/TLS stack and QuickJS as a 32-bit Linux
   builds `browser.c`, ngui, the image/TLS stack and QuickJS as a 32-bit Linux
   program against the same `SYS_*` ABI (`host/syscall.c`, an SDL3 window,
   program against the same `SYS_*` ABI (`host/syscall.c`, an SDL3 window,
@@ -514,6 +516,20 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   window behind it.  While dragged or resized the WM owns the pointer
   window behind it.  While dragged or resized the WM owns the pointer
   (`SYS_WM_MOUSE_CAPTURE`), so canvas clients see no held button and cannot
   (`SYS_WM_MOUSE_CAPTURE`), so canvas clients see no held button and cannot
   start a scrollbar drag.  The kernel hit-test uses the same order.
   start a scrollbar drag.  The kernel hit-test uses the same order.
+  The desktop background is `COL_BG` until a wallpaper is installed.  The
+  image viewer pre-renders the current image to a screen-aspect RGB565
+  buffer (at most 1440x810, cropped to cover) and writes one
+  `struct wm_wallpaper_hdr` (magic `NBWAL1`, width, height) plus the pixels to
+  `/data/WALLPAPR.BIN` when the data volume is mounted, else the RAM-only
+  `/share/wallpaper`.  It then calls `SYS_WM_WALLPAPER` (123, op 1), which
+  bumps `wm_status.wallpaper_seq`; the WM's loop sees the new sequence,
+  reloads the file (`/data` first, then `/share`), rebuilds its x/y
+  nearest-neighbour maps and repaints.  The WM has no image decoder: it
+  stretches the stored buffer into every desktop damage rectangle under the
+  bar and windows, with a static 2.4 MB buffer plus a 256 KB RGB565-to-BGRA
+  table for 32 bpp (and direct stores for 16/24 bpp).  A missing or invalid
+  file falls back to `COL_BG`.  The file survives a reboot on an installed
+  board because it lives on the `NEBOSDATA` volume.
   The NebOS banner in the top-left corner is the start button.  Clicking it
   The NebOS banner in the top-left corner is the start button.  Clicking it
   opens the WM's Windows-like start menu, a flat overlay panel drawn by the
   opens the WM's Windows-like start menu, a flat overlay panel drawn by the
   WM (not a window, so it does not consume a `WM_MAX_WINDOWS` slot).  It is
   WM (not a window, so it does not consume a `WM_MAX_WINDOWS` slot).  It is
@@ -912,6 +928,8 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   works.  Files are 8.3 only until FAT LFN write support exists.  A reinstall
   works.  Files are 8.3 only until FAT LFN write support exists.  A reinstall
   replaces P1..P3 and keeps P4; `net-install` never touches it.  The data
   replaces P1..P3 and keeps P4; `net-install` never touches it.  The data
   volume is not available on the live USB unless that stick was installed.
   volume is not available on the live USB unless that stick was installed.
+  The image viewer stores the desktop wallpaper in `WALLPAPR.BIN` here (the
+  WM's `wall_load()` looks for it at boot and after a change).
 - **SATA/AHCI**: `ahci.c/h` — polled AHCI driver (first controller only, PCI
 - **SATA/AHCI**: `ahci.c/h` — polled AHCI driver (first controller only, PCI
   class `01:06:01`, no IRQs, no NCQ): port bring-up with COMRESET, IDENTIFY
   class `01:06:01`, no IRQs, no NCQ): port bring-up with COMRESET, IDENTIFY
   (shared `ata_identify_parse()` with `ata.c`) and READ/WRITE DMA EXT through
   (shared `ata_identify_parse()` with `ata.c`) and READ/WRITE DMA EXT through
@@ -2225,7 +2243,12 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   animated GIF frames are supported as well; arithmetic JPEG/CMYK are not.
   animated GIF frames are supported as well; arithmetic JPEG/CMYK are not.
   The viewer, `ucmd_view.c`, owns a canvas window and reads its file through
   The viewer, `ucmd_view.c`, owns a canvas window and reads its file through
   `SYS_READ_FILE`; it uses fit/1:1/zoom/pan, shows format/dimensions/size,
   `SYS_READ_FILE`; it uses fit/1:1/zoom/pan, shows format/dimensions/size,
-  animates GIFs, and gives a clear error for unsupported variants.  The chat
+  animates GIFs, and gives a clear error for unsupported variants.  Its
+  toolbar's *Background* button (or `b`) calls `view_set_wallpaper()`: it
+  reads `SYS_SCREEN_INFO`, cover-scales the image into a screen-aspect
+  RGB565 buffer capped at 1440x810, writes `/data/WALLPAPR.BIN` or
+  `/share/wallpaper` and signals the WM.  The status bar shows
+  "Wallpaper set" (or the failure) for four seconds.  The chat
   client decodes fetched message images and avatars into a 480x320 / 32x32
   client decodes fetched message images and avatars into a 480x320 / 32x32
   cache (passing those bounds to `img_options.max_w/h`) and draws them with
   cache (passing those bounds to `img_options.max_w/h`) and draws them with
   `img_draw()`.  Host
   `img_draw()`.  Host
@@ -2906,7 +2929,7 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
    with `--gc-sections`; the current full pack with the UI fonts, both
    with `--gc-sections`; the current full pack with the UI fonts, both
    WiFi firmwares (7265D 1,036,312 bytes, ~442 KiB gzip; 6000 454,608 bytes,
    WiFi firmwares (7265D 1,036,312 bytes, ~442 KiB gzip; 6000 454,608 bytes,
    ~204 KiB gzip), the RTL8821A firmware and tables (31,898 + 11,668
    ~204 KiB gzip), the RTL8821A firmware and tables (31,898 + 11,668
-   bytes) and `install/kernel.bin` is ~2.00 MiB gzip (~3 KB under the
+   bytes) and `install/kernel.bin` is ~2.00 MiB gzip (~4 KB under the
    cap), inflating to ~4.13 MB, which adds about
    cap), inflating to ~4.13 MB, which adds about
    half a second to the boot; the floppy lite pack is ~641 KiB gzip and
    half a second to the boot; the floppy lite pack is ~641 KiB gzip and
    deliberately omits the firmware, `install/kernel.bin` and the browser
    deliberately omits the firmware, `install/kernel.bin` and the browser
@@ -3415,3 +3438,13 @@ and the WM falls back to polling (`io_keyboard_irq_masked()`).
     The cursor shape follows the WM poll, so it can lag the pointer by
     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) are included in
     one WM loop.  The 4 KB save buffer and the cursor code (~8 KB in all) are included in
     `_end` ~0x8df80, ~8 KB below the `0x90000` wall.
     `_end` ~0x8df80, ~8 KB below the `0x90000` wall.
+
+40. Wallpaper limits: one screen-aspect image pre-scaled to at most
+    1440x810 (cropped to cover, no crop/position choice), no slideshow and
+    no Clear action in the viewer.  The WM reads the whole file when
+    `SYS_WM_WALLPAPER` bumps the sequence, so installing/switching a
+    wallpaper blocks the WM for the FAT read; the storage is
+    `/data/WALLPAPR.BIN` on installed boards and the RAM-only
+    `/share/wallpaper` otherwise.  The WM keeps a 2.4 MB static copy plus a
+    256 KB RGB565->BGRA table (16/24 bpp convert per pixel), all inside the
+    4 MB user image window, and has no image decoder of its own.

+ 5 - 0
Makefile

@@ -412,6 +412,11 @@ $(call app,edit).o $(call app,wm).o $(call app,files).o $(call app,mines).o \
 $(call app,settime).o $(call app,solitaire).o $(U)/apps/gallery/gallery.o \
 $(call app,settime).o $(call app,solitaire).o $(U)/apps/gallery/gallery.o \
 $(U)/apps/hello/hello.o: XFLAGS =
 $(U)/apps/hello/hello.o: XFLAGS =
 $(call app,settime).o $(call app,solitaire).o: OPT = -Os
 $(call app,settime).o $(call app,solitaire).o: OPT = -Os
+# Both link with --gc-sections; the section-per-function build lets the pack
+# drop their unreachable helpers (the rootfs cap is tight).
+$(call app,wm).o: XFLAGS = $(SECTION_CFLAGS)
+$(call app,view).o: XFLAGS = $(SECTION_CFLAGS) $(LIBC_INC)
+$(call app,wm).o $(call app,view).o: OPT = -Os
 
 
 # membench times memcpy/memset over a libc heap buffer.
 # membench times memcpy/memset over a libc heap buffer.
 $(call app,membench).elf: $(call app,membench).o $(UCMD_LD) $(LIBC_A)
 $(call app,membench).elf: $(call app,membench).o $(UCMD_LD) $(LIBC_A)

+ 1 - 0
docs/filesystems.html

@@ -316,6 +316,7 @@
 			<li><strong>8.3 names.</strong> The FAT driver creates and reads short names only; long names need VFAT LFN write support.</li>
 			<li><strong>8.3 names.</strong> The FAT driver creates and reads short names only; long names need VFAT LFN write support.</li>
 			<li><strong>WiFi credentials.</strong> A successful WiFi connect adds the network to a most-recently-used list of up to 16 SSID/passphrase records in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper scans and joins the strongest saved network it finds. <code>wifi saved</code> lists them and <code>wifi forget [ssid]</code> removes one. The file is plaintext and 8.3, like the rest of the volume.</li>
 			<li><strong>WiFi credentials.</strong> A successful WiFi connect adds the network to a most-recently-used list of up to 16 SSID/passphrase records in <code>WIFI.CFG</code>; the boot-time <code>wifi autoconnect</code> helper scans and joins the strongest saved network it finds. <code>wifi saved</code> lists them and <code>wifi forget [ssid]</code> removes one. The file is plaintext and 8.3, like the rest of the volume.</li>
 			<li><strong>Chat session.</strong> The Chat client stores its server and session cookie in <code>CHAT.CFG</code> when this volume is mounted; without it the client falls back to the RAM-only <code>/share/chat.cfg</code>. The cookie is a bearer credential, and the password is never stored.</li>
 			<li><strong>Chat session.</strong> The Chat client stores its server and session cookie in <code>CHAT.CFG</code> when this volume is mounted; without it the client falls back to the RAM-only <code>/share/chat.cfg</code>. The cookie is a bearer credential, and the password is never stored.</li>
+			<li><strong>Wallpaper.</strong> The image viewer's <em>Background</em> action writes the pre-scaled RGB565 desktop image to <code>WALLPAPR.BIN</code> here when this volume is mounted; the WM loads it at boot and after every change. Without <code>/data</code> the image goes to the RAM-only <code>/share/wallpaper</code>.</li>
 			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
 			<li><strong>Fixed start.</strong> P4 starts at LBA 34816. A BIOS-only install leaves the space between P2 and P4 unpartitioned, and a later disk clone does not resize P4.</li>
 			<li><strong>Mount settings.</strong> The <code>disks</code> app saves the mount policies in <code>MOUNTS.CFG</code>; the boot scan applies them (see below).</li>
 			<li><strong>Mount settings.</strong> The <code>disks</code> app saves the mount policies in <code>MOUNTS.CFG</code>; the boot scan applies them (see below).</li>
 			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>
 			<li><strong>No fsck.</strong> Like the other FAT mounts, the volume has no journal, repair tool or permissions; it is writable by every process.</li>

+ 7 - 2
docs/graphics.html

@@ -156,7 +156,12 @@
 			A normal window has no visible frame; an invisible resize band
 			A normal window has no visible frame; an invisible resize band
 			(1 px inside, 3 px outside its outer edge) is the resize grab
 			(1 px inside, 3 px outside its outer edge) is the resize grab
 			area, and the active title bar uses the toolkit's highlight
 			area, and the active title bar uses the toolkit's highlight
-			colour.
+			colour. The desktop starts as a flat colour; <code>view</code>'s
+			<em>Background</em> button stores a pre-scaled RGB565 file
+			(<code>/data/WALLPAPR.BIN</code> on an installed board, else
+			<code>/share/wallpaper</code>) and signals the WM through
+			<code>SYS_WM_WALLPAPER</code>, which reloads and stretches it
+			under the windows. The WM has no image decoder of its own.
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			Click-to-focus, drag-by-title-bar and close are routed through the
 			same pointer path. To the left of the rightmost AC/battery status
 			same pointer path. To the left of the rightmost AC/battery status
 			icon, a network status icon shows the Adwaita
 			icon, a network status icon shows the Adwaita
@@ -467,7 +472,7 @@
 			</div>
 			</div>
 			<div class="card">
 			<div class="card">
 				<h3>view</h3>
 				<h3>view</h3>
-				<p>Image viewer with fit, 1:1, zoom, pan, EXIF orientation and animated GIF playback.</p>
+				<p>Image viewer with fit, 1:1, zoom, pan, EXIF orientation and animated GIF playback. Its <em>Background</em> button (or the <code>b</code> key) installs the image as the desktop wallpaper, persisted in <code>/data/WALLPAPR.BIN</code> when the data volume is mounted.</p>
 			</div>
 			</div>
 			<div class="card">
 			<div class="card">
 				<h3>clock / colors / settime</h3>
 				<h3>clock / colors / settime</h3>

+ 2 - 1
docs/userland.html

@@ -234,6 +234,7 @@
 					<tr><td>Boot log</td><td>122</td><td><code>SYS_BOOTLOG(STAT|READ, ...)</code> returns the ring counters or one 1 KB chunk of the <code>bootscan</code> thread's captured COM1 output (the <code>bootscan</code> window)</td></tr>
 					<tr><td>Boot log</td><td>122</td><td><code>SYS_BOOTLOG(STAT|READ, ...)</code> returns the ring counters or one 1 KB chunk of the <code>bootscan</code> thread's captured COM1 output (the <code>bootscan</code> window)</td></tr>
 					<tr><td>Storage</td><td>119–120</td><td><code>SYS_BLK_READ(index, lba, buf)</code> reads one raw sector, <code>SYS_MOUNT_CTL(op, ...)</code> sets/gets a mount policy (name, read-only, no automount); used by <code>disks</code></td></tr>
 					<tr><td>Storage</td><td>119–120</td><td><code>SYS_BLK_READ(index, lba, buf)</code> reads one raw sector, <code>SYS_MOUNT_CTL(op, ...)</code> sets/gets a mount policy (name, read-only, no automount); used by <code>disks</code></td></tr>
 					<tr><td>Mouse cursor</td><td>117–118, 121</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, and <code>SYS_WM_MOUSE_CAPTURE(on)</code> takes/releases the pointer while the WM drags or resizes a window (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
 					<tr><td>Mouse cursor</td><td>117–118, 121</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, and <code>SYS_WM_MOUSE_CAPTURE(on)</code> takes/releases the pointer while the WM drags or resizes a window (shapes in <span class="file">cursor_abi.h</span>)</td></tr>
+					<tr><td>Wallpaper</td><td>123</td><td><code>SYS_WM_WALLPAPER(1)</code> bumps <code>wm_status.wallpaper_seq</code> after a client writes the RGB565 wallpaper file; the WM reloads it and repaints the desktop</td></tr>
 				</tbody>
 				</tbody>
 			</table>
 			</table>
 		</div>
 		</div>
@@ -357,7 +358,7 @@
 			<li><strong>chat</strong> — native Cloudron Chat client (start menu <em>Chat</em> entry, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, inline image attachments, link-preview cards (click one to open it in the browser) and profile-picture avatars, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/data/CHAT.CFG</code> when the persistent volume is mounted, or in the RAM-only <code>/share/chat.cfg</code> otherwise.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
 			<li><strong>chat</strong> — native Cloudron Chat client (start menu <em>Chat</em> entry, or <code>chat [config-path]</code>): username/password/TOTP login through the Cloudron OIDC API or a pasted session cookie, channels and DMs with unread badges and presence, history with older paging, inline image attachments, link-preview cards (click one to open it in the browser) and profile-picture avatars, sending, REST polling, read markers, browse/join and new DM.  The session cookie (never the password) is saved in <code>/data/CHAT.CFG</code> when the persistent volume is mounted, or in the RAM-only <code>/share/chat.cfg</code> otherwise.  Keys: Enter sends, PgUp/PgDn scroll, Home at the top pages in older history, Ctrl+B browse, Ctrl+N new DM, Ctrl+R refresh, and with the composer unfocused (Esc) Up/Down switch conversations.  See <a href="network.html#chat">Network → Chat client</a>.</li>
 			<li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>
 			<li><strong>solitaire</strong> — Klondike with drag-and-drop, scoring and a win animation.</li>
 			<li><strong>mines</strong> — second ngui canvas app; shares the toolkit and event model.</li>
 			<li><strong>mines</strong> — second ngui canvas app; shares the toolkit and event model.</li>
-			<li><strong>view</strong> — fit/1:1/zoom/pan image viewer, animated GIF playback.</li>
+			<li><strong>view</strong> — fit/1:1/zoom/pan image viewer, animated GIF playback. Its <em>Background</em> button (or <code>b</code>) cover-scales the image to a screen-aspect RGB565 wallpaper, writes <code>/data/WALLPAPR.BIN</code> when the data volume is mounted (otherwise the RAM-only <code>/share/wallpaper</code>), and signals the WM; the status bar reports the result.</li>
 			<li><strong>edit</strong> — text editor that draws its own text with the 8x16 font.</li>
 			<li><strong>edit</strong> — text editor that draws its own text with the 8x16 font.</li>
 			<li><strong>clock</strong> / <strong>colors</strong> — small window canvas demos.</li>
 			<li><strong>clock</strong> / <strong>colors</strong> — small window canvas demos.</li>
 			<li><strong>settime</strong> — the standalone Date &amp; Time editor (the control panel has an inline editor on its Date &amp; Time tile). It reads and writes the CMOS RTC through <code>SYS_RTC_GET</code>/<code>SYS_RTC_SET</code>; the <em>Sync NTP</em> button asks the kernel's SNTP client (<code>SYS_NTP_SYNC</code>, 106) for UTC and writes the result to the RTC. The two-digit RTC year is expanded over 1970–2069 so dead-battery defaults such as 1980/1990 can be corrected.</li>
 			<li><strong>settime</strong> — the standalone Date &amp; Time editor (the control panel has an inline editor on its Date &amp; Time tile). It reads and writes the CMOS RTC through <code>SYS_RTC_GET</code>/<code>SYS_RTC_SET</code>; the <em>Sync NTP</em> button asks the kernel's SNTP client (<code>SYS_NTP_SYNC</code>, 106) for UTC and writes the result to the RTC. The two-digit RTC year is expanded over 1970–2069 so dead-battery defaults such as 1980/1990 can be corrected.</li>

+ 2 - 0
include/syscall.h

@@ -123,6 +123,8 @@
 				  WM only: cursor for its chrome/drags */
 				  WM only: cursor for its chrome/drags */
 #define SYS_WM_MOUSE_CAPTURE 121 /* (int on) -> 0/-1; WM only: own the pointer
 #define SYS_WM_MOUSE_CAPTURE 121 /* (int on) -> 0/-1; WM only: own the pointer
 				    while it drags or resizes a window */
 				    while it drags or resizes a window */
+#define SYS_WM_WALLPAPER  123 /* (int op) -> 0/<0; op 1 = a client installed a
+				 new wallpaper, bump wm_status.wallpaper_seq */
 
 
 /* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
 /* Storage management (see sys_fs.c, the `disks` app).  SYS_BLK_READ reads one
  * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3
  * raw 512-byte sector of block device `index` (SYS_BLKDEV order) so ring 3

+ 20 - 0
include/wm_abi.h

@@ -69,6 +69,26 @@ struct wm_status {
 	unsigned int backlight_present;
 	unsigned int backlight_present;
 	unsigned int backlight_level;
 	unsigned int backlight_level;
 	unsigned int backlight_seq;
 	unsigned int backlight_seq;
+	/* Bumped when a client installs a new wallpaper (SYS_WM_WALLPAPER);
+	 * the WM reloads its copy when this changes. */
+	unsigned int wallpaper_seq;
+};
+
+/* Wallpaper file: one header followed by tightly packed RGB565 pixels,
+ * width * height * 2 bytes.  The viewer pre-scales to at most 1440x810 and
+ * the WM stretches the stored image to the framebuffer, so no image decoder
+ * is linked into the WM.  The installed data volume wins over the RAM copy. */
+#define WM_WALLPAPER_MAGIC "NBWAL1\0"
+#define WM_WALLPAPER_DATA  "/data/WALLPAPR.BIN"
+#define WM_WALLPAPER_SHARE "/share/wallpaper"
+#define WM_WALLPAPER_MAX_W 1440
+#define WM_WALLPAPER_MAX_H 810
+
+struct wm_wallpaper_hdr {
+	char magic[8];
+	unsigned int width;
+	unsigned int height;
+	unsigned int reserved;
 };
 };
 
 
 struct wm_window_info {
 struct wm_window_info {

+ 8 - 0
kernel/sys_wm.c

@@ -309,3 +309,11 @@ int sys_wm_mouse_capture(int on)
 {
 {
 	return wm_server_set_pointer_capture(on);
 	return wm_server_set_pointer_capture(on);
 }
 }
+
+int sys_wm_wallpaper(int op)
+{
+	if (op != 1)
+		return -1;
+	wm_server_wallpaper_changed();
+	return 0;
+}

+ 1 - 0
kernel/sys_wm.h

@@ -32,6 +32,7 @@ int sys_win_resize_self(unsigned int w, unsigned int h);
 int sys_win_set_cursor(int shape);
 int sys_win_set_cursor(int shape);
 int sys_wm_set_cursor(int shape);
 int sys_wm_set_cursor(int shape);
 int sys_wm_mouse_capture(int on);
 int sys_wm_mouse_capture(int on);
+int sys_wm_wallpaper(int op);
 
 
 /* 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. */

+ 2 - 0
kernel/syscall.c

@@ -361,6 +361,8 @@ static int syscall_dispatch_locked(int num, int a1, int a2, int a3)
 		return sys_wm_set_cursor((int)a1);
 		return sys_wm_set_cursor((int)a1);
 	case SYS_WM_MOUSE_CAPTURE:
 	case SYS_WM_MOUSE_CAPTURE:
 		return sys_wm_mouse_capture((int)a1);
 		return sys_wm_mouse_capture((int)a1);
+	case SYS_WM_WALLPAPER:
+		return sys_wm_wallpaper((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;
 
 

+ 9 - 0
kernel/wm.c

@@ -52,6 +52,7 @@ static int wm_frame_active;
 static int wm_first_frame;
 static int wm_first_frame;
 static int wm_boot_ready;
 static int wm_boot_ready;
 static int wm_boot_logged;
 static int wm_boot_logged;
+static unsigned int wm_wallpaper_seq;
 static unsigned int wm_last_start;
 static unsigned int wm_last_start;
 static unsigned int wm_last_seen;
 static unsigned int wm_last_seen;
 
 
@@ -1115,6 +1116,7 @@ int wm_server_poll(struct wm_status *out)
 		out->backlight_level = level > 0 ? (unsigned int)level : 0;
 		out->backlight_level = level > 0 ? (unsigned int)level : 0;
 		out->backlight_seq = backlight_seq();
 		out->backlight_seq = backlight_seq();
 	}
 	}
+	out->wallpaper_seq = wm_wallpaper_seq;
 	wm_full_redraw = 0;
 	wm_full_redraw = 0;
 	return 0;
 	return 0;
 }
 }
@@ -1124,6 +1126,13 @@ void wm_server_boot_ready(void)
 	wm_boot_ready = 1;
 	wm_boot_ready = 1;
 }
 }
 
 
+/* SYS_WM_WALLPAPER: a client (the image viewer) wrote a new wallpaper file.
+ * Bumping the sequence makes the WM reload it and repaint the desktop. */
+void wm_server_wallpaper_changed(void)
+{
+	wm_wallpaper_seq++;
+}
+
 int wm_server_begin(void)
 int wm_server_begin(void)
 {
 {
 	if (!wm_server_is_wm() || wm_frame_active)
 	if (!wm_server_is_wm() || wm_frame_active)

+ 1 - 0
kernel/wm.h

@@ -93,6 +93,7 @@ int  wm_server_win_resize_self(unsigned int cw, unsigned int ch);
 int  wm_server_win_set_cursor(int shape);
 int  wm_server_win_set_cursor(int shape);
 int  wm_server_set_cursor(int shape);
 int  wm_server_set_cursor(int shape);
 int  wm_server_set_pointer_capture(int on);
 int  wm_server_set_pointer_capture(int on);
+void wm_server_wallpaper_changed(void);
 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);

+ 111 - 0
user/apps/view/ucmd_view.c

@@ -5,6 +5,7 @@
  * and draws it with fit/1:1/zoom/pan support. */
  * and draws it with fit/1:1/zoom/pan support. */
 
 
 #include <stdlib.h>
 #include <stdlib.h>
+#include <string.h>
 
 
 #include "ucmd_lib.h"
 #include "ucmd_lib.h"
 #include "ngui.h"
 #include "ngui.h"
@@ -21,6 +22,7 @@
 #define VIEW_ID_ONE    2
 #define VIEW_ID_ONE    2
 #define VIEW_ID_OUT    3
 #define VIEW_ID_OUT    3
 #define VIEW_ID_IN     4
 #define VIEW_ID_IN     4
+#define VIEW_ID_WALL   5
 
 
 #define VIEW_BG        NGUI_RGB(24, 26, 32)
 #define VIEW_BG        NGUI_RGB(24, 26, 32)
 #define VIEW_VIEW_BG   NGUI_RGB(16, 17, 21)
 #define VIEW_VIEW_BG   NGUI_RGB(16, 17, 21)
@@ -48,6 +50,8 @@ struct viewer {
 	unsigned int gif_frames, gif_index, gif_delay, gif_next_ms;
 	unsigned int gif_frames, gif_index, gif_delay, gif_next_ms;
 	int last_w, last_h;
 	int last_w, last_h;
 	char err[64];
 	char err[64];
+	char notice[40];
+	unsigned int notice_until;
 	char name[20];
 	char name[20];
 	char title[20];
 	char title[20];
 	int dirty;
 	int dirty;
@@ -226,11 +230,106 @@ static void view_zoom_step(struct ngui *ui, int in)
 	view_set_scale(ui, s);
 	view_set_scale(ui, s);
 }
 }
 
 
+static void view_notice(const char *s)
+{
+	s_copy(v.notice, s, sizeof(v.notice));
+	v.notice_until = u_ticks_ms() + 4000;
+	v.dirty = 1;
+}
+
+/* Pre-render the image as a screen-aspect RGB565 wallpaper (at most
+ * 1440x810) and hand it to the WM.  The WM has no image decoder; it only
+ * scales this buffer to the desktop.  A mounted data volume gets the
+ * persistent copy, otherwise the RAM copy is used. */
+static void view_set_wallpaper(void)
+{
+	const char *magic = WM_WALLPAPER_MAGIC;
+	const char *path = WM_WALLPAPER_SHARE;
+	struct wm_wallpaper_hdr *h;
+	struct sys_stat st;
+	struct img_blit b;
+	unsigned char *buf;
+	unsigned int sw, sh, tw, th, pixbytes, total, i;
+	unsigned long long sx, sy, scale, dw, dh;
+
+	if (!v.have_img) {
+		view_notice("No image to use");
+		return;
+	}
+	if (u_screen_size(&sw, &sh) != 0 || !sw || !sh) {
+		view_notice("Cannot read screen size");
+		return;
+	}
+	tw = sw;
+	th = sh;
+	if (tw > WM_WALLPAPER_MAX_W) {
+		th = (unsigned int)((unsigned long long)th *
+				    WM_WALLPAPER_MAX_W / tw);
+		tw = WM_WALLPAPER_MAX_W;
+	}
+	if (th > WM_WALLPAPER_MAX_H) {
+		tw = (unsigned int)((unsigned long long)tw *
+				    WM_WALLPAPER_MAX_H / th);
+		th = WM_WALLPAPER_MAX_H;
+	}
+	if (!tw)
+		tw = 1;
+	if (!th)
+		th = 1;
+	pixbytes = tw * th * 2u;
+	total = sizeof(*h) + pixbytes;
+	buf = (unsigned char *)malloc(total);
+	if (!buf) {
+		view_notice("Not enough memory");
+		return;
+	}
+	memset(buf, 0, total);
+	h = (struct wm_wallpaper_hdr *)buf;
+	for (i = 0; i < sizeof(h->magic); i++)
+		h->magic[i] = magic[i];
+	h->width = tw;
+	h->height = th;
+
+	/* Cover the screen, cropping the longer source axis. */
+	sx = (((unsigned long long)tw << 16) + v.iw - 1) / v.iw;
+	sy = (((unsigned long long)th << 16) + v.ih - 1) / v.ih;
+	scale = sx > sy ? sx : sy;
+	if (!scale)
+		scale = 1;
+	dw = ((unsigned long long)v.iw * scale) >> 16;
+	dh = ((unsigned long long)v.ih * scale) >> 16;
+	memset(&b, 0, sizeof(b));
+	b.dst = (unsigned short *)(buf + sizeof(*h));
+	b.dst_w = (int)tw;
+	b.dst_h = (int)th;
+	b.clip_w = (int)tw;
+	b.clip_h = (int)th;
+	b.x = ((int)tw - (int)dw) / 2;
+	b.y = ((int)th - (int)dh) / 2;
+	b.scale = (unsigned int)scale;
+	img_draw(&v.img, &b);
+
+	if (u_stat("/data", &st) == 0 && st.is_dir)
+		path = WM_WALLPAPER_DATA;
+	if (u_syscall3(SYS_WRITE_FILE, (int)path, (int)buf, (int)total) < 0) {
+		free(buf);
+		view_notice("Cannot save wallpaper");
+		return;
+	}
+	u_syscall1(SYS_WM_WALLPAPER, 1);
+	free(buf);
+	view_notice("Wallpaper set");
+}
+
 static void format_status(char *buf, unsigned int cap)
 static void format_status(char *buf, unsigned int cap)
 {
 {
 	unsigned int n = 0;
 	unsigned int n = 0;
 
 
 	buf[0] = '\0';
 	buf[0] = '\0';
+	if (v.notice[0]) {
+		s_copy(buf, v.notice, cap);
+		return;
+	}
 	if (v.have_img) {
 	if (v.have_img) {
 		unsigned int scale = view_scale(&v.ui);
 		unsigned int scale = view_scale(&v.ui);
 
 
@@ -313,6 +412,12 @@ static void draw_toolbar(struct ngui *ui, struct ngui_rect r)
 		v.dirty = 1;
 		v.dirty = 1;
 	}
 	}
 	x += 32;
 	x += 32;
+	if (v.have_img && x + 90 < r.x + r.w) {
+		b = (struct ngui_rect){x, by, 84, 20};
+		if (ngui_button(ui, VIEW_ID_WALL, b, "Background"))
+			view_set_wallpaper();
+		x += 89;
+	}
 	if (v.have_img && x + 70 < r.x + r.w) {
 	if (v.have_img && x + 70 < r.x + r.w) {
 		char z[16];
 		char z[16];
 		unsigned int n = 0;
 		unsigned int n = 0;
@@ -573,6 +678,10 @@ void _start(const char *arg)
 		int k;
 		int k;
 		unsigned int now = u_ticks_ms();
 		unsigned int now = u_ticks_ms();
 
 
+		if (v.notice[0] && (int)(now - v.notice_until) >= 0) {
+			v.notice[0] = '\0';
+			v.dirty = 1;
+		}
 		if (v.gif_frames > 1 &&
 		if (v.gif_frames > 1 &&
 		    (int)(now - v.gif_next_ms) >= 0) {
 		    (int)(now - v.gif_next_ms) >= 0) {
 			unsigned int d = v.gif_delay;
 			unsigned int d = v.gif_delay;
@@ -614,6 +723,8 @@ void _start(const char *arg)
 			} else if (k == '-' || k == '_') {
 			} else if (k == '-' || k == '_') {
 				view_zoom_step(ui, 0);
 				view_zoom_step(ui, 0);
 				v.dirty = 1;
 				v.dirty = 1;
+			} else if (k == 'b' || k == 'B') {
+				view_set_wallpaper();
 			} else if (k == NGUI_KEY_LEFT || k == NGUI_KEY_RIGHT ||
 			} else if (k == NGUI_KEY_LEFT || k == NGUI_KEY_RIGHT ||
 				   k == NGUI_KEY_UP || k == NGUI_KEY_DOWN) {
 				   k == NGUI_KEY_UP || k == NGUI_KEY_DOWN) {
 				if (v.have_img) {
 				if (v.have_img) {

+ 175 - 2
user/apps/wm/ucmd_wm.c

@@ -53,6 +53,25 @@ struct uwin {
 static struct usurf screen;
 static struct usurf screen;
 static struct uwin wins[WM_MAX_WINDOWS];
 static struct uwin wins[WM_MAX_WINDOWS];
 static unsigned int fbw, fbh;
 static unsigned int fbw, fbh;
+
+/* Desktop wallpaper: the viewer stores a header plus RGB565 pixels; the WM
+ * only has to stretch that small buffer to the framebuffer, so no image
+ * decoder is linked in.  The static buffer avoids pulling malloc into the
+ * WM and stays within the 4 MB user image window. */
+#define WALL_MAP_MAX 2048
+static unsigned char wall_data[sizeof(struct wm_wallpaper_hdr) +
+	(unsigned int)WM_WALLPAPER_MAX_W * WM_WALLPAPER_MAX_H * 2u + 512];
+static unsigned int wall_w, wall_h;
+static int wall_ready;
+static unsigned int wall_seq_seen;
+static int wall_boot_seen;
+static int wall_retries;
+static unsigned int wall_retry_ms;
+static unsigned short wall_xmap[WALL_MAP_MAX];
+static unsigned short wall_ymap[WALL_MAP_MAX];
+/* RGB565 -> 0xFFRRGGBB, built when the wallpaper loads so the 32 bpp
+ * desktop path is a table lookup instead of per-pixel shifts. */
+static unsigned int wall_rgb32[65536];
 static int drag_id = -1;
 static int drag_id = -1;
 static int drag_dx, drag_dy;
 static int drag_dx, drag_dy;
 static int drag_mx, drag_my;       /* press position, for restore-on-drag */
 static int drag_mx, drag_my;       /* press position, for restore-on-drag */
@@ -852,12 +871,143 @@ static unsigned int rect_sub(struct orect *out, const struct orect *r,
 	return n;
 	return n;
 }
 }
 
 
+static void wall_build_maps(void)
+{
+	unsigned int i;
+
+	if (screen.bpp == 32) {
+		for (i = 0; i < 65536; i++) {
+			unsigned int r = (i >> 11) & 31;
+			unsigned int g = (i >> 5) & 63;
+			unsigned int b = i & 31;
+
+			wall_rgb32[i] = 0xFF000000u |
+				((r << 3 | r >> 2) << 16) |
+				((g << 2 | g >> 4) << 8) |
+				(b << 3 | b >> 2);
+		}
+	}
+	for (i = 0; i < WALL_MAP_MAX; i++) {
+		unsigned int sx = 0, sy = 0;
+
+		if (fbw && i < fbw)
+			sx = (unsigned int)((unsigned long long)i * wall_w / fbw);
+		if (fbh && i < fbh)
+			sy = (unsigned int)((unsigned long long)i * wall_h / fbh);
+		if (sx >= wall_w)
+			sx = wall_w - 1;
+		if (sy >= wall_h)
+			sy = wall_h - 1;
+		wall_xmap[i] = (unsigned short)sx;
+		wall_ymap[i] = (unsigned short)sy;
+	}
+}
+
+static int wall_magic_ok(const struct wm_wallpaper_hdr *h)
+{
+	unsigned int i;
+
+	for (i = 0; i < sizeof(h->magic); i++)
+		if (h->magic[i] != WM_WALLPAPER_MAGIC[i])
+			return 0;
+	return 1;
+}
+
+/* Read the persistent copy first, then the RAM copy.  The FAT driver may
+ * write a final partial sector past the requested length, so wall_data has
+ * 512 bytes of slack and the read is capped one sector below its size. */
+static void wall_load(void)
+{
+	static const char *const paths[] = {
+		WM_WALLPAPER_DATA, WM_WALLPAPER_SHARE
+	};
+	struct wm_wallpaper_hdr *h = (struct wm_wallpaper_hdr *)wall_data;
+	unsigned int i, pixbytes;
+	int rn;
+
+	wall_ready = 0;
+	for (i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
+		rn = u_syscall3(SYS_READ_FILE, (int)paths[i], (int)wall_data,
+				(int)(sizeof(wall_data) - 512));
+		if (rn < (int)sizeof(*h) || !wall_magic_ok(h))
+			continue;
+		if (!h->width || !h->height ||
+		    h->width > WM_WALLPAPER_MAX_W ||
+		    h->height > WM_WALLPAPER_MAX_H)
+			continue;
+		pixbytes = h->width * h->height * 2u;
+		if ((unsigned int)rn < sizeof(*h) + pixbytes)
+			continue;
+		wall_w = h->width;
+		wall_h = h->height;
+		wall_ready = 1;
+		wall_build_maps();
+		return;
+	}
+}
+
+/* Paint the desktop part of one damage rectangle. */
+static void wall_draw(int x, int y, unsigned int w, unsigned int h)
+{
+	const unsigned short *src = (const unsigned short *)(wall_data +
+		sizeof(struct wm_wallpaper_hdr));
+	int x0 = x, y0 = y, x1 = x + (int)w, y1 = y + (int)h;
+	unsigned int yy, xx;
+
+	if (!wall_ready) {
+		usurf_fill(&screen, x, y, w, h, COL_BG);
+		return;
+	}
+	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 > WALL_MAP_MAX) x1 = WALL_MAP_MAX;
+	if (y1 > WALL_MAP_MAX) y1 = WALL_MAP_MAX;
+	if (x1 <= x0 || y1 <= y0)
+		return;
+	usurf_pixels += (unsigned int)(x1 - x0) * (unsigned int)(y1 - y0);
+	for (yy = (unsigned int)y0; yy < (unsigned int)y1; yy++) {
+		const unsigned short *srow = src + (unsigned int)wall_ymap[yy] *
+						 wall_w;
+
+		if (screen.bpp == 16) {
+			unsigned short *d = (unsigned short *)(screen.px +
+				(unsigned long)yy * screen.pitch) + x0;
+
+			for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
+				d[xx] = srow[wall_xmap[xx]];
+		} else if (screen.bpp == 32) {
+			unsigned int *d = (unsigned int *)(screen.px +
+				(unsigned long)yy * screen.pitch) + x0;
+
+			for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++)
+				d[xx] = wall_rgb32[srow[wall_xmap[xx]]];
+		} else {
+			unsigned char *d = screen.px +
+				(unsigned long)yy * screen.pitch + x0 * 3;
+
+			for (xx = (unsigned int)x0; xx < (unsigned int)x1; xx++) {
+				unsigned int c = srow[wall_xmap[xx]];
+				unsigned int r = (c >> 11) & 31;
+				unsigned int g = (c >> 5) & 63;
+				unsigned int b = c & 31;
+
+				d[0] = (unsigned char)(b << 3 | b >> 2);
+				d[1] = (unsigned char)(g << 2 | g >> 4);
+				d[2] = (unsigned char)(r << 3 | r >> 2);
+				d += 3;
+			}
+		}
+	}
+}
+
 /* Back-to-front repaint, used only if the visible-region list overflows. */
 /* Back-to-front repaint, used only if the visible-region list overflows. */
 static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
 static void draw_rect_naive(int x, int y, unsigned int w, unsigned int h)
 {
 {
 	unsigned int i;
 	unsigned int i;
 
 
-	usurf_fill(&screen, x, y, w, h, COL_BG);
+	wall_draw(x, y, w, h);
 	for (i = 0; i < WM_MAX_WINDOWS; i++)
 	for (i = 0; i < WM_MAX_WINDOWS; i++)
 		if (wins[i].info.used && !wins[i].info.focused &&
 		if (wins[i].info.used && !wins[i].info.focused &&
 		    !wins[i].info.popover &&
 		    !wins[i].info.popover &&
@@ -904,7 +1054,7 @@ static void draw_rect(int x, int y, unsigned int w, unsigned int h)
 	dmg.y = y0;
 	dmg.y = y0;
 	dmg.w = (unsigned int)(x1 - x0);
 	dmg.w = (unsigned int)(x1 - x0);
 	dmg.h = (unsigned int)(y1 - y0);
 	dmg.h = (unsigned int)(y1 - y0);
-	usurf_fill(&screen, dmg.x, dmg.y, dmg.w, dmg.h, COL_BG);
+	wall_draw(dmg.x, dmg.y, dmg.w, dmg.h);
 
 
 	n = build_order(order);
 	n = build_order(order);
 	for (i = 0; i < n; i++) {
 	for (i = 0; i < n; i++) {
@@ -2527,6 +2677,7 @@ void _start(const char *arg)
 	usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
 	usurf_font_attach(&screen, "/ramfs/fonts/sans.ttf", 14);
 	fbw = si.width;
 	fbw = si.width;
 	fbh = si.height;
 	fbh = si.height;
+	wall_load();
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 	for (i = 0; i < WM_MAX_WINDOWS; i++) {
 		wins[i].info.used = 0;
 		wins[i].info.used = 0;
 		wins[i].mapped = 0;
 		wins[i].mapped = 0;
@@ -2538,6 +2689,28 @@ void _start(const char *arg)
 
 
 		if (u_wm_poll(&st) != 0)
 		if (u_wm_poll(&st) != 0)
 			u_exit(1);
 			u_exit(1);
+		if (st.wallpaper_seq != wall_seq_seen) {
+			wall_seq_seen = st.wallpaper_seq;
+			wall_load();
+			full_redraw = 1;
+		}
+		/* The WM starts before the storage scan, so the first load may not
+		 * see /data yet.  Reload when boot_ready says the mounts are up,
+		 * and retry for ten seconds for the deferred USB /data volume. */
+		if (st.boot_ready && !wall_boot_seen) {
+			wall_boot_seen = 1;
+			wall_retries = 0;
+			wall_retry_ms = u_ticks_ms() + 1000;
+			wall_load();
+			full_redraw = 1;
+		} else if (wall_boot_seen && !wall_ready && wall_retries < 10 &&
+			   (int)(u_ticks_ms() - wall_retry_ms) >= 0) {
+			wall_retries++;
+			wall_retry_ms += 1000;
+			wall_load();
+			if (wall_ready)
+				full_redraw = 1;
+		}
 		if (st.full_redraw)
 		if (st.full_redraw)
 			full_redraw = 1;
 			full_redraw = 1;
 		usurf_pixels = 0;
 		usurf_pixels = 0;