i3-session-manager/main.go

138 lines
2.9 KiB
Go
Raw Permalink Normal View History

2021-12-21 19:43:46 +01:00
package main
import (
2024-07-13 17:44:00 +02:00
// Internal
"i3-session-manager/i3"
2021-12-21 19:43:46 +01:00
// Standard
"fmt"
"net"
"os"
"strings"
2021-12-23 09:06:43 +01:00
"time"
2021-12-21 19:43:46 +01:00
)
var (
2024-07-13 17:44:00 +02:00
session i3.I3Session
sessionSubscription i3.I3Session
2021-12-23 09:06:43 +01:00
subscriptions []string
2021-12-21 19:43:46 +01:00
)
func init() {
2021-12-23 09:06:43 +01:00
subscriptions = []string{
"binding",
"shutdown",
}
2024-07-13 17:44:00 +02:00
}
func main() {
var err error
2021-12-23 09:06:43 +01:00
2024-07-13 17:44:00 +02:00
i3.SetWorkspaces([]i3.I3Workspace{
2021-12-21 19:43:46 +01:00
{ 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" },
2024-07-13 17:44:00 +02:00
})
2021-12-21 19:43:46 +01:00
2021-12-23 09:06:43 +01:00
go SessionLoop()
2021-12-21 19:43:46 +01:00
// 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
2021-12-21 19:43:46 +01:00
for {
conn, err = listener.Accept()
if err != nil {
fmt.Printf("Connection accept: %s\n", err)
}
buf := make([]byte, 1024)
n, err = conn.Read(buf)
2021-12-21 19:43:46 +01:00
if err != nil {
fmt.Printf("Connection read: %s\n", err)
}
cmd := strings.TrimSpace(string(buf[:n]))
2021-12-23 09:06:43 +01:00
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()
2021-12-23 09:06:43 +01:00
case "fix workspaces":
err = session.FixWorkspaces()
if err != nil {
fmt.Printf("Fix workspaces: %s\n", err)
}
2021-12-23 09:11:49 +01:00
conn.Close()
2024-07-13 17:44:00 +02:00
case "test":
fmt.Printf("test\n")
conn.Close()
2021-12-23 09:11:49 +01:00
}
2021-12-21 19:43:46 +01:00
}
}
2021-12-23 09:06:43 +01:00
2021-12-23 09:29:18 +01:00
// SessionLoop opens sockets and loops when a socket error occurs, trying to
// open the sockets again.
2021-12-23 09:06:43 +01:00
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)
}
}