// create creates the window, initializes the keybind and mousebind packages // and sets up the window to act like a real top-level client. func (w *window) create() { keybind.Initialize(w.X) mousebind.Initialize(w.X) err := w.CreateChecked(w.X.RootWin(), 0, 0, flagWidth, flagHeight, xproto.CwBackPixel, 0xffffff) if err != nil { errLg.Fatalf("Could not create window: %s", err) } // Make the window close gracefully using the WM_DELETE_WINDOW protocol. w.WMGracefulClose(func(w *xwindow.Window) { xevent.Detach(w.X, w.Id) keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() xevent.Quit(w.X) }) // Set WM_STATE so it is interpreted as top-level and is mapped. err = icccm.WmStateSet(w.X, w.Id, &icccm.WmState{ State: icccm.StateNormal, }) if err != nil { // not a fatal error lg("Could not set WM_STATE: %s", err) } // _NET_WM_STATE = _NET_WM_STATE_NORMAL ewmh.WmStateSet(w.X, w.Id, []string{"_NET_WM_STATE_NORMAL"}) // Set the name to something. w.nameSet("Decoding all images...") w.Map() }
// This is a slightly modified version of xgraphics.XShowExtra that does // not set any resize constraints on the window (so that it can go // fullscreen). func showImage(im *xgraphics.Image, name string, quit bool) *xwindow.Window { if len(name) == 0 { name = "xgbutil Image Window" } w, h := im.Rect.Dx(), im.Rect.Dy() win, err := xwindow.Generate(im.X) if err != nil { xgbutil.Logger.Printf("Could not generate new window id: %s", err) return nil } // Create a very simple window with dimensions equal to the image. win.Create(im.X.RootWin(), 0, 0, w, h, 0) // Make this window close gracefully. win.WMGracefulClose(func(w *xwindow.Window) { xevent.Detach(w.X, w.Id) keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() if quit { xevent.Quit(w.X) } }) // Set WM_STATE so it is interpreted as a top-level window. err = icccm.WmStateSet(im.X, win.Id, &icccm.WmState{ State: icccm.StateNormal, }) if err != nil { // not a fatal error xgbutil.Logger.Printf("Could not set WM_STATE: %s", err) } // Set _NET_WM_NAME so it looks nice. err = ewmh.WmNameSet(im.X, win.Id, name) if err != nil { // not a fatal error xgbutil.Logger.Printf("Could not set _NET_WM_NAME: %s", err) } // Paint our image before mapping. im.XSurfaceSet(win.Id) im.XDraw() im.XPaint(win.Id) // Now we can map, since we've set all our properties. // (The initial map is when the window manager starts managing.) win.Map() return win }
func bindall(configfile string, X *xgbutil.XUtil) (err error) { config, err := ioutil.ReadFile(configfile) if err != nil { log.Fatal("Could not find config file: ", err) return } hotkeys := []Hotkey{} err = json.Unmarshal(config, &hotkeys) if err != nil { log.Fatal("Could not parse config file:", err) return } keybind.Detach(X, X.RootWin()) for _, hotkey := range hotkeys { hotkey.attach(X) } return }
// newWindow creates the window, initializes the keybind and mousebind packages // and sets up the window to act like a real top-level client. func newWindow(X *xgbutil.XUtil) *Window { w, err := xwindow.Generate(X) if err != nil { errLg.Fatalf("Could not create window: %s", err) } keybind.Initialize(w.X) mousebind.Initialize(w.X) err = w.CreateChecked(w.X.RootWin(), 0, 0, 600, 600, xproto.CwBackPixel, 0xffffff) if err != nil { errLg.Fatalf("Could not create window: %s", err) } // Make the window close gracefully using the WM_DELETE_WINDOW protocol. w.WMGracefulClose(func(w *xwindow.Window) { xevent.Detach(w.X, w.Id) keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() xevent.Quit(w.X) }) // Set WM_STATE so it is interpreted as top-level and is mapped. err = icccm.WmStateSet(w.X, w.Id, &icccm.WmState{State: icccm.StateNormal}) if err != nil { lg("Could not set WM_STATE: %s", err) } // _NET_WM_STATE = _NET_WM_STATE_NORMAL // not needed because we we set FS later anyway? //ewmh.WmStateSet(w.X, w.Id, []string{"_NET_WM_STATE_NORMAL"}) w.Map() err = ewmh.WmStateReq(w.X, w.Id, ewmh.StateToggle, "_NET_WM_STATE_FULLSCREEN") if err != nil { lg("Failed to go FullScreen:", err) } return &Window{w} }
func NewWindow() *Window { win, err := xwindow.Generate(X) if err != nil { log.Fatal("cannot generate window %v\n", err) return nil } width, height := 800, 600 win.Create(X.RootWin(), 0, 0, width, height, xproto.CwBackPixel, 0x0) win.WMGracefulClose(func(w *xwindow.Window) { xevent.Detach(w.X, w.Id) keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() xevent.Quit(w.X) }) icccm.WmStateSet(X, win.Id, &icccm.WmState{ State: icccm.StateNormal, }) win.Listen(xproto.EventMaskKeyPress) win.Clear(0, 0, 0, 0) win.Map() self := &Window{ win, nil, false, nil, } self.bindKeys() return self }
// Detach will detach this window's event handlers from all xevent, keybind // and mousebind callbacks. func (w *Window) Detach() { keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) xevent.Detach(w.X, w.Id) }
func makeWindow(ximage *xgraphics.Image) (*xwindow.Window, *bool) { w, h := ximage.Rect.Dx(), ximage.Rect.Dy() window, err := xwindow.Generate(ximage.X) if err != nil { xgbutil.Logger.Printf("Could not generate new window id: %s", err) return nil, nil } window.Create(ximage.X.RootWin(), 0, 0, w, h, xproto.CwBackPixel, 0x00000000) window.Listen(xproto.EventMaskExposure, xproto.EventMaskKeyPress, xproto.EventMaskStructureNotify, xproto.EventMaskVisibilityChange) window.WMGracefulClose(func(w *xwindow.Window) { xevent.Detach(w.X, w.Id) keybind.Detach(w.X, w.Id) mousebind.Detach(w.X, w.Id) w.Destroy() xevent.Quit(w.X) }) err = icccm.WmStateSet(ximage.X, window.Id, &icccm.WmState{ State: icccm.StateNormal, }) if err != nil { xgbutil.Logger.Printf("Could not set WM_STATE: %s", err) } err = ewmh.WmNameSet(ximage.X, window.Id, "Computer System Monitor") if err != nil { xgbutil.Logger.Printf("Could not set _NET_WM_NAME: %s", err) } err = keybind.KeyPressFun( func(X *xgbutil.XUtil, ev xevent.KeyPressEvent) { err := ewmh.WmStateReq(ximage.X, window.Id, ewmh.StateToggle, "_NET_WM_STATE_FULLSCREEN") if err != nil { log.Fatal(err) } }).Connect(ximage.X, window.Id, "f", false) if err != nil { log.Fatal(err) } xevent.ExposeFun( func(xu *xgbutil.XUtil, event xevent.ExposeEvent) { ximage.XExpPaint(window.Id, 0, 0) }).Connect(ximage.X, window.Id) obscured := false xevent.VisibilityNotifyFun( func(xu *xgbutil.XUtil, event xevent.VisibilityNotifyEvent) { obscured = event.State == xproto.VisibilityFullyObscured }).Connect(ximage.X, window.Id) window.Map() return window, &obscured }
func main() { // Connect to the X server using the DISPLAY environment variable. X, err := xgbutil.NewConn() if err != nil { log.Fatal(err) } // Anytime the keybind (mousebind) package is used, keybind.Initialize // *should* be called once. It isn't strictly necessary, but allows your // keybindings to persist even if the keyboard mapping is changed during // run-time. (Assuming you're using the xevent package's event loop.) keybind.Initialize(X) // Before attaching callbacks, wrap them in a callback function type. // The keybind package exposes two such callback types: keybind.KeyPressFun // and keybind.KeyReleaseFun. cb1 := keybind.KeyPressFun( func(X *xgbutil.XUtil, e xevent.KeyPressEvent) { log.Println("Key press!") }) // We can now attach the callback to a particular window and key // combination. This particular example grabs a key on the root window, // which makes it a global keybinding. // Also, "Mod4-j" typically corresponds to pressing down the "Super" or // "Windows" key on your keyboard, and then pressing the letter "j". // N.B. This approach works by issuing a passive grab on the window // specified. To respond to Key{Press,Release} events without a grab, use // the xevent.Key{Press,Release}Fun callback function types instead. err = cb1.Connect(X, X.RootWin(), "Mod4-j", true) // A keybinding can fail if the key string could not be parsed, or if you're // trying to bind a key that has already been grabbed by another client. if err != nil { log.Fatal(err) } // We can even attach multiple callbacks to the same key. err = keybind.KeyPressFun( func(X *xgbutil.XUtil, e xevent.KeyPressEvent) { log.Println("A second handler always happens after the first.") }).Connect(X, X.RootWin(), "Mod4-j", true) if err != nil { log.Fatal(err) } // Finally, if we want this client to stop responding to key events, we // can attach another handler that, when run, detaches all previous // handlers. // This time, we'll show an example of a KeyRelease binding. err = keybind.KeyReleaseFun( func(X *xgbutil.XUtil, e xevent.KeyReleaseEvent) { // Use keybind.Detach to detach the root window // from all KeyPress *and* KeyRelease handlers. keybind.Detach(X, X.RootWin()) log.Printf("Detached all Key{Press,Release}Events from the "+ "root window (%d).", X.RootWin()) }).Connect(X, X.RootWin(), "Mod4-Shift-q", true) if err != nil { log.Fatal(err) } // Finally, start the main event loop. This will route any appropriate // KeyPressEvents to your callback function. log.Println("Program initialized. Start pressing keys!") xevent.Main(X) }