110 lines
2.4 KiB
Go
110 lines
2.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
// Standard
|
||
|
"fmt"
|
||
|
"net"
|
||
|
"os"
|
||
|
"regexp"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
session I3Session
|
||
|
sessionSubscription I3Session
|
||
|
workspaces []I3Workspace
|
||
|
outputIndices map[string]int
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
outputIndices = make(map[string]int)
|
||
|
workspaces = []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" },
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
var err error
|
||
|
|
||
|
// session is used for interactively communicating with i3,
|
||
|
// like getting workspace and output data.
|
||
|
if session, err = NewI3Session(); err != nil {
|
||
|
fmt.Printf("%s\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
defer session.Close()
|
||
|
|
||
|
// sessionSubscription is used for listening to subscribed events
|
||
|
// from i3.
|
||
|
if sessionSubscription, err = NewI3Session(); err != nil {
|
||
|
fmt.Printf("%s\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
sessionSubscription.Subscribe([]string{"binding", "output"})
|
||
|
defer sessionSubscription.Close()
|
||
|
go sessionSubscription.Loop()
|
||
|
|
||
|
// 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)
|
||
|
}
|
||
|
|
||
|
// Find outputs and assign an index to them
|
||
|
outputs, err := session.Outputs()
|
||
|
if err != nil {
|
||
|
fmt.Printf("Output error: %s\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
idx := 0
|
||
|
for _, output := range outputs {
|
||
|
if output.Active {
|
||
|
outputIndices[output.Name] = idx
|
||
|
idx++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// XXX: still needed? Find the configured workspaces
|
||
|
if false {
|
||
|
var configData string
|
||
|
var configLines []string
|
||
|
configData, err = session.Config()
|
||
|
configLines = strings.Split(configData, "\n")
|
||
|
r := regexp.MustCompile(`(?i)^\s*workspace\s+["']?([^"']+?)["']?\s+output\s+["']?([^"']+?)["']?\s*$`)
|
||
|
for _, line := range configLines {
|
||
|
matches := r.FindAllStringSubmatch(line, -1)
|
||
|
if len(matches) > 0 && len(matches[0]) == 3 {
|
||
|
fmt.Printf("%#v\n", matches[0][1:])
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Listen for external commands
|
||
|
var conn net.Conn
|
||
|
for {
|
||
|
conn, err = listener.Accept()
|
||
|
if err != nil {
|
||
|
fmt.Printf("Connection accept: %s\n", err)
|
||
|
}
|
||
|
buf := make([]byte, 1024)
|
||
|
_, err = conn.Read(buf)
|
||
|
if err != nil {
|
||
|
fmt.Printf("Connection read: %s\n", err)
|
||
|
} else {
|
||
|
fmt.Printf("%s\n", buf)
|
||
|
}
|
||
|
conn.Close()
|
||
|
}
|
||
|
}
|