diff --git a/default/systemd/user/omarchy-clock-resync.service b/default/systemd/user/omarchy-clock-resync.service new file mode 100644 index 000000000000..093c3c81d562 --- /dev/null +++ b/default/systemd/user/omarchy-clock-resync.service @@ -0,0 +1,11 @@ +[Unit] +Description=Restart the bar clock after the system clock jumped +# The same environment omarchy-sleep-lock waits for: the IPC call finds the +# running shell through OMARCHY_PATH and WAYLAND_DISPLAY. -q makes a shell that +# is not running, or still starting, a success rather than a failed unit. +ConditionEnvironment=OMARCHY_PATH +ConditionEnvironment=WAYLAND_DISPLAY + +[Service] +Type=oneshot +ExecStart=/usr/bin/omarchy-shell -q omarchy.clock resync diff --git a/default/systemd/user/omarchy-clock-resync.timer b/default/systemd/user/omarchy-clock-resync.timer new file mode 100644 index 000000000000..8bb0120ad9cb --- /dev/null +++ b/default/systemd/user/omarchy-clock-resync.timer @@ -0,0 +1,16 @@ +[Unit] +Description=Watch for the system clock jumping +# Nothing to count down: OnClockChange= waits on a timerfd the user manager +# already holds, so the timer costs no process and no wakeups. The kernel +# cancels that timerfd when CLOCK_REALTIME jumps against CLOCK_MONOTONIC, which +# is what a resume from suspend does as well as a clock being set. +PartOf=graphical-session.target + +[Timer] +OnClockChange=yes +# A zone change leaves the label on the old zone until the next tick, which is +# why omarchy-menu-timezone pushes a refresh; this covers any other way it is set. +OnTimezoneChange=yes + +[Install] +WantedBy=graphical-session.target diff --git a/docs/file-layout.md b/docs/file-layout.md index 1825c6c441bb..d00dfa09484b 100644 --- a/docs/file-layout.md +++ b/docs/file-layout.md @@ -337,7 +337,7 @@ and/or a working user systemd instance: `systemctl --user enable --now` the shipped user units (`bt-agent`, `omarchy-sleep-lock`, `omarchy-recover-internal-monitor`, `omarchy-migrate-notify.service`, `omarchy-fcitx5.service`, - `omarchy-crash-watch.service`) so they run in the first session too. + `omarchy-crash-watch.service`, `omarchy-clock-resync.timer`) so they run in the first session too. Done here, not at finalize, because the user manager isn't reachable from the ISO chroot; `ConditionPath*` in the unit files keeps services inert when they don't apply. diff --git a/docs/omarchy-shell.md b/docs/omarchy-shell.md index 98559d019c7f..4e115cdf95aa 100644 --- a/docs/omarchy-shell.md +++ b/docs/omarchy-shell.md @@ -97,6 +97,12 @@ bar from the CLI — `use | reset | defaults | position | background | put | move | set | get`, with placement flags such as `--section` and `--index`. The lower-level IPC methods remain available through `omarchy-shell shell ...`. +## Waking from suspend + +`SystemClock` waits out the rest of the minute on a timer that stops while the machine is suspended, so it wakes still reporting the time it had before, for up to a minute. `omarchy-clock-resync.timer`, a user timer shipped in `default/systemd/user/`, fixes that for the bar clock without a process or a poll. It sets `OnClockChange=yes`, which makes systemd's user manager wait on a `timerfd` created with `TFD_TIMER_CANCEL_ON_SET`. The kernel cancels that timerfd when `CLOCK_REALTIME` jumps against `CLOCK_MONOTONIC`, and resuming from suspend does that as well as `settimeofday`, an NTP step and `timedatectl set-time`. `OnTimezoneChange=yes` covers a changed timezone. The timer starts `omarchy-clock-resync.service`, which runs `omarchy-shell -q omarchy.clock resync`. + +`resync` restarts the label's `SystemClock` and the calendar's: a re-enabled `SystemClock` re-reads the time and schedules its next tick from now. Unlike `refresh`, it leaves a month the user is browsing in the calendar where it is. A plugin with a `SystemClock` of its own can register an IPC method that does the same and have a user unit call it. + ## Elsewhen Elsewhen (`omarchy.elsewhen`), the world clock, is a first-party plugin in `shell/plugins/panels/elsewhen/`; [`elsewhen.md`](elsewhen.md) covers how it works. It shipped as the separate `elsewhen` package under the id `omacom.elsewhen` until it moved in. New installs place it immediately after the clock, before Weather; the placement migration uses `omarchy bar put omarchy.elsewhen --after omarchy.clock`, which preserves an existing placement and uses Elsewhen's normal right-side placement if the clock is absent. With no shell to ask, as in an update from a TTY, it skips the placement rather than failing the update. A later migration renames existing `omacom.elsewhen` entries in `shell.json`, keeping their settings, and removes the retired package and any dev-checkout link to its `/usr/share/omarchy` path, leaving links and checkouts the user made alone. diff --git a/install/user/first-run/enable-user-units.sh b/install/user/first-run/enable-user-units.sh index 0f73262282ec..a9b627376edd 100755 --- a/install/user/first-run/enable-user-units.sh +++ b/install/user/first-run/enable-user-units.sh @@ -13,6 +13,15 @@ set -euo pipefail omarchy-input-method configure --defaults || echo "Input method defaults deferred; run omarchy setup input after login." >&2 +# The clock resync units live in default/, so link them where the user manager +# looks. A link follows the shipped file, so it never goes stale. +mkdir -p "$HOME/.config/systemd/user" +for unit in omarchy-clock-resync.{service,timer}; do + if [[ ! -e /usr/lib/systemd/user/$unit ]]; then + ln -sfn "$OMARCHY_PATH/default/systemd/user/$unit" "$HOME/.config/systemd/user/$unit" + fi +done + systemctl --user daemon-reload # One at a time: given a list, systemctl enables none of it when it cannot find @@ -25,7 +34,8 @@ for unit in \ omarchy-sleep-lock.service \ omarchy-migrate-notify.service \ omarchy-fcitx5.service \ - omarchy-crash-watch.service; do + omarchy-crash-watch.service \ + omarchy-clock-resync.timer; do systemctl --user enable --now "$unit" || failed=1 done diff --git a/migrations/1795277547.sh b/migrations/1795277547.sh new file mode 100644 index 000000000000..adf79eebc7a9 --- /dev/null +++ b/migrations/1795277547.sh @@ -0,0 +1,34 @@ +echo "Catch the bar clock up when the system clock jumps" + +# SystemClock waits out the rest of the minute on a timer that stops during +# suspend, so after a wake the bar kept the old time for up to a minute. The +# timer restarts the clock when the system clock jumps, which a wake is. New +# installs enable it with the other user units; this is for existing ones. + +# The units live in default/, so link them where the user manager looks. A +# link follows the shipped file, so it never goes stale. +mkdir -p "$HOME/.config/systemd/user" +for unit in omarchy-clock-resync.service omarchy-clock-resync.timer; do + if [[ ! -e /usr/lib/systemd/user/$unit ]]; then + ln -sfn "$OMARCHY_PATH/default/systemd/user/$unit" "$HOME/.config/systemd/user/$unit" + fi +done + +systemctl --user daemon-reload >/dev/null 2>&1 || true + +# `systemctl enable` needs a live user manager, which an update from a TTY does +# not have, so fall back to writing the symlink it would have written. +if ! systemctl --user enable omarchy-clock-resync.timer >/dev/null 2>&1; then + wants_dir="$HOME/.config/systemd/user/graphical-session.target.wants" + mkdir -p "$wants_dir" + if [[ -e /usr/lib/systemd/user/omarchy-clock-resync.timer ]]; then + ln -sfn /usr/lib/systemd/user/omarchy-clock-resync.timer "$wants_dir/omarchy-clock-resync.timer" + else + ln -sfn "$HOME/.config/systemd/user/omarchy-clock-resync.timer" "$wants_dir/omarchy-clock-resync.timer" + fi +fi + +# Nothing to start into over SSH; the next graphical login handles it. +if systemctl --user is-active --quiet graphical-session.target; then + systemctl --user start omarchy-clock-resync.timer >/dev/null 2>&1 || true +fi diff --git a/shell/plugins/panels/clock/BarWidget.qml b/shell/plugins/panels/clock/BarWidget.qml index b69f2f157c50..a70597ad78b9 100644 --- a/shell/plugins/panels/clock/BarWidget.qml +++ b/shell/plugins/panels/clock/BarWidget.qml @@ -40,6 +40,16 @@ BarWidget { if (panelLoader.item && panelLoader.item.refresh) panelLoader.item.refresh() } + // The timer stops during suspend, so after a wake the label kept the time + // from before it for up to a minute. Resyncing re-reads the time and arms + // its next tick from now, and onDateChanged below puts the result on the + // label. Unlike refresh() this leaves a month the user is browsing in the + // calendar where it is. + function resync() { + clock.resync() + if (panelLoader.item && panelLoader.item.resync) panelLoader.item.resync() + } + function cycleFormat() { var current = String(configuredFormat) var next = Model.nextClockFormat(formatRing, current) @@ -142,6 +152,7 @@ BarWidget { target: "omarchy.clock" function refresh(): void { root.broadcast("refresh") } + function resync(): void { root.broadcast("resync") } function cycleFormat(): void { root.cycleFormat() } function toggleWeekStart(): void { root.toggleWeekStart() } function open(): void { root.open() } diff --git a/shell/plugins/panels/clock/Panel.qml b/shell/plugins/panels/clock/Panel.qml index 6b9fcd244207..11cd57a9ed1e 100644 --- a/shell/plugins/panels/clock/Panel.qml +++ b/shell/plugins/panels/clock/Panel.qml @@ -120,6 +120,13 @@ Panel { root.goToToday() } + // The same catch-up for the calendar's own clock, which is how today moves + // after a sleep that crossed midnight. Its onDateChanged keeps a browsed + // month, where refresh() would send it back to today. + function resync() { + clock.resync() + } + function goToToday() { root.viewYear = today.getFullYear() root.viewMonth = today.getMonth() diff --git a/shell/plugins/panels/clock/WallClock.qml b/shell/plugins/panels/clock/WallClock.qml index da1f87a7f36e..560367f212c6 100644 --- a/shell/plugins/panels/clock/WallClock.qml +++ b/shell/plugins/panels/clock/WallClock.qml @@ -18,6 +18,14 @@ Timer { restart() } + // A timer started before suspend keeps counting down that old interval, so + // after a wake (or a stepped system clock) the label stays stale until the + // next tick. Re-read now and arm the next tick from here. + function resync() { + date = new Date() + arm() + } + onStepChanged: arm() onTriggered: { date = new Date() diff --git a/test/shell.d/clock-resync-test.sh b/test/shell.d/clock-resync-test.sh new file mode 100755 index 000000000000..0b7953f1d3f9 --- /dev/null +++ b/test/shell.d/clock-resync-test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +units="$ROOT/default/systemd/user" + +require_command systemd-analyze + +# systemd ignores a key it does not know with only a warning, so a misspelt +# OnClockChange= would leave a timer that never fires. verify also checks that +# the ExecStart binary exists, so it needs the package. +if [[ -x /usr/bin/omarchy-shell ]]; then + verify=$(systemd-analyze --user verify "$units/omarchy-clock-resync.timer" "$units/omarchy-clock-resync.service" 2>&1) || + fail "systemd accepts the clock resync units" "$verify" + [[ -z $verify ]] || fail "systemd accepts the clock resync units" "$verify" + pass "systemd accepts the clock resync units" +else + skip "systemd accepts the clock resync units: /usr/bin/omarchy-shell is not installed" +fi diff --git a/test/shell.d/config-test.sh b/test/shell.d/config-test.sh index 83397007bf5a..1bf0fbea24ca 100755 --- a/test/shell.d/config-test.sh +++ b/test/shell.d/config-test.sh @@ -126,11 +126,12 @@ for source, destination, legacy in package_defaults: errors.append(f"PKGBUILD does not explicitly install {source} -> {destination}") # A user unit has to be on the machine before anything turns it on: shipped in -# /usr/lib/systemd/user, or copied out of default/ by the command enabling it. +# /usr/lib/systemd/user, or copied or linked out of default/ by the command +# enabling it. def installs_first(text): source = re.search(r'(\w+)="[^"\n]*default/systemd/user/', text) held = rf"|\$\{{?{source[1]}\b" if source else "" - copy = re.search(rf"^[ \t]*(?:sudo[ \t]+)?(?:install|cp)\b[^\n]*(?:default/systemd/user/{held})", text, re.MULTILINE) + copy = re.search(rf"^[ \t]*(?:sudo[ \t]+)?(?:install|cp|ln)\b[^\n]*(?:default/systemd/user/{held})", text, re.MULTILINE) enable = text.find("systemctl --user enable") return bool(copy) and (enable < 0 or copy.start() < enable) diff --git a/test/shell.d/first-run-test.sh b/test/shell.d/first-run-test.sh index 974488181f96..32ba61549834 100644 --- a/test/shell.d/first-run-test.sh +++ b/test/shell.d/first-run-test.sh @@ -42,7 +42,7 @@ chmod +x "$mock_bin/systemctl" "$mock_bin/omarchy-hook-install" enable_user_units() { rm -f "$test_tmp/calls" - PATH="$mock_bin:$PATH" OMARCHY_TEST_CALLS="$test_tmp/calls" OMARCHY_TEST_MISSING_UNIT="$1" \ + HOME="$test_tmp/home" OMARCHY_PATH="$ROOT" PATH="$mock_bin:$PATH" OMARCHY_TEST_CALLS="$test_tmp/calls" OMARCHY_TEST_MISSING_UNIT="$1" \ bash "$ROOT/install/user/first-run/enable-user-units.sh" } @@ -62,6 +62,16 @@ done (( ${#units[@]} > 1 )) || fail "first-run enables each unit on its own" pass "a unit missing from the build costs first-run only that unit" +# Units omarchy-settings does not ship yet are copied where the user manager +# looks before anything enables them, under the user's home and nowhere else. +for unit in omarchy-clock-resync.service omarchy-clock-resync.timer; do + [[ -e /usr/lib/systemd/user/$unit ]] || cmp -s "$ROOT/default/systemd/user/$unit" "$test_tmp/home/.config/systemd/user/$unit" || + fail "first-run puts the clock resync units where the user manager finds them" "$unit" +done +grep -Fxq -- '--user enable --now omarchy-clock-resync.timer' <<<"$clean_run" || + fail "first-run enables the clock resync timer" +pass "first-run enables the clock resync timer" + cat >"$mock_bin/nm-online" <<'SH' #!/bin/bash exit 0 diff --git a/test/shell.d/fixtures/bar-widget-contract/shell.qml b/test/shell.d/fixtures/bar-widget-contract/shell.qml index a43e781cde40..1a8718147bad 100644 --- a/test/shell.d/fixtures/bar-widget-contract/shell.qml +++ b/test/shell.d/fixtures/bar-widget-contract/shell.qml @@ -62,6 +62,50 @@ ShellRoot { return isFinite(n) && n >= 0 } + function calendarOf(item) { + for (var i = 0; i < item.children.length; i++) { + var child = item.children[i] + if (child && child.item && ("viewMonth" in child.item)) return child.item + } + return null + } + + // The WallClock a QML object declares, found by the properties only it has. + function wallClockOf(item) { + for (var i = 0; i < item.data.length; i++) { + var child = item.data[i] + if (child && ("step" in child) && ("date" in child) && typeof child.arm === "function") return child + } + return null + } + + // A wake leaves WallClock counting down an interval it started before the + // sleep. Resyncing is what re-reads the time, so the catch-up the system + // clock timer asks for has to update both the label's clock and the + // calendar's, and has to do it without sending a browsed month back to today. + function assertClockResyncs(item) { + var calendar = calendarOf(item) + root.assertTrue(calendar !== null, "omarchy.clock loads its calendar") + if (!calendar) return + + var labelClock = wallClockOf(item) + var calendarClock = wallClockOf(calendar) + root.assertTrue(labelClock !== null && calendarClock !== null, "omarchy.clock and its calendar each run a WallClock") + if (!labelClock || !calendarClock) return + + calendar.viewMonth = (calendar.viewMonth + 3) % 12 + var browsed = calendar.viewMonth + + var stale = new Date(Date.now() - 5 * 60 * 1000) + labelClock.date = stale + calendarClock.date = stale + item.resync() + + root.assertTrue(Math.abs(labelClock.date.getTime() - Date.now()) < 2000, "omarchy.clock catches its label up when the system clock jumps") + root.assertTrue(Math.abs(calendarClock.date.getTime() - Date.now()) < 2000, "omarchy.clock catches its calendar up when the system clock jumps") + root.assertEqual(calendar.viewMonth, browsed, "omarchy.clock leaves a browsed month alone when the system clock jumps") + } + function loadWidget(entry) { var component = Qt.createComponent(entry.url, Component.PreferSynchronous) if (component.status !== Component.Ready) { @@ -162,6 +206,7 @@ ShellRoot { var id = root.createdIds[j] root.assertTrue(root.finiteDimension(item.implicitWidth), id + " has a finite implicitWidth") root.assertTrue(root.finiteDimension(item.implicitHeight), id + " has a finite implicitHeight") + if (id === "omarchy.clock") root.assertClockResyncs(item) } fakeBar.vertical = true diff --git a/test/shell.d/owe-integration-test.sh b/test/shell.d/owe-integration-test.sh index dec14af0efe5..805fab263a71 100644 --- a/test/shell.d/owe-integration-test.sh +++ b/test/shell.d/owe-integration-test.sh @@ -19,13 +19,13 @@ SH done : >"$TEST_CALLS" -HOME="$work/home" PATH="$work/bin:$PATH" bash "$ROOT/install/user/first-run/enable-user-units.sh" +HOME="$work/home" OMARCHY_PATH="$ROOT" PATH="$work/bin:$PATH" bash "$ROOT/install/user/first-run/enable-user-units.sh" grep -E '^systemctl --user enable --now .*owed.service' "$TEST_CALLS" >/dev/null grep -Fx 'omarchy-hook-install theme-set /usr/share/owe/10-owe-sync' "$TEST_CALLS" >/dev/null pass "fresh installs enable OWE and install the theme refresh hook" : >"$TEST_CALLS" -HOME="$work/home" PATH="$work/bin:$PATH" TEST_INPUT_FAIL=1 bash "$ROOT/install/user/first-run/enable-user-units.sh" +HOME="$work/home" OMARCHY_PATH="$ROOT" PATH="$work/bin:$PATH" TEST_INPUT_FAIL=1 bash "$ROOT/install/user/first-run/enable-user-units.sh" grep -Fx 'omarchy-input-method configure --defaults' "$TEST_CALLS" >/dev/null grep -E '^systemctl --user enable --now .*owed.service' "$TEST_CALLS" >/dev/null pass "failed input defaults do not block enabling first-run services"