1
0
mirror of https://github.com/joshuarubin/go-sway synced 2024-11-22 21:11:59 +01:00
go-sway/socket_test.go

150 lines
2.4 KiB
Go
Raw Normal View History

2019-04-13 00:31:58 +02:00
package sway_test
import (
"context"
"encoding/json"
"fmt"
2019-04-13 00:31:58 +02:00
"log"
"testing"
"time"
sway "github.com/joshuarubin/go-sway"
)
func printJSON(v interface{}) {
out, _ := json.MarshalIndent(v, "", " ")
fmt.Println(string(out))
}
2019-04-13 00:31:58 +02:00
func TestSocket(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
2019-04-13 00:31:58 +02:00
defer cancel()
client, err := sway.New(ctx)
if err != nil {
t.Fatal(err)
}
workspaces, err := client.GetWorkspaces(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(workspaces)
outputs, err := client.GetOutputs(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(outputs)
marks, err := client.GetMarks(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(marks)
barIDs, err := client.GetBarIDs(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(barIDs)
for _, id := range barIDs {
config, err := client.GetBarConfig(ctx, id)
if err != nil {
t.Fatal(err)
}
printJSON(config)
}
version, err := client.GetVersion(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(*version)
bindingModes, err := client.GetBindingModes(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(bindingModes)
config, err := client.GetConfig(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(config)
tick, err := client.SendTick(ctx, "foo")
if err != nil {
t.Fatal(err)
}
printJSON(tick)
inputs, err := client.GetInputs(ctx)
if err != nil {
t.Fatal(err)
}
2019-04-13 00:31:58 +02:00
printJSON(inputs)
seats, err := client.GetSeats(ctx)
if err != nil {
t.Fatal(err)
}
printJSON(seats)
n, err := client.GetTree(ctx)
if err != nil {
t.Fatal(err)
2019-04-13 00:31:58 +02:00
}
processFocus(ctx, client, n.FocusedNode())
th := testHandler{
EventHandler: sway.NoOpEventHandler(),
client: client,
}
if err = sway.Subscribe(ctx, th, sway.EventTypeWindow); err != context.DeadlineExceeded && err != nil {
2019-04-13 00:31:58 +02:00
t.Fatal(err)
}
}
type testHandler struct {
sway.EventHandler
client sway.Client
}
2019-04-13 00:31:58 +02:00
func (h testHandler) Window(ctx context.Context, e sway.WindowEvent) {
if e.Change != "focus" {
return
}
2019-04-13 00:31:58 +02:00
processFocus(ctx, h.client, e.Container.FocusedNode())
}
func processFocus(ctx context.Context, client sway.Client, node *sway.Node) {
if node == nil {
return
}
opt := "none"
if node.AppID == nil || *node.AppID != "kitty" {
opt = "altwin:ctrl_win"
}
if _, err := client.RunCommand(ctx, `input '*' xkb_options `+opt); err != nil {
log.Println(err)
2019-04-13 00:31:58 +02:00
}
}