mirror of
https://codeberg.org/scip/swaycycle.git
synced 2025-12-16 20:11:02 +01:00
Compare commits
17 Commits
v0.1.3
...
dependabot
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0755d20673 | ||
|
|
85a1e6530c | ||
| feba0f3580 | |||
| 2fbb3ebc59 | |||
|
|
8e48b42bad | ||
|
|
7a5657b778 | ||
| 0bdef90f97 | |||
| 3402df69b4 | |||
| b21d8ebed9 | |||
|
|
a481cc7172 | ||
|
|
cb8421e6f6 | ||
| 051b68c266 | |||
| bde1301e2c | |||
| 977c374197 | |||
| 4741481527 | |||
| dacdc5c214 | |||
| 1bc1b2a963 |
BIN
.github/assets/screenshot.png
vendored
BIN
.github/assets/screenshot.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 76 KiB |
4
.github/workflows/release.yaml
vendored
4
.github/workflows/release.yaml
vendored
@@ -10,10 +10,10 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v6
|
||||||
with:
|
with:
|
||||||
go-version: 1.23.5
|
go-version: 1.23.5
|
||||||
|
|
||||||
|
|||||||
46
README.md
46
README.md
@@ -5,12 +5,16 @@ floating ones or windows in sub-containers. So it simulates the
|
|||||||
behavior of other window managers and desktop environments. Just bind
|
behavior of other window managers and desktop environments. Just bind
|
||||||
the tool to `ALT-tab` and there you go.
|
the tool to `ALT-tab` and there you go.
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Checkout the repo and execute `make`. You'll need the go toolkit. Then
|
Download the binary for your architecture from the [release
|
||||||
copy the binary `swaycycle` to some location within your `$PATH`.
|
page](https://github.com/TLINDEN/swaycycle/releases) and copy to
|
||||||
|
some location within your `$PATH`.
|
||||||
|
|
||||||
|
To build the tool from source, checkout the repo and execute
|
||||||
|
`make`. You'll need the go toolkit. Then copy the binary `swaycycle`
|
||||||
|
to some location within your `$PATH`.
|
||||||
|
|
||||||
## Configuration
|
## Configuration
|
||||||
|
|
||||||
@@ -20,6 +24,13 @@ Add such a line to your sway config file (e.g. in `$HOME/.config/sway/config`):
|
|||||||
bindsym $mod+Tab exec ~/bin/swaycycle
|
bindsym $mod+Tab exec ~/bin/swaycycle
|
||||||
```
|
```
|
||||||
|
|
||||||
|
You may also add a second key binding to do the reverse, which is
|
||||||
|
sometimes very useful:
|
||||||
|
|
||||||
|
```default
|
||||||
|
bindsym $mod+Shift+Tab exec ~/bin/swaycycle --prev
|
||||||
|
```
|
||||||
|
|
||||||
## Debugging
|
## Debugging
|
||||||
|
|
||||||
You may call `swaycycle` in a terminal window on a workspace with at
|
You may call `swaycycle` in a terminal window on a workspace with at
|
||||||
@@ -35,6 +46,29 @@ It's also possible to debug an instance executed by sway using the
|
|||||||
bindsym $mod+Tab exec ~/bin/swaycycle -d -l /tmp/cycle.log
|
bindsym $mod+Tab exec ~/bin/swaycycle -d -l /tmp/cycle.log
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## How does it work?
|
||||||
|
|
||||||
|
`swaycycle` is being executed by sway when the user presses a key
|
||||||
|
(e.g. `ALT-tab`). It then connects to the running sway instance via
|
||||||
|
the provided IPC unix domain socket as available in the environment
|
||||||
|
variable `SWAYSOCK`. Via that connection it sends the `GET_TREE`
|
||||||
|
command and processes the retrieved JSON response. This JSON tree
|
||||||
|
contains all information about the running instance such as outputs,
|
||||||
|
workspaces and containers.
|
||||||
|
|
||||||
|
Then it determines which workspace is the current active one and
|
||||||
|
builds a list of all windows visible on that workspace, whether
|
||||||
|
floating or not.
|
||||||
|
|
||||||
|
Next it determines which window is following the one in the list with
|
||||||
|
the current active focus. If the active one is at the end of the list,
|
||||||
|
it starts from the top.
|
||||||
|
|
||||||
|
Finally `swaycycle` sends the propper switch focus command via the IPC
|
||||||
|
connection to sway, e.g.:
|
||||||
|
|
||||||
|
`[con_id=14] focus`
|
||||||
|
|
||||||
## Getting help
|
## Getting help
|
||||||
|
|
||||||
Although I'm happy to hear from swaycycle users in private email, that's the
|
Although I'm happy to hear from swaycycle users in private email, that's the
|
||||||
@@ -44,6 +78,12 @@ In order to report a bug, unexpected behavior, feature requests or to
|
|||||||
submit a patch, please open an issue on github:
|
submit a patch, please open an issue on github:
|
||||||
https://github.com/tlinden/swaycycle/issues.
|
https://github.com/tlinden/swaycycle/issues.
|
||||||
|
|
||||||
|
## See also
|
||||||
|
|
||||||
|
- [sway-ipc(7)](https://www.mankier.com/7/sway-ipc)
|
||||||
|
- [swaywm](https://github.com/swaywm/sway/)
|
||||||
|
- [swayfx](https://github.com/WillPower3309/swayfx)
|
||||||
|
|
||||||
## Copyright and license
|
## Copyright and license
|
||||||
|
|
||||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
||||||
|
|||||||
1
go.mod
1
go.mod
@@ -9,6 +9,7 @@ require (
|
|||||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/spf13/pflag v1.0.7 // indirect
|
github.com/spf13/pflag v1.0.7 // indirect
|
||||||
|
github.com/tlinden/i3ipc v0.0.0-20250815101608-4f7e27528be3 // indirect
|
||||||
github.com/tlinden/yadu v0.1.3 // indirect
|
github.com/tlinden/yadu v0.1.3 // indirect
|
||||||
golang.org/x/sys v0.14.0 // indirect
|
golang.org/x/sys v0.14.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -11,6 +11,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
|
|||||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||||
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
|
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
|
||||||
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/tlinden/i3ipc v0.0.0-20250815101608-4f7e27528be3 h1:/kIZO4852sAVemXtqnsBid0r4Q1h87jDwHa8f7v1h5I=
|
||||||
|
github.com/tlinden/i3ipc v0.0.0-20250815101608-4f7e27528be3/go.mod h1:mc0toDHmgqgX6FpE69U5yMPnHuLTdekHRslSLDp8xSE=
|
||||||
github.com/tlinden/yadu v0.1.3 h1:5cRCUmj+l5yvlM2irtpFBIJwVV2DPEgYSaWvF19FtcY=
|
github.com/tlinden/yadu v0.1.3 h1:5cRCUmj+l5yvlM2irtpFBIJwVV2DPEgYSaWvF19FtcY=
|
||||||
github.com/tlinden/yadu v0.1.3/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA=
|
github.com/tlinden/yadu v0.1.3/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA=
|
||||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
|||||||
329
main.go
329
main.go
@@ -18,35 +18,22 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"sort"
|
||||||
|
|
||||||
"github.com/lmittmann/tint"
|
"github.com/lmittmann/tint"
|
||||||
"github.com/mattn/go-isatty"
|
"github.com/mattn/go-isatty"
|
||||||
|
"github.com/tlinden/i3ipc"
|
||||||
"github.com/tlinden/yadu"
|
"github.com/tlinden/yadu"
|
||||||
|
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Node struct {
|
|
||||||
Id int `json:"id"`
|
|
||||||
Nodetype string `json:"type"` // output, workspace or container
|
|
||||||
Name string `json:"name"` // workspace number or app name
|
|
||||||
Nodes []Node `json:"nodes"`
|
|
||||||
FloatingNodes []Node `json:"floating_nodes"`
|
|
||||||
Focused bool `json:"focused"`
|
|
||||||
Window int `json:"window"` // wayland native
|
|
||||||
X11Window string `json:"app_id"` // x11 compat
|
|
||||||
Current_workspace string `json:"current_workspace"`
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
root = iota + 1
|
root = iota + 1
|
||||||
output
|
output
|
||||||
@@ -56,14 +43,23 @@ const (
|
|||||||
|
|
||||||
LevelNotice = slog.Level(2)
|
LevelNotice = slog.Level(2)
|
||||||
|
|
||||||
VERSION = "v0.1.3"
|
VERSION = "v0.3.1"
|
||||||
|
|
||||||
|
IPC_HEADER_SIZE = 14
|
||||||
|
IPC_MAGIC = "i3-ipc"
|
||||||
|
|
||||||
|
// message types
|
||||||
|
IPC_GET_TREE = 4
|
||||||
|
IPC_RUN_COMMAND = 0
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Visibles = []Node{}
|
Visibles = []*i3ipc.Node{}
|
||||||
CurrentWorkspace = ""
|
CurrentWorkspace = ""
|
||||||
|
Previous = false
|
||||||
Debug = false
|
Debug = false
|
||||||
Dumptree = false
|
Dumptree = false
|
||||||
|
Dumpvisibles = false
|
||||||
Version = false
|
Version = false
|
||||||
Verbose = false
|
Verbose = false
|
||||||
Notswitch = false
|
Notswitch = false
|
||||||
@@ -73,26 +69,28 @@ var (
|
|||||||
|
|
||||||
const Usage string = `This is swaycycle - cycle focus through all visible windows on a sway workspace.
|
const Usage string = `This is swaycycle - cycle focus through all visible windows on a sway workspace.
|
||||||
|
|
||||||
Usage: swaycycle [-vVdDn] [-l <log>]
|
Usage: swaycycle [-vdDn] [-l <log>]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-p, --prev cycle backward
|
||||||
-n, --no-switch do not switch windows
|
-n, --no-switch do not switch windows
|
||||||
-d, --debug enable debugging
|
-d, --debug enable debugging
|
||||||
-D, --dump dump the sway tree (needs -d as well)
|
-D, --dump dump the sway tree (needs -d as well)
|
||||||
|
--dump-visibles dump a list of visible windows on current workspace (needs -d)
|
||||||
-l, --logfile string write output to logfile
|
-l, --logfile string write output to logfile
|
||||||
-v, --verbose enable verbose logging
|
-v, --version show program version
|
||||||
-V, --version show program version
|
|
||||||
|
|
||||||
Copyleft (L) 2025 Thomas von Dein.
|
Copyleft (L) 2025 Thomas von Dein.
|
||||||
Licensed under the terms of the GNU GPL version 3.
|
Licensed under the terms of the GNU GPL version 3.
|
||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
flag.BoolVarP(&Previous, "prev", "p", false, "cycle backward")
|
||||||
flag.BoolVarP(&Debug, "debug", "d", false, "enable debugging")
|
flag.BoolVarP(&Debug, "debug", "d", false, "enable debugging")
|
||||||
flag.BoolVarP(&Dumptree, "dump", "D", false, "dump the sway tree (needs -d as well)")
|
flag.BoolVarP(&Dumptree, "dump", "D", false, "dump the sway tree (needs -d as well)")
|
||||||
flag.BoolVarP(&Verbose, "verbose", "v", false, "enable verbose logging")
|
flag.BoolVarP(&Dumpvisibles, "dump-visibles", "", false, "dump a list of visible windows on current workspace (needs -d)")
|
||||||
flag.BoolVarP(&Notswitch, "no-switch", "n", false, "do not switch windows")
|
flag.BoolVarP(&Notswitch, "no-switch", "n", false, "do not switch windows")
|
||||||
flag.BoolVarP(&Version, "version", "V", false, "show program version")
|
flag.BoolVarP(&Version, "version", "v", false, "show program version")
|
||||||
flag.BoolVarP(&Showhelp, "help", "h", Showhelp, "show help")
|
flag.BoolVarP(&Showhelp, "help", "h", Showhelp, "show help")
|
||||||
|
|
||||||
flag.StringVarP(&Logfile, "logfile", "l", "", "write output to logfile")
|
flag.StringVarP(&Logfile, "logfile", "l", "", "write output to logfile")
|
||||||
@@ -120,60 +118,68 @@ func main() {
|
|||||||
setupLogging(os.Stdout)
|
setupLogging(os.Stdout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fills Visibles node list
|
// connect to sway unix socket
|
||||||
fetchSwayTree()
|
ipc := i3ipc.NewI3ipc()
|
||||||
|
|
||||||
|
err := ipc.Connect()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer ipc.Close()
|
||||||
|
|
||||||
|
sway, err := ipc.GetTree()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// traverse the tree and find visible windows
|
||||||
|
if err := processJSON(sway); err != nil {
|
||||||
|
log.Fatalf("%s", err)
|
||||||
|
}
|
||||||
|
|
||||||
if len(Visibles) == 0 {
|
if len(Visibles) == 0 {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
id := findNextWindow()
|
id := 0
|
||||||
|
if Previous {
|
||||||
|
id = findPrevWindow()
|
||||||
|
slog.Debug("findPrevWindow", "nextid", id)
|
||||||
|
} else {
|
||||||
|
id = findNextWindow()
|
||||||
|
slog.Debug("findNextWindow", "nextid", id)
|
||||||
|
}
|
||||||
|
|
||||||
if id > 0 && !Notswitch {
|
if id > 0 && !Notswitch {
|
||||||
switchFocus(id)
|
switchFocus(id, ipc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogging(output io.Writer) {
|
// get into the sway tree, determine current workspace and extract all
|
||||||
logLevel := &slog.LevelVar{}
|
// its visible windows, store them in the global var Visibles
|
||||||
|
func processJSON(sway *i3ipc.Node) error {
|
||||||
if !Debug {
|
if !istype(sway, root) && len(sway.Nodes) == 0 {
|
||||||
// default logging
|
return fmt.Errorf("Invalid or empty JSON structure")
|
||||||
opts := &tint.Options{
|
|
||||||
Level: logLevel,
|
|
||||||
AddSource: false,
|
|
||||||
NoColor: IsNoTty(),
|
|
||||||
}
|
|
||||||
|
|
||||||
if Verbose {
|
|
||||||
logLevel.Set(slog.LevelInfo)
|
|
||||||
} else {
|
|
||||||
logLevel.Set(LevelNotice)
|
|
||||||
}
|
|
||||||
|
|
||||||
handler := tint.NewHandler(output, opts)
|
|
||||||
logger := slog.New(handler)
|
|
||||||
|
|
||||||
slog.SetDefault(logger)
|
|
||||||
} else {
|
|
||||||
// we're using a more verbose logger in debug mode
|
|
||||||
buildInfo, _ := debug.ReadBuildInfo()
|
|
||||||
opts := &yadu.Options{
|
|
||||||
Level: logLevel,
|
|
||||||
AddSource: true,
|
|
||||||
}
|
|
||||||
|
|
||||||
logLevel.Set(slog.LevelDebug)
|
|
||||||
|
|
||||||
handler := yadu.NewHandler(output, opts)
|
|
||||||
debuglogger := slog.New(handler).With(
|
|
||||||
slog.Group("program_info",
|
|
||||||
slog.Int("pid", os.Getpid()),
|
|
||||||
slog.String("go_version", buildInfo.GoVersion),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
slog.SetDefault(debuglogger)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if Dumptree {
|
||||||
|
slog.Debug("processed sway tree", "sway", sway)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, node := range sway.Nodes {
|
||||||
|
if node.Current_workspace != "" {
|
||||||
|
// this is an output node containing the current workspace
|
||||||
|
CurrentWorkspace = node.Current_workspace
|
||||||
|
recurseNodes(node.Nodes)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if Dumpvisibles {
|
||||||
|
dumpVisibles()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the next window after the one with current focus. if the last
|
// find the next window after the one with current focus. if the last
|
||||||
@@ -203,114 +209,51 @@ func findNextWindow() int {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// actually switch focus using a swaymsg command
|
func findPrevWindow() int {
|
||||||
func switchFocus(id int) {
|
vislen := len(Visibles)
|
||||||
var cmd *exec.Cmd
|
if vislen == 0 {
|
||||||
arg := fmt.Sprintf("[con_id=%d]", id)
|
return 0
|
||||||
|
|
||||||
slog.Debug("executing", "command", "swaymsg "+arg+" focus")
|
|
||||||
|
|
||||||
cmd = exec.Command("swaymsg", arg, "focus")
|
|
||||||
|
|
||||||
errbuf := &bytes.Buffer{}
|
|
||||||
cmd.Stderr = errbuf
|
|
||||||
|
|
||||||
out, err := cmd.Output()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
slog.Debug("failed to execute swaymsg", "output", out)
|
|
||||||
log.Fatalf("Failed to execute swaymsg to switch focus: %s", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if errbuf.String() != "" {
|
prevnode := Visibles[vislen-1].Id
|
||||||
log.Fatalf("swaymsg error: %s", errbuf.String())
|
|
||||||
|
for _, node := range Visibles {
|
||||||
|
if node.Focused {
|
||||||
|
return prevnode
|
||||||
|
}
|
||||||
|
prevnode = node.Id
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// actually switch focus using a swaymsg command
|
||||||
|
func switchFocus(id int, ipc *i3ipc.I3ipc) error {
|
||||||
|
responses, err := ipc.RunContainerCommand(id, "focus")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to send focus command to container %d: %w (%s)",
|
||||||
|
id, responses[0].Error, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
slog.Info("switched focus", "con_id", id)
|
slog.Info("switched focus", "con_id", id)
|
||||||
}
|
|
||||||
|
|
||||||
// execute swaymsg to get its internal tree
|
|
||||||
func fetchSwayTree() {
|
|
||||||
var cmd *exec.Cmd
|
|
||||||
|
|
||||||
slog.Debug("executing", "command", "swaymsg -t get_tree -r")
|
|
||||||
|
|
||||||
cmd = exec.Command("swaymsg", "-t", "get_tree", "-r")
|
|
||||||
|
|
||||||
errbuf := &bytes.Buffer{}
|
|
||||||
cmd.Stderr = errbuf
|
|
||||||
|
|
||||||
out, err := cmd.Output()
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to execute swaymsg to get json tree: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if errbuf.String() != "" {
|
|
||||||
log.Fatalf("swaymsg error: %s", errbuf.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := processJSON(out); err != nil {
|
|
||||||
log.Fatalf("%s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func istype(nd Node, which int) bool {
|
|
||||||
switch nd.Nodetype {
|
|
||||||
case "root":
|
|
||||||
return which == root
|
|
||||||
case "output":
|
|
||||||
return which == output
|
|
||||||
case "workspace":
|
|
||||||
return which == workspace
|
|
||||||
case "con":
|
|
||||||
return which == con
|
|
||||||
case "floating_con":
|
|
||||||
return which == floating
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// get into the sway tree, determine current workspace and extract all
|
|
||||||
// its visible windows, store them in the global var Visibles
|
|
||||||
func processJSON(jsoncode []byte) error {
|
|
||||||
sway := Node{}
|
|
||||||
|
|
||||||
if err := json.Unmarshal(jsoncode, &sway); err != nil {
|
|
||||||
return fmt.Errorf("Failed to unmarshal json: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !istype(sway, root) && len(sway.Nodes) == 0 {
|
|
||||||
return fmt.Errorf("Invalid or empty JSON structure")
|
|
||||||
}
|
|
||||||
|
|
||||||
if Dumptree {
|
|
||||||
slog.Debug("processed sway tree", "sway", sway)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, node := range sway.Nodes {
|
|
||||||
if node.Current_workspace != "" {
|
|
||||||
// this is an output node containing the current workspace
|
|
||||||
CurrentWorkspace = node.Current_workspace
|
|
||||||
recurseNodes(node.Nodes)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
slog.Debug("processed visible windows", "visibles", Visibles)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterate recursively over given node list extracting visible windows
|
// iterate recursively over given node list extracting visible windows
|
||||||
func recurseNodes(nodes []Node) {
|
func recurseNodes(nodes []*i3ipc.Node) {
|
||||||
for _, node := range nodes {
|
for _, node := range nodes {
|
||||||
// we handle nodes and floating_nodes identical
|
|
||||||
node.Nodes = append(node.Nodes, node.FloatingNodes...)
|
|
||||||
|
|
||||||
if istype(node, workspace) {
|
if istype(node, workspace) {
|
||||||
if node.Name == CurrentWorkspace {
|
if node.Name == CurrentWorkspace {
|
||||||
|
//floating_nodes need to be sorted because
|
||||||
|
//order changes each time they are focused.
|
||||||
|
FloatVis := node.FloatingNodes
|
||||||
|
sort.Slice(FloatVis, func(i, j int) bool {
|
||||||
|
return FloatVis[i].Id < FloatVis[j].Id
|
||||||
|
})
|
||||||
|
//now we can handle nodes and floating_nodes identical
|
||||||
|
node.Nodes = append(node.Nodes, FloatVis...)
|
||||||
recurseNodes(node.Nodes)
|
recurseNodes(node.Nodes)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -330,6 +273,74 @@ func recurseNodes(nodes []Node) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func dumpVisibles() {
|
||||||
|
windows := make([]string, len(Visibles))
|
||||||
|
|
||||||
|
for idx, node := range Visibles {
|
||||||
|
windows[idx] = fmt.Sprintf("id: %02d, focus: %5t, name: %s", node.Id, node.Focused, node.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
slog.Debug("visible windows on current workspace", "visibles", windows)
|
||||||
|
}
|
||||||
|
|
||||||
|
// we use line wise logging, unless debugging is enabled
|
||||||
|
func setupLogging(output io.Writer) {
|
||||||
|
logLevel := &slog.LevelVar{}
|
||||||
|
|
||||||
|
if !Debug {
|
||||||
|
// default logging
|
||||||
|
opts := &tint.Options{
|
||||||
|
Level: logLevel,
|
||||||
|
AddSource: false,
|
||||||
|
NoColor: IsNoTty(),
|
||||||
|
}
|
||||||
|
|
||||||
|
logLevel.Set(slog.LevelInfo)
|
||||||
|
|
||||||
|
handler := tint.NewHandler(output, opts)
|
||||||
|
logger := slog.New(handler)
|
||||||
|
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
} else {
|
||||||
|
// we're using a more verbose logger in debug mode
|
||||||
|
buildInfo, _ := debug.ReadBuildInfo()
|
||||||
|
opts := &yadu.Options{
|
||||||
|
Level: logLevel,
|
||||||
|
AddSource: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
logLevel.Set(slog.LevelDebug)
|
||||||
|
|
||||||
|
handler := yadu.NewHandler(output, opts)
|
||||||
|
debuglogger := slog.New(handler).With(
|
||||||
|
slog.Group("program_info",
|
||||||
|
slog.Int("pid", os.Getpid()),
|
||||||
|
slog.String("go_version", buildInfo.GoVersion),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
slog.SetDefault(debuglogger)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// little helper to distinguish sway tree node types
|
||||||
|
func istype(nd *i3ipc.Node, which int) bool {
|
||||||
|
switch nd.Type {
|
||||||
|
case "root":
|
||||||
|
return which == root
|
||||||
|
case "output":
|
||||||
|
return which == output
|
||||||
|
case "workspace":
|
||||||
|
return which == workspace
|
||||||
|
case "con":
|
||||||
|
return which == con
|
||||||
|
case "floating_con":
|
||||||
|
return which == floating
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// returns TRUE if stdout is NOT a tty or windows
|
// returns TRUE if stdout is NOT a tty or windows
|
||||||
func IsNoTty() bool {
|
func IsNoTty() bool {
|
||||||
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user