From 08e84947efc74b488ca9a4d997119597d0025d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Tue, 28 Dec 2021 09:31:56 +0100 Subject: [PATCH] Partial store/restore functionality implemented --- main.go | 15 ++++++++++++++- session.go | 48 +++++++++++++++++++++++++++++++++++++++++------- types.go | 3 +++ 3 files changed, 58 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index bd9bdda..e409415 100644 --- a/main.go +++ b/main.go @@ -67,12 +67,25 @@ func main() { cmd := strings.TrimSpace(string(buf[:n])) switch(cmd) { + case "store workspaces": + err = session.StoreWorkspaces() + if err != nil { + fmt.Printf("Store workspaces: %s\n", err) + } + conn.Close() + + case "restore workspaces": + err = session.RestoreWorkspaces() + if err != nil { + fmt.Printf("Restore workspaces: %s\n", err) + } + conn.Close() + case "fix workspaces": err = session.FixWorkspaces() if err != nil { fmt.Printf("Fix workspaces: %s\n", err) } - conn.Close() } } diff --git a/session.go b/session.go index a831643..3299778 100644 --- a/session.go +++ b/session.go @@ -85,14 +85,10 @@ func (sess I3Session) Workspaces() (workspaces []I3Workspace, err error) { } // Subscribe subscribes to events. -func (sess I3Session) Subscribe(events []string) error { +func (sess I3Session) Subscribe(events []string) (err error) { eventsJSON, _ := json.Marshal(events) - res, err := sess.Socket.Request(SUBSCRIBE, string(eventsJSON)) - if err != nil { - return err - } - fmt.Printf("Subscribe: %s\n", res) - return nil + _, err = sess.Socket.Request(SUBSCRIBE, string(eventsJSON)) + return } // Config returns the currently running configuration data, as @@ -299,6 +295,44 @@ func (sess I3Session) MarkMove() error { return nil } +// StoreWorkspaces stores the output of each workspace. +func (sess *I3Session) StoreWorkspaces() (err error) { + var workspaces []I3Workspace + + sess.workspaces = make(map[int]string) + workspaces, err = sess.Workspaces() + if err != nil { + return + } + + for _, ws := range workspaces { + sess.workspaces[ws.Id] = ws.Output + } + return +} + +// RestoreWorkspaces moves the workspace back to its stored place, if possible. +func (sess *I3Session) RestoreWorkspaces() (err error) { + err = sess.UpdateOutputIndices() + if err != nil { + return err + } + + for wsId, output := range sess.workspaces { + cmd := fmt.Sprintf( + "[workspace=%d] move workspace to output %s", + wsId, + output, + ) + res, err := sess.Socket.Request(RUN_COMMAND, cmd) + if err != nil { + return err + } + fmt.Printf("%s\n", res) + } + return +} + // FixWorkspaces moves windows from workspaces on the wrong monitor to // to workspaces on the correct monitor. For fixing when going to less // outputs. diff --git a/types.go b/types.go index cc1520f..147bd46 100644 --- a/types.go +++ b/types.go @@ -8,6 +8,9 @@ import ( type I3Session struct { Socket I3Socket filename string + + // workspace ID → output name + workspaces map[int]string } type I3Socket struct {