From 98a50ede9ec1d9a7da5628adfba4ffcb2c54be42 Mon Sep 17 00:00:00 2001
From: Richard Hughes <richard@hughsie.com>
Date: Mon, 19 Jan 2009 14:13:31 +0000
Subject: [PATCH] According to the following bug report the gnome settings daemon causes

2009-01-19  Richard Hughes  <richard@hughsie.com>

* src/gpm-brightness-xrandr.c:
(gpm_brightness_xrandr_update_cache):
According to the following bug report the gnome settings daemon causes
high CPU usage with libxrandr 1.2.99.2 or higher:
https://bugs.edge.launchpad.net/ubuntu/+source/libxrandr/+bug/307306

The problem is that it calls XRRGetScreenResources() (which is an rather
expensive call) instead of XRRGetScreenResourcesCurrent() which is
cheaper and doesn't cause hardware to be reprobed.

While I agree that listening to events which represent changes in output
properties is ideal, calling XRRGetScreenResources each time a property
(e.g. the backlight) changes is really expensive and (if RandR 1.3 is
available) this can be avoided.

Patch from Alberto Milone <albertomilone@alice.it>, many thanks.

svn path=/trunk/; revision=3167

From 879f753df7126f41e581e95e6654c301be930d3f Mon Sep 17 00:00:00 2001
From: Richard Hughes <richard@hughsie.com>
Date: Mon, 19 Jan 2009 16:20:49 +0000
Subject: [PATCH] This patch relies on randr.h i.e. on the RandR procol rather than on the

2009-01-19  Richard Hughes  <richard@hughsie.com>

* src/gpm-brightness-xrandr.c:
(gpm_brightness_xrandr_update_cache):
This patch relies on randr.h i.e. on the RandR procol rather than on the
xserver itself. This will prevent us from adding the xserver development
libraries as build dependencies (thus preserving the previous behaviour).

Patch from Alberto Milone <albertomilone@alice.it>, many thanks.

svn path=/trunk/; revision=3168

From 4c54d0583dd1f80d3a4447c06276069a772eef80 Mon Sep 17 00:00:00 2001
From: Priit Laes <plaes@plaes.org>
Date: Sun, 10 May 2009 21:34:30 +0300
Subject: [PATCH] Check whether xrandr-1.3 features are really supported by xorg-server.
 Fixes #582142.

---
 src/gpm-brightness-xrandr.c |   49 ++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 47 insertions(+), 2 deletions(-)

--- a/src/gpm-brightness-xrandr.c
+++ b/src/gpm-brightness-xrandr.c
@@ -58,8 +58,11 @@
 	Display			*dpy;
 	guint			 shared_value;
 	gboolean		 has_extension;
+#if (RANDR_MAJOR == 1 && RANDR_MINOR >= 3)
+	gboolean		 has_randr13;
+#endif
 	gboolean		 hw_changed;
-	/* An cache of XRRScreenResources is used as XRRGetScreenResources is expensive */
+	/* A cache of XRRScreenResources is used as XRRGetScreenResources is expensive */
 	GPtrArray		*resources;
 };
 
@@ -173,6 +176,34 @@
 	return TRUE;
 }
 
+#if (RANDR_MAJOR == 1 && RANDR_MINOR >= 3)
+/**
+ * gpm_brightness_xrandr_setup_version: Check whether xserver really supports xrandr-1.3 features.
+ **/
+static gboolean
+gpm_brightness_xrandr_setup_version (GpmBrightnessXRandR *brightness)
+{
+	gint major, minor;
+
+	g_return_val_if_fail (GPM_IS_BRIGHTNESS_XRANDR (brightness), FALSE);
+
+	/* get the display */
+	brightness->priv->dpy = GDK_DISPLAY();
+	if (!brightness->priv->dpy) {
+		egg_error ("Cannot open display");
+		return FALSE;
+	}
+	if (!XRRQueryVersion (brightness->priv->dpy, &major, &minor)) {
+		return FALSE;
+	}
+	if (major == 1 && minor < 3) {
+		egg_debug ("RandR version %d.%d does not support XRRGetScreenResourcesCurrent", major, minor);
+		return FALSE;
+	}
+	return TRUE;
+}
+#endif
+
 /**
  * gpm_brightness_xrandr_output_get_limits:
  **/
@@ -627,9 +658,20 @@
 					  G_CALLBACK (gpm_brightness_monitors_changed), brightness);
 		}
 
-		/* this is slow */
 		root = RootWindow (brightness->priv->dpy, screen);
+		/* XRRGetScreenResourcesCurrent is less expensive than
+		   XRRGetScreenResources, however it is available only
+		   in RandR 1.3 or higher and of course xserver needs
+		   to support it.
+		*/
+#if (RANDR_MAJOR == 1 && RANDR_MINOR >= 3)
+		if (brightness->priv->has_randr13)
+			resource = XRRGetScreenResourcesCurrent (brightness->priv->dpy, root);
+		else
+			resource = XRRGetScreenResources (brightness->priv->dpy, root);
+#else
 		resource = XRRGetScreenResources (brightness->priv->dpy, root);
+#endif
 		if (resource != NULL) {
 			egg_debug ("adding resource %p", resource);
 			g_ptr_array_add (brightness->priv->resources, resource);
@@ -694,6 +736,9 @@
 	brightness->priv->has_extension = gpm_brightness_xrandr_setup_display (brightness);
 	if (brightness->priv->has_extension == FALSE)
 		return;
+#if (RANDR_MAJOR == 1 && RANDR_MINOR >= 3)
+	brightness->priv->has_randr13 = gpm_brightness_xrandr_setup_version (brightness);
+#endif
 
 	screen = gdk_screen_get_default ();
 	window = gdk_screen_get_root_window (screen);
