Skip to content

Commit

Permalink
Add time factor when replaying events
Browse files Browse the repository at this point in the history
  • Loading branch information
serenity4 committed Nov 8, 2024
1 parent 2e9a506 commit 8bc5224
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions src/testing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,23 @@ end
function save_history(wm::XWindowManager, queue::EventQueue{XWindowManager,XCBWindow})
events = Event{WindowRef}[]
windows = XCBWindow[]
previous_time = 0.0
for event in queue.history
i = findfirst(==(event.win), windows)
isnothing(i) && push!(windows, event.win)
winref = WindowRef(something(i, lastindex(windows)))
push!(events, Event(event.type, event.data, event.location, event.time, winref))
Δt = previous_time == 0 ? 0.0 : event.time - previous_time
previous_time = event.time
push!(events, Event(event.type, event.data, event.location, Δt, winref))
end
events
end

# FIXME: Events will be triggered multiple times if an event triggers another. How should we tackle that?
function replay_history(wm::XWindowManager, events::AbstractVector{Event{WindowRef}})
function replay_history(wm::XWindowManager, events::AbstractVector{Event{WindowRef}}; time_factor = 1.0)
windows = Dict{WindowRef,XCBWindow}()
all_windows = xcb_window_t[]
replay_time = nothing
replay_time = time()
for event in events
win = get!(windows, event.win) do
# Assume that window IDs will be ordered chronologically.
Expand All @@ -129,9 +132,9 @@ function replay_history(wm::XWindowManager, events::AbstractVector{Event{WindowR
i = event.win.number
wm.windows[all_windows[i]]
end
wait_for(event.time - something(replay_time, event.time))
replay_time = event.time
event = Event(event.type, event.data, event.location, time(), win)
Δt = event.time * time_factor
wait_for(Δt)
event = Event(event.type, event.data, event.location, replay_time + Δt, win)
send_event(wm, event)
end
end
Expand Down
3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function test(; replay_events = !interactive)
ctx = GraphicsContext(win, attributes=[XCB.XCB_GC_FOREGROUND, XCB.XCB_GC_GRAPHICS_EXPOSURES], values=[screen.black_pixel, 0])
attach_graphics_context!(win, ctx)
send = send_event(wm, win)
send = (_ -> sleep(0.01)) send
queue = EventQueue(wm; record_history = true)

if interactive
Expand Down Expand Up @@ -113,7 +114,7 @@ function test(; replay_events = !interactive)
ctx = GraphicsContext(win, attributes=[XCB.XCB_GC_FOREGROUND, XCB.XCB_GC_GRAPHICS_EXPOSURES], values=[screen.black_pixel, 0])
attach_graphics_context!(win, ctx)
task = @async main(wm)
replay_history(wm, events)
replay_history(wm, events; time_factor = 0.1)
wait(task)
@test !istaskfailed(task)
isfile("keymap.c") && rm("keymap.c")
Expand Down

0 comments on commit 8bc5224

Please sign in to comment.