Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an explanation of how to use events to the create-window example #1

Merged
merged 19 commits into from
Mar 7, 2021
Merged
39 changes: 38 additions & 1 deletion examples/create-window/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
fmt.Println(err)
return
}
defer X.Close()
jezek marked this conversation as resolved.
Show resolved Hide resolved

// xproto.Setup retrieves the Setup information from the setup bytes
// gathered during connection.
Expand Down Expand Up @@ -49,7 +50,8 @@ func main() {
0xffffffff,
xproto.EventMaskStructureNotify |
xproto.EventMaskKeyPress |
xproto.EventMaskKeyRelease})
xproto.EventMaskKeyRelease,
})

// MapWindow makes the window we've created appear on the screen.
// We demonstrated the use of a 'checked' request here.
Expand Down Expand Up @@ -103,5 +105,40 @@ func main() {
if xerr != nil {
fmt.Printf("Error: %s\n", xerr)
}

// This is how accepting events work:
// The application checks what event we got
// (the event must be registered using either xproto.CreateWindow (see l.35) or
// xproto.ChangeWindowAttributes (see l.50)).
scrouthtv marked this conversation as resolved.
Show resolved Hide resolved
// and reacts to it accordingly. All events are defined in the xproto subpackage.
switch ev.(type) {
case xproto.KeyPressEvent:
// See https://pkg.go.dev/github.com/jezek/xgb@v0.0.0-20210121230032-cec22bda1ce1/xproto#KeyPressEvent
scrouthtv marked this conversation as resolved.
Show resolved Hide resolved
// for documentation about a key press event.
kpe := ev.(xproto.KeyPressEvent)
fmt.Printf("Key pressed: %d", kpe.Detail)
// The Detail value depends on the keyboard layout,
// for QWERTY, q is #24.
if kpe.Detail == 24 {
return // exit on q
}
case xproto.DestroyNotifyEvent:
scrouthtv marked this conversation as resolved.
Show resolved Hide resolved
// Depending on the user's desktop environment (especially
// window manager), killing a window might close the
// client's X connection (e. g. the default Ubuntu
// desktop environment).
//
// If that's the case for your environment, closing this example's window
// will also call the underlying Go program (because closing the X
scrouthtv marked this conversation as resolved.
Show resolved Hide resolved
// connection gives a nil event and EOF error).
scrouthtv marked this conversation as resolved.
Show resolved Hide resolved
//
// Consider how a single application might have multiple windows
// (e.g. an open popup or dialog, ...)
//
// With other DEs, the X connection will still stay open even after the
// X window is closed. For these DEs (e.g. i3) we have to check whether
// the WM sent us a DestroyNotifyEvent and close our program:
return
}
}
}