Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/lib/src/sugar-utils.c
blob: 6cc59462b96e63a029607669c553f916d2939942 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <gdk/gdkx.h>
#include <gdk/gdkscreen.h>
#include <gtk/gtksettings.h>

#include "sugar-utils.h"

/* Ported from mozilla nsDeviceContextGTK.cpp */

static gint
get_gtk_settings_dpi(void)
{
    GtkSettings *settings = gtk_settings_get_default();
    GParamSpec *spec;
    gint dpi = 0;

    spec = g_object_class_find_property(
        G_OBJECT_GET_CLASS(G_OBJECT(settings)), "gtk-xft-dpi");

    if (spec) {
        g_object_get(G_OBJECT(settings),
                     "gtk-xft-dpi", &dpi,
                     NULL);
    }

    return (int)(dpi / 1024.0 + 0.5);
}

static gint
get_xft_dpi(void)
{
    char *val = XGetDefault(GDK_DISPLAY(), "Xft", "dpi");
    if (val) {
        char *e;
        double d = strtod(val, &e);

        if (e != val)
            return (int)(d + 0.5);
    }

    return 0;
}

static int
get_dpi_from_physical_resolution(void)
{
    float screen_width_in;

    screen_width_in = (float)(gdk_screen_width_mm()) / 25.4f;

    return (int)((float)(gdk_screen_width()) / screen_width_in + 0.5);
}

gint
sugar_get_screen_dpi(void)
{
    int dpi;

    dpi = get_gtk_settings_dpi();

    if (dpi == 0) {
        dpi = get_xft_dpi();
    }

    if (dpi == 0) {
        dpi = get_dpi_from_physical_resolution();
    }

    return dpi;
}