mirror of
https://codeberg.org/scip/swaycycle.git
synced 2025-12-16 20:11:02 +01:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9703fd4ad0 | ||
|
|
85a1e6530c | ||
| feba0f3580 | |||
| 2fbb3ebc59 | |||
|
|
8e48b42bad | ||
|
|
7a5657b778 | ||
| 0bdef90f97 | |||
| 3402df69b4 | |||
| b21d8ebed9 | |||
|
|
a481cc7172 | ||
|
|
cb8421e6f6 | ||
| 051b68c266 | |||
| bde1301e2c | |||
| 977c374197 | |||
| 4741481527 | |||
| dacdc5c214 | |||
| 1bc1b2a963 | |||
| c567e64772 | |||
| cbc061c082 | |||
| cb9b63f568 | |||
| 0cad8cbb62 | |||
| 17fbc4f6a2 | |||
| 852b5c2de9 |
10
.github/dependabot.yml
vendored
Normal file
10
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "gomod"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
- package-ecosystem: "github-actions"
|
||||
directory: "/"
|
||||
schedule:
|
||||
interval: "monthly"
|
||||
65
.goreleaser.yaml
Normal file
65
.goreleaser.yaml
Normal file
@@ -0,0 +1,65 @@
|
||||
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
|
||||
|
||||
version: 2
|
||||
|
||||
before:
|
||||
hooks:
|
||||
- go mod tidy
|
||||
|
||||
gitea_urls:
|
||||
api: https://codeberg.org/api/v1
|
||||
download: https://codeberg.org
|
||||
|
||||
builds:
|
||||
- env:
|
||||
- CGO_ENABLED=0
|
||||
goos:
|
||||
- linux
|
||||
- freebsd
|
||||
|
||||
archives:
|
||||
- formats: [tar.gz]
|
||||
# this name template makes the OS and Arch compatible with the results of `uname`.
|
||||
name_template: >-
|
||||
{{ .ProjectName }}_
|
||||
{{- title .Os }}_
|
||||
{{- if eq .Arch "amd64" }}x86_64
|
||||
{{- else if eq .Arch "386" }}i386
|
||||
{{- else }}{{ .Arch }}{{ end }}
|
||||
{{- if .Arm }}v{{ .Arm }}{{ end }}_{{ .Tag }}
|
||||
# use zip for windows archives
|
||||
format_overrides:
|
||||
- goos: windows
|
||||
formats: [zip]
|
||||
- goos: linux
|
||||
formats: [tar.gz,binary]
|
||||
files:
|
||||
- src: "*.md"
|
||||
strip_parent: true
|
||||
- src: Makefile.dist
|
||||
dst: Makefile
|
||||
wrap_in_directory: true
|
||||
|
||||
changelog:
|
||||
sort: asc
|
||||
filters:
|
||||
exclude:
|
||||
- "^docs:"
|
||||
- "^test:"
|
||||
groups:
|
||||
- title: Improved
|
||||
regexp: '^.*?(feat|add|new)(\([[:word:]]+\))??!?:.+$'
|
||||
order: 0
|
||||
- title: Fixed
|
||||
regexp: '^.*?(bug|fix)(\([[:word:]]+\))??!?:.+$'
|
||||
order: 1
|
||||
- title: Changed
|
||||
order: 999
|
||||
|
||||
release:
|
||||
header: "# Release Notes"
|
||||
footer: >-
|
||||
|
||||
---
|
||||
|
||||
Full Changelog: [{{ .PreviousTag }}...{{ .Tag }}](https://codeberg.org/scip/swaycycle/compare/{{ .PreviousTag }}...{{ .Tag }})
|
||||
36
.woodpecker/build.yaml
Normal file
36
.woodpecker/build.yaml
Normal file
@@ -0,0 +1,36 @@
|
||||
matrix:
|
||||
platform:
|
||||
- linux/amd64
|
||||
goversion:
|
||||
- 1.24
|
||||
|
||||
labels:
|
||||
platform: ${platform}
|
||||
|
||||
steps:
|
||||
build:
|
||||
when:
|
||||
event: [push]
|
||||
image: golang:${goversion}
|
||||
commands:
|
||||
- go get
|
||||
- go build
|
||||
|
||||
linter:
|
||||
when:
|
||||
event: [push]
|
||||
image: golang:${goversion}
|
||||
commands:
|
||||
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.5.0
|
||||
- golangci-lint --version
|
||||
- golangci-lint run ./...
|
||||
depends_on: [build]
|
||||
|
||||
test:
|
||||
when:
|
||||
event: [push]
|
||||
image: golang:${goversion}
|
||||
commands:
|
||||
- go get
|
||||
- go test -v -cover
|
||||
depends_on: [build,linter]
|
||||
15
.woodpecker/release.yaml
Normal file
15
.woodpecker/release.yaml
Normal file
@@ -0,0 +1,15 @@
|
||||
# build release
|
||||
|
||||
labels:
|
||||
platform: linux/amd64
|
||||
|
||||
steps:
|
||||
goreleaser:
|
||||
image: goreleaser/goreleaser
|
||||
when:
|
||||
event: [tag]
|
||||
environment:
|
||||
GITEA_TOKEN:
|
||||
from_secret: DEPLOY_TOKEN
|
||||
commands:
|
||||
- goreleaser release --clean --verbose
|
||||
18
Makefile.dist
Normal file
18
Makefile.dist
Normal file
@@ -0,0 +1,18 @@
|
||||
# -*-make-*-
|
||||
|
||||
.PHONY: install all
|
||||
|
||||
tool = rpn
|
||||
PREFIX = /usr/local
|
||||
UID = root
|
||||
GID = 0
|
||||
|
||||
all:
|
||||
@echo "Type 'sudo make install' to install the tool."
|
||||
@echo "To change prefix, type 'sudo make install PREFIX=/opt'"
|
||||
|
||||
install:
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/share/doc
|
||||
install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/
|
||||
install -o $(UID) -g $(GID) -m 444 *.md $(PREFIX)/share/doc/
|
||||
109
README.md
109
README.md
@@ -1,2 +1,109 @@
|
||||
[](https://ci.codeberg.org/repos/15562)
|
||||
[](https://codeberg.org/scip/swaycycle/raw/branch/main/LICENSE)
|
||||
[](https://goreportcard.com/report/codeberg.org/scip/swaycycle)
|
||||
|
||||
# swaycycle
|
||||
Cycle through all visible windows on a sway[fx] workspace
|
||||
|
||||
Cycle through all visible windows on a sway[fx] workspace including
|
||||
floating ones or windows in sub-containers. So it simulates the
|
||||
behavior of other window managers and desktop environments. Just bind
|
||||
the tool to `ALT-tab` and there you go.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
Download the binary for your architecture from the [release
|
||||
page](https://codeberg.org/scip/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
|
||||
|
||||
Add such a line to your sway config file (e.g. in `$HOME/.config/sway/config`):
|
||||
|
||||
```default
|
||||
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
|
||||
|
||||
You may call `swaycycle` in a terminal window on a workspace with at
|
||||
least one another window to test it. Use the option `--debug (-d)` to
|
||||
get comprehensive debugging output. Add the option `--dump (-D)` to
|
||||
also get a dump of the sway data tree retrieved by swaycycle. You may
|
||||
also try `--verbose (-v)` to get a oneliner about the switch.
|
||||
|
||||
It's also possible to debug an instance executed by sway using the
|
||||
`--logfile (-l)` switch, e.g.:
|
||||
|
||||
```default
|
||||
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
|
||||
|
||||
Although I'm happy to hear from swaycycle users in private email, that's the
|
||||
best way for me to forget to do something.
|
||||
|
||||
In order to report a bug, unexpected behavior, feature requests or to
|
||||
submit a patch, please open an issue on github:
|
||||
https://codeberg.org/scip/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
|
||||
|
||||
This software is licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
||||
|
||||
## Authors
|
||||
|
||||
T.v.Dein <tom AT vondein DOT org>
|
||||
|
||||
## Project homepage
|
||||
|
||||
https://codeberg.org/scip/swaycycle
|
||||
|
||||
## Copyright and License
|
||||
|
||||
Licensed under the GNU GENERAL PUBLIC LICENSE version 3.
|
||||
|
||||
## Author
|
||||
|
||||
T.v.Dein <tom AT vondein DOT org>
|
||||
|
||||
8
go.mod
8
go.mod
@@ -4,5 +4,13 @@ go 1.23
|
||||
|
||||
require (
|
||||
github.com/alecthomas/repr v0.5.1 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/lmittmann/tint v1.1.2 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // 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
|
||||
golang.org/x/sys v0.14.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
21
go.sum
21
go.sum
@@ -1,4 +1,25 @@
|
||||
github.com/alecthomas/repr v0.5.1 h1:E3G4t2QbHTSNpPKBgMTln5KLkZHLOcU7r37J4pXBuIg=
|
||||
github.com/alecthomas/repr v0.5.1/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
|
||||
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
|
||||
github.com/lmittmann/tint v1.1.2 h1:2CQzrL6rslrsyjqLDwD11bZ5OpLBPU+g3G/r5LSfS8w=
|
||||
github.com/lmittmann/tint v1.1.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
|
||||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
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/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/go.mod h1:l3bRmHKL9zGAR6pnBHY2HRPxBecf7L74BoBgOOpTcUA=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
|
||||
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
347
main.go
347
main.go
@@ -18,14 +18,19 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
|
||||
"github.com/alecthomas/repr"
|
||||
"github.com/lmittmann/tint"
|
||||
"github.com/mattn/go-isatty"
|
||||
"github.com/tlinden/i3ipc"
|
||||
"github.com/tlinden/yadu"
|
||||
|
||||
flag "github.com/spf13/pflag"
|
||||
)
|
||||
@@ -37,29 +42,58 @@ const (
|
||||
con
|
||||
floating
|
||||
|
||||
VERSION = "v0.1.1"
|
||||
LevelNotice = slog.Level(2)
|
||||
|
||||
VERSION = "v0.3.1"
|
||||
|
||||
IPC_HEADER_SIZE = 14
|
||||
IPC_MAGIC = "i3-ipc"
|
||||
|
||||
// message types
|
||||
IPC_GET_TREE = 4
|
||||
IPC_RUN_COMMAND = 0
|
||||
)
|
||||
|
||||
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"`
|
||||
}
|
||||
var (
|
||||
Visibles = []*i3ipc.Node{}
|
||||
CurrentWorkspace = ""
|
||||
Previous = false
|
||||
Debug = false
|
||||
Dumptree = false
|
||||
Dumpvisibles = false
|
||||
Version = false
|
||||
Verbose = false
|
||||
Notswitch = false
|
||||
Showhelp = false
|
||||
Logfile = ""
|
||||
)
|
||||
|
||||
var Visibles = []Node{}
|
||||
var CurrentWorkspace = ""
|
||||
var Debug = false
|
||||
var Version = false
|
||||
const Usage string = `This is swaycycle - cycle focus through all visible windows on a sway workspace.
|
||||
|
||||
Usage: swaycycle [-vdDn] [-l <log>]
|
||||
|
||||
Options:
|
||||
-p, --prev cycle backward
|
||||
-n, --no-switch do not switch windows
|
||||
-d, --debug enable debugging
|
||||
-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
|
||||
-v, --version show program version
|
||||
|
||||
Copyleft (L) 2025 Thomas von Dein.
|
||||
Licensed under the terms of the GNU GPL version 3.`
|
||||
|
||||
func main() {
|
||||
flag.BoolVarP(&Previous, "prev", "p", false, "cycle backward")
|
||||
flag.BoolVarP(&Debug, "debug", "d", false, "enable debugging")
|
||||
flag.BoolVarP(&Dumptree, "dump", "D", false, "dump the sway tree (needs -d as well)")
|
||||
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(&Version, "version", "v", false, "show program version")
|
||||
flag.BoolVarP(&Showhelp, "help", "h", Showhelp, "show help")
|
||||
|
||||
flag.StringVarP(&Logfile, "logfile", "l", "", "write output to logfile")
|
||||
flag.Parse()
|
||||
|
||||
if Version {
|
||||
@@ -67,18 +101,91 @@ func main() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// fills Visibles node list
|
||||
fetchSwayTree()
|
||||
if Showhelp {
|
||||
fmt.Println(Usage)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// setup logging
|
||||
if Logfile != "" {
|
||||
file, err := os.OpenFile(Logfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open logfile %s: %s", Logfile, err)
|
||||
}
|
||||
defer func() {
|
||||
if err := file.Close(); err != nil {
|
||||
log.Fatalf("failed to close log file: %s", err)
|
||||
}
|
||||
}()
|
||||
setupLogging(file)
|
||||
} else {
|
||||
setupLogging(os.Stdout)
|
||||
}
|
||||
|
||||
// connect to sway unix socket
|
||||
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 {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
id := findNextWindow()
|
||||
|
||||
if id > 0 {
|
||||
switchFocus(id)
|
||||
id := 0
|
||||
if Previous {
|
||||
id = findPrevWindow()
|
||||
slog.Debug("findPrevWindow", "nextid", id)
|
||||
} else {
|
||||
id = findNextWindow()
|
||||
slog.Debug("findNextWindow", "nextid", id)
|
||||
}
|
||||
|
||||
if id > 0 && !Notswitch {
|
||||
if err := switchFocus(id, ipc); err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// get into the sway tree, determine current workspace and extract all
|
||||
// its visible windows, store them in the global var Visibles
|
||||
func processJSON(sway *i3ipc.Node) error {
|
||||
if !istype(sway, root) && len(sway.Nodes) == 0 {
|
||||
return errors.New("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
|
||||
}
|
||||
}
|
||||
|
||||
if Dumpvisibles {
|
||||
dumpVisibles()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// find the next window after the one with current focus. if the last
|
||||
@@ -108,64 +215,123 @@ func findNextWindow() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// actually switch focus using a swaymsg command
|
||||
func switchFocus(id int) {
|
||||
var cmd *exec.Cmd
|
||||
arg := fmt.Sprintf("[con_id=%d]", id)
|
||||
|
||||
if Debug {
|
||||
fmt.Printf("executing: swaymsg %s focus\n", arg)
|
||||
func findPrevWindow() int {
|
||||
vislen := len(Visibles)
|
||||
if vislen == 0 {
|
||||
return 0
|
||||
}
|
||||
cmd = exec.Command("swaymsg", arg, "focus")
|
||||
|
||||
errbuf := &bytes.Buffer{}
|
||||
cmd.Stderr = errbuf
|
||||
prevnode := Visibles[vislen-1].Id
|
||||
|
||||
out, err := cmd.Output()
|
||||
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 execute swaymsg to switch focus: %s", err)
|
||||
if Debug {
|
||||
fmt.Println(out)
|
||||
log.Fatalf("failed to send focus command to container %d: %s (%s)",
|
||||
id, responses[0].Error, err)
|
||||
}
|
||||
|
||||
slog.Info("switched focus", "con_id", id)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// iterate recursively over given node list extracting visible windows
|
||||
func recurseNodes(nodes []*i3ipc.Node) {
|
||||
for _, node := range nodes {
|
||||
|
||||
if istype(node, workspace) {
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
// ignore other workspaces
|
||||
continue
|
||||
}
|
||||
|
||||
// the first nodes seen are workspaces, so if we see a con
|
||||
// node, we are already inside the current workspace
|
||||
if (istype(node, con) || istype(node, floating)) &&
|
||||
(node.Window > 0 || node.X11Window != "") {
|
||||
Visibles = append(Visibles, node)
|
||||
} else {
|
||||
recurseNodes(node.Nodes)
|
||||
}
|
||||
}
|
||||
|
||||
if errbuf.String() != "" {
|
||||
log.Fatalf("swaymsg error: %s", errbuf.String())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// execute swaymsg to get its internal tree
|
||||
func fetchSwayTree() {
|
||||
var cmd *exec.Cmd
|
||||
func dumpVisibles() {
|
||||
windows := make([]string, len(Visibles))
|
||||
|
||||
if Debug {
|
||||
fmt.Println("executing: swaymsg -t get_tree -r")
|
||||
for idx, node := range Visibles {
|
||||
windows[idx] = fmt.Sprintf("id: %02d, focus: %5t, name: %s", node.Id, node.Focused, node.Name)
|
||||
}
|
||||
|
||||
cmd = exec.Command("swaymsg", "-t", "get_tree", "-r")
|
||||
slog.Debug("visible windows on current workspace", "visibles", windows)
|
||||
}
|
||||
|
||||
errbuf := &bytes.Buffer{}
|
||||
cmd.Stderr = errbuf
|
||||
// we use line wise logging, unless debugging is enabled
|
||||
func setupLogging(output io.Writer) {
|
||||
logLevel := &slog.LevelVar{}
|
||||
|
||||
out, err := cmd.Output()
|
||||
if !Debug {
|
||||
// default logging
|
||||
opts := &tint.Options{
|
||||
Level: logLevel,
|
||||
AddSource: false,
|
||||
NoColor: IsNoTty(),
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to execute swaymsg to get json tree: %s", err)
|
||||
}
|
||||
logLevel.Set(slog.LevelInfo)
|
||||
|
||||
if errbuf.String() != "" {
|
||||
log.Fatalf("swaymsg error: %s", errbuf.String())
|
||||
}
|
||||
handler := tint.NewHandler(output, opts)
|
||||
logger := slog.New(handler)
|
||||
|
||||
if err := processJSON(out); err != nil {
|
||||
log.Fatalf("%s", err)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func istype(nd Node, which int) bool {
|
||||
switch nd.Nodetype {
|
||||
// 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":
|
||||
@@ -181,53 +347,12 @@ func istype(nd Node, which int) bool {
|
||||
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)
|
||||
// returns TRUE if stdout is NOT a tty or windows
|
||||
func IsNoTty() bool {
|
||||
if !isatty.IsTerminal(os.Stdout.Fd()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if !istype(sway, root) && len(sway.Nodes) == 0 {
|
||||
return fmt.Errorf("Invalid or empty JSON structure")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
if Debug {
|
||||
repr.Println(Visibles)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// iterate recursively over given node list extracting visible windows
|
||||
func recurseNodes(nodes []Node) {
|
||||
for _, node := range nodes {
|
||||
// we handle nodes and floating_nodes identical
|
||||
node.Nodes = append(node.Nodes, node.FloatingNodes...)
|
||||
|
||||
if istype(node, workspace) {
|
||||
if node.Name == CurrentWorkspace {
|
||||
recurseNodes(node.Nodes)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// the first nodes seen are workspaces, so if we see a con
|
||||
// node, we are already inside the current workspace
|
||||
if (istype(node, con) || istype(node, floating)) && (node.Window > 0 || node.X11Window != "") {
|
||||
Visibles = append(Visibles, node)
|
||||
} else {
|
||||
recurseNodes(node.Nodes)
|
||||
}
|
||||
}
|
||||
// it is a tty
|
||||
return false
|
||||
}
|
||||
|
||||
69
mkrel.sh
Executable file
69
mkrel.sh
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright © 2025 Thomas von Dein
|
||||
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
# get list with: go tool dist list
|
||||
DIST="darwin/amd64
|
||||
freebsd/amd64
|
||||
linux/amd64
|
||||
freebsd/arm64
|
||||
linux/arm64"
|
||||
|
||||
tool="$1"
|
||||
version="$2"
|
||||
|
||||
if test -z "$version"; then
|
||||
echo "Usage: $0 <tool name> <release version>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
rm -rf releases
|
||||
mkdir -p releases
|
||||
|
||||
|
||||
for D in $DIST; do
|
||||
os=${D/\/*/}
|
||||
arch=${D/*\//}
|
||||
binfile="releases/${tool}-${os}-${arch}-${version}"
|
||||
|
||||
if test "$os" = "windows"; then
|
||||
binfile="${binfile}.exe"
|
||||
fi
|
||||
|
||||
tardir="${tool}-${os}-${arch}-${version}"
|
||||
tarfile="releases/${tool}-${os}-${arch}-${version}.tar.gz"
|
||||
set -x
|
||||
GOOS=${os} GOARCH=${arch} go build -tags osusergo,netgo -ldflags "-extldflags=-static" -o ${binfile}
|
||||
mkdir -p ${tardir}
|
||||
cp ${binfile} README.md LICENSE ${tardir}/
|
||||
echo 'tool = swaycycle
|
||||
PREFIX = /usr/local
|
||||
UID = root
|
||||
GID = 0
|
||||
|
||||
install:
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/bin
|
||||
install -d -o $(UID) -g $(GID) $(PREFIX)/man/man1
|
||||
install -o $(UID) -g $(GID) -m 555 $(tool) $(PREFIX)/sbin/
|
||||
install -o $(UID) -g $(GID) -m 444 $(tool).1 $(PREFIX)/man/man1/' > ${tardir}/Makefile
|
||||
tar cpzf ${tarfile} ${tardir}
|
||||
sha256sum ${binfile} | cut -d' ' -f1 > ${binfile}.sha256
|
||||
sha256sum ${tarfile} | cut -d' ' -f1 > ${tarfile}.sha256
|
||||
rm -rf ${tardir}
|
||||
set +x
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user