From ab48890aa26faf5a8b61ba2a3a72a9bd3f4c3f52 Mon Sep 17 00:00:00 2001 From: Sam Stokes Date: Tue, 16 Aug 2016 18:24:00 -0700 Subject: [PATCH] Only report windows on current desktop as "visible"... Prior to this change, on my Linux machine (XUbuntu + XMonad), Thyme would report all open windows as visible, even windows on other virtual desktops. Reporting those windows as "visible" doesn't match the colloquial definition, and also seems to go against the intended use case. This checks the current desktop via `wmctrl -d` and then filters out windows on other desktops from the visible list. --- data.go | 16 ++++++++++++++++ linux.go | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) 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) } }