From c60221e0b4187a6eaa34dd1ec31e92cccab57349 Mon Sep 17 00:00:00 2001
From: Giovanni Campagna <gcampagna@src.gnome.org>
Date: Sat, 12 Jan 2013 16:43:21 +0000
Subject: Don't use glxinfo to access the graphic driver name

glxinfo is part of mesa-demos, and installing it pulls a lot of unnecessary
programs. We can get the same informations by querying the driver directly.

https://bugzilla.gnome.org/show_bug.cgi?id=691613
---
--- a/configure.ac
+++ b/configure.ac
@@ -136,7 +136,7 @@
                   polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION
                   gdk-pixbuf-2.0 >= $GDKPIXBUF_REQUIRED_VERSION)
 PKG_CHECK_MODULES(DISPLAY_PANEL, $COMMON_MODULES gnome-desktop-3.0 >= 3.1.0)
-PKG_CHECK_MODULES(INFO_PANEL, $COMMON_MODULES libgtop-2.0
+PKG_CHECK_MODULES(INFO_PANEL, $COMMON_MODULES libgtop-2.0 gl x11
 		  polkit-gobject-1 >= $POLKIT_REQUIRED_VERSION)
 PKG_CHECK_MODULES(KEYBOARD_PANEL, $COMMON_MODULES
                   gnome-desktop-3.0 >= $GNOME_DESKTOP_REQUIRED_VERSION
--- a/panels/info/cc-info-panel.c
+++ b/panels/info/cc-info-panel.c
@@ -36,6 +36,10 @@
 #include <glibtop/mem.h>
 #include <glibtop/sysinfo.h>
 
+#include <gdk/gdkx.h>
+#include <GL/gl.h>
+#include <GL/glx.h>
+
 #include "hostname-helper.h"
 #include "gsd-disk-space-helper.h"
 
@@ -301,54 +305,70 @@
 }
 
 static char *
-get_graphics_data_glx_renderer (void)
+get_graphics_data_glx_renderer ()
 {
-  GError     *error;
-  GRegex     *re;
-  GMatchInfo *match_info;
-  char       *output;
-  char       *result;
-  GString    *info;
-
-  info = g_string_new (NULL);
-
-  error = NULL;
-  g_spawn_command_line_sync ("glxinfo -l", &output, NULL, NULL, &error);
-  if (error != NULL)
-    {
-      g_warning ("Unable to get graphics info: %s", error->message);
-      g_error_free (error);
-      return NULL;
-    }
-
-  re = g_regex_new ("^OpenGL renderer string: (.+)$", G_REGEX_MULTILINE, 0, &error);
-  if (re == NULL)
-    {
-      g_warning ("Error building regex: %s", error->message);
-      g_error_free (error);
-      goto out;
-    }
-
-  g_regex_match (re, output, 0, &match_info);
-  while (g_match_info_matches (match_info))
-    {
-      char *device;
-
-      device = g_match_info_fetch (match_info, 1);
-      g_string_append_printf (info, "%s ", device);
-      g_free (device);
-
-      g_match_info_next (match_info, NULL);
-    }
-  g_match_info_free (match_info);
-  g_regex_unref (re);
-
- out:
-  g_free (output);
-  result = prettify_info (info->str);
-  g_string_free (info, TRUE);
+  Display *display;
+  int attributes[] = {
+    GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
+    GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
+    GLX_RENDER_TYPE, GLX_RGBA_BIT,
+    None
+  };
+  int nconfigs;
+  int major, minor;
+  Window window;
+  GLXFBConfig *config;
+  GLXWindow glxwin;
+  GLXContext context;
+  XSetWindowAttributes win_attributes;
+  XVisualInfo *visualInfo;
+  char *renderer;
+
+  gdk_error_trap_push ();
+
+  display = GDK_DISPLAY_XDISPLAY (gdk_display_get_default ());
+
+  glXQueryVersion (display, &major, &minor);
+  config = glXChooseFBConfig (display, DefaultScreen (display),
+                              attributes, &nconfigs);
+  if (config == NULL) {
+    g_warning ("Failed to get OpenGL configuration");
+
+    gdk_error_trap_pop_ignored ();
+    return NULL;
+  }
+  visualInfo = glXGetVisualFromFBConfig (display, *config);
+  win_attributes.colormap = XCreateColormap (display, DefaultRootWindow(display),
+                                        visualInfo->visual, AllocNone );
+
+  window = XCreateWindow (display, DefaultRootWindow (display),
+                                0, 0, /* x, y */
+                                1, 1, /* width, height */
+                                0,   /* border_width */
+                                visualInfo->depth, InputOutput,
+                                visualInfo->visual, CWColormap, &win_attributes);
+  glxwin = glXCreateWindow (display, *config, window, NULL);
+
+  context = glXCreateNewContext (display, *config, GLX_RGBA_TYPE,
+                                 NULL, TRUE);
+  XFree (config);
+
+  glXMakeContextCurrent (display, glxwin, glxwin, context);
+  renderer = (char *) glGetString (GL_RENDERER);
+  renderer = renderer ? prettify_info (renderer) : NULL;
+
+  glXMakeContextCurrent (display, None, None, NULL);
+  glXDestroyContext (display, context);
+  glXDestroyWindow (display, glxwin);
+  XDestroyWindow (display, window);
+  XFree (visualInfo);
+
+  if (gdk_error_trap_pop () != Success) {
+    g_warning ("Failed to get OpenGL driver info");
+    return NULL;
+  }
 
-  return result;
+  return renderer;
 }
 
 static char *
