Description: Read display scaling from gsettings.
Author: Chad MILLER <chad.miller@canonical.com>
Forwarded: yes

Index: dev.xenial/build/linux/system.gyp
===================================================================
--- dev.xenial.orig/build/linux/system.gyp
+++ dev.xenial/build/linux/system.gyp
@@ -34,7 +34,12 @@
       'g_settings_get_boolean',
       'g_settings_get_int',
       'g_settings_get_strv',
+      'g_settings_new_full',
       'g_settings_list_schemas',
+      'g_settings_schema_source_get_default',
+      'g_settings_schema_source_lookup',
+      'g_settings_get',
+      'g_variant_lookup',
     ],
     'libpci_functions': [
       'pci_alloc',
Index: dev.xenial/ui/views/BUILD.gn
===================================================================
--- dev.xenial.orig/ui/views/BUILD.gn
+++ dev.xenial/ui/views/BUILD.gn
@@ -270,6 +270,7 @@ test("views_unittests") {
       "//ui/events/devices",
       "//ui/events/platform/x11",
       "//ui/gfx/x",
+      "//build/linux:gio",
     ]
   }
 
Index: dev.xenial/ui/views/views.gyp
===================================================================
--- dev.xenial.orig/ui/views/views.gyp
+++ dev.xenial/ui/views/views.gyp
@@ -764,6 +764,7 @@
           'dependencies': [
             '../../build/linux/system.gyp:x11',
             '../../build/linux/system.gyp:xrandr',
+            '../../build/linux/system.gyp:gio',
             '../events/devices/events_devices.gyp:events_devices',
             '../events/devices/x11/events_devices_x11.gyp:events_devices_x11',
             '../events/keycodes/events_keycodes.gyp:keycodes_x11',
Index: dev.xenial/ui/views/widget/desktop_aura/desktop_screen_x11.cc
===================================================================
--- dev.xenial.orig/ui/views/widget/desktop_aura/desktop_screen_x11.cc
+++ dev.xenial/ui/views/widget/desktop_aura/desktop_screen_x11.cc
@@ -7,7 +7,12 @@
 #include <X11/extensions/Xrandr.h>
 #include <X11/Xlib.h>
 
-// It clashes with out RootWindow.
+#ifdef USE_GLIB
+#include <gio/gio.h>
+#include <glib.h>
+#endif
+
+// It clashes with our RootWindow.
 #undef RootWindow
 
 #include "base/logging.h"
@@ -298,6 +303,29 @@ std::vector<gfx::Display> DesktopScreenX
     return GetFallbackDisplayList();
   }
 
+#ifdef USE_GLIB
+  GSettingsSchemaSource* gsettings_schema_source =
+      g_settings_schema_source_get_default();
+  GSettingsSchema* gsettings_schema =
+      g_settings_schema_source_lookup(gsettings_schema_source,
+                                      "com.ubuntu.user-interface", TRUE);
+
+  GVariant* display_scales = NULL;
+  if (gsettings_schema != NULL) {
+    GSettings* gsettings = NULL;
+    gsettings = g_settings_new_full(gsettings_schema, NULL, NULL);
+
+    if (gsettings != NULL) {
+      g_settings_get(gsettings, "scale-factor", "@a{si}", &display_scales);
+      DVLOG(1) << "Got com.ubuntu.desktop gsettings.";
+    } else {
+      DVLOG(1) << "No com.ubuntu.desktop gsettings available.";
+    }
+  } else {
+    DVLOG(1) << "No com.ubuntu.desktop gsettings schema available.";
+  }
+#endif
+
   bool has_work_area = false;
   gfx::Rect work_area_in_pixels;
   std::vector<int> value;
@@ -309,7 +337,7 @@ std::vector<gfx::Display> DesktopScreenX
 
   // As per-display scale factor is not supported right now,
   // the X11 root window's scale factor is always used.
-  const float device_scale_factor = GetDeviceScaleFactor();
+  float device_scale_factor = GetDeviceScaleFactor();
   for (int i = 0; i < resources->noutput; ++i) {
     RROutput output_id = resources->outputs[i];
     gfx::XScopedPtr<XRROutputInfo,
@@ -336,7 +364,27 @@ std::vector<gfx::Display> DesktopScreenX
       gfx::Rect crtc_bounds(crtc->x, crtc->y, crtc->width, crtc->height);
       gfx::Display display(display_id, crtc_bounds);
 
-      if (!gfx::Display::HasForceDeviceScaleFactor()) {
+      // An integer that forces discrete steps.
+      int density_indicator = 0;
+#ifdef USE_GLIB
+      if (display_scales != NULL) {
+        (void) g_variant_lookup(display_scales, output_info->name, "i",
+                                &density_indicator);
+        DCHECK_LE(0, density_indicator);
+        DVLOG(1) << "Got density indictor " << density_indicator << " from display_scales for " << output_info->name;
+      }
+#else
+      VLOG(1) << "Not using gsettings to get display scale info. No use_glib";
+#endif
+
+      if (density_indicator != 0) {
+        // n/8 is actual scaling factor.  Zero means discover from hardware.
+        device_scale_factor = float(density_indicator / 8.0);
+        DVLOG(1) << "Set " << output_info->name << " screen scaling to "
+                 << device_scale_factor << " from gsettings.";
+        display.SetScaleAndBounds(device_scale_factor, crtc_bounds);
+      } else if (!gfx::Display::HasForceDeviceScaleFactor()) {
+        DVLOG(1) << "Didn't use gsettings info at all.  Picked " << device_scale_factor;
         display.SetScaleAndBounds(device_scale_factor, crtc_bounds);
       }
 
