1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package autotrack
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)
// Sample is one OS observation handed to the segmentation loop.
type Sample struct {
Idle int // HID idle seconds
Raw *Ctx // foreground context; nil when no foreground app
Locked bool // screen locked (hard, immediate away signal)
}
// parseHIDIdle extracts HIDIdleTime (nanoseconds) from `ioreg -c IOHIDSystem`
// output and returns whole seconds. 0 when absent or malformed.
func parseHIDIdle(out string) int {
for _, line := range strings.Split(out, "\n") {
if !strings.Contains(line, "HIDIdleTime") {
continue
}
i := strings.LastIndex(line, "=")
if i < 0 {
continue
}
ns, err := strconv.ParseInt(strings.TrimSpace(line[i+1:]), 10, 64)
if err != nil {
continue
}
return int(ns / 1_000_000_000)
}
return 0
}
// parseLocked reports whether ioreg output shows the lock flag. The
// CGSSessionScreenIsLocked key is present only while the screen is locked.
func parseLocked(out string) bool {
return strings.Contains(strings.ReplaceAll(out, " ", ""), `"CGSSessionScreenIsLocked"=Yes`)
}
// parseWinctx decodes the winctx helper's {"app","title"} JSON. Empty on error.
func parseWinctx(b []byte) (app, title string) {
var v struct {
App string `json:"app"`
Title string `json:"title"`
}
if err := json.Unmarshal(b, &v); err != nil {
return "", ""
}
return v.App, v.Title
}
// HelperPath finds a bundled native helper (winctx, cal-events): first next
// to the symlink-resolved tt binary (make install symlinks ~/.local/bin/tt →
// <repo>/bin/tt, and the helpers build into <repo>/bin), then on $PATH.
// "" when not found — callers degrade gracefully.
func HelperPath(name string) string {
if exe, err := os.Executable(); err == nil {
if resolved, rerr := filepath.EvalSymlinks(exe); rerr == nil {
p := filepath.Join(filepath.Dir(resolved), name)
if st, serr := os.Stat(p); serr == nil && !st.IsDir() {
return p
}
}
}
if p, err := exec.LookPath(name); err == nil {
return p
}
return ""
}
|