diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 70c38859f..273b25287 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -169,37 +169,12 @@ func initialize() error { glfw.WindowHint(glfw.Visible, glfw.False) glfw.WindowHint(glfw.ClientAPI, glfw.NoAPI) - var m *glfw.Monitor - if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { - var err error - m, err = initialMonitorByOS() - if err != nil { - return err - } - if m == nil { - m = glfw.GetPrimaryMonitor() - } - } else { - // Create a window to set the initial monitor. - // TODO: Instead of a dummy window, get a mouse cursor position and get a monitor from it (#1982). - w, err := glfw.CreateWindow(16, 16, "", nil, nil) - if err != nil { - return err - } - if w == nil { - // This can happen on Windows Remote Desktop (#903). - panic("ui: glfw.CreateWindow must not return nil") - } - defer w.Destroy() - initializeWindowAfterCreation(w) - theUI.waitForFramebufferSizeCallback(w, nil) - m, err = initialMonitorByOS() - if err != nil { - return err - } - if m == nil { - m = currentMonitorImpl(w) - } + m, err := initialMonitorByOS() + if err != nil { + return err + } + if m == nil { + m = glfw.GetPrimaryMonitor() } theUI.initMonitor = m diff --git a/internal/ui/ui_glfw_unix.go b/internal/ui/ui_glfw_unix.go index eb7b70fd9..00fee87b4 100644 --- a/internal/ui/ui_glfw_unix.go +++ b/internal/ui/ui_glfw_unix.go @@ -134,6 +134,28 @@ func (u *UserInterface) adjustWindowPosition(x, y int, monitor *glfw.Monitor) (i } func initialMonitorByOS() (*glfw.Monitor, error) { + xconn, err := xgb.NewConn() + if err != nil { + // Assume we're on pure Wayland then. + return nil, nil + } + defer xconn.Close() + + root := xproto.Setup(xconn).DefaultScreen(xconn).Root + rep, err := xproto.QueryPointer(xconn, root).Reply() + if err != nil { + return nil, err + } + x, y := int(rep.RootX), int(rep.RootY) + + // Find the monitor including the cursor. + for _, m := range ensureMonitors() { + w, h := m.vm.Width, m.vm.Height + if x >= m.x && x < m.x+w && y >= m.y && y < m.y+h { + return m.m, nil + } + } + return nil, nil }