From e5f827aed4a1c13eb52577942886b70e557a4975 Mon Sep 17 00:00:00 2001 From: a Date: Sun, 11 Jun 2023 09:56:54 -0500 Subject: [PATCH] okg --- cmd/bspwm/main.go | 39 ++++++++++++++++++++++++++++------- src/bsp/cfg/cfg.go | 21 +++++++++++++++++++ src/bsp/wm.go | 2 +- src/handler/domains/lambda.go | 16 ++++++++++++++ src/handler/handler.go | 11 ++++++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/handler/domains/lambda.go diff --git a/cmd/bspwm/main.go b/cmd/bspwm/main.go index 7f2aae8..74dfc58 100644 --- a/cmd/bspwm/main.go +++ b/cmd/bspwm/main.go @@ -7,6 +7,7 @@ import ( "net" "os" "os/signal" + "strconv" "syscall" "github.com/jezek/xgbutil" @@ -18,18 +19,19 @@ import ( func main() { - err := _main() + c, err := _main() if err != nil { panic(err) } + os.Exit(c) } -func _main() error { +func _main() (code int, err error) { // connect to the x server log.Printf("connecting to xorg") x11, err := xgbutil.NewConn() if err != nil { - return err + return 1, err } defer x11.Conn().Close() @@ -38,7 +40,7 @@ func _main() error { log.Printf("starting bspwm") ln, err := sock.Server(addr) if err != nil { - return err + return 1, err } defer ln.Close() @@ -67,18 +69,41 @@ func _main() error { handler.AddDomain[domains.Todo](h, "rule") handler.AddDomain[domains.Config](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") + codeCh := make(chan int, 1) + + handler.AddDomain[domains.Todo](h, "quit") + h.AddDomainFunc("quit", func() handler.Domain { + d := &domains.Lambda{ + Fn: func(x *bsp.XWM, msg *sock.Msg) ([]byte, error) { + if !msg.HasNext() { + codeCh <- 0 + return nil, nil + } + str := msg.Next() + cd, err := strconv.Atoi(str) + if err != nil { + return nil, err + } + codeCh <- cd + return nil, nil + }, + } + return d + }) + // message listen loop for { select { case m := <-ln.Msg(): h.Run(m) + case cint := <-codeCh: + stop() + return cint, nil case <-ctx.Done(): - log.Println("bspwm shutting down...") - return nil + return 0, nil } } } diff --git a/src/bsp/cfg/cfg.go b/src/bsp/cfg/cfg.go index c7b4c48..af96ab6 100644 --- a/src/bsp/cfg/cfg.go +++ b/src/bsp/cfg/cfg.go @@ -114,6 +114,27 @@ func (m *Modifier[T]) setup() { } return fmt.Sprintf("%v", fv.Elem()), nil } + case reflect.Float32, reflect.Float64: + m.setters[k] = func(v string) (err error) { + var val float64 + if v != "" { + val, err = strconv.ParseFloat(v, 10) + if err != nil { + return copies.NewInvalidValueErr(k, v) + } + } + if fv.IsNil() { + fv.Set(reflect.New(ft.Type.Elem())) + } + fv.Elem().SetFloat(val) + return nil + } + m.getters[k] = func() (string, error) { + if fv.IsNil() { + return "", nil + } + return fmt.Sprintf("%v", fv.Elem()), nil + } case reflect.Bool: m.setters[k] = func(v string) error { var b bool diff --git a/src/bsp/wm.go b/src/bsp/wm.go index a830f4c..f9f6f41 100644 --- a/src/bsp/wm.go +++ b/src/bsp/wm.go @@ -71,7 +71,7 @@ func (w *WM) AddDesktop(name string, monitorName string) error { } } if monitor == nil { - return fmt.Errorf("invalid descriptor found in '%s'", monitor) + return fmt.Errorf("invalid descriptor found in '%s'", monitorName) } w.Desktops = append(w.Desktops, &Desktop{ Name: name, diff --git a/src/handler/domains/lambda.go b/src/handler/domains/lambda.go new file mode 100644 index 0000000..bbfa014 --- /dev/null +++ b/src/handler/domains/lambda.go @@ -0,0 +1,16 @@ +package domains + +import ( + "tuxpa.in/t/wm/src/bsp" + "tuxpa.in/t/wm/src/sock" +) + +type Lambda struct { + Fn func(x *bsp.XWM, msg *sock.Msg) ([]byte, error) + + inject +} + +func (n *Lambda) Run(msg *sock.Msg) ([]byte, error) { + return n.Fn(n.XWM, msg) +} diff --git a/src/handler/handler.go b/src/handler/handler.go index f731a4d..062de79 100644 --- a/src/handler/handler.go +++ b/src/handler/handler.go @@ -28,6 +28,17 @@ func AddDomain[T any, PT interface { } } +func (h *Handler) AddDomainFunc(name string, fn func() Domain) { + if h.domains == nil { + h.domains = map[string]func() Domain{} + } + h.domains[name] = func() Domain { + d := fn() + d.SetXWM(h.XWM) + return d + } +} + func (h *Handler) Run(msg *sock.Msg) { resp, err := h.run(msg) if msg.Err(err) {