diff --git a/Makefile b/Makefile
index 32d96f0..2f280ba 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: GPL-3.0-only
SDK ?= ../osos-lab/rvx-sdk
OUT ?= build
-extensions := balance crossfeed dsp night-shift playback-log
+extensions := balance cover-grid crossfeed dsp night-shift playback-log
.PHONY: all $(extensions)
all: $(extensions)
@@ -14,6 +14,7 @@ $(extensions):
.PHONY: check
check:
$(PYTHON) extensions/balance/tests/run.py "$(abspath $(SDK))"
+ $(PYTHON) extensions/cover-grid/tests/run.py "$(abspath $(SDK))"
$(PYTHON) extensions/crossfeed/tests/run.py "$(abspath $(SDK))"
$(PYTHON) extensions/dsp/tests/run.py "$(abspath $(SDK))"
@mkdir -p "$(OUT)"
diff --git a/NOTICE b/NOTICE
index 8f60e86..b976d55 100644
--- a/NOTICE
+++ b/NOTICE
@@ -4,7 +4,10 @@ Balance, Crossfeed, DSP and the Night Shift scheduling and gamma code are from
RevolaOS extensions, revision 36d197b180e55a304ea0468de4330c0d07fe715f.
Night Shift derives from kayboll’s RepriseOS work. Playback Log derives from
the work of brilliant-alien and lgreitis in osos-lab. Its Settings artwork is
-from osos-lab commit 10485fa. The original RevolaOS notice follows.
+from osos-lab commit 10485fa. Cover Grid is from RevolaOS extensions, revision
+72562b52fe7e4074a3479caac86b84ec1d285966; its catalog icon is a Cupertino
+Icons glyph (MIT), see the LICENSE.cupertino-icons beside it. The original
+RevolaOS notice follows.
RevolaOS
Copyright (C) 2026 The RevolaOS Authors. GPL-3.0-only.
diff --git a/README.md b/README.md
index c7692aa..2252230 100644
--- a/README.md
+++ b/README.md
@@ -6,5 +6,6 @@ This repository hosts the source for these RepriseOS extensions and accepts prop
- [Night Shift](extensions/night-shift/) warms the display manually or automatically from sunset to sunrise.
- [Playback Log](extensions/playback-log/) records listening history for scrobbling and optional detailed reports.
- [Crossfeed](extensions/crossfeed/) blends a little of each stereo channel into the other for headphone listening. It uses the included [DSP library](extensions/dsp/).
+- [Cover Grid](extensions/cover-grid/) shows albums as a grid of covers, from a row in Music after Cover Flow and on the main menu.
To add your extension to the catalog, open a PR following the [submission guide](catalog/README.md). Later releases are picked up automatically.
diff --git a/catalog/sources.toml b/catalog/sources.toml
index 1e88372..1897be2 100644
--- a/catalog/sources.toml
+++ b/catalog/sources.toml
@@ -3,6 +3,11 @@ repo = "RepriseOS/repriseos-extensions"
path = "extensions/balance"
tag_prefix = "balance/v"
+[cover-grid]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/cover-grid"
+tag_prefix = "cover-grid/v"
+
[crossfeed]
repo = "RepriseOS/repriseos-extensions"
path = "extensions/crossfeed"
diff --git a/extensions/cover-grid/Makefile b/extensions/cover-grid/Makefile
new file mode 100644
index 0000000..1ee1095
--- /dev/null
+++ b/extensions/cover-grid/Makefile
@@ -0,0 +1,4 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c
+grid_SOURCES := grid.c scale.c
+include $(SDK)/rv.mk
diff --git a/extensions/cover-grid/README.md b/extensions/cover-grid/README.md
new file mode 100644
index 0000000..5aa4804
--- /dev/null
+++ b/extensions/cover-grid/README.md
@@ -0,0 +1,21 @@
+# Cover Grid
+
+Shows albums as a grid of covers, from a row in Music after Cover Flow and on the main menu. It uses Cover Flow's library query: the same music-only albums sorted by artist, and it does not open without music. Like Cover Flow, it reopens where it was left.
+
+The grid shows four columns, two full rows and part of a third. The selected cover is lifted, with an edge in the selection colour and a shadow. Its album and artist scroll in the bar at the bottom. A scrollbar shows while the grid scrolls.
+
+| Action | Result |
+|---|---|
+| Wheel | moves the selection, scrolling by whole rows once it leaves the two full rows |
+| Select | opens the album's songs, at the top of the list |
+| Menu | leaves the grid |
+
+Covers load in the background through Cover Flow's loader, with its placeholder until they arrive. Each is scaled once and kept while the grid is on screen. Colours follow the stock roles, so themes apply.
+
+Leaving the grid frees the covers. Opening an album keeps them for one second, then frees them.
+
+| | |
+|---|---|
+| Default | off |
+| Memory | the extension image while closed; about 160 KB of scaled covers and the artwork session while the grid is on screen |
+| Settings | the two menu visibility flags |
diff --git a/extensions/cover-grid/assets/LICENSE.cupertino-icons b/extensions/cover-grid/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/cover-grid/assets/LICENSE.cupertino-icons
@@ -0,0 +1,20 @@
+The MIT License (MIT)
+
+Copyright (c) 2016 Vladimir Kharlampidi
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
diff --git a/extensions/cover-grid/assets/settings-cover-grid.png b/extensions/cover-grid/assets/settings-cover-grid.png
new file mode 100644
index 0000000..63da36b
Binary files /dev/null and b/extensions/cover-grid/assets/settings-cover-grid.png differ
diff --git a/extensions/cover-grid/assets/settings-cover-grid.svg b/extensions/cover-grid/assets/settings-cover-grid.svg
new file mode 100644
index 0000000..0606062
--- /dev/null
+++ b/extensions/cover-grid/assets/settings-cover-grid.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/cover-grid/geometry.h b/extensions/cover-grid/geometry.h
new file mode 100644
index 0000000..d82fb1c
--- /dev/null
+++ b/extensions/cover-grid/geometry.h
@@ -0,0 +1,94 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* The grid's layout inside the canvas, shared with the host tests: four
+ * columns of covers with gaps and margins, two full rows above the caption
+ * strip and a third peeking in, the lifted selected cover and the scrollbar. */
+
+#ifndef COVER_GRID_GEOMETRY_H
+#define COVER_GRID_GEOMETRY_H
+#include
+
+enum {
+ GRID_COLUMNS = 4,
+ GRID_COVER = 77,
+ GRID_GAP = 2,
+ GRID_MARGIN = 3,
+ GRID_PITCH = GRID_COVER + GRID_GAP,
+ /* The selected cover grows by this much on each side, inside a
+ * selection-coloured edge and a shadow. */
+ GRID_LIFT = 5,
+ GRID_LIFT_SIZE = GRID_COVER + 2 * GRID_LIFT,
+ GRID_EDGE = 2,
+ GRID_SHADOW = 3,
+ /* The caption strip at the bottom: the visible rows of the Now Playing
+ * status bar's background. */
+ GRID_CAPTION = 22,
+ /* Rows that fit entirely above the caption strip; a third peeks below. */
+ GRID_FULL_ROWS = 2,
+ GRID_VISIBLE_ROWS = GRID_FULL_ROWS + 1,
+ GRID_VISIBLE_COVERS = GRID_COLUMNS * GRID_VISIBLE_ROWS,
+};
+
+static inline int grid_clamp(int value, int low, int high)
+{
+ return value < low ? low : value > high ? high : value;
+}
+
+/* The first visible row after moving the selection to `index`: the selection
+ * stays within the two full rows, scrolling by whole rows. */
+static inline int grid_top_row(int top, int index)
+{
+ int row = index / GRID_COLUMNS;
+ if (row < top)
+ return row;
+ if (row >= top + GRID_FULL_ROWS)
+ return row - GRID_FULL_ROWS + 1;
+ return top;
+}
+
+/* The top left of album `index`'s cell with `top` the first visible row; 0
+ * when the cell is not among the visible rows. */
+static inline int grid_cell(int index, int top, int *x, int *y)
+{
+ int row = index / GRID_COLUMNS - top;
+ if (index < 0 || row < 0 || row >= GRID_VISIBLE_ROWS)
+ return 0;
+ *x = GRID_MARGIN + index % GRID_COLUMNS * GRID_PITCH;
+ *y = GRID_MARGIN + row * GRID_PITCH;
+ return 1;
+}
+
+/* The lifted cover's origin for the cover at (x, y): centred on it and shifted
+ * inward so that its edge stays inside `width` and above the caption strip,
+ * `height` being the covers' area. */
+static inline void grid_lift_origin(int x, int y, int width, int height, int *lx,
+ int *ly)
+{
+ *lx = grid_clamp(x - GRID_LIFT, GRID_EDGE, width - GRID_LIFT_SIZE - GRID_EDGE);
+ *ly = grid_clamp(y - GRID_LIFT, GRID_EDGE, height - GRID_LIFT_SIZE - GRID_EDGE);
+}
+
+/* The stock list thumb: a track from 1 px below the top to 2 px above the
+ * bottom of the covers' area, a thumb as long as the visible fraction (at
+ * least `minimum`) placed by the scrolled fraction, with its art reaching 1 px
+ * above and 2 px below it. 0 when the grid does not scroll. */
+static inline int grid_scrollbar_span(int count, int top, int height, int minimum,
+ int *y0, int *y1)
+{
+ int rows = (count + GRID_COLUMNS - 1) / GRID_COLUMNS;
+ if (rows <= GRID_FULL_ROWS)
+ return 0;
+ int track = height - 3;
+ int scroll = (rows - GRID_FULL_ROWS) * GRID_PITCH;
+ int length = track * height / (scroll + height);
+ if (length < minimum)
+ length = minimum;
+ if (length > track)
+ length = track;
+ int position = (track - length) * top * GRID_PITCH / scroll;
+ *y0 = position;
+ *y1 = position + length + 3;
+ return 1;
+}
+
+#endif
diff --git a/extensions/cover-grid/grid.c b/extensions/cover-grid/grid.c
new file mode 100644
index 0000000..98a9bcb
--- /dev/null
+++ b/extensions/cover-grid/grid.c
@@ -0,0 +1,865 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* The grid canvas: covers from Cover Flow's artwork session downscaled into a
+ * cache of the visible cells, the selected one lifted over its neighbours, a
+ * caption strip on the Now Playing status bar's art, the stock scrollbar while
+ * the grid scrolls, and Center opening the album's song list. The cache and
+ * the session live from the first draw to the close, or until the grid has
+ * sat a second under an album's songs. */
+
+#include "geometry.h"
+#include "scale.h"
+#include "shared.h"
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_canvas.h"
+#include "rv_libc.h"
+#include "rv_media.h"
+#include "rv_ui.h"
+#include
+
+enum {
+ CACHE_SLOTS = GRID_VISIBLE_COVERS,
+ PLACEHOLDER =
+ CACHE_SLOTS, /* Cover Flow's no-artwork cover, after the album slots */
+ /* Downscales per draw; covers loaded beyond it follow with the next. */
+ SCALES_PER_DRAW = 4,
+ /* Rows of the lifted cover filtered per blit. */
+ LIFT_STRIP = 8,
+ /* Pixels the lifted cover's edge and shadow reach beyond it. */
+ DECOR = GRID_EDGE + GRID_SHADOW + 1,
+ /* The hairline inside each cover's edge: the text colour at 15 %, so a cover
+ * stays distinct from a background of its own tone. */
+ HAIRLINE_ALPHA = 38,
+ /* An empty tile: the background 11 % toward the text. */
+ TILE_WEIGHT = 28,
+ CAPTION_INSET = 8,
+ /* The bar's art is this tall; the grid shows GRID_CAPTION of it and the
+ * rest falls below the screen. The caption is centred on the whole of it,
+ * as the stock bar's title is. */
+ CAPTION_ART_ROWS = 24,
+ SCROLLBAR_HOLD_MS = 1500,
+ SCROLLBAR_FADE_MS = 500,
+ SCROLLBAR_TICK_MS = 50,
+ MARQUEE_TICK_MS = 40,
+ MARQUEE_HOLD_TICKS = 25,
+ MARQUEE_GAP = 40,
+ IDLE_MS = 1000,
+ /* After the session starts or the grid scrolls, cells still showing the
+ * placeholder are checked this often, this many times, for a cover the
+ * session holds without having reported it. */
+ POLL_MS = 100,
+ POLL_TICKS = 30,
+};
+
+static const uint8_t shadow_alpha[GRID_SHADOW] = {72, 38, 14};
+#define SHADOW 0x000000u
+#define CAPTION_INK 0xffffffffu /* the status bar's title on its black art */
+
+/* A downscaled cover as a BMap, keyed by artwork id (0: empty); the slots are
+ * replaced least recently drawn first. */
+struct cover_slot {
+ uint32_t artwork, used;
+ uint32_t bmap[RV_CANVAS_BMAP_BYTES(GRID_COVER, GRID_COVER) / 4];
+};
+
+static const struct rv_api *api;
+static rv_extension_handle self;
+
+static struct {
+ struct grid_link *link;
+ struct cover_slot *cache;
+ void *session; /* Cover Flow's artwork session: its worker reads covers from disk */
+ uint32_t clock;
+ rv_color hairline; /* the text colour baked into the cache */
+ int width, height; /* the canvas, and the covers' area above the caption */
+ uint16_t late; /* visible cells whose cover loaded beyond a draw's budget */
+ uint16_t waiting; /* visible cells drawn with the placeholder for want of a cover */
+ uint8_t placeholder_ready, drawn, poll_ticks;
+ uint8_t covered, idle_ticks; /* an album's songs are above the grid */
+ struct rv_timer *late_timer, *scrollbar_timer, *marquee_timer, *idle_timer,
+ *poll_timer;
+ uint32_t scrollbar_ms; /* since the last scroll */
+ uint8_t scrollbar_alpha;
+ char caption[256];
+ int caption_width; /* -1 until measured */
+ int marquee_offset, marquee_hold;
+} grid;
+
+static void *model(void)
+{
+ return grid.link->reference ? LibraryModelRef_Get(&grid.link->reference) : NULL;
+}
+
+static int count(void)
+{
+ return grid_album_count(model());
+}
+
+static uint32_t artwork_of(int index)
+{
+ uint8_t *browser = grid_browser(model());
+ if (!browser || index < 0 || index >= count())
+ return 0;
+ const uint8_t *albums = RV_FIELD(browser, MediaBrowser_albums, const uint8_t *);
+ return RV_FIELD(albums + index * AlbumGroup_bytes, AlbumGroup_artwork, uint32_t);
+}
+
+static const uint8_t *stock_bitmap(uint32_t id)
+{
+ return ResourceService_GetBitmap(UI_GetResourceService(), id);
+}
+
+static int bmap_height(const uint8_t *bmap)
+{
+ const int32_t *bounds = (const int32_t *)(bmap + BMap_bounds);
+ return bounds[2] - bounds[0];
+}
+
+static int meets(const struct rv_rect *clip, int x, int y, int width, int height)
+{
+ return width > 0 && height > 0 && x < clip->x + clip->width &&
+ x + width > clip->x && y < clip->y + clip->height && y + height > clip->y;
+}
+
+static void invalidate(int x, int y, int width, int height)
+{
+ struct rv_rect rect = {(int16_t)x, (int16_t)y, (int16_t)width, (int16_t)height};
+ api->ui_invalidate_rect(self, &rect);
+}
+
+/* Redraws one visible cover, or with `lifted` the area it covers when
+ * selected, edge and shadow included. */
+static void invalidate_cover(int index, int lifted)
+{
+ int x, y;
+ if (!grid_cell(index, grid.link->top, &x, &y))
+ return;
+ if (!grid.width) {
+ api->ui_invalidate(self);
+ return;
+ }
+ if (!lifted) {
+ invalidate(x, y, GRID_COVER, GRID_COVER);
+ return;
+ }
+ grid_lift_origin(x, y, grid.width, grid.height, &x, &y);
+ invalidate(x - DECOR, y - DECOR, GRID_LIFT_SIZE + 2 * DECOR,
+ GRID_LIFT_SIZE + 2 * DECOR);
+}
+
+static void stop_timer(struct rv_timer **timer)
+{
+ if (*timer)
+ api->timer_stop(self, *timer);
+ *timer = NULL;
+}
+
+/* Artwork session */
+
+static void late_fired(void *arg);
+static void poll_start(void);
+
+/* The session reports each cover its worker finishes through slot 2 of this
+ * listener's vtable, on the UI task; the other slots are never called. The
+ * redraw waits for the late-cell timer: an invalidation made while a frame
+ * renders is lost. */
+static void cover_loaded(void *listener, void *item, int index)
+{
+ (void)listener;
+ (void)item;
+ int cell = index - grid.link->top * GRID_COLUMNS;
+ if (cell < 0 || cell >= GRID_VISIBLE_COVERS)
+ return;
+ grid.late |= (uint16_t)(1u << cell);
+ if (!grid.late_timer)
+ grid.late_timer = api->timer_start(self, 1, 0, late_fired, NULL);
+}
+
+static const uintptr_t listener_vtable[3] = {0, 0, (uintptr_t)cover_loaded};
+static const uintptr_t *const listener = listener_vtable;
+
+/* Cover Flow's activation: listen, choose the 128 px format, follow the
+ * current music query, centre on the first visible album (the window reaches
+ * a few albums behind its centre and well past the visible rows ahead), then
+ * start the worker. */
+static void start_session(void)
+{
+ void *block = api->alloc(self, ArtworkSession_bytes);
+ if (!block)
+ return;
+ grid.session = ArtworkSession_Construct(block, "CoverGrid");
+ ArtworkSession_Attach(grid.session, (void *)&listener);
+ ArtworkSession_SetFormat(grid.session, ArtworkFormat_Cover128);
+ ArtworkSession_BindQuery(grid.session);
+ ArtworkSession_Center(grid.session, 1, grid.link->top * GRID_COLUMNS);
+ ArtworkSession_Start(grid.session);
+ poll_start();
+}
+
+static void release_session(void)
+{
+ if (grid.session) {
+ ArtworkSession_Stop(grid.session);
+ ArtworkSession_Destroy(grid.session);
+ api->free(self, grid.session);
+ }
+ grid.session = NULL;
+}
+
+/* The session's 128 px RGB565 pixels for an album once its worker has loaded
+ * them; never waits. */
+static const uint8_t *session_cover(int index)
+{
+ uint32_t image[10];
+ uint8_t found = 0;
+ if (!grid.session || index < 0)
+ return NULL;
+ PlaybackArtwork_GetImage(grid.session, image, index, &found,
+ ArtworkFormat_Cover128);
+ return found ? (const uint8_t *)image[0] : NULL;
+}
+
+/* Cover Flow's placeholder cover's pixels and row bytes, or NULL when the
+ * resource is not the 128 px RGB565 bitmap expected. */
+static const uint8_t *placeholder_source(uint32_t *row_bytes)
+{
+ const uint8_t *bmap = stock_bitmap(RES_CoverFlow_Proxy_Image);
+ if (!bmap || *(const uint16_t *)(bmap + BMap_format) != BMapFormat_RGB565)
+ return NULL;
+ const int32_t *bounds = (const int32_t *)(bmap + BMap_bounds);
+ *row_bytes = *(const uint16_t *)(bmap + BMap_stride);
+ if (bounds[2] - bounds[0] != SCALE_SOURCE ||
+ bounds[3] - bounds[1] != SCALE_SOURCE || *row_bytes < SCALE_SOURCE * 2)
+ return NULL;
+ return bmap + BMap_payload;
+}
+
+/* Cache */
+
+static void release_cache(void)
+{
+ api->free(self, grid.cache);
+ grid.cache = NULL;
+ grid.late = 0;
+ grid.waiting = 0;
+}
+
+static void empty_cache(void)
+{
+ for (unsigned int i = 0; i <= CACHE_SLOTS; i++)
+ grid.cache[i].artwork = 0;
+ grid.placeholder_ready = 0;
+ grid.clock = 0;
+}
+
+/* `color` at alpha/255 over an RGB565 pixel. */
+static uint16_t blend565(uint16_t pixel, rv_color color, uint32_t alpha)
+{
+ uint32_t r = (pixel >> 11) << 3 | pixel >> 13;
+ uint32_t g = (pixel >> 5 & 0x3f) << 2 | (pixel >> 9 & 3);
+ uint32_t b = (pixel & 0x1f) << 3 | (pixel >> 2 & 7);
+ uint32_t over = 255 - alpha;
+ r = (r * over + (color >> 16 & 0xff) * alpha + 127) / 255;
+ g = (g * over + (color >> 8 & 0xff) * alpha + 127) / 255;
+ b = (b * over + (color & 0xff) * alpha + 127) / 255;
+ return (uint16_t)((r >> 3) << 11 | (g >> 2) << 5 | b >> 3);
+}
+
+/* A whole cover at grid size into a slot, with the hairline inside its edge. */
+static void downscale(struct cover_slot *slot, const uint8_t *pixels,
+ uint32_t row_bytes)
+{
+ enum { PITCH = RV_CANVAS_BMAP_PITCH(GRID_COVER), LAST = GRID_COVER - 1 };
+
+ uint16_t *out = rv_canvas_bmap(slot->bmap, GRID_COVER, GRID_COVER);
+ struct scale scale;
+ scale_init(&scale, GRID_COVER);
+ for (int y = 0; y < GRID_COVER; y++)
+ scale_row(out + y * PITCH, &scale, pixels, row_bytes, y, 0, GRID_COVER);
+ for (int i = 0; i < GRID_COVER; i++) {
+ out[i] = blend565(out[i], grid.hairline, HAIRLINE_ALPHA);
+ out[LAST * PITCH + i] =
+ blend565(out[LAST * PITCH + i], grid.hairline, HAIRLINE_ALPHA);
+ }
+ for (int y = 1; y < LAST; y++) {
+ out[y * PITCH] = blend565(out[y * PITCH], grid.hairline, HAIRLINE_ALPHA);
+ out[y * PITCH + LAST] =
+ blend565(out[y * PITCH + LAST], grid.hairline, HAIRLINE_ALPHA);
+ }
+}
+
+static struct cover_slot *placeholder(void)
+{
+ if (!grid.cache)
+ return NULL;
+ struct cover_slot *slot = &grid.cache[PLACEHOLDER];
+ if (!grid.placeholder_ready) {
+ uint32_t row_bytes;
+ const uint8_t *pixels = placeholder_source(&row_bytes);
+ if (!pixels)
+ return NULL;
+ downscale(slot, pixels, row_bytes);
+ grid.placeholder_ready = 1;
+ }
+ return slot;
+}
+
+/* The album's slot, downscaling its cover while `budget` remains once the
+ * session has it; NULL draws the placeholder, with `late` set when the cover
+ * is there but the budget was spent. */
+static struct cover_slot *cover(int index, int *budget, int *late)
+{
+ uint32_t artwork = artwork_of(index);
+ if (!grid.cache || !artwork)
+ return NULL;
+ struct cover_slot *victim = &grid.cache[0];
+ for (unsigned int i = 0; i < CACHE_SLOTS; i++) {
+ struct cover_slot *slot = &grid.cache[i];
+ if (slot->artwork == artwork) {
+ slot->used = ++grid.clock;
+ return slot;
+ }
+ if (!slot->artwork || (victim->artwork && slot->used < victim->used))
+ victim = slot;
+ }
+ const uint8_t *pixels = session_cover(index);
+ if (!pixels)
+ return NULL;
+ if (*budget <= 0) {
+ *late = 1;
+ return NULL;
+ }
+ (*budget)--;
+ downscale(victim, pixels, SCALE_SOURCE * 2);
+ victim->artwork = artwork;
+ victim->used = ++grid.clock;
+ return victim;
+}
+
+/* Timers */
+
+/* Covers loaded beyond a draw's budget, and covers the session reported,
+ * are redrawn from here, outside any frame. */
+static void late_fired(void *arg)
+{
+ (void)arg;
+ grid.late_timer = NULL;
+ uint16_t late = grid.late;
+ grid.late = 0;
+ for (int cell = 0; late; cell++, late >>= 1)
+ if (late & 1) {
+ int index = grid.link->top * GRID_COLUMNS + cell;
+ invalidate_cover(index, index == grid.link->selected);
+ }
+}
+
+/* The session reports covers its worker reads from disk; cells still on the
+ * placeholder are checked for a while after a start or a scroll in case a
+ * cover turned up without a report. */
+static void poll_tick(void *arg)
+{
+ (void)arg;
+ uint16_t waiting = grid.waiting;
+ for (int cell = 0; waiting; cell++, waiting >>= 1) {
+ int index = grid.link->top * GRID_COLUMNS + cell;
+ if ((waiting & 1) && session_cover(index)) {
+ grid.waiting &= (uint16_t)~(1u << cell);
+ invalidate_cover(index, index == grid.link->selected);
+ }
+ }
+ if (!grid.waiting || !--grid.poll_ticks)
+ stop_timer(&grid.poll_timer);
+}
+
+static void poll_start(void)
+{
+ grid.poll_ticks = POLL_TICKS;
+ if (!grid.poll_timer)
+ grid.poll_timer = api->timer_start(self, POLL_MS, 1, poll_tick, NULL);
+}
+
+/* Like the stock lists, the scrollbar shows when the grid scrolls, holds for
+ * 1.5 s after the last scroll, then fades out over 0.5 s. */
+static uint32_t scrollbar_alpha(void)
+{
+ if (!grid.scrollbar_timer)
+ return 0;
+ if (grid.scrollbar_ms < SCROLLBAR_HOLD_MS)
+ return 255;
+ if (grid.scrollbar_ms >= SCROLLBAR_HOLD_MS + SCROLLBAR_FADE_MS)
+ return 0;
+ return 255 - (grid.scrollbar_ms - SCROLLBAR_HOLD_MS) * 255 / SCROLLBAR_FADE_MS;
+}
+
+static void scrollbar_tick(void *arg)
+{
+ (void)arg;
+ grid.scrollbar_ms += SCROLLBAR_TICK_MS;
+ uint32_t alpha = scrollbar_alpha();
+ if (alpha == grid.scrollbar_alpha)
+ return;
+ const uint8_t *art = stock_bitmap(RES_System_ScrollbarCenter_Image);
+ int width = art ? ((const int32_t *)(art + BMap_bounds))[3] : 0;
+ invalidate(grid.width - width, 0, width, grid.height);
+ if (!alpha)
+ stop_timer(&grid.scrollbar_timer);
+}
+
+static void scrollbar_show(void)
+{
+ grid.scrollbar_ms = 0;
+ if (!grid.scrollbar_timer)
+ grid.scrollbar_timer =
+ api->timer_start(self, SCROLLBAR_TICK_MS, 1, scrollbar_tick, NULL);
+}
+
+/* A caption too long for the strip scrolls across it after a pause and
+ * starts over after a gap; one that fits stands still. */
+static void marquee_tick(void *arg)
+{
+ (void)arg;
+ if (grid.marquee_hold) {
+ grid.marquee_hold--;
+ return;
+ }
+ if (++grid.marquee_offset >= grid.caption_width + MARQUEE_GAP) {
+ grid.marquee_offset = 0;
+ grid.marquee_hold = MARQUEE_HOLD_TICKS;
+ }
+ invalidate(0, grid.height, grid.width, GRID_CAPTION);
+}
+
+/* While an album's songs cover the grid, its covers are worth keeping for a
+ * moment in case the list is left at once, and not worth keeping after that.
+ * The core tells a screen when it is pushed and when it closed, but not when
+ * another screen covers or uncovers it, so a draw stands in for the latter:
+ * the first tick absorbs the frames of the push transition, and a draw in any
+ * tick after it means the grid is on top again. Input means the same and is
+ * the surer signal, since a grid nobody touches never draws. */
+static void idle_tick(void *arg)
+{
+ (void)arg;
+ if (!grid.covered || !grid.idle_ticks++) {
+ grid.drawn = 0;
+ return;
+ }
+ if (grid.drawn) {
+ grid.covered = 0;
+ stop_timer(&grid.idle_timer);
+ return;
+ }
+ release_cache();
+ release_session();
+ stop_timer(&grid.poll_timer);
+ stop_timer(&grid.idle_timer);
+}
+
+/* Caption */
+
+/* The album's name, as the stock Albums list shows it. */
+static void album_name(void *browser, int index, struct NativeString *out)
+{
+ struct {
+ uint16_t units;
+ uint16_t text[255];
+ } name;
+
+ name.units = 0;
+ MediaBrowser_ReadAlbumName(browser, index, &name.units);
+ NativeString_AssignUtf16(out, name.text, name.units < 255 ? name.units : 255);
+}
+
+static uint32_t append(uint32_t length, const char *text)
+{
+ if (!text || length + 1 >= sizeof grid.caption)
+ return length;
+ return length +
+ rv_copy_utf8(grid.caption + length, sizeof grid.caption - length, text);
+}
+
+/* Album and artist read like the stock Albums list: the name, then its
+ * subtitle, joined by a dash (the title font has no middle dot). */
+static void update_caption(void)
+{
+ _Alignas(uint32_t) uint8_t storage[NativeString_bytes];
+ struct NativeString *string = (struct NativeString *)storage;
+ void *library = model();
+ int selected = grid.link->selected;
+ uint32_t length = 0;
+ grid.caption[0] = 0;
+ if (library && selected < count()) {
+ NativeString_InitEmpty(string);
+ album_name(grid_browser(library), selected, string);
+ length = append(length, NativeString_GetUtf8(string));
+ NativeString_Destroy(string);
+ NativeString_InitEmpty(string);
+ LibraryBrowser_CopyAlbumArtistSubtitle(library, selected, string);
+ const char *artist = NativeString_GetUtf8(string);
+ if (artist && *artist) {
+ length = append(length, " - ");
+ append(length, artist);
+ }
+ NativeString_Destroy(string);
+ }
+ grid.caption_width = -1;
+ grid.marquee_offset = 0;
+ grid.marquee_hold = MARQUEE_HOLD_TICKS;
+ stop_timer(&grid.marquee_timer);
+ if (grid.width)
+ invalidate(0, grid.height, grid.width, GRID_CAPTION);
+}
+
+/* Drawing */
+
+static void fill(const struct rv_gfx *g, int x, int y, int width, int height,
+ rv_color color)
+{
+ struct rv_rect rect = {(int16_t)x, (int16_t)y, (int16_t)width, (int16_t)height};
+ if (width > 0 && height > 0)
+ g->fill(&rect, color);
+}
+
+/* A rectangle's outline `thickness` px wide, inside it. */
+static void frame(const struct rv_gfx *g, int x, int y, int width, int height,
+ int thickness, rv_color color)
+{
+ fill(g, x, y, width, thickness, color);
+ fill(g, x, y + height - thickness, width, thickness, color);
+ fill(g, x, y + thickness, thickness, height - 2 * thickness, color);
+ fill(g, x + width - thickness, y + thickness, thickness, height - 2 * thickness,
+ color);
+}
+
+/* Background in the margins and gaps around the covers; the covers and the
+ * caption paint the rest. */
+static void draw_gaps(const struct rv_gfx *g, const struct rv_rect *clip,
+ rv_color background)
+{
+ int width = grid.width, height = grid.height;
+ if (meets(clip, 0, 0, width, GRID_MARGIN))
+ fill(g, 0, 0, width, GRID_MARGIN, background);
+ for (int column = 0; column <= GRID_COLUMNS; column++) {
+ int x = column ? GRID_MARGIN + column * GRID_PITCH - GRID_GAP : 0;
+ int gap = !column ? GRID_MARGIN : column == GRID_COLUMNS ? width - x : GRID_GAP;
+ if (meets(clip, x, GRID_MARGIN, gap, height - GRID_MARGIN))
+ fill(g, x, GRID_MARGIN, gap, height - GRID_MARGIN, background);
+ }
+ for (int row = 0; row < GRID_VISIBLE_ROWS; row++) {
+ int y = GRID_MARGIN + row * GRID_PITCH + GRID_COVER;
+ int gap = height - y < GRID_GAP ? height - y : GRID_GAP;
+ if (meets(clip, 0, y, width, gap))
+ fill(g, 0, y, width, gap, background);
+ }
+}
+
+/* The selected cover, lifted over its neighbours: its shadow, its edge in the
+ * selection colour, then its art box-filtered strip by strip from the
+ * session's 128 px cover or Cover Flow's placeholder. */
+static void draw_lifted(const struct rv_gfx *g, const struct rv_rect *clip, int x,
+ int y, rv_color tile)
+{
+ int edge = GRID_LIFT_SIZE + 2 * GRID_EDGE;
+ grid_lift_origin(x, y, grid.width, grid.height, &x, &y);
+ if (!meets(clip, x - DECOR, y - DECOR, GRID_LIFT_SIZE + 2 * DECOR,
+ GRID_LIFT_SIZE + 2 * DECOR))
+ return;
+ for (int d = GRID_SHADOW; d >= 1; d--)
+ frame(g, x - GRID_EDGE - d, y - GRID_EDGE - d + 1, edge + 2 * d, edge + 2 * d,
+ 1, (rv_color)shadow_alpha[d - 1] << 24 | SHADOW);
+ frame(g, x - GRID_EDGE, y - GRID_EDGE, edge, edge, GRID_EDGE,
+ g->color(RV_COLOR_HIGHLIGHT));
+ uint32_t row_bytes = SCALE_SOURCE * 2;
+ const uint8_t *source = session_cover(grid.link->selected);
+ if (!source)
+ source = placeholder_source(&row_bytes);
+ if (!source) {
+ fill(g, x, y, GRID_LIFT_SIZE, GRID_LIFT_SIZE, tile);
+ return;
+ }
+
+ enum { PITCH = RV_CANVAS_BMAP_PITCH(GRID_LIFT_SIZE) };
+
+ uint32_t strip[RV_CANVAS_BMAP_BYTES(GRID_LIFT_SIZE, LIFT_STRIP) / 4];
+ uint16_t *pixels = rv_canvas_bmap(strip, GRID_LIFT_SIZE, LIFT_STRIP);
+ struct scale scale;
+ scale_init(&scale, GRID_LIFT_SIZE);
+ for (int first = 0; first < GRID_LIFT_SIZE; first += LIFT_STRIP) {
+ int rows =
+ GRID_LIFT_SIZE - first < LIFT_STRIP ? GRID_LIFT_SIZE - first : LIFT_STRIP;
+ if (!meets(clip, x, y + first, GRID_LIFT_SIZE, rows))
+ continue;
+ for (int row = 0; row < rows; row++)
+ scale_row(pixels + row * PITCH, &scale, source, row_bytes, first + row, 0,
+ GRID_LIFT_SIZE);
+ g->bmap_rows(x, y + first, strip, rows, 0);
+ }
+}
+
+/* Caps and a tiled centre, flush with the right edge over the covers. */
+static void draw_scrollbar(const struct rv_gfx *g, const struct rv_rect *clip,
+ int albums)
+{
+ uint32_t alpha = scrollbar_alpha();
+ grid.scrollbar_alpha = (uint8_t)alpha;
+ if (!alpha)
+ return;
+ const uint8_t *top = stock_bitmap(RES_System_ScrollbarTop_Image);
+ const uint8_t *centre = stock_bitmap(RES_System_ScrollbarCenter_Image);
+ const uint8_t *bottom = stock_bitmap(RES_System_ScrollbarBottom_Image);
+ if (!top || !centre || !bottom)
+ return;
+ int width = ((const int32_t *)(centre + BMap_bounds))[3];
+ int top_rows = bmap_height(top), centre_rows = bmap_height(centre),
+ bottom_rows = bmap_height(bottom);
+ int y0, y1;
+ if (!grid_scrollbar_span(albums, grid.link->top, grid.height,
+ top_rows + bottom_rows - 3, &y0, &y1))
+ return;
+ int x = grid.width - width;
+ if (!meets(clip, x, y0, width, y1 - y0))
+ return;
+ int end = y1 - bottom_rows;
+ g->bmap_rows_blend(x, y0, top, top_rows, (uint8_t)alpha);
+ for (int y = y0 + top_rows; y < end; y += centre_rows)
+ g->bmap_rows_blend(x, y, centre, end - y < centre_rows ? end - y : centre_rows,
+ (uint8_t)alpha);
+ g->bmap_rows_blend(x, end, bottom, bottom_rows, (uint8_t)alpha);
+}
+
+/* The status bar's art with the caption on it, centred when it fits. */
+static void draw_caption(const struct rv_gfx *g, const struct rv_rect *clip,
+ rv_color background)
+{
+ int y = grid.height, width = grid.width;
+ if (!meets(clip, 0, y, width, GRID_CAPTION))
+ return;
+ /* The bar's art is translucent at its edges, as it is over the Now Playing
+ * background, so the strip is painted before it or the covers show
+ * through. */
+ fill(g, 0, y, width, GRID_CAPTION, background);
+ const uint8_t *bar = stock_bitmap(RES_StatusBarBlack_Background_Image);
+ if (bar)
+ g->bmap_rows(0, y, bar, GRID_CAPTION, 0);
+ if (!grid.caption[0])
+ return;
+ if (grid.caption_width < 0)
+ grid.caption_width = g->measure(RV_FONT_TITLE, grid.caption);
+ int text_y = y + (CAPTION_ART_ROWS - g->line_height(RV_FONT_TITLE)) / 2;
+ if (grid.caption_width <= width - 2 * CAPTION_INSET) {
+ stop_timer(&grid.marquee_timer);
+ g->text((width - grid.caption_width) / 2, text_y, RV_FONT_TITLE, CAPTION_INK,
+ grid.caption);
+ return;
+ }
+ int x = CAPTION_INSET - grid.marquee_offset;
+ g->text(x, text_y, RV_FONT_TITLE, CAPTION_INK, grid.caption);
+ if (x + grid.caption_width + MARQUEE_GAP < width)
+ g->text(x + grid.caption_width + MARQUEE_GAP, text_y, RV_FONT_TITLE,
+ CAPTION_INK, grid.caption);
+ if (!grid.marquee_timer)
+ grid.marquee_timer =
+ api->timer_start(self, MARQUEE_TICK_MS, 1, marquee_tick, NULL);
+}
+
+static void draw(const struct rv_context *ctx, const struct rv_gfx *g, int width,
+ int height)
+{
+ (void)ctx;
+ const struct rv_rect *clip = g->clip();
+ rv_color background = g->color(RV_COLOR_BACKGROUND), text = g->color(RV_COLOR_TEXT);
+ rv_color tile = g->mix(background, text, TILE_WEIGHT);
+ grid.width = width;
+ grid.height = height - GRID_CAPTION;
+ grid.drawn = 1;
+ /* A theme that changed the text colour changes the baked hairlines. */
+ if (grid.cache && grid.hairline != text)
+ empty_cache();
+ grid.hairline = text;
+ if (!grid.cache &&
+ (grid.cache = api->alloc(self, (CACHE_SLOTS + 1) * sizeof *grid.cache)))
+ empty_cache();
+ if (!grid.session)
+ start_session();
+
+ draw_gaps(g, clip, background);
+ int albums = count(), budget = SCALES_PER_DRAW;
+ int lifted_x = -1, lifted_y = 0;
+ uint16_t late = 0;
+ for (int row = 0; row < GRID_VISIBLE_ROWS; row++) {
+ int y = GRID_MARGIN + row * GRID_PITCH;
+ int below = grid.height - y;
+ if (below <= 0)
+ break;
+ int side = below < GRID_COVER ? below : GRID_COVER;
+ for (int column = 0; column < GRID_COLUMNS; column++) {
+ int index = (grid.link->top + row) * GRID_COLUMNS + column;
+ int x = GRID_MARGIN + column * GRID_PITCH;
+ if (index == grid.link->selected && index < albums) {
+ lifted_x = x;
+ lifted_y = y;
+ continue;
+ }
+ if (!meets(clip, x, y, GRID_COVER, side))
+ continue;
+ if (index >= albums) {
+ fill(g, x, y, GRID_COVER, side, background);
+ continue;
+ }
+ int pending = 0;
+ uint16_t bit = (uint16_t)(1u << (row * GRID_COLUMNS + column));
+ struct cover_slot *slot = cover(index, &budget, &pending);
+ if (pending)
+ late |= bit;
+ if (slot || pending || !artwork_of(index))
+ grid.waiting &= (uint16_t)~bit;
+ else
+ grid.waiting |= bit;
+ if (!slot)
+ slot = placeholder();
+ if (slot)
+ g->bmap(x, y, slot->bmap);
+ else
+ fill(g, x, y, GRID_COVER, side, tile);
+ }
+ }
+ if (lifted_x >= 0)
+ draw_lifted(g, clip, lifted_x, lifted_y, tile);
+ draw_scrollbar(g, clip, albums);
+ draw_caption(g, clip, background);
+ grid.late |= late;
+ if (grid.late && !grid.late_timer)
+ grid.late_timer = api->timer_start(self, 1, 0, late_fired, NULL);
+}
+
+/* Input */
+
+/* Like the stock Albums list: select the album on the shared browser, title
+ * the song list with its name, then push it in its plain layout on the grid's
+ * model. The list's saved row is cleared first so it opens at the top. */
+static void open_album(const struct rv_context *ctx)
+{
+ struct grid_link *link = grid.link;
+ void *browser = grid_browser(model());
+ if (!browser || link->selected >= count())
+ return;
+ if (MediaBrowser_SelectAlbumIndex(browser, link->selected))
+ return;
+ _Alignas(uint32_t) uint8_t storage[NativeString_bytes];
+ struct NativeString *title = (struct NativeString *)storage;
+ NativeString_InitEmpty(title);
+ album_name(browser, link->selected, title);
+ MediaListSelection_SetTitle(MediaListSelection_GetInstance(), title);
+ NativeString_Destroy(title);
+ UI_SetScreenState(UI_GetLayoutManager(), RES_MediaLists_Songs_Screen, 0, 0, 0, 0);
+ void *screen = UI_CreateScreenController(
+ UI_GetControllerRegistry(), RES_MediaLists_Songs_Screen,
+ RES_MediaLists_Songs_Plain_Layout, Transition_ForegroundPush);
+ if (!screen)
+ return;
+ LibraryModelRef_Assign((uint8_t *)screen + MediaListController_model,
+ &link->reference);
+ stop_timer(&grid.scrollbar_timer);
+ stop_timer(&grid.marquee_timer);
+ grid.drawn = 0;
+ grid.covered = 1;
+ grid.idle_ticks = 0;
+ if (!grid.idle_timer)
+ grid.idle_timer = api->timer_start(self, IDLE_MS, 1, idle_tick, NULL);
+ rv_controller_push(ctx->controller, screen);
+}
+
+static int input(const struct rv_context *ctx, const struct rv_input *in)
+{
+ struct grid_link *link = grid.link;
+ /* Only the top screen is given input, so the grid is not covered. */
+ if (grid.covered) {
+ grid.covered = 0;
+ stop_timer(&grid.idle_timer);
+ }
+ if (in->kind == RV_INPUT_WHEEL) {
+ int albums = count();
+ if (!albums)
+ return 1;
+ int next = grid_clamp(link->selected + in->delta, 0, albums - 1);
+ if (next == link->selected)
+ return 1;
+ int previous = link->selected;
+ link->selected = next;
+ int top = grid_top_row(link->top, next);
+ if (top != link->top) {
+ if (grid.session)
+ ArtworkSession_Center(grid.session, 0,
+ (top - link->top) * GRID_COLUMNS);
+ link->top = top;
+ grid.waiting = 0;
+ scrollbar_show();
+ poll_start();
+ api->ui_invalidate(self);
+ } else {
+ /* Only the two lifted areas change. */
+ invalidate_cover(previous, 1);
+ invalidate_cover(next, 1);
+ }
+ update_caption();
+ return 1;
+ }
+ if (in->kind == RV_INPUT_PRESS)
+ return in->button == RV_BUTTON_CENTER;
+ if (in->kind == RV_INPUT_RELEASE && in->button == RV_BUTTON_CENTER) {
+ open_album(ctx);
+ return 1;
+ }
+ return in->kind == RV_INPUT_HOLD;
+}
+
+static void opened(const struct rv_context *ctx)
+{
+ memset(&grid, 0, sizeof grid);
+ grid.link = ctx->arg;
+ grid.caption_width = -1;
+ update_caption();
+}
+
+static void release_grid(void)
+{
+ stop_timer(&grid.late_timer);
+ stop_timer(&grid.scrollbar_timer);
+ stop_timer(&grid.marquee_timer);
+ stop_timer(&grid.idle_timer);
+ stop_timer(&grid.poll_timer);
+ release_session();
+ release_cache();
+ if (grid.link && grid.link->reference)
+ LibraryModelRef_Release(&grid.link->reference);
+ if (grid.link)
+ grid.link->reference = NULL;
+}
+
+/* After the last draw of the slide-out. */
+static void closed(const struct rv_context *ctx)
+{
+ (void)ctx;
+ release_grid();
+}
+
+static const struct rv_screen screens[] = {
+ RV_SCREEN_CANVAS("grid", "Cover Grid", draw, input, .opened = opened,
+ .closed = closed),
+};
+
+/* An unload while the grid is open, as after disk mode, gets no closed: the
+ * worker still reads into the session. */
+static const struct rv_descriptor module = {
+ .size = sizeof module,
+ RV_SCREENS(screens),
+ .unload = release_grid,
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *core,
+ rv_extension_handle handle)
+{
+ api = core;
+ self = handle;
+ return &module;
+}
diff --git a/extensions/cover-grid/listing.toml b/extensions/cover-grid/listing.toml
new file mode 100644
index 0000000..479f281
--- /dev/null
+++ b/extensions/cover-grid/listing.toml
@@ -0,0 +1,11 @@
+details = "Browse albums as a grid of covers from a Cover Grid row in Music after Cover Flow, and on the main menu once that row is switched on in Settings > Main Menu. Turn the wheel to move across the covers, press Select to open the album's songs."
+
+icon = "assets/settings-cover-grid.png"
+
+[[screenshots]]
+file = "screenshots/grid.png"
+alt = "The Cover Grid: four columns of album covers, the top-left cover lifted with a blue edge and a shadow, its album and artist in the bar at the bottom."
+
+[[screenshots]]
+file = "screenshots/album.png"
+alt = "An album's songs opened from the grid, in Apple's Songs list."
diff --git a/extensions/cover-grid/main.c b/extensions/cover-grid/main.c
new file mode 100644
index 0000000..b7bbdbd
--- /dev/null
+++ b/extensions/cover-grid/main.c
@@ -0,0 +1,75 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Cover Grid: a row in Music and on the main menu that opens the grid on
+ * Cover Flow's library query, music-only album groups sorted by artist. */
+
+#include "geometry.h"
+#include "shared.h"
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_menus.h"
+#include "rv_ui.h"
+#include
+
+static const struct rv_api *api;
+static rv_extension_handle self;
+
+/* The grid reopens on the last album while it exists; the screen module
+ * unloads after the grid closes, so the selection lives here. */
+static struct grid_link link;
+
+static void release(void)
+{
+ if (link.reference)
+ LibraryModelRef_Release(&link.reference);
+ link.reference = NULL;
+}
+
+static void open_grid(const struct rv_context *ctx)
+{
+ void *menu = ctx->controller;
+ /* Cover Flow's handler raises the artwork preview first; the query returns
+ * the album count, 0 for a library without music. */
+ RV_FIELD(menu, MainMediaMenu_preview_kind, uint8_t) = 1;
+ if (!MusicMenu_ConfigureLibraryBrowser(
+ menu, MediaKind_Music, 0, 1, LibraryGrouping_Albums, LibrarySort_Artist, 0))
+ return;
+ release();
+ void *model = LibraryBrowser_Create(0);
+ if (!model)
+ return;
+ LibraryModelRef_Init(&link.reference, model, 0);
+ if (link.selected >= grid_album_count(model))
+ link.selected = link.top = 0;
+ link.top = grid_top_row(link.top, link.selected);
+ if (api->ui_push(self, "grid", &link) != RV_OK)
+ release();
+}
+
+static const struct rv_menu_entry entries[] = {
+ RV_MENU_ENTRY(RV_MENU_MUSIC, RV_MUSIC_COVER_FLOW,
+ RV_ROW_ACTION("Cover Grid", open_grid), .shown_by = KEY_IN_MUSIC),
+ RV_MENU_ENTRY(RV_MENU_MAIN, RV_MAIN_COVER_FLOW,
+ RV_ROW_ACTION("Cover Grid", open_grid), .shown_by = KEY_IN_MAIN,
+ .preview = RV_PREVIEW_MUSIC),
+};
+
+static const struct rv_setting settings[] = {
+ RV_SETTING_BOOL(KEY_IN_MUSIC, "Cover Grid in Music", 1),
+ RV_SETTING_BOOL(KEY_IN_MAIN, "Cover Grid in Main Menu", 0),
+};
+
+static const struct rv_descriptor extension = {
+ .size = sizeof extension,
+ .unload = release,
+ RV_SETTINGS(settings),
+ RV_ENTRIES(entries),
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *core,
+ rv_extension_handle handle)
+{
+ api = core;
+ self = handle;
+ return &extension;
+}
diff --git a/extensions/cover-grid/manifest.toml b/extensions/cover-grid/manifest.toml
new file mode 100644
index 0000000..cc5bf20
--- /dev/null
+++ b/extensions/cover-grid/manifest.toml
@@ -0,0 +1,18 @@
+id = "cover-grid"
+name = "Cover Grid"
+description = "Browse albums as a grid of covers next to Cover Flow, in Music and on the main menu."
+version = "0.1.0"
+author = "RevolaOS"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
+
+[[module]]
+name = "grid"
+trigger = "screen"
+screens = ["grid"]
diff --git a/extensions/cover-grid/scale.c b/extensions/cover-grid/scale.c
new file mode 100644
index 0000000..383b6cb
--- /dev/null
+++ b/extensions/cover-grid/scale.c
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+#include "scale.h"
+
+/* The CPU has no divider. Each output pixel covers 2 or 3 source pixels per
+ * axis, so the filter divides by 4, 6 or 9; this 16.16 reciprocal rounds
+ * exactly for every channel sum it can see (at most 9 * 63). */
+static inline uint32_t divide_rounded(uint32_t sum, uint32_t n)
+{
+ static const uint16_t reciprocal[10] = {0, 0, 32768, 21846, 16384,
+ 13108, 10923, 9363, 8192, 7282};
+ return (sum + n / 2) * reciprocal[n] >> 16;
+}
+
+/* RGB565 spread so that nine pixels sum without carries: blue in bits 0-10,
+ * red in 11-20 and green from 21. */
+static inline uint32_t spread(uint32_t pixel)
+{
+ return (pixel | pixel << 16) & 0x07e0f81fu;
+}
+
+void scale_init(struct scale *scale, int size)
+{
+ for (int i = 0; i < size; i++) {
+ scale->first[i] = (uint8_t)(i * SCALE_SOURCE / size);
+ scale->span[i] =
+ (uint8_t)(((i + 1) * SCALE_SOURCE + size - 1) / size - scale->first[i]);
+ }
+}
+
+void scale_row(uint16_t *out, const struct scale *scale, const uint8_t *pixels,
+ uint32_t row_bytes, int y, int x0, int x1)
+{
+ const uint8_t *first = scale->first, *span = scale->span;
+ const uint16_t *row0 = (const uint16_t *)(pixels + first[y] * row_bytes);
+ const uint16_t *row1 = (const uint16_t *)((const uint8_t *)row0 + row_bytes);
+ const uint16_t *row2 = (const uint16_t *)((const uint8_t *)row1 + row_bytes);
+ int tall = span[y] == 3;
+ for (int x = x0; x < x1; x++) {
+ int c = first[x], wide = span[x] == 3;
+ uint32_t sum = spread(row0[c]) + spread(row0[c + 1]) + spread(row1[c]) +
+ spread(row1[c + 1]);
+ if (wide)
+ sum += spread(row0[c + 2]) + spread(row1[c + 2]);
+ if (tall) {
+ sum += spread(row2[c]) + spread(row2[c + 1]);
+ if (wide)
+ sum += spread(row2[c + 2]);
+ }
+ uint32_t n = (uint32_t)span[y] * span[x];
+ *out++ = (uint16_t)(divide_rounded(sum >> 11 & 0x3ff, n) << 11 |
+ divide_rounded(sum >> 21, n) << 5 |
+ divide_rounded(sum & 0x7ff, n));
+ }
+}
diff --git a/extensions/cover-grid/scale.h b/extensions/cover-grid/scale.h
new file mode 100644
index 0000000..b3376f9
--- /dev/null
+++ b/extensions/cover-grid/scale.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Box filter from a 128 px RGB565 cover to the grid's sizes. */
+
+#ifndef COVER_GRID_SCALE_H
+#define COVER_GRID_SCALE_H
+#include
+
+enum { SCALE_SOURCE = 128, SCALE_MAX = 87 };
+
+/* The source pixels each output pixel covers at one output size: the first of
+ * 2 or 3 per axis. */
+struct scale {
+ uint8_t first[SCALE_MAX], span[SCALE_MAX];
+};
+
+void scale_init(struct scale *scale, int size);
+
+/* Pixels [x0, x1) of output row `y`, written from `out` onward: each the
+ * average of the 2 or 3 by 2 or 3 source pixels it covers, from the source
+ * rows `row_bytes` apart. */
+void scale_row(uint16_t *out, const struct scale *scale, const uint8_t *pixels,
+ uint32_t row_bytes, int y, int x0, int x1);
+
+#endif
diff --git a/extensions/cover-grid/screenshots/album.png b/extensions/cover-grid/screenshots/album.png
new file mode 100644
index 0000000..c0a66dc
Binary files /dev/null and b/extensions/cover-grid/screenshots/album.png differ
diff --git a/extensions/cover-grid/screenshots/grid.png b/extensions/cover-grid/screenshots/grid.png
new file mode 100644
index 0000000..cce1cd2
Binary files /dev/null and b/extensions/cover-grid/screenshots/grid.png differ
diff --git a/extensions/cover-grid/shared.h b/extensions/cover-grid/shared.h
new file mode 100644
index 0000000..667d8e3
--- /dev/null
+++ b/extensions/cover-grid/shared.h
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* What the main module hands the grid screen: the library model the grid
+ * browses and the selection it reopens on. */
+
+#ifndef COVER_GRID_SHARED_H
+#define COVER_GRID_SHARED_H
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include
+
+#define KEY_IN_MUSIC 1 /* Settings > Music Menu and Main Menu flip these two */
+#define KEY_IN_MAIN 2
+
+struct grid_link {
+ void *reference; /* LibraryModelRef on the shared music browser, NULL when none */
+ int selected, top;
+};
+
+/* The shared browser behind a library model, and its album groups. */
+static inline void *grid_browser(void *model)
+{
+ return model ? RV_FIELD(model, LibraryBrowser_browser, void *) : NULL;
+}
+
+static inline int grid_album_count(void *model)
+{
+ uint8_t *browser = grid_browser(model);
+ if (!browser)
+ return 0;
+ const uint8_t *begin = RV_FIELD(browser, MediaBrowser_albums, const uint8_t *);
+ const uint8_t *end = RV_FIELD(browser, MediaBrowser_albums_end, const uint8_t *);
+ return begin ? (int)((end - begin) / AlbumGroup_bytes) : 0;
+}
+
+#endif
diff --git a/extensions/cover-grid/tests/contracts.c b/extensions/cover-grid/tests/contracts.c
new file mode 100644
index 0000000..6b7cefd
--- /dev/null
+++ b/extensions/cover-grid/tests/contracts.c
@@ -0,0 +1,187 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Host contracts for the grid's maths: the box filter against a floating-point
+ * box average, the scroll and lift geometry against the rules they implement. */
+
+#include "../geometry.h"
+#include "../scale.h"
+#include
+#include
+#include
+#include
+
+static int failures;
+
+#define CHECK(condition) \
+ do { \
+ if (!(condition)) { \
+ failures++; \
+ printf("%s:%d: %s\n", __FILE__, __LINE__, #condition); \
+ } \
+ } while (0)
+
+static uint32_t next = 1;
+
+static uint16_t noise(void)
+{
+ next = next * 1103515245u + 12345u;
+ return (uint16_t)(next >> 8);
+}
+
+/* The exact box average of the source pixels an output pixel covers, each
+ * channel rounded half up. */
+static uint16_t reference(const uint16_t *source, int size, int x, int y)
+{
+ int x0 = x * SCALE_SOURCE / size, x1 = ((x + 1) * SCALE_SOURCE + size - 1) / size;
+ int y0 = y * SCALE_SOURCE / size, y1 = ((y + 1) * SCALE_SOURCE + size - 1) / size;
+ double r = 0, g = 0, b = 0;
+ int n = (x1 - x0) * (y1 - y0);
+ for (int sy = y0; sy < y1; sy++)
+ for (int sx = x0; sx < x1; sx++) {
+ uint16_t p = source[sy * SCALE_SOURCE + sx];
+ r += p >> 11;
+ g += p >> 5 & 0x3f;
+ b += p & 0x1f;
+ }
+ return (uint16_t)((unsigned)floor(r / n + 0.5) << 11 |
+ (unsigned)floor(g / n + 0.5) << 5 | (unsigned)floor(b / n + 0.5));
+}
+
+static void test_scale(void)
+{
+ static uint16_t source[SCALE_SOURCE * SCALE_SOURCE];
+ static uint16_t row[SCALE_MAX], part[SCALE_MAX];
+ static const int sizes[] = {GRID_COVER, GRID_LIFT_SIZE};
+ for (unsigned int i = 0; i < sizeof source / sizeof source[0]; i++)
+ source[i] = noise();
+ for (unsigned int s = 0; s < 2; s++) {
+ int size = sizes[s];
+ struct scale scale;
+ scale_init(&scale, size);
+ /* Windows of 2 or 3 that overlap, cover the source without a gap and
+ * never reach past it: scale_row reads first[i] + span[i] - 1. */
+ CHECK(scale.first[0] == 0);
+ for (int i = 0; i < size; i++) {
+ CHECK(scale.span[i] == 2 || scale.span[i] == 3);
+ CHECK(scale.first[i] + scale.span[i] <= SCALE_SOURCE);
+ if (i) {
+ CHECK(scale.first[i] >= scale.first[i - 1]);
+ CHECK(scale.first[i] <= scale.first[i - 1] + scale.span[i - 1]);
+ }
+ }
+ CHECK(scale.first[size - 1] + scale.span[size - 1] == SCALE_SOURCE);
+ for (int y = 0; y < size; y++) {
+ scale_row(row, &scale, (const uint8_t *)source, SCALE_SOURCE * 2, y, 0,
+ size);
+ for (int x = 0; x < size; x++)
+ CHECK(row[x] == reference(source, size, x, y));
+ int x0 = y % 7, x1 = size - y % 5;
+ scale_row(part, &scale, (const uint8_t *)source, SCALE_SOURCE * 2, y, x0,
+ x1);
+ for (int x = x0; x < x1; x++)
+ CHECK(part[x - x0] == row[x]);
+ }
+ }
+}
+
+static void test_top_row(void)
+{
+ for (int count = 1; count <= 50; count++) {
+ int rows = (count + GRID_COLUMNS - 1) / GRID_COLUMNS;
+ for (int top = 0; top < rows; top++)
+ for (int index = 0; index < count; index++) {
+ int next_top = grid_top_row(top, index), row = index / GRID_COLUMNS;
+ CHECK(row >= next_top && row < next_top + GRID_FULL_ROWS);
+ CHECK(next_top >= 0);
+ CHECK(grid_top_row(next_top, index) == next_top);
+ if (row >= top && row < top + GRID_FULL_ROWS)
+ CHECK(next_top == top);
+ }
+ /* Down to the end and back: the top row never jumps by more than one. */
+ int top = 0;
+ for (int index = 1; index < count; index++) {
+ int after = grid_top_row(top, index);
+ CHECK(after - top >= 0 && after - top <= 1);
+ top = after;
+ }
+ for (int index = count - 2; index >= 0; index--) {
+ int after = grid_top_row(top, index);
+ CHECK(top - after >= 0 && top - after <= 1);
+ top = after;
+ }
+ CHECK(top == 0);
+ }
+}
+
+static void test_cell(void)
+{
+ int x, y;
+ CHECK(grid_cell(0, 0, &x, &y) && x == GRID_MARGIN && y == GRID_MARGIN);
+ CHECK(grid_cell(5, 0, &x, &y) && x == GRID_MARGIN + GRID_PITCH &&
+ y == GRID_MARGIN + GRID_PITCH);
+ CHECK(grid_cell(8, 0, &x, &y) && y == GRID_MARGIN + 2 * GRID_PITCH);
+ CHECK(!grid_cell(12, 0, &x, &y));
+ CHECK(!grid_cell(3, 1, &x, &y));
+ CHECK(grid_cell(4, 1, &x, &y) && y == GRID_MARGIN);
+ CHECK(!grid_cell(-1, 0, &x, &y));
+}
+
+static void test_lift_origin(void)
+{
+ enum { WIDTH = 320, HEIGHT = 220 - GRID_CAPTION };
+
+ for (int index = 0; index < GRID_VISIBLE_COVERS; index++) {
+ int x, y, lx, ly;
+ CHECK(grid_cell(index, 0, &x, &y));
+ grid_lift_origin(x, y, WIDTH, HEIGHT, &lx, &ly);
+ /* The edge stays on screen wherever the cell is. */
+ CHECK(lx - GRID_EDGE >= 0 && lx + GRID_LIFT_SIZE + GRID_EDGE <= WIDTH);
+ CHECK(ly - GRID_EDGE >= 0 && ly + GRID_LIFT_SIZE + GRID_EDGE <= HEIGHT);
+ /* grid_top_row keeps the selection in the full rows, and there the
+ * lifted cover hides the cover beneath it. */
+ if (index < GRID_FULL_ROWS * GRID_COLUMNS) {
+ CHECK(lx <= x && lx + GRID_LIFT_SIZE >= x + GRID_COVER);
+ CHECK(ly <= y && ly + GRID_LIFT_SIZE >= y + GRID_COVER);
+ }
+ }
+ int lx, ly;
+ grid_lift_origin(GRID_MARGIN + GRID_PITCH, GRID_MARGIN + GRID_PITCH, WIDTH, HEIGHT,
+ &lx, &ly);
+ CHECK(lx == GRID_MARGIN + GRID_PITCH - GRID_LIFT &&
+ ly == GRID_MARGIN + GRID_PITCH - GRID_LIFT);
+}
+
+static void test_scrollbar(void)
+{
+ enum { HEIGHT = 220 - GRID_CAPTION, MINIMUM = 16 };
+
+ int y0, y1;
+ CHECK(!grid_scrollbar_span(8, 0, HEIGHT, MINIMUM, &y0, &y1));
+ CHECK(grid_scrollbar_span(9, 0, HEIGHT, MINIMUM, &y0, &y1));
+ for (int count = 9; count < 400; count += 13) {
+ int rows = (count + GRID_COLUMNS - 1) / GRID_COLUMNS, last = -1;
+ for (int top = 0; top <= rows - GRID_FULL_ROWS; top++) {
+ CHECK(grid_scrollbar_span(count, top, HEIGHT, MINIMUM, &y0, &y1));
+ CHECK(y0 >= 0 && y1 <= HEIGHT && y1 - y0 >= MINIMUM + 3);
+ CHECK(y0 >= last);
+ last = y0;
+ }
+ CHECK(y1 ==
+ HEIGHT); /* the thumb reaches the end of the track at the last row */
+ }
+}
+
+int main(void)
+{
+ test_scale();
+ test_top_row();
+ test_cell();
+ test_lift_origin();
+ test_scrollbar();
+ if (failures) {
+ printf("%d failures\n", failures);
+ return 1;
+ }
+ puts("Cover Grid contracts passed");
+ return 0;
+}
diff --git a/extensions/cover-grid/tests/run.py b/extensions/cover-grid/tests/run.py
new file mode 100755
index 0000000..a97b1bf
--- /dev/null
+++ b/extensions/cover-grid/tests/run.py
@@ -0,0 +1,33 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-3.0-only
+"""Run the host contracts with a staged SDK: python3 tests/run.py SDK.
+
+The box filter and the geometry have no Apple dependency, so they build
+natively under the sanitizers from the sources themselves."""
+
+import subprocess
+import tempfile
+from pathlib import Path
+
+root = Path(__file__).resolve().parent
+with tempfile.TemporaryDirectory(prefix="cover-grid-tests-") as name:
+ executable = Path(name) / "contracts"
+ subprocess.run(
+ [
+ "cc",
+ "-std=c11",
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ "-g",
+ "-fsanitize=address,undefined",
+ "-fno-sanitize-recover=undefined",
+ str(root / "contracts.c"),
+ str(root.parent / "scale.c"),
+ "-lm",
+ "-o",
+ str(executable),
+ ],
+ check=True,
+ )
+ subprocess.run([str(executable)], check=True)