Skip to content

Commit

Permalink
Make xorg connection optional
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed May 27, 2021
1 parent 68e1d79 commit 039afdb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
17 changes: 7 additions & 10 deletions desktop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 9 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion widget_recent_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

0 comments on commit 039afdb

Please sign in to comment.