/
connection.go
57 lines (50 loc) · 1.25 KB
/
connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package x11
import (
"errors"
"fmt"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
)
type Connection struct {
conn *xgb.Conn
util *xgbutil.XUtil
screen xproto.ScreenInfo
atoms map[string]xproto.Atom
}
func Connect() (*Connection, error) {
atoms := make(map[string]xproto.Atom)
xconn, err := xgb.NewConn()
if err != nil {
return nil, fmt.Errorf("failed to connect: %w", err)
}
xutil, err := xgbutil.NewConnXgb(xconn)
if err != nil {
return nil, fmt.Errorf("failed to create XUtil connection: %w", err)
}
return &Connection{conn: xconn, util: xutil, atoms: atoms}, nil
}
func (xc *Connection) X() *xgb.Conn { return xc.conn }
func (xc *Connection) Screen() xproto.ScreenInfo { return xc.screen }
func (xc *Connection) Init() error {
conninfo := xproto.Setup(xc.conn)
if conninfo == nil {
return errors.New("could not parse X connection info")
}
if len(conninfo.Roots) != 1 {
return errors.New("wrong number of roots, possibly xinerama did not initialize properly")
}
xc.screen = conninfo.Roots[0]
err := xc.setHints()
if err != nil {
return err
}
err = xc.initDesktop()
if err != nil {
return err
}
return nil
}
func (xc *Connection) Close() {
xc.conn.Close()
}