2022-12-26 05:06:40 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-11 14:11:36 +00:00
|
|
|
"fmt"
|
2022-12-26 05:06:40 +00:00
|
|
|
"log"
|
2023-06-11 14:11:36 +00:00
|
|
|
"net"
|
2022-12-26 05:06:40 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2023-06-11 14:11:36 +00:00
|
|
|
"github.com/jezek/xgbutil"
|
2023-06-11 10:24:15 +00:00
|
|
|
"tuxpa.in/t/wm/src/bsp"
|
2022-12-26 05:06:40 +00:00
|
|
|
"tuxpa.in/t/wm/src/handler"
|
2023-06-11 10:24:15 +00:00
|
|
|
"tuxpa.in/t/wm/src/handler/domains"
|
2022-12-26 05:06:40 +00:00
|
|
|
"tuxpa.in/t/wm/src/sock"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2023-06-11 14:11:36 +00:00
|
|
|
|
|
|
|
err := _main()
|
2022-12-26 05:06:40 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-06-11 14:11:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
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
|
2022-12-26 05:06:40 +00:00
|
|
|
log.Printf("starting bspwm")
|
2023-06-11 14:11:36 +00:00
|
|
|
ln, err := sock.Server(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
// construct context
|
2022-12-26 05:06:40 +00:00
|
|
|
ctx, stop := signal.NotifyContext(context.Background(),
|
|
|
|
os.Interrupt,
|
|
|
|
syscall.SIGTERM,
|
2023-06-11 10:24:15 +00:00
|
|
|
syscall.SIGQUIT,
|
|
|
|
syscall.SIGINT,
|
|
|
|
)
|
2022-12-26 05:06:40 +00:00
|
|
|
defer stop()
|
2023-06-11 14:11:36 +00:00
|
|
|
|
2023-06-11 10:24:15 +00:00
|
|
|
// initialize WM state
|
|
|
|
w := bsp.NewWM()
|
2023-06-11 14:11:36 +00:00
|
|
|
// create a wm-x11 connection
|
|
|
|
xwm := bsp.NewXWM(w, x11)
|
|
|
|
// create a handler
|
2023-06-11 10:24:15 +00:00
|
|
|
h := &handler.Handler{
|
|
|
|
XWM: xwm,
|
|
|
|
}
|
2023-06-11 14:11:36 +00:00
|
|
|
// install the handlers
|
2023-06-11 10:24:15 +00:00
|
|
|
handler.AddDomain[domains.Todo](h, "node")
|
|
|
|
handler.AddDomain[domains.Todo](h, "desktop")
|
|
|
|
handler.AddDomain[domains.Todo](h, "monitor")
|
2023-06-11 14:11:36 +00:00
|
|
|
handler.AddDomain[domains.Wm](h, "wm")
|
2023-06-11 10:24:15 +00:00
|
|
|
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")
|
2023-06-11 14:11:36 +00:00
|
|
|
|
|
|
|
// message listen loop
|
2022-12-26 05:06:40 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case m := <-ln.Msg():
|
|
|
|
h.Run(m)
|
|
|
|
case <-ctx.Done():
|
2023-06-11 10:24:15 +00:00
|
|
|
log.Println("bspwm shutting down...")
|
2023-06-11 14:11:36 +00:00
|
|
|
return nil
|
2022-12-26 05:06:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-11 14:11:36 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|