- xConn, err := xgb.NewConn()
- if err != nil {
- log.Fatal(err)
- }
- xc = xConn
- defer xc.Close()
-
- // Determine what sort of screen(s) will display output --------------------
-
- setup := xproto.Setup(xc)
- if setup == nil || len(setup.Roots) < 1 {
- log.Fatal("ILLI: Unable to parse received SetupInfo from X11 server.")
- }
-
- if err := xinerama.Init(xc); err != nil {
- log.Fatal(err)
- }
-
- if r, err := xinerama.QueryScreens(xc).Reply(); err != nil {
- log.Fatal(err)
- } else {
- if len(r.ScreenInfo) == 0 {
- attachedScreens = []xinerama.ScreenInfo{
- xinerama.ScreenInfo{
- Width: setup.Roots[0].WidthInPixels,
- Height: setup.Roots[0].HeightInPixels,
- },
- }
- } else {
- attachedScreens = r.ScreenInfo
- }
- }
-
- // Establish a root for the tree -------------------------------------------
-
- connInfo := xproto.Setup(xc)
- if connInfo == nil {
- log.Fatal("ILLI: Unable to parse X connection information")
- }
- if len(connInfo.Roots) != 1 {
- log.Fatal("ILLI: Inappropriate number of roots. Did xinerama initialize correctly?")
- }
- xroot = connInfo.Roots[0]
-
- // Register with the X server as the new window manager --------------------
-
- if err := TakeWMOwnership(); err != nil {
- if _, ok := err.(xproto.AccessError); ok {
- log.Fatal("ILLI: Unable to register as window manager with X server. Perhaps another WM is already running?")
- }
- log.Fatal(err)
- }
-
- fmt.Println("ILLI: Successfully registered as WM with X server.")
-
- // Setup keysym mappings and register for WM-relevant key events -----------
-
- const (
- loKey = 8
- hiKey = 255
- )
-
- m := xproto.GetKeyboardMapping(xc, loKey, hiKey-loKey+1)
- reply, err := m.Reply()
- if err != nil {
- log.Fatal(err)
- }
- if reply == nil {
- log.Fatal("Could not load keyboard map")
- }
-
- for i := 0; i < hiKey-loKey+1; i++ {
- keymap[loKey+i] = reply.Keysyms[i*int(reply.KeysymsPerKeycode) : (i+1)*int(reply.KeysymsPerKeycode)]
- }
- grabs := []struct {
- sym xproto.Keysym
- modifiers uint16
- codes []xproto.Keycode
- }{
- {
- sym: XK_q,
- modifiers: xproto.ModMask1 | xproto.ModMaskShift,
- },
- {
- sym: XK_Return,
- modifiers: xproto.ModMask1 | xproto.ModMaskShift,
- },
- }
-
- for i, syms := range keymap {
- for _, sym := range syms {
- for c := range grabs {
- if grabs[c].sym == sym {
- grabs[c].codes = append(grabs[c].codes, xproto.Keycode(i))
- }
- }
- }
- }
- for _, grabbed := range grabs {
- for _, code := range grabbed.codes {
- if err := xproto.GrabKeyChecked(
- xc,
- false,
- xroot.Root,
- grabbed.modifiers,
- code,
- xproto.GrabModeAsync,
- xproto.GrabModeAsync,
- ).Check(); err != nil {
- log.Print(err)
- }