diff --git a/Makefile b/Makefile
index 32d96f0..295dad4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,8 @@
# SPDX-License-Identifier: GPL-3.0-only
SDK ?= ../osos-lab/rvx-sdk
OUT ?= build
-extensions := balance crossfeed dsp night-shift playback-log
+extensions := balance chapter-marks chapter-menu crossfeed dsp fast-seek long-audio-fix night-shift \
+ playback-log sleep-timer
.PHONY: all $(extensions)
all: $(extensions)
@@ -14,6 +15,8 @@ $(extensions):
.PHONY: check
check:
$(PYTHON) extensions/balance/tests/run.py "$(abspath $(SDK))"
+ $(PYTHON) extensions/chapter-marks/tests/run.py "$(abspath $(SDK))"
+ $(PYTHON) extensions/chapter-menu/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..5800073 100644
--- a/NOTICE
+++ b/NOTICE
@@ -4,7 +4,15 @@ 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. Sleep Timer, Fast Seek and Long Audio Fix are
+from RevolaOS extensions, revision c179e6dc21ea8a32e46bf9af1d0906c8793190e8.
+Sleep Timer is based on kayboll’s RepriseOS Sleep Timer shortcut; Fast Seek
+and Long Audio Fix are the same fixes as kayboll’s RepriseOS pull requests
+63 and 62. Their catalog icons and the Sleep Timer moon are Cupertino Icons
+glyphs (MIT), see the LICENSE.cupertino-icons beside each. Chapter Menu and
+Chapter Marks are from RevolaOS extensions, revision
+72562b52fe7e4074a3479caac86b84ec1d285966; their catalog icons are Cupertino
+Icons glyphs (MIT) as well. 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..4fe054f 100644
--- a/README.md
+++ b/README.md
@@ -6,5 +6,10 @@ 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/).
+- [Sleep Timer](extensions/sleep-timer/) sets Apple's sleep timer from the Now Playing context menu and shows the minutes left in Now Playing.
+- [Fast Seek](extensions/fast-seek/) makes seeks in long AAC and Apple Lossless files jump to the target instead of stepping through every frame.
+- [Long Audio Fix](extensions/long-audio-fix/) plays AAC and Apple Lossless files longer than 13.5 hours to the end.
+- [Chapter Menu](extensions/chapter-menu/) lists the chapters of a podcast or iTunes U episode, or of the playing track, in its context menu and jumps to one.
+- [Chapter Marks](extensions/chapter-marks/) leaves the chapter marks off the Now Playing bar when there are too many to tell apart.
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..518520f 100644
--- a/catalog/sources.toml
+++ b/catalog/sources.toml
@@ -22,3 +22,28 @@ tag_prefix = "playback-log/v"
repo = "RepriseOS/repriseos-extensions"
path = "extensions/dsp"
tag_prefix = "dsp/v"
+
+[fast-seek]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/fast-seek"
+tag_prefix = "fast-seek/v"
+
+[long-audio-fix]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/long-audio-fix"
+tag_prefix = "long-audio-fix/v"
+
+[sleep-timer]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/sleep-timer"
+tag_prefix = "sleep-timer/v"
+
+[chapter-menu]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/chapter-menu"
+tag_prefix = "chapter-menu/v"
+
+[chapter-marks]
+repo = "RepriseOS/repriseos-extensions"
+path = "extensions/chapter-marks"
+tag_prefix = "chapter-marks/v"
diff --git a/extensions/chapter-marks/Makefile b/extensions/chapter-marks/Makefile
new file mode 100644
index 0000000..9c3fa85
--- /dev/null
+++ b/extensions/chapter-marks/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c marks.c
+include $(SDK)/rv.mk
diff --git a/extensions/chapter-marks/README.md b/extensions/chapter-marks/README.md
new file mode 100644
index 0000000..1b5d221
--- /dev/null
+++ b/extensions/chapter-marks/README.md
@@ -0,0 +1,9 @@
+# Chapter Marks
+
+Leaves all chapter marks off the Now Playing bar once they would average under 3 px each, about 55 chapters on the classic's bar. Fewer chapters keep the stock marks.
+
+| | |
+|---|---|
+| Default | on |
+| Memory | image only, plus 8 bytes for the bar being drawn |
+| Settings | none |
diff --git a/extensions/chapter-marks/assets/LICENSE.cupertino-icons b/extensions/chapter-marks/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/chapter-marks/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/chapter-marks/assets/settings-chapter-marks.png b/extensions/chapter-marks/assets/settings-chapter-marks.png
new file mode 100644
index 0000000..524712f
Binary files /dev/null and b/extensions/chapter-marks/assets/settings-chapter-marks.png differ
diff --git a/extensions/chapter-marks/assets/settings-chapter-marks.svg b/extensions/chapter-marks/assets/settings-chapter-marks.svg
new file mode 100644
index 0000000..ef14275
--- /dev/null
+++ b/extensions/chapter-marks/assets/settings-chapter-marks.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/chapter-marks/listing.toml b/extensions/chapter-marks/listing.toml
new file mode 100644
index 0000000..929d762
--- /dev/null
+++ b/extensions/chapter-marks/listing.toml
@@ -0,0 +1,11 @@
+details = "Hides the chapter marks on the Now Playing progress bar once a track has so many that they blur into a band, about 55 or more. Tracks with fewer chapters keep the stock marks."
+
+icon = "assets/settings-chapter-marks.png"
+
+[[screenshots]]
+file = "screenshots/stock.png"
+alt = "Now Playing of an episode with 80 chapters as Apple draws it: the progress bar is a dense comb of chapter marks."
+
+[[screenshots]]
+file = "screenshots/hidden.png"
+alt = "The same episode with Chapter Marks: a plain progress bar."
diff --git a/extensions/chapter-marks/main.c b/extensions/chapter-marks/main.c
new file mode 100644
index 0000000..05942a0
--- /dev/null
+++ b/extensions/chapter-marks/main.c
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Chapter marks on the Now Playing bar. Apple draws one mark per chapter after
+ * the fill: the first hook measures the pass, the second leaves every mark out
+ * when they are too crowded to read. */
+
+#include "marks.h"
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_sites.h"
+#include "rv_ui.h"
+
+static const struct rv_api *api;
+static int hidden;
+
+static uint32_t draw_marks(uintptr_t view, uintptr_t context, uintptr_t bar,
+ const void *next)
+{
+ const int32_t *rect = (const int32_t *)bar;
+ hidden = marks_hidden(rect[3] - rect[1],
+ RV_FIELD(view, ProgressBar_marker_count, uint32_t));
+ return api->hook_next(next, view, context, bar, 0, 0, 0);
+}
+
+static uint32_t blit_mark(uintptr_t cache, uintptr_t context, uintptr_t source,
+ uintptr_t rect, const void *next, uint32_t alpha)
+{
+ return hidden ? 0 : api->hook_next(next, cache, context, source, rect, alpha, 0);
+}
+
+static const struct rv_hook hooks[] = {
+ RV_HOOK_FILTER(RV_SITE_ProgressBar_DrawMarkers_slot, draw_marks),
+ RV_HOOK_FILTER(RV_SITE_ProgressBarStyle8_DrawMarkers_slot, draw_marks),
+ RV_HOOK_FILTER(RV_SITE_ProgressBar_MarkerBlit_call, blit_mark),
+};
+static const struct rv_descriptor descriptor = {
+ .size = sizeof descriptor,
+ RV_HOOKS(hooks),
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *core,
+ rv_extension_handle handle)
+{
+ (void)handle;
+ api = core;
+ return &descriptor;
+}
diff --git a/extensions/chapter-marks/manifest.toml b/extensions/chapter-marks/manifest.toml
new file mode 100644
index 0000000..059d019
--- /dev/null
+++ b/extensions/chapter-marks/manifest.toml
@@ -0,0 +1,13 @@
+id = "chapter-marks"
+name = "Chapter Marks"
+description = "Leave the chapter marks off the Now Playing bar when there are too many to tell apart."
+version = "0.1.0"
+author = "RevolaOS"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
diff --git a/extensions/chapter-marks/marks.c b/extensions/chapter-marks/marks.c
new file mode 100644
index 0000000..453f4f1
--- /dev/null
+++ b/extensions/chapter-marks/marks.c
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Chapter marks are hidden once they average under MARKS_MIN_SPACING_PX of bar
+ * each: at that spacing they merge into a block that says nothing about where
+ * the chapters are. Fewer marks are left exactly as stock draws them. */
+
+#include "marks.h"
+
+#define MARKS_MIN_SPACING_PX 3
+
+int marks_hidden(int bar_width, uint32_t count)
+{
+ return bar_width > 0 && count * MARKS_MIN_SPACING_PX > (uint32_t)bar_width;
+}
diff --git a/extensions/chapter-marks/marks.h b/extensions/chapter-marks/marks.h
new file mode 100644
index 0000000..4234b70
--- /dev/null
+++ b/extensions/chapter-marks/marks.h
@@ -0,0 +1,10 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+#ifndef MARKS_H
+#define MARKS_H
+#include
+
+/* Whether `count` chapter marks on a bar `bar_width` px wide are too crowded
+ * to tell apart, in which case none is drawn. */
+int marks_hidden(int bar_width, uint32_t count);
+
+#endif
diff --git a/extensions/chapter-marks/screenshots/hidden.png b/extensions/chapter-marks/screenshots/hidden.png
new file mode 100644
index 0000000..fcb446f
Binary files /dev/null and b/extensions/chapter-marks/screenshots/hidden.png differ
diff --git a/extensions/chapter-marks/screenshots/stock.png b/extensions/chapter-marks/screenshots/stock.png
new file mode 100644
index 0000000..d5b7f00
Binary files /dev/null and b/extensions/chapter-marks/screenshots/stock.png differ
diff --git a/extensions/chapter-marks/tests/contracts.c b/extensions/chapter-marks/tests/contracts.c
new file mode 100644
index 0000000..0799c65
--- /dev/null
+++ b/extensions/chapter-marks/tests/contracts.c
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+#include
+#include
+#include "../marks.c"
+
+int main(void)
+{
+ /* A 170 px bar keeps up to 170 / MARKS_MIN_SPACING_PX marks. */
+ assert(!marks_hidden(170, 0));
+ assert(!marks_hidden(170, 25));
+ assert(!marks_hidden(170, 170 / MARKS_MIN_SPACING_PX));
+ assert(marks_hidden(170, 170 / MARKS_MIN_SPACING_PX + 1));
+ assert(marks_hidden(170, 120));
+ assert(marks_hidden(170, 2000));
+ /* A degenerate bar hides nothing. */
+ assert(!marks_hidden(0, 500));
+ assert(!marks_hidden(-5, 500));
+ puts("Chapter Marks contracts passed");
+ return 0;
+}
diff --git a/extensions/chapter-marks/tests/run.py b/extensions/chapter-marks/tests/run.py
new file mode 100755
index 0000000..096045e
--- /dev/null
+++ b/extensions/chapter-marks/tests/run.py
@@ -0,0 +1,27 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-3.0-only
+"""Run the mark selection tests: python3 tests/run.py SDK (the SDK is not needed)."""
+
+import subprocess
+import tempfile
+from pathlib import Path
+
+root = Path(__file__).resolve().parent
+with tempfile.TemporaryDirectory(prefix="chapter-marks-tests-") as name:
+ executable = Path(name) / "contracts"
+ subprocess.run(
+ [
+ "cc",
+ "-std=c11",
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ "-g",
+ "-fsanitize=address,undefined",
+ str(root / "contracts.c"),
+ "-o",
+ str(executable),
+ ],
+ check=True,
+ )
+ subprocess.run([str(executable)], check=True)
diff --git a/extensions/chapter-menu/Makefile b/extensions/chapter-menu/Makefile
new file mode 100644
index 0000000..bad731c
--- /dev/null
+++ b/extensions/chapter-menu/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c chapters.c
+include $(SDK)/rv.mk
diff --git a/extensions/chapter-menu/README.md b/extensions/chapter-menu/README.md
new file mode 100644
index 0000000..5c325fe
--- /dev/null
+++ b/extensions/chapter-menu/README.md
@@ -0,0 +1,16 @@
+# Chapter Menu
+
+Adds Chapters to the context menu (hold Select) of a podcast or iTunes U episode, and to the Now Playing context menu, when the item has chapters. The screen lists each chapter's title and start time, "m:ss" or "h:mm:ss".
+
+| Opened from | Highlighted | Choosing a chapter |
+|---|---|---|
+| Now Playing | the chapter playing | seeks the track there and returns to Now Playing |
+| An episode list | the first chapter | plays the episode from there in Now Playing, as choosing an audiobook chapter does |
+
+A chapter with an empty title shows as "Chapter 3"; one with no name atom is missing from Apple's library and from the list. Videos are not covered, and audiobooks keep Apple's own chapter list. If the track changed or the library reloaded while the list was open, choosing a chapter does nothing.
+
+| | |
+|---|---|
+| Default | on |
+| Memory | image only, plus 44 bytes for the row being drawn |
+| Settings | none |
diff --git a/extensions/chapter-menu/assets/LICENSE.cupertino-icons b/extensions/chapter-menu/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/chapter-menu/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/chapter-menu/assets/settings-chapter-menu.png b/extensions/chapter-menu/assets/settings-chapter-menu.png
new file mode 100644
index 0000000..1198031
Binary files /dev/null and b/extensions/chapter-menu/assets/settings-chapter-menu.png differ
diff --git a/extensions/chapter-menu/assets/settings-chapter-menu.svg b/extensions/chapter-menu/assets/settings-chapter-menu.svg
new file mode 100644
index 0000000..b071e0e
--- /dev/null
+++ b/extensions/chapter-menu/assets/settings-chapter-menu.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/chapter-menu/chapters.c b/extensions/chapter-menu/chapters.c
new file mode 100644
index 0000000..d73d846
--- /dev/null
+++ b/extensions/chapter-menu/chapters.c
@@ -0,0 +1,123 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Chapter rows read from Apple's MediaItem, and the two ways a chosen chapter
+ * plays: a seek of the playing track, or Apple's audiobook chapter start for
+ * an episode list row. */
+
+#include "chapters.h"
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_libc.h"
+#include "rv_media.h"
+
+/* The native string the last title lives in, and the texts of a row without
+ * one: "Chapter 4294967295" and a start of at most "1193:02:47". */
+static struct {
+ uint32_t title[NativeString_bytes / 4];
+ uint8_t title_live;
+ char label[20];
+ char start[12];
+} chapters;
+
+static struct MediaItem *media(const void *item)
+{
+ return (struct MediaItem *)item;
+}
+
+uint32_t chapters_count(const void *item)
+{
+ if (!rv_item_valid(item) || MediaItem_HasKind8062(media(item)))
+ return 0;
+ uint32_t count = MediaItem_GetChapterCount(media(item));
+ return count <= INT32_MAX ? count : 0;
+}
+
+void chapters_release(void)
+{
+ if (chapters.title_live)
+ NativeString_Destroy((struct NativeString *)chapters.title);
+ chapters.title_live = 0;
+}
+
+const char *chapters_row(const void *item, uint32_t index, const char **start)
+{
+ chapters_release();
+ *start = NULL;
+ if (index >= chapters_count(item))
+ return "";
+ uint32_t ms = MediaItem_GetChapterStartMs(media(item), index);
+ rv_format_duration(chapters.start, sizeof chapters.start, ms / 1000);
+ *start = chapters.start;
+ struct NativeString *title = (struct NativeString *)chapters.title;
+ NativeString_InitEmpty(title);
+ chapters.title_live = 1;
+ /* The title lookup takes a time and finds the chapter starting at or before it. */
+ const char *text = MediaItem_GetChapterTitleAt(media(item), (int)ms, title)
+ ? NativeString_GetUtf8(title)
+ : NULL;
+ if (text && *text)
+ return text;
+ rv_format(chapters.label, sizeof chapters.label, "Chapter %lu",
+ (unsigned long)index + 1);
+ return chapters.label;
+}
+
+/* Chapter `index`'s start in `item` into *ms; 0 when the chapter is gone or
+ * starts at or past the end. */
+static int start_of(const void *item, uint32_t index, uint32_t *ms)
+{
+ if (index >= chapters_count(item))
+ return 0;
+ typedef uint32_t (*getter)(const void *);
+ *ms = MediaItem_GetChapterStartMs(media(item), index);
+ return *ms <
+ RV_FIELD(RV_FIELD(item, 0, void *), MediaItem_duration_method, getter)(item);
+}
+
+uint32_t chapters_current(const void *item)
+{
+ uint32_t current[MediaItem_bytes / 4];
+ int index = 0;
+ void *player = rv_player_lock();
+ rv_player_current(player, media(current));
+ if (chapters_count(current) && rv_item_same(current, item))
+ index = MediaItem_GetChapterIndexAt(media(current),
+ MediaPlayer_GetPosition(player));
+ rv_player_unlock(player);
+ MediaItem_Destroy(media(current));
+ return index > 0 ? (uint32_t)index : 0;
+}
+
+void chapters_seek(const void *item, uint32_t index)
+{
+ uint32_t current[MediaItem_bytes / 4];
+ uint32_t ms;
+ void *player = rv_player_lock();
+ rv_player_current(player, media(current));
+ if (rv_item_same(current, item) && start_of(current, index, &ms))
+ MediaPlayer_Seek(player, ms);
+ rv_player_unlock(player);
+ MediaItem_Destroy(media(current));
+}
+
+void chapters_play(void *list, int32_t row, const void *item, uint32_t index)
+{
+ void *model = LibraryModelRef_Get((uint8_t *)list + MediaListController_model);
+ if (row < 0 || !model)
+ return;
+ uint32_t selected[MediaItem_bytes / 4];
+ rv_model_item(model, row, media(selected));
+ uint32_t ms;
+ int ok = rv_item_same(selected, item) && start_of(selected, index, &ms);
+ MediaItem_Destroy(media(selected));
+ if (!ok)
+ return;
+ /* As AudiobookChapters_SelectRow: Now Playing reads and removes the start
+ * time when it activates after PlayRow. */
+ MediaPlayer_Stop(MediaPlayer_GetInstance());
+ uint32_t key[CppString_bytes / 4];
+ CppString_Init(key, "NowPlayingStartTime");
+ NavigatorProperties_SetInteger(NavigatorProperties, key, &ms);
+ CppString_Destroy(key);
+ MediaListController_PlayRow(list, row, 0, UINT32_MAX);
+}
diff --git a/extensions/chapter-menu/chapters.h b/extensions/chapter-menu/chapters.h
new file mode 100644
index 0000000..2e4f9ce
--- /dev/null
+++ b/extensions/chapter-menu/chapters.h
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+#ifndef CHAPTERS_H
+#define CHAPTERS_H
+#include
+
+/* `item` is a MediaItem of the symbols header. */
+
+/* How many chapters `item` has; 0 when it is no longer valid or is a video. */
+uint32_t chapters_count(const void *item);
+/* Chapter `index`'s title ("Chapter 3" when it has none) and in *start its
+ * start time as "m:ss" or "h:mm:ss". Valid until the next call or
+ * chapters_release. */
+const char *chapters_row(const void *item, uint32_t index, const char **start);
+void chapters_release(void);
+/* The chapter the playing track is in if it is still `item`, else 0. */
+uint32_t chapters_current(const void *item);
+/* Seeks the playing track to chapter `index` if it is still `item`. */
+void chapters_seek(const void *item, uint32_t index);
+/* Plays model row `row` of the episode list `list` from chapter `index` if
+ * that row is still `item`. */
+void chapters_play(void *list, int32_t row, const void *item, uint32_t index);
+
+#endif
diff --git a/extensions/chapter-menu/listing.toml b/extensions/chapter-menu/listing.toml
new file mode 100644
index 0000000..509f053
--- /dev/null
+++ b/extensions/chapter-menu/listing.toml
@@ -0,0 +1,11 @@
+details = "Hold Select on a podcast or iTunes U episode, or in Now Playing, and choose Chapters to see the chapter titles and start times. Choosing one plays from there."
+
+icon = "assets/settings-chapter-menu.png"
+
+[[screenshots]]
+file = "screenshots/context-menu.png"
+alt = "The context menu of a podcast episode with Chapters highlighted, between Add to On-The-Go and File Info."
+
+[[screenshots]]
+file = "screenshots/list.png"
+alt = "The Chapters list of an episode: Part 1 to Part 9 with their start times, Part 1 highlighted."
diff --git a/extensions/chapter-menu/main.c b/extensions/chapter-menu/main.c
new file mode 100644
index 0000000..6c0a532
--- /dev/null
+++ b/extensions/chapter-menu/main.c
@@ -0,0 +1,77 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Chapters in the Now Playing, podcast and iTunes U episode context menus: the item's
+ * chapter titles and start times. Choosing one seeks the playing track there,
+ * or plays the episode from it. */
+
+#include "chapters.h"
+#include "rv_api.h"
+#include "rv_ui.h"
+#include
+
+static int has_chapters(const struct rv_context *ctx)
+{
+ return chapters_count(ctx->item) != 0;
+}
+
+static uint32_t count(const struct rv_context *ctx)
+{
+ return chapters_count(ctx->item);
+}
+
+static const char *row(const struct rv_context *ctx, uint32_t index,
+ const char **detail)
+{
+ return chapters_row(ctx->item, index, detail);
+}
+
+static int select(const struct rv_context *ctx, uint32_t index)
+{
+ if (ctx->list)
+ chapters_play(ctx->list, ctx->row, ctx->item, index);
+ else
+ chapters_seek(ctx->item, index);
+ return 1;
+}
+
+static uint32_t initial(const struct rv_context *ctx)
+{
+ return ctx->list ? 0 : chapters_current(ctx->item);
+}
+
+static void closed(const struct rv_context *ctx)
+{
+ (void)ctx;
+ chapters_release();
+}
+
+static const struct rv_screen screens[] = {
+ RV_SCREEN_DYNAMIC_LIST("chapters", "Chapters", count, row, .dynamic.select = select,
+ .layout = RV_LAYOUT_FULL, .dynamic.detail = RV_DETAIL_BESIDE,
+ .dynamic.initial = initial, .closed = closed),
+};
+
+/* The context menus of an item that can have chapters. */
+#define CHAPTER_MENUS \
+ (RV_MENU_BIT(RV_MENU_NOW_PLAYING_CONTEXT) | \
+ RV_MENU_BIT(RV_MENU_PODCAST_EPISODE_CONTEXT) | \
+ RV_MENU_BIT(RV_MENU_ITUNES_U_EPISODE_CONTEXT))
+
+static const struct rv_menu_entry entries[] = {
+ RV_MENUS_ENTRY(CHAPTER_MENUS, 0, RV_ROW_PUSH("Chapters", "chapters"),
+ .available = has_chapters),
+};
+
+static const struct rv_descriptor extension = {
+ .size = sizeof extension,
+ RV_SCREENS(screens),
+ RV_ENTRIES(entries),
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *api,
+ rv_extension_handle self)
+{
+ (void)api;
+ (void)self;
+ return &extension;
+}
diff --git a/extensions/chapter-menu/manifest.toml b/extensions/chapter-menu/manifest.toml
new file mode 100644
index 0000000..f185fcb
--- /dev/null
+++ b/extensions/chapter-menu/manifest.toml
@@ -0,0 +1,13 @@
+id = "chapter-menu"
+name = "Chapter Menu"
+description = "Browse the chapters of a podcast or iTunes U episode, or of the playing track, from its context menu and jump to one."
+version = "0.1.0"
+author = "RevolaOS"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
diff --git a/extensions/chapter-menu/screenshots/context-menu.png b/extensions/chapter-menu/screenshots/context-menu.png
new file mode 100644
index 0000000..fecb096
Binary files /dev/null and b/extensions/chapter-menu/screenshots/context-menu.png differ
diff --git a/extensions/chapter-menu/screenshots/list.png b/extensions/chapter-menu/screenshots/list.png
new file mode 100644
index 0000000..bdae594
Binary files /dev/null and b/extensions/chapter-menu/screenshots/list.png differ
diff --git a/extensions/chapter-menu/tests/contracts.c b/extensions/chapter-menu/tests/contracts.c
new file mode 100644
index 0000000..00494d4
--- /dev/null
+++ b/extensions/chapter-menu/tests/contracts.c
@@ -0,0 +1,359 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+#include
+#include
+#include
+#include
+#include "../chapters.c"
+
+/* The host formats; the device's rv_libc does the same for these conversions. */
+uint32_t rv_format(char *out, uint32_t capacity, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ int n = vsnprintf(out, capacity, format, args);
+ va_end(args);
+ return (uint32_t)n;
+}
+
+uint32_t rv_format_duration(char *out, uint32_t capacity, uint32_t seconds)
+{
+ uint32_t minutes = seconds / 60;
+ if (minutes < 60)
+ return rv_format(out, capacity, "%u:%02u", minutes, seconds % 60);
+ return rv_format(out, capacity, "%u:%02u:%02u", minutes / 60, minutes % 60,
+ seconds % 60);
+}
+
+/* A native string holds one pointer; init and destroy are counted. */
+static int strings_live;
+
+struct NativeString *NativeString_InitEmpty(struct NativeString *s)
+{
+ RV_FIELD(s, 0, const char *) = "";
+ strings_live++;
+ return s;
+}
+
+struct NativeString *NativeString_Destroy(struct NativeString *s)
+{
+ assert(strings_live > 0);
+ strings_live--;
+ RV_FIELD(s, 0, const char *) = NULL;
+ return s;
+}
+
+char *NativeString_GetUtf8(struct NativeString *s)
+{
+ return (char *)RV_FIELD(s, 0, const char *);
+}
+
+/* The library: one record whose chapters, duration, validity and kind the
+ * tests set. Items are views of it: a vtable for the duration getter, the
+ * record and its generation. */
+static struct {
+ uint32_t count, duration, generation;
+ uint32_t starts[4];
+ const char *titles[4];
+ int valid, video;
+} fake;
+
+static _Alignas(8) uint8_t track_record[16];
+static uintptr_t methods[MediaItem_duration_method / sizeof(void *) + 1];
+static _Alignas(8) uint8_t item[MediaItem_bytes];
+
+static uint32_t get_duration(const void *i)
+{
+ (void)i;
+ return fake.duration;
+}
+
+static void view(void *i)
+{
+ memset(i, 0, MediaItem_bytes);
+ RV_FIELD(i, 0, void *) = methods;
+ RV_FIELD(i, MediaItem_record, void *) = track_record;
+ RV_FIELD(i, MediaItem_generation, uint32_t) = fake.generation;
+}
+
+int MediaItem_IsValid(struct MediaItem *i)
+{
+ (void)i;
+ return fake.valid;
+}
+
+uint32_t MediaItem_HasKind8062(struct MediaItem *i)
+{
+ (void)i;
+ return (uint32_t)fake.video;
+}
+
+uint32_t MediaItem_GetChapterCount(struct MediaItem *i)
+{
+ (void)i;
+ return fake.count;
+}
+
+uint32_t MediaItem_GetChapterStartMs(struct MediaItem *i, uint32_t index)
+{
+ (void)i;
+ return index < fake.count ? fake.starts[index] : 0;
+}
+
+/* As Apple's: the last chapter starting at or before the time. */
+int MediaItem_GetChapterTitleAt(struct MediaItem *i, int position_ms,
+ struct NativeString *out)
+{
+ (void)i;
+ for (uint32_t n = fake.count; n-- > 0;)
+ if (fake.starts[n] <= (uint32_t)position_ms) {
+ if (!fake.titles[n])
+ return 0;
+ RV_FIELD(out, 0, const char *) = fake.titles[n];
+ return 1;
+ }
+ return 0;
+}
+
+int MediaItem_GetChapterIndexAt(struct MediaItem *i, uint32_t position_ms)
+{
+ (void)i;
+ int index = -1;
+ for (uint32_t n = 0; n < fake.count; n++)
+ if (fake.starts[n] <= position_ms)
+ index = (int)n;
+ return index;
+}
+
+static int items_live;
+
+struct MediaItem *MediaItem_InitFromRecord(struct MediaItem *i, void *record,
+ uint8_t flags)
+{
+ (void)flags;
+ assert(!record);
+ memset(i, 0, MediaItem_bytes);
+ items_live++;
+ return i;
+}
+
+void MediaItem_Destroy(struct MediaItem *i)
+{
+ (void)i;
+ assert(items_live > 0);
+ items_live--;
+}
+
+/* The player plays the record (or nothing) at `position`; the mutex is
+ * counted so a seek can be checked to run under it. */
+static _Alignas(8) uint8_t player[MediaPlayer_mutex + 4];
+static int locked, playing;
+static uint32_t position, seeks, seek_to;
+
+void *MediaPlayer_GetInstance(void)
+{
+ return player;
+}
+
+int Mutex_Lock(void *mutex)
+{
+ assert(mutex == player + MediaPlayer_mutex);
+ locked++;
+ return 0;
+}
+
+int Mutex_Unlock(void *mutex)
+{
+ assert(mutex == player + MediaPlayer_mutex && locked > 0);
+ locked--;
+ return 0;
+}
+
+int MediaPlayer_GetCurrentItem(void *p, struct MediaItem *i)
+{
+ assert(p == player && locked);
+ if (playing)
+ view(i);
+ return playing;
+}
+
+uint32_t MediaPlayer_GetPosition(void *p)
+{
+ assert(p == player);
+ return position;
+}
+
+int MediaPlayer_Seek(void *p, uint32_t ms)
+{
+ assert(p == player && locked);
+ seeks++;
+ seek_to = ms;
+ return 1;
+}
+
+/* The episode list: its model hands out a view of the record for any row;
+ * the play path records what it was asked. */
+static _Alignas(8) uint8_t list[MediaListController_model + sizeof(void *)];
+static uintptr_t model_methods[SongModel_get_item_method / sizeof(void *) + 1];
+static void *model[1] = {model_methods};
+static int32_t got_row;
+
+static void get_item(struct MediaItem *i, void *m, int32_t row)
+{
+ assert(m == model);
+ got_row = row;
+ view(i);
+}
+
+void *LibraryModelRef_Get(void *reference)
+{
+ assert(reference == list + MediaListController_model);
+ return model;
+}
+
+uint8_t NavigatorProperties[4];
+static int stops, plays, keys_live;
+static uint32_t start_time;
+static int32_t played_row;
+
+void MediaPlayer_Stop(void *p)
+{
+ assert(p == player);
+ stops++;
+}
+
+void *CppString_Init(void *s, const char *text)
+{
+ assert(!strcmp(text, "NowPlayingStartTime"));
+ RV_FIELD(s, 0, const char *) = text;
+ keys_live++;
+ return s;
+}
+
+void CppString_Destroy(void *s)
+{
+ (void)s;
+ keys_live--;
+}
+
+void NavigatorProperties_SetInteger(void *properties, void *key, const uint32_t *value)
+{
+ assert(properties == NavigatorProperties && keys_live == 1 && stops == plays + 1);
+ assert(!strcmp(RV_FIELD(key, 0, const char *), "NowPlayingStartTime"));
+ start_time = *value;
+}
+
+void MediaListController_PlayRow(void *controller, int row, int mode, uint32_t shuffle)
+{
+ assert(controller == list && mode == 0 && shuffle == UINT32_MAX);
+ plays++;
+ played_row = row;
+}
+
+static void reset(void)
+{
+ chapters_release();
+ memset(&fake, 0, sizeof fake);
+ fake.valid = 1;
+ fake.count = 3;
+ fake.duration = 3723000;
+ fake.generation = 7;
+ fake.starts[1] = 60000;
+ fake.starts[2] = 3600000;
+ fake.titles[0] = "Intro";
+ fake.titles[2] = "Final Hour";
+ methods[MediaItem_duration_method / sizeof(void *)] = (uintptr_t)get_duration;
+ model_methods[SongModel_get_item_method / sizeof(void *)] = (uintptr_t)get_item;
+ RV_FIELD(list, MediaListController_model, void *) = model;
+ view(item);
+ playing = 1;
+ position = 0;
+ seeks = 0;
+ stops = plays = 0;
+ start_time = 0;
+}
+
+static void expect(uint32_t index, const char *label, const char *start)
+{
+ const char *detail;
+ const char *text = chapters_row(item, index, &detail);
+ if (strcmp(text, label) ||
+ (start ? !detail || strcmp(detail, start) : detail != NULL)) {
+ fprintf(stderr, "row %u: %s / %s, want %s / %s\n", index, text,
+ detail ? detail : "-", label, start ? start : "-");
+ assert(0);
+ }
+}
+
+int main(void)
+{
+ /* Rows: the title, or "Chapter N" when it has none; the start beside it. */
+ reset();
+ assert(chapters_count(item) == 3);
+ expect(0, "Intro", "0:00");
+ expect(1, "Chapter 2", "1:00");
+ expect(2, "Final Hour", "1:00:00");
+ expect(3, "", NULL);
+ chapters_release();
+ assert(strings_live == 0);
+
+ /* No rows for no item, a stale item, a video or an absurd count. */
+ assert(chapters_count(NULL) == 0);
+ fake.valid = 0;
+ assert(chapters_count(item) == 0);
+ expect(0, "", NULL);
+ reset();
+ fake.video = 1;
+ assert(chapters_count(item) == 0);
+ reset();
+ fake.count = 0x80000000u;
+ assert(chapters_count(item) == 0);
+ reset();
+ RV_FIELD(item, MediaItem_record, void *) = NULL;
+ assert(chapters_count(item) == 0);
+
+ /* Now Playing: the current chapter is highlighted, a chosen one seeks
+ * the same track under the player mutex. */
+ reset();
+ position = 61000;
+ assert(chapters_current(item) == 1);
+ position = 0;
+ fake.starts[0] = 5000;
+ assert(chapters_current(item) == 0); /* before the first chapter */
+ reset();
+ chapters_seek(item, 2);
+ assert(seeks == 1 && seek_to == 3600000 && !locked && items_live == 0);
+ chapters_seek(item, 0);
+ assert(seeks == 2 && seek_to == 0);
+
+ /* Never seek another track, a reloaded record, a missing chapter or one
+ * at or past the end. */
+ reset();
+ playing = 0;
+ chapters_seek(item, 1);
+ assert(chapters_current(item) == 0);
+ reset();
+ fake.generation++;
+ chapters_seek(item, 1);
+ reset();
+ chapters_seek(item, 3);
+ reset();
+ fake.duration = 3600000;
+ chapters_seek(item, 2);
+ assert(seeks == 0 && !locked && items_live == 0);
+
+ /* Episode list: stop, store the start, play the row. */
+ reset();
+ chapters_play(list, 4, item, 1);
+ assert(stops == 1 && plays == 1 && played_row == 4 && got_row == 4);
+ assert(start_time == 60000 && keys_live == 0 && items_live == 0);
+
+ /* Nothing for a row that changed, a negative row or a missing chapter. */
+ reset();
+ fake.generation++;
+ chapters_play(list, 4, item, 1);
+ chapters_play(list, -1, item, 1);
+ reset();
+ chapters_play(list, 0, item, 3);
+ assert(stops == 0 && plays == 0 && items_live == 0);
+ return 0;
+}
diff --git a/extensions/chapter-menu/tests/run.py b/extensions/chapter-menu/tests/run.py
new file mode 100755
index 0000000..5df1620
--- /dev/null
+++ b/extensions/chapter-menu/tests/run.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-3.0-only
+"""Run the row, seek and play tests with a staged SDK: python3 tests/run.py SDK."""
+
+import re
+import subprocess
+import sys
+import tempfile
+from pathlib import Path
+
+root = Path(__file__).resolve().parent
+sdk = Path(sys.argv[1]).resolve()
+with tempfile.TemporaryDirectory(prefix="chapter-menu-tests-") as name:
+ work = Path(name)
+ # Widen the pointer-bearing native layouts for the host; record offsets stay.
+ header = (sdk / "osos-2.0.5.h").read_text()
+
+ def widen(match):
+ key, value = match.groups()
+ widened = ("MediaItem_", "NativeString_", "CppString_", "SongModel_")
+ if key.startswith(widened + ("MediaListController_",)):
+ return f"#define {key} {int(value, 16) * 2:#x}"
+ return match[0]
+
+ (work / "osos-2.0.5.h").write_text(
+ re.sub(r"#define (\w+) (0x[0-9a-f]+)", widen, header)
+ )
+ (work / "rv_api.h").write_text((sdk / "rv_api.h").read_text())
+ (work / "rv_libc.h").write_text((sdk / "rv_libc.h").read_text())
+ (work / "rv_media.h").write_text((sdk / "rv_media.h").read_text())
+ executable = work / "contracts"
+ subprocess.run(
+ [
+ "cc",
+ "-std=c11",
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+ "-g",
+ "-fsanitize=address,undefined",
+ "-I",
+ str(work),
+ str(root / "contracts.c"),
+ "-o",
+ str(executable),
+ ],
+ check=True,
+ )
+ subprocess.run([str(executable)], check=True)
+print("Chapter Menu contracts passed")
diff --git a/extensions/fast-seek/Makefile b/extensions/fast-seek/Makefile
new file mode 100644
index 0000000..cb973a7
--- /dev/null
+++ b/extensions/fast-seek/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c
+include $(SDK)/rv.mk
diff --git a/extensions/fast-seek/README.md b/extensions/fast-seek/README.md
new file mode 100644
index 0000000..cdc5d6f
--- /dev/null
+++ b/extensions/fast-seek/README.md
@@ -0,0 +1,15 @@
+# Fast Seek
+
+The same approach as [Seek MP4 audio by chunk instead of sample by sample](https://github.com/RepriseOS/repriseos/pull/63) by kayboll for RepriseOS, arrived at independently.
+
+Apple's MP4 parser seeks an AAC or Apple Lossless file (M4A, M4B) by stepping through every sample to the target, reading the sample-size table from disk in 128 KB windows. A backward seek starts from the first sample. A jump of several hours in an audiobook takes seconds and many disk reads. Chapters, the scrubber and resuming an episode all seek this way.
+
+The extension binds the entry of the sample walk. It finds the chunk holding the target from the in-memory time-to-sample and sample-to-chunk tables and starts the walk at that chunk's first sample, leaving Apple's loop at most a few seconds of audio in iTunes files. Files with more than one edit, or more than one frame length before the target, seek the stock way.
+
+Disabling it restores Apple's walk at once.
+
+| | |
+|---|---|
+| Default | on |
+| Memory | image only |
+| Settings | none |
diff --git a/extensions/fast-seek/assets/LICENSE.cupertino-icons b/extensions/fast-seek/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/fast-seek/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/fast-seek/assets/settings-fast-seek.png b/extensions/fast-seek/assets/settings-fast-seek.png
new file mode 100644
index 0000000..80b782f
Binary files /dev/null and b/extensions/fast-seek/assets/settings-fast-seek.png differ
diff --git a/extensions/fast-seek/assets/settings-fast-seek.svg b/extensions/fast-seek/assets/settings-fast-seek.svg
new file mode 100644
index 0000000..37928d2
--- /dev/null
+++ b/extensions/fast-seek/assets/settings-fast-seek.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/fast-seek/listing.toml b/extensions/fast-seek/listing.toml
new file mode 100644
index 0000000..b7b0b9f
--- /dev/null
+++ b/extensions/fast-seek/listing.toml
@@ -0,0 +1,3 @@
+details = "Seek, pick a chapter or resume in a long AAC or Apple Lossless file (M4A, M4B). Playback continues at the target right away instead of after a long pause. Nothing is added to the menus."
+
+icon = "assets/settings-fast-seek.png"
diff --git a/extensions/fast-seek/main.c b/extensions/fast-seek/main.c
new file mode 100644
index 0000000..fe56917
--- /dev/null
+++ b/extensions/fast-seek/main.c
@@ -0,0 +1,152 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* The same approach as RepriseOS pull request 63 by kayboll, arrived at
+ * independently. */
+
+/* Apple's MP4 parser seeks by stepping through every sample from its current
+ * position, or from the first sample when the target lies behind, until the
+ * presentation time reaches the target: about 155 instructions per AAC frame and
+ * a 128 KB table read every 32,768 frames. This handler runs at the entry of
+ * that walk (MP4Parser_SeekAudioTrack), works out from the time-to-sample and
+ * sample-to-chunk runs which chunk holds the target, moves the track state to
+ * that chunk's first sample and lets Apple's loop finish the chunk, loading the
+ * table windows as it always does. Tables outside the plain shape (several
+ * edits, several time-to-sample runs before the target, windows not at the
+ * table start) leave the walk to Apple. */
+
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_sites.h"
+#include "rv_ui.h"
+
+static const struct rv_api *api;
+
+static uint32_t word(const void *base, uint32_t offset)
+{
+ return RV_FIELD(base, offset, uint32_t);
+}
+
+static uint64_t pair(const void *base, uint32_t offset)
+{
+ return (uint64_t)word(base, offset + 4) << 32 | word(base, offset);
+}
+
+static void set_pair(void *base, uint32_t offset, uint64_t value)
+{
+ RV_FIELD(base, offset, uint32_t) = (uint32_t)value;
+ RV_FIELD(base, offset + 4, uint32_t) = (uint32_t)(value >> 32);
+}
+
+/* a × b for b below 2^16 (timescales and frame lengths), without the 64-bit
+ * multiply helper. */
+static uint64_t mul(uint32_t a, uint32_t b)
+{
+ uint32_t low = (a & 0xffff) * b, high = (a >> 16) * b;
+ return ((uint64_t)(high >> 16) << 32) + ((uint64_t)(high & 0xffff) << 16) + low;
+}
+
+/* Move the audio track to the first sample of the chunk that holds the seek
+ * target, when the tables allow it. Samples and chunks are 1-based, as in the
+ * track state. */
+static void position(uint8_t *parser, uint32_t ms)
+{
+ uint8_t *track = RV_FIELD(parser, MP4Parser_audio_track, uint8_t *);
+ /* Apple's walk returns at once for a track without a chunk offset atom. */
+ if (!track || pair(track, MP4Track_chunk_offset_atom) == UINT64_MAX)
+ return;
+ const uint8_t *sizes = RV_FIELD(track, MP4Track_sample_sizes, const uint8_t *);
+ const uint8_t *chunks = RV_FIELD(track, MP4Track_chunk_offsets, const uint8_t *);
+ const uint8_t *runs = RV_FIELD(track, MP4Track_sample_to_chunk, const uint8_t *);
+ const uint8_t *times = RV_FIELD(track, MP4Track_time_to_sample, const uint8_t *);
+ const uint8_t *edits = RV_FIELD(track, MP4Track_edits, const uint8_t *);
+ uint32_t timescale = word(track, MP4Track_timescale);
+ if (!sizes || !chunks || !runs || !times || !timescale || timescale > 0xffff)
+ return;
+ uint32_t run_count = word(runs, MP4SampleTable_count);
+ if (word(times, MP4SampleTable_window_first) ||
+ !word(times, MP4SampleTable_count) || word(runs, MP4SampleTable_window_first) ||
+ !run_count || run_count > MP4_SampleToChunkWindow)
+ return;
+ /* One edit, already the current one: presentation time starts at its media start. */
+ uint64_t edit_start = 0;
+ if (edits) {
+ if (word(edits, MP4EditList_count) != 1 || word(track, MP4Track_edit_index))
+ return;
+ edit_start = pair(track, MP4Track_edit_media_start);
+ if (edit_start == UINT64_MAX)
+ return;
+ }
+ /* Only the first time-to-sample run; a target past it stays with Apple. */
+ const uint8_t *time = RV_FIELD(times, MP4SampleTable_window, const uint8_t *);
+ uint32_t delta = word(time, MP4TimeToSampleEntry_delta);
+ uint32_t in_run = word(time, MP4TimeToSampleEntry_count);
+ if (!delta || delta > 0xffff)
+ return;
+ /* The first sample inside the edit, and the sample at which Apple's loop stops,
+ * rounded down: the loop can only move forward from where it is put. */
+ uint64_t target = Runtime_UDiv64(mul(ms, timescale), 1000);
+ uint64_t first = Runtime_UDiv64(edit_start + delta - 1, delta) + 1;
+ uint64_t stop = first - 1 + Runtime_UDiv64(target, delta);
+ if (stop < 1 || stop > in_run || stop > word(sizes, MP4SampleSizeTable_count))
+ return;
+ /* The chunk holding `stop`, from the sample-to-chunk runs. */
+ const uint8_t *run = RV_FIELD(runs, MP4SampleTable_window, const uint8_t *);
+ uint32_t chunk_count = word(chunks, MP4SampleTable_count);
+ uint32_t index = (uint32_t)stop - 1, before = 0, run_index = 0, chunk, chunk_first;
+ for (;; run_index++, run += MP4SampleToChunkEntry_bytes) {
+ uint32_t from = word(run, MP4SampleToChunkEntry_first_chunk);
+ uint32_t per = word(run, MP4SampleToChunkEntry_samples_per_chunk);
+ uint32_t until = run_index + 1 < run_count
+ ? word(run + MP4SampleToChunkEntry_bytes,
+ MP4SampleToChunkEntry_first_chunk)
+ : chunk_count + 1;
+ if (!per || until <= from)
+ return;
+ uint32_t span = (until - from) * per;
+ if (index - before < span) {
+ chunk = from + (index - before) / per;
+ chunk_first = before + (chunk - from) * per + 1;
+ break;
+ }
+ before += span;
+ if (run_index + 1 >= run_count)
+ return;
+ }
+ /* Already inside the stretch Apple would walk forward: leave it. */
+ uint32_t sample = word(track, MP4Track_sample);
+ if (sample >= chunk_first && sample <= stop)
+ return;
+ RV_FIELD(track, MP4Track_sample, uint32_t) = chunk_first;
+ RV_FIELD(track, MP4Track_sample_to_chunk_index, uint32_t) = run_index;
+ RV_FIELD(track, MP4Track_chunk, uint32_t) = chunk;
+ RV_FIELD(track, MP4Track_sample_in_chunk, uint32_t) = 0;
+ RV_FIELD(track, MP4Track_chunk_byte_offset, uint32_t) = 0;
+ RV_FIELD(track, MP4Track_time_to_sample_index, uint32_t) = 0;
+ RV_FIELD(track, MP4Track_time_to_sample_run, uint32_t) = chunk_first - 1;
+ set_pair(track, MP4Track_decode_time, mul(chunk_first - 1, delta));
+ set_pair(track, MP4Track_presentation_time,
+ chunk_first > first ? mul(chunk_first - (uint32_t)first, delta) : 0);
+}
+
+static uint32_t on_seek(uint8_t *parser, uint32_t ms, const void *next)
+{
+ position(parser, ms);
+ return api->hook_next(next, (uint32_t)parser, ms, 0, 0, 0, 0);
+}
+
+static const struct rv_hook hooks[] = {
+ RV_HOOK_FILTER(RV_SITE_MP4Parser_SeekAudioTrack_entry, on_seek),
+};
+
+static const struct rv_descriptor extension = {
+ .size = sizeof extension,
+ RV_HOOKS(hooks),
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *core,
+ rv_extension_handle handle)
+{
+ (void)handle;
+ api = core;
+ return &extension;
+}
diff --git a/extensions/fast-seek/manifest.toml b/extensions/fast-seek/manifest.toml
new file mode 100644
index 0000000..15895a3
--- /dev/null
+++ b/extensions/fast-seek/manifest.toml
@@ -0,0 +1,13 @@
+id = "fast-seek"
+name = "Fast Seek"
+description = "Seeking in long AAC and Apple Lossless files (audiobooks, podcasts) jumps straight to the target instead of stepping through every frame on the way."
+version = "0.1.0"
+author = "RepriseOS and RevolaOS contributors"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
diff --git a/extensions/long-audio-fix/Makefile b/extensions/long-audio-fix/Makefile
new file mode 100644
index 0000000..cb973a7
--- /dev/null
+++ b/extensions/long-audio-fix/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c
+include $(SDK)/rv.mk
diff --git a/extensions/long-audio-fix/README.md b/extensions/long-audio-fix/README.md
new file mode 100644
index 0000000..47657f8
--- /dev/null
+++ b/extensions/long-audio-fix/README.md
@@ -0,0 +1,15 @@
+# Long Audio Fix
+
+The same fix as [Play MP4 audiobooks past 13.5 hours](https://github.com/RepriseOS/repriseos/pull/62) by kayboll for RepriseOS, arrived at independently.
+
+Apple's MP4 parser ends an AAC or Apple Lossless file (M4A, M4B) once playback passes 2^31 samples: 13:31:35 at 44.1 kHz, 12:25:39 at 48 kHz. Past that point every sample counts as outside the edit list, so the track ends, and a seek there ends it at once. This affects files with an edit list, which iTunes, ffmpeg and most audiobook tools write.
+
+The parser keeps the decode time in 64 bits but compares it with the edit list in 32. The extension replaces the three sign-extensions of the low word with loads of the high word.
+
+Disabling it restores Apple's compares at once.
+
+| | |
+|---|---|
+| Default | on |
+| Memory | image only |
+| Settings | none |
diff --git a/extensions/long-audio-fix/assets/LICENSE.cupertino-icons b/extensions/long-audio-fix/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/long-audio-fix/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/long-audio-fix/assets/settings-long-audio-fix.png b/extensions/long-audio-fix/assets/settings-long-audio-fix.png
new file mode 100644
index 0000000..d527304
Binary files /dev/null and b/extensions/long-audio-fix/assets/settings-long-audio-fix.png differ
diff --git a/extensions/long-audio-fix/assets/settings-long-audio-fix.svg b/extensions/long-audio-fix/assets/settings-long-audio-fix.svg
new file mode 100644
index 0000000..2233d75
--- /dev/null
+++ b/extensions/long-audio-fix/assets/settings-long-audio-fix.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/long-audio-fix/listing.toml b/extensions/long-audio-fix/listing.toml
new file mode 100644
index 0000000..3c0a10d
--- /dev/null
+++ b/extensions/long-audio-fix/listing.toml
@@ -0,0 +1,3 @@
+details = "Play an AAC or Apple Lossless file (M4A, M4B) longer than 13.5 hours. It plays to the end instead of stopping at 13:31:35. Nothing is added to the menus."
+
+icon = "assets/settings-long-audio-fix.png"
diff --git a/extensions/long-audio-fix/main.c b/extensions/long-audio-fix/main.c
new file mode 100644
index 0000000..6a16b74
--- /dev/null
+++ b/extensions/long-audio-fix/main.c
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* The same fix as RepriseOS pull request 62 by kayboll, arrived at independently. */
+
+/* The MP4 parser compares a track's 64-bit decode time with its edit list after
+ * sign-extending the low word, so past 2^31 media ticks (13:31:35 at 44.1 kHz)
+ * every sample counts as outside the edit and the track ends. Each site holds
+ * `asr r1, r0, #31` with r0 the low word; the replacement loads the high word
+ * from the track instead. */
+
+#include "rv_api.h"
+#include "rv_sites.h"
+#include "rv_ui.h"
+
+static const struct rv_hook hooks[] = {
+ /* ldr r1, [r2, #-20]: r2 is track + edit_media_start, the high word of decode_time
+ * sits 20 bytes before it. */
+ RV_HOOK_WORD(RV_SITE_MP4Parser_ReadEditStartTest_word, 0xe5121014),
+ /* ldr r1, [r2, #-28]: r2 is track + edit_media_end. */
+ RV_HOOK_WORD(RV_SITE_MP4Parser_ReadEditEndTest_word, 0xe512101c),
+ /* The same compare in the seek walk, r2 again track + edit_media_end. */
+ RV_HOOK_WORD(RV_SITE_MP4Parser_SeekEditEndTest_word, 0xe512101c),
+};
+
+static const struct rv_descriptor extension = {
+ .size = sizeof extension,
+ RV_HOOKS(hooks),
+};
+
+const struct rv_descriptor *rv_module_entry(const struct rv_api *core,
+ rv_extension_handle handle)
+{
+ (void)core;
+ (void)handle;
+ return &extension;
+}
diff --git a/extensions/long-audio-fix/manifest.toml b/extensions/long-audio-fix/manifest.toml
new file mode 100644
index 0000000..1f5d05d
--- /dev/null
+++ b/extensions/long-audio-fix/manifest.toml
@@ -0,0 +1,13 @@
+id = "long-audio-fix"
+name = "Long Audio Fix"
+description = "AAC and Apple Lossless files longer than 13.5 hours (12.4 hours at 48 kHz) play to the end instead of stopping partway."
+version = "0.1.0"
+author = "RepriseOS and RevolaOS contributors"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
diff --git a/extensions/sleep-timer/Makefile b/extensions/sleep-timer/Makefile
new file mode 100644
index 0000000..3dde248
--- /dev/null
+++ b/extensions/sleep-timer/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-3.0-only
+main_SOURCES := main.c indicator.c
+include $(SDK)/rv.mk
diff --git a/extensions/sleep-timer/README.md b/extensions/sleep-timer/README.md
new file mode 100644
index 0000000..6600134
--- /dev/null
+++ b/extensions/sleep-timer/README.md
@@ -0,0 +1,13 @@
+# Sleep Timer
+
+Based on [Sleep Timer shortcut](https://github.com/RepriseOS/repriseos/pull/75) by kayboll in RepriseOS.
+
+Adds Sleep Timer to the Now Playing context menu (hold Select) of a song, podcast episode, iTunes U episode or audiobook. It opens a list of Apple's sleep timer choices, Off, 15, 30, 60, 90 and 120 Minutes, with the active one highlighted. Choosing one sets Apple's timer, the one Extras > Alarms > Sleep Timer sets, and returns to Now Playing.
+
+While the timer runs, Now Playing shows the minutes left and a moon before the shuffle icon. From an hour the minutes read h:mm.
+
+| | |
+|---|---|
+| Default | on |
+| Memory | image only; about 1 KB for the Now Playing image from setting the timer until Now Playing shows it stopped, and a 4.6 KB scratch while its minutes are drawn |
+| Settings | none |
diff --git a/extensions/sleep-timer/assets/LICENSE.cupertino-icons b/extensions/sleep-timer/assets/LICENSE.cupertino-icons
new file mode 100644
index 0000000..581f84c
--- /dev/null
+++ b/extensions/sleep-timer/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/sleep-timer/assets/render.py b/extensions/sleep-timer/assets/render.py
new file mode 100755
index 0000000..ce96c96
--- /dev/null
+++ b/extensions/sleep-timer/assets/render.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-3.0-only
+"""Render moon.h, the moon of the Now Playing timer: Cupertino Icons moon_fill
+fitted into an 11 x 11 coverage mask, one byte per pixel. Drawing uses
+ImageMagick's `magick`; the font (CupertinoIcons.ttf from the Flutter
+`cupertino_icons` package, MIT, see LICENSE.cupertino-icons) is not in the
+repository.
+
+ python3 assets/render.py --font CupertinoIcons.ttf
+ python3 assets/render.py --font CupertinoIcons.ttf --check
+"""
+
+import argparse
+import os
+import subprocess
+import sys
+from pathlib import Path
+
+HERE = Path(__file__).resolve().parent
+OUT = HERE.parent / "moon.h"
+SIZE = 11
+MOON_FILL = 0xF719
+
+
+def render(font):
+ pixels = subprocess.run(
+ [
+ "magick",
+ "-background",
+ "black",
+ "-fill",
+ "white",
+ "-font",
+ font,
+ "-pointsize",
+ "400",
+ f"label:{chr(MOON_FILL)}",
+ "-trim",
+ "+repage",
+ "-resize",
+ f"{SIZE}x{SIZE}",
+ "-gravity",
+ "Center",
+ "-extent",
+ f"{SIZE}x{SIZE}",
+ "-colorspace",
+ "gray",
+ "-depth",
+ "8",
+ "gray:-",
+ ],
+ check=True,
+ capture_output=True,
+ ).stdout
+ if len(pixels) != SIZE * SIZE:
+ sys.exit(f"magick returned {len(pixels)} bytes, not {SIZE * SIZE}")
+ rows = "\n".join(
+ " " + ", ".join(f"{p:3d}" for p in pixels[y * SIZE : (y + 1) * SIZE]) + ","
+ for y in range(SIZE)
+ )
+ return (
+ "/* SPDX-License-Identifier: GPL-3.0-only */\n\n"
+ "/* Generated by assets/render.py from Cupertino Icons moon_fill (MIT, see\n"
+ " * assets/LICENSE.cupertino-icons). */\n\n"
+ f"#define MOON_SIZE {SIZE}\n\n"
+ "/* clang-format off */\n"
+ f"static const unsigned char moon[MOON_SIZE * MOON_SIZE] = {{\n{rows}\n}};\n"
+ "/* clang-format on */\n"
+ )
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__.split("\n")[0])
+ parser.add_argument(
+ "--font", default=os.environ.get("CUPERTINO_ICONS"), help="CupertinoIcons.ttf"
+ )
+ parser.add_argument("--check", action="store_true", help="fail when moon.h differs")
+ args = parser.parse_args()
+ if not args.font:
+ sys.exit("give the font with --font or CUPERTINO_ICONS")
+ text = render(args.font)
+ if args.check:
+ if not OUT.exists() or OUT.read_text() != text:
+ sys.exit(f"{OUT.name} is out of date: run {Path(__file__).name}")
+ return
+ OUT.write_text(text)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/extensions/sleep-timer/assets/settings-sleep-timer.png b/extensions/sleep-timer/assets/settings-sleep-timer.png
new file mode 100644
index 0000000..090d4fc
Binary files /dev/null and b/extensions/sleep-timer/assets/settings-sleep-timer.png differ
diff --git a/extensions/sleep-timer/assets/settings-sleep-timer.svg b/extensions/sleep-timer/assets/settings-sleep-timer.svg
new file mode 100644
index 0000000..8803515
--- /dev/null
+++ b/extensions/sleep-timer/assets/settings-sleep-timer.svg
@@ -0,0 +1,5 @@
+
diff --git a/extensions/sleep-timer/indicator.c b/extensions/sleep-timer/indicator.c
new file mode 100644
index 0000000..11badb4
--- /dev/null
+++ b/extensions/sleep-timer/indicator.c
@@ -0,0 +1,210 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* The timer is drawn as the stock shuffle glyph is stored: an Indexed8 BMap of
+ * black at alpha levels up to the glyph's own 0xce. The minutes are drawn in
+ * Apple's Helvetica, black on an opaque white scratch, and read back as
+ * coverage, so the glyph blitter's handling of alpha does not matter. */
+
+#include "indicator.h"
+#include "moon.h"
+#include "osos-2.0.5.h"
+#include "rv_libc.h"
+#include
+
+enum {
+ HEIGHT = 14, /* the shuffle glyph's */
+ MOON_GAP = 3, /* between the minutes and the moon */
+ CHAIN_GAP = 5, /* before the shuffle glyph, as between shuffle and repeat */
+ LEVELS = 33,
+ INK = 0xce, /* the shuffle glyph's darkest alpha */
+ SCRATCH_W = 48, /* the minutes, "2:00" at most */
+ SCRATCH_H = 24,
+ FONT_SIZE = 0x40, /* bold, as tall as the glyphs beside it */
+};
+
+static uint32_t font[2];
+static uint8_t font_built;
+static uint8_t *built;
+static uint32_t built_minutes;
+static const void *built_shuffle;
+
+static uint32_t word(const uint8_t *p)
+{
+ return p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24;
+}
+
+static void put_word(uint8_t *p, uint32_t value)
+{
+ for (unsigned i = 0; i < 4; i++)
+ p[i] = (uint8_t)(value >> (8 * i));
+}
+
+static void header(uint8_t *b, uint16_t format, uint16_t stride, uint16_t depth,
+ uint32_t width, uint32_t height, uint32_t payload)
+{
+ for (unsigned i = 0; i < BMap_payload; i++)
+ b[i] = 0;
+ b[BMap_format] = (uint8_t)format;
+ b[BMap_format + 1] = (uint8_t)(format >> 8);
+ b[BMap_stride] = (uint8_t)stride;
+ b[BMap_stride + 1] = (uint8_t)(stride >> 8);
+ b[BMap_depth] = (uint8_t)depth;
+ put_word(b + BMap_bounds + 8, height);
+ put_word(b + BMap_bounds + 12, width);
+ put_word(b + BMap_payload_bytes, payload);
+}
+
+/* Draws `text` into a scratch block from `api` and leaves its coverage, trimmed
+ * to the ink, at the start of the block, SCRATCH_W bytes a row; the ink's width
+ * and height in *ink_w and *ink_h. NULL when nothing could be drawn. */
+static uint8_t *draw_minutes(const struct rv_api *api, rv_extension_handle self,
+ const char *text, int *ink_w, int *ink_h)
+{
+ uint8_t *scratch = api->alloc(self, BMap_payload + SCRATCH_W * SCRATCH_H * 4);
+ if (!scratch)
+ return NULL;
+ header(scratch, BMapFormat_BGRA8888, SCRATCH_W * 4, 32, SCRATCH_W, SCRATCH_H,
+ SCRATCH_W * SCRATCH_H * 4);
+ uint32_t storage[Bitmap_bytes / 4];
+ uint32_t context[GraphicsContext_bytes / 4];
+ Bitmap_Construct(storage);
+ GraphicsContext_InitFromBitmapResource(context, storage, scratch);
+ static const uint8_t white[4] = {0xff, 0xff, 0xff, 0xff},
+ black[4] = {0, 0, 0, 0xff};
+ int32_t all[4] = {0, 0, SCRATCH_H, SCRATCH_W};
+ GraphicsContext_SetColor(context, white);
+ GraphicsContext_SetDrawMode(context, GraphicsDrawMode_Copy);
+ GraphicsContext_FillRect(context, all);
+ if (!font_built) {
+ Font_Construct(font, "Helvetica", FONT_SIZE, 1);
+ font_built = 1;
+ }
+ GraphicsContext_SetFont(context, font);
+ GraphicsContext_SetColor(context, black);
+ GraphicsContext_SetDrawMode(context, GraphicsDrawMode_Text);
+ GraphicsContext_SetTextPen(context, 2, 2 + GraphicsContext_GetFontAscent(context));
+ GraphicsContext_DrawUtf8(context, text);
+ GraphicsContext_Destroy(context);
+ Bitmap_Destroy(storage);
+
+ /* Each coverage byte lands at or before the pixel it is read from. */
+ const uint8_t *pixels = scratch + BMap_payload;
+ int left = SCRATCH_W, right = -1, top = SCRATCH_H, bottom = -1;
+ for (int i = 0; i < SCRATCH_W * SCRATCH_H; i++) {
+ uint8_t c = 0xff - pixels[i * 4 + 2];
+ scratch[i] = c;
+ if (c) {
+ int x = i % SCRATCH_W, y = i / SCRATCH_W;
+ left = x < left ? x : left;
+ right = x > right ? x : right;
+ top = y < top ? y : top;
+ bottom = y > bottom ? y : bottom;
+ }
+ }
+ if (right < 0) {
+ api->free(self, scratch);
+ return NULL;
+ }
+ for (int y = top; y <= bottom; y++)
+ for (int x = left; x <= right; x++)
+ scratch[(y - top) * SCRATCH_W + x - left] = scratch[y * SCRATCH_W + x];
+ *ink_w = right - left + 1;
+ *ink_h = bottom - top + 1;
+ return scratch;
+}
+
+static uint8_t level(uint32_t coverage)
+{
+ return (uint8_t)((coverage * (LEVELS - 1) + 127) / 255);
+}
+
+/* The stock glyph's pixel alpha at (x, y) as one of our levels. */
+static uint8_t glyph_level(const uint8_t *g, int x, int y)
+{
+ uint32_t colors = word(g + BMap_payload),
+ stride = g[BMap_stride] | g[BMap_stride + 1] << 8;
+ uint8_t index = g[BMap_payload + 4 + colors * 4 + y * stride + x];
+ if (index >= colors)
+ return 0;
+ uint32_t alpha = g[BMap_payload + 4 + index * 4 + 3];
+ uint32_t l = (alpha * (LEVELS - 1) + INK / 2) / INK;
+ return (uint8_t)(l < LEVELS ? l : LEVELS - 1);
+}
+
+static int glyph_usable(const uint8_t *g, int *w, int *h)
+{
+ const int32_t top = (int32_t)word(g + BMap_bounds),
+ left = (int32_t)word(g + BMap_bounds + 4);
+ *h = (int32_t)word(g + BMap_bounds + 8) - top;
+ *w = (int32_t)word(g + BMap_bounds + 12) - left;
+ return g[BMap_format] == (uint8_t)BMapFormat_Indexed8 && !g[BMap_format + 1] &&
+ g[BMap_depth] == 8 && !top && !left && *w > 0 && *h > 0 && *h <= HEIGHT &&
+ word(g + BMap_payload) <= 256;
+}
+
+void indicator_free(const struct rv_api *api, rv_extension_handle self)
+{
+ if (built)
+ api->free(self, built);
+ built = NULL;
+}
+
+const void *indicator_build(const struct rv_api *api, rv_extension_handle self,
+ uint32_t minutes, const void *shuffle)
+{
+ if (built && built_minutes == minutes && built_shuffle == shuffle)
+ return built;
+ char text[8];
+ if (minutes < 60)
+ rv_format(text, sizeof text, "%lu", minutes);
+ else
+ rv_format(text, sizeof text, "%lu:%02lu", minutes / 60, minutes % 60);
+ int text_w, text_h;
+ uint8_t *cover = draw_minutes(api, self, text, &text_w, &text_h);
+ if (!cover)
+ return NULL;
+ int glyph_w = 0, glyph_h = 0;
+ if (shuffle && !glyph_usable(shuffle, &glyph_w, &glyph_h))
+ shuffle = NULL;
+
+ int timer = text_w + MOON_GAP + MOON_SIZE;
+ int width = timer + (shuffle ? CHAIN_GAP + glyph_w : 0);
+ uint32_t pixels = BMap_payload + 4 + LEVELS * 4;
+ uint8_t *b = api->alloc(self, pixels + (uint32_t)(width * HEIGHT));
+ if (!b) {
+ api->free(self, cover);
+ return NULL;
+ }
+ header(b, BMapFormat_Indexed8, (uint16_t)width, 8, (uint32_t)width, HEIGHT,
+ 4 + LEVELS * 4 + (uint32_t)(width * HEIGHT));
+ put_word(b + BMap_payload, LEVELS);
+ for (unsigned i = 0; i < LEVELS; i++) {
+ uint8_t *entry = b + BMap_payload + 4 + i * 4;
+ entry[0] = entry[1] = entry[2] = 0;
+ entry[3] = (uint8_t)(i * INK / (LEVELS - 1));
+ }
+ int text_y = (HEIGHT - text_h) / 2; /* ink taller than HEIGHT is clipped */
+ int moon_x = text_w + MOON_GAP, moon_y = (HEIGHT - MOON_SIZE) / 2;
+ for (int y = 0; y < HEIGHT; y++)
+ for (int x = 0; x < width; x++) {
+ uint8_t *out = b + pixels + y * width + x;
+ if (x < text_w)
+ *out = y >= text_y && y < text_y + text_h
+ ? level(cover[(y - text_y) * SCRATCH_W + x])
+ : 0;
+ else if (x >= moon_x && x < timer)
+ *out = y >= moon_y && y < moon_y + MOON_SIZE
+ ? level(moon[(y - moon_y) * MOON_SIZE + x - moon_x])
+ : 0;
+ else if (shuffle && x >= timer + CHAIN_GAP && y < glyph_h)
+ *out = glyph_level(shuffle, x - timer - CHAIN_GAP, y);
+ else
+ *out = 0;
+ }
+ api->free(self, cover);
+ indicator_free(api, self);
+ built = b;
+ built_minutes = minutes;
+ built_shuffle = shuffle;
+ return built;
+}
diff --git a/extensions/sleep-timer/indicator.h b/extensions/sleep-timer/indicator.h
new file mode 100644
index 0000000..4fd4073
--- /dev/null
+++ b/extensions/sleep-timer/indicator.h
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+#ifndef SLEEP_TIMER_INDICATOR_H
+#define SLEEP_TIMER_INDICATOR_H
+#include "rv_api.h"
+#include
+
+/* The Now Playing shuffle image with the timer before it: the minutes left
+ * and a moon, then `shuffle` (a stock BMap, NULL when shuffle is off) 5 px to
+ * their right. A BMap in a block from `api`, NULL when the heap is
+ * full; freed with indicator_free. UI task. */
+const void *indicator_build(const struct rv_api *api, rv_extension_handle self,
+ uint32_t minutes, const void *shuffle);
+void indicator_free(const struct rv_api *api, rv_extension_handle self);
+
+#endif
diff --git a/extensions/sleep-timer/listing.toml b/extensions/sleep-timer/listing.toml
new file mode 100644
index 0000000..741d663
--- /dev/null
+++ b/extensions/sleep-timer/listing.toml
@@ -0,0 +1,15 @@
+details = "Hold Select in Now Playing and choose Sleep Timer to set Apple's sleep timer without leaving the song. While it runs, Now Playing shows the minutes left before the shuffle icon."
+
+icon = "assets/settings-sleep-timer.png"
+
+[[screenshots]]
+file = "screenshots/context-menu.png"
+alt = "The Now Playing context menu of an audiobook with Sleep Timer highlighted, above File Info and Cancel."
+
+[[screenshots]]
+file = "screenshots/list.png"
+alt = "The Sleep Timer list: Off, 15, 30, 60, 90 and 120 Minutes, with 30 Minutes highlighted."
+
+[[screenshots]]
+file = "screenshots/now-playing.png"
+alt = "Now Playing with 30 and a moon in the top right, where the shuffle icon goes."
diff --git a/extensions/sleep-timer/main.c b/extensions/sleep-timer/main.c
new file mode 100644
index 0000000..95c99cc
--- /dev/null
+++ b/extensions/sleep-timer/main.c
@@ -0,0 +1,150 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Based on RepriseOS payload/sleep_timer.c (91d94ce) by kayboll.
+ *
+ * Sleep Timer in the Now Playing context menu: Apple's sleep timer choices as
+ * a full-width list. Choosing one sets the stock timer, the one Extras >
+ * Alarms > Sleep Timer sets, and returns to Now Playing. While the timer runs,
+ * Now Playing shows the minutes left and a moon before the shuffle icon. */
+
+#include "indicator.h"
+#include "osos-2.0.5.h"
+#include "rv_api.h"
+#include "rv_libc.h"
+#include "rv_sites.h"
+#include "rv_ui.h"
+
+#define POLL_MS 2000 /* the App counts the minutes on its own timer */
+
+static const struct rv_api *api;
+static rv_extension_handle self;
+static struct rv_timer *poll;
+static uint32_t shown; /* minutes on the image Now Playing has, 0 for none */
+static int stopping;
+static char label[16];
+
+static void *service(void)
+{
+ return AlarmsService_Get();
+}
+
+static uint32_t active_choice(void)
+{
+ return RV_FIELD(service(), AlarmsService_sleep_timer_choice, uint32_t);
+}
+
+static uint32_t remaining(void)
+{
+ return RV_FIELD(App, App_sleep_timer_remaining, uint32_t);
+}
+
+/* Now Playing asks for the shuffle image again. */
+static void refresh(void)
+{
+ Model_Notify(NowPlayingModel_Get(), Property_Bitmap, Property_ShuffleImage);
+}
+
+static void tick(void *arg)
+{
+ (void)arg;
+ uint32_t want = active_choice() ? remaining() : 0;
+ if (want != shown)
+ refresh();
+ if (!active_choice())
+ rv_timer_cancel(api, self, &poll);
+}
+
+static uint32_t property(uintptr_t model, uint32_t type, uint32_t which, uintptr_t out,
+ const void *next)
+{
+ uint32_t handled = api->hook_next(next, model, type, which, out, 0, 0);
+ if (type != Property_Bitmap || which != Property_ShuffleImage)
+ return handled;
+ uintptr_t *image = (uintptr_t *)out;
+ if (stopping || !active_choice()) {
+ /* Now Playing takes this answer in place of ours. */
+ indicator_free(api, self);
+ shown = 0;
+ return handled;
+ }
+ const void *bitmap =
+ indicator_build(api, self, remaining(), handled ? (void *)*image : NULL);
+ if (!bitmap)
+ return handled;
+ *image = (uintptr_t)bitmap;
+ shown = remaining();
+ if (!poll)
+ poll = api->timer_start(self, POLL_MS, 1, tick, NULL);
+ return 1;
+}
+
+static uint32_t count(const struct rv_context *ctx)
+{
+ (void)ctx;
+ return RV_FIELD(service(), AlarmsService_sleep_timer_choice_count, uint32_t);
+}
+
+static const char *row(const struct rv_context *ctx, uint32_t index,
+ const char **detail)
+{
+ (void)ctx;
+ *detail = NULL;
+ const uint8_t *choices =
+ RV_FIELD(service(), AlarmsService_sleep_timer_choices, const uint8_t *);
+ uint32_t minutes = RV_FIELD(choices + index * SleepTimerChoice_bytes,
+ SleepTimerChoice_minutes, uint32_t);
+ if (!minutes)
+ return "Off";
+ rv_format(label, sizeof label, "%lu Minutes", minutes);
+ return label;
+}
+
+static int choose(const struct rv_context *ctx, uint32_t index)
+{
+ (void)ctx;
+ AlarmsService_SelectSleepTimer(service(), index);
+ refresh();
+ return 1;
+}
+
+static uint32_t active(const struct rv_context *ctx)
+{
+ (void)ctx;
+ return active_choice();
+}
+
+static void unload(void)
+{
+ stopping = 1;
+ refresh();
+ indicator_free(api, self);
+}
+
+static const struct rv_hook hooks[] = {
+ RV_HOOK_FILTER(RV_SITE_NowPlayingModel_Property_slot, property),
+};
+
+static const struct rv_screen screens[] = {
+ RV_SCREEN_DYNAMIC_LIST("timer", "Sleep Timer", count, row, .dynamic.select = choose,
+ .layout = RV_LAYOUT_FULL, .dynamic.initial = active),
+};
+
+static const struct rv_menu_entry entries[] = {
+ RV_MENU_ENTRY(RV_MENU_NOW_PLAYING_CONTEXT, 0, RV_ROW_PUSH("Sleep Timer", "timer")),
+};
+
+static const struct rv_descriptor extension = {
+ .size = sizeof extension,
+ RV_HOOKS(hooks),
+ RV_SCREENS(screens),
+ RV_ENTRIES(entries),
+ .unload = unload,
+};
+
+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/sleep-timer/manifest.toml b/extensions/sleep-timer/manifest.toml
new file mode 100644
index 0000000..2b9dc38
--- /dev/null
+++ b/extensions/sleep-timer/manifest.toml
@@ -0,0 +1,13 @@
+id = "sleep-timer"
+name = "Sleep Timer"
+description = "Set Apple's sleep timer from the Now Playing context menu and see the minutes left in Now Playing."
+version = "0.1.0"
+author = "RepriseOS and RevolaOS contributors"
+license = "GPL-3.0-only"
+api = "1.82"
+stability = "beta"
+category = "media"
+
+[[module]]
+name = "main"
+trigger = "always"
diff --git a/extensions/sleep-timer/moon.h b/extensions/sleep-timer/moon.h
new file mode 100644
index 0000000..0621d94
--- /dev/null
+++ b/extensions/sleep-timer/moon.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+
+/* Generated by assets/render.py from Cupertino Icons moon_fill (MIT, see
+ * assets/LICENSE.cupertino-icons). */
+
+#define MOON_SIZE 11
+
+/* clang-format off */
+static const unsigned char moon[MOON_SIZE * MOON_SIZE] = {
+ 0, 9, 124, 201, 30, 0, 0, 0, 0, 0, 0,
+ 4, 128, 253, 187, 8, 0, 0, 0, 0, 0, 0,
+ 82, 241, 255, 165, 1, 0, 0, 0, 0, 0, 0,
+ 188, 255, 255, 179, 5, 0, 0, 0, 0, 0, 0,
+ 242, 255, 255, 221, 32, 0, 0, 0, 0, 0, 0,
+ 254, 255, 255, 255, 132, 2, 0, 0, 0, 0, 0,
+ 237, 255, 255, 255, 244, 129, 28, 2, 0, 2, 14,
+ 174, 255, 255, 255, 255, 254, 215, 167, 148, 168, 187,
+ 60, 225, 255, 255, 255, 255, 255, 255, 255, 255, 145,
+ 0, 81, 221, 255, 255, 255, 255, 255, 245, 145, 16,
+ 0, 0, 52, 161, 228, 249, 241, 195, 95, 8, 0,
+};
+/* clang-format on */
diff --git a/extensions/sleep-timer/screenshots/context-menu.png b/extensions/sleep-timer/screenshots/context-menu.png
new file mode 100644
index 0000000..a03bd07
Binary files /dev/null and b/extensions/sleep-timer/screenshots/context-menu.png differ
diff --git a/extensions/sleep-timer/screenshots/list.png b/extensions/sleep-timer/screenshots/list.png
new file mode 100644
index 0000000..5de4477
Binary files /dev/null and b/extensions/sleep-timer/screenshots/list.png differ
diff --git a/extensions/sleep-timer/screenshots/now-playing.png b/extensions/sleep-timer/screenshots/now-playing.png
new file mode 100644
index 0000000..e2598b4
Binary files /dev/null and b/extensions/sleep-timer/screenshots/now-playing.png differ
diff --git a/tests/test_catalog.py b/tests/test_catalog.py
index fd4de66..14c6ca5 100644
--- a/tests/test_catalog.py
+++ b/tests/test_catalog.py
@@ -40,8 +40,9 @@ def test_preview_packages_and_referenced_images(self):
for entry in entries.values():
assets = [entry["package"]]
if entry["category"] != "library":
- self.assertTrue(entry["screenshots"][0]["alt"])
- self.assertEqual(entry["screenshots"][0]["width"], 320)
+ for shot in entry["screenshots"]:
+ self.assertTrue(shot["alt"])
+ self.assertEqual(shot["width"], 320)
assets += [entry["icon"], *entry["screenshots"]]
for asset in assets:
data = (self.out / asset["url"]).read_bytes()