1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "systemScale.h" 25 #include "jni.h" 26 #include "jni_util.h" 27 #include "jvm_md.h" 28 #include <dlfcn.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 33 typedef void* g_settings_schema_source_get_default(); 34 typedef void* g_settings_schema_source_ref(void *); 35 typedef void g_settings_schema_source_unref(void *); 36 typedef void* g_settings_schema_source_lookup(void *, char *, int); 37 typedef int g_settings_schema_has_key(void *, char *); 38 typedef void* g_settings_new_full(void *, void *, char *); 39 typedef void* g_settings_get_value(void *, char *); 40 typedef int g_variant_is_of_type(void *, char *); 41 typedef unsigned long g_variant_n_children(void *); 42 typedef void* g_variant_get_child_value(void *, unsigned long); 43 typedef void g_variant_unref(void *); 44 typedef char* g_variant_get_string(void *, unsigned long *); 45 typedef int g_variant_get_int32(void *); 46 typedef double g_variant_get_double(void *); 47 48 static g_settings_schema_has_key* fp_g_settings_schema_has_key; 49 static g_settings_new_full* fp_g_settings_new_full; 50 static g_settings_get_value* fp_g_settings_get_value; 51 static g_variant_is_of_type* fp_g_variant_is_of_type; 52 static g_variant_n_children* fp_g_variant_n_children; 53 static g_variant_get_child_value* fp_g_variant_get_child_value; 54 static g_variant_get_string* fp_g_variant_get_string; 55 static g_variant_get_int32* fp_g_variant_get_int32; 56 static g_variant_get_double* fp_g_variant_get_double; 57 static g_variant_unref* fp_g_variant_unref; 58 59 static void* get_schema_value(char *name, char *key) { 60 static void *lib_handle; 61 static int initialized = 0; 62 static void * default_schema; 63 static g_settings_schema_source_lookup* schema_lookup; 64 void *schema = NULL, *fp = NULL; 65 if (!initialized) { 66 initialized = 1; 67 lib_handle = dlopen(JNI_LIB_NAME("gio-2.0"), RTLD_GLOBAL | RTLD_LAZY); 68 if (!lib_handle) { 69 CHECK_NULL_RETURN(lib_handle = 70 dlopen(VERSIONED_JNI_LIB_NAME("gio-2.0", "0"), 71 RTLD_GLOBAL | RTLD_LAZY), NULL); 72 } 73 CHECK_NULL_RETURN(fp_g_settings_schema_has_key = 74 (g_settings_schema_has_key*) 75 dlsym(lib_handle, "g_settings_schema_has_key"), NULL); 76 CHECK_NULL_RETURN(fp_g_settings_new_full = 77 (g_settings_new_full*) 78 dlsym(lib_handle, "g_settings_new_full"), NULL); 79 CHECK_NULL_RETURN(fp_g_settings_get_value = 80 (g_settings_get_value*) 81 dlsym(lib_handle, "g_settings_get_value"), NULL); 82 CHECK_NULL_RETURN(fp_g_variant_is_of_type = 83 (g_variant_is_of_type*) 84 dlsym(lib_handle, "g_variant_is_of_type"), NULL); 85 CHECK_NULL_RETURN(fp_g_variant_n_children = 86 (g_variant_n_children*) 87 dlsym(lib_handle, "g_variant_n_children"), NULL); 88 CHECK_NULL_RETURN(fp_g_variant_get_child_value = 89 (g_variant_get_child_value*) 90 dlsym(lib_handle, "g_variant_get_child_value"), NULL); 91 CHECK_NULL_RETURN(fp_g_variant_get_string = 92 (g_variant_get_string*) 93 dlsym(lib_handle, "g_variant_get_string"), NULL); 94 CHECK_NULL_RETURN(fp_g_variant_get_int32 = 95 (g_variant_get_int32*) 96 dlsym(lib_handle, "g_variant_get_int32"), NULL); 97 CHECK_NULL_RETURN(fp_g_variant_get_double = 98 (g_variant_get_double*) 99 dlsym(lib_handle, "g_variant_get_double"), NULL); 100 CHECK_NULL_RETURN(fp_g_variant_unref = 101 (g_variant_unref*) 102 dlsym(lib_handle, "g_variant_unref"), NULL); 103 104 fp = dlsym(lib_handle, "g_settings_schema_source_get_default"); 105 if (fp) { 106 default_schema = ((g_settings_schema_source_get_default*)fp)(); 107 } 108 if (default_schema) { 109 fp = dlsym(lib_handle, "g_settings_schema_source_ref"); 110 if (fp) { 111 ((g_settings_schema_source_ref*)fp)(default_schema); 112 } 113 } 114 schema_lookup = (g_settings_schema_source_lookup*) 115 dlsym(lib_handle, "g_settings_schema_source_lookup"); 116 } 117 118 if (!default_schema || !schema_lookup) { 119 return NULL; 120 } 121 122 schema = schema_lookup(default_schema, name, 1); 123 if (schema) { 124 if (fp_g_settings_schema_has_key(schema, key)) { 125 void *settings = fp_g_settings_new_full(schema, NULL, NULL); 126 if (settings) { 127 return fp_g_settings_get_value(settings, key); 128 } 129 } 130 } 131 return NULL; 132 } 133 134 135 static double getDesktopScale(char *output_name) { 136 double result = -1; 137 if(output_name) { 138 void *value = get_schema_value("com.ubuntu.user-interface", 139 "scale-factor"); 140 if (value) { 141 if(fp_g_variant_is_of_type(value, "a{si}")) { 142 int num = fp_g_variant_n_children(value); 143 int i = 0; 144 while (i < num) { 145 void *entry = fp_g_variant_get_child_value(value, i++); 146 if (entry) { 147 void *screen = fp_g_variant_get_child_value(entry, 0); 148 void *scale = fp_g_variant_get_child_value(entry, 1); 149 if (screen && scale) { 150 char *name = fp_g_variant_get_string(screen, NULL); 151 if (name && strcmp(name, output_name)) { 152 result = fp_g_variant_get_int32(scale) / 8.; 153 } 154 fp_g_variant_unref(screen); 155 fp_g_variant_unref(scale); 156 } 157 fp_g_variant_unref(entry); 158 } 159 if (result > 0) { 160 break; 161 } 162 } 163 } 164 fp_g_variant_unref(value); 165 } 166 if (result > 0) { 167 value = get_schema_value("com.canonical.Unity.Interface", 168 "text-scale-factor"); 169 if (value && fp_g_variant_is_of_type(value, "d")) { 170 result *= fp_g_variant_get_double(value); 171 fp_g_variant_unref(value); 172 } 173 } 174 } 175 176 if (result <= 0) { 177 void *value = get_schema_value("org.gnome.desktop.interface", 178 "text-scaling-factor"); 179 if (value && fp_g_variant_is_of_type(value, "d")) { 180 result = fp_g_variant_get_double(value); 181 fp_g_variant_unref(value); 182 } 183 } 184 185 return result; 186 187 } 188 189 static int getScale(const char *name) { 190 char *uiScale = getenv(name); 191 if (uiScale != NULL) { 192 double scale = strtod(uiScale, NULL); 193 if (scale < 1) { 194 return -1; 195 } 196 return (int) scale; 197 } 198 return -1; 199 } 200 201 double getNativeScaleFactor(char *output_name) { 202 static int scale = -2.0; 203 double native_scale = 0; 204 int gdk_scale = 0; 205 206 if (scale == -2) { 207 scale = getScale("J2D_UISCALE"); 208 } 209 210 if (scale > 0) { 211 return scale; 212 } 213 214 native_scale = getDesktopScale(output_name); 215 216 if (native_scale <= 0) { 217 native_scale = 1; 218 } 219 220 gdk_scale = getScale("GDK_SCALE"); 221 222 return gdk_scale > 0 ? native_scale * gdk_scale : native_scale; 223 }