diff --git a/data.go b/data.go index 6131bca..9c0d550 100644 --- a/data.go +++ b/data.go @@ -92,6 +92,10 @@ type Window struct { // ID is the numerical identifier of the window. ID int64 + // Desktop is the numerical identifier of the desktop the + // window belongs to. Equal to -1 if the window is sticky. + Desktop int64 + // Name is the display name of the window (typically what the // windowing system shows in the top bar of the window). Name string @@ -118,6 +122,18 @@ func (w *Window) IsSystem() bool { return false } +// IsSticky returns true if the window is a sticky window (i.e. +// present on all desktops) +func (w *Window) IsSticky() bool { + return w.Desktop == -1 +} + +// IsOnDesktop returns true if the window is present on the specified +// desktop +func (w *Window) IsOnDesktop(desktop int64) bool { + return w.IsSticky() || w.Desktop == desktop +} + // Info returns more structured metadata about a window. The metadata // is extracted using heuristics. func (w *Window) Info() *Winfo { diff --git a/linux.go b/linux.go index f02ced7..dbe3a3c 100644 --- a/linux.go +++ b/linux.go @@ -61,18 +61,45 @@ func (t *LinuxTracker) Snap() (*Snapshot, error) { if len(fields) < 4 { continue } - id_, name := fields[0], strings.Join(fields[3:], " ") + id_, desktop_, name := fields[0], fields[1], strings.Join(fields[3:], " ") id, err := strconv.ParseInt(id_, 0, 64) if err != nil { return nil, err } - w := Window{ID: id, Name: name} + 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 { @@ -97,7 +124,7 @@ func (t *LinuxTracker) Snap() (*Snapshot, error) { if err != nil { return nil, err } - if isVisible(x, y, w, h, viewHeight, viewWidth) { + if window.IsOnDesktop(currentDesktop) && isVisible(x, y, w, h, viewHeight, viewWidth) { visible = append(visible, window.ID) } }