Count typing as activity in both desktop break meters
This commit is contained in:
1 parent
a609e0aa6f
commit
541b8eb996
9 files changed
+243
-27
No files matched your search
@@ -23,6 +23,10 @@ sun-salutation-assets-test:
|
||||
break-engine-test: $(ZI2C_BIN)
|
||||
@env -u DISPLAY -u WAYLAND_DISPLAY sh tests/break_engine_zi_test.sh $(ZIRAN_BUILD_DIR)/bin
|
||||
|
||||
.PHONY: break-activity-x11-test
|
||||
break-activity-x11-test: $(ZI2C_BIN)
|
||||
@sh tests/break_activity_x11_test.sh $(ZIRAN_BUILD_DIR)/bin
|
||||
|
||||
.PHONY: breath-engine-test
|
||||
breath-engine-test: $(ZI2C_BIN)
|
||||
@env -u DISPLAY -u WAYLAND_DISPLAY sh tests/breath_rounds_zi_test.sh $(ZIRAN_BUILD_DIR)/bin
|
||||
|
||||
+26
-24
@@ -22,6 +22,7 @@ WEB :: #defined(PLATFORM_WEB)
|
||||
#import, file "break_reminder_layout.zi";
|
||||
#import, file "break_calendar.zi";
|
||||
#import, file "break_types.zi";
|
||||
#import, file "break_activity.zi";
|
||||
#import "date_time"
|
||||
#import "std/vec"
|
||||
|
||||
@@ -76,10 +77,8 @@ WIN32 :: #defined(_WIN32)
|
||||
app_web_extension_break_now :: (break_type: s32) -> void #foreign c;
|
||||
}
|
||||
|
||||
BreakActivityThresholdMs :: 1000
|
||||
BreakEventQueueMax :: 8
|
||||
BreakStatsFlushS :: 60
|
||||
BreakFallbackInputTicks :: 90
|
||||
BreakCatchupMaxS :: 300
|
||||
|
||||
last_window_fail_frame: s32 = -10000;
|
||||
@@ -216,27 +215,28 @@ break_persist_daily_state :: (app: *InnerBreeze) {
|
||||
app.settings_dirty = 1
|
||||
}
|
||||
|
||||
break_fallback_input_seen :: () -> s32 {
|
||||
break_fallback_pointer_seen :: () -> bool {
|
||||
wheel := cast(s32)(GetMouseWheelMove() * 1000.0f)
|
||||
if wheel != 0 {
|
||||
return 1
|
||||
return true
|
||||
}
|
||||
dx: float32 = GetMouseDelta().x
|
||||
dy: float32 = GetMouseDelta().y
|
||||
if dx != 0.0f || dy != 0.0f {
|
||||
return 1
|
||||
return true
|
||||
}
|
||||
{
|
||||
k: s32 = KEY_SPACE
|
||||
while k <= KEY_KB_MENU {
|
||||
if IsKeyPressed(k) {
|
||||
return 1
|
||||
}
|
||||
k += 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
break_fallback_key_seen :: () -> bool {
|
||||
k: s32 = KEY_SPACE
|
||||
while k <= KEY_KB_MENU {
|
||||
if IsKeyPressed(k) {
|
||||
return true
|
||||
}
|
||||
k += 1
|
||||
}
|
||||
return 0
|
||||
return false
|
||||
}
|
||||
|
||||
break_sample_activity :: (app: *InnerBreeze, idle_ms_out: *s32) -> s32 {
|
||||
@@ -245,17 +245,19 @@ break_sample_activity :: (app: *InnerBreeze, idle_ms_out: *s32) -> s32 {
|
||||
idle_ms_out.* = cast(s32)idle_ms
|
||||
}
|
||||
|
||||
if idle_ms >= 0 {
|
||||
return ifx (idle_ms < BreakActivityThresholdMs) then (1) else (0)
|
||||
input: BreakActivityInput
|
||||
input.idle_ms = idle_ms
|
||||
input.frame = app.breathing.frame
|
||||
if idle_ms < 0 {
|
||||
input.focused = IsWindowFocused()
|
||||
if input.focused {
|
||||
input.key_pressed = break_fallback_key_seen()
|
||||
input.pointer_active = break_fallback_pointer_seen()
|
||||
}
|
||||
}
|
||||
|
||||
if !IsWindowFocused() {
|
||||
return 0
|
||||
}
|
||||
if break_fallback_input_seen() != 0 {
|
||||
app.break_fallback_last_input = app.breathing.frame
|
||||
}
|
||||
return ifx (app.breathing.frame - app.break_fallback_last_input < BreakFallbackInputTicks) then (1) else (0)
|
||||
sample := BreakSampleActivity(input, app.break_fallback_last_input)
|
||||
app.break_fallback_last_input = sample.last_input_frame
|
||||
return sample.active
|
||||
}
|
||||
|
||||
break_play_cue :: (app: *InnerBreeze, scale: float32) {
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
BreakActivityThresholdMs :: 1000
|
||||
BreakFallbackInputTicks :: 90
|
||||
|
||||
BreakActivityInput :: struct {
|
||||
idle_ms: s64
|
||||
frame: s32
|
||||
focused: bool
|
||||
key_pressed: bool
|
||||
pointer_active: bool
|
||||
}
|
||||
|
||||
BreakActivitySample :: struct {
|
||||
active: s32
|
||||
last_input_frame: s32
|
||||
}
|
||||
|
||||
// One activity decision feeds the engine used by the floating bars and overlay.
|
||||
// Session idle time includes input in other applications, irrespective of focus.
|
||||
BreakSampleActivity :: (input: BreakActivityInput,
|
||||
last_input_frame: s32) -> BreakActivitySample {
|
||||
sample: BreakActivitySample
|
||||
sample.last_input_frame = last_input_frame
|
||||
if input.idle_ms >= 0 {
|
||||
sample.active = ifx input.idle_ms < BreakActivityThresholdMs then 1 else 0
|
||||
return sample
|
||||
}
|
||||
if !input.focused {
|
||||
return sample
|
||||
}
|
||||
if input.key_pressed || input.pointer_active {
|
||||
sample.last_input_frame = input.frame
|
||||
}
|
||||
sample.active = ifx input.frame - sample.last_input_frame < BreakFallbackInputTicks then 1 else 0
|
||||
return sample
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#import "idle_time"
|
||||
#import, file "../src/breaks/break_types.zi";
|
||||
#import, file "../src/breaks/break_activity.zi";
|
||||
#import, file "../src/breaks/break_rules.zi";
|
||||
|
||||
engine: BreakEngine;
|
||||
initialized: bool;
|
||||
active: s32;
|
||||
|
||||
#program_export
|
||||
Sample :: () -> s64 {
|
||||
if !initialized {
|
||||
initialized = true
|
||||
engine = BreakInit()
|
||||
}
|
||||
input: BreakActivityInput
|
||||
input.idle_ms = IdleMilliseconds()
|
||||
// This host has no focused app window and receives no local input events.
|
||||
sample := BreakSampleActivity(input, -10000)
|
||||
active = sample.active
|
||||
for tick: 0..BreakTicksPerSecond - 1 {
|
||||
engine = BreakTick(engine, active)
|
||||
}
|
||||
return input.idle_ms
|
||||
}
|
||||
|
||||
#program_export
|
||||
Activity :: () -> s32 { return active }
|
||||
#program_export
|
||||
MicroIdle :: () -> s32 { return engine.timers[BREAK_MICRO].idle_s }
|
||||
#program_export
|
||||
RestIdle :: () -> s32 { return engine.timers[BREAK_REST].idle_s }
|
||||
@@ -0,0 +1,63 @@
|
||||
"""Type into another owned window on the private Xvfb display, sampling Breeze."""
|
||||
import os
|
||||
import pathlib
|
||||
import select
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
def main():
|
||||
display = os.environ["DISPLAY"]
|
||||
number = display.removeprefix(":").split(".")[0]
|
||||
# xvfb-run owns this server; also verify its live executable before input.
|
||||
lock = pathlib.Path(f"/tmp/.X{number}-lock")
|
||||
pid = int(lock.read_text().strip())
|
||||
assert pathlib.Path(f"/proc/{pid}/exe").resolve().name == "Xvfb", display
|
||||
assert number != "0", "refusing the real desktop display"
|
||||
title = f"Breeze typing test {os.getpid()}"
|
||||
other = subprocess.Popen(["xmessage", "-title", title, "Private typing target"])
|
||||
probe = subprocess.Popen([sys.argv[1]], stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE, text=True, bufsize=1)
|
||||
typing = None
|
||||
|
||||
def sample():
|
||||
probe.stdin.write("\n")
|
||||
probe.stdin.flush()
|
||||
assert select.select([probe.stdout], [], [], 5)[0], "probe timed out"
|
||||
values = tuple(map(int, probe.stdout.readline().split()))
|
||||
assert len(values) == 4 and values[0] >= 0, values
|
||||
return values
|
||||
|
||||
try:
|
||||
window = subprocess.check_output(
|
||||
["xdotool", "search", "--sync", "--onlyvisible", "--name", title],
|
||||
text=True, timeout=10).splitlines()[0]
|
||||
subprocess.run(["xdotool", "windowfocus", "--sync", window], check=True)
|
||||
time.sleep(1.6)
|
||||
idle = sample()
|
||||
assert idle[1] == 0 and idle[2] > 0 and idle[3] > 0, idle
|
||||
typing = subprocess.Popen(["xdotool", "type", "--delay", "35",
|
||||
"continuous keyboard activity " * 6])
|
||||
# Allow the shared OS sampler's 500 ms cache to refresh.
|
||||
time.sleep(0.65)
|
||||
samples = 0
|
||||
while typing.poll() is None:
|
||||
values = sample()
|
||||
assert values[1:] == (1, 0, 0), values
|
||||
samples += 1
|
||||
time.sleep(0.15)
|
||||
assert typing.wait() == 0 and samples >= 15, samples
|
||||
time.sleep(1.6)
|
||||
stopped = sample()
|
||||
assert stopped[1] == 0 and stopped[2] > 0 and stopped[3] > 0, stopped
|
||||
print(f"Private Xvfb typing: {samples} active samples; both rest meters stayed at 0; idle resumed after typing stopped")
|
||||
finally:
|
||||
for process in (typing, probe, other):
|
||||
if process is not None and process.poll() is None:
|
||||
process.terminate()
|
||||
process.wait(timeout=5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,31 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
bin=${1:-"$root/build/ziran-toolchain/bin"}
|
||||
work=$root/build/break-activity-x11-test
|
||||
mkdir -p "$work/c" "$work/runtime"
|
||||
kryon=$root/build/packages/kryon
|
||||
"$bin/zi2c" --no-main --define PLATFORM_DESKTOP --root "$root/tests" \
|
||||
--module-path "$kryon/src/backend" --module-path "$root/build/packages/ziran/std" \
|
||||
-o "$work/c" "$root/tests/break_activity_x11.zi"
|
||||
cat > "$work/c/main.c" <<'C'
|
||||
#include <stdio.h>
|
||||
#include "break_activity_x11.h"
|
||||
int main(void)
|
||||
{
|
||||
while (getchar() != EOF) {
|
||||
long long idle = Sample();
|
||||
printf("%lld %d %d %d\n", idle, Activity(), MicroIdle(), RestIdle());
|
||||
fflush(stdout);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
C
|
||||
"${CC:-cc}" -std=c11 -D_DEFAULT_SOURCE -I"$root/build/packages/ziran/include" \
|
||||
-I"$work/c" "$work/c"/*.c -ldl -o "$work/test"
|
||||
# Scrub the owner's session before creating a new private display and auth file.
|
||||
env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS \
|
||||
-u SESSION_MANAGER -u GDK_DISPLAY YUE_DESKTOP_RECOVERY=0 \
|
||||
TMPDIR="$work/runtime" xvfb-run -a -s '-screen 0 800x600x24 -nolisten tcp' \
|
||||
env XDG_SESSION_TYPE=x11 DBUS_SESSION_BUS_ADDRESS=invalid: \
|
||||
python3 "$root/tests/break_activity_x11_test.py" "$work/test"
|
||||
@@ -7,6 +7,7 @@
|
||||
#import, file "../src/breaks/break_types.zi";
|
||||
#import, file "../src/breaks/break_rules.zi";
|
||||
#import, file "../src/breaks/break_reminder_layout.zi";
|
||||
#import, file "../src/breaks/break_activity.zi";
|
||||
|
||||
c :: #system_library "c";
|
||||
write_out :: (descriptor: s32, data: *void, count: usize) -> s64 #foreign c "write";
|
||||
@@ -507,8 +508,47 @@ test_break_reminder_layout_separates_long_text :: () {
|
||||
"long reminder text keeps the progress bar on its own row")
|
||||
}
|
||||
|
||||
test_typing_is_activity :: () {
|
||||
e := BreakInit()
|
||||
input: BreakActivityInput
|
||||
input.idle_ms = -1
|
||||
input.focused = true
|
||||
last_input: s32 = -10000
|
||||
// Longer than both rest durations, with keys and no pointer movement.
|
||||
for frame: 0..60 * 700 - 1 {
|
||||
input.frame = cast(s32)frame
|
||||
input.key_pressed = frame % 15 == 0
|
||||
sample := BreakSampleActivity(input, last_input)
|
||||
last_input = sample.last_input_frame
|
||||
expect(sample.active == 1, "continuous keys count as activity")
|
||||
e = BreakTick(e, sample.active)
|
||||
}
|
||||
for timer: 0..1 {
|
||||
expect(e.timers[timer].idle_s == 0,
|
||||
"typing does not fill either rest meter")
|
||||
expect(e.stats[timer].natural == 0,
|
||||
"typing never completes a natural break")
|
||||
}
|
||||
input.key_pressed = false
|
||||
input.frame += BreakFallbackInputTicks
|
||||
expect(BreakSampleActivity(input, last_input).active == 0,
|
||||
"rest resumes after typing stops")
|
||||
input.pointer_active = true
|
||||
expect(BreakSampleActivity(input, last_input).active == 1,
|
||||
"pointer and keyboard activity use the same decision")
|
||||
input.pointer_active = false
|
||||
input.focused = false
|
||||
input.idle_ms = 20
|
||||
expect(BreakSampleActivity(input, last_input).active == 1,
|
||||
"session-wide typing counts while Breeze is unfocused")
|
||||
input.idle_ms = BreakActivityThresholdMs
|
||||
expect(BreakSampleActivity(input, last_input).active == 0,
|
||||
"session idle threshold starts rest")
|
||||
}
|
||||
|
||||
#program_export
|
||||
Check :: () -> s32 {
|
||||
test_typing_is_activity()
|
||||
test_defaults_match_workrave()
|
||||
test_active_time_prompts_at_limit()
|
||||
test_input_resets_idle_and_natural_take()
|
||||
|
||||
+11
-2
@@ -99,6 +99,15 @@
|
||||
"source": true,
|
||||
"url": "https://github.com/curl/curl.git"
|
||||
},
|
||||
{
|
||||
"commit": "81906dfd3dee7c4a6b5dd9d649e97022ba979899",
|
||||
"dependencies": {"libwebp": "p3b3c785a0e17e605", "raylib": "pcc7aa76725f2fb2f", "sdl3": "p16cd0434af3dff7b"},
|
||||
"dependency_refs": {"libwebp": "1844042d1ca751e62e61df7617dba19c98588339", "raylib": "fafa2cea97d0cb379d832086ba45a989fa6f9c8e", "sdl3": "829a65d769d935c4852f8159e964312c0957260a"},
|
||||
"id": "p80e1f6ee613f82d7",
|
||||
"name": "kryon",
|
||||
"ref": "81906dfd3dee7c4a6b5dd9d649e97022ba979899",
|
||||
"url": "https://github.com/kryonlabs/kryon.git"
|
||||
},
|
||||
{
|
||||
"commit": "51b7f2abdade71cd9bb0e7a373ef2610ec6f9daf",
|
||||
"dependencies": {},
|
||||
@@ -207,8 +216,8 @@
|
||||
}
|
||||
],
|
||||
"root": {
|
||||
"dependencies": {"apps": "pf7f4c26f3cf01fbe", "crypto": "pc23fbf455e97614d", "curl": "p739f5b821e102505", "daochi_client": "p6af0b5bd9fd0cd31", "entity": "p507e86a4cf18dc9c", "game2d": "p0e63706817d43aba", "kryon": "pe42819128210d99c", "kss": "pa3347b028712cc32", "liboqs": "p2567a9faecca698d", "mingcute": "pe1027b9d16704793", "monocypher": "p002f6ed23e3a3576", "oqs": "p7098ac9cd92f7e5f", "plot": "pab5377a2950368a2", "practices": "p948d09611e15b580", "qrcode": "p0dfa70137405b096", "rini": "p8ea355e8cb04093e", "sqlite": "pb6abbb780a670e17"},
|
||||
"dependency_refs": {"apps": "3515cdf3af20e79001f7774eed224e94180540ed", "crypto": "master", "curl": "master", "daochi_client": "master", "entity": "075784438af94fe4a0aba48ed4d8c7208c161bf1", "game2d": "8b5a989dea686250078da2d209fde1cd2946fc77", "kryon": "1cd873fda5d12cd4dd562956da94cee33c3e364c", "kss": "e3536e6d056961d2ba7fa5f18af81f75c80e8f62", "liboqs": "main", "mingcute": "master", "monocypher": "master", "oqs": "master", "plot": "master", "practices": "329ad781a2d396836344789ffc0bd4b90a831e25", "qrcode": "master", "rini": "main", "sqlite": "master"},
|
||||
"dependencies": {"apps": "pf7f4c26f3cf01fbe", "crypto": "pc23fbf455e97614d", "curl": "p739f5b821e102505", "daochi_client": "p6af0b5bd9fd0cd31", "entity": "p507e86a4cf18dc9c", "game2d": "p0e63706817d43aba", "kryon": "p80e1f6ee613f82d7", "kss": "pa3347b028712cc32", "liboqs": "p2567a9faecca698d", "mingcute": "pe1027b9d16704793", "monocypher": "p002f6ed23e3a3576", "oqs": "p7098ac9cd92f7e5f", "plot": "pab5377a2950368a2", "practices": "p948d09611e15b580", "qrcode": "p0dfa70137405b096", "rini": "p8ea355e8cb04093e", "sqlite": "pb6abbb780a670e17"},
|
||||
"dependency_refs": {"apps": "3515cdf3af20e79001f7774eed224e94180540ed", "crypto": "master", "curl": "master", "daochi_client": "master", "entity": "075784438af94fe4a0aba48ed4d8c7208c161bf1", "game2d": "8b5a989dea686250078da2d209fde1cd2946fc77", "kryon": "81906dfd3dee7c4a6b5dd9d649e97022ba979899", "kss": "e3536e6d056961d2ba7fa5f18af81f75c80e8f62", "liboqs": "main", "mingcute": "master", "monocypher": "master", "oqs": "master", "plot": "master", "practices": "329ad781a2d396836344789ffc0bd4b90a831e25", "qrcode": "master", "rini": "main", "sqlite": "master"},
|
||||
"name": "Inbe"
|
||||
},
|
||||
"toolchain": {
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ ref = "e4e8efa4a92f36e2fb0cea1d56b11471afa68621"
|
||||
|
||||
[dependencies.kryon]
|
||||
git = "https://github.com/kryonlabs/kryon.git"
|
||||
ref = "1cd873fda5d12cd4dd562956da94cee33c3e364c"
|
||||
ref = "81906dfd3dee7c4a6b5dd9d649e97022ba979899"
|
||||
|
||||
[dependencies.kss]
|
||||
git = "https://github.com/kryonlabs/kss.git"
|
||||
|
||||
Reference in new issue
Block a user