Partial store/restore functionality implemented

This commit is contained in:
Magnus Åhall 2021-12-28 09:31:56 +01:00
parent 6822cf93fb
commit 08e84947ef
3 changed files with 58 additions and 8 deletions

15
main.go
View File

@ -67,12 +67,25 @@ func main() {
cmd := strings.TrimSpace(string(buf[:n])) cmd := strings.TrimSpace(string(buf[:n]))
switch(cmd) { 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": case "fix workspaces":
err = session.FixWorkspaces() err = session.FixWorkspaces()
if err != nil { if err != nil {
fmt.Printf("Fix workspaces: %s\n", err) fmt.Printf("Fix workspaces: %s\n", err)
} }
conn.Close() conn.Close()
} }
} }

View File

@ -85,14 +85,10 @@ func (sess I3Session) Workspaces() (workspaces []I3Workspace, err error) {
} }
// Subscribe subscribes to events. // Subscribe subscribes to events.
func (sess I3Session) Subscribe(events []string) error { func (sess I3Session) Subscribe(events []string) (err error) {
eventsJSON, _ := json.Marshal(events) eventsJSON, _ := json.Marshal(events)
res, err := sess.Socket.Request(SUBSCRIBE, string(eventsJSON)) _, err = sess.Socket.Request(SUBSCRIBE, string(eventsJSON))
if err != nil { return
return err
}
fmt.Printf("Subscribe: %s\n", res)
return nil
} }
// Config returns the currently running configuration data, as // Config returns the currently running configuration data, as
@ -299,6 +295,44 @@ func (sess I3Session) MarkMove() error {
return nil 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 // FixWorkspaces moves windows from workspaces on the wrong monitor to
// to workspaces on the correct monitor. For fixing when going to less // to workspaces on the correct monitor. For fixing when going to less
// outputs. // outputs.

View File

@ -8,6 +8,9 @@ import (
type I3Session struct { type I3Session struct {
Socket I3Socket Socket I3Socket
filename string filename string
// workspace ID → output name
workspaces map[int]string
} }
type I3Socket struct { type I3Socket struct {