Files
adamfandClaude Opus 5.5 f4ce04875c
Test and publish / test (push) Successful in 2m38s
Test and publish / publish (push) Failing after 2m30s
Add activity tracker server, Hyprland agent and Gitea image publishing
The Go API on PostgreSQL ingests desktop, browser, presence, manual and
Health Auto Export events and serves activities and daily goal summaries.
The Hyprland agent records window focus, idle and lock intervals.

The CI workflow publishes adam-french.co.uk/adamf/activity-tracker to the
Gitea registry and rolls the web_server stack onto it. The image probes
itself through a new `tracker healthcheck` command, because distroless
has no wget.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 20:38:12 +01:00

143 lines
3.4 KiB
Go

// Package hypr implements the Hyprland desktop collector: it follows window
// focus over Hyprland's IPC sockets, turns it into intervals, buffers them on
// disk and uploads them to the tracker server.
package hypr
import (
"bufio"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net"
"os"
"path/filepath"
"strings"
"time"
)
// Focus is the currently focused window. An empty Class means nothing is
// focused (e.g. an empty workspace).
type Focus struct {
Class string
Title string
}
// FocusEvent is a focus change stamped with the time it was observed.
type FocusEvent struct {
Focus
At time.Time
}
func socketDir() (string, error) {
sig := os.Getenv("HYPRLAND_INSTANCE_SIGNATURE")
if sig == "" {
return "", errors.New("HYPRLAND_INSTANCE_SIGNATURE not set; is Hyprland running?")
}
return filepath.Join(os.Getenv("XDG_RUNTIME_DIR"), "hypr", sig), nil
}
// ParseLine splits a socket2 line of the form "event>>data".
func ParseLine(line string) (event, data string, ok bool) {
return strings.Cut(line, ">>")
}
// ParseActiveWindow parses the data of an "activewindow" event, "class,title".
// Titles may contain commas, so only the first one is a separator.
func ParseActiveWindow(data string) Focus {
class, title, _ := strings.Cut(data, ",")
return Focus{Class: class, Title: title}
}
// ActiveWindow asks Hyprland (via the request socket) for the focused window.
func ActiveWindow() (Focus, error) {
dir, err := socketDir()
if err != nil {
return Focus{}, err
}
conn, err := net.DialTimeout("unix", filepath.Join(dir, ".socket.sock"), 2*time.Second)
if err != nil {
return Focus{}, err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(2 * time.Second))
if _, err := conn.Write([]byte("j/activewindow")); err != nil {
return Focus{}, err
}
body, err := io.ReadAll(conn)
if err != nil {
return Focus{}, err
}
var w struct {
Class string `json:"class"`
Title string `json:"title"`
}
if err := json.Unmarshal(body, &w); err != nil {
return Focus{}, fmt.Errorf("decode activewindow: %w", err)
}
return Focus{Class: w.Class, Title: w.Title}, nil
}
// Listen streams focus changes to out until ctx is cancelled, reconnecting to
// the event socket if the connection drops.
func Listen(ctx context.Context, out chan<- FocusEvent, log *slog.Logger) error {
dir, err := socketDir()
if err != nil {
return err
}
path := filepath.Join(dir, ".socket2.sock")
for {
err := listenOnce(ctx, path, out)
if ctx.Err() != nil {
return nil
}
log.Warn("hyprland event socket disconnected; retrying", "err", err)
select {
case <-ctx.Done():
return nil
case <-time.After(2 * time.Second):
}
}
}
func listenOnce(ctx context.Context, path string, out chan<- FocusEvent) error {
var d net.Dialer
conn, err := d.DialContext(ctx, "unix", path)
if err != nil {
return err
}
defer conn.Close()
go func() {
<-ctx.Done()
conn.Close()
}()
send := func(f Focus) {
select {
case out <- FocusEvent{Focus: f, At: time.Now()}:
case <-ctx.Done():
}
}
// Seed with the current window so time before the first switch is counted.
if f, err := ActiveWindow(); err == nil {
send(f)
}
sc := bufio.NewScanner(conn)
sc.Buffer(make([]byte, 64*1024), 1024*1024)
for sc.Scan() {
event, data, ok := ParseLine(sc.Text())
if ok && event == "activewindow" {
send(ParseActiveWindow(data))
}
}
if err := sc.Err(); err != nil {
return err
}
return io.EOF
}