From 99f43d34088d7d0023dd6bda5406c4e9673ff589 Mon Sep 17 00:00:00 2001 From: a Date: Sun, 11 Jun 2023 09:34:42 -0500 Subject: [PATCH] monitor --- src/bsp/wm.go | 46 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/bsp/wm.go b/src/bsp/wm.go index e67b7fe..a830f4c 100644 --- a/src/bsp/wm.go +++ b/src/bsp/wm.go @@ -1,6 +1,7 @@ package bsp import ( + "fmt" "sync" "tuxpa.in/t/wm/src/bsp/cfg" @@ -19,39 +20,66 @@ type Desktop struct { Name string Monitor *Monitor - Cfg *cfg.Modifier[Config] + Cfg *cfg.Modifier[*Config] } type Monitor struct { Name string - Cfg *cfg.Modifier[Config] + Cfg *cfg.Modifier[*Config] } type Node struct { Name string - Cfg *cfg.Modifier[Config] + Cfg *cfg.Modifier[*Config] } -func (w *WM) Mutate(fn func() error) { +func (w *WM) Mutate(fn func() error) error { w.mu.Lock() defer w.mu.Unlock() if fn != nil { - fn() + return fn() } + return nil } -func (w *WM) View(fn func() error) { +func (w *WM) View(fn func() error) error { w.mu.RLock() defer w.mu.RUnlock() if fn != nil { - fn() + return fn() } + return nil } -func (w *WM) AddDesktop(name string) { - +func (w *WM) AddMonitor(name string) error { + return w.Mutate(func() error { + w.Monitors = append(w.Monitors, &Monitor{ + Name: name, + Cfg: cfg.NewModifier(&Config{}), + }) + return nil + }) +} +func (w *WM) AddDesktop(name string, monitorName string) error { + return w.Mutate(func() error { + var monitor *Monitor + for _, v := range w.Monitors { + if v.Name == monitorName { + monitor = v + } + } + if monitor == nil { + return fmt.Errorf("invalid descriptor found in '%s'", monitor) + } + w.Desktops = append(w.Desktops, &Desktop{ + Name: name, + Cfg: cfg.NewModifier(&Config{}), + Monitor: monitor, + }) + return nil + }) } func NewWM() *WM {