< prev index next >

src/java.desktop/unix/native/common/awt/systemscale/systemScale.c

Print this page




   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             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                           dlsym(lib_handle, "g_settings_schema_has_key"), NULL);
  75         CHECK_NULL_RETURN(fp_g_settings_new_full =
  76                           dlsym(lib_handle, "g_settings_new_full"), NULL);
  77         CHECK_NULL_RETURN(fp_g_settings_get_value =
  78                           dlsym(lib_handle, "g_settings_get_value"), NULL);
  79         CHECK_NULL_RETURN(fp_g_variant_is_of_type =
  80                           dlsym(lib_handle, "g_variant_is_of_type"), NULL);
  81         CHECK_NULL_RETURN(fp_g_variant_n_children =
  82                           dlsym(lib_handle, "g_variant_n_children"), NULL);
  83         CHECK_NULL_RETURN(fp_g_variant_get_child_value =
  84                           dlsym(lib_handle, "g_variant_get_child_value"), NULL);
  85         CHECK_NULL_RETURN(fp_g_variant_get_string =
  86                           dlsym(lib_handle, "g_variant_get_string"), NULL);
  87         CHECK_NULL_RETURN(fp_g_variant_get_int32 =
  88                           dlsym(lib_handle, "g_variant_get_int32"), NULL);
  89         CHECK_NULL_RETURN(fp_g_variant_get_double =
  90                           dlsym(lib_handle, "g_variant_get_double"), NULL);
  91         CHECK_NULL_RETURN(fp_g_variant_unref =
  92                           dlsym(lib_handle, "g_variant_unref"), NULL);
  93 
  94         void *fp = dlsym(lib_handle, "g_settings_schema_source_get_default");
  95         if (fp) {
  96             default_schema = ((g_settings_schema_source_get_default*)fp)();
  97         }
  98         if (default_schema) {
  99             fp = dlsym(lib_handle, "g_settings_schema_source_ref");
 100             if (fp) {
 101                 ((g_settings_schema_source_ref*)fp)(default_schema);
 102             }
 103         }
 104         schema_lookup = dlsym(lib_handle, "g_settings_schema_source_lookup");
 105     }
 106 
 107     if (!default_schema || !schema_lookup) {
 108         return NULL;
 109     }
 110     void *schema = schema_lookup(default_schema, name, 1);
 111     if (schema) {
 112         if (fp_g_settings_schema_has_key(schema, key)) {
 113             void *settings = fp_g_settings_new_full(schema, NULL, NULL);
 114             if (settings) {
 115                 return fp_g_settings_get_value(settings, key);
 116             }
 117         }
 118     }
 119     return NULL;
 120 }
 121 
 122 
 123 static double getDesktopScale(char *output_name) {
 124     double result = -1;
 125     if(output_name) {
 126         void *value = get_schema_value("com.ubuntu.user-interface",
 127                                                                 "scale-factor");
 128         if (value) {
 129             if(fp_g_variant_is_of_type(value, "a{si}")) {
 130                 int num = fp_g_variant_n_children(value);
 131                 int i = 0;
 132                 while (i < num) {
 133                     void *entry = fp_g_variant_get_child_value(value, i++);
 134                     if (entry) {
 135                         void *screen = fp_g_variant_get_child_value(entry, 0);
 136                         void *scale = fp_g_variant_get_child_value(entry, 1);
 137                         if (screen && scale) {
 138                             char *name = fp_g_variant_get_string(screen, NULL);
 139                             if (name && strcmp(name, output_name)) {
 140                                 result = fp_g_variant_get_int32(scale) / 8.;
 141                             }
 142                             fp_g_variant_unref(screen);
 143                             fp_g_variant_unref(scale);
 144                         }
 145                         fp_g_variant_unref(entry);
 146                     }
 147                     if (result > 0) {
 148                         break;
 149                     }
 150                 }
 151             }
 152             fp_g_variant_unref(value);
 153         }
 154         if (result > 0) {
 155             value = get_schema_value("com.canonical.Unity.Interface",
 156                                                            "text-scale-factor");
 157             if (value && fp_g_variant_is_of_type(value, "d")) {
 158                 result *= fp_g_variant_get_double(value);
 159                 fp_g_variant_unref(value);
 160             }
 161         }
 162     }
 163 
 164     if (result <= 0) {
 165         void *value = get_schema_value("org.gnome.desktop.interface",
 166                                                          "text-scaling-factor");
 167         if (value && fp_g_variant_is_of_type(value, "d")) {
 168             result = fp_g_variant_get_double(value);
 169             fp_g_variant_unref(value);
 170         }
 171     }
 172 
 173     return result;
 174 
 175 }
 176 
 177 static int getScale(const char *name) {
 178     char *uiScale = getenv(name);
 179     if (uiScale != NULL) {
 180         double scale = strtod(uiScale, NULL);
 181         if (scale < 1) {
 182             return -1;
 183         }
 184         return (int) scale;
 185     }
 186     return -1;
 187 }
 188 
 189 double getNativeScaleFactor(char *output_name) {
 190 
 191     static int scale = -2.0;
 192 
 193     if (scale == -2) {
 194         scale = getScale("J2D_UISCALE");
 195     }
 196 
 197     if (scale > 0) {
 198         return scale;
 199     }
 200 
 201     double native_scale = getDesktopScale(output_name);
 202 
 203     if (native_scale <= 0) {
 204         native_scale = 1;
 205     }
 206 
 207     int gdk_scale = getScale("GDK_SCALE");
 208 
 209     return gdk_scale > 0 ? native_scale * gdk_scale : native_scale;
 210 }
< prev index next >