1
1
/*
2
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
24
24
*/
25
25
26
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
27
#include <stdlib.h>
33
- #include <string.h>
34
-
35
- typedef void * g_settings_schema_source_get_default ();
36
- typedef void * g_settings_schema_source_ref (void * );
37
- typedef void g_settings_schema_source_unref (void * );
38
- typedef void * g_settings_schema_source_lookup (void * , char * , int );
39
- typedef int g_settings_schema_has_key (void * , char * );
40
- typedef void * g_settings_new_full (void * , void * , char * );
41
- typedef void * g_settings_get_value (void * , char * );
42
- typedef int g_variant_is_of_type (void * , char * );
43
- typedef unsigned long g_variant_n_children (void * );
44
- typedef void * g_variant_get_child_value (void * , unsigned long );
45
- typedef void g_variant_unref (void * );
46
- typedef char * g_variant_get_string (void * , unsigned long * );
47
- typedef int g_variant_get_int32 (void * );
48
- typedef double g_variant_get_double (void * );
49
-
50
- static g_settings_schema_has_key * fp_g_settings_schema_has_key ;
51
- static g_settings_new_full * fp_g_settings_new_full ;
52
- static g_settings_get_value * fp_g_settings_get_value ;
53
- static g_variant_is_of_type * fp_g_variant_is_of_type ;
54
- static g_variant_n_children * fp_g_variant_n_children ;
55
- static g_variant_get_child_value * fp_g_variant_get_child_value ;
56
- static g_variant_get_string * fp_g_variant_get_string ;
57
- static g_variant_get_int32 * fp_g_variant_get_int32 ;
58
- static g_variant_get_double * fp_g_variant_get_double ;
59
- static g_variant_unref * fp_g_variant_unref ;
60
-
61
- static 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
-
137
- static 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
28
191
29
static int getScale (const char * name ) {
192
30
char * uiScale = getenv (name );
@@ -200,10 +38,8 @@ static int getScale(const char *name) {
200
38
return -1 ;
201
39
}
202
40
203
- double getNativeScaleFactor (char * output_name ) {
41
+ double getNativeScaleFactor () {
204
42
static int scale = -2.0 ;
205
- double native_scale = 0 ;
206
- int gdk_scale = 0 ;
207
43
208
44
if (scale == -2 ) {
209
45
scale = getScale ("J2D_UISCALE" );
@@ -213,13 +49,5 @@ double getNativeScaleFactor(char *output_name) {
213
49
return scale ;
214
50
}
215
51
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 ;
52
+ return getScale ("GDK_SCALE" );
225
53
}
0 commit comments