...
1 package xorg
2
3 import (
4 "context"
5 "fmt"
6 "time"
7
8 "github.com/jezek/xgb"
9 "k8s.io/apimachinery/pkg/util/wait"
10
11 "edge-infra.dev/pkg/sds/display/displaymanager/waiter"
12 "edge-infra.dev/pkg/sds/lib/xorg/xrandr"
13 )
14
15 const tick = time.Second
16
17
18
19
20
21 func NewXorgDisplayWaiter(r xrandr.Xrandr, minWaitTime, timeout time.Duration) waiter.DisplayWaiter {
22 return &displayWaiter{
23 Xrandr: r,
24 minWaitTime: minWaitTime,
25 timeout: timeout,
26 }
27 }
28
29 type displayWaiter struct {
30 xrandr.Xrandr
31
32 minWaitTime time.Duration
33 timeout time.Duration
34 }
35
36 func (x *displayWaiter) Wait(ctx context.Context) error {
37
38 if err := waitForXgbConnection(ctx, x.timeout); err != nil {
39 return fmt.Errorf("unable to connect to X: %w", err)
40 }
41
42 return x.Xrandr.WaitUntilReady(x.minWaitTime, x.timeout)
43 }
44
45 type ConditionWithContextFunc func(context.Context) (done bool, err error)
46
47 func waitForXgbConnection(ctx context.Context, timeout time.Duration) error {
48 return wait.PollUntilContextTimeout(ctx, tick, timeout, true, func(_ context.Context) (bool, error) {
49 _, err := xgb.NewConn()
50 return err == nil, err
51 })
52 }
53
View as plain text