From 52c7a4fc7f243095fb116d6ac721a7590000e972 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Camilla=20L=C3=B6wy?= <elmindreda@elmindreda.org>
Date: Wed, 19 Dec 2018 14:53:01 +0100
Subject: [PATCH] Fix glfwGetGammaRamp error handling

This makes glfwGetGammaRamp return NULL on platform error as specified.

Related to #1387.
---
 src/cocoa_monitor.m |  3 ++-
 src/internal.h      |  2 +-
 src/monitor.c       |  8 ++++++--
 src/null_monitor.c  |  3 ++-
 src/win32_monitor.c |  4 +++-
 src/wl_monitor.c    |  3 ++-
 src/x11_monitor.c   | 15 ++++++++++++++-
 7 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m
index 986d799e..39fff6f7 100644
--- a/src/cocoa_monitor.m
+++ b/src/cocoa_monitor.m
@@ -467,7 +467,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode *mode)
     CVDisplayLinkRelease(link);
 }
 
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
 {
     uint32_t i, size = CGDisplayGammaTableCapacity(monitor->ns.displayID);
     CGGammaValue* values = calloc(size * 3, sizeof(CGGammaValue));
@@ -489,6 +489,7 @@ void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
     }
 
     free(values);
+    return GLFW_TRUE;
 }
 
 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
diff --git a/src/internal.h b/src/internal.h
index 30c7551a..c7c5bf8f 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -611,7 +611,7 @@ void _glfwPlatformGetMonitorContentScale(_GLFWmonitor* monitor,
                                          float* xscale, float* yscale);
 GLFWvidmode* _glfwPlatformGetVideoModes(_GLFWmonitor* monitor, int* count);
 void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode);
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp);
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp);
 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp);
 
 void _glfwPlatformSetClipboardString(const char* string);
diff --git a/src/monitor.c b/src/monitor.c
index c161ea11..dea45d6e 100644
--- a/src/monitor.c
+++ b/src/monitor.c
@@ -472,7 +472,8 @@ GLFWAPI const GLFWgammaramp* glfwGetGammaRamp(GLFWmonitor* handle)
     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
 
     _glfwFreeGammaArrays(&monitor->currentRamp);
-    _glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp);
+    if (!_glfwPlatformGetGammaRamp(monitor, &monitor->currentRamp))
+        return NULL;
 
     return &monitor->currentRamp;
 }
@@ -498,7 +499,10 @@ GLFWAPI void glfwSetGammaRamp(GLFWmonitor* handle, const GLFWgammaramp* ramp)
     _GLFW_REQUIRE_INIT();
 
     if (!monitor->originalRamp.size)
-        _glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp);
+    {
+        if (!_glfwPlatformGetGammaRamp(monitor, &monitor->originalRamp))
+            return;
+    }
 
     _glfwPlatformSetGammaRamp(monitor, ramp);
 }
diff --git a/src/null_monitor.c b/src/null_monitor.c
index 84b41c7e..45c4a10f 100644
--- a/src/null_monitor.c
+++ b/src/null_monitor.c
@@ -58,8 +58,9 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
 {
 }
 
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
 {
+    return GLFW_FALSE;
 }
 
 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
diff --git a/src/win32_monitor.c b/src/win32_monitor.c
index b7a24e61..07b3614b 100644
--- a/src/win32_monitor.c
+++ b/src/win32_monitor.c
@@ -455,7 +455,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
                   &mode->blueBits);
 }
 
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
 {
     HDC dc;
     WORD values[768];
@@ -469,6 +469,8 @@ void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
     memcpy(ramp->red,   values +   0, 256 * sizeof(unsigned short));
     memcpy(ramp->green, values + 256, 256 * sizeof(unsigned short));
     memcpy(ramp->blue,  values + 512, 256 * sizeof(unsigned short));
+
+    return GLFW_TRUE;
 }
 
 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
diff --git a/src/wl_monitor.c b/src/wl_monitor.c
index f033fb6e..588f8b0d 100644
--- a/src/wl_monitor.c
+++ b/src/wl_monitor.c
@@ -180,10 +180,11 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
     *mode = monitor->modes[monitor->wl.currentMode];
 }
 
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
 {
     _glfwInputError(GLFW_PLATFORM_ERROR,
                     "Wayland: Gamma ramp access it not available");
+    return GLFW_FALSE;
 }
 
 void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor,
diff --git a/src/x11_monitor.c b/src/x11_monitor.c
index f557fe47..df53041d 100644
--- a/src/x11_monitor.c
+++ b/src/x11_monitor.c
@@ -422,7 +422,7 @@ void _glfwPlatformGetVideoMode(_GLFWmonitor* monitor, GLFWvidmode* mode)
     }
 }
 
-void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
+GLFWbool _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
 {
     if (_glfw.x11.randr.available && !_glfw.x11.randr.gammaBroken)
     {
@@ -438,6 +438,7 @@ void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
         memcpy(ramp->blue,  gamma->blue,  size * sizeof(unsigned short));
 
         XRRFreeGamma(gamma);
+        return GLFW_TRUE;
     }
     else if (_glfw.x11.vidmode.available)
     {
@@ -449,6 +450,13 @@ void _glfwPlatformGetGammaRamp(_GLFWmonitor* monitor, GLFWgammaramp* ramp)
         XF86VidModeGetGammaRamp(_glfw.x11.display,
                                 _glfw.x11.screen,
                                 ramp->size, ramp->red, ramp->green, ramp->blue);
+        return GLFW_TRUE;
+    }
+    else
+    {
+        _glfwInputError(GLFW_PLATFORM_ERROR,
+                        "X11: Gamma ramp access not supported by server");
+        return GLFW_FALSE;
     }
 }
 
@@ -481,6 +489,11 @@ void _glfwPlatformSetGammaRamp(_GLFWmonitor* monitor, const GLFWgammaramp* ramp)
                                 (unsigned short*) ramp->green,
                                 (unsigned short*) ramp->blue);
     }
+    else
+    {
+        _glfwInputError(GLFW_PLATFORM_ERROR,
+                        "X11: Gamma ramp access not supported by server");
+    }
 }