internal/uidriver/glfw: Bug fix: Potential crash when xgb.NewConn fails

This commit is contained in:
Hajime Hoshi 2021-09-19 18:18:18 +09:00
parent 65c2fd4bcd
commit 0ac4c7323f

View File

@ -58,34 +58,35 @@ func videoModeScale(m *glfw.Monitor) float64 {
// videoModeScaleUncached must be called from the main thread. // videoModeScaleUncached must be called from the main thread.
func videoModeScaleUncached(m *glfw.Monitor, monitorX, monitorY int) float64 { func videoModeScaleUncached(m *glfw.Monitor, monitorX, monitorY int) float64 {
// TODO: if https://github.com/glfw/glfw/issues/1961 gets fixed, this function may need revising. // TODO: if glfw/glfw#1961 gets fixed, this function may need revising.
// In case GLFW decides to switch to returning logical pixels, we can just return 1. // In case GLFW decides to switch to returning logical pixels, we can just return 1.
// Note: GLFW currently returns physical pixel sizes, // Note: GLFW currently returns physical pixel sizes,
// but we need to predict the window system-side size of the fullscreen window // but we need to predict the window system-side size of the fullscreen window
// for our `ScreenSizeInFullscreen` public API. // for Ebiten's `ScreenSizeInFullscreen` public API.
// Also at the moment we need this prior to switching to fullscreen, but that might be replacable. // Also at the moment we need this prior to switching to fullscreen, but that might be replacable.
// So this function computes the ratio of physical per logical pixels. // So this function computes the ratio of physical per logical pixels.
xconn, err := xgb.NewConn() xconn, err := xgb.NewConn()
defer xconn.Close()
if err != nil { if err != nil {
// No X11 connection? // No X11 connection?
// Assume we're on pure Wayland then. // Assume we're on pure Wayland then.
// GLFW/Wayland shouldn't be having this issue. // GLFW/Wayland shouldn't be having this issue.
return 1 return 1
} }
defer xconn.Close()
if err = randr.Init(xconn); err != nil { if err = randr.Init(xconn); err != nil {
// No RANDR extension? // No RANDR extension? No problem.
// No problem.
return 1 return 1
} }
root := xproto.Setup(xconn).DefaultScreen(xconn).Root root := xproto.Setup(xconn).DefaultScreen(xconn).Root
res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply() res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply()
if err != nil { if err != nil {
// Likely means RANDR is not working. // Likely means RANDR is not working. No problem.
// No problem.
return 1 return 1
} }
for _, crtc := range res.Crtcs[:res.NumCrtcs] { for _, crtc := range res.Crtcs[:res.NumCrtcs] {
info, err := randr.GetCrtcInfo(xconn, crtc, res.ConfigTimestamp).Reply() info, err := randr.GetCrtcInfo(xconn, crtc, res.ConfigTimestamp).Reply()
if err != nil { if err != nil {
@ -107,6 +108,7 @@ func videoModeScaleUncached(m *glfw.Monitor, monitorX, monitorY int) float64 {
return scale return scale
} }
} }
// Monitor not known to XRandR. Weird. // Monitor not known to XRandR. Weird.
return 1 return 1
} }