This repository was archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathlinux.go
More file actions
190 lines (172 loc) · 4.92 KB
/
linux.go
File metadata and controls
190 lines (172 loc) · 4.92 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package thyme
import (
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
)
func init() {
RegisterTracker("linux", NewLinuxTracker)
}
// LinuxTracker tracks application usage on Linux via a few standard command-line utilities.
type LinuxTracker struct{}
var _ Tracker = (*LinuxTracker)(nil)
func NewLinuxTracker() Tracker {
return &LinuxTracker{}
}
func (t *LinuxTracker) Deps() string {
return `
Install the following command-line utilities via your package manager of choice:
* xdpyinfo
* xwininfo
* xdotool
* wmctrl
For example:
* Debian: apt-get install x11-utils xdotool wmctrl
Note: this command prints out this message regardless of whether the dependencies are already installed.
`
}
func (t *LinuxTracker) Snap() (*Snapshot, error) {
var viewWidth, viewHeight int
{
out, err := exec.Command("bash", "-c", "xdpyinfo | grep dimensions").Output()
if err != nil {
return nil, fmt.Errorf("xdpyinfo failed with error: %s. Try running `xdpyinfo | grep dimensions` to diagnose.", err)
}
matches := dimRx.FindStringSubmatch(string(out))
if len(matches) != 3 {
return nil, fmt.Errorf("could not parse viewport dimensions from output line %q", string(out))
}
w, err := strconv.Atoi(matches[1])
if err != nil {
return nil, err
}
h, err := strconv.Atoi(matches[2])
if err != nil {
return nil, err
}
viewWidth, viewHeight = w, h
}
var windows []*Window
{
out, err := exec.Command("wmctrl", "-l").Output()
if err != nil {
return nil, fmt.Errorf("wmctrl failed with error: %s. Try running `wmctrl -l` to diagnose.", err)
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 4 {
continue
}
id_, desktop_, name := fields[0], fields[1], strings.Join(fields[3:], " ")
id, err := strconv.ParseInt(id_, 0, 64)
if err != nil {
return nil, err
}
desktop, err := strconv.ParseInt(desktop_, 0, 64)
if err != nil {
return nil, err
}
w := Window{ID: id, Desktop: desktop, Name: name}
if !w.IsSystem() {
windows = append(windows, &w)
}
}
}
var currentDesktop int64
{
out, err := exec.Command("wmctrl", "-d").Output()
if err != nil {
return nil, err
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
id_, mode := fields[0], fields[1]
id, err := strconv.ParseInt(id_, 0, 64)
if err != nil {
return nil, err
}
if "*" == mode {
currentDesktop = id
}
}
}
var visible []int64
{
for _, window := range windows {
out_, err := exec.Command("xwininfo", "-id", fmt.Sprintf("%d", window.ID), "-stats").Output()
if err != nil {
return nil, fmt.Errorf("xwininfo failed with error: %s", err)
}
out := string(out_)
x, err := parseWinDim(xRx, out, "X")
if err != nil {
return nil, err
}
y, err := parseWinDim(yRx, out, "Y")
if err != nil {
return nil, err
}
w, err := parseWinDim(wRx, out, "W")
if err != nil {
return nil, err
}
h, err := parseWinDim(hRx, out, "H")
if err != nil {
return nil, err
}
if window.IsOnDesktop(currentDesktop) && isVisible(x, y, w, h, viewHeight, viewWidth) {
visible = append(visible, window.ID)
}
}
}
var active int64
{
out, err := exec.Command("xdotool", "getactivewindow").Output()
if err != nil {
return nil, fmt.Errorf("xdotool failed with error: %s. Try running `xdotool getactivewindow` to diagnose.", err)
}
id, err := strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64)
if err != nil {
return nil, err
}
active = id
}
return &Snapshot{Windows: windows, Active: active, Visible: visible, Time: time.Now()}, nil
}
// isVisible checks if the window is visible in the current viewport.
// x and y are assumed to be relative to the current viewport (i.e.,
// (0, 0) is the coordinate of the top-left corner of the current
// viewport.
func isVisible(x, y, w, h, viewHeight, viewWidth int) bool {
return (0 <= x && x < viewWidth && 0 <= y && y < viewHeight) ||
(0 <= x+w && x+w < viewWidth && 0 <= y && y < viewHeight) ||
(0 <= x && x < viewWidth && 0 <= y+h && y+h < viewHeight) ||
(0 <= x+w && x+w < viewWidth && 0 <= y+h && y+h < viewHeight)
}
var (
dimRx = regexp.MustCompile(`dimensions:\s+([0-9]+)x([0-9]+)\s+pixels`)
xRx = regexp.MustCompile(`Absolute upper\-left X:\s+(\-?[0-9]+)`)
yRx = regexp.MustCompile(`Absolute upper\-left Y:\s+(\-?[0-9]+)`)
wRx = regexp.MustCompile(`Width:\s+([0-9]+)`)
hRx = regexp.MustCompile(`Height:\s+([0-9]+)`)
)
// parseWinDim parses window dimension info from the output of `xwininfo`
func parseWinDim(rx *regexp.Regexp, out string, varname string) (int, error) {
if matches := rx.FindStringSubmatch(out); len(matches) == 2 {
n, err := strconv.Atoi(matches[1])
if err != nil {
return 0, err
}
return n, nil
} else {
return 0, fmt.Errorf("could not parse window %s from output %s", varname, out)
}
}