From 2d8896857441a51b9ac14c76a39662c9ee08d566 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=89ric=20Piel?= <eric.piel@tremplin-utc.net>
Date: Wed, 6 Jan 2010 14:42:15 +0100
Subject: [PATCH] xrandr: fix brightness to prevent gamma to overflow and to allow 0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

With the new brightness option, gamma would overflow with values > 1,
leading to rainbow looking screen.

In addition, have the brightness by default to 1, so that specifying 0
actually does the expected behaviour of leading to a black screen.

Signed-off-by: Éric Piel <eric.piel@tremplin-utc.net>
---
 xrandr.c |   16 +++++++---------
 1 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/xrandr.c b/xrandr.c
index 2fc0b81..17715ae 100644
--- a/xrandr.c
+++ b/xrandr.c
@@ -626,6 +626,7 @@ add_output (void)
 	fatal ("out of memory\n");
     output->next = NULL;
     output->found = False;
+    output->brightness = 1.0;
     *outputs_tail = output;
     outputs_tail = &output->next;
     return output;
@@ -1216,30 +1217,27 @@ set_gamma(void)
 	if(output->gamma.red == 0.0 && output->gamma.green == 0.0 && output->gamma.blue == 0.0)
 	    output->gamma.red = output->gamma.green = output->gamma.blue = 1.0;
 
-	if (output->brightness == 0.0)
-	    output->brightness = 1.0;
-
 	for (i = 0; i < size; i++) {
 	    if (output->gamma.red == 1.0 && output->brightness == 1.0)
 		gamma->red[i] = i << 8;
 	    else
-		gamma->red[i] = (CARD16)(pow((double)i/(double)(size - 1),
+		gamma->red[i] = (CARD16)(fmin(pow((double)i/(double)(size - 1),
 			    (double)output->gamma.red) * (double)(size - 1)
-			    * (double)output->brightness * 256);
+			    * (double)output->brightness * 256, 65535));
 
 	    if (output->gamma.green == 1.0 && output->brightness == 1.0)
 		gamma->green[i] = i << 8;
 	    else
-		gamma->green[i] = (CARD16)(pow((double)i/(double)(size - 1),
+		gamma->green[i] = (CARD16)(fmin(pow((double)i/(double)(size - 1),
 			    (double)output->gamma.green) * (double)(size - 1)
-			    * (double)output->brightness * 256);
+			    * (double)output->brightness * 256, 65535));
 
 	    if (output->gamma.blue == 1.0 && output->brightness == 1.0)
 		gamma->blue[i] = i << 8;
 	    else
-		gamma->blue[i] = (CARD16)(pow((double)i/(double)(size - 1),
+		gamma->blue[i] = (CARD16)(fmin(pow((double)i/(double)(size - 1),
 			    (double)output->gamma.blue) * (double)(size - 1)
-			    * (double)output->brightness * 256);
+			    * (double)output->brightness * 256, 65535));
 	}
 
 	XRRSetCrtcGamma(dpy, crtc->crtc.xid, gamma);
-- 
1.6.6
