2019-04-13 00:31:58 +02:00
|
|
|
package sway_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-04-14 08:13:12 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-04-13 00:31:58 +02:00
|
|
|
"log"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sway "github.com/joshuarubin/go-sway"
|
|
|
|
)
|
|
|
|
|
2019-04-14 08:13:12 +02:00
|
|
|
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) {
|
2019-04-14 08:13:12 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:13:12 +02:00
|
|
|
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
|
|
|
|
2019-04-14 08:13:12 +02:00
|
|
|
printJSON(inputs)
|
|
|
|
|
|
|
|
seats, err := client.GetSeats(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
printJSON(seats)
|
|
|
|
|
2019-04-14 08:35:40 +02:00
|
|
|
n, err := client.GetTree(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-04-13 00:31:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-14 08:35:40 +02:00
|
|
|
processFocus(ctx, client, n.FocusedNode())
|
|
|
|
|
2019-04-15 04:21:31 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:35:40 +02:00
|
|
|
type testHandler struct {
|
2019-04-15 04:21:31 +02:00
|
|
|
sway.EventHandler
|
2019-04-14 08:35:40 +02:00
|
|
|
client sway.Client
|
|
|
|
}
|
2019-04-13 00:31:58 +02:00
|
|
|
|
2019-04-14 08:35:40 +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
|
|
|
|
2019-04-14 08:35:40 +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
|
|
|
}
|
|
|
|
}
|