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 <stdlib.h> 26 27 int getNativeScaleFactor() { 28 29 static int scale = -2.0; 30 31 if (scale == -2) { 32 scale = getScale("J2D_UISCALE"); 33 } 34 35 if (scale >= 1) { 36 return (int) scale; 37 } 38 return getScale("GDK_SCALE"); 39 } 40 41 int getScale(const char *name) { 42 char *uiScale = getenv(name); 43 if (uiScale != NULL) { 44 double scale = strtod(uiScale, NULL); 45 if (scale < 1) { 46 return -1; 47 } 48 return (int) scale; 49 } 50 return -1; 51 } 52 | 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 if (!initialized) { 65 initialized = 1; 66 void *lib_handle = dlopen(JNI_LIB_NAME("gio-2.0"), 67 RTLD_GLOBAL | RTLD_LAZY); 68 if (!lib_handle) { 69 lib_handle = dlopen(VERSIONED_JNI_LIB_NAME("gio-2.0", "0"), 70 RTLD_GLOBAL | RTLD_LAZY); 71 } 72 if (lib_handle == NULL) { 73 return NULL; 74 } 75 fp_g_settings_schema_has_key = 76 dlsym(lib_handle, "g_settings_schema_has_key"); 77 if (!fp_g_settings_schema_has_key) { 78 return NULL; 79 } 80 fp_g_settings_new_full = dlsym(lib_handle, "g_settings_new_full"); 81 if (!fp_g_settings_new_full) { 82 return NULL; 83 } 84 fp_g_settings_get_value = dlsym(lib_handle, "g_settings_get_value"); 85 if (!fp_g_settings_get_value) { 86 return NULL; 87 } 88 fp_g_variant_is_of_type = dlsym(lib_handle, "g_variant_is_of_type"); 89 if (!fp_g_variant_is_of_type) { 90 return NULL; 91 } 92 fp_g_variant_n_children = dlsym(lib_handle, "g_variant_n_children"); 93 if (!fp_g_variant_n_children) { 94 return NULL; 95 } 96 fp_g_variant_get_child_value = 97 dlsym(lib_handle, "g_variant_get_child_value"); 98 if (!fp_g_variant_get_child_value) { 99 return NULL; 100 } 101 fp_g_variant_get_string = dlsym(lib_handle, "g_variant_get_string"); 102 if (!fp_g_variant_get_string) { 103 return NULL; 104 } 105 fp_g_variant_get_int32 = dlsym(lib_handle, "g_variant_get_int32"); 106 if (!fp_g_variant_get_int32) { 107 return NULL; 108 } 109 fp_g_variant_get_double = dlsym(lib_handle, "g_variant_get_double"); 110 if (!fp_g_variant_get_double) { 111 return NULL; 112 } 113 fp_g_variant_unref = dlsym(lib_handle, "g_variant_unref"); 114 if (!fp_g_variant_unref) { 115 return NULL; 116 } 117 118 void *fp = dlsym(lib_handle, "g_settings_schema_source_get_default"); 119 if (fp) { 120 default_schema = ((g_settings_schema_source_get_default*)fp)(); 121 } 122 if (default_schema) { 123 fp = dlsym(lib_handle, "g_settings_schema_source_ref"); 124 if (fp) { 125 ((g_settings_schema_source_ref*)fp)(default_schema); 126 } 127 } 128 schema_lookup = dlsym(lib_handle, "g_settings_schema_source_lookup"); 129 } 130 131 if (!default_schema || !schema_lookup) { 132 return NULL; 133 } 134 void *schema = schema_lookup(default_schema, name, 1); 135 if (schema) { 136 if (fp_g_settings_schema_has_key(schema, key)) { 137 void *settings = fp_g_settings_new_full(schema, NULL, NULL); 138 if (settings) { 139 return fp_g_settings_get_value(settings, key); 140 } 141 } 142 } 143 return NULL; 144 } 145 146 147 static double getDesktopScale(char *output_name) { 148 double result = -1; 149 if(output_name) { 150 void *value = get_schema_value("com.ubuntu.user-interface", 151 "scale-factor"); 152 if (value) { 153 if(fp_g_variant_is_of_type(value, "a{si}")) { 154 int num = fp_g_variant_n_children(value); 155 int i = 0; 156 while (i < num) { 157 void *entry = fp_g_variant_get_child_value(value, i++); 158 if (entry) { 159 void *screen = fp_g_variant_get_child_value(entry, 0); 160 void *scale = fp_g_variant_get_child_value(entry, 1); 161 if (screen && scale) { 162 char *name = fp_g_variant_get_string(screen, NULL); 163 if (name && strcmp(name, output_name)) { 164 result = fp_g_variant_get_int32(scale) / 8.; 165 } 166 fp_g_variant_unref(screen); 167 fp_g_variant_unref(scale); 168 } 169 fp_g_variant_unref(entry); 170 } 171 } 172 } 173 fp_g_variant_unref(value); 174 } 175 if (result > 0) { 176 value = get_schema_value("com.canonical.Unity.Interface", 177 "text-scale-factor"); 178 if (value && fp_g_variant_is_of_type(value, "d")) { 179 result *= fp_g_variant_get_double(value); 180 fp_g_variant_unref(value); 181 } 182 } 183 } 184 185 if (result <= 0) { 186 void *value = get_schema_value("org.gnome.desktop.interface", 187 "text-scaling-factor"); 188 if (value && fp_g_variant_is_of_type(value, "d")) { 189 result = fp_g_variant_get_double(value); 190 fp_g_variant_unref(value); 191 } 192 } 193 194 return result; 195 196 } 197 198 static int getScale(const char *name) { 199 char *uiScale = getenv(name); 200 if (uiScale != NULL) { 201 double scale = strtod(uiScale, NULL); 202 if (scale < 1) { 203 return -1; 204 } 205 return (int) scale; 206 } 207 return -1; 208 } 209 210 double getNativeScaleFactor(char *output_name) { 211 212 static int scale = -2.0; 213 214 if (scale == -2) { 215 scale = getScale("J2D_UISCALE"); 216 } 217 218 if (scale > 0) { 219 return scale; 220 } 221 222 double native_scale = getDesktopScale(output_name); 223 224 if (native_scale <= 0) { 225 native_scale = 1; 226 } 227 228 int gdk_scale = getScale("GDK_SCALE"); 229 230 return gdk_scale > 0 ? native_scale * gdk_scale : native_scale; 231 } |