Created
December 2, 2014 08:21
-
-
Save fourdollars/e27d798ced91f680a22e to your computer and use it in GitHub Desktop.
gcc -Wall -g unity-scale-factor.c `pkg-config --cflags --libs gio-2.0 xrandr x11` -lm -o unity-scale-factor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*- */ | |
/** | |
* Copyright (C) 2014 Shih-Yuan Lee (FourDollars) <fourdollars@gmail.com> | |
* | |
* This program is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
#include <stdio.h> | |
#include <math.h> | |
#include <gio/gio.h> | |
#include <X11/extensions/Xrandr.h> | |
static GVariant* | |
add_dict_entry (GVariant *dict, const char *key, int value) | |
{ | |
GVariantBuilder builder; | |
GVariantIter iter; | |
const gchar *k; | |
guint32 v; | |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{si}")); | |
g_variant_iter_init (&iter, dict); | |
while (g_variant_iter_next (&iter, "{&si}", &k, &v)) | |
{ | |
if (!g_str_equal (k, key)) | |
g_variant_builder_add (&builder, "{si}", k, v); | |
} | |
g_variant_builder_add (&builder, "{si}", key, value); | |
return g_variant_builder_end (&builder); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
int i; | |
Display *display = XOpenDisplay (NULL); | |
if (!display) | |
return 0; | |
Window window = RootWindow (display, XDefaultScreen (display)); | |
XRRScreenResources *resources = XRRGetScreenResourcesCurrent (display, window); | |
if (!resources) | |
return 0; | |
GSettingsSchemaSource *source = g_settings_schema_source_get_default (); | |
GSettingsSchema *schema = g_settings_schema_source_lookup (source, "com.ubuntu.user-interface", FALSE); | |
GSettings *settings = g_settings_new ("com.ubuntu.user-interface"); | |
GVariant *dict = g_settings_get_value (settings, "scale-factor"); | |
for (i = 0; i < resources->noutput; ++i) { | |
XRROutputInfo *output = XRRGetOutputInfo (display, resources, resources->outputs[i]); | |
if (output->connection == RR_Disconnected) { | |
XRRFreeOutputInfo (output); | |
continue; | |
} | |
if (output->crtc) { | |
XRRCrtcInfo *crtc = XRRGetCrtcInfo (display, resources, output->crtc); | |
double diagonal = sqrt (crtc->width * crtc->width + crtc->height * crtc->height); | |
double diagonal_mm = sqrt (output->mm_width * output->mm_width + output->mm_height * output->mm_height); | |
int ppi = (int) floor (diagonal * 25.4 / diagonal_mm); | |
int scale = 8; | |
if (ppi >= 192 && crtc->height >= 1200) | |
scale = 16; | |
GVariant *new_dict = add_dict_entry (dict, output->name, scale); | |
g_variant_unref (dict); | |
dict = new_dict; | |
XRRFreeCrtcInfo (crtc); | |
} | |
XRRFreeOutputInfo (output); | |
} | |
g_settings_set_value (settings, "scale-factor", dict); | |
g_settings_apply (settings); | |
g_settings_sync (); | |
g_object_unref (settings); | |
g_settings_schema_unref (schema); | |
XRRFreeScreenResources (resources); | |
XCloseDisplay (display); | |
return 0; | |
} | |
/* vim:set fileencodings=utf-8 tabstop=4 expandtab shiftwidth=4 softtabstop=4: */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment