wm/cmd/bspwm/main.go

99 lines
1.9 KiB
Go

package main
import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/jezek/xgbutil"
"tuxpa.in/t/wm/src/bsp"
"tuxpa.in/t/wm/src/handler"
"tuxpa.in/t/wm/src/handler/domains"
"tuxpa.in/t/wm/src/sock"
)
func main() {
err := _main()
if err != nil {
panic(err)
}
}
func _main() error {
// connect to the x server
log.Printf("connecting to xorg")
x11, err := xgbutil.NewConn()
if err != nil {
return err
}
defer x11.Conn().Close()
addr := parsePath(x11, "./bspwm.sock")
// create socket
log.Printf("starting bspwm")
ln, err := sock.Server(addr)
if err != nil {
return err
}
defer ln.Close()
// construct context
ctx, stop := signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGTERM,
syscall.SIGQUIT,
syscall.SIGINT,
)
defer stop()
// initialize WM state
w := bsp.NewWM()
// create a wm-x11 connection
xwm := bsp.NewXWM(w, x11)
// create a handler
h := &handler.Handler{
XWM: xwm,
}
// install the handlers
handler.AddDomain[domains.Todo](h, "node")
handler.AddDomain[domains.Todo](h, "desktop")
handler.AddDomain[domains.Todo](h, "monitor")
handler.AddDomain[domains.Wm](h, "wm")
handler.AddDomain[domains.Todo](h, "rule")
handler.AddDomain[domains.Todo](h, "config")
handler.AddDomain[domains.Todo](h, "subscribe")
handler.AddDomain[domains.Todo](h, "quit")
handler.AddDomain[domains.Query](h, "query")
handler.AddDomain[domains.Echo](h, "echo")
// message listen loop
for {
select {
case m := <-ln.Msg():
h.Run(m)
case <-ctx.Done():
log.Println("bspwm shutting down...")
return nil
}
}
}
func parsePath(xc *xgbutil.XUtil, path string) *net.UnixAddr {
if path == "" {
path = os.Getenv(sock.SOCKET_ENV_VAR)
}
if path == "" {
path = fmt.Sprintf(sock.SOCKET_PATH_TPL, "", xc.Conn().DisplayNumber, xc.Conn().DefaultScreen)
}
addr, err := net.ResolveUnixAddr("unix", path)
if err != nil {
panic(err)
}
return addr
}