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