diff --git a/desktop_unix.go b/desktop_unix.go index 96bf628..f82e0ba 100644 --- a/desktop_unix.go +++ b/desktop_unix.go @@ -48,38 +48,35 @@ var ( ErrNoClass = errors.New("empty class") ) -func Connect(display string) Xorg { +func Connect(display string) (*Xorg, error) { var x Xorg var err error x.conn, err = xgb.NewConnDisplay(display) if err != nil { - log.Fatal("xgb:", err) + return nil, err } x.util, err = xgbutil.NewConnDisplay(display) if err != nil { - log.Fatal(err) + return nil, err } - err = screensaver.Init(x.conn) - if err != nil { - log.Fatal("screensaver:", err) + if err := screensaver.Init(x.conn); err == nil { + drw := xproto.Drawable(x.root) + screensaver.SelectInput(x.conn, drw, screensaver.EventNotifyMask) } setup := xproto.Setup(x.conn) x.root = setup.DefaultScreen(x.conn).Root - drw := xproto.Drawable(x.root) - screensaver.SelectInput(x.conn, drw, screensaver.EventNotifyMask) - x.activeAtom = x.atom("_NET_ACTIVE_WINDOW") x.netNameAtom = x.atom("_NET_WM_NAME") x.nameAtom = x.atom("WM_NAME") x.classAtom = x.atom("WM_CLASS") x.spy(x.root) - return x + return &x, nil } func (x Xorg) Close() { diff --git a/main.go b/main.go index 97d5b51..952cfc8 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,13 @@ import ( ) var ( - dev streamdeck.Device + dev streamdeck.Device + deck *Deck + dbusConn *dbus.Conn keyboard uinput.Keyboard - x Xorg - deck *Deck + xorg *Xorg recentWindows []Window deckFile = flag.String("deck", "deckmaster.deck", "path to deck config file") @@ -79,11 +80,12 @@ func main() { log.Fatal(err) } - x = Connect(os.Getenv("DISPLAY")) - defer x.Close() - tch := make(chan interface{}) - x.TrackWindows(tch, time.Second) + xorg, err = Connect(os.Getenv("DISPLAY")) + if err == nil { + defer xorg.Close() + xorg.TrackWindows(tch, time.Second) + } d, err := streamdeck.Devices() if err != nil { diff --git a/widget_recent_window.go b/widget_recent_window.go index de8e4f6..062477c 100644 --- a/widget_recent_window.go +++ b/widget_recent_window.go @@ -37,7 +37,12 @@ func (w *RecentWindowWidget) Update(dev *streamdeck.Device) { } func (w *RecentWindowWidget) TriggerAction() { + if xorg == nil { + log.Println("xorg support is disabled!") + return + } + if int(w.window) < len(recentWindows) { - x.RequestActivation(recentWindows[w.window]) + xorg.RequestActivation(recentWindows[w.window]) } }