From 6d183638c3cb8e1bd0664a3af6db533a5ff08bb6 Mon Sep 17 00:00:00 2001 From: leukipp Date: Wed, 31 May 2023 23:04:39 +0200 Subject: [PATCH] update socket communication scripts --- README.md | 9 +++++++++ assets/scripts/listen.sh | 15 +++++++++------ desktop/workspace.go | 7 +++++-- input/action.go | 20 ++++++++++++++++---- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index dab6daf..c85afc5 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,15 @@ nc -Ulk /tmp/cortile.sock.out socat UNIX-LISTEN:/tmp/cortile.sock.out,reuseaddr,fork STDOUT ``` +For debugging purposes, you can also dump the json messages into a file: +```bash +# Netcat +nc -Ulk /tmp/cortile.sock.out 2>&1 | tee /tmp/cortile.json + +# Socat +socat -v UNIX-LISTEN:/tmp/cortile.sock.out,reuseaddr,fork OPEN:/tmp/cortile.json,create,truncate +``` + Similarly, requests about the status of cortile can be sent to the incoming socket: ```bash # Netcat diff --git a/assets/scripts/listen.sh b/assets/scripts/listen.sh index 63795c0..65975e3 100755 --- a/assets/scripts/listen.sh +++ b/assets/scripts/listen.sh @@ -58,19 +58,22 @@ while json=$(nc -Ulw 1 $sockout | jq -r "."); do case ${type} in "Action") - ws=$(echo $data | jq -r ".Workspace") + desk=$(echo $data | jq -r ".Desk") + screen=$(echo $data | jq -r ".Screen") # EXAMPLE: retrieve action event on active workspace - echo "Received 'action' with name '$name' on 'workspace = $ws'";; + echo "Received 'action' with name '$name' on 'desktop = $desk' and 'screen = $screen'";; "State") case ${name} in "workspaces") - ws=$(xprop -root -notype _NET_CURRENT_DESKTOP | awk -F " = " '{print $2}') - enabled=$(echo $data | jq -r ".\"$ws\".TilingEnabled") - layout=$(echo $data | jq -r ".\"$ws\".ActiveLayoutNum") + desk=$(echo $data | jq -r ".Desk") + screen=$(echo $data | jq -r ".Screen") + workspace=$(echo $data | jq -r ".Workspaces[] | select((.Location.DeskNum==$desk) and (.Location.ScreenNum==$screen))") + enabled=$(echo $workspace | jq -r ".TilingEnabled") + layout=$(echo $workspace | jq -r ".ActiveLayoutNum") # EXAMPLE: retrieve tiling state and layout on active workspace - echo "Received '$name' with tiling 'enabled = $enabled' on 'workspace = $ws' with 'layout = $layout'";; + echo "Received '$name' with tiling 'enabled = $enabled' on 'desktop = $desk' and 'screen = $screen' with 'layout = $layout'";; "arguments") config=$(echo $data | jq -r ".Config") diff --git a/desktop/workspace.go b/desktop/workspace.go index 65ca5af..cc4533d 100644 --- a/desktop/workspace.go +++ b/desktop/workspace.go @@ -9,6 +9,7 @@ import ( ) type Workspace struct { + Location Location // Desktop and screen location Layouts []Layout // List of available layouts TilingEnabled bool // Tiling is enabled or not ActiveLayoutNum uint // Active layout index @@ -24,8 +25,10 @@ func CreateWorkspaces() map[Location]*Workspace { // Create layouts for each desktop and screen layouts := CreateLayouts(location) ws := &Workspace{ - Layouts: layouts, - TilingEnabled: common.Config.TilingEnabled, + Location: location, + Layouts: layouts, + TilingEnabled: common.Config.TilingEnabled, + ActiveLayoutNum: 0, } // Activate default layout diff --git a/input/action.go b/input/action.go index 3641cd9..322ba6e 100644 --- a/input/action.go +++ b/input/action.go @@ -71,11 +71,14 @@ func Execute(a string, tr *desktop.Tracker) bool { // Notify socket if success { - type Action struct{ Workspace, Screen uint } + type Action struct { + Desk uint + Screen uint + } NotifySocket(Message[Action]{ Type: "Action", Name: a, - Data: Action{Workspace: common.CurrentDesk, Screen: common.CurrentScreen}, + Data: Action{Desk: common.CurrentDesk, Screen: common.CurrentScreen}, }) } @@ -92,10 +95,19 @@ func Query(s string, tr *desktop.Tracker) bool { switch s { case "workspaces": - NotifySocket(Message[map[desktop.Location]*desktop.Workspace]{ + type Workspaces struct { + Desk uint + Screen uint + Workspaces []*desktop.Workspace + } + ws := Workspaces{Desk: common.CurrentDesk, Screen: common.CurrentScreen} + for _, v := range tr.Workspaces { + ws.Workspaces = append(ws.Workspaces, v) + } + NotifySocket(Message[Workspaces]{ Type: "State", Name: s, - Data: tr.Workspaces, + Data: ws, }) success = true case "arguments":