update to go 1.26, update to SwayIPC v2

This commit is contained in:
2026-08-22 20:26:53 +02:00
parent 9703fd4ad0
commit 60081f9dce
3 changed files with 43 additions and 31 deletions

34
main.go
View File

@@ -1,5 +1,5 @@
/*
Copyright © 2025 Thomas von Dein
Copyright © 2025-2026 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
@@ -27,9 +27,9 @@ import (
"runtime/debug"
"sort"
"codeberg.org/scip/swayipc/v2"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/tlinden/i3ipc"
"github.com/tlinden/yadu"
flag "github.com/spf13/pflag"
@@ -44,7 +44,7 @@ const (
LevelNotice = slog.Level(2)
VERSION = "v0.3.1"
VERSION = "v0.3.2"
IPC_HEADER_SIZE = 14
IPC_MAGIC = "i3-ipc"
@@ -55,7 +55,7 @@ const (
)
var (
Visibles = []*i3ipc.Node{}
Visibles = []*swayipc.Node{}
CurrentWorkspace = ""
Previous = false
Debug = false
@@ -123,7 +123,7 @@ func main() {
}
// connect to sway unix socket
ipc := i3ipc.NewI3ipc()
ipc := swayipc.NewSwayIPC()
err := ipc.Connect()
if err != nil {
@@ -163,7 +163,7 @@ func main() {
// 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 {
func processJSON(sway *swayipc.Node) error {
if !istype(sway, root) && len(sway.Nodes) == 0 {
return errors.New("invalid or empty JSON structure")
}
@@ -173,9 +173,9 @@ func processJSON(sway *i3ipc.Node) error {
}
for _, node := range sway.Nodes {
if node.Current_workspace != "" {
if node.CurrentWorkspace != "" {
// this is an output node containing the current workspace
CurrentWorkspace = node.Current_workspace
CurrentWorkspace = node.CurrentWorkspace
recurseNodes(node.Nodes)
break
}
@@ -204,12 +204,12 @@ func findNextWindow() int {
}
if seenfocused {
return node.Id
return node.ID
}
}
if seenfocused {
return Visibles[0].Id
return Visibles[0].ID
}
return 0
@@ -221,20 +221,20 @@ func findPrevWindow() int {
return 0
}
prevnode := Visibles[vislen-1].Id
prevnode := Visibles[vislen-1].ID
for _, node := range Visibles {
if node.Focused {
return prevnode
}
prevnode = node.Id
prevnode = node.ID
}
return 0
}
// actually switch focus using a swaymsg command
func switchFocus(id int, ipc *i3ipc.I3ipc) error {
func switchFocus(id int, ipc *swayipc.SwayIPC) error {
responses, err := ipc.RunContainerCommand(id, "focus")
if err != nil {
log.Fatalf("failed to send focus command to container %d: %s (%s)",
@@ -247,7 +247,7 @@ func switchFocus(id int, ipc *i3ipc.I3ipc) error {
}
// iterate recursively over given node list extracting visible windows
func recurseNodes(nodes []*i3ipc.Node) {
func recurseNodes(nodes []*swayipc.Node) {
for _, node := range nodes {
if istype(node, workspace) {
@@ -256,7 +256,7 @@ func recurseNodes(nodes []*i3ipc.Node) {
//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
return FloatVis[i].ID < FloatVis[j].ID
})
//now we can handle nodes and floating_nodes identical
node.Nodes = append(node.Nodes, FloatVis...)
@@ -283,7 +283,7 @@ 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)
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)
@@ -330,7 +330,7 @@ func setupLogging(output io.Writer) {
}
// little helper to distinguish sway tree node types
func istype(nd *i3ipc.Node, which int) bool {
func istype(nd *swayipc.Node, which int) bool {
switch nd.Type {
case "root":
return which == root