diff --git a/glfw/cocoa_monitor.m b/glfw/cocoa_monitor.m index 4f4be9158..eab8ff9d0 100644 --- a/glfw/cocoa_monitor.m +++ b/glfw/cocoa_monitor.m @@ -618,9 +618,13 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count) return result; } -void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode *mode) +bool _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode *mode) { CGDisplayModeRef native = CGDisplayCopyDisplayMode(monitor->ns.displayID); + if (!native) { + _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to query display mode"); + return false; + } *mode = vidmodeFromCGDisplayMode(native, monitor->ns.fallbackRefreshRate); CGDisplayModeRelease(native); } diff --git a/glfw/internal.h b/glfw/internal.h index 428839d9e..7af6ec000 100644 --- a/glfw/internal.h +++ b/glfw/internal.h @@ -686,7 +686,7 @@ void _glfwPlatformGetMonitorContentScale(_GLFWmonitor* monitor, float* xscale, float* yscale); void _glfwPlatformGetMonitorWorkarea(_GLFWmonitor* monitor, int* xpos, int* ypos, int *width, int *height); GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count); -void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode); +bool _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode); bool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp); void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp); diff --git a/glfw/monitor.c b/glfw/monitor.c index c25148e3c..423d2c83b 100644 --- a/glfw/monitor.c +++ b/glfw/monitor.c @@ -445,7 +445,7 @@ GLFWAPI const GLFWvidmode* glfwGetVideoMode(GLFWmonitor* handle) _GLFW_REQUIRE_INIT_OR_RETURN(NULL); - _glfwPlatformGetVideoMode(monitor, &monitor->currentMode); + if (!_glfwPlatformGetVideoMode(monitor, &monitor->currentMode)) return NULL; return &monitor->currentMode; } diff --git a/glfw/wl_monitor.c b/glfw/wl_monitor.c index 5b3a7f149..15cc0bc14 100644 --- a/glfw/wl_monitor.c +++ b/glfw/wl_monitor.c @@ -195,9 +195,13 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* found) return monitor->modes; } -void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) +bool _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) { - *mode = monitor->modes[monitor->wl.currentMode]; + if (monitor->modeCount > monitor->wl.currentMode) { + *mode = monitor->modes[monitor->wl.currentMode]; + return true; + } + return false; } bool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor UNUSED, GLFWgammaramp* ramp UNUSED) diff --git a/glfw/x11_monitor.c b/glfw/x11_monitor.c index 26b42e293..6c594fbe8 100644 --- a/glfw/x11_monitor.c +++ b/glfw/x11_monitor.c @@ -494,8 +494,8 @@ GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count) return result; } -void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) -{ +bool _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) { + bool ok = false; if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken) { XRRScreenResources* sr = @@ -505,8 +505,10 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) if (ci) { const XRRModeInfo* mi = getModeInfo(sr, ci->mode); - if (mi) // mi can be NULL if the monitor has been disconnected + if (mi) { // mi can be NULL if the monitor has been disconnected *mode = vidmodeFromModeInfo(mi, ci); + ok = true; + } XRRFreeCrtcInfo(ci); } @@ -515,6 +517,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) } else { + ok = true; mode->width = DisplayWidth(_glfw.x11.display, _glfw.x11.screen); mode->height = DisplayHeight(_glfw.x11.display, _glfw.x11.screen); mode->refreshRate = 0; @@ -522,6 +525,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode) _glfwSplitBPP(DefaultDepth(_glfw.x11.display, _glfw.x11.screen), &mode->redBits, &mode->greenBits, &mode->blueBits); } + return ok; } bool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp) diff --git a/kitty/glfw.c b/kitty/glfw.c index 1f73c3a9f..082bf03aa 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -1823,6 +1823,7 @@ static PyObject* primary_monitor_size(PYNOARG) { GLFWmonitor* monitor = glfwGetPrimaryMonitor(); const GLFWvidmode* mode = glfwGetVideoMode(monitor); + if (mode == NULL) { PyErr_SetString(PyExc_ValueError, "Failed to get video mode for primary monitor"); return NULL; } return Py_BuildValue("ii", mode->width, mode->height); }