fix(evidence): X11 sensor polls so tab/title changes are tracked
The X11 source only re-read the active window on _NET_ACTIVE_WINDOW changes, so switching a browser tab — which changes the window title but not the active window — was invisible. All the time on the new tab was credited to the stale title (e.g. reading a Consume article showed up as time on the Keel tab). Mirror the Windows sensor: poll the active window every 750ms and emit only when the window or its title changes. A shared, display-free pollLoop carries the change-dedup and is unit-tested for the exact regression (a title-only change must emit). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+15
-37
@@ -4,24 +4,29 @@ package evidence
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jezek/xgb/xproto"
|
||||
"github.com/jezek/xgbutil"
|
||||
"github.com/jezek/xgbutil/ewmh"
|
||||
"github.com/jezek/xgbutil/icccm"
|
||||
"github.com/jezek/xgbutil/xevent"
|
||||
"github.com/jezek/xgbutil/xprop"
|
||||
"github.com/jezek/xgbutil/xwindow"
|
||||
)
|
||||
|
||||
// pollInterval is how often the X11 sensor samples the active window. ~1s
|
||||
// latency on a switch is immaterial for a focus tracker, and polling is what
|
||||
// catches a title change inside the same window — e.g. switching a browser tab,
|
||||
// which changes the title but not the active window, so an _NET_ACTIVE_WINDOW
|
||||
// event subscription would miss it and credit the stale title. Mirrors the
|
||||
// Windows sensor's polling cadence.
|
||||
const pollInterval = 750 * time.Millisecond
|
||||
|
||||
// NewSource returns the real X11 active-window sensor.
|
||||
func NewSource() Source { return &x11Source{} }
|
||||
|
||||
type x11Source struct{}
|
||||
|
||||
// Watch opens one long-lived X connection, subscribes to _NET_ACTIVE_WINDOW
|
||||
// changes on the root window, and emits a snapshot immediately plus on every
|
||||
// change. Any failure degrades to an Unavailable snapshot; it never panics the
|
||||
// Watch opens one long-lived X connection and samples the active window every
|
||||
// pollInterval, emitting a snapshot whenever the active window OR its title
|
||||
// changes. Any failure degrades to an Unavailable snapshot; it never panics the
|
||||
// daemon.
|
||||
func (s *x11Source) Watch(ctx context.Context, onChange func(WindowSnapshot)) {
|
||||
X, err := xgbutil.NewConn()
|
||||
@@ -32,38 +37,11 @@ func (s *x11Source) Watch(ctx context.Context, onChange func(WindowSnapshot)) {
|
||||
}
|
||||
defer X.Conn().Close()
|
||||
|
||||
root := X.RootWin()
|
||||
activeAtom, err := xprop.Atm(X, "_NET_ACTIVE_WINDOW")
|
||||
if err != nil {
|
||||
onChange(unavailable("no _NET_ACTIVE_WINDOW atom: " + err.Error()))
|
||||
<-ctx.Done()
|
||||
return
|
||||
}
|
||||
|
||||
// Listen for property changes on the root window.
|
||||
if err := xwindow.New(X, root).Listen(xproto.EventMaskPropertyChange); err != nil {
|
||||
onChange(unavailable("cannot listen on root window: " + err.Error()))
|
||||
<-ctx.Done()
|
||||
return
|
||||
}
|
||||
|
||||
emit := func() { onChange(snapshot(X)) }
|
||||
emit() // immediate current window
|
||||
|
||||
xevent.PropertyNotifyFun(func(_ *xgbutil.XUtil, ev xevent.PropertyNotifyEvent) {
|
||||
if ev.Atom == activeAtom {
|
||||
emit()
|
||||
}
|
||||
}).Connect(X, root)
|
||||
|
||||
// Run the event loop until ctx is cancelled.
|
||||
go func() {
|
||||
<-ctx.Done()
|
||||
xevent.Quit(X)
|
||||
}()
|
||||
xevent.Main(X)
|
||||
pollLoop(ctx, pollInterval, func() WindowSnapshot { return snapshot(X) }, onChange)
|
||||
}
|
||||
|
||||
// snapshot reads the current active window's title and class. A missing active
|
||||
// window (or read error) yields an Unavailable snapshot.
|
||||
func snapshot(X *xgbutil.XUtil) WindowSnapshot {
|
||||
active, err := ewmh.ActiveWindowGet(X)
|
||||
if err != nil || active == 0 {
|
||||
|
||||
Reference in New Issue
Block a user