137 lines
2.9 KiB
Go
137 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
// Internal
|
|
"i3-session-manager/i3"
|
|
|
|
// Standard
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
session i3.I3Session
|
|
sessionSubscription i3.I3Session
|
|
subscriptions []string
|
|
)
|
|
|
|
func init() {
|
|
subscriptions = []string{
|
|
"binding",
|
|
"shutdown",
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var err error
|
|
|
|
i3.SetWorkspaces([]i3.I3Workspace{
|
|
{ Num: 0, Name: "Development" },
|
|
{ Num: 1, Name: "Terminal" },
|
|
{ Num: 2, Name: "Browser" },
|
|
{ Num: 3, Name: "Communication" },
|
|
{ Num: 4, Name: "Multimedia" },
|
|
{ Num: 5, Name: "Graphics" },
|
|
{ Num: 6, Name: "SQL" },
|
|
{ Num: 7, Name: "Debug" },
|
|
{ Num: 8, Name: "Email" },
|
|
{ Num: 9, Name: "Virtualization" },
|
|
})
|
|
|
|
go SessionLoop()
|
|
|
|
// Socket server reading commands and acting upon them
|
|
var listener net.Listener
|
|
listener, err = net.Listen("unix", "/tmp/i3-session.sock")
|
|
if err != nil {
|
|
fmt.Printf("Socket listener: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Listen for external commands
|
|
var conn net.Conn
|
|
var n int
|
|
for {
|
|
conn, err = listener.Accept()
|
|
if err != nil {
|
|
fmt.Printf("Connection accept: %s\n", err)
|
|
}
|
|
buf := make([]byte, 1024)
|
|
n, err = conn.Read(buf)
|
|
if err != nil {
|
|
fmt.Printf("Connection read: %s\n", err)
|
|
}
|
|
|
|
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()
|
|
|
|
case "test":
|
|
fmt.Printf("test\n")
|
|
conn.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
// SessionLoop opens sockets and loops when a socket error occurs, trying to
|
|
// open the sockets again.
|
|
func SessionLoop() {
|
|
var err error
|
|
for {
|
|
fmt.Printf("-------- SessionLoop --------\n")
|
|
|
|
// session is used for interactively communicating with i3,
|
|
// like getting workspace and output data.
|
|
if err = session.Open(); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
continue
|
|
}
|
|
|
|
// sessionSubscription is used for listening to subscribed
|
|
// events from i3.
|
|
if err = sessionSubscription.Open(); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
continue
|
|
}
|
|
|
|
// i3-session-manager reacts to keybindings and i3 restarts.
|
|
sessionSubscription.Subscribe(subscriptions)
|
|
|
|
// Find outputs and assign an index to them
|
|
err = session.UpdateOutputIndices()
|
|
if err != nil {
|
|
fmt.Printf("Output error: %s\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
/* Loop will return on socket errors, for example when an i3
|
|
* restart occurs. The socket will not be immediately available
|
|
* then. Some connection errors can thus occur until i3 has
|
|
* initialized the socket again. */
|
|
err = sessionSubscription.Loop()
|
|
fmt.Printf("Loop error: %s\n", err)
|
|
time.Sleep(time.Millisecond*100)
|
|
}
|
|
}
|